]> git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/cp/parser.c
Some OpenACC host_data cleanup
[thirdparty/gcc.git] / gcc / cp / parser.c
1 /* -*- C++ -*- Parser.
2 Copyright (C) 2000-2015 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
43 \f
44 /* The lexer. */
45
46 /* The cp_lexer_* routines mediate between the lexer proper (in libcpp
47 and c-lex.c) and the C++ parser. */
48
49 static cp_token eof_token =
50 {
51 CPP_EOF, RID_MAX, 0, PRAGMA_NONE, false, false, false, 0, { NULL }
52 };
53
54 /* The various kinds of non integral constant we encounter. */
55 enum non_integral_constant {
56 NIC_NONE,
57 /* floating-point literal */
58 NIC_FLOAT,
59 /* %<this%> */
60 NIC_THIS,
61 /* %<__FUNCTION__%> */
62 NIC_FUNC_NAME,
63 /* %<__PRETTY_FUNCTION__%> */
64 NIC_PRETTY_FUNC,
65 /* %<__func__%> */
66 NIC_C99_FUNC,
67 /* "%<va_arg%> */
68 NIC_VA_ARG,
69 /* a cast */
70 NIC_CAST,
71 /* %<typeid%> operator */
72 NIC_TYPEID,
73 /* non-constant compound literals */
74 NIC_NCC,
75 /* a function call */
76 NIC_FUNC_CALL,
77 /* an increment */
78 NIC_INC,
79 /* an decrement */
80 NIC_DEC,
81 /* an array reference */
82 NIC_ARRAY_REF,
83 /* %<->%> */
84 NIC_ARROW,
85 /* %<.%> */
86 NIC_POINT,
87 /* the address of a label */
88 NIC_ADDR_LABEL,
89 /* %<*%> */
90 NIC_STAR,
91 /* %<&%> */
92 NIC_ADDR,
93 /* %<++%> */
94 NIC_PREINCREMENT,
95 /* %<--%> */
96 NIC_PREDECREMENT,
97 /* %<new%> */
98 NIC_NEW,
99 /* %<delete%> */
100 NIC_DEL,
101 /* calls to overloaded operators */
102 NIC_OVERLOADED,
103 /* an assignment */
104 NIC_ASSIGNMENT,
105 /* a comma operator */
106 NIC_COMMA,
107 /* a call to a constructor */
108 NIC_CONSTRUCTOR,
109 /* a transaction expression */
110 NIC_TRANSACTION
111 };
112
113 /* The various kinds of errors about name-lookup failing. */
114 enum name_lookup_error {
115 /* NULL */
116 NLE_NULL,
117 /* is not a type */
118 NLE_TYPE,
119 /* is not a class or namespace */
120 NLE_CXX98,
121 /* is not a class, namespace, or enumeration */
122 NLE_NOT_CXX98
123 };
124
125 /* The various kinds of required token */
126 enum required_token {
127 RT_NONE,
128 RT_SEMICOLON, /* ';' */
129 RT_OPEN_PAREN, /* '(' */
130 RT_CLOSE_BRACE, /* '}' */
131 RT_OPEN_BRACE, /* '{' */
132 RT_CLOSE_SQUARE, /* ']' */
133 RT_OPEN_SQUARE, /* '[' */
134 RT_COMMA, /* ',' */
135 RT_SCOPE, /* '::' */
136 RT_LESS, /* '<' */
137 RT_GREATER, /* '>' */
138 RT_EQ, /* '=' */
139 RT_ELLIPSIS, /* '...' */
140 RT_MULT, /* '*' */
141 RT_COMPL, /* '~' */
142 RT_COLON, /* ':' */
143 RT_COLON_SCOPE, /* ':' or '::' */
144 RT_CLOSE_PAREN, /* ')' */
145 RT_COMMA_CLOSE_PAREN, /* ',' or ')' */
146 RT_PRAGMA_EOL, /* end of line */
147 RT_NAME, /* identifier */
148
149 /* The type is CPP_KEYWORD */
150 RT_NEW, /* new */
151 RT_DELETE, /* delete */
152 RT_RETURN, /* return */
153 RT_WHILE, /* while */
154 RT_EXTERN, /* extern */
155 RT_STATIC_ASSERT, /* static_assert */
156 RT_DECLTYPE, /* decltype */
157 RT_OPERATOR, /* operator */
158 RT_CLASS, /* class */
159 RT_TEMPLATE, /* template */
160 RT_NAMESPACE, /* namespace */
161 RT_USING, /* using */
162 RT_ASM, /* asm */
163 RT_TRY, /* try */
164 RT_CATCH, /* catch */
165 RT_THROW, /* throw */
166 RT_LABEL, /* __label__ */
167 RT_AT_TRY, /* @try */
168 RT_AT_SYNCHRONIZED, /* @synchronized */
169 RT_AT_THROW, /* @throw */
170
171 RT_SELECT, /* selection-statement */
172 RT_INTERATION, /* iteration-statement */
173 RT_JUMP, /* jump-statement */
174 RT_CLASS_KEY, /* class-key */
175 RT_CLASS_TYPENAME_TEMPLATE, /* class, typename, or template */
176 RT_TRANSACTION_ATOMIC, /* __transaction_atomic */
177 RT_TRANSACTION_RELAXED, /* __transaction_relaxed */
178 RT_TRANSACTION_CANCEL /* __transaction_cancel */
179 };
180
181 /* Prototypes. */
182
183 static cp_lexer *cp_lexer_new_main
184 (void);
185 static cp_lexer *cp_lexer_new_from_tokens
186 (cp_token_cache *tokens);
187 static void cp_lexer_destroy
188 (cp_lexer *);
189 static int cp_lexer_saving_tokens
190 (const cp_lexer *);
191 static cp_token *cp_lexer_token_at
192 (cp_lexer *, cp_token_position);
193 static void cp_lexer_get_preprocessor_token
194 (cp_lexer *, cp_token *);
195 static inline cp_token *cp_lexer_peek_token
196 (cp_lexer *);
197 static cp_token *cp_lexer_peek_nth_token
198 (cp_lexer *, size_t);
199 static inline bool cp_lexer_next_token_is
200 (cp_lexer *, enum cpp_ttype);
201 static bool cp_lexer_next_token_is_not
202 (cp_lexer *, enum cpp_ttype);
203 static bool cp_lexer_next_token_is_keyword
204 (cp_lexer *, enum rid);
205 static cp_token *cp_lexer_consume_token
206 (cp_lexer *);
207 static void cp_lexer_purge_token
208 (cp_lexer *);
209 static void cp_lexer_purge_tokens_after
210 (cp_lexer *, cp_token_position);
211 static void cp_lexer_save_tokens
212 (cp_lexer *);
213 static void cp_lexer_commit_tokens
214 (cp_lexer *);
215 static void cp_lexer_rollback_tokens
216 (cp_lexer *);
217 static void cp_lexer_print_token
218 (FILE *, cp_token *);
219 static inline bool cp_lexer_debugging_p
220 (cp_lexer *);
221 static void cp_lexer_start_debugging
222 (cp_lexer *) ATTRIBUTE_UNUSED;
223 static void cp_lexer_stop_debugging
224 (cp_lexer *) ATTRIBUTE_UNUSED;
225
226 static cp_token_cache *cp_token_cache_new
227 (cp_token *, cp_token *);
228
229 static void cp_parser_initial_pragma
230 (cp_token *);
231
232 static tree cp_literal_operator_id
233 (const char *);
234
235 static void cp_parser_cilk_simd
236 (cp_parser *, cp_token *);
237 static tree cp_parser_cilk_for
238 (cp_parser *, tree);
239 static bool cp_parser_omp_declare_reduction_exprs
240 (tree, cp_parser *);
241 static tree cp_parser_cilk_simd_vectorlength
242 (cp_parser *, tree, bool);
243 static void cp_finalize_oacc_routine
244 (cp_parser *, tree, bool);
245
246 /* Manifest constants. */
247 #define CP_LEXER_BUFFER_SIZE ((256 * 1024) / sizeof (cp_token))
248 #define CP_SAVED_TOKEN_STACK 5
249
250 /* Variables. */
251
252 /* The stream to which debugging output should be written. */
253 static FILE *cp_lexer_debug_stream;
254
255 /* Nonzero if we are parsing an unevaluated operand: an operand to
256 sizeof, typeof, or alignof. */
257 int cp_unevaluated_operand;
258
259 /* Dump up to NUM tokens in BUFFER to FILE starting with token
260 START_TOKEN. If START_TOKEN is NULL, the dump starts with the
261 first token in BUFFER. If NUM is 0, dump all the tokens. If
262 CURR_TOKEN is set and it is one of the tokens in BUFFER, it will be
263 highlighted by surrounding it in [[ ]]. */
264
265 static void
266 cp_lexer_dump_tokens (FILE *file, vec<cp_token, va_gc> *buffer,
267 cp_token *start_token, unsigned num,
268 cp_token *curr_token)
269 {
270 unsigned i, nprinted;
271 cp_token *token;
272 bool do_print;
273
274 fprintf (file, "%u tokens\n", vec_safe_length (buffer));
275
276 if (buffer == NULL)
277 return;
278
279 if (num == 0)
280 num = buffer->length ();
281
282 if (start_token == NULL)
283 start_token = buffer->address ();
284
285 if (start_token > buffer->address ())
286 {
287 cp_lexer_print_token (file, &(*buffer)[0]);
288 fprintf (file, " ... ");
289 }
290
291 do_print = false;
292 nprinted = 0;
293 for (i = 0; buffer->iterate (i, &token) && nprinted < num; i++)
294 {
295 if (token == start_token)
296 do_print = true;
297
298 if (!do_print)
299 continue;
300
301 nprinted++;
302 if (token == curr_token)
303 fprintf (file, "[[");
304
305 cp_lexer_print_token (file, token);
306
307 if (token == curr_token)
308 fprintf (file, "]]");
309
310 switch (token->type)
311 {
312 case CPP_SEMICOLON:
313 case CPP_OPEN_BRACE:
314 case CPP_CLOSE_BRACE:
315 case CPP_EOF:
316 fputc ('\n', file);
317 break;
318
319 default:
320 fputc (' ', file);
321 }
322 }
323
324 if (i == num && i < buffer->length ())
325 {
326 fprintf (file, " ... ");
327 cp_lexer_print_token (file, &buffer->last ());
328 }
329
330 fprintf (file, "\n");
331 }
332
333
334 /* Dump all tokens in BUFFER to stderr. */
335
336 void
337 cp_lexer_debug_tokens (vec<cp_token, va_gc> *buffer)
338 {
339 cp_lexer_dump_tokens (stderr, buffer, NULL, 0, NULL);
340 }
341
342 DEBUG_FUNCTION void
343 debug (vec<cp_token, va_gc> &ref)
344 {
345 cp_lexer_dump_tokens (stderr, &ref, NULL, 0, NULL);
346 }
347
348 DEBUG_FUNCTION void
349 debug (vec<cp_token, va_gc> *ptr)
350 {
351 if (ptr)
352 debug (*ptr);
353 else
354 fprintf (stderr, "<nil>\n");
355 }
356
357
358 /* Dump the cp_parser tree field T to FILE if T is non-NULL. DESC is the
359 description for T. */
360
361 static void
362 cp_debug_print_tree_if_set (FILE *file, const char *desc, tree t)
363 {
364 if (t)
365 {
366 fprintf (file, "%s: ", desc);
367 print_node_brief (file, "", t, 0);
368 }
369 }
370
371
372 /* Dump parser context C to FILE. */
373
374 static void
375 cp_debug_print_context (FILE *file, cp_parser_context *c)
376 {
377 const char *status_s[] = { "OK", "ERROR", "COMMITTED" };
378 fprintf (file, "{ status = %s, scope = ", status_s[c->status]);
379 print_node_brief (file, "", c->object_type, 0);
380 fprintf (file, "}\n");
381 }
382
383
384 /* Print the stack of parsing contexts to FILE starting with FIRST. */
385
386 static void
387 cp_debug_print_context_stack (FILE *file, cp_parser_context *first)
388 {
389 unsigned i;
390 cp_parser_context *c;
391
392 fprintf (file, "Parsing context stack:\n");
393 for (i = 0, c = first; c; c = c->next, i++)
394 {
395 fprintf (file, "\t#%u: ", i);
396 cp_debug_print_context (file, c);
397 }
398 }
399
400
401 /* Print the value of FLAG to FILE. DESC is a string describing the flag. */
402
403 static void
404 cp_debug_print_flag (FILE *file, const char *desc, bool flag)
405 {
406 if (flag)
407 fprintf (file, "%s: true\n", desc);
408 }
409
410
411 /* Print an unparsed function entry UF to FILE. */
412
413 static void
414 cp_debug_print_unparsed_function (FILE *file, cp_unparsed_functions_entry *uf)
415 {
416 unsigned i;
417 cp_default_arg_entry *default_arg_fn;
418 tree fn;
419
420 fprintf (file, "\tFunctions with default args:\n");
421 for (i = 0;
422 vec_safe_iterate (uf->funs_with_default_args, i, &default_arg_fn);
423 i++)
424 {
425 fprintf (file, "\t\tClass type: ");
426 print_node_brief (file, "", default_arg_fn->class_type, 0);
427 fprintf (file, "\t\tDeclaration: ");
428 print_node_brief (file, "", default_arg_fn->decl, 0);
429 fprintf (file, "\n");
430 }
431
432 fprintf (file, "\n\tFunctions with definitions that require "
433 "post-processing\n\t\t");
434 for (i = 0; vec_safe_iterate (uf->funs_with_definitions, i, &fn); i++)
435 {
436 print_node_brief (file, "", fn, 0);
437 fprintf (file, " ");
438 }
439 fprintf (file, "\n");
440
441 fprintf (file, "\n\tNon-static data members with initializers that require "
442 "post-processing\n\t\t");
443 for (i = 0; vec_safe_iterate (uf->nsdmis, i, &fn); i++)
444 {
445 print_node_brief (file, "", fn, 0);
446 fprintf (file, " ");
447 }
448 fprintf (file, "\n");
449 }
450
451
452 /* Print the stack of unparsed member functions S to FILE. */
453
454 static void
455 cp_debug_print_unparsed_queues (FILE *file,
456 vec<cp_unparsed_functions_entry, va_gc> *s)
457 {
458 unsigned i;
459 cp_unparsed_functions_entry *uf;
460
461 fprintf (file, "Unparsed functions\n");
462 for (i = 0; vec_safe_iterate (s, i, &uf); i++)
463 {
464 fprintf (file, "#%u:\n", i);
465 cp_debug_print_unparsed_function (file, uf);
466 }
467 }
468
469
470 /* Dump the tokens in a window of size WINDOW_SIZE around the next_token for
471 the given PARSER. If FILE is NULL, the output is printed on stderr. */
472
473 static void
474 cp_debug_parser_tokens (FILE *file, cp_parser *parser, int window_size)
475 {
476 cp_token *next_token, *first_token, *start_token;
477
478 if (file == NULL)
479 file = stderr;
480
481 next_token = parser->lexer->next_token;
482 first_token = parser->lexer->buffer->address ();
483 start_token = (next_token > first_token + window_size / 2)
484 ? next_token - window_size / 2
485 : first_token;
486 cp_lexer_dump_tokens (file, parser->lexer->buffer, start_token, window_size,
487 next_token);
488 }
489
490
491 /* Dump debugging information for the given PARSER. If FILE is NULL,
492 the output is printed on stderr. */
493
494 void
495 cp_debug_parser (FILE *file, cp_parser *parser)
496 {
497 const size_t window_size = 20;
498 cp_token *token;
499 expanded_location eloc;
500
501 if (file == NULL)
502 file = stderr;
503
504 fprintf (file, "Parser state\n\n");
505 fprintf (file, "Number of tokens: %u\n",
506 vec_safe_length (parser->lexer->buffer));
507 cp_debug_print_tree_if_set (file, "Lookup scope", parser->scope);
508 cp_debug_print_tree_if_set (file, "Object scope",
509 parser->object_scope);
510 cp_debug_print_tree_if_set (file, "Qualifying scope",
511 parser->qualifying_scope);
512 cp_debug_print_context_stack (file, parser->context);
513 cp_debug_print_flag (file, "Allow GNU extensions",
514 parser->allow_gnu_extensions_p);
515 cp_debug_print_flag (file, "'>' token is greater-than",
516 parser->greater_than_is_operator_p);
517 cp_debug_print_flag (file, "Default args allowed in current "
518 "parameter list", parser->default_arg_ok_p);
519 cp_debug_print_flag (file, "Parsing integral constant-expression",
520 parser->integral_constant_expression_p);
521 cp_debug_print_flag (file, "Allow non-constant expression in current "
522 "constant-expression",
523 parser->allow_non_integral_constant_expression_p);
524 cp_debug_print_flag (file, "Seen non-constant expression",
525 parser->non_integral_constant_expression_p);
526 cp_debug_print_flag (file, "Local names and 'this' forbidden in "
527 "current context",
528 parser->local_variables_forbidden_p);
529 cp_debug_print_flag (file, "In unbraced linkage specification",
530 parser->in_unbraced_linkage_specification_p);
531 cp_debug_print_flag (file, "Parsing a declarator",
532 parser->in_declarator_p);
533 cp_debug_print_flag (file, "In template argument list",
534 parser->in_template_argument_list_p);
535 cp_debug_print_flag (file, "Parsing an iteration statement",
536 parser->in_statement & IN_ITERATION_STMT);
537 cp_debug_print_flag (file, "Parsing a switch statement",
538 parser->in_statement & IN_SWITCH_STMT);
539 cp_debug_print_flag (file, "Parsing a structured OpenMP block",
540 parser->in_statement & IN_OMP_BLOCK);
541 cp_debug_print_flag (file, "Parsing a Cilk Plus for loop",
542 parser->in_statement & IN_CILK_SIMD_FOR);
543 cp_debug_print_flag (file, "Parsing a an OpenMP loop",
544 parser->in_statement & IN_OMP_FOR);
545 cp_debug_print_flag (file, "Parsing an if statement",
546 parser->in_statement & IN_IF_STMT);
547 cp_debug_print_flag (file, "Parsing a type-id in an expression "
548 "context", parser->in_type_id_in_expr_p);
549 cp_debug_print_flag (file, "Declarations are implicitly extern \"C\"",
550 parser->implicit_extern_c);
551 cp_debug_print_flag (file, "String expressions should be translated "
552 "to execution character set",
553 parser->translate_strings_p);
554 cp_debug_print_flag (file, "Parsing function body outside of a "
555 "local class", parser->in_function_body);
556 cp_debug_print_flag (file, "Auto correct a colon to a scope operator",
557 parser->colon_corrects_to_scope_p);
558 cp_debug_print_flag (file, "Colon doesn't start a class definition",
559 parser->colon_doesnt_start_class_def_p);
560 if (parser->type_definition_forbidden_message)
561 fprintf (file, "Error message for forbidden type definitions: %s\n",
562 parser->type_definition_forbidden_message);
563 cp_debug_print_unparsed_queues (file, parser->unparsed_queues);
564 fprintf (file, "Number of class definitions in progress: %u\n",
565 parser->num_classes_being_defined);
566 fprintf (file, "Number of template parameter lists for the current "
567 "declaration: %u\n", parser->num_template_parameter_lists);
568 cp_debug_parser_tokens (file, parser, window_size);
569 token = parser->lexer->next_token;
570 fprintf (file, "Next token to parse:\n");
571 fprintf (file, "\tToken: ");
572 cp_lexer_print_token (file, token);
573 eloc = expand_location (token->location);
574 fprintf (file, "\n\tFile: %s\n", eloc.file);
575 fprintf (file, "\tLine: %d\n", eloc.line);
576 fprintf (file, "\tColumn: %d\n", eloc.column);
577 }
578
579 DEBUG_FUNCTION void
580 debug (cp_parser &ref)
581 {
582 cp_debug_parser (stderr, &ref);
583 }
584
585 DEBUG_FUNCTION void
586 debug (cp_parser *ptr)
587 {
588 if (ptr)
589 debug (*ptr);
590 else
591 fprintf (stderr, "<nil>\n");
592 }
593
594 /* Allocate memory for a new lexer object and return it. */
595
596 static cp_lexer *
597 cp_lexer_alloc (void)
598 {
599 cp_lexer *lexer;
600
601 c_common_no_more_pch ();
602
603 /* Allocate the memory. */
604 lexer = ggc_cleared_alloc<cp_lexer> ();
605
606 /* Initially we are not debugging. */
607 lexer->debugging_p = false;
608
609 lexer->saved_tokens.create (CP_SAVED_TOKEN_STACK);
610
611 /* Create the buffer. */
612 vec_alloc (lexer->buffer, CP_LEXER_BUFFER_SIZE);
613
614 return lexer;
615 }
616
617
618 /* Create a new main C++ lexer, the lexer that gets tokens from the
619 preprocessor. */
620
621 static cp_lexer *
622 cp_lexer_new_main (void)
623 {
624 cp_lexer *lexer;
625 cp_token token;
626
627 /* It's possible that parsing the first pragma will load a PCH file,
628 which is a GC collection point. So we have to do that before
629 allocating any memory. */
630 cp_parser_initial_pragma (&token);
631
632 lexer = cp_lexer_alloc ();
633
634 /* Put the first token in the buffer. */
635 lexer->buffer->quick_push (token);
636
637 /* Get the remaining tokens from the preprocessor. */
638 while (token.type != CPP_EOF)
639 {
640 cp_lexer_get_preprocessor_token (lexer, &token);
641 vec_safe_push (lexer->buffer, token);
642 }
643
644 lexer->last_token = lexer->buffer->address ()
645 + lexer->buffer->length ()
646 - 1;
647 lexer->next_token = lexer->buffer->length ()
648 ? lexer->buffer->address ()
649 : &eof_token;
650
651 /* Subsequent preprocessor diagnostics should use compiler
652 diagnostic functions to get the compiler source location. */
653 done_lexing = true;
654
655 gcc_assert (!lexer->next_token->purged_p);
656 return lexer;
657 }
658
659 /* Create a new lexer whose token stream is primed with the tokens in
660 CACHE. When these tokens are exhausted, no new tokens will be read. */
661
662 static cp_lexer *
663 cp_lexer_new_from_tokens (cp_token_cache *cache)
664 {
665 cp_token *first = cache->first;
666 cp_token *last = cache->last;
667 cp_lexer *lexer = ggc_cleared_alloc<cp_lexer> ();
668
669 /* We do not own the buffer. */
670 lexer->buffer = NULL;
671 lexer->next_token = first == last ? &eof_token : first;
672 lexer->last_token = last;
673
674 lexer->saved_tokens.create (CP_SAVED_TOKEN_STACK);
675
676 /* Initially we are not debugging. */
677 lexer->debugging_p = false;
678
679 gcc_assert (!lexer->next_token->purged_p);
680 return lexer;
681 }
682
683 /* Frees all resources associated with LEXER. */
684
685 static void
686 cp_lexer_destroy (cp_lexer *lexer)
687 {
688 vec_free (lexer->buffer);
689 lexer->saved_tokens.release ();
690 ggc_free (lexer);
691 }
692
693 /* Returns nonzero if debugging information should be output. */
694
695 static inline bool
696 cp_lexer_debugging_p (cp_lexer *lexer)
697 {
698 return lexer->debugging_p;
699 }
700
701
702 static inline cp_token_position
703 cp_lexer_token_position (cp_lexer *lexer, bool previous_p)
704 {
705 gcc_assert (!previous_p || lexer->next_token != &eof_token);
706
707 return lexer->next_token - previous_p;
708 }
709
710 static inline cp_token *
711 cp_lexer_token_at (cp_lexer * /*lexer*/, cp_token_position pos)
712 {
713 return pos;
714 }
715
716 static inline void
717 cp_lexer_set_token_position (cp_lexer *lexer, cp_token_position pos)
718 {
719 lexer->next_token = cp_lexer_token_at (lexer, pos);
720 }
721
722 static inline cp_token_position
723 cp_lexer_previous_token_position (cp_lexer *lexer)
724 {
725 if (lexer->next_token == &eof_token)
726 return lexer->last_token - 1;
727 else
728 return cp_lexer_token_position (lexer, true);
729 }
730
731 static inline cp_token *
732 cp_lexer_previous_token (cp_lexer *lexer)
733 {
734 cp_token_position tp = cp_lexer_previous_token_position (lexer);
735
736 return cp_lexer_token_at (lexer, tp);
737 }
738
739 /* nonzero if we are presently saving tokens. */
740
741 static inline int
742 cp_lexer_saving_tokens (const cp_lexer* lexer)
743 {
744 return lexer->saved_tokens.length () != 0;
745 }
746
747 /* Store the next token from the preprocessor in *TOKEN. Return true
748 if we reach EOF. If LEXER is NULL, assume we are handling an
749 initial #pragma pch_preprocess, and thus want the lexer to return
750 processed strings. */
751
752 static void
753 cp_lexer_get_preprocessor_token (cp_lexer *lexer, cp_token *token)
754 {
755 static int is_extern_c = 0;
756
757 /* Get a new token from the preprocessor. */
758 token->type
759 = c_lex_with_flags (&token->u.value, &token->location, &token->flags,
760 lexer == NULL ? 0 : C_LEX_STRING_NO_JOIN);
761 token->keyword = RID_MAX;
762 token->pragma_kind = PRAGMA_NONE;
763 token->purged_p = false;
764 token->error_reported = false;
765
766 /* On some systems, some header files are surrounded by an
767 implicit extern "C" block. Set a flag in the token if it
768 comes from such a header. */
769 is_extern_c += pending_lang_change;
770 pending_lang_change = 0;
771 token->implicit_extern_c = is_extern_c > 0;
772
773 /* Check to see if this token is a keyword. */
774 if (token->type == CPP_NAME)
775 {
776 if (C_IS_RESERVED_WORD (token->u.value))
777 {
778 /* Mark this token as a keyword. */
779 token->type = CPP_KEYWORD;
780 /* Record which keyword. */
781 token->keyword = C_RID_CODE (token->u.value);
782 }
783 else
784 {
785 if (warn_cxx11_compat
786 && C_RID_CODE (token->u.value) >= RID_FIRST_CXX11
787 && C_RID_CODE (token->u.value) <= RID_LAST_CXX11)
788 {
789 /* Warn about the C++0x keyword (but still treat it as
790 an identifier). */
791 warning (OPT_Wc__11_compat,
792 "identifier %qE is a keyword in C++11",
793 token->u.value);
794
795 /* Clear out the C_RID_CODE so we don't warn about this
796 particular identifier-turned-keyword again. */
797 C_SET_RID_CODE (token->u.value, RID_MAX);
798 }
799
800 token->keyword = RID_MAX;
801 }
802 }
803 else if (token->type == CPP_AT_NAME)
804 {
805 /* This only happens in Objective-C++; it must be a keyword. */
806 token->type = CPP_KEYWORD;
807 switch (C_RID_CODE (token->u.value))
808 {
809 /* Replace 'class' with '@class', 'private' with '@private',
810 etc. This prevents confusion with the C++ keyword
811 'class', and makes the tokens consistent with other
812 Objective-C 'AT' keywords. For example '@class' is
813 reported as RID_AT_CLASS which is consistent with
814 '@synchronized', which is reported as
815 RID_AT_SYNCHRONIZED.
816 */
817 case RID_CLASS: token->keyword = RID_AT_CLASS; break;
818 case RID_PRIVATE: token->keyword = RID_AT_PRIVATE; break;
819 case RID_PROTECTED: token->keyword = RID_AT_PROTECTED; break;
820 case RID_PUBLIC: token->keyword = RID_AT_PUBLIC; break;
821 case RID_THROW: token->keyword = RID_AT_THROW; break;
822 case RID_TRY: token->keyword = RID_AT_TRY; break;
823 case RID_CATCH: token->keyword = RID_AT_CATCH; break;
824 case RID_SYNCHRONIZED: token->keyword = RID_AT_SYNCHRONIZED; break;
825 default: token->keyword = C_RID_CODE (token->u.value);
826 }
827 }
828 else if (token->type == CPP_PRAGMA)
829 {
830 /* We smuggled the cpp_token->u.pragma value in an INTEGER_CST. */
831 token->pragma_kind = ((enum pragma_kind)
832 TREE_INT_CST_LOW (token->u.value));
833 token->u.value = NULL_TREE;
834 }
835 }
836
837 /* Update the globals input_location and the input file stack from TOKEN. */
838 static inline void
839 cp_lexer_set_source_position_from_token (cp_token *token)
840 {
841 if (token->type != CPP_EOF)
842 {
843 input_location = token->location;
844 }
845 }
846
847 /* Update the globals input_location and the input file stack from LEXER. */
848 static inline void
849 cp_lexer_set_source_position (cp_lexer *lexer)
850 {
851 cp_token *token = cp_lexer_peek_token (lexer);
852 cp_lexer_set_source_position_from_token (token);
853 }
854
855 /* Return a pointer to the next token in the token stream, but do not
856 consume it. */
857
858 static inline cp_token *
859 cp_lexer_peek_token (cp_lexer *lexer)
860 {
861 if (cp_lexer_debugging_p (lexer))
862 {
863 fputs ("cp_lexer: peeking at token: ", cp_lexer_debug_stream);
864 cp_lexer_print_token (cp_lexer_debug_stream, lexer->next_token);
865 putc ('\n', cp_lexer_debug_stream);
866 }
867 return lexer->next_token;
868 }
869
870 /* Return true if the next token has the indicated TYPE. */
871
872 static inline bool
873 cp_lexer_next_token_is (cp_lexer* lexer, enum cpp_ttype type)
874 {
875 return cp_lexer_peek_token (lexer)->type == type;
876 }
877
878 /* Return true if the next token does not have the indicated TYPE. */
879
880 static inline bool
881 cp_lexer_next_token_is_not (cp_lexer* lexer, enum cpp_ttype type)
882 {
883 return !cp_lexer_next_token_is (lexer, type);
884 }
885
886 /* Return true if the next token is the indicated KEYWORD. */
887
888 static inline bool
889 cp_lexer_next_token_is_keyword (cp_lexer* lexer, enum rid keyword)
890 {
891 return cp_lexer_peek_token (lexer)->keyword == keyword;
892 }
893
894 static inline bool
895 cp_lexer_nth_token_is (cp_lexer* lexer, size_t n, enum cpp_ttype type)
896 {
897 return cp_lexer_peek_nth_token (lexer, n)->type == type;
898 }
899
900 static inline bool
901 cp_lexer_nth_token_is_keyword (cp_lexer* lexer, size_t n, enum rid keyword)
902 {
903 return cp_lexer_peek_nth_token (lexer, n)->keyword == keyword;
904 }
905
906 /* Return true if the next token is not the indicated KEYWORD. */
907
908 static inline bool
909 cp_lexer_next_token_is_not_keyword (cp_lexer* lexer, enum rid keyword)
910 {
911 return cp_lexer_peek_token (lexer)->keyword != keyword;
912 }
913
914 /* Return true if the next token is a keyword for a decl-specifier. */
915
916 static bool
917 cp_lexer_next_token_is_decl_specifier_keyword (cp_lexer *lexer)
918 {
919 cp_token *token;
920
921 token = cp_lexer_peek_token (lexer);
922 switch (token->keyword)
923 {
924 /* auto specifier: storage-class-specifier in C++,
925 simple-type-specifier in C++0x. */
926 case RID_AUTO:
927 /* Storage classes. */
928 case RID_REGISTER:
929 case RID_STATIC:
930 case RID_EXTERN:
931 case RID_MUTABLE:
932 case RID_THREAD:
933 /* Elaborated type specifiers. */
934 case RID_ENUM:
935 case RID_CLASS:
936 case RID_STRUCT:
937 case RID_UNION:
938 case RID_TYPENAME:
939 /* Simple type specifiers. */
940 case RID_CHAR:
941 case RID_CHAR16:
942 case RID_CHAR32:
943 case RID_WCHAR:
944 case RID_BOOL:
945 case RID_SHORT:
946 case RID_INT:
947 case RID_LONG:
948 case RID_SIGNED:
949 case RID_UNSIGNED:
950 case RID_FLOAT:
951 case RID_DOUBLE:
952 case RID_VOID:
953 /* GNU extensions. */
954 case RID_ATTRIBUTE:
955 case RID_TYPEOF:
956 /* C++0x extensions. */
957 case RID_DECLTYPE:
958 case RID_UNDERLYING_TYPE:
959 return true;
960
961 default:
962 if (token->keyword >= RID_FIRST_INT_N
963 && token->keyword < RID_FIRST_INT_N + NUM_INT_N_ENTS
964 && int_n_enabled_p[token->keyword - RID_FIRST_INT_N])
965 return true;
966 return false;
967 }
968 }
969
970 /* Returns TRUE iff the token T begins a decltype type. */
971
972 static bool
973 token_is_decltype (cp_token *t)
974 {
975 return (t->keyword == RID_DECLTYPE
976 || t->type == CPP_DECLTYPE);
977 }
978
979 /* Returns TRUE iff the next token begins a decltype type. */
980
981 static bool
982 cp_lexer_next_token_is_decltype (cp_lexer *lexer)
983 {
984 cp_token *t = cp_lexer_peek_token (lexer);
985 return token_is_decltype (t);
986 }
987
988 /* Return a pointer to the Nth token in the token stream. If N is 1,
989 then this is precisely equivalent to cp_lexer_peek_token (except
990 that it is not inline). One would like to disallow that case, but
991 there is one case (cp_parser_nth_token_starts_template_id) where
992 the caller passes a variable for N and it might be 1. */
993
994 static cp_token *
995 cp_lexer_peek_nth_token (cp_lexer* lexer, size_t n)
996 {
997 cp_token *token;
998
999 /* N is 1-based, not zero-based. */
1000 gcc_assert (n > 0);
1001
1002 if (cp_lexer_debugging_p (lexer))
1003 fprintf (cp_lexer_debug_stream,
1004 "cp_lexer: peeking ahead %ld at token: ", (long)n);
1005
1006 --n;
1007 token = lexer->next_token;
1008 gcc_assert (!n || token != &eof_token);
1009 while (n != 0)
1010 {
1011 ++token;
1012 if (token == lexer->last_token)
1013 {
1014 token = &eof_token;
1015 break;
1016 }
1017
1018 if (!token->purged_p)
1019 --n;
1020 }
1021
1022 if (cp_lexer_debugging_p (lexer))
1023 {
1024 cp_lexer_print_token (cp_lexer_debug_stream, token);
1025 putc ('\n', cp_lexer_debug_stream);
1026 }
1027
1028 return token;
1029 }
1030
1031 /* Return the next token, and advance the lexer's next_token pointer
1032 to point to the next non-purged token. */
1033
1034 static cp_token *
1035 cp_lexer_consume_token (cp_lexer* lexer)
1036 {
1037 cp_token *token = lexer->next_token;
1038
1039 gcc_assert (token != &eof_token);
1040 gcc_assert (!lexer->in_pragma || token->type != CPP_PRAGMA_EOL);
1041
1042 do
1043 {
1044 lexer->next_token++;
1045 if (lexer->next_token == lexer->last_token)
1046 {
1047 lexer->next_token = &eof_token;
1048 break;
1049 }
1050
1051 }
1052 while (lexer->next_token->purged_p);
1053
1054 cp_lexer_set_source_position_from_token (token);
1055
1056 /* Provide debugging output. */
1057 if (cp_lexer_debugging_p (lexer))
1058 {
1059 fputs ("cp_lexer: consuming token: ", cp_lexer_debug_stream);
1060 cp_lexer_print_token (cp_lexer_debug_stream, token);
1061 putc ('\n', cp_lexer_debug_stream);
1062 }
1063
1064 return token;
1065 }
1066
1067 /* Permanently remove the next token from the token stream, and
1068 advance the next_token pointer to refer to the next non-purged
1069 token. */
1070
1071 static void
1072 cp_lexer_purge_token (cp_lexer *lexer)
1073 {
1074 cp_token *tok = lexer->next_token;
1075
1076 gcc_assert (tok != &eof_token);
1077 tok->purged_p = true;
1078 tok->location = UNKNOWN_LOCATION;
1079 tok->u.value = NULL_TREE;
1080 tok->keyword = RID_MAX;
1081
1082 do
1083 {
1084 tok++;
1085 if (tok == lexer->last_token)
1086 {
1087 tok = &eof_token;
1088 break;
1089 }
1090 }
1091 while (tok->purged_p);
1092 lexer->next_token = tok;
1093 }
1094
1095 /* Permanently remove all tokens after TOK, up to, but not
1096 including, the token that will be returned next by
1097 cp_lexer_peek_token. */
1098
1099 static void
1100 cp_lexer_purge_tokens_after (cp_lexer *lexer, cp_token *tok)
1101 {
1102 cp_token *peek = lexer->next_token;
1103
1104 if (peek == &eof_token)
1105 peek = lexer->last_token;
1106
1107 gcc_assert (tok < peek);
1108
1109 for ( tok += 1; tok != peek; tok += 1)
1110 {
1111 tok->purged_p = true;
1112 tok->location = UNKNOWN_LOCATION;
1113 tok->u.value = NULL_TREE;
1114 tok->keyword = RID_MAX;
1115 }
1116 }
1117
1118 /* Begin saving tokens. All tokens consumed after this point will be
1119 preserved. */
1120
1121 static void
1122 cp_lexer_save_tokens (cp_lexer* lexer)
1123 {
1124 /* Provide debugging output. */
1125 if (cp_lexer_debugging_p (lexer))
1126 fprintf (cp_lexer_debug_stream, "cp_lexer: saving tokens\n");
1127
1128 lexer->saved_tokens.safe_push (lexer->next_token);
1129 }
1130
1131 /* Commit to the portion of the token stream most recently saved. */
1132
1133 static void
1134 cp_lexer_commit_tokens (cp_lexer* lexer)
1135 {
1136 /* Provide debugging output. */
1137 if (cp_lexer_debugging_p (lexer))
1138 fprintf (cp_lexer_debug_stream, "cp_lexer: committing tokens\n");
1139
1140 lexer->saved_tokens.pop ();
1141 }
1142
1143 /* Return all tokens saved since the last call to cp_lexer_save_tokens
1144 to the token stream. Stop saving tokens. */
1145
1146 static void
1147 cp_lexer_rollback_tokens (cp_lexer* lexer)
1148 {
1149 /* Provide debugging output. */
1150 if (cp_lexer_debugging_p (lexer))
1151 fprintf (cp_lexer_debug_stream, "cp_lexer: restoring tokens\n");
1152
1153 lexer->next_token = lexer->saved_tokens.pop ();
1154 }
1155
1156 /* RAII wrapper around the above functions, with sanity checking. Creating
1157 a variable saves tokens, which are committed when the variable is
1158 destroyed unless they are explicitly rolled back by calling the rollback
1159 member function. */
1160
1161 struct saved_token_sentinel
1162 {
1163 cp_lexer *lexer;
1164 unsigned len;
1165 bool commit;
1166 saved_token_sentinel(cp_lexer *lexer): lexer(lexer), commit(true)
1167 {
1168 len = lexer->saved_tokens.length ();
1169 cp_lexer_save_tokens (lexer);
1170 }
1171 void rollback ()
1172 {
1173 cp_lexer_rollback_tokens (lexer);
1174 commit = false;
1175 }
1176 ~saved_token_sentinel()
1177 {
1178 if (commit)
1179 cp_lexer_commit_tokens (lexer);
1180 gcc_assert (lexer->saved_tokens.length () == len);
1181 }
1182 };
1183
1184 /* Print a representation of the TOKEN on the STREAM. */
1185
1186 static void
1187 cp_lexer_print_token (FILE * stream, cp_token *token)
1188 {
1189 /* We don't use cpp_type2name here because the parser defines
1190 a few tokens of its own. */
1191 static const char *const token_names[] = {
1192 /* cpplib-defined token types */
1193 #define OP(e, s) #e,
1194 #define TK(e, s) #e,
1195 TTYPE_TABLE
1196 #undef OP
1197 #undef TK
1198 /* C++ parser token types - see "Manifest constants", above. */
1199 "KEYWORD",
1200 "TEMPLATE_ID",
1201 "NESTED_NAME_SPECIFIER",
1202 };
1203
1204 /* For some tokens, print the associated data. */
1205 switch (token->type)
1206 {
1207 case CPP_KEYWORD:
1208 /* Some keywords have a value that is not an IDENTIFIER_NODE.
1209 For example, `struct' is mapped to an INTEGER_CST. */
1210 if (!identifier_p (token->u.value))
1211 break;
1212 /* else fall through */
1213 case CPP_NAME:
1214 fputs (IDENTIFIER_POINTER (token->u.value), stream);
1215 break;
1216
1217 case CPP_STRING:
1218 case CPP_STRING16:
1219 case CPP_STRING32:
1220 case CPP_WSTRING:
1221 case CPP_UTF8STRING:
1222 fprintf (stream, " \"%s\"", TREE_STRING_POINTER (token->u.value));
1223 break;
1224
1225 case CPP_NUMBER:
1226 print_generic_expr (stream, token->u.value, 0);
1227 break;
1228
1229 default:
1230 /* If we have a name for the token, print it out. Otherwise, we
1231 simply give the numeric code. */
1232 if (token->type < ARRAY_SIZE(token_names))
1233 fputs (token_names[token->type], stream);
1234 else
1235 fprintf (stream, "[%d]", token->type);
1236 break;
1237 }
1238 }
1239
1240 DEBUG_FUNCTION void
1241 debug (cp_token &ref)
1242 {
1243 cp_lexer_print_token (stderr, &ref);
1244 fprintf (stderr, "\n");
1245 }
1246
1247 DEBUG_FUNCTION void
1248 debug (cp_token *ptr)
1249 {
1250 if (ptr)
1251 debug (*ptr);
1252 else
1253 fprintf (stderr, "<nil>\n");
1254 }
1255
1256
1257 /* Start emitting debugging information. */
1258
1259 static void
1260 cp_lexer_start_debugging (cp_lexer* lexer)
1261 {
1262 lexer->debugging_p = true;
1263 cp_lexer_debug_stream = stderr;
1264 }
1265
1266 /* Stop emitting debugging information. */
1267
1268 static void
1269 cp_lexer_stop_debugging (cp_lexer* lexer)
1270 {
1271 lexer->debugging_p = false;
1272 cp_lexer_debug_stream = NULL;
1273 }
1274
1275 /* Create a new cp_token_cache, representing a range of tokens. */
1276
1277 static cp_token_cache *
1278 cp_token_cache_new (cp_token *first, cp_token *last)
1279 {
1280 cp_token_cache *cache = ggc_alloc<cp_token_cache> ();
1281 cache->first = first;
1282 cache->last = last;
1283 return cache;
1284 }
1285
1286 /* Diagnose if #pragma omp declare simd isn't followed immediately
1287 by function declaration or definition. */
1288
1289 static inline void
1290 cp_ensure_no_omp_declare_simd (cp_parser *parser)
1291 {
1292 if (parser->omp_declare_simd && !parser->omp_declare_simd->error_seen)
1293 {
1294 error ("%<#pragma omp declare simd%> not immediately followed by "
1295 "function declaration or definition");
1296 parser->omp_declare_simd = NULL;
1297 }
1298 }
1299
1300 /* Finalize #pragma omp declare simd clauses after FNDECL has been parsed,
1301 and put that into "omp declare simd" attribute. */
1302
1303 static inline void
1304 cp_finalize_omp_declare_simd (cp_parser *parser, tree fndecl)
1305 {
1306 if (__builtin_expect (parser->omp_declare_simd != NULL, 0))
1307 {
1308 if (fndecl == error_mark_node)
1309 {
1310 parser->omp_declare_simd = NULL;
1311 return;
1312 }
1313 if (TREE_CODE (fndecl) != FUNCTION_DECL)
1314 {
1315 cp_ensure_no_omp_declare_simd (parser);
1316 return;
1317 }
1318 }
1319 }
1320
1321 /* Diagnose if #pragma acc routine isn't followed immediately by function
1322 declaration or definition. */
1323
1324 static inline void
1325 cp_ensure_no_oacc_routine (cp_parser *parser)
1326 {
1327 if (parser->oacc_routine && !parser->oacc_routine->error_seen)
1328 {
1329 tree clauses = parser->oacc_routine->clauses;
1330 location_t loc = OMP_CLAUSE_LOCATION (TREE_PURPOSE (clauses));
1331
1332 error_at (loc, "%<#pragma oacc routine%> not followed by function "
1333 "declaration or definition");
1334 parser->oacc_routine = NULL;
1335 }
1336 }
1337 \f
1338 /* Decl-specifiers. */
1339
1340 /* Set *DECL_SPECS to represent an empty decl-specifier-seq. */
1341
1342 static void
1343 clear_decl_specs (cp_decl_specifier_seq *decl_specs)
1344 {
1345 memset (decl_specs, 0, sizeof (cp_decl_specifier_seq));
1346 }
1347
1348 /* Declarators. */
1349
1350 /* Nothing other than the parser should be creating declarators;
1351 declarators are a semi-syntactic representation of C++ entities.
1352 Other parts of the front end that need to create entities (like
1353 VAR_DECLs or FUNCTION_DECLs) should do that directly. */
1354
1355 static cp_declarator *make_call_declarator
1356 (cp_declarator *, tree, cp_cv_quals, cp_virt_specifiers, cp_ref_qualifier, tree, tree, tree, tree);
1357 static cp_declarator *make_array_declarator
1358 (cp_declarator *, tree);
1359 static cp_declarator *make_pointer_declarator
1360 (cp_cv_quals, cp_declarator *, tree);
1361 static cp_declarator *make_reference_declarator
1362 (cp_cv_quals, cp_declarator *, bool, tree);
1363 static cp_declarator *make_ptrmem_declarator
1364 (cp_cv_quals, tree, cp_declarator *, tree);
1365
1366 /* An erroneous declarator. */
1367 static cp_declarator *cp_error_declarator;
1368
1369 /* The obstack on which declarators and related data structures are
1370 allocated. */
1371 static struct obstack declarator_obstack;
1372
1373 /* Alloc BYTES from the declarator memory pool. */
1374
1375 static inline void *
1376 alloc_declarator (size_t bytes)
1377 {
1378 return obstack_alloc (&declarator_obstack, bytes);
1379 }
1380
1381 /* Allocate a declarator of the indicated KIND. Clear fields that are
1382 common to all declarators. */
1383
1384 static cp_declarator *
1385 make_declarator (cp_declarator_kind kind)
1386 {
1387 cp_declarator *declarator;
1388
1389 declarator = (cp_declarator *) alloc_declarator (sizeof (cp_declarator));
1390 declarator->kind = kind;
1391 declarator->attributes = NULL_TREE;
1392 declarator->std_attributes = NULL_TREE;
1393 declarator->declarator = NULL;
1394 declarator->parameter_pack_p = false;
1395 declarator->id_loc = UNKNOWN_LOCATION;
1396
1397 return declarator;
1398 }
1399
1400 /* Make a declarator for a generalized identifier. If
1401 QUALIFYING_SCOPE is non-NULL, the identifier is
1402 QUALIFYING_SCOPE::UNQUALIFIED_NAME; otherwise, it is just
1403 UNQUALIFIED_NAME. SFK indicates the kind of special function this
1404 is, if any. */
1405
1406 static cp_declarator *
1407 make_id_declarator (tree qualifying_scope, tree unqualified_name,
1408 special_function_kind sfk)
1409 {
1410 cp_declarator *declarator;
1411
1412 /* It is valid to write:
1413
1414 class C { void f(); };
1415 typedef C D;
1416 void D::f();
1417
1418 The standard is not clear about whether `typedef const C D' is
1419 legal; as of 2002-09-15 the committee is considering that
1420 question. EDG 3.0 allows that syntax. Therefore, we do as
1421 well. */
1422 if (qualifying_scope && TYPE_P (qualifying_scope))
1423 qualifying_scope = TYPE_MAIN_VARIANT (qualifying_scope);
1424
1425 gcc_assert (identifier_p (unqualified_name)
1426 || TREE_CODE (unqualified_name) == BIT_NOT_EXPR
1427 || TREE_CODE (unqualified_name) == TEMPLATE_ID_EXPR);
1428
1429 declarator = make_declarator (cdk_id);
1430 declarator->u.id.qualifying_scope = qualifying_scope;
1431 declarator->u.id.unqualified_name = unqualified_name;
1432 declarator->u.id.sfk = sfk;
1433
1434 return declarator;
1435 }
1436
1437 /* Make a declarator for a pointer to TARGET. CV_QUALIFIERS is a list
1438 of modifiers such as const or volatile to apply to the pointer
1439 type, represented as identifiers. ATTRIBUTES represent the attributes that
1440 appertain to the pointer or reference. */
1441
1442 cp_declarator *
1443 make_pointer_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target,
1444 tree attributes)
1445 {
1446 cp_declarator *declarator;
1447
1448 declarator = make_declarator (cdk_pointer);
1449 declarator->declarator = target;
1450 declarator->u.pointer.qualifiers = cv_qualifiers;
1451 declarator->u.pointer.class_type = NULL_TREE;
1452 if (target)
1453 {
1454 declarator->id_loc = target->id_loc;
1455 declarator->parameter_pack_p = target->parameter_pack_p;
1456 target->parameter_pack_p = false;
1457 }
1458 else
1459 declarator->parameter_pack_p = false;
1460
1461 declarator->std_attributes = attributes;
1462
1463 return declarator;
1464 }
1465
1466 /* Like make_pointer_declarator -- but for references. ATTRIBUTES
1467 represent the attributes that appertain to the pointer or
1468 reference. */
1469
1470 cp_declarator *
1471 make_reference_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target,
1472 bool rvalue_ref, tree attributes)
1473 {
1474 cp_declarator *declarator;
1475
1476 declarator = make_declarator (cdk_reference);
1477 declarator->declarator = target;
1478 declarator->u.reference.qualifiers = cv_qualifiers;
1479 declarator->u.reference.rvalue_ref = rvalue_ref;
1480 if (target)
1481 {
1482 declarator->id_loc = target->id_loc;
1483 declarator->parameter_pack_p = target->parameter_pack_p;
1484 target->parameter_pack_p = false;
1485 }
1486 else
1487 declarator->parameter_pack_p = false;
1488
1489 declarator->std_attributes = attributes;
1490
1491 return declarator;
1492 }
1493
1494 /* Like make_pointer_declarator -- but for a pointer to a non-static
1495 member of CLASS_TYPE. ATTRIBUTES represent the attributes that
1496 appertain to the pointer or reference. */
1497
1498 cp_declarator *
1499 make_ptrmem_declarator (cp_cv_quals cv_qualifiers, tree class_type,
1500 cp_declarator *pointee,
1501 tree attributes)
1502 {
1503 cp_declarator *declarator;
1504
1505 declarator = make_declarator (cdk_ptrmem);
1506 declarator->declarator = pointee;
1507 declarator->u.pointer.qualifiers = cv_qualifiers;
1508 declarator->u.pointer.class_type = class_type;
1509
1510 if (pointee)
1511 {
1512 declarator->parameter_pack_p = pointee->parameter_pack_p;
1513 pointee->parameter_pack_p = false;
1514 }
1515 else
1516 declarator->parameter_pack_p = false;
1517
1518 declarator->std_attributes = attributes;
1519
1520 return declarator;
1521 }
1522
1523 /* Make a declarator for the function given by TARGET, with the
1524 indicated PARMS. The CV_QUALIFIERS aply to the function, as in
1525 "const"-qualified member function. The EXCEPTION_SPECIFICATION
1526 indicates what exceptions can be thrown. */
1527
1528 cp_declarator *
1529 make_call_declarator (cp_declarator *target,
1530 tree parms,
1531 cp_cv_quals cv_qualifiers,
1532 cp_virt_specifiers virt_specifiers,
1533 cp_ref_qualifier ref_qualifier,
1534 tree tx_qualifier,
1535 tree exception_specification,
1536 tree late_return_type,
1537 tree requires_clause)
1538 {
1539 cp_declarator *declarator;
1540
1541 declarator = make_declarator (cdk_function);
1542 declarator->declarator = target;
1543 declarator->u.function.parameters = parms;
1544 declarator->u.function.qualifiers = cv_qualifiers;
1545 declarator->u.function.virt_specifiers = virt_specifiers;
1546 declarator->u.function.ref_qualifier = ref_qualifier;
1547 declarator->u.function.tx_qualifier = tx_qualifier;
1548 declarator->u.function.exception_specification = exception_specification;
1549 declarator->u.function.late_return_type = late_return_type;
1550 declarator->u.function.requires_clause = requires_clause;
1551 if (target)
1552 {
1553 declarator->id_loc = target->id_loc;
1554 declarator->parameter_pack_p = target->parameter_pack_p;
1555 target->parameter_pack_p = false;
1556 }
1557 else
1558 declarator->parameter_pack_p = false;
1559
1560 return declarator;
1561 }
1562
1563 /* Make a declarator for an array of BOUNDS elements, each of which is
1564 defined by ELEMENT. */
1565
1566 cp_declarator *
1567 make_array_declarator (cp_declarator *element, tree bounds)
1568 {
1569 cp_declarator *declarator;
1570
1571 declarator = make_declarator (cdk_array);
1572 declarator->declarator = element;
1573 declarator->u.array.bounds = bounds;
1574 if (element)
1575 {
1576 declarator->id_loc = element->id_loc;
1577 declarator->parameter_pack_p = element->parameter_pack_p;
1578 element->parameter_pack_p = false;
1579 }
1580 else
1581 declarator->parameter_pack_p = false;
1582
1583 return declarator;
1584 }
1585
1586 /* Determine whether the declarator we've seen so far can be a
1587 parameter pack, when followed by an ellipsis. */
1588 static bool
1589 declarator_can_be_parameter_pack (cp_declarator *declarator)
1590 {
1591 if (declarator && declarator->parameter_pack_p)
1592 /* We already saw an ellipsis. */
1593 return false;
1594
1595 /* Search for a declarator name, or any other declarator that goes
1596 after the point where the ellipsis could appear in a parameter
1597 pack. If we find any of these, then this declarator can not be
1598 made into a parameter pack. */
1599 bool found = false;
1600 while (declarator && !found)
1601 {
1602 switch ((int)declarator->kind)
1603 {
1604 case cdk_id:
1605 case cdk_array:
1606 found = true;
1607 break;
1608
1609 case cdk_error:
1610 return true;
1611
1612 default:
1613 declarator = declarator->declarator;
1614 break;
1615 }
1616 }
1617
1618 return !found;
1619 }
1620
1621 cp_parameter_declarator *no_parameters;
1622
1623 /* Create a parameter declarator with the indicated DECL_SPECIFIERS,
1624 DECLARATOR and DEFAULT_ARGUMENT. */
1625
1626 cp_parameter_declarator *
1627 make_parameter_declarator (cp_decl_specifier_seq *decl_specifiers,
1628 cp_declarator *declarator,
1629 tree default_argument,
1630 bool template_parameter_pack_p = false)
1631 {
1632 cp_parameter_declarator *parameter;
1633
1634 parameter = ((cp_parameter_declarator *)
1635 alloc_declarator (sizeof (cp_parameter_declarator)));
1636 parameter->next = NULL;
1637 if (decl_specifiers)
1638 parameter->decl_specifiers = *decl_specifiers;
1639 else
1640 clear_decl_specs (&parameter->decl_specifiers);
1641 parameter->declarator = declarator;
1642 parameter->default_argument = default_argument;
1643 parameter->template_parameter_pack_p = template_parameter_pack_p;
1644
1645 return parameter;
1646 }
1647
1648 /* Returns true iff DECLARATOR is a declaration for a function. */
1649
1650 static bool
1651 function_declarator_p (const cp_declarator *declarator)
1652 {
1653 while (declarator)
1654 {
1655 if (declarator->kind == cdk_function
1656 && declarator->declarator->kind == cdk_id)
1657 return true;
1658 if (declarator->kind == cdk_id
1659 || declarator->kind == cdk_error)
1660 return false;
1661 declarator = declarator->declarator;
1662 }
1663 return false;
1664 }
1665
1666 /* The parser. */
1667
1668 /* Overview
1669 --------
1670
1671 A cp_parser parses the token stream as specified by the C++
1672 grammar. Its job is purely parsing, not semantic analysis. For
1673 example, the parser breaks the token stream into declarators,
1674 expressions, statements, and other similar syntactic constructs.
1675 It does not check that the types of the expressions on either side
1676 of an assignment-statement are compatible, or that a function is
1677 not declared with a parameter of type `void'.
1678
1679 The parser invokes routines elsewhere in the compiler to perform
1680 semantic analysis and to build up the abstract syntax tree for the
1681 code processed.
1682
1683 The parser (and the template instantiation code, which is, in a
1684 way, a close relative of parsing) are the only parts of the
1685 compiler that should be calling push_scope and pop_scope, or
1686 related functions. The parser (and template instantiation code)
1687 keeps track of what scope is presently active; everything else
1688 should simply honor that. (The code that generates static
1689 initializers may also need to set the scope, in order to check
1690 access control correctly when emitting the initializers.)
1691
1692 Methodology
1693 -----------
1694
1695 The parser is of the standard recursive-descent variety. Upcoming
1696 tokens in the token stream are examined in order to determine which
1697 production to use when parsing a non-terminal. Some C++ constructs
1698 require arbitrary look ahead to disambiguate. For example, it is
1699 impossible, in the general case, to tell whether a statement is an
1700 expression or declaration without scanning the entire statement.
1701 Therefore, the parser is capable of "parsing tentatively." When the
1702 parser is not sure what construct comes next, it enters this mode.
1703 Then, while we attempt to parse the construct, the parser queues up
1704 error messages, rather than issuing them immediately, and saves the
1705 tokens it consumes. If the construct is parsed successfully, the
1706 parser "commits", i.e., it issues any queued error messages and
1707 the tokens that were being preserved are permanently discarded.
1708 If, however, the construct is not parsed successfully, the parser
1709 rolls back its state completely so that it can resume parsing using
1710 a different alternative.
1711
1712 Future Improvements
1713 -------------------
1714
1715 The performance of the parser could probably be improved substantially.
1716 We could often eliminate the need to parse tentatively by looking ahead
1717 a little bit. In some places, this approach might not entirely eliminate
1718 the need to parse tentatively, but it might still speed up the average
1719 case. */
1720
1721 /* Flags that are passed to some parsing functions. These values can
1722 be bitwise-ored together. */
1723
1724 enum
1725 {
1726 /* No flags. */
1727 CP_PARSER_FLAGS_NONE = 0x0,
1728 /* The construct is optional. If it is not present, then no error
1729 should be issued. */
1730 CP_PARSER_FLAGS_OPTIONAL = 0x1,
1731 /* When parsing a type-specifier, treat user-defined type-names
1732 as non-type identifiers. */
1733 CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES = 0x2,
1734 /* When parsing a type-specifier, do not try to parse a class-specifier
1735 or enum-specifier. */
1736 CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS = 0x4,
1737 /* When parsing a decl-specifier-seq, only allow type-specifier or
1738 constexpr. */
1739 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR = 0x8
1740 };
1741
1742 /* This type is used for parameters and variables which hold
1743 combinations of the above flags. */
1744 typedef int cp_parser_flags;
1745
1746 /* The different kinds of declarators we want to parse. */
1747
1748 enum cp_parser_declarator_kind
1749 {
1750 /* We want an abstract declarator. */
1751 CP_PARSER_DECLARATOR_ABSTRACT,
1752 /* We want a named declarator. */
1753 CP_PARSER_DECLARATOR_NAMED,
1754 /* We don't mind, but the name must be an unqualified-id. */
1755 CP_PARSER_DECLARATOR_EITHER
1756 };
1757
1758 /* The precedence values used to parse binary expressions. The minimum value
1759 of PREC must be 1, because zero is reserved to quickly discriminate
1760 binary operators from other tokens. */
1761
1762 enum cp_parser_prec
1763 {
1764 PREC_NOT_OPERATOR,
1765 PREC_LOGICAL_OR_EXPRESSION,
1766 PREC_LOGICAL_AND_EXPRESSION,
1767 PREC_INCLUSIVE_OR_EXPRESSION,
1768 PREC_EXCLUSIVE_OR_EXPRESSION,
1769 PREC_AND_EXPRESSION,
1770 PREC_EQUALITY_EXPRESSION,
1771 PREC_RELATIONAL_EXPRESSION,
1772 PREC_SHIFT_EXPRESSION,
1773 PREC_ADDITIVE_EXPRESSION,
1774 PREC_MULTIPLICATIVE_EXPRESSION,
1775 PREC_PM_EXPRESSION,
1776 NUM_PREC_VALUES = PREC_PM_EXPRESSION
1777 };
1778
1779 /* A mapping from a token type to a corresponding tree node type, with a
1780 precedence value. */
1781
1782 struct cp_parser_binary_operations_map_node
1783 {
1784 /* The token type. */
1785 enum cpp_ttype token_type;
1786 /* The corresponding tree code. */
1787 enum tree_code tree_type;
1788 /* The precedence of this operator. */
1789 enum cp_parser_prec prec;
1790 };
1791
1792 struct cp_parser_expression_stack_entry
1793 {
1794 /* Left hand side of the binary operation we are currently
1795 parsing. */
1796 tree lhs;
1797 /* Original tree code for left hand side, if it was a binary
1798 expression itself (used for -Wparentheses). */
1799 enum tree_code lhs_type;
1800 /* Tree code for the binary operation we are parsing. */
1801 enum tree_code tree_type;
1802 /* Precedence of the binary operation we are parsing. */
1803 enum cp_parser_prec prec;
1804 /* Location of the binary operation we are parsing. */
1805 location_t loc;
1806 };
1807
1808 /* The stack for storing partial expressions. We only need NUM_PREC_VALUES
1809 entries because precedence levels on the stack are monotonically
1810 increasing. */
1811 typedef struct cp_parser_expression_stack_entry
1812 cp_parser_expression_stack[NUM_PREC_VALUES];
1813
1814 /* Prototypes. */
1815
1816 /* Constructors and destructors. */
1817
1818 static cp_parser_context *cp_parser_context_new
1819 (cp_parser_context *);
1820
1821 /* Class variables. */
1822
1823 static GTY((deletable)) cp_parser_context* cp_parser_context_free_list;
1824
1825 /* The operator-precedence table used by cp_parser_binary_expression.
1826 Transformed into an associative array (binops_by_token) by
1827 cp_parser_new. */
1828
1829 static const cp_parser_binary_operations_map_node binops[] = {
1830 { CPP_DEREF_STAR, MEMBER_REF, PREC_PM_EXPRESSION },
1831 { CPP_DOT_STAR, DOTSTAR_EXPR, PREC_PM_EXPRESSION },
1832
1833 { CPP_MULT, MULT_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1834 { CPP_DIV, TRUNC_DIV_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1835 { CPP_MOD, TRUNC_MOD_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1836
1837 { CPP_PLUS, PLUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1838 { CPP_MINUS, MINUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1839
1840 { CPP_LSHIFT, LSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1841 { CPP_RSHIFT, RSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1842
1843 { CPP_LESS, LT_EXPR, PREC_RELATIONAL_EXPRESSION },
1844 { CPP_GREATER, GT_EXPR, PREC_RELATIONAL_EXPRESSION },
1845 { CPP_LESS_EQ, LE_EXPR, PREC_RELATIONAL_EXPRESSION },
1846 { CPP_GREATER_EQ, GE_EXPR, PREC_RELATIONAL_EXPRESSION },
1847
1848 { CPP_EQ_EQ, EQ_EXPR, PREC_EQUALITY_EXPRESSION },
1849 { CPP_NOT_EQ, NE_EXPR, PREC_EQUALITY_EXPRESSION },
1850
1851 { CPP_AND, BIT_AND_EXPR, PREC_AND_EXPRESSION },
1852
1853 { CPP_XOR, BIT_XOR_EXPR, PREC_EXCLUSIVE_OR_EXPRESSION },
1854
1855 { CPP_OR, BIT_IOR_EXPR, PREC_INCLUSIVE_OR_EXPRESSION },
1856
1857 { CPP_AND_AND, TRUTH_ANDIF_EXPR, PREC_LOGICAL_AND_EXPRESSION },
1858
1859 { CPP_OR_OR, TRUTH_ORIF_EXPR, PREC_LOGICAL_OR_EXPRESSION }
1860 };
1861
1862 /* The same as binops, but initialized by cp_parser_new so that
1863 binops_by_token[N].token_type == N. Used in cp_parser_binary_expression
1864 for speed. */
1865 static cp_parser_binary_operations_map_node binops_by_token[N_CP_TTYPES];
1866
1867 /* Constructors and destructors. */
1868
1869 /* Construct a new context. The context below this one on the stack
1870 is given by NEXT. */
1871
1872 static cp_parser_context *
1873 cp_parser_context_new (cp_parser_context* next)
1874 {
1875 cp_parser_context *context;
1876
1877 /* Allocate the storage. */
1878 if (cp_parser_context_free_list != NULL)
1879 {
1880 /* Pull the first entry from the free list. */
1881 context = cp_parser_context_free_list;
1882 cp_parser_context_free_list = context->next;
1883 memset (context, 0, sizeof (*context));
1884 }
1885 else
1886 context = ggc_cleared_alloc<cp_parser_context> ();
1887
1888 /* No errors have occurred yet in this context. */
1889 context->status = CP_PARSER_STATUS_KIND_NO_ERROR;
1890 /* If this is not the bottommost context, copy information that we
1891 need from the previous context. */
1892 if (next)
1893 {
1894 /* If, in the NEXT context, we are parsing an `x->' or `x.'
1895 expression, then we are parsing one in this context, too. */
1896 context->object_type = next->object_type;
1897 /* Thread the stack. */
1898 context->next = next;
1899 }
1900
1901 return context;
1902 }
1903
1904 /* Managing the unparsed function queues. */
1905
1906 #define unparsed_funs_with_default_args \
1907 parser->unparsed_queues->last ().funs_with_default_args
1908 #define unparsed_funs_with_definitions \
1909 parser->unparsed_queues->last ().funs_with_definitions
1910 #define unparsed_nsdmis \
1911 parser->unparsed_queues->last ().nsdmis
1912 #define unparsed_classes \
1913 parser->unparsed_queues->last ().classes
1914
1915 static void
1916 push_unparsed_function_queues (cp_parser *parser)
1917 {
1918 cp_unparsed_functions_entry e = {NULL, make_tree_vector (), NULL, NULL};
1919 vec_safe_push (parser->unparsed_queues, e);
1920 }
1921
1922 static void
1923 pop_unparsed_function_queues (cp_parser *parser)
1924 {
1925 release_tree_vector (unparsed_funs_with_definitions);
1926 parser->unparsed_queues->pop ();
1927 }
1928
1929 /* Prototypes. */
1930
1931 /* Constructors and destructors. */
1932
1933 static cp_parser *cp_parser_new
1934 (void);
1935
1936 /* Routines to parse various constructs.
1937
1938 Those that return `tree' will return the error_mark_node (rather
1939 than NULL_TREE) if a parse error occurs, unless otherwise noted.
1940 Sometimes, they will return an ordinary node if error-recovery was
1941 attempted, even though a parse error occurred. So, to check
1942 whether or not a parse error occurred, you should always use
1943 cp_parser_error_occurred. If the construct is optional (indicated
1944 either by an `_opt' in the name of the function that does the
1945 parsing or via a FLAGS parameter), then NULL_TREE is returned if
1946 the construct is not present. */
1947
1948 /* Lexical conventions [gram.lex] */
1949
1950 static tree cp_parser_identifier
1951 (cp_parser *);
1952 static tree cp_parser_string_literal
1953 (cp_parser *, bool, bool, bool);
1954 static tree cp_parser_userdef_char_literal
1955 (cp_parser *);
1956 static tree cp_parser_userdef_string_literal
1957 (tree);
1958 static tree cp_parser_userdef_numeric_literal
1959 (cp_parser *);
1960
1961 /* Basic concepts [gram.basic] */
1962
1963 static bool cp_parser_translation_unit
1964 (cp_parser *);
1965
1966 /* Expressions [gram.expr] */
1967
1968 static tree cp_parser_primary_expression
1969 (cp_parser *, bool, bool, bool, cp_id_kind *);
1970 static tree cp_parser_id_expression
1971 (cp_parser *, bool, bool, bool *, bool, bool);
1972 static tree cp_parser_unqualified_id
1973 (cp_parser *, bool, bool, bool, bool);
1974 static tree cp_parser_nested_name_specifier_opt
1975 (cp_parser *, bool, bool, bool, bool);
1976 static tree cp_parser_nested_name_specifier
1977 (cp_parser *, bool, bool, bool, bool);
1978 static tree cp_parser_qualifying_entity
1979 (cp_parser *, bool, bool, bool, bool, bool);
1980 static tree cp_parser_postfix_expression
1981 (cp_parser *, bool, bool, bool, bool, cp_id_kind *);
1982 static tree cp_parser_postfix_open_square_expression
1983 (cp_parser *, tree, bool, bool);
1984 static tree cp_parser_postfix_dot_deref_expression
1985 (cp_parser *, enum cpp_ttype, tree, bool, cp_id_kind *, location_t);
1986 static vec<tree, va_gc> *cp_parser_parenthesized_expression_list
1987 (cp_parser *, int, bool, bool, bool *);
1988 /* Values for the second parameter of cp_parser_parenthesized_expression_list. */
1989 enum { non_attr = 0, normal_attr = 1, id_attr = 2 };
1990 static void cp_parser_pseudo_destructor_name
1991 (cp_parser *, tree, tree *, tree *);
1992 static tree cp_parser_unary_expression
1993 (cp_parser *, cp_id_kind * = NULL, bool = false, bool = false, bool = false);
1994 static enum tree_code cp_parser_unary_operator
1995 (cp_token *);
1996 static tree cp_parser_new_expression
1997 (cp_parser *);
1998 static vec<tree, va_gc> *cp_parser_new_placement
1999 (cp_parser *);
2000 static tree cp_parser_new_type_id
2001 (cp_parser *, tree *);
2002 static cp_declarator *cp_parser_new_declarator_opt
2003 (cp_parser *);
2004 static cp_declarator *cp_parser_direct_new_declarator
2005 (cp_parser *);
2006 static vec<tree, va_gc> *cp_parser_new_initializer
2007 (cp_parser *);
2008 static tree cp_parser_delete_expression
2009 (cp_parser *);
2010 static tree cp_parser_cast_expression
2011 (cp_parser *, bool, bool, bool, cp_id_kind *);
2012 static tree cp_parser_binary_expression
2013 (cp_parser *, bool, bool, enum cp_parser_prec, cp_id_kind *);
2014 static tree cp_parser_question_colon_clause
2015 (cp_parser *, tree);
2016 static tree cp_parser_assignment_expression
2017 (cp_parser *, cp_id_kind * = NULL, bool = false, bool = false);
2018 static enum tree_code cp_parser_assignment_operator_opt
2019 (cp_parser *);
2020 static tree cp_parser_expression
2021 (cp_parser *, cp_id_kind * = NULL, bool = false, bool = false);
2022 static tree cp_parser_constant_expression
2023 (cp_parser *, bool = false, bool * = NULL);
2024 static tree cp_parser_builtin_offsetof
2025 (cp_parser *);
2026 static tree cp_parser_lambda_expression
2027 (cp_parser *);
2028 static void cp_parser_lambda_introducer
2029 (cp_parser *, tree);
2030 static bool cp_parser_lambda_declarator_opt
2031 (cp_parser *, tree);
2032 static void cp_parser_lambda_body
2033 (cp_parser *, tree);
2034
2035 /* Statements [gram.stmt.stmt] */
2036
2037 static void cp_parser_statement
2038 (cp_parser *, tree, bool, bool *, vec<tree> * = NULL);
2039 static void cp_parser_label_for_labeled_statement
2040 (cp_parser *, tree);
2041 static tree cp_parser_expression_statement
2042 (cp_parser *, tree);
2043 static tree cp_parser_compound_statement
2044 (cp_parser *, tree, int, bool);
2045 static void cp_parser_statement_seq_opt
2046 (cp_parser *, tree);
2047 static tree cp_parser_selection_statement
2048 (cp_parser *, bool *, vec<tree> *);
2049 static tree cp_parser_condition
2050 (cp_parser *);
2051 static tree cp_parser_iteration_statement
2052 (cp_parser *, bool);
2053 static bool cp_parser_for_init_statement
2054 (cp_parser *, tree *decl);
2055 static tree cp_parser_for
2056 (cp_parser *, bool);
2057 static tree cp_parser_c_for
2058 (cp_parser *, tree, tree, bool);
2059 static tree cp_parser_range_for
2060 (cp_parser *, tree, tree, tree, bool);
2061 static void do_range_for_auto_deduction
2062 (tree, tree);
2063 static tree cp_parser_perform_range_for_lookup
2064 (tree, tree *, tree *);
2065 static tree cp_parser_range_for_member_function
2066 (tree, tree);
2067 static tree cp_parser_jump_statement
2068 (cp_parser *);
2069 static void cp_parser_declaration_statement
2070 (cp_parser *);
2071
2072 static tree cp_parser_implicitly_scoped_statement
2073 (cp_parser *, bool *, const token_indent_info &, vec<tree> * = NULL);
2074 static void cp_parser_already_scoped_statement
2075 (cp_parser *, const token_indent_info &);
2076
2077 /* Declarations [gram.dcl.dcl] */
2078
2079 static void cp_parser_declaration_seq_opt
2080 (cp_parser *);
2081 static void cp_parser_declaration
2082 (cp_parser *);
2083 static void cp_parser_block_declaration
2084 (cp_parser *, bool);
2085 static void cp_parser_simple_declaration
2086 (cp_parser *, bool, tree *);
2087 static void cp_parser_decl_specifier_seq
2088 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, int *);
2089 static tree cp_parser_storage_class_specifier_opt
2090 (cp_parser *);
2091 static tree cp_parser_function_specifier_opt
2092 (cp_parser *, cp_decl_specifier_seq *);
2093 static tree cp_parser_type_specifier
2094 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, bool,
2095 int *, bool *);
2096 static tree cp_parser_simple_type_specifier
2097 (cp_parser *, cp_decl_specifier_seq *, cp_parser_flags);
2098 static tree cp_parser_type_name
2099 (cp_parser *, bool);
2100 static tree cp_parser_type_name
2101 (cp_parser *);
2102 static tree cp_parser_nonclass_name
2103 (cp_parser* parser);
2104 static tree cp_parser_elaborated_type_specifier
2105 (cp_parser *, bool, bool);
2106 static tree cp_parser_enum_specifier
2107 (cp_parser *);
2108 static void cp_parser_enumerator_list
2109 (cp_parser *, tree);
2110 static void cp_parser_enumerator_definition
2111 (cp_parser *, tree);
2112 static tree cp_parser_namespace_name
2113 (cp_parser *);
2114 static void cp_parser_namespace_definition
2115 (cp_parser *);
2116 static void cp_parser_namespace_body
2117 (cp_parser *);
2118 static tree cp_parser_qualified_namespace_specifier
2119 (cp_parser *);
2120 static void cp_parser_namespace_alias_definition
2121 (cp_parser *);
2122 static bool cp_parser_using_declaration
2123 (cp_parser *, bool);
2124 static void cp_parser_using_directive
2125 (cp_parser *);
2126 static tree cp_parser_alias_declaration
2127 (cp_parser *);
2128 static void cp_parser_asm_definition
2129 (cp_parser *);
2130 static void cp_parser_linkage_specification
2131 (cp_parser *);
2132 static void cp_parser_static_assert
2133 (cp_parser *, bool);
2134 static tree cp_parser_decltype
2135 (cp_parser *);
2136
2137 /* Declarators [gram.dcl.decl] */
2138
2139 static tree cp_parser_init_declarator
2140 (cp_parser *, cp_decl_specifier_seq *, vec<deferred_access_check, va_gc> *,
2141 bool, bool, int, bool *, tree *, location_t *);
2142 static cp_declarator *cp_parser_declarator
2143 (cp_parser *, cp_parser_declarator_kind, int *, bool *, bool, bool);
2144 static cp_declarator *cp_parser_direct_declarator
2145 (cp_parser *, cp_parser_declarator_kind, int *, bool, bool);
2146 static enum tree_code cp_parser_ptr_operator
2147 (cp_parser *, tree *, cp_cv_quals *, tree *);
2148 static cp_cv_quals cp_parser_cv_qualifier_seq_opt
2149 (cp_parser *);
2150 static cp_virt_specifiers cp_parser_virt_specifier_seq_opt
2151 (cp_parser *);
2152 static cp_ref_qualifier cp_parser_ref_qualifier_opt
2153 (cp_parser *);
2154 static tree cp_parser_tx_qualifier_opt
2155 (cp_parser *);
2156 static tree cp_parser_late_return_type_opt
2157 (cp_parser *, cp_declarator *, tree &, cp_cv_quals);
2158 static tree cp_parser_declarator_id
2159 (cp_parser *, bool);
2160 static tree cp_parser_type_id
2161 (cp_parser *);
2162 static tree cp_parser_template_type_arg
2163 (cp_parser *);
2164 static tree cp_parser_trailing_type_id (cp_parser *);
2165 static tree cp_parser_type_id_1
2166 (cp_parser *, bool, bool);
2167 static void cp_parser_type_specifier_seq
2168 (cp_parser *, bool, bool, cp_decl_specifier_seq *);
2169 static tree cp_parser_parameter_declaration_clause
2170 (cp_parser *);
2171 static tree cp_parser_parameter_declaration_list
2172 (cp_parser *, bool *);
2173 static cp_parameter_declarator *cp_parser_parameter_declaration
2174 (cp_parser *, bool, bool *);
2175 static tree cp_parser_default_argument
2176 (cp_parser *, bool);
2177 static void cp_parser_function_body
2178 (cp_parser *, bool);
2179 static tree cp_parser_initializer
2180 (cp_parser *, bool *, bool *);
2181 static tree cp_parser_initializer_clause
2182 (cp_parser *, bool *);
2183 static tree cp_parser_braced_list
2184 (cp_parser*, bool*);
2185 static vec<constructor_elt, va_gc> *cp_parser_initializer_list
2186 (cp_parser *, bool *);
2187
2188 static bool cp_parser_ctor_initializer_opt_and_function_body
2189 (cp_parser *, bool);
2190
2191 static tree cp_parser_late_parsing_omp_declare_simd
2192 (cp_parser *, tree);
2193
2194 static tree cp_parser_late_parsing_cilk_simd_fn_info
2195 (cp_parser *, tree);
2196
2197 static tree cp_parser_late_parsing_oacc_routine
2198 (cp_parser *, tree);
2199
2200 static tree synthesize_implicit_template_parm
2201 (cp_parser *, tree);
2202 static tree finish_fully_implicit_template
2203 (cp_parser *, tree);
2204
2205 /* Classes [gram.class] */
2206
2207 static tree cp_parser_class_name
2208 (cp_parser *, bool, bool, enum tag_types, bool, bool, bool, bool = false);
2209 static tree cp_parser_class_specifier
2210 (cp_parser *);
2211 static tree cp_parser_class_head
2212 (cp_parser *, bool *);
2213 static enum tag_types cp_parser_class_key
2214 (cp_parser *);
2215 static void cp_parser_type_parameter_key
2216 (cp_parser* parser);
2217 static void cp_parser_member_specification_opt
2218 (cp_parser *);
2219 static void cp_parser_member_declaration
2220 (cp_parser *);
2221 static tree cp_parser_pure_specifier
2222 (cp_parser *);
2223 static tree cp_parser_constant_initializer
2224 (cp_parser *);
2225
2226 /* Derived classes [gram.class.derived] */
2227
2228 static tree cp_parser_base_clause
2229 (cp_parser *);
2230 static tree cp_parser_base_specifier
2231 (cp_parser *);
2232
2233 /* Special member functions [gram.special] */
2234
2235 static tree cp_parser_conversion_function_id
2236 (cp_parser *);
2237 static tree cp_parser_conversion_type_id
2238 (cp_parser *);
2239 static cp_declarator *cp_parser_conversion_declarator_opt
2240 (cp_parser *);
2241 static bool cp_parser_ctor_initializer_opt
2242 (cp_parser *);
2243 static void cp_parser_mem_initializer_list
2244 (cp_parser *);
2245 static tree cp_parser_mem_initializer
2246 (cp_parser *);
2247 static tree cp_parser_mem_initializer_id
2248 (cp_parser *);
2249
2250 /* Overloading [gram.over] */
2251
2252 static tree cp_parser_operator_function_id
2253 (cp_parser *);
2254 static tree cp_parser_operator
2255 (cp_parser *);
2256
2257 /* Templates [gram.temp] */
2258
2259 static void cp_parser_template_declaration
2260 (cp_parser *, bool);
2261 static tree cp_parser_template_parameter_list
2262 (cp_parser *);
2263 static tree cp_parser_template_parameter
2264 (cp_parser *, bool *, bool *);
2265 static tree cp_parser_type_parameter
2266 (cp_parser *, bool *);
2267 static tree cp_parser_template_id
2268 (cp_parser *, bool, bool, enum tag_types, bool);
2269 static tree cp_parser_template_name
2270 (cp_parser *, bool, bool, bool, enum tag_types, bool *);
2271 static tree cp_parser_template_argument_list
2272 (cp_parser *);
2273 static tree cp_parser_template_argument
2274 (cp_parser *);
2275 static void cp_parser_explicit_instantiation
2276 (cp_parser *);
2277 static void cp_parser_explicit_specialization
2278 (cp_parser *);
2279
2280 /* Exception handling [gram.exception] */
2281
2282 static tree cp_parser_try_block
2283 (cp_parser *);
2284 static bool cp_parser_function_try_block
2285 (cp_parser *);
2286 static void cp_parser_handler_seq
2287 (cp_parser *);
2288 static void cp_parser_handler
2289 (cp_parser *);
2290 static tree cp_parser_exception_declaration
2291 (cp_parser *);
2292 static tree cp_parser_throw_expression
2293 (cp_parser *);
2294 static tree cp_parser_exception_specification_opt
2295 (cp_parser *);
2296 static tree cp_parser_type_id_list
2297 (cp_parser *);
2298
2299 /* GNU Extensions */
2300
2301 static tree cp_parser_asm_specification_opt
2302 (cp_parser *);
2303 static tree cp_parser_asm_operand_list
2304 (cp_parser *);
2305 static tree cp_parser_asm_clobber_list
2306 (cp_parser *);
2307 static tree cp_parser_asm_label_list
2308 (cp_parser *);
2309 static bool cp_next_tokens_can_be_attribute_p
2310 (cp_parser *);
2311 static bool cp_next_tokens_can_be_gnu_attribute_p
2312 (cp_parser *);
2313 static bool cp_next_tokens_can_be_std_attribute_p
2314 (cp_parser *);
2315 static bool cp_nth_tokens_can_be_std_attribute_p
2316 (cp_parser *, size_t);
2317 static bool cp_nth_tokens_can_be_gnu_attribute_p
2318 (cp_parser *, size_t);
2319 static bool cp_nth_tokens_can_be_attribute_p
2320 (cp_parser *, size_t);
2321 static tree cp_parser_attributes_opt
2322 (cp_parser *);
2323 static tree cp_parser_gnu_attributes_opt
2324 (cp_parser *);
2325 static tree cp_parser_gnu_attribute_list
2326 (cp_parser *);
2327 static tree cp_parser_std_attribute
2328 (cp_parser *);
2329 static tree cp_parser_std_attribute_spec
2330 (cp_parser *);
2331 static tree cp_parser_std_attribute_spec_seq
2332 (cp_parser *);
2333 static bool cp_parser_extension_opt
2334 (cp_parser *, int *);
2335 static void cp_parser_label_declaration
2336 (cp_parser *);
2337
2338 /* Concept Extensions */
2339
2340 static tree cp_parser_requires_clause
2341 (cp_parser *);
2342 static tree cp_parser_requires_clause_opt
2343 (cp_parser *);
2344 static tree cp_parser_requires_expression
2345 (cp_parser *);
2346 static tree cp_parser_requirement_parameter_list
2347 (cp_parser *);
2348 static tree cp_parser_requirement_body
2349 (cp_parser *);
2350 static tree cp_parser_requirement_list
2351 (cp_parser *);
2352 static tree cp_parser_requirement
2353 (cp_parser *);
2354 static tree cp_parser_simple_requirement
2355 (cp_parser *);
2356 static tree cp_parser_compound_requirement
2357 (cp_parser *);
2358 static tree cp_parser_type_requirement
2359 (cp_parser *);
2360 static tree cp_parser_nested_requirement
2361 (cp_parser *);
2362
2363 /* Transactional Memory Extensions */
2364
2365 static tree cp_parser_transaction
2366 (cp_parser *, cp_token *);
2367 static tree cp_parser_transaction_expression
2368 (cp_parser *, enum rid);
2369 static bool cp_parser_function_transaction
2370 (cp_parser *, enum rid);
2371 static tree cp_parser_transaction_cancel
2372 (cp_parser *);
2373
2374 enum pragma_context {
2375 pragma_external,
2376 pragma_member,
2377 pragma_objc_icode,
2378 pragma_stmt,
2379 pragma_compound
2380 };
2381 static bool cp_parser_pragma
2382 (cp_parser *, enum pragma_context);
2383
2384 /* Objective-C++ Productions */
2385
2386 static tree cp_parser_objc_message_receiver
2387 (cp_parser *);
2388 static tree cp_parser_objc_message_args
2389 (cp_parser *);
2390 static tree cp_parser_objc_message_expression
2391 (cp_parser *);
2392 static tree cp_parser_objc_encode_expression
2393 (cp_parser *);
2394 static tree cp_parser_objc_defs_expression
2395 (cp_parser *);
2396 static tree cp_parser_objc_protocol_expression
2397 (cp_parser *);
2398 static tree cp_parser_objc_selector_expression
2399 (cp_parser *);
2400 static tree cp_parser_objc_expression
2401 (cp_parser *);
2402 static bool cp_parser_objc_selector_p
2403 (enum cpp_ttype);
2404 static tree cp_parser_objc_selector
2405 (cp_parser *);
2406 static tree cp_parser_objc_protocol_refs_opt
2407 (cp_parser *);
2408 static void cp_parser_objc_declaration
2409 (cp_parser *, tree);
2410 static tree cp_parser_objc_statement
2411 (cp_parser *);
2412 static bool cp_parser_objc_valid_prefix_attributes
2413 (cp_parser *, tree *);
2414 static void cp_parser_objc_at_property_declaration
2415 (cp_parser *) ;
2416 static void cp_parser_objc_at_synthesize_declaration
2417 (cp_parser *) ;
2418 static void cp_parser_objc_at_dynamic_declaration
2419 (cp_parser *) ;
2420 static tree cp_parser_objc_struct_declaration
2421 (cp_parser *) ;
2422
2423 /* Utility Routines */
2424
2425 static tree cp_parser_lookup_name
2426 (cp_parser *, tree, enum tag_types, bool, bool, bool, tree *, location_t);
2427 static tree cp_parser_lookup_name_simple
2428 (cp_parser *, tree, location_t);
2429 static tree cp_parser_maybe_treat_template_as_class
2430 (tree, bool);
2431 static bool cp_parser_check_declarator_template_parameters
2432 (cp_parser *, cp_declarator *, location_t);
2433 static bool cp_parser_check_template_parameters
2434 (cp_parser *, unsigned, location_t, cp_declarator *);
2435 static tree cp_parser_simple_cast_expression
2436 (cp_parser *);
2437 static tree cp_parser_global_scope_opt
2438 (cp_parser *, bool);
2439 static bool cp_parser_constructor_declarator_p
2440 (cp_parser *, bool);
2441 static tree cp_parser_function_definition_from_specifiers_and_declarator
2442 (cp_parser *, cp_decl_specifier_seq *, tree, const cp_declarator *);
2443 static tree cp_parser_function_definition_after_declarator
2444 (cp_parser *, bool);
2445 static bool cp_parser_template_declaration_after_export
2446 (cp_parser *, bool);
2447 static void cp_parser_perform_template_parameter_access_checks
2448 (vec<deferred_access_check, va_gc> *);
2449 static tree cp_parser_single_declaration
2450 (cp_parser *, vec<deferred_access_check, va_gc> *, bool, bool, bool *);
2451 static tree cp_parser_functional_cast
2452 (cp_parser *, tree);
2453 static tree cp_parser_save_member_function_body
2454 (cp_parser *, cp_decl_specifier_seq *, cp_declarator *, tree);
2455 static tree cp_parser_save_nsdmi
2456 (cp_parser *);
2457 static tree cp_parser_enclosed_template_argument_list
2458 (cp_parser *);
2459 static void cp_parser_save_default_args
2460 (cp_parser *, tree);
2461 static void cp_parser_late_parsing_for_member
2462 (cp_parser *, tree);
2463 static tree cp_parser_late_parse_one_default_arg
2464 (cp_parser *, tree, tree, tree);
2465 static void cp_parser_late_parsing_nsdmi
2466 (cp_parser *, tree);
2467 static void cp_parser_late_parsing_default_args
2468 (cp_parser *, tree);
2469 static tree cp_parser_sizeof_operand
2470 (cp_parser *, enum rid);
2471 static tree cp_parser_trait_expr
2472 (cp_parser *, enum rid);
2473 static bool cp_parser_declares_only_class_p
2474 (cp_parser *);
2475 static void cp_parser_set_storage_class
2476 (cp_parser *, cp_decl_specifier_seq *, enum rid, cp_token *);
2477 static void cp_parser_set_decl_spec_type
2478 (cp_decl_specifier_seq *, tree, cp_token *, bool);
2479 static void set_and_check_decl_spec_loc
2480 (cp_decl_specifier_seq *decl_specs,
2481 cp_decl_spec ds, cp_token *);
2482 static bool cp_parser_friend_p
2483 (const cp_decl_specifier_seq *);
2484 static void cp_parser_required_error
2485 (cp_parser *, required_token, bool);
2486 static cp_token *cp_parser_require
2487 (cp_parser *, enum cpp_ttype, required_token);
2488 static cp_token *cp_parser_require_keyword
2489 (cp_parser *, enum rid, required_token);
2490 static bool cp_parser_token_starts_function_definition_p
2491 (cp_token *);
2492 static bool cp_parser_next_token_starts_class_definition_p
2493 (cp_parser *);
2494 static bool cp_parser_next_token_ends_template_argument_p
2495 (cp_parser *);
2496 static bool cp_parser_nth_token_starts_template_argument_list_p
2497 (cp_parser *, size_t);
2498 static enum tag_types cp_parser_token_is_class_key
2499 (cp_token *);
2500 static enum tag_types cp_parser_token_is_type_parameter_key
2501 (cp_token *);
2502 static void cp_parser_check_class_key
2503 (enum tag_types, tree type);
2504 static void cp_parser_check_access_in_redeclaration
2505 (tree type, location_t location);
2506 static bool cp_parser_optional_template_keyword
2507 (cp_parser *);
2508 static void cp_parser_pre_parsed_nested_name_specifier
2509 (cp_parser *);
2510 static bool cp_parser_cache_group
2511 (cp_parser *, enum cpp_ttype, unsigned);
2512 static tree cp_parser_cache_defarg
2513 (cp_parser *parser, bool nsdmi);
2514 static void cp_parser_parse_tentatively
2515 (cp_parser *);
2516 static void cp_parser_commit_to_tentative_parse
2517 (cp_parser *);
2518 static void cp_parser_commit_to_topmost_tentative_parse
2519 (cp_parser *);
2520 static void cp_parser_abort_tentative_parse
2521 (cp_parser *);
2522 static bool cp_parser_parse_definitely
2523 (cp_parser *);
2524 static inline bool cp_parser_parsing_tentatively
2525 (cp_parser *);
2526 static bool cp_parser_uncommitted_to_tentative_parse_p
2527 (cp_parser *);
2528 static void cp_parser_error
2529 (cp_parser *, const char *);
2530 static void cp_parser_name_lookup_error
2531 (cp_parser *, tree, tree, name_lookup_error, location_t);
2532 static bool cp_parser_simulate_error
2533 (cp_parser *);
2534 static bool cp_parser_check_type_definition
2535 (cp_parser *);
2536 static void cp_parser_check_for_definition_in_return_type
2537 (cp_declarator *, tree, location_t type_location);
2538 static void cp_parser_check_for_invalid_template_id
2539 (cp_parser *, tree, enum tag_types, location_t location);
2540 static bool cp_parser_non_integral_constant_expression
2541 (cp_parser *, non_integral_constant);
2542 static void cp_parser_diagnose_invalid_type_name
2543 (cp_parser *, tree, location_t);
2544 static bool cp_parser_parse_and_diagnose_invalid_type_name
2545 (cp_parser *);
2546 static int cp_parser_skip_to_closing_parenthesis
2547 (cp_parser *, bool, bool, bool);
2548 static void cp_parser_skip_to_end_of_statement
2549 (cp_parser *);
2550 static void cp_parser_consume_semicolon_at_end_of_statement
2551 (cp_parser *);
2552 static void cp_parser_skip_to_end_of_block_or_statement
2553 (cp_parser *);
2554 static bool cp_parser_skip_to_closing_brace
2555 (cp_parser *);
2556 static void cp_parser_skip_to_end_of_template_parameter_list
2557 (cp_parser *);
2558 static void cp_parser_skip_to_pragma_eol
2559 (cp_parser*, cp_token *);
2560 static bool cp_parser_error_occurred
2561 (cp_parser *);
2562 static bool cp_parser_allow_gnu_extensions_p
2563 (cp_parser *);
2564 static bool cp_parser_is_pure_string_literal
2565 (cp_token *);
2566 static bool cp_parser_is_string_literal
2567 (cp_token *);
2568 static bool cp_parser_is_keyword
2569 (cp_token *, enum rid);
2570 static tree cp_parser_make_typename_type
2571 (cp_parser *, tree, location_t location);
2572 static cp_declarator * cp_parser_make_indirect_declarator
2573 (enum tree_code, tree, cp_cv_quals, cp_declarator *, tree);
2574 static bool cp_parser_compound_literal_p
2575 (cp_parser *);
2576 static bool cp_parser_array_designator_p
2577 (cp_parser *);
2578 static bool cp_parser_skip_to_closing_square_bracket
2579 (cp_parser *);
2580
2581 /* Concept-related syntactic transformations */
2582
2583 static tree cp_parser_maybe_concept_name (cp_parser *, tree);
2584 static tree cp_parser_maybe_partial_concept_id (cp_parser *, tree, tree);
2585
2586 // -------------------------------------------------------------------------- //
2587 // Unevaluated Operand Guard
2588 //
2589 // Implementation of an RAII helper for unevaluated operand parsing.
2590 cp_unevaluated::cp_unevaluated ()
2591 {
2592 ++cp_unevaluated_operand;
2593 ++c_inhibit_evaluation_warnings;
2594 }
2595
2596 cp_unevaluated::~cp_unevaluated ()
2597 {
2598 --c_inhibit_evaluation_warnings;
2599 --cp_unevaluated_operand;
2600 }
2601
2602 // -------------------------------------------------------------------------- //
2603 // Tentative Parsing
2604
2605 /* Returns nonzero if we are parsing tentatively. */
2606
2607 static inline bool
2608 cp_parser_parsing_tentatively (cp_parser* parser)
2609 {
2610 return parser->context->next != NULL;
2611 }
2612
2613 /* Returns nonzero if TOKEN is a string literal. */
2614
2615 static bool
2616 cp_parser_is_pure_string_literal (cp_token* token)
2617 {
2618 return (token->type == CPP_STRING ||
2619 token->type == CPP_STRING16 ||
2620 token->type == CPP_STRING32 ||
2621 token->type == CPP_WSTRING ||
2622 token->type == CPP_UTF8STRING);
2623 }
2624
2625 /* Returns nonzero if TOKEN is a string literal
2626 of a user-defined string literal. */
2627
2628 static bool
2629 cp_parser_is_string_literal (cp_token* token)
2630 {
2631 return (cp_parser_is_pure_string_literal (token) ||
2632 token->type == CPP_STRING_USERDEF ||
2633 token->type == CPP_STRING16_USERDEF ||
2634 token->type == CPP_STRING32_USERDEF ||
2635 token->type == CPP_WSTRING_USERDEF ||
2636 token->type == CPP_UTF8STRING_USERDEF);
2637 }
2638
2639 /* Returns nonzero if TOKEN is the indicated KEYWORD. */
2640
2641 static bool
2642 cp_parser_is_keyword (cp_token* token, enum rid keyword)
2643 {
2644 return token->keyword == keyword;
2645 }
2646
2647 /* If not parsing tentatively, issue a diagnostic of the form
2648 FILE:LINE: MESSAGE before TOKEN
2649 where TOKEN is the next token in the input stream. MESSAGE
2650 (specified by the caller) is usually of the form "expected
2651 OTHER-TOKEN". */
2652
2653 static void
2654 cp_parser_error (cp_parser* parser, const char* gmsgid)
2655 {
2656 if (!cp_parser_simulate_error (parser))
2657 {
2658 cp_token *token = cp_lexer_peek_token (parser->lexer);
2659 /* This diagnostic makes more sense if it is tagged to the line
2660 of the token we just peeked at. */
2661 cp_lexer_set_source_position_from_token (token);
2662
2663 if (token->type == CPP_PRAGMA)
2664 {
2665 error_at (token->location,
2666 "%<#pragma%> is not allowed here");
2667 cp_parser_skip_to_pragma_eol (parser, token);
2668 return;
2669 }
2670
2671 c_parse_error (gmsgid,
2672 /* Because c_parser_error does not understand
2673 CPP_KEYWORD, keywords are treated like
2674 identifiers. */
2675 (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
2676 token->u.value, token->flags);
2677 }
2678 }
2679
2680 /* Issue an error about name-lookup failing. NAME is the
2681 IDENTIFIER_NODE DECL is the result of
2682 the lookup (as returned from cp_parser_lookup_name). DESIRED is
2683 the thing that we hoped to find. */
2684
2685 static void
2686 cp_parser_name_lookup_error (cp_parser* parser,
2687 tree name,
2688 tree decl,
2689 name_lookup_error desired,
2690 location_t location)
2691 {
2692 /* If name lookup completely failed, tell the user that NAME was not
2693 declared. */
2694 if (decl == error_mark_node)
2695 {
2696 if (parser->scope && parser->scope != global_namespace)
2697 error_at (location, "%<%E::%E%> has not been declared",
2698 parser->scope, name);
2699 else if (parser->scope == global_namespace)
2700 error_at (location, "%<::%E%> has not been declared", name);
2701 else if (parser->object_scope
2702 && !CLASS_TYPE_P (parser->object_scope))
2703 error_at (location, "request for member %qE in non-class type %qT",
2704 name, parser->object_scope);
2705 else if (parser->object_scope)
2706 error_at (location, "%<%T::%E%> has not been declared",
2707 parser->object_scope, name);
2708 else
2709 error_at (location, "%qE has not been declared", name);
2710 }
2711 else if (parser->scope && parser->scope != global_namespace)
2712 {
2713 switch (desired)
2714 {
2715 case NLE_TYPE:
2716 error_at (location, "%<%E::%E%> is not a type",
2717 parser->scope, name);
2718 break;
2719 case NLE_CXX98:
2720 error_at (location, "%<%E::%E%> is not a class or namespace",
2721 parser->scope, name);
2722 break;
2723 case NLE_NOT_CXX98:
2724 error_at (location,
2725 "%<%E::%E%> is not a class, namespace, or enumeration",
2726 parser->scope, name);
2727 break;
2728 default:
2729 gcc_unreachable ();
2730
2731 }
2732 }
2733 else if (parser->scope == global_namespace)
2734 {
2735 switch (desired)
2736 {
2737 case NLE_TYPE:
2738 error_at (location, "%<::%E%> is not a type", name);
2739 break;
2740 case NLE_CXX98:
2741 error_at (location, "%<::%E%> is not a class or namespace", name);
2742 break;
2743 case NLE_NOT_CXX98:
2744 error_at (location,
2745 "%<::%E%> is not a class, namespace, or enumeration",
2746 name);
2747 break;
2748 default:
2749 gcc_unreachable ();
2750 }
2751 }
2752 else
2753 {
2754 switch (desired)
2755 {
2756 case NLE_TYPE:
2757 error_at (location, "%qE is not a type", name);
2758 break;
2759 case NLE_CXX98:
2760 error_at (location, "%qE is not a class or namespace", name);
2761 break;
2762 case NLE_NOT_CXX98:
2763 error_at (location,
2764 "%qE is not a class, namespace, or enumeration", name);
2765 break;
2766 default:
2767 gcc_unreachable ();
2768 }
2769 }
2770 }
2771
2772 /* If we are parsing tentatively, remember that an error has occurred
2773 during this tentative parse. Returns true if the error was
2774 simulated; false if a message should be issued by the caller. */
2775
2776 static bool
2777 cp_parser_simulate_error (cp_parser* parser)
2778 {
2779 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2780 {
2781 parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
2782 return true;
2783 }
2784 return false;
2785 }
2786
2787 /* This function is called when a type is defined. If type
2788 definitions are forbidden at this point, an error message is
2789 issued. */
2790
2791 static bool
2792 cp_parser_check_type_definition (cp_parser* parser)
2793 {
2794 /* If types are forbidden here, issue a message. */
2795 if (parser->type_definition_forbidden_message)
2796 {
2797 /* Don't use `%s' to print the string, because quotations (`%<', `%>')
2798 in the message need to be interpreted. */
2799 error (parser->type_definition_forbidden_message);
2800 return false;
2801 }
2802 return true;
2803 }
2804
2805 /* This function is called when the DECLARATOR is processed. The TYPE
2806 was a type defined in the decl-specifiers. If it is invalid to
2807 define a type in the decl-specifiers for DECLARATOR, an error is
2808 issued. TYPE_LOCATION is the location of TYPE and is used
2809 for error reporting. */
2810
2811 static void
2812 cp_parser_check_for_definition_in_return_type (cp_declarator *declarator,
2813 tree type, location_t type_location)
2814 {
2815 /* [dcl.fct] forbids type definitions in return types.
2816 Unfortunately, it's not easy to know whether or not we are
2817 processing a return type until after the fact. */
2818 while (declarator
2819 && (declarator->kind == cdk_pointer
2820 || declarator->kind == cdk_reference
2821 || declarator->kind == cdk_ptrmem))
2822 declarator = declarator->declarator;
2823 if (declarator
2824 && declarator->kind == cdk_function)
2825 {
2826 error_at (type_location,
2827 "new types may not be defined in a return type");
2828 inform (type_location,
2829 "(perhaps a semicolon is missing after the definition of %qT)",
2830 type);
2831 }
2832 }
2833
2834 /* A type-specifier (TYPE) has been parsed which cannot be followed by
2835 "<" in any valid C++ program. If the next token is indeed "<",
2836 issue a message warning the user about what appears to be an
2837 invalid attempt to form a template-id. LOCATION is the location
2838 of the type-specifier (TYPE) */
2839
2840 static void
2841 cp_parser_check_for_invalid_template_id (cp_parser* parser,
2842 tree type,
2843 enum tag_types tag_type,
2844 location_t location)
2845 {
2846 cp_token_position start = 0;
2847
2848 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
2849 {
2850 if (TYPE_P (type))
2851 error_at (location, "%qT is not a template", type);
2852 else if (identifier_p (type))
2853 {
2854 if (tag_type != none_type)
2855 error_at (location, "%qE is not a class template", type);
2856 else
2857 error_at (location, "%qE is not a template", type);
2858 }
2859 else
2860 error_at (location, "invalid template-id");
2861 /* Remember the location of the invalid "<". */
2862 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2863 start = cp_lexer_token_position (parser->lexer, true);
2864 /* Consume the "<". */
2865 cp_lexer_consume_token (parser->lexer);
2866 /* Parse the template arguments. */
2867 cp_parser_enclosed_template_argument_list (parser);
2868 /* Permanently remove the invalid template arguments so that
2869 this error message is not issued again. */
2870 if (start)
2871 cp_lexer_purge_tokens_after (parser->lexer, start);
2872 }
2873 }
2874
2875 /* If parsing an integral constant-expression, issue an error message
2876 about the fact that THING appeared and return true. Otherwise,
2877 return false. In either case, set
2878 PARSER->NON_INTEGRAL_CONSTANT_EXPRESSION_P. */
2879
2880 static bool
2881 cp_parser_non_integral_constant_expression (cp_parser *parser,
2882 non_integral_constant thing)
2883 {
2884 parser->non_integral_constant_expression_p = true;
2885 if (parser->integral_constant_expression_p)
2886 {
2887 if (!parser->allow_non_integral_constant_expression_p)
2888 {
2889 const char *msg = NULL;
2890 switch (thing)
2891 {
2892 case NIC_FLOAT:
2893 error ("floating-point literal "
2894 "cannot appear in a constant-expression");
2895 return true;
2896 case NIC_CAST:
2897 error ("a cast to a type other than an integral or "
2898 "enumeration type cannot appear in a "
2899 "constant-expression");
2900 return true;
2901 case NIC_TYPEID:
2902 error ("%<typeid%> operator "
2903 "cannot appear in a constant-expression");
2904 return true;
2905 case NIC_NCC:
2906 error ("non-constant compound literals "
2907 "cannot appear in a constant-expression");
2908 return true;
2909 case NIC_FUNC_CALL:
2910 error ("a function call "
2911 "cannot appear in a constant-expression");
2912 return true;
2913 case NIC_INC:
2914 error ("an increment "
2915 "cannot appear in a constant-expression");
2916 return true;
2917 case NIC_DEC:
2918 error ("an decrement "
2919 "cannot appear in a constant-expression");
2920 return true;
2921 case NIC_ARRAY_REF:
2922 error ("an array reference "
2923 "cannot appear in a constant-expression");
2924 return true;
2925 case NIC_ADDR_LABEL:
2926 error ("the address of a label "
2927 "cannot appear in a constant-expression");
2928 return true;
2929 case NIC_OVERLOADED:
2930 error ("calls to overloaded operators "
2931 "cannot appear in a constant-expression");
2932 return true;
2933 case NIC_ASSIGNMENT:
2934 error ("an assignment cannot appear in a constant-expression");
2935 return true;
2936 case NIC_COMMA:
2937 error ("a comma operator "
2938 "cannot appear in a constant-expression");
2939 return true;
2940 case NIC_CONSTRUCTOR:
2941 error ("a call to a constructor "
2942 "cannot appear in a constant-expression");
2943 return true;
2944 case NIC_TRANSACTION:
2945 error ("a transaction expression "
2946 "cannot appear in a constant-expression");
2947 return true;
2948 case NIC_THIS:
2949 msg = "this";
2950 break;
2951 case NIC_FUNC_NAME:
2952 msg = "__FUNCTION__";
2953 break;
2954 case NIC_PRETTY_FUNC:
2955 msg = "__PRETTY_FUNCTION__";
2956 break;
2957 case NIC_C99_FUNC:
2958 msg = "__func__";
2959 break;
2960 case NIC_VA_ARG:
2961 msg = "va_arg";
2962 break;
2963 case NIC_ARROW:
2964 msg = "->";
2965 break;
2966 case NIC_POINT:
2967 msg = ".";
2968 break;
2969 case NIC_STAR:
2970 msg = "*";
2971 break;
2972 case NIC_ADDR:
2973 msg = "&";
2974 break;
2975 case NIC_PREINCREMENT:
2976 msg = "++";
2977 break;
2978 case NIC_PREDECREMENT:
2979 msg = "--";
2980 break;
2981 case NIC_NEW:
2982 msg = "new";
2983 break;
2984 case NIC_DEL:
2985 msg = "delete";
2986 break;
2987 default:
2988 gcc_unreachable ();
2989 }
2990 if (msg)
2991 error ("%qs cannot appear in a constant-expression", msg);
2992 return true;
2993 }
2994 }
2995 return false;
2996 }
2997
2998 /* Emit a diagnostic for an invalid type name. This function commits
2999 to the current active tentative parse, if any. (Otherwise, the
3000 problematic construct might be encountered again later, resulting
3001 in duplicate error messages.) LOCATION is the location of ID. */
3002
3003 static void
3004 cp_parser_diagnose_invalid_type_name (cp_parser *parser, tree id,
3005 location_t location)
3006 {
3007 tree decl, ambiguous_decls;
3008 cp_parser_commit_to_tentative_parse (parser);
3009 /* Try to lookup the identifier. */
3010 decl = cp_parser_lookup_name (parser, id, none_type,
3011 /*is_template=*/false,
3012 /*is_namespace=*/false,
3013 /*check_dependency=*/true,
3014 &ambiguous_decls, location);
3015 if (ambiguous_decls)
3016 /* If the lookup was ambiguous, an error will already have
3017 been issued. */
3018 return;
3019 /* If the lookup found a template-name, it means that the user forgot
3020 to specify an argument list. Emit a useful error message. */
3021 if (DECL_TYPE_TEMPLATE_P (decl))
3022 {
3023 error_at (location,
3024 "invalid use of template-name %qE without an argument list",
3025 decl);
3026 inform (DECL_SOURCE_LOCATION (decl), "%qD declared here", decl);
3027 }
3028 else if (TREE_CODE (id) == BIT_NOT_EXPR)
3029 error_at (location, "invalid use of destructor %qD as a type", id);
3030 else if (TREE_CODE (decl) == TYPE_DECL)
3031 /* Something like 'unsigned A a;' */
3032 error_at (location, "invalid combination of multiple type-specifiers");
3033 else if (!parser->scope)
3034 {
3035 /* Issue an error message. */
3036 error_at (location, "%qE does not name a type", id);
3037 /* If we're in a template class, it's possible that the user was
3038 referring to a type from a base class. For example:
3039
3040 template <typename T> struct A { typedef T X; };
3041 template <typename T> struct B : public A<T> { X x; };
3042
3043 The user should have said "typename A<T>::X". */
3044 if (cxx_dialect < cxx11 && id == ridpointers[(int)RID_CONSTEXPR])
3045 inform (location, "C++11 %<constexpr%> only available with "
3046 "-std=c++11 or -std=gnu++11");
3047 else if (cxx_dialect < cxx11 && id == ridpointers[(int)RID_NOEXCEPT])
3048 inform (location, "C++11 %<noexcept%> only available with "
3049 "-std=c++11 or -std=gnu++11");
3050 else if (cxx_dialect < cxx11
3051 && TREE_CODE (id) == IDENTIFIER_NODE
3052 && !strcmp (IDENTIFIER_POINTER (id), "thread_local"))
3053 inform (location, "C++11 %<thread_local%> only available with "
3054 "-std=c++11 or -std=gnu++11");
3055 else if (processing_template_decl && current_class_type
3056 && TYPE_BINFO (current_class_type))
3057 {
3058 tree b;
3059
3060 for (b = TREE_CHAIN (TYPE_BINFO (current_class_type));
3061 b;
3062 b = TREE_CHAIN (b))
3063 {
3064 tree base_type = BINFO_TYPE (b);
3065 if (CLASS_TYPE_P (base_type)
3066 && dependent_type_p (base_type))
3067 {
3068 tree field;
3069 /* Go from a particular instantiation of the
3070 template (which will have an empty TYPE_FIELDs),
3071 to the main version. */
3072 base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type);
3073 for (field = TYPE_FIELDS (base_type);
3074 field;
3075 field = DECL_CHAIN (field))
3076 if (TREE_CODE (field) == TYPE_DECL
3077 && DECL_NAME (field) == id)
3078 {
3079 inform (location,
3080 "(perhaps %<typename %T::%E%> was intended)",
3081 BINFO_TYPE (b), id);
3082 break;
3083 }
3084 if (field)
3085 break;
3086 }
3087 }
3088 }
3089 }
3090 /* Here we diagnose qualified-ids where the scope is actually correct,
3091 but the identifier does not resolve to a valid type name. */
3092 else if (parser->scope != error_mark_node)
3093 {
3094 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
3095 {
3096 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3097 error_at (location_of (id),
3098 "%qE in namespace %qE does not name a template type",
3099 id, parser->scope);
3100 else
3101 error_at (location_of (id),
3102 "%qE in namespace %qE does not name a type",
3103 id, parser->scope);
3104 if (DECL_P (decl))
3105 inform (DECL_SOURCE_LOCATION (decl), "%qD declared here", decl);
3106 }
3107 else if (CLASS_TYPE_P (parser->scope)
3108 && constructor_name_p (id, parser->scope))
3109 {
3110 /* A<T>::A<T>() */
3111 error_at (location, "%<%T::%E%> names the constructor, not"
3112 " the type", parser->scope, id);
3113 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3114 error_at (location, "and %qT has no template constructors",
3115 parser->scope);
3116 }
3117 else if (TYPE_P (parser->scope)
3118 && dependent_scope_p (parser->scope))
3119 error_at (location, "need %<typename%> before %<%T::%E%> because "
3120 "%qT is a dependent scope",
3121 parser->scope, id, parser->scope);
3122 else if (TYPE_P (parser->scope))
3123 {
3124 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3125 error_at (location_of (id),
3126 "%qE in %q#T does not name a template type",
3127 id, parser->scope);
3128 else
3129 error_at (location_of (id),
3130 "%qE in %q#T does not name a type",
3131 id, parser->scope);
3132 if (DECL_P (decl))
3133 inform (DECL_SOURCE_LOCATION (decl), "%qD declared here", decl);
3134 }
3135 else
3136 gcc_unreachable ();
3137 }
3138 }
3139
3140 /* Check for a common situation where a type-name should be present,
3141 but is not, and issue a sensible error message. Returns true if an
3142 invalid type-name was detected.
3143
3144 The situation handled by this function are variable declarations of the
3145 form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
3146 Usually, `ID' should name a type, but if we got here it means that it
3147 does not. We try to emit the best possible error message depending on
3148 how exactly the id-expression looks like. */
3149
3150 static bool
3151 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser *parser)
3152 {
3153 tree id;
3154 cp_token *token = cp_lexer_peek_token (parser->lexer);
3155
3156 /* Avoid duplicate error about ambiguous lookup. */
3157 if (token->type == CPP_NESTED_NAME_SPECIFIER)
3158 {
3159 cp_token *next = cp_lexer_peek_nth_token (parser->lexer, 2);
3160 if (next->type == CPP_NAME && next->error_reported)
3161 goto out;
3162 }
3163
3164 cp_parser_parse_tentatively (parser);
3165 id = cp_parser_id_expression (parser,
3166 /*template_keyword_p=*/false,
3167 /*check_dependency_p=*/true,
3168 /*template_p=*/NULL,
3169 /*declarator_p=*/true,
3170 /*optional_p=*/false);
3171 /* If the next token is a (, this is a function with no explicit return
3172 type, i.e. constructor, destructor or conversion op. */
3173 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
3174 || TREE_CODE (id) == TYPE_DECL)
3175 {
3176 cp_parser_abort_tentative_parse (parser);
3177 return false;
3178 }
3179 if (!cp_parser_parse_definitely (parser))
3180 return false;
3181
3182 /* Emit a diagnostic for the invalid type. */
3183 cp_parser_diagnose_invalid_type_name (parser, id, token->location);
3184 out:
3185 /* If we aren't in the middle of a declarator (i.e. in a
3186 parameter-declaration-clause), skip to the end of the declaration;
3187 there's no point in trying to process it. */
3188 if (!parser->in_declarator_p)
3189 cp_parser_skip_to_end_of_block_or_statement (parser);
3190 return true;
3191 }
3192
3193 /* Consume tokens up to, and including, the next non-nested closing `)'.
3194 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
3195 are doing error recovery. Returns -1 if OR_TTYPE is not CPP_EOF and we
3196 found an unnested token of that type. */
3197
3198 static int
3199 cp_parser_skip_to_closing_parenthesis_1 (cp_parser *parser,
3200 bool recovering,
3201 cpp_ttype or_ttype,
3202 bool consume_paren)
3203 {
3204 unsigned paren_depth = 0;
3205 unsigned brace_depth = 0;
3206 unsigned square_depth = 0;
3207
3208 if (recovering && or_ttype == CPP_EOF
3209 && cp_parser_uncommitted_to_tentative_parse_p (parser))
3210 return 0;
3211
3212 while (true)
3213 {
3214 cp_token * token = cp_lexer_peek_token (parser->lexer);
3215
3216 /* Have we found what we're looking for before the closing paren? */
3217 if (token->type == or_ttype && or_ttype != CPP_EOF
3218 && !brace_depth && !paren_depth && !square_depth)
3219 return -1;
3220
3221 switch (token->type)
3222 {
3223 case CPP_EOF:
3224 case CPP_PRAGMA_EOL:
3225 /* If we've run out of tokens, then there is no closing `)'. */
3226 return 0;
3227
3228 /* This is good for lambda expression capture-lists. */
3229 case CPP_OPEN_SQUARE:
3230 ++square_depth;
3231 break;
3232 case CPP_CLOSE_SQUARE:
3233 if (!square_depth--)
3234 return 0;
3235 break;
3236
3237 case CPP_SEMICOLON:
3238 /* This matches the processing in skip_to_end_of_statement. */
3239 if (!brace_depth)
3240 return 0;
3241 break;
3242
3243 case CPP_OPEN_BRACE:
3244 ++brace_depth;
3245 break;
3246 case CPP_CLOSE_BRACE:
3247 if (!brace_depth--)
3248 return 0;
3249 break;
3250
3251 case CPP_OPEN_PAREN:
3252 if (!brace_depth)
3253 ++paren_depth;
3254 break;
3255
3256 case CPP_CLOSE_PAREN:
3257 if (!brace_depth && !paren_depth--)
3258 {
3259 if (consume_paren)
3260 cp_lexer_consume_token (parser->lexer);
3261 return 1;
3262 }
3263 break;
3264
3265 default:
3266 break;
3267 }
3268
3269 /* Consume the token. */
3270 cp_lexer_consume_token (parser->lexer);
3271 }
3272 }
3273
3274 /* Consume tokens up to, and including, the next non-nested closing `)'.
3275 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
3276 are doing error recovery. Returns -1 if OR_COMMA is true and we
3277 found an unnested token of that type. */
3278
3279 static int
3280 cp_parser_skip_to_closing_parenthesis (cp_parser *parser,
3281 bool recovering,
3282 bool or_comma,
3283 bool consume_paren)
3284 {
3285 cpp_ttype ttype = or_comma ? CPP_COMMA : CPP_EOF;
3286 return cp_parser_skip_to_closing_parenthesis_1 (parser, recovering,
3287 ttype, consume_paren);
3288 }
3289
3290 /* Consume tokens until we reach the end of the current statement.
3291 Normally, that will be just before consuming a `;'. However, if a
3292 non-nested `}' comes first, then we stop before consuming that. */
3293
3294 static void
3295 cp_parser_skip_to_end_of_statement (cp_parser* parser)
3296 {
3297 unsigned nesting_depth = 0;
3298
3299 /* Unwind generic function template scope if necessary. */
3300 if (parser->fully_implicit_function_template_p)
3301 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
3302
3303 while (true)
3304 {
3305 cp_token *token = cp_lexer_peek_token (parser->lexer);
3306
3307 switch (token->type)
3308 {
3309 case CPP_EOF:
3310 case CPP_PRAGMA_EOL:
3311 /* If we've run out of tokens, stop. */
3312 return;
3313
3314 case CPP_SEMICOLON:
3315 /* If the next token is a `;', we have reached the end of the
3316 statement. */
3317 if (!nesting_depth)
3318 return;
3319 break;
3320
3321 case CPP_CLOSE_BRACE:
3322 /* If this is a non-nested '}', stop before consuming it.
3323 That way, when confronted with something like:
3324
3325 { 3 + }
3326
3327 we stop before consuming the closing '}', even though we
3328 have not yet reached a `;'. */
3329 if (nesting_depth == 0)
3330 return;
3331
3332 /* If it is the closing '}' for a block that we have
3333 scanned, stop -- but only after consuming the token.
3334 That way given:
3335
3336 void f g () { ... }
3337 typedef int I;
3338
3339 we will stop after the body of the erroneously declared
3340 function, but before consuming the following `typedef'
3341 declaration. */
3342 if (--nesting_depth == 0)
3343 {
3344 cp_lexer_consume_token (parser->lexer);
3345 return;
3346 }
3347
3348 case CPP_OPEN_BRACE:
3349 ++nesting_depth;
3350 break;
3351
3352 default:
3353 break;
3354 }
3355
3356 /* Consume the token. */
3357 cp_lexer_consume_token (parser->lexer);
3358 }
3359 }
3360
3361 /* This function is called at the end of a statement or declaration.
3362 If the next token is a semicolon, it is consumed; otherwise, error
3363 recovery is attempted. */
3364
3365 static void
3366 cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
3367 {
3368 /* Look for the trailing `;'. */
3369 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
3370 {
3371 /* If there is additional (erroneous) input, skip to the end of
3372 the statement. */
3373 cp_parser_skip_to_end_of_statement (parser);
3374 /* If the next token is now a `;', consume it. */
3375 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
3376 cp_lexer_consume_token (parser->lexer);
3377 }
3378 }
3379
3380 /* Skip tokens until we have consumed an entire block, or until we
3381 have consumed a non-nested `;'. */
3382
3383 static void
3384 cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
3385 {
3386 int nesting_depth = 0;
3387
3388 /* Unwind generic function template scope if necessary. */
3389 if (parser->fully_implicit_function_template_p)
3390 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
3391
3392 while (nesting_depth >= 0)
3393 {
3394 cp_token *token = cp_lexer_peek_token (parser->lexer);
3395
3396 switch (token->type)
3397 {
3398 case CPP_EOF:
3399 case CPP_PRAGMA_EOL:
3400 /* If we've run out of tokens, stop. */
3401 return;
3402
3403 case CPP_SEMICOLON:
3404 /* Stop if this is an unnested ';'. */
3405 if (!nesting_depth)
3406 nesting_depth = -1;
3407 break;
3408
3409 case CPP_CLOSE_BRACE:
3410 /* Stop if this is an unnested '}', or closes the outermost
3411 nesting level. */
3412 nesting_depth--;
3413 if (nesting_depth < 0)
3414 return;
3415 if (!nesting_depth)
3416 nesting_depth = -1;
3417 break;
3418
3419 case CPP_OPEN_BRACE:
3420 /* Nest. */
3421 nesting_depth++;
3422 break;
3423
3424 default:
3425 break;
3426 }
3427
3428 /* Consume the token. */
3429 cp_lexer_consume_token (parser->lexer);
3430 }
3431 }
3432
3433 /* Skip tokens until a non-nested closing curly brace is the next
3434 token, or there are no more tokens. Return true in the first case,
3435 false otherwise. */
3436
3437 static bool
3438 cp_parser_skip_to_closing_brace (cp_parser *parser)
3439 {
3440 unsigned nesting_depth = 0;
3441
3442 while (true)
3443 {
3444 cp_token *token = cp_lexer_peek_token (parser->lexer);
3445
3446 switch (token->type)
3447 {
3448 case CPP_EOF:
3449 case CPP_PRAGMA_EOL:
3450 /* If we've run out of tokens, stop. */
3451 return false;
3452
3453 case CPP_CLOSE_BRACE:
3454 /* If the next token is a non-nested `}', then we have reached
3455 the end of the current block. */
3456 if (nesting_depth-- == 0)
3457 return true;
3458 break;
3459
3460 case CPP_OPEN_BRACE:
3461 /* If it the next token is a `{', then we are entering a new
3462 block. Consume the entire block. */
3463 ++nesting_depth;
3464 break;
3465
3466 default:
3467 break;
3468 }
3469
3470 /* Consume the token. */
3471 cp_lexer_consume_token (parser->lexer);
3472 }
3473 }
3474
3475 /* Consume tokens until we reach the end of the pragma. The PRAGMA_TOK
3476 parameter is the PRAGMA token, allowing us to purge the entire pragma
3477 sequence. */
3478
3479 static void
3480 cp_parser_skip_to_pragma_eol (cp_parser* parser, cp_token *pragma_tok)
3481 {
3482 cp_token *token;
3483
3484 parser->lexer->in_pragma = false;
3485
3486 do
3487 token = cp_lexer_consume_token (parser->lexer);
3488 while (token->type != CPP_PRAGMA_EOL && token->type != CPP_EOF);
3489
3490 /* Ensure that the pragma is not parsed again. */
3491 cp_lexer_purge_tokens_after (parser->lexer, pragma_tok);
3492 }
3493
3494 /* Require pragma end of line, resyncing with it as necessary. The
3495 arguments are as for cp_parser_skip_to_pragma_eol. */
3496
3497 static void
3498 cp_parser_require_pragma_eol (cp_parser *parser, cp_token *pragma_tok)
3499 {
3500 parser->lexer->in_pragma = false;
3501 if (!cp_parser_require (parser, CPP_PRAGMA_EOL, RT_PRAGMA_EOL))
3502 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
3503 }
3504
3505 /* This is a simple wrapper around make_typename_type. When the id is
3506 an unresolved identifier node, we can provide a superior diagnostic
3507 using cp_parser_diagnose_invalid_type_name. */
3508
3509 static tree
3510 cp_parser_make_typename_type (cp_parser *parser, tree id,
3511 location_t id_location)
3512 {
3513 tree result;
3514 if (identifier_p (id))
3515 {
3516 result = make_typename_type (parser->scope, id, typename_type,
3517 /*complain=*/tf_none);
3518 if (result == error_mark_node)
3519 cp_parser_diagnose_invalid_type_name (parser, id, id_location);
3520 return result;
3521 }
3522 return make_typename_type (parser->scope, id, typename_type, tf_error);
3523 }
3524
3525 /* This is a wrapper around the
3526 make_{pointer,ptrmem,reference}_declarator functions that decides
3527 which one to call based on the CODE and CLASS_TYPE arguments. The
3528 CODE argument should be one of the values returned by
3529 cp_parser_ptr_operator. ATTRIBUTES represent the attributes that
3530 appertain to the pointer or reference. */
3531
3532 static cp_declarator *
3533 cp_parser_make_indirect_declarator (enum tree_code code, tree class_type,
3534 cp_cv_quals cv_qualifiers,
3535 cp_declarator *target,
3536 tree attributes)
3537 {
3538 if (code == ERROR_MARK)
3539 return cp_error_declarator;
3540
3541 if (code == INDIRECT_REF)
3542 if (class_type == NULL_TREE)
3543 return make_pointer_declarator (cv_qualifiers, target, attributes);
3544 else
3545 return make_ptrmem_declarator (cv_qualifiers, class_type,
3546 target, attributes);
3547 else if (code == ADDR_EXPR && class_type == NULL_TREE)
3548 return make_reference_declarator (cv_qualifiers, target,
3549 false, attributes);
3550 else if (code == NON_LVALUE_EXPR && class_type == NULL_TREE)
3551 return make_reference_declarator (cv_qualifiers, target,
3552 true, attributes);
3553 gcc_unreachable ();
3554 }
3555
3556 /* Create a new C++ parser. */
3557
3558 static cp_parser *
3559 cp_parser_new (void)
3560 {
3561 cp_parser *parser;
3562 cp_lexer *lexer;
3563 unsigned i;
3564
3565 /* cp_lexer_new_main is called before doing GC allocation because
3566 cp_lexer_new_main might load a PCH file. */
3567 lexer = cp_lexer_new_main ();
3568
3569 /* Initialize the binops_by_token so that we can get the tree
3570 directly from the token. */
3571 for (i = 0; i < sizeof (binops) / sizeof (binops[0]); i++)
3572 binops_by_token[binops[i].token_type] = binops[i];
3573
3574 parser = ggc_cleared_alloc<cp_parser> ();
3575 parser->lexer = lexer;
3576 parser->context = cp_parser_context_new (NULL);
3577
3578 /* For now, we always accept GNU extensions. */
3579 parser->allow_gnu_extensions_p = 1;
3580
3581 /* The `>' token is a greater-than operator, not the end of a
3582 template-id. */
3583 parser->greater_than_is_operator_p = true;
3584
3585 parser->default_arg_ok_p = true;
3586
3587 /* We are not parsing a constant-expression. */
3588 parser->integral_constant_expression_p = false;
3589 parser->allow_non_integral_constant_expression_p = false;
3590 parser->non_integral_constant_expression_p = false;
3591
3592 /* Local variable names are not forbidden. */
3593 parser->local_variables_forbidden_p = false;
3594
3595 /* We are not processing an `extern "C"' declaration. */
3596 parser->in_unbraced_linkage_specification_p = false;
3597
3598 /* We are not processing a declarator. */
3599 parser->in_declarator_p = false;
3600
3601 /* We are not processing a template-argument-list. */
3602 parser->in_template_argument_list_p = false;
3603
3604 /* We are not in an iteration statement. */
3605 parser->in_statement = 0;
3606
3607 /* We are not in a switch statement. */
3608 parser->in_switch_statement_p = false;
3609
3610 /* We are not parsing a type-id inside an expression. */
3611 parser->in_type_id_in_expr_p = false;
3612
3613 /* Declarations aren't implicitly extern "C". */
3614 parser->implicit_extern_c = false;
3615
3616 /* String literals should be translated to the execution character set. */
3617 parser->translate_strings_p = true;
3618
3619 /* We are not parsing a function body. */
3620 parser->in_function_body = false;
3621
3622 /* We can correct until told otherwise. */
3623 parser->colon_corrects_to_scope_p = true;
3624
3625 /* The unparsed function queue is empty. */
3626 push_unparsed_function_queues (parser);
3627
3628 /* There are no classes being defined. */
3629 parser->num_classes_being_defined = 0;
3630
3631 /* No template parameters apply. */
3632 parser->num_template_parameter_lists = 0;
3633
3634 /* Not declaring an implicit function template. */
3635 parser->auto_is_implicit_function_template_parm_p = false;
3636 parser->fully_implicit_function_template_p = false;
3637 parser->implicit_template_parms = 0;
3638 parser->implicit_template_scope = 0;
3639
3640 /* Active OpenACC routine clauses. */
3641 parser->oacc_routine = NULL;
3642
3643 /* Allow constrained-type-specifiers. */
3644 parser->prevent_constrained_type_specifiers = 0;
3645
3646 return parser;
3647 }
3648
3649 /* Create a cp_lexer structure which will emit the tokens in CACHE
3650 and push it onto the parser's lexer stack. This is used for delayed
3651 parsing of in-class method bodies and default arguments, and should
3652 not be confused with tentative parsing. */
3653 static void
3654 cp_parser_push_lexer_for_tokens (cp_parser *parser, cp_token_cache *cache)
3655 {
3656 cp_lexer *lexer = cp_lexer_new_from_tokens (cache);
3657 lexer->next = parser->lexer;
3658 parser->lexer = lexer;
3659
3660 /* Move the current source position to that of the first token in the
3661 new lexer. */
3662 cp_lexer_set_source_position_from_token (lexer->next_token);
3663 }
3664
3665 /* Pop the top lexer off the parser stack. This is never used for the
3666 "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens. */
3667 static void
3668 cp_parser_pop_lexer (cp_parser *parser)
3669 {
3670 cp_lexer *lexer = parser->lexer;
3671 parser->lexer = lexer->next;
3672 cp_lexer_destroy (lexer);
3673
3674 /* Put the current source position back where it was before this
3675 lexer was pushed. */
3676 cp_lexer_set_source_position_from_token (parser->lexer->next_token);
3677 }
3678
3679 /* Lexical conventions [gram.lex] */
3680
3681 /* Parse an identifier. Returns an IDENTIFIER_NODE representing the
3682 identifier. */
3683
3684 static tree
3685 cp_parser_identifier (cp_parser* parser)
3686 {
3687 cp_token *token;
3688
3689 /* Look for the identifier. */
3690 token = cp_parser_require (parser, CPP_NAME, RT_NAME);
3691 /* Return the value. */
3692 return token ? token->u.value : error_mark_node;
3693 }
3694
3695 /* Parse a sequence of adjacent string constants. Returns a
3696 TREE_STRING representing the combined, nul-terminated string
3697 constant. If TRANSLATE is true, translate the string to the
3698 execution character set. If WIDE_OK is true, a wide string is
3699 invalid here.
3700
3701 C++98 [lex.string] says that if a narrow string literal token is
3702 adjacent to a wide string literal token, the behavior is undefined.
3703 However, C99 6.4.5p4 says that this results in a wide string literal.
3704 We follow C99 here, for consistency with the C front end.
3705
3706 This code is largely lifted from lex_string() in c-lex.c.
3707
3708 FUTURE: ObjC++ will need to handle @-strings here. */
3709 static tree
3710 cp_parser_string_literal (cp_parser *parser, bool translate, bool wide_ok,
3711 bool lookup_udlit = true)
3712 {
3713 tree value;
3714 size_t count;
3715 struct obstack str_ob;
3716 cpp_string str, istr, *strs;
3717 cp_token *tok;
3718 enum cpp_ttype type, curr_type;
3719 int have_suffix_p = 0;
3720 tree string_tree;
3721 tree suffix_id = NULL_TREE;
3722 bool curr_tok_is_userdef_p = false;
3723
3724 tok = cp_lexer_peek_token (parser->lexer);
3725 if (!cp_parser_is_string_literal (tok))
3726 {
3727 cp_parser_error (parser, "expected string-literal");
3728 return error_mark_node;
3729 }
3730
3731 if (cpp_userdef_string_p (tok->type))
3732 {
3733 string_tree = USERDEF_LITERAL_VALUE (tok->u.value);
3734 curr_type = cpp_userdef_string_remove_type (tok->type);
3735 curr_tok_is_userdef_p = true;
3736 }
3737 else
3738 {
3739 string_tree = tok->u.value;
3740 curr_type = tok->type;
3741 }
3742 type = curr_type;
3743
3744 /* Try to avoid the overhead of creating and destroying an obstack
3745 for the common case of just one string. */
3746 if (!cp_parser_is_string_literal
3747 (cp_lexer_peek_nth_token (parser->lexer, 2)))
3748 {
3749 cp_lexer_consume_token (parser->lexer);
3750
3751 str.text = (const unsigned char *)TREE_STRING_POINTER (string_tree);
3752 str.len = TREE_STRING_LENGTH (string_tree);
3753 count = 1;
3754
3755 if (curr_tok_is_userdef_p)
3756 {
3757 suffix_id = USERDEF_LITERAL_SUFFIX_ID (tok->u.value);
3758 have_suffix_p = 1;
3759 curr_type = cpp_userdef_string_remove_type (tok->type);
3760 }
3761 else
3762 curr_type = tok->type;
3763
3764 strs = &str;
3765 }
3766 else
3767 {
3768 gcc_obstack_init (&str_ob);
3769 count = 0;
3770
3771 do
3772 {
3773 cp_lexer_consume_token (parser->lexer);
3774 count++;
3775 str.text = (const unsigned char *)TREE_STRING_POINTER (string_tree);
3776 str.len = TREE_STRING_LENGTH (string_tree);
3777
3778 if (curr_tok_is_userdef_p)
3779 {
3780 tree curr_suffix_id = USERDEF_LITERAL_SUFFIX_ID (tok->u.value);
3781 if (have_suffix_p == 0)
3782 {
3783 suffix_id = curr_suffix_id;
3784 have_suffix_p = 1;
3785 }
3786 else if (have_suffix_p == 1
3787 && curr_suffix_id != suffix_id)
3788 {
3789 error ("inconsistent user-defined literal suffixes"
3790 " %qD and %qD in string literal",
3791 suffix_id, curr_suffix_id);
3792 have_suffix_p = -1;
3793 }
3794 curr_type = cpp_userdef_string_remove_type (tok->type);
3795 }
3796 else
3797 curr_type = tok->type;
3798
3799 if (type != curr_type)
3800 {
3801 if (type == CPP_STRING)
3802 type = curr_type;
3803 else if (curr_type != CPP_STRING)
3804 error_at (tok->location,
3805 "unsupported non-standard concatenation "
3806 "of string literals");
3807 }
3808
3809 obstack_grow (&str_ob, &str, sizeof (cpp_string));
3810
3811 tok = cp_lexer_peek_token (parser->lexer);
3812 if (cpp_userdef_string_p (tok->type))
3813 {
3814 string_tree = USERDEF_LITERAL_VALUE (tok->u.value);
3815 curr_type = cpp_userdef_string_remove_type (tok->type);
3816 curr_tok_is_userdef_p = true;
3817 }
3818 else
3819 {
3820 string_tree = tok->u.value;
3821 curr_type = tok->type;
3822 curr_tok_is_userdef_p = false;
3823 }
3824 }
3825 while (cp_parser_is_string_literal (tok));
3826
3827 strs = (cpp_string *) obstack_finish (&str_ob);
3828 }
3829
3830 if (type != CPP_STRING && !wide_ok)
3831 {
3832 cp_parser_error (parser, "a wide string is invalid in this context");
3833 type = CPP_STRING;
3834 }
3835
3836 if ((translate ? cpp_interpret_string : cpp_interpret_string_notranslate)
3837 (parse_in, strs, count, &istr, type))
3838 {
3839 value = build_string (istr.len, (const char *)istr.text);
3840 free (CONST_CAST (unsigned char *, istr.text));
3841
3842 switch (type)
3843 {
3844 default:
3845 case CPP_STRING:
3846 case CPP_UTF8STRING:
3847 TREE_TYPE (value) = char_array_type_node;
3848 break;
3849 case CPP_STRING16:
3850 TREE_TYPE (value) = char16_array_type_node;
3851 break;
3852 case CPP_STRING32:
3853 TREE_TYPE (value) = char32_array_type_node;
3854 break;
3855 case CPP_WSTRING:
3856 TREE_TYPE (value) = wchar_array_type_node;
3857 break;
3858 }
3859
3860 value = fix_string_type (value);
3861
3862 if (have_suffix_p)
3863 {
3864 tree literal = build_userdef_literal (suffix_id, value,
3865 OT_NONE, NULL_TREE);
3866 if (lookup_udlit)
3867 value = cp_parser_userdef_string_literal (literal);
3868 else
3869 value = literal;
3870 }
3871 }
3872 else
3873 /* cpp_interpret_string has issued an error. */
3874 value = error_mark_node;
3875
3876 if (count > 1)
3877 obstack_free (&str_ob, 0);
3878
3879 return value;
3880 }
3881
3882 /* Look up a literal operator with the name and the exact arguments. */
3883
3884 static tree
3885 lookup_literal_operator (tree name, vec<tree, va_gc> *args)
3886 {
3887 tree decl, fns;
3888 decl = lookup_name (name);
3889 if (!decl || !is_overloaded_fn (decl))
3890 return error_mark_node;
3891
3892 for (fns = decl; fns; fns = OVL_NEXT (fns))
3893 {
3894 unsigned int ix;
3895 bool found = true;
3896 tree fn = OVL_CURRENT (fns);
3897 tree parmtypes = TYPE_ARG_TYPES (TREE_TYPE (fn));
3898 if (parmtypes != NULL_TREE)
3899 {
3900 for (ix = 0; ix < vec_safe_length (args) && parmtypes != NULL_TREE;
3901 ++ix, parmtypes = TREE_CHAIN (parmtypes))
3902 {
3903 tree tparm = TREE_VALUE (parmtypes);
3904 tree targ = TREE_TYPE ((*args)[ix]);
3905 bool ptr = TYPE_PTR_P (tparm);
3906 bool arr = TREE_CODE (targ) == ARRAY_TYPE;
3907 if ((ptr || arr || !same_type_p (tparm, targ))
3908 && (!ptr || !arr
3909 || !same_type_p (TREE_TYPE (tparm),
3910 TREE_TYPE (targ))))
3911 found = false;
3912 }
3913 if (found
3914 && ix == vec_safe_length (args)
3915 /* May be this should be sufficient_parms_p instead,
3916 depending on how exactly should user-defined literals
3917 work in presence of default arguments on the literal
3918 operator parameters. */
3919 && parmtypes == void_list_node)
3920 return decl;
3921 }
3922 }
3923
3924 return error_mark_node;
3925 }
3926
3927 /* Parse a user-defined char constant. Returns a call to a user-defined
3928 literal operator taking the character as an argument. */
3929
3930 static tree
3931 cp_parser_userdef_char_literal (cp_parser *parser)
3932 {
3933 cp_token *token = cp_lexer_consume_token (parser->lexer);
3934 tree literal = token->u.value;
3935 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
3936 tree value = USERDEF_LITERAL_VALUE (literal);
3937 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
3938 tree decl, result;
3939
3940 /* Build up a call to the user-defined operator */
3941 /* Lookup the name we got back from the id-expression. */
3942 vec<tree, va_gc> *args = make_tree_vector ();
3943 vec_safe_push (args, value);
3944 decl = lookup_literal_operator (name, args);
3945 if (!decl || decl == error_mark_node)
3946 {
3947 error ("unable to find character literal operator %qD with %qT argument",
3948 name, TREE_TYPE (value));
3949 release_tree_vector (args);
3950 return error_mark_node;
3951 }
3952 result = finish_call_expr (decl, &args, false, true, tf_warning_or_error);
3953 release_tree_vector (args);
3954 return result;
3955 }
3956
3957 /* A subroutine of cp_parser_userdef_numeric_literal to
3958 create a char... template parameter pack from a string node. */
3959
3960 static tree
3961 make_char_string_pack (tree value)
3962 {
3963 tree charvec;
3964 tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
3965 const char *str = TREE_STRING_POINTER (value);
3966 int i, len = TREE_STRING_LENGTH (value) - 1;
3967 tree argvec = make_tree_vec (1);
3968
3969 /* Fill in CHARVEC with all of the parameters. */
3970 charvec = make_tree_vec (len);
3971 for (i = 0; i < len; ++i)
3972 TREE_VEC_ELT (charvec, i) = build_int_cst (char_type_node, str[i]);
3973
3974 /* Build the argument packs. */
3975 SET_ARGUMENT_PACK_ARGS (argpack, charvec);
3976 TREE_TYPE (argpack) = char_type_node;
3977
3978 TREE_VEC_ELT (argvec, 0) = argpack;
3979
3980 return argvec;
3981 }
3982
3983 /* A subroutine of cp_parser_userdef_numeric_literal to
3984 create a char... template parameter pack from a string node. */
3985
3986 static tree
3987 make_string_pack (tree value)
3988 {
3989 tree charvec;
3990 tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
3991 const unsigned char *str
3992 = (const unsigned char *) TREE_STRING_POINTER (value);
3993 int sz = TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (value))));
3994 int len = TREE_STRING_LENGTH (value) / sz - 1;
3995 tree argvec = make_tree_vec (2);
3996
3997 tree str_char_type_node = TREE_TYPE (TREE_TYPE (value));
3998 str_char_type_node = TYPE_MAIN_VARIANT (str_char_type_node);
3999
4000 /* First template parm is character type. */
4001 TREE_VEC_ELT (argvec, 0) = str_char_type_node;
4002
4003 /* Fill in CHARVEC with all of the parameters. */
4004 charvec = make_tree_vec (len);
4005 for (int i = 0; i < len; ++i)
4006 TREE_VEC_ELT (charvec, i)
4007 = double_int_to_tree (str_char_type_node,
4008 double_int::from_buffer (str + i * sz, sz));
4009
4010 /* Build the argument packs. */
4011 SET_ARGUMENT_PACK_ARGS (argpack, charvec);
4012 TREE_TYPE (argpack) = str_char_type_node;
4013
4014 TREE_VEC_ELT (argvec, 1) = argpack;
4015
4016 return argvec;
4017 }
4018
4019 /* Parse a user-defined numeric constant. returns a call to a user-defined
4020 literal operator. */
4021
4022 static tree
4023 cp_parser_userdef_numeric_literal (cp_parser *parser)
4024 {
4025 cp_token *token = cp_lexer_consume_token (parser->lexer);
4026 tree literal = token->u.value;
4027 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
4028 tree value = USERDEF_LITERAL_VALUE (literal);
4029 int overflow = USERDEF_LITERAL_OVERFLOW (literal);
4030 tree num_string = USERDEF_LITERAL_NUM_STRING (literal);
4031 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
4032 tree decl, result;
4033 vec<tree, va_gc> *args;
4034
4035 /* Look for a literal operator taking the exact type of numeric argument
4036 as the literal value. */
4037 args = make_tree_vector ();
4038 vec_safe_push (args, value);
4039 decl = lookup_literal_operator (name, args);
4040 if (decl && decl != error_mark_node)
4041 {
4042 result = finish_call_expr (decl, &args, false, true,
4043 tf_warning_or_error);
4044
4045 if (TREE_CODE (TREE_TYPE (value)) == INTEGER_TYPE && overflow > 0)
4046 {
4047 warning_at (token->location, OPT_Woverflow,
4048 "integer literal exceeds range of %qT type",
4049 long_long_unsigned_type_node);
4050 }
4051 else
4052 {
4053 if (overflow > 0)
4054 warning_at (token->location, OPT_Woverflow,
4055 "floating literal exceeds range of %qT type",
4056 long_double_type_node);
4057 else if (overflow < 0)
4058 warning_at (token->location, OPT_Woverflow,
4059 "floating literal truncated to zero");
4060 }
4061
4062 release_tree_vector (args);
4063 return result;
4064 }
4065 release_tree_vector (args);
4066
4067 /* If the numeric argument didn't work, look for a raw literal
4068 operator taking a const char* argument consisting of the number
4069 in string format. */
4070 args = make_tree_vector ();
4071 vec_safe_push (args, num_string);
4072 decl = lookup_literal_operator (name, args);
4073 if (decl && decl != error_mark_node)
4074 {
4075 result = finish_call_expr (decl, &args, false, true,
4076 tf_warning_or_error);
4077 release_tree_vector (args);
4078 return result;
4079 }
4080 release_tree_vector (args);
4081
4082 /* If the raw literal didn't work, look for a non-type template
4083 function with parameter pack char.... Call the function with
4084 template parameter characters representing the number. */
4085 args = make_tree_vector ();
4086 decl = lookup_literal_operator (name, args);
4087 if (decl && decl != error_mark_node)
4088 {
4089 tree tmpl_args = make_char_string_pack (num_string);
4090 decl = lookup_template_function (decl, tmpl_args);
4091 result = finish_call_expr (decl, &args, false, true,
4092 tf_warning_or_error);
4093 release_tree_vector (args);
4094 return result;
4095 }
4096
4097 release_tree_vector (args);
4098
4099 error ("unable to find numeric literal operator %qD", name);
4100 if (!cpp_get_options (parse_in)->ext_numeric_literals)
4101 inform (token->location, "use -std=gnu++11 or -fext-numeric-literals "
4102 "to enable more built-in suffixes");
4103 return error_mark_node;
4104 }
4105
4106 /* Parse a user-defined string constant. Returns a call to a user-defined
4107 literal operator taking a character pointer and the length of the string
4108 as arguments. */
4109
4110 static tree
4111 cp_parser_userdef_string_literal (tree literal)
4112 {
4113 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
4114 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
4115 tree value = USERDEF_LITERAL_VALUE (literal);
4116 int len = TREE_STRING_LENGTH (value)
4117 / TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (value)))) - 1;
4118 tree decl, result;
4119 vec<tree, va_gc> *args;
4120
4121 /* Build up a call to the user-defined operator. */
4122 /* Lookup the name we got back from the id-expression. */
4123 args = make_tree_vector ();
4124 vec_safe_push (args, value);
4125 vec_safe_push (args, build_int_cst (size_type_node, len));
4126 decl = lookup_literal_operator (name, args);
4127
4128 if (decl && decl != error_mark_node)
4129 {
4130 result = finish_call_expr (decl, &args, false, true,
4131 tf_warning_or_error);
4132 release_tree_vector (args);
4133 return result;
4134 }
4135 release_tree_vector (args);
4136
4137 /* Look for a template function with typename parameter CharT
4138 and parameter pack CharT... Call the function with
4139 template parameter characters representing the string. */
4140 args = make_tree_vector ();
4141 decl = lookup_literal_operator (name, args);
4142 if (decl && decl != error_mark_node)
4143 {
4144 tree tmpl_args = make_string_pack (value);
4145 decl = lookup_template_function (decl, tmpl_args);
4146 result = finish_call_expr (decl, &args, false, true,
4147 tf_warning_or_error);
4148 release_tree_vector (args);
4149 return result;
4150 }
4151 release_tree_vector (args);
4152
4153 error ("unable to find string literal operator %qD with %qT, %qT arguments",
4154 name, TREE_TYPE (value), size_type_node);
4155 return error_mark_node;
4156 }
4157
4158
4159 /* Basic concepts [gram.basic] */
4160
4161 /* Parse a translation-unit.
4162
4163 translation-unit:
4164 declaration-seq [opt]
4165
4166 Returns TRUE if all went well. */
4167
4168 static bool
4169 cp_parser_translation_unit (cp_parser* parser)
4170 {
4171 /* The address of the first non-permanent object on the declarator
4172 obstack. */
4173 static void *declarator_obstack_base;
4174
4175 bool success;
4176
4177 /* Create the declarator obstack, if necessary. */
4178 if (!cp_error_declarator)
4179 {
4180 gcc_obstack_init (&declarator_obstack);
4181 /* Create the error declarator. */
4182 cp_error_declarator = make_declarator (cdk_error);
4183 /* Create the empty parameter list. */
4184 no_parameters = make_parameter_declarator (NULL, NULL, NULL_TREE);
4185 /* Remember where the base of the declarator obstack lies. */
4186 declarator_obstack_base = obstack_next_free (&declarator_obstack);
4187 }
4188
4189 cp_parser_declaration_seq_opt (parser);
4190
4191 /* If there are no tokens left then all went well. */
4192 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
4193 {
4194 /* Get rid of the token array; we don't need it any more. */
4195 cp_lexer_destroy (parser->lexer);
4196 parser->lexer = NULL;
4197
4198 /* This file might have been a context that's implicitly extern
4199 "C". If so, pop the lang context. (Only relevant for PCH.) */
4200 if (parser->implicit_extern_c)
4201 {
4202 pop_lang_context ();
4203 parser->implicit_extern_c = false;
4204 }
4205
4206 /* Finish up. */
4207 finish_translation_unit ();
4208
4209 success = true;
4210 }
4211 else
4212 {
4213 cp_parser_error (parser, "expected declaration");
4214 success = false;
4215 }
4216
4217 /* Make sure the declarator obstack was fully cleaned up. */
4218 gcc_assert (obstack_next_free (&declarator_obstack)
4219 == declarator_obstack_base);
4220
4221 /* All went well. */
4222 return success;
4223 }
4224
4225 /* Return the appropriate tsubst flags for parsing, possibly in N3276
4226 decltype context. */
4227
4228 static inline tsubst_flags_t
4229 complain_flags (bool decltype_p)
4230 {
4231 tsubst_flags_t complain = tf_warning_or_error;
4232 if (decltype_p)
4233 complain |= tf_decltype;
4234 return complain;
4235 }
4236
4237 /* We're about to parse a collection of statements. If we're currently
4238 parsing tentatively, set up a firewall so that any nested
4239 cp_parser_commit_to_tentative_parse won't affect the current context. */
4240
4241 static cp_token_position
4242 cp_parser_start_tentative_firewall (cp_parser *parser)
4243 {
4244 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
4245 return 0;
4246
4247 cp_parser_parse_tentatively (parser);
4248 cp_parser_commit_to_topmost_tentative_parse (parser);
4249 return cp_lexer_token_position (parser->lexer, false);
4250 }
4251
4252 /* We've finished parsing the collection of statements. Wrap up the
4253 firewall and replace the relevant tokens with the parsed form. */
4254
4255 static void
4256 cp_parser_end_tentative_firewall (cp_parser *parser, cp_token_position start,
4257 tree expr)
4258 {
4259 if (!start)
4260 return;
4261
4262 /* Finish the firewall level. */
4263 cp_parser_parse_definitely (parser);
4264 /* And remember the result of the parse for when we try again. */
4265 cp_token *token = cp_lexer_token_at (parser->lexer, start);
4266 token->type = CPP_PREPARSED_EXPR;
4267 token->u.value = expr;
4268 token->keyword = RID_MAX;
4269 cp_lexer_purge_tokens_after (parser->lexer, start);
4270 }
4271
4272 /* Parse a GNU statement-expression, i.e. ({ stmts }), except for the
4273 enclosing parentheses. */
4274
4275 static tree
4276 cp_parser_statement_expr (cp_parser *parser)
4277 {
4278 cp_token_position start = cp_parser_start_tentative_firewall (parser);
4279
4280 /* Consume the '('. */
4281 cp_lexer_consume_token (parser->lexer);
4282 /* Start the statement-expression. */
4283 tree expr = begin_stmt_expr ();
4284 /* Parse the compound-statement. */
4285 cp_parser_compound_statement (parser, expr, BCS_NORMAL, false);
4286 /* Finish up. */
4287 expr = finish_stmt_expr (expr, false);
4288 /* Consume the ')'. */
4289 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
4290 cp_parser_skip_to_end_of_statement (parser);
4291
4292 cp_parser_end_tentative_firewall (parser, start, expr);
4293 return expr;
4294 }
4295
4296 /* Expressions [gram.expr] */
4297
4298 /* Parse a fold-operator.
4299
4300 fold-operator:
4301 - * / % ^ & | = < > << >>
4302 = -= *= /= %= ^= &= |= <<= >>=
4303 == != <= >= && || , .* ->*
4304
4305 This returns the tree code corresponding to the matched operator
4306 as an int. When the current token matches a compound assignment
4307 opertor, the resulting tree code is the negative value of the
4308 non-assignment operator. */
4309
4310 static int
4311 cp_parser_fold_operator (cp_token *token)
4312 {
4313 switch (token->type)
4314 {
4315 case CPP_PLUS: return PLUS_EXPR;
4316 case CPP_MINUS: return MINUS_EXPR;
4317 case CPP_MULT: return MULT_EXPR;
4318 case CPP_DIV: return TRUNC_DIV_EXPR;
4319 case CPP_MOD: return TRUNC_MOD_EXPR;
4320 case CPP_XOR: return BIT_XOR_EXPR;
4321 case CPP_AND: return BIT_AND_EXPR;
4322 case CPP_OR: return BIT_IOR_EXPR;
4323 case CPP_LSHIFT: return LSHIFT_EXPR;
4324 case CPP_RSHIFT: return RSHIFT_EXPR;
4325
4326 case CPP_EQ: return -NOP_EXPR;
4327 case CPP_PLUS_EQ: return -PLUS_EXPR;
4328 case CPP_MINUS_EQ: return -MINUS_EXPR;
4329 case CPP_MULT_EQ: return -MULT_EXPR;
4330 case CPP_DIV_EQ: return -TRUNC_DIV_EXPR;
4331 case CPP_MOD_EQ: return -TRUNC_MOD_EXPR;
4332 case CPP_XOR_EQ: return -BIT_XOR_EXPR;
4333 case CPP_AND_EQ: return -BIT_AND_EXPR;
4334 case CPP_OR_EQ: return -BIT_IOR_EXPR;
4335 case CPP_LSHIFT_EQ: return -LSHIFT_EXPR;
4336 case CPP_RSHIFT_EQ: return -RSHIFT_EXPR;
4337
4338 case CPP_EQ_EQ: return EQ_EXPR;
4339 case CPP_NOT_EQ: return NE_EXPR;
4340 case CPP_LESS: return LT_EXPR;
4341 case CPP_GREATER: return GT_EXPR;
4342 case CPP_LESS_EQ: return LE_EXPR;
4343 case CPP_GREATER_EQ: return GE_EXPR;
4344
4345 case CPP_AND_AND: return TRUTH_ANDIF_EXPR;
4346 case CPP_OR_OR: return TRUTH_ORIF_EXPR;
4347
4348 case CPP_COMMA: return COMPOUND_EXPR;
4349
4350 case CPP_DOT_STAR: return DOTSTAR_EXPR;
4351 case CPP_DEREF_STAR: return MEMBER_REF;
4352
4353 default: return ERROR_MARK;
4354 }
4355 }
4356
4357 /* Returns true if CODE indicates a binary expression, which is not allowed in
4358 the LHS of a fold-expression. More codes will need to be added to use this
4359 function in other contexts. */
4360
4361 static bool
4362 is_binary_op (tree_code code)
4363 {
4364 switch (code)
4365 {
4366 case PLUS_EXPR:
4367 case POINTER_PLUS_EXPR:
4368 case MINUS_EXPR:
4369 case MULT_EXPR:
4370 case TRUNC_DIV_EXPR:
4371 case TRUNC_MOD_EXPR:
4372 case BIT_XOR_EXPR:
4373 case BIT_AND_EXPR:
4374 case BIT_IOR_EXPR:
4375 case LSHIFT_EXPR:
4376 case RSHIFT_EXPR:
4377
4378 case MODOP_EXPR:
4379
4380 case EQ_EXPR:
4381 case NE_EXPR:
4382 case LE_EXPR:
4383 case GE_EXPR:
4384 case LT_EXPR:
4385 case GT_EXPR:
4386
4387 case TRUTH_ANDIF_EXPR:
4388 case TRUTH_ORIF_EXPR:
4389
4390 case COMPOUND_EXPR:
4391
4392 case DOTSTAR_EXPR:
4393 case MEMBER_REF:
4394 return true;
4395
4396 default:
4397 return false;
4398 }
4399 }
4400
4401 /* If the next token is a suitable fold operator, consume it and return as
4402 the function above. */
4403
4404 static int
4405 cp_parser_fold_operator (cp_parser *parser)
4406 {
4407 cp_token* token = cp_lexer_peek_token (parser->lexer);
4408 int code = cp_parser_fold_operator (token);
4409 if (code != ERROR_MARK)
4410 cp_lexer_consume_token (parser->lexer);
4411 return code;
4412 }
4413
4414 /* Parse a fold-expression.
4415
4416 fold-expression:
4417 ( ... folding-operator cast-expression)
4418 ( cast-expression folding-operator ... )
4419 ( cast-expression folding operator ... folding-operator cast-expression)
4420
4421 Note that the '(' and ')' are matched in primary expression. */
4422
4423 static tree
4424 cp_parser_fold_expression (cp_parser *parser, tree expr1)
4425 {
4426 cp_id_kind pidk;
4427
4428 // Left fold.
4429 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
4430 {
4431 cp_lexer_consume_token (parser->lexer);
4432 int op = cp_parser_fold_operator (parser);
4433 if (op == ERROR_MARK)
4434 {
4435 cp_parser_error (parser, "expected binary operator");
4436 return error_mark_node;
4437 }
4438
4439 tree expr = cp_parser_cast_expression (parser, false, false,
4440 false, &pidk);
4441 if (expr == error_mark_node)
4442 return error_mark_node;
4443 return finish_left_unary_fold_expr (expr, op);
4444 }
4445
4446 const cp_token* token = cp_lexer_peek_token (parser->lexer);
4447 int op = cp_parser_fold_operator (parser);
4448 if (op == ERROR_MARK)
4449 {
4450 cp_parser_error (parser, "expected binary operator");
4451 return error_mark_node;
4452 }
4453
4454 if (cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS))
4455 {
4456 cp_parser_error (parser, "expected ...");
4457 return error_mark_node;
4458 }
4459 cp_lexer_consume_token (parser->lexer);
4460
4461 /* The operands of a fold-expression are cast-expressions, so binary or
4462 conditional expressions are not allowed. We check this here to avoid
4463 tentative parsing. */
4464 if (is_binary_op (TREE_CODE (expr1)))
4465 error_at (location_of (expr1),
4466 "binary expression in operand of fold-expression");
4467 else if (TREE_CODE (expr1) == COND_EXPR)
4468 error_at (location_of (expr1),
4469 "conditional expression in operand of fold-expression");
4470
4471 // Right fold.
4472 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
4473 return finish_right_unary_fold_expr (expr1, op);
4474
4475 if (cp_lexer_next_token_is_not (parser->lexer, token->type))
4476 {
4477 cp_parser_error (parser, "mismatched operator in fold-expression");
4478 return error_mark_node;
4479 }
4480 cp_lexer_consume_token (parser->lexer);
4481
4482 // Binary left or right fold.
4483 tree expr2 = cp_parser_cast_expression (parser, false, false, false, &pidk);
4484 if (expr2 == error_mark_node)
4485 return error_mark_node;
4486 return finish_binary_fold_expr (expr1, expr2, op);
4487 }
4488
4489 /* Parse a primary-expression.
4490
4491 primary-expression:
4492 literal
4493 this
4494 ( expression )
4495 id-expression
4496 lambda-expression (C++11)
4497
4498 GNU Extensions:
4499
4500 primary-expression:
4501 ( compound-statement )
4502 __builtin_va_arg ( assignment-expression , type-id )
4503 __builtin_offsetof ( type-id , offsetof-expression )
4504
4505 C++ Extensions:
4506 __has_nothrow_assign ( type-id )
4507 __has_nothrow_constructor ( type-id )
4508 __has_nothrow_copy ( type-id )
4509 __has_trivial_assign ( type-id )
4510 __has_trivial_constructor ( type-id )
4511 __has_trivial_copy ( type-id )
4512 __has_trivial_destructor ( type-id )
4513 __has_virtual_destructor ( type-id )
4514 __is_abstract ( type-id )
4515 __is_base_of ( type-id , type-id )
4516 __is_class ( type-id )
4517 __is_empty ( type-id )
4518 __is_enum ( type-id )
4519 __is_final ( type-id )
4520 __is_literal_type ( type-id )
4521 __is_pod ( type-id )
4522 __is_polymorphic ( type-id )
4523 __is_std_layout ( type-id )
4524 __is_trivial ( type-id )
4525 __is_union ( type-id )
4526
4527 Objective-C++ Extension:
4528
4529 primary-expression:
4530 objc-expression
4531
4532 literal:
4533 __null
4534
4535 ADDRESS_P is true iff this expression was immediately preceded by
4536 "&" and therefore might denote a pointer-to-member. CAST_P is true
4537 iff this expression is the target of a cast. TEMPLATE_ARG_P is
4538 true iff this expression is a template argument.
4539
4540 Returns a representation of the expression. Upon return, *IDK
4541 indicates what kind of id-expression (if any) was present. */
4542
4543 static tree
4544 cp_parser_primary_expression (cp_parser *parser,
4545 bool address_p,
4546 bool cast_p,
4547 bool template_arg_p,
4548 bool decltype_p,
4549 cp_id_kind *idk)
4550 {
4551 cp_token *token = NULL;
4552
4553 /* Assume the primary expression is not an id-expression. */
4554 *idk = CP_ID_KIND_NONE;
4555
4556 /* Peek at the next token. */
4557 token = cp_lexer_peek_token (parser->lexer);
4558 switch ((int) token->type)
4559 {
4560 /* literal:
4561 integer-literal
4562 character-literal
4563 floating-literal
4564 string-literal
4565 boolean-literal
4566 pointer-literal
4567 user-defined-literal */
4568 case CPP_CHAR:
4569 case CPP_CHAR16:
4570 case CPP_CHAR32:
4571 case CPP_WCHAR:
4572 case CPP_UTF8CHAR:
4573 case CPP_NUMBER:
4574 case CPP_PREPARSED_EXPR:
4575 if (TREE_CODE (token->u.value) == USERDEF_LITERAL)
4576 return cp_parser_userdef_numeric_literal (parser);
4577 token = cp_lexer_consume_token (parser->lexer);
4578 if (TREE_CODE (token->u.value) == FIXED_CST)
4579 {
4580 error_at (token->location,
4581 "fixed-point types not supported in C++");
4582 return error_mark_node;
4583 }
4584 /* Floating-point literals are only allowed in an integral
4585 constant expression if they are cast to an integral or
4586 enumeration type. */
4587 if (TREE_CODE (token->u.value) == REAL_CST
4588 && parser->integral_constant_expression_p
4589 && pedantic)
4590 {
4591 /* CAST_P will be set even in invalid code like "int(2.7 +
4592 ...)". Therefore, we have to check that the next token
4593 is sure to end the cast. */
4594 if (cast_p)
4595 {
4596 cp_token *next_token;
4597
4598 next_token = cp_lexer_peek_token (parser->lexer);
4599 if (/* The comma at the end of an
4600 enumerator-definition. */
4601 next_token->type != CPP_COMMA
4602 /* The curly brace at the end of an enum-specifier. */
4603 && next_token->type != CPP_CLOSE_BRACE
4604 /* The end of a statement. */
4605 && next_token->type != CPP_SEMICOLON
4606 /* The end of the cast-expression. */
4607 && next_token->type != CPP_CLOSE_PAREN
4608 /* The end of an array bound. */
4609 && next_token->type != CPP_CLOSE_SQUARE
4610 /* The closing ">" in a template-argument-list. */
4611 && (next_token->type != CPP_GREATER
4612 || parser->greater_than_is_operator_p)
4613 /* C++0x only: A ">>" treated like two ">" tokens,
4614 in a template-argument-list. */
4615 && (next_token->type != CPP_RSHIFT
4616 || (cxx_dialect == cxx98)
4617 || parser->greater_than_is_operator_p))
4618 cast_p = false;
4619 }
4620
4621 /* If we are within a cast, then the constraint that the
4622 cast is to an integral or enumeration type will be
4623 checked at that point. If we are not within a cast, then
4624 this code is invalid. */
4625 if (!cast_p)
4626 cp_parser_non_integral_constant_expression (parser, NIC_FLOAT);
4627 }
4628 return token->u.value;
4629
4630 case CPP_CHAR_USERDEF:
4631 case CPP_CHAR16_USERDEF:
4632 case CPP_CHAR32_USERDEF:
4633 case CPP_WCHAR_USERDEF:
4634 case CPP_UTF8CHAR_USERDEF:
4635 return cp_parser_userdef_char_literal (parser);
4636
4637 case CPP_STRING:
4638 case CPP_STRING16:
4639 case CPP_STRING32:
4640 case CPP_WSTRING:
4641 case CPP_UTF8STRING:
4642 case CPP_STRING_USERDEF:
4643 case CPP_STRING16_USERDEF:
4644 case CPP_STRING32_USERDEF:
4645 case CPP_WSTRING_USERDEF:
4646 case CPP_UTF8STRING_USERDEF:
4647 /* ??? Should wide strings be allowed when parser->translate_strings_p
4648 is false (i.e. in attributes)? If not, we can kill the third
4649 argument to cp_parser_string_literal. */
4650 return cp_parser_string_literal (parser,
4651 parser->translate_strings_p,
4652 true);
4653
4654 case CPP_OPEN_PAREN:
4655 /* If we see `( { ' then we are looking at the beginning of
4656 a GNU statement-expression. */
4657 if (cp_parser_allow_gnu_extensions_p (parser)
4658 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_BRACE))
4659 {
4660 /* Statement-expressions are not allowed by the standard. */
4661 pedwarn (token->location, OPT_Wpedantic,
4662 "ISO C++ forbids braced-groups within expressions");
4663
4664 /* And they're not allowed outside of a function-body; you
4665 cannot, for example, write:
4666
4667 int i = ({ int j = 3; j + 1; });
4668
4669 at class or namespace scope. */
4670 if (!parser->in_function_body
4671 || parser->in_template_argument_list_p)
4672 {
4673 error_at (token->location,
4674 "statement-expressions are not allowed outside "
4675 "functions nor in template-argument lists");
4676 cp_parser_skip_to_end_of_block_or_statement (parser);
4677 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
4678 cp_lexer_consume_token (parser->lexer);
4679 return error_mark_node;
4680 }
4681 else
4682 return cp_parser_statement_expr (parser);
4683 }
4684 /* Otherwise it's a normal parenthesized expression. */
4685 {
4686 tree expr;
4687 bool saved_greater_than_is_operator_p;
4688
4689 /* Consume the `('. */
4690 cp_lexer_consume_token (parser->lexer);
4691 /* Within a parenthesized expression, a `>' token is always
4692 the greater-than operator. */
4693 saved_greater_than_is_operator_p
4694 = parser->greater_than_is_operator_p;
4695 parser->greater_than_is_operator_p = true;
4696
4697 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
4698 /* Left fold expression. */
4699 expr = NULL_TREE;
4700 else
4701 /* Parse the parenthesized expression. */
4702 expr = cp_parser_expression (parser, idk, cast_p, decltype_p);
4703
4704 token = cp_lexer_peek_token (parser->lexer);
4705 if (token->type == CPP_ELLIPSIS || cp_parser_fold_operator (token))
4706 {
4707 expr = cp_parser_fold_expression (parser, expr);
4708 if (expr != error_mark_node
4709 && cxx_dialect < cxx1z
4710 && !in_system_header_at (input_location))
4711 pedwarn (input_location, 0, "fold-expressions only available "
4712 "with -std=c++1z or -std=gnu++1z");
4713 }
4714 else
4715 /* Let the front end know that this expression was
4716 enclosed in parentheses. This matters in case, for
4717 example, the expression is of the form `A::B', since
4718 `&A::B' might be a pointer-to-member, but `&(A::B)' is
4719 not. */
4720 expr = finish_parenthesized_expr (expr);
4721
4722 /* DR 705: Wrapping an unqualified name in parentheses
4723 suppresses arg-dependent lookup. We want to pass back
4724 CP_ID_KIND_QUALIFIED for suppressing vtable lookup
4725 (c++/37862), but none of the others. */
4726 if (*idk != CP_ID_KIND_QUALIFIED)
4727 *idk = CP_ID_KIND_NONE;
4728
4729 /* The `>' token might be the end of a template-id or
4730 template-parameter-list now. */
4731 parser->greater_than_is_operator_p
4732 = saved_greater_than_is_operator_p;
4733 /* Consume the `)'. */
4734 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN)
4735 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
4736 cp_parser_skip_to_end_of_statement (parser);
4737
4738 return expr;
4739 }
4740
4741 case CPP_OPEN_SQUARE:
4742 {
4743 if (c_dialect_objc ())
4744 {
4745 /* We might have an Objective-C++ message. */
4746 cp_parser_parse_tentatively (parser);
4747 tree msg = cp_parser_objc_message_expression (parser);
4748 /* If that works out, we're done ... */
4749 if (cp_parser_parse_definitely (parser))
4750 return msg;
4751 /* ... else, fall though to see if it's a lambda. */
4752 }
4753 tree lam = cp_parser_lambda_expression (parser);
4754 /* Don't warn about a failed tentative parse. */
4755 if (cp_parser_error_occurred (parser))
4756 return error_mark_node;
4757 maybe_warn_cpp0x (CPP0X_LAMBDA_EXPR);
4758 return lam;
4759 }
4760
4761 case CPP_OBJC_STRING:
4762 if (c_dialect_objc ())
4763 /* We have an Objective-C++ string literal. */
4764 return cp_parser_objc_expression (parser);
4765 cp_parser_error (parser, "expected primary-expression");
4766 return error_mark_node;
4767
4768 case CPP_KEYWORD:
4769 switch (token->keyword)
4770 {
4771 /* These two are the boolean literals. */
4772 case RID_TRUE:
4773 cp_lexer_consume_token (parser->lexer);
4774 return boolean_true_node;
4775 case RID_FALSE:
4776 cp_lexer_consume_token (parser->lexer);
4777 return boolean_false_node;
4778
4779 /* The `__null' literal. */
4780 case RID_NULL:
4781 cp_lexer_consume_token (parser->lexer);
4782 return null_node;
4783
4784 /* The `nullptr' literal. */
4785 case RID_NULLPTR:
4786 cp_lexer_consume_token (parser->lexer);
4787 return nullptr_node;
4788
4789 /* Recognize the `this' keyword. */
4790 case RID_THIS:
4791 cp_lexer_consume_token (parser->lexer);
4792 if (parser->local_variables_forbidden_p)
4793 {
4794 error_at (token->location,
4795 "%<this%> may not be used in this context");
4796 return error_mark_node;
4797 }
4798 /* Pointers cannot appear in constant-expressions. */
4799 if (cp_parser_non_integral_constant_expression (parser, NIC_THIS))
4800 return error_mark_node;
4801 return finish_this_expr ();
4802
4803 /* The `operator' keyword can be the beginning of an
4804 id-expression. */
4805 case RID_OPERATOR:
4806 goto id_expression;
4807
4808 case RID_FUNCTION_NAME:
4809 case RID_PRETTY_FUNCTION_NAME:
4810 case RID_C99_FUNCTION_NAME:
4811 {
4812 non_integral_constant name;
4813
4814 /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
4815 __func__ are the names of variables -- but they are
4816 treated specially. Therefore, they are handled here,
4817 rather than relying on the generic id-expression logic
4818 below. Grammatically, these names are id-expressions.
4819
4820 Consume the token. */
4821 token = cp_lexer_consume_token (parser->lexer);
4822
4823 switch (token->keyword)
4824 {
4825 case RID_FUNCTION_NAME:
4826 name = NIC_FUNC_NAME;
4827 break;
4828 case RID_PRETTY_FUNCTION_NAME:
4829 name = NIC_PRETTY_FUNC;
4830 break;
4831 case RID_C99_FUNCTION_NAME:
4832 name = NIC_C99_FUNC;
4833 break;
4834 default:
4835 gcc_unreachable ();
4836 }
4837
4838 if (cp_parser_non_integral_constant_expression (parser, name))
4839 return error_mark_node;
4840
4841 /* Look up the name. */
4842 return finish_fname (token->u.value);
4843 }
4844
4845 case RID_VA_ARG:
4846 {
4847 tree expression;
4848 tree type;
4849 source_location type_location;
4850
4851 /* The `__builtin_va_arg' construct is used to handle
4852 `va_arg'. Consume the `__builtin_va_arg' token. */
4853 cp_lexer_consume_token (parser->lexer);
4854 /* Look for the opening `('. */
4855 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
4856 /* Now, parse the assignment-expression. */
4857 expression = cp_parser_assignment_expression (parser);
4858 /* Look for the `,'. */
4859 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
4860 type_location = cp_lexer_peek_token (parser->lexer)->location;
4861 /* Parse the type-id. */
4862 type = cp_parser_type_id (parser);
4863 /* Look for the closing `)'. */
4864 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
4865 /* Using `va_arg' in a constant-expression is not
4866 allowed. */
4867 if (cp_parser_non_integral_constant_expression (parser,
4868 NIC_VA_ARG))
4869 return error_mark_node;
4870 return build_x_va_arg (type_location, expression, type);
4871 }
4872
4873 case RID_OFFSETOF:
4874 return cp_parser_builtin_offsetof (parser);
4875
4876 case RID_HAS_NOTHROW_ASSIGN:
4877 case RID_HAS_NOTHROW_CONSTRUCTOR:
4878 case RID_HAS_NOTHROW_COPY:
4879 case RID_HAS_TRIVIAL_ASSIGN:
4880 case RID_HAS_TRIVIAL_CONSTRUCTOR:
4881 case RID_HAS_TRIVIAL_COPY:
4882 case RID_HAS_TRIVIAL_DESTRUCTOR:
4883 case RID_HAS_VIRTUAL_DESTRUCTOR:
4884 case RID_IS_ABSTRACT:
4885 case RID_IS_BASE_OF:
4886 case RID_IS_CLASS:
4887 case RID_IS_EMPTY:
4888 case RID_IS_ENUM:
4889 case RID_IS_FINAL:
4890 case RID_IS_LITERAL_TYPE:
4891 case RID_IS_POD:
4892 case RID_IS_POLYMORPHIC:
4893 case RID_IS_SAME_AS:
4894 case RID_IS_STD_LAYOUT:
4895 case RID_IS_TRIVIAL:
4896 case RID_IS_TRIVIALLY_ASSIGNABLE:
4897 case RID_IS_TRIVIALLY_CONSTRUCTIBLE:
4898 case RID_IS_TRIVIALLY_COPYABLE:
4899 case RID_IS_UNION:
4900 return cp_parser_trait_expr (parser, token->keyword);
4901
4902 // C++ concepts
4903 case RID_REQUIRES:
4904 return cp_parser_requires_expression (parser);
4905
4906 /* Objective-C++ expressions. */
4907 case RID_AT_ENCODE:
4908 case RID_AT_PROTOCOL:
4909 case RID_AT_SELECTOR:
4910 return cp_parser_objc_expression (parser);
4911
4912 case RID_TEMPLATE:
4913 if (parser->in_function_body
4914 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
4915 == CPP_LESS))
4916 {
4917 error_at (token->location,
4918 "a template declaration cannot appear at block scope");
4919 cp_parser_skip_to_end_of_block_or_statement (parser);
4920 return error_mark_node;
4921 }
4922 default:
4923 cp_parser_error (parser, "expected primary-expression");
4924 return error_mark_node;
4925 }
4926
4927 /* An id-expression can start with either an identifier, a
4928 `::' as the beginning of a qualified-id, or the "operator"
4929 keyword. */
4930 case CPP_NAME:
4931 case CPP_SCOPE:
4932 case CPP_TEMPLATE_ID:
4933 case CPP_NESTED_NAME_SPECIFIER:
4934 {
4935 tree id_expression;
4936 tree decl;
4937 const char *error_msg;
4938 bool template_p;
4939 bool done;
4940 cp_token *id_expr_token;
4941
4942 id_expression:
4943 /* Parse the id-expression. */
4944 id_expression
4945 = cp_parser_id_expression (parser,
4946 /*template_keyword_p=*/false,
4947 /*check_dependency_p=*/true,
4948 &template_p,
4949 /*declarator_p=*/false,
4950 /*optional_p=*/false);
4951 if (id_expression == error_mark_node)
4952 return error_mark_node;
4953 id_expr_token = token;
4954 token = cp_lexer_peek_token (parser->lexer);
4955 done = (token->type != CPP_OPEN_SQUARE
4956 && token->type != CPP_OPEN_PAREN
4957 && token->type != CPP_DOT
4958 && token->type != CPP_DEREF
4959 && token->type != CPP_PLUS_PLUS
4960 && token->type != CPP_MINUS_MINUS);
4961 /* If we have a template-id, then no further lookup is
4962 required. If the template-id was for a template-class, we
4963 will sometimes have a TYPE_DECL at this point. */
4964 if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
4965 || TREE_CODE (id_expression) == TYPE_DECL)
4966 decl = id_expression;
4967 /* Look up the name. */
4968 else
4969 {
4970 tree ambiguous_decls;
4971
4972 /* If we already know that this lookup is ambiguous, then
4973 we've already issued an error message; there's no reason
4974 to check again. */
4975 if (id_expr_token->type == CPP_NAME
4976 && id_expr_token->error_reported)
4977 {
4978 cp_parser_simulate_error (parser);
4979 return error_mark_node;
4980 }
4981
4982 decl = cp_parser_lookup_name (parser, id_expression,
4983 none_type,
4984 template_p,
4985 /*is_namespace=*/false,
4986 /*check_dependency=*/true,
4987 &ambiguous_decls,
4988 id_expr_token->location);
4989 /* If the lookup was ambiguous, an error will already have
4990 been issued. */
4991 if (ambiguous_decls)
4992 return error_mark_node;
4993
4994 /* In Objective-C++, we may have an Objective-C 2.0
4995 dot-syntax for classes here. */
4996 if (c_dialect_objc ()
4997 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT
4998 && TREE_CODE (decl) == TYPE_DECL
4999 && objc_is_class_name (decl))
5000 {
5001 tree component;
5002 cp_lexer_consume_token (parser->lexer);
5003 component = cp_parser_identifier (parser);
5004 if (component == error_mark_node)
5005 return error_mark_node;
5006
5007 return objc_build_class_component_ref (id_expression, component);
5008 }
5009
5010 /* In Objective-C++, an instance variable (ivar) may be preferred
5011 to whatever cp_parser_lookup_name() found. */
5012 decl = objc_lookup_ivar (decl, id_expression);
5013
5014 /* If name lookup gives us a SCOPE_REF, then the
5015 qualifying scope was dependent. */
5016 if (TREE_CODE (decl) == SCOPE_REF)
5017 {
5018 /* At this point, we do not know if DECL is a valid
5019 integral constant expression. We assume that it is
5020 in fact such an expression, so that code like:
5021
5022 template <int N> struct A {
5023 int a[B<N>::i];
5024 };
5025
5026 is accepted. At template-instantiation time, we
5027 will check that B<N>::i is actually a constant. */
5028 return decl;
5029 }
5030 /* Check to see if DECL is a local variable in a context
5031 where that is forbidden. */
5032 if (parser->local_variables_forbidden_p
5033 && local_variable_p (decl))
5034 {
5035 /* It might be that we only found DECL because we are
5036 trying to be generous with pre-ISO scoping rules.
5037 For example, consider:
5038
5039 int i;
5040 void g() {
5041 for (int i = 0; i < 10; ++i) {}
5042 extern void f(int j = i);
5043 }
5044
5045 Here, name look up will originally find the out
5046 of scope `i'. We need to issue a warning message,
5047 but then use the global `i'. */
5048 decl = check_for_out_of_scope_variable (decl);
5049 if (local_variable_p (decl))
5050 {
5051 error_at (id_expr_token->location,
5052 "local variable %qD may not appear in this context",
5053 decl);
5054 return error_mark_node;
5055 }
5056 }
5057 }
5058
5059 decl = (finish_id_expression
5060 (id_expression, decl, parser->scope,
5061 idk,
5062 parser->integral_constant_expression_p,
5063 parser->allow_non_integral_constant_expression_p,
5064 &parser->non_integral_constant_expression_p,
5065 template_p, done, address_p,
5066 template_arg_p,
5067 &error_msg,
5068 id_expr_token->location));
5069 if (error_msg)
5070 cp_parser_error (parser, error_msg);
5071 return decl;
5072 }
5073
5074 /* Anything else is an error. */
5075 default:
5076 cp_parser_error (parser, "expected primary-expression");
5077 return error_mark_node;
5078 }
5079 }
5080
5081 static inline tree
5082 cp_parser_primary_expression (cp_parser *parser,
5083 bool address_p,
5084 bool cast_p,
5085 bool template_arg_p,
5086 cp_id_kind *idk)
5087 {
5088 return cp_parser_primary_expression (parser, address_p, cast_p, template_arg_p,
5089 /*decltype*/false, idk);
5090 }
5091
5092 /* Parse an id-expression.
5093
5094 id-expression:
5095 unqualified-id
5096 qualified-id
5097
5098 qualified-id:
5099 :: [opt] nested-name-specifier template [opt] unqualified-id
5100 :: identifier
5101 :: operator-function-id
5102 :: template-id
5103
5104 Return a representation of the unqualified portion of the
5105 identifier. Sets PARSER->SCOPE to the qualifying scope if there is
5106 a `::' or nested-name-specifier.
5107
5108 Often, if the id-expression was a qualified-id, the caller will
5109 want to make a SCOPE_REF to represent the qualified-id. This
5110 function does not do this in order to avoid wastefully creating
5111 SCOPE_REFs when they are not required.
5112
5113 If TEMPLATE_KEYWORD_P is true, then we have just seen the
5114 `template' keyword.
5115
5116 If CHECK_DEPENDENCY_P is false, then names are looked up inside
5117 uninstantiated templates.
5118
5119 If *TEMPLATE_P is non-NULL, it is set to true iff the
5120 `template' keyword is used to explicitly indicate that the entity
5121 named is a template.
5122
5123 If DECLARATOR_P is true, the id-expression is appearing as part of
5124 a declarator, rather than as part of an expression. */
5125
5126 static tree
5127 cp_parser_id_expression (cp_parser *parser,
5128 bool template_keyword_p,
5129 bool check_dependency_p,
5130 bool *template_p,
5131 bool declarator_p,
5132 bool optional_p)
5133 {
5134 bool global_scope_p;
5135 bool nested_name_specifier_p;
5136
5137 /* Assume the `template' keyword was not used. */
5138 if (template_p)
5139 *template_p = template_keyword_p;
5140
5141 /* Look for the optional `::' operator. */
5142 global_scope_p
5143 = (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false)
5144 != NULL_TREE);
5145 /* Look for the optional nested-name-specifier. */
5146 nested_name_specifier_p
5147 = (cp_parser_nested_name_specifier_opt (parser,
5148 /*typename_keyword_p=*/false,
5149 check_dependency_p,
5150 /*type_p=*/false,
5151 declarator_p)
5152 != NULL_TREE);
5153 /* If there is a nested-name-specifier, then we are looking at
5154 the first qualified-id production. */
5155 if (nested_name_specifier_p)
5156 {
5157 tree saved_scope;
5158 tree saved_object_scope;
5159 tree saved_qualifying_scope;
5160 tree unqualified_id;
5161 bool is_template;
5162
5163 /* See if the next token is the `template' keyword. */
5164 if (!template_p)
5165 template_p = &is_template;
5166 *template_p = cp_parser_optional_template_keyword (parser);
5167 /* Name lookup we do during the processing of the
5168 unqualified-id might obliterate SCOPE. */
5169 saved_scope = parser->scope;
5170 saved_object_scope = parser->object_scope;
5171 saved_qualifying_scope = parser->qualifying_scope;
5172 /* Process the final unqualified-id. */
5173 unqualified_id = cp_parser_unqualified_id (parser, *template_p,
5174 check_dependency_p,
5175 declarator_p,
5176 /*optional_p=*/false);
5177 /* Restore the SAVED_SCOPE for our caller. */
5178 parser->scope = saved_scope;
5179 parser->object_scope = saved_object_scope;
5180 parser->qualifying_scope = saved_qualifying_scope;
5181
5182 return unqualified_id;
5183 }
5184 /* Otherwise, if we are in global scope, then we are looking at one
5185 of the other qualified-id productions. */
5186 else if (global_scope_p)
5187 {
5188 cp_token *token;
5189 tree id;
5190
5191 /* Peek at the next token. */
5192 token = cp_lexer_peek_token (parser->lexer);
5193
5194 /* If it's an identifier, and the next token is not a "<", then
5195 we can avoid the template-id case. This is an optimization
5196 for this common case. */
5197 if (token->type == CPP_NAME
5198 && !cp_parser_nth_token_starts_template_argument_list_p
5199 (parser, 2))
5200 return cp_parser_identifier (parser);
5201
5202 cp_parser_parse_tentatively (parser);
5203 /* Try a template-id. */
5204 id = cp_parser_template_id (parser,
5205 /*template_keyword_p=*/false,
5206 /*check_dependency_p=*/true,
5207 none_type,
5208 declarator_p);
5209 /* If that worked, we're done. */
5210 if (cp_parser_parse_definitely (parser))
5211 return id;
5212
5213 /* Peek at the next token. (Changes in the token buffer may
5214 have invalidated the pointer obtained above.) */
5215 token = cp_lexer_peek_token (parser->lexer);
5216
5217 switch (token->type)
5218 {
5219 case CPP_NAME:
5220 return cp_parser_identifier (parser);
5221
5222 case CPP_KEYWORD:
5223 if (token->keyword == RID_OPERATOR)
5224 return cp_parser_operator_function_id (parser);
5225 /* Fall through. */
5226
5227 default:
5228 cp_parser_error (parser, "expected id-expression");
5229 return error_mark_node;
5230 }
5231 }
5232 else
5233 return cp_parser_unqualified_id (parser, template_keyword_p,
5234 /*check_dependency_p=*/true,
5235 declarator_p,
5236 optional_p);
5237 }
5238
5239 /* Parse an unqualified-id.
5240
5241 unqualified-id:
5242 identifier
5243 operator-function-id
5244 conversion-function-id
5245 ~ class-name
5246 template-id
5247
5248 If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
5249 keyword, in a construct like `A::template ...'.
5250
5251 Returns a representation of unqualified-id. For the `identifier'
5252 production, an IDENTIFIER_NODE is returned. For the `~ class-name'
5253 production a BIT_NOT_EXPR is returned; the operand of the
5254 BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name. For the
5255 other productions, see the documentation accompanying the
5256 corresponding parsing functions. If CHECK_DEPENDENCY_P is false,
5257 names are looked up in uninstantiated templates. If DECLARATOR_P
5258 is true, the unqualified-id is appearing as part of a declarator,
5259 rather than as part of an expression. */
5260
5261 static tree
5262 cp_parser_unqualified_id (cp_parser* parser,
5263 bool template_keyword_p,
5264 bool check_dependency_p,
5265 bool declarator_p,
5266 bool optional_p)
5267 {
5268 cp_token *token;
5269
5270 /* Peek at the next token. */
5271 token = cp_lexer_peek_token (parser->lexer);
5272
5273 switch ((int) token->type)
5274 {
5275 case CPP_NAME:
5276 {
5277 tree id;
5278
5279 /* We don't know yet whether or not this will be a
5280 template-id. */
5281 cp_parser_parse_tentatively (parser);
5282 /* Try a template-id. */
5283 id = cp_parser_template_id (parser, template_keyword_p,
5284 check_dependency_p,
5285 none_type,
5286 declarator_p);
5287 /* If it worked, we're done. */
5288 if (cp_parser_parse_definitely (parser))
5289 return id;
5290 /* Otherwise, it's an ordinary identifier. */
5291 return cp_parser_identifier (parser);
5292 }
5293
5294 case CPP_TEMPLATE_ID:
5295 return cp_parser_template_id (parser, template_keyword_p,
5296 check_dependency_p,
5297 none_type,
5298 declarator_p);
5299
5300 case CPP_COMPL:
5301 {
5302 tree type_decl;
5303 tree qualifying_scope;
5304 tree object_scope;
5305 tree scope;
5306 bool done;
5307
5308 /* Consume the `~' token. */
5309 cp_lexer_consume_token (parser->lexer);
5310 /* Parse the class-name. The standard, as written, seems to
5311 say that:
5312
5313 template <typename T> struct S { ~S (); };
5314 template <typename T> S<T>::~S() {}
5315
5316 is invalid, since `~' must be followed by a class-name, but
5317 `S<T>' is dependent, and so not known to be a class.
5318 That's not right; we need to look in uninstantiated
5319 templates. A further complication arises from:
5320
5321 template <typename T> void f(T t) {
5322 t.T::~T();
5323 }
5324
5325 Here, it is not possible to look up `T' in the scope of `T'
5326 itself. We must look in both the current scope, and the
5327 scope of the containing complete expression.
5328
5329 Yet another issue is:
5330
5331 struct S {
5332 int S;
5333 ~S();
5334 };
5335
5336 S::~S() {}
5337
5338 The standard does not seem to say that the `S' in `~S'
5339 should refer to the type `S' and not the data member
5340 `S::S'. */
5341
5342 /* DR 244 says that we look up the name after the "~" in the
5343 same scope as we looked up the qualifying name. That idea
5344 isn't fully worked out; it's more complicated than that. */
5345 scope = parser->scope;
5346 object_scope = parser->object_scope;
5347 qualifying_scope = parser->qualifying_scope;
5348
5349 /* Check for invalid scopes. */
5350 if (scope == error_mark_node)
5351 {
5352 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
5353 cp_lexer_consume_token (parser->lexer);
5354 return error_mark_node;
5355 }
5356 if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
5357 {
5358 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
5359 error_at (token->location,
5360 "scope %qT before %<~%> is not a class-name",
5361 scope);
5362 cp_parser_simulate_error (parser);
5363 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
5364 cp_lexer_consume_token (parser->lexer);
5365 return error_mark_node;
5366 }
5367 gcc_assert (!scope || TYPE_P (scope));
5368
5369 /* If the name is of the form "X::~X" it's OK even if X is a
5370 typedef. */
5371 token = cp_lexer_peek_token (parser->lexer);
5372 if (scope
5373 && token->type == CPP_NAME
5374 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
5375 != CPP_LESS)
5376 && (token->u.value == TYPE_IDENTIFIER (scope)
5377 || (CLASS_TYPE_P (scope)
5378 && constructor_name_p (token->u.value, scope))))
5379 {
5380 cp_lexer_consume_token (parser->lexer);
5381 return build_nt (BIT_NOT_EXPR, scope);
5382 }
5383
5384 /* ~auto means the destructor of whatever the object is. */
5385 if (cp_parser_is_keyword (token, RID_AUTO))
5386 {
5387 if (cxx_dialect < cxx14)
5388 pedwarn (input_location, 0,
5389 "%<~auto%> only available with "
5390 "-std=c++14 or -std=gnu++14");
5391 cp_lexer_consume_token (parser->lexer);
5392 return build_nt (BIT_NOT_EXPR, make_auto ());
5393 }
5394
5395 /* If there was an explicit qualification (S::~T), first look
5396 in the scope given by the qualification (i.e., S).
5397
5398 Note: in the calls to cp_parser_class_name below we pass
5399 typename_type so that lookup finds the injected-class-name
5400 rather than the constructor. */
5401 done = false;
5402 type_decl = NULL_TREE;
5403 if (scope)
5404 {
5405 cp_parser_parse_tentatively (parser);
5406 type_decl = cp_parser_class_name (parser,
5407 /*typename_keyword_p=*/false,
5408 /*template_keyword_p=*/false,
5409 typename_type,
5410 /*check_dependency=*/false,
5411 /*class_head_p=*/false,
5412 declarator_p);
5413 if (cp_parser_parse_definitely (parser))
5414 done = true;
5415 }
5416 /* In "N::S::~S", look in "N" as well. */
5417 if (!done && scope && qualifying_scope)
5418 {
5419 cp_parser_parse_tentatively (parser);
5420 parser->scope = qualifying_scope;
5421 parser->object_scope = NULL_TREE;
5422 parser->qualifying_scope = NULL_TREE;
5423 type_decl
5424 = cp_parser_class_name (parser,
5425 /*typename_keyword_p=*/false,
5426 /*template_keyword_p=*/false,
5427 typename_type,
5428 /*check_dependency=*/false,
5429 /*class_head_p=*/false,
5430 declarator_p);
5431 if (cp_parser_parse_definitely (parser))
5432 done = true;
5433 }
5434 /* In "p->S::~T", look in the scope given by "*p" as well. */
5435 else if (!done && object_scope)
5436 {
5437 cp_parser_parse_tentatively (parser);
5438 parser->scope = object_scope;
5439 parser->object_scope = NULL_TREE;
5440 parser->qualifying_scope = NULL_TREE;
5441 type_decl
5442 = cp_parser_class_name (parser,
5443 /*typename_keyword_p=*/false,
5444 /*template_keyword_p=*/false,
5445 typename_type,
5446 /*check_dependency=*/false,
5447 /*class_head_p=*/false,
5448 declarator_p);
5449 if (cp_parser_parse_definitely (parser))
5450 done = true;
5451 }
5452 /* Look in the surrounding context. */
5453 if (!done)
5454 {
5455 parser->scope = NULL_TREE;
5456 parser->object_scope = NULL_TREE;
5457 parser->qualifying_scope = NULL_TREE;
5458 if (processing_template_decl)
5459 cp_parser_parse_tentatively (parser);
5460 type_decl
5461 = cp_parser_class_name (parser,
5462 /*typename_keyword_p=*/false,
5463 /*template_keyword_p=*/false,
5464 typename_type,
5465 /*check_dependency=*/false,
5466 /*class_head_p=*/false,
5467 declarator_p);
5468 if (processing_template_decl
5469 && ! cp_parser_parse_definitely (parser))
5470 {
5471 /* We couldn't find a type with this name. If we're parsing
5472 tentatively, fail and try something else. */
5473 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
5474 {
5475 cp_parser_simulate_error (parser);
5476 return error_mark_node;
5477 }
5478 /* Otherwise, accept it and check for a match at instantiation
5479 time. */
5480 type_decl = cp_parser_identifier (parser);
5481 if (type_decl != error_mark_node)
5482 type_decl = build_nt (BIT_NOT_EXPR, type_decl);
5483 return type_decl;
5484 }
5485 }
5486 /* If an error occurred, assume that the name of the
5487 destructor is the same as the name of the qualifying
5488 class. That allows us to keep parsing after running
5489 into ill-formed destructor names. */
5490 if (type_decl == error_mark_node && scope)
5491 return build_nt (BIT_NOT_EXPR, scope);
5492 else if (type_decl == error_mark_node)
5493 return error_mark_node;
5494
5495 /* Check that destructor name and scope match. */
5496 if (declarator_p && scope && !check_dtor_name (scope, type_decl))
5497 {
5498 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
5499 error_at (token->location,
5500 "declaration of %<~%T%> as member of %qT",
5501 type_decl, scope);
5502 cp_parser_simulate_error (parser);
5503 return error_mark_node;
5504 }
5505
5506 /* [class.dtor]
5507
5508 A typedef-name that names a class shall not be used as the
5509 identifier in the declarator for a destructor declaration. */
5510 if (declarator_p
5511 && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
5512 && !DECL_SELF_REFERENCE_P (type_decl)
5513 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
5514 error_at (token->location,
5515 "typedef-name %qD used as destructor declarator",
5516 type_decl);
5517
5518 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
5519 }
5520
5521 case CPP_KEYWORD:
5522 if (token->keyword == RID_OPERATOR)
5523 {
5524 tree id;
5525
5526 /* This could be a template-id, so we try that first. */
5527 cp_parser_parse_tentatively (parser);
5528 /* Try a template-id. */
5529 id = cp_parser_template_id (parser, template_keyword_p,
5530 /*check_dependency_p=*/true,
5531 none_type,
5532 declarator_p);
5533 /* If that worked, we're done. */
5534 if (cp_parser_parse_definitely (parser))
5535 return id;
5536 /* We still don't know whether we're looking at an
5537 operator-function-id or a conversion-function-id. */
5538 cp_parser_parse_tentatively (parser);
5539 /* Try an operator-function-id. */
5540 id = cp_parser_operator_function_id (parser);
5541 /* If that didn't work, try a conversion-function-id. */
5542 if (!cp_parser_parse_definitely (parser))
5543 id = cp_parser_conversion_function_id (parser);
5544 else if (UDLIT_OPER_P (id))
5545 {
5546 /* 17.6.3.3.5 */
5547 const char *name = UDLIT_OP_SUFFIX (id);
5548 if (name[0] != '_' && !in_system_header_at (input_location)
5549 && declarator_p)
5550 warning (0, "literal operator suffixes not preceded by %<_%>"
5551 " are reserved for future standardization");
5552 }
5553
5554 return id;
5555 }
5556 /* Fall through. */
5557
5558 default:
5559 if (optional_p)
5560 return NULL_TREE;
5561 cp_parser_error (parser, "expected unqualified-id");
5562 return error_mark_node;
5563 }
5564 }
5565
5566 /* Parse an (optional) nested-name-specifier.
5567
5568 nested-name-specifier: [C++98]
5569 class-or-namespace-name :: nested-name-specifier [opt]
5570 class-or-namespace-name :: template nested-name-specifier [opt]
5571
5572 nested-name-specifier: [C++0x]
5573 type-name ::
5574 namespace-name ::
5575 nested-name-specifier identifier ::
5576 nested-name-specifier template [opt] simple-template-id ::
5577
5578 PARSER->SCOPE should be set appropriately before this function is
5579 called. TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
5580 effect. TYPE_P is TRUE if we non-type bindings should be ignored
5581 in name lookups.
5582
5583 Sets PARSER->SCOPE to the class (TYPE) or namespace
5584 (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
5585 it unchanged if there is no nested-name-specifier. Returns the new
5586 scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
5587
5588 If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
5589 part of a declaration and/or decl-specifier. */
5590
5591 static tree
5592 cp_parser_nested_name_specifier_opt (cp_parser *parser,
5593 bool typename_keyword_p,
5594 bool check_dependency_p,
5595 bool type_p,
5596 bool is_declaration)
5597 {
5598 bool success = false;
5599 cp_token_position start = 0;
5600 cp_token *token;
5601
5602 /* Remember where the nested-name-specifier starts. */
5603 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
5604 {
5605 start = cp_lexer_token_position (parser->lexer, false);
5606 push_deferring_access_checks (dk_deferred);
5607 }
5608
5609 while (true)
5610 {
5611 tree new_scope;
5612 tree old_scope;
5613 tree saved_qualifying_scope;
5614 bool template_keyword_p;
5615
5616 /* Spot cases that cannot be the beginning of a
5617 nested-name-specifier. */
5618 token = cp_lexer_peek_token (parser->lexer);
5619
5620 /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
5621 the already parsed nested-name-specifier. */
5622 if (token->type == CPP_NESTED_NAME_SPECIFIER)
5623 {
5624 /* Grab the nested-name-specifier and continue the loop. */
5625 cp_parser_pre_parsed_nested_name_specifier (parser);
5626 /* If we originally encountered this nested-name-specifier
5627 with IS_DECLARATION set to false, we will not have
5628 resolved TYPENAME_TYPEs, so we must do so here. */
5629 if (is_declaration
5630 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
5631 {
5632 new_scope = resolve_typename_type (parser->scope,
5633 /*only_current_p=*/false);
5634 if (TREE_CODE (new_scope) != TYPENAME_TYPE)
5635 parser->scope = new_scope;
5636 }
5637 success = true;
5638 continue;
5639 }
5640
5641 /* Spot cases that cannot be the beginning of a
5642 nested-name-specifier. On the second and subsequent times
5643 through the loop, we look for the `template' keyword. */
5644 if (success && token->keyword == RID_TEMPLATE)
5645 ;
5646 /* A template-id can start a nested-name-specifier. */
5647 else if (token->type == CPP_TEMPLATE_ID)
5648 ;
5649 /* DR 743: decltype can be used in a nested-name-specifier. */
5650 else if (token_is_decltype (token))
5651 ;
5652 else
5653 {
5654 /* If the next token is not an identifier, then it is
5655 definitely not a type-name or namespace-name. */
5656 if (token->type != CPP_NAME)
5657 break;
5658 /* If the following token is neither a `<' (to begin a
5659 template-id), nor a `::', then we are not looking at a
5660 nested-name-specifier. */
5661 token = cp_lexer_peek_nth_token (parser->lexer, 2);
5662
5663 if (token->type == CPP_COLON
5664 && parser->colon_corrects_to_scope_p
5665 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_NAME)
5666 {
5667 error_at (token->location,
5668 "found %<:%> in nested-name-specifier, expected %<::%>");
5669 token->type = CPP_SCOPE;
5670 }
5671
5672 if (token->type != CPP_SCOPE
5673 && !cp_parser_nth_token_starts_template_argument_list_p
5674 (parser, 2))
5675 break;
5676 }
5677
5678 /* The nested-name-specifier is optional, so we parse
5679 tentatively. */
5680 cp_parser_parse_tentatively (parser);
5681
5682 /* Look for the optional `template' keyword, if this isn't the
5683 first time through the loop. */
5684 if (success)
5685 template_keyword_p = cp_parser_optional_template_keyword (parser);
5686 else
5687 template_keyword_p = false;
5688
5689 /* Save the old scope since the name lookup we are about to do
5690 might destroy it. */
5691 old_scope = parser->scope;
5692 saved_qualifying_scope = parser->qualifying_scope;
5693 /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
5694 look up names in "X<T>::I" in order to determine that "Y" is
5695 a template. So, if we have a typename at this point, we make
5696 an effort to look through it. */
5697 if (is_declaration
5698 && !typename_keyword_p
5699 && parser->scope
5700 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
5701 parser->scope = resolve_typename_type (parser->scope,
5702 /*only_current_p=*/false);
5703 /* Parse the qualifying entity. */
5704 new_scope
5705 = cp_parser_qualifying_entity (parser,
5706 typename_keyword_p,
5707 template_keyword_p,
5708 check_dependency_p,
5709 type_p,
5710 is_declaration);
5711 /* Look for the `::' token. */
5712 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
5713
5714 /* If we found what we wanted, we keep going; otherwise, we're
5715 done. */
5716 if (!cp_parser_parse_definitely (parser))
5717 {
5718 bool error_p = false;
5719
5720 /* Restore the OLD_SCOPE since it was valid before the
5721 failed attempt at finding the last
5722 class-or-namespace-name. */
5723 parser->scope = old_scope;
5724 parser->qualifying_scope = saved_qualifying_scope;
5725
5726 /* If the next token is a decltype, and the one after that is a
5727 `::', then the decltype has failed to resolve to a class or
5728 enumeration type. Give this error even when parsing
5729 tentatively since it can't possibly be valid--and we're going
5730 to replace it with a CPP_NESTED_NAME_SPECIFIER below, so we
5731 won't get another chance.*/
5732 if (cp_lexer_next_token_is (parser->lexer, CPP_DECLTYPE)
5733 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
5734 == CPP_SCOPE))
5735 {
5736 token = cp_lexer_consume_token (parser->lexer);
5737 error_at (token->location, "decltype evaluates to %qT, "
5738 "which is not a class or enumeration type",
5739 token->u.value);
5740 parser->scope = error_mark_node;
5741 error_p = true;
5742 /* As below. */
5743 success = true;
5744 cp_lexer_consume_token (parser->lexer);
5745 }
5746
5747 if (cp_lexer_next_token_is (parser->lexer, CPP_TEMPLATE_ID)
5748 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_SCOPE))
5749 {
5750 /* If we have a non-type template-id followed by ::, it can't
5751 possibly be valid. */
5752 token = cp_lexer_peek_token (parser->lexer);
5753 tree tid = token->u.tree_check_value->value;
5754 if (TREE_CODE (tid) == TEMPLATE_ID_EXPR
5755 && TREE_CODE (TREE_OPERAND (tid, 0)) != IDENTIFIER_NODE)
5756 {
5757 tree tmpl = NULL_TREE;
5758 if (is_overloaded_fn (tid))
5759 {
5760 tree fns = get_fns (tid);
5761 if (!OVL_CHAIN (fns))
5762 tmpl = OVL_CURRENT (fns);
5763 error_at (token->location, "function template-id %qD "
5764 "in nested-name-specifier", tid);
5765 }
5766 else
5767 {
5768 /* Variable template. */
5769 tmpl = TREE_OPERAND (tid, 0);
5770 gcc_assert (variable_template_p (tmpl));
5771 error_at (token->location, "variable template-id %qD "
5772 "in nested-name-specifier", tid);
5773 }
5774 if (tmpl)
5775 inform (DECL_SOURCE_LOCATION (tmpl),
5776 "%qD declared here", tmpl);
5777
5778 parser->scope = error_mark_node;
5779 error_p = true;
5780 /* As below. */
5781 success = true;
5782 cp_lexer_consume_token (parser->lexer);
5783 cp_lexer_consume_token (parser->lexer);
5784 }
5785 }
5786
5787 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
5788 break;
5789 /* If the next token is an identifier, and the one after
5790 that is a `::', then any valid interpretation would have
5791 found a class-or-namespace-name. */
5792 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
5793 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
5794 == CPP_SCOPE)
5795 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
5796 != CPP_COMPL))
5797 {
5798 token = cp_lexer_consume_token (parser->lexer);
5799 if (!error_p)
5800 {
5801 if (!token->error_reported)
5802 {
5803 tree decl;
5804 tree ambiguous_decls;
5805
5806 decl = cp_parser_lookup_name (parser, token->u.value,
5807 none_type,
5808 /*is_template=*/false,
5809 /*is_namespace=*/false,
5810 /*check_dependency=*/true,
5811 &ambiguous_decls,
5812 token->location);
5813 if (TREE_CODE (decl) == TEMPLATE_DECL)
5814 error_at (token->location,
5815 "%qD used without template parameters",
5816 decl);
5817 else if (ambiguous_decls)
5818 {
5819 // cp_parser_lookup_name has the same diagnostic,
5820 // thus make sure to emit it at most once.
5821 if (cp_parser_uncommitted_to_tentative_parse_p
5822 (parser))
5823 {
5824 error_at (token->location,
5825 "reference to %qD is ambiguous",
5826 token->u.value);
5827 print_candidates (ambiguous_decls);
5828 }
5829 decl = error_mark_node;
5830 }
5831 else
5832 {
5833 if (cxx_dialect != cxx98)
5834 cp_parser_name_lookup_error
5835 (parser, token->u.value, decl, NLE_NOT_CXX98,
5836 token->location);
5837 else
5838 cp_parser_name_lookup_error
5839 (parser, token->u.value, decl, NLE_CXX98,
5840 token->location);
5841 }
5842 }
5843 parser->scope = error_mark_node;
5844 error_p = true;
5845 /* Treat this as a successful nested-name-specifier
5846 due to:
5847
5848 [basic.lookup.qual]
5849
5850 If the name found is not a class-name (clause
5851 _class_) or namespace-name (_namespace.def_), the
5852 program is ill-formed. */
5853 success = true;
5854 }
5855 cp_lexer_consume_token (parser->lexer);
5856 }
5857 break;
5858 }
5859 /* We've found one valid nested-name-specifier. */
5860 success = true;
5861 /* Name lookup always gives us a DECL. */
5862 if (TREE_CODE (new_scope) == TYPE_DECL)
5863 new_scope = TREE_TYPE (new_scope);
5864 /* Uses of "template" must be followed by actual templates. */
5865 if (template_keyword_p
5866 && !(CLASS_TYPE_P (new_scope)
5867 && ((CLASSTYPE_USE_TEMPLATE (new_scope)
5868 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (new_scope)))
5869 || CLASSTYPE_IS_TEMPLATE (new_scope)))
5870 && !(TREE_CODE (new_scope) == TYPENAME_TYPE
5871 && (TREE_CODE (TYPENAME_TYPE_FULLNAME (new_scope))
5872 == TEMPLATE_ID_EXPR)))
5873 permerror (input_location, TYPE_P (new_scope)
5874 ? G_("%qT is not a template")
5875 : G_("%qD is not a template"),
5876 new_scope);
5877 /* If it is a class scope, try to complete it; we are about to
5878 be looking up names inside the class. */
5879 if (TYPE_P (new_scope)
5880 /* Since checking types for dependency can be expensive,
5881 avoid doing it if the type is already complete. */
5882 && !COMPLETE_TYPE_P (new_scope)
5883 /* Do not try to complete dependent types. */
5884 && !dependent_type_p (new_scope))
5885 {
5886 new_scope = complete_type (new_scope);
5887 /* If it is a typedef to current class, use the current
5888 class instead, as the typedef won't have any names inside
5889 it yet. */
5890 if (!COMPLETE_TYPE_P (new_scope)
5891 && currently_open_class (new_scope))
5892 new_scope = TYPE_MAIN_VARIANT (new_scope);
5893 }
5894 /* Make sure we look in the right scope the next time through
5895 the loop. */
5896 parser->scope = new_scope;
5897 }
5898
5899 /* If parsing tentatively, replace the sequence of tokens that makes
5900 up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
5901 token. That way, should we re-parse the token stream, we will
5902 not have to repeat the effort required to do the parse, nor will
5903 we issue duplicate error messages. */
5904 if (success && start)
5905 {
5906 cp_token *token;
5907
5908 token = cp_lexer_token_at (parser->lexer, start);
5909 /* Reset the contents of the START token. */
5910 token->type = CPP_NESTED_NAME_SPECIFIER;
5911 /* Retrieve any deferred checks. Do not pop this access checks yet
5912 so the memory will not be reclaimed during token replacing below. */
5913 token->u.tree_check_value = ggc_cleared_alloc<struct tree_check> ();
5914 token->u.tree_check_value->value = parser->scope;
5915 token->u.tree_check_value->checks = get_deferred_access_checks ();
5916 token->u.tree_check_value->qualifying_scope =
5917 parser->qualifying_scope;
5918 token->keyword = RID_MAX;
5919
5920 /* Purge all subsequent tokens. */
5921 cp_lexer_purge_tokens_after (parser->lexer, start);
5922 }
5923
5924 if (start)
5925 pop_to_parent_deferring_access_checks ();
5926
5927 return success ? parser->scope : NULL_TREE;
5928 }
5929
5930 /* Parse a nested-name-specifier. See
5931 cp_parser_nested_name_specifier_opt for details. This function
5932 behaves identically, except that it will an issue an error if no
5933 nested-name-specifier is present. */
5934
5935 static tree
5936 cp_parser_nested_name_specifier (cp_parser *parser,
5937 bool typename_keyword_p,
5938 bool check_dependency_p,
5939 bool type_p,
5940 bool is_declaration)
5941 {
5942 tree scope;
5943
5944 /* Look for the nested-name-specifier. */
5945 scope = cp_parser_nested_name_specifier_opt (parser,
5946 typename_keyword_p,
5947 check_dependency_p,
5948 type_p,
5949 is_declaration);
5950 /* If it was not present, issue an error message. */
5951 if (!scope)
5952 {
5953 cp_parser_error (parser, "expected nested-name-specifier");
5954 parser->scope = NULL_TREE;
5955 }
5956
5957 return scope;
5958 }
5959
5960 /* Parse the qualifying entity in a nested-name-specifier. For C++98,
5961 this is either a class-name or a namespace-name (which corresponds
5962 to the class-or-namespace-name production in the grammar). For
5963 C++0x, it can also be a type-name that refers to an enumeration
5964 type or a simple-template-id.
5965
5966 TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
5967 TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
5968 CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
5969 TYPE_P is TRUE iff the next name should be taken as a class-name,
5970 even the same name is declared to be another entity in the same
5971 scope.
5972
5973 Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
5974 specified by the class-or-namespace-name. If neither is found the
5975 ERROR_MARK_NODE is returned. */
5976
5977 static tree
5978 cp_parser_qualifying_entity (cp_parser *parser,
5979 bool typename_keyword_p,
5980 bool template_keyword_p,
5981 bool check_dependency_p,
5982 bool type_p,
5983 bool is_declaration)
5984 {
5985 tree saved_scope;
5986 tree saved_qualifying_scope;
5987 tree saved_object_scope;
5988 tree scope;
5989 bool only_class_p;
5990 bool successful_parse_p;
5991
5992 /* DR 743: decltype can appear in a nested-name-specifier. */
5993 if (cp_lexer_next_token_is_decltype (parser->lexer))
5994 {
5995 scope = cp_parser_decltype (parser);
5996 if (TREE_CODE (scope) != ENUMERAL_TYPE
5997 && !MAYBE_CLASS_TYPE_P (scope))
5998 {
5999 cp_parser_simulate_error (parser);
6000 return error_mark_node;
6001 }
6002 if (TYPE_NAME (scope))
6003 scope = TYPE_NAME (scope);
6004 return scope;
6005 }
6006
6007 /* Before we try to parse the class-name, we must save away the
6008 current PARSER->SCOPE since cp_parser_class_name will destroy
6009 it. */
6010 saved_scope = parser->scope;
6011 saved_qualifying_scope = parser->qualifying_scope;
6012 saved_object_scope = parser->object_scope;
6013 /* Try for a class-name first. If the SAVED_SCOPE is a type, then
6014 there is no need to look for a namespace-name. */
6015 only_class_p = template_keyword_p
6016 || (saved_scope && TYPE_P (saved_scope) && cxx_dialect == cxx98);
6017 if (!only_class_p)
6018 cp_parser_parse_tentatively (parser);
6019 scope = cp_parser_class_name (parser,
6020 typename_keyword_p,
6021 template_keyword_p,
6022 type_p ? class_type : none_type,
6023 check_dependency_p,
6024 /*class_head_p=*/false,
6025 is_declaration,
6026 /*enum_ok=*/cxx_dialect > cxx98);
6027 successful_parse_p = only_class_p || cp_parser_parse_definitely (parser);
6028 /* If that didn't work, try for a namespace-name. */
6029 if (!only_class_p && !successful_parse_p)
6030 {
6031 /* Restore the saved scope. */
6032 parser->scope = saved_scope;
6033 parser->qualifying_scope = saved_qualifying_scope;
6034 parser->object_scope = saved_object_scope;
6035 /* If we are not looking at an identifier followed by the scope
6036 resolution operator, then this is not part of a
6037 nested-name-specifier. (Note that this function is only used
6038 to parse the components of a nested-name-specifier.) */
6039 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
6040 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
6041 return error_mark_node;
6042 scope = cp_parser_namespace_name (parser);
6043 }
6044
6045 return scope;
6046 }
6047
6048 /* Return true if we are looking at a compound-literal, false otherwise. */
6049
6050 static bool
6051 cp_parser_compound_literal_p (cp_parser *parser)
6052 {
6053 /* Consume the `('. */
6054 cp_lexer_consume_token (parser->lexer);
6055
6056 cp_lexer_save_tokens (parser->lexer);
6057
6058 /* Skip tokens until the next token is a closing parenthesis.
6059 If we find the closing `)', and the next token is a `{', then
6060 we are looking at a compound-literal. */
6061 bool compound_literal_p
6062 = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
6063 /*consume_paren=*/true)
6064 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
6065
6066 /* Roll back the tokens we skipped. */
6067 cp_lexer_rollback_tokens (parser->lexer);
6068
6069 return compound_literal_p;
6070 }
6071
6072 /* Parse a postfix-expression.
6073
6074 postfix-expression:
6075 primary-expression
6076 postfix-expression [ expression ]
6077 postfix-expression ( expression-list [opt] )
6078 simple-type-specifier ( expression-list [opt] )
6079 typename :: [opt] nested-name-specifier identifier
6080 ( expression-list [opt] )
6081 typename :: [opt] nested-name-specifier template [opt] template-id
6082 ( expression-list [opt] )
6083 postfix-expression . template [opt] id-expression
6084 postfix-expression -> template [opt] id-expression
6085 postfix-expression . pseudo-destructor-name
6086 postfix-expression -> pseudo-destructor-name
6087 postfix-expression ++
6088 postfix-expression --
6089 dynamic_cast < type-id > ( expression )
6090 static_cast < type-id > ( expression )
6091 reinterpret_cast < type-id > ( expression )
6092 const_cast < type-id > ( expression )
6093 typeid ( expression )
6094 typeid ( type-id )
6095
6096 GNU Extension:
6097
6098 postfix-expression:
6099 ( type-id ) { initializer-list , [opt] }
6100
6101 This extension is a GNU version of the C99 compound-literal
6102 construct. (The C99 grammar uses `type-name' instead of `type-id',
6103 but they are essentially the same concept.)
6104
6105 If ADDRESS_P is true, the postfix expression is the operand of the
6106 `&' operator. CAST_P is true if this expression is the target of a
6107 cast.
6108
6109 If MEMBER_ACCESS_ONLY_P, we only allow postfix expressions that are
6110 class member access expressions [expr.ref].
6111
6112 Returns a representation of the expression. */
6113
6114 static tree
6115 cp_parser_postfix_expression (cp_parser *parser, bool address_p, bool cast_p,
6116 bool member_access_only_p, bool decltype_p,
6117 cp_id_kind * pidk_return)
6118 {
6119 cp_token *token;
6120 location_t loc;
6121 enum rid keyword;
6122 cp_id_kind idk = CP_ID_KIND_NONE;
6123 tree postfix_expression = NULL_TREE;
6124 bool is_member_access = false;
6125 int saved_in_statement = -1;
6126
6127 /* Peek at the next token. */
6128 token = cp_lexer_peek_token (parser->lexer);
6129 loc = token->location;
6130 /* Some of the productions are determined by keywords. */
6131 keyword = token->keyword;
6132 switch (keyword)
6133 {
6134 case RID_DYNCAST:
6135 case RID_STATCAST:
6136 case RID_REINTCAST:
6137 case RID_CONSTCAST:
6138 {
6139 tree type;
6140 tree expression;
6141 const char *saved_message;
6142 bool saved_in_type_id_in_expr_p;
6143
6144 /* All of these can be handled in the same way from the point
6145 of view of parsing. Begin by consuming the token
6146 identifying the cast. */
6147 cp_lexer_consume_token (parser->lexer);
6148
6149 /* New types cannot be defined in the cast. */
6150 saved_message = parser->type_definition_forbidden_message;
6151 parser->type_definition_forbidden_message
6152 = G_("types may not be defined in casts");
6153
6154 /* Look for the opening `<'. */
6155 cp_parser_require (parser, CPP_LESS, RT_LESS);
6156 /* Parse the type to which we are casting. */
6157 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
6158 parser->in_type_id_in_expr_p = true;
6159 type = cp_parser_type_id (parser);
6160 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
6161 /* Look for the closing `>'. */
6162 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
6163 /* Restore the old message. */
6164 parser->type_definition_forbidden_message = saved_message;
6165
6166 bool saved_greater_than_is_operator_p
6167 = parser->greater_than_is_operator_p;
6168 parser->greater_than_is_operator_p = true;
6169
6170 /* And the expression which is being cast. */
6171 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
6172 expression = cp_parser_expression (parser, & idk, /*cast_p=*/true);
6173 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
6174
6175 parser->greater_than_is_operator_p
6176 = saved_greater_than_is_operator_p;
6177
6178 /* Only type conversions to integral or enumeration types
6179 can be used in constant-expressions. */
6180 if (!cast_valid_in_integral_constant_expression_p (type)
6181 && cp_parser_non_integral_constant_expression (parser, NIC_CAST))
6182 return error_mark_node;
6183
6184 switch (keyword)
6185 {
6186 case RID_DYNCAST:
6187 postfix_expression
6188 = build_dynamic_cast (type, expression, tf_warning_or_error);
6189 break;
6190 case RID_STATCAST:
6191 postfix_expression
6192 = build_static_cast (type, expression, tf_warning_or_error);
6193 break;
6194 case RID_REINTCAST:
6195 postfix_expression
6196 = build_reinterpret_cast (type, expression,
6197 tf_warning_or_error);
6198 break;
6199 case RID_CONSTCAST:
6200 postfix_expression
6201 = build_const_cast (type, expression, tf_warning_or_error);
6202 break;
6203 default:
6204 gcc_unreachable ();
6205 }
6206 }
6207 break;
6208
6209 case RID_TYPEID:
6210 {
6211 tree type;
6212 const char *saved_message;
6213 bool saved_in_type_id_in_expr_p;
6214
6215 /* Consume the `typeid' token. */
6216 cp_lexer_consume_token (parser->lexer);
6217 /* Look for the `(' token. */
6218 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
6219 /* Types cannot be defined in a `typeid' expression. */
6220 saved_message = parser->type_definition_forbidden_message;
6221 parser->type_definition_forbidden_message
6222 = G_("types may not be defined in a %<typeid%> expression");
6223 /* We can't be sure yet whether we're looking at a type-id or an
6224 expression. */
6225 cp_parser_parse_tentatively (parser);
6226 /* Try a type-id first. */
6227 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
6228 parser->in_type_id_in_expr_p = true;
6229 type = cp_parser_type_id (parser);
6230 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
6231 /* Look for the `)' token. Otherwise, we can't be sure that
6232 we're not looking at an expression: consider `typeid (int
6233 (3))', for example. */
6234 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
6235 /* If all went well, simply lookup the type-id. */
6236 if (cp_parser_parse_definitely (parser))
6237 postfix_expression = get_typeid (type, tf_warning_or_error);
6238 /* Otherwise, fall back to the expression variant. */
6239 else
6240 {
6241 tree expression;
6242
6243 /* Look for an expression. */
6244 expression = cp_parser_expression (parser, & idk);
6245 /* Compute its typeid. */
6246 postfix_expression = build_typeid (expression, tf_warning_or_error);
6247 /* Look for the `)' token. */
6248 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
6249 }
6250 /* Restore the saved message. */
6251 parser->type_definition_forbidden_message = saved_message;
6252 /* `typeid' may not appear in an integral constant expression. */
6253 if (cp_parser_non_integral_constant_expression (parser, NIC_TYPEID))
6254 return error_mark_node;
6255 }
6256 break;
6257
6258 case RID_TYPENAME:
6259 {
6260 tree type;
6261 /* The syntax permitted here is the same permitted for an
6262 elaborated-type-specifier. */
6263 ++parser->prevent_constrained_type_specifiers;
6264 type = cp_parser_elaborated_type_specifier (parser,
6265 /*is_friend=*/false,
6266 /*is_declaration=*/false);
6267 --parser->prevent_constrained_type_specifiers;
6268 postfix_expression = cp_parser_functional_cast (parser, type);
6269 }
6270 break;
6271
6272 case RID_CILK_SPAWN:
6273 {
6274 cp_lexer_consume_token (parser->lexer);
6275 token = cp_lexer_peek_token (parser->lexer);
6276 if (token->type == CPP_SEMICOLON)
6277 {
6278 error_at (token->location, "%<_Cilk_spawn%> must be followed by "
6279 "an expression");
6280 postfix_expression = error_mark_node;
6281 break;
6282 }
6283 else if (!current_function_decl)
6284 {
6285 error_at (token->location, "%<_Cilk_spawn%> may only be used "
6286 "inside a function");
6287 postfix_expression = error_mark_node;
6288 break;
6289 }
6290 else
6291 {
6292 /* Consecutive _Cilk_spawns are not allowed in a statement. */
6293 saved_in_statement = parser->in_statement;
6294 parser->in_statement |= IN_CILK_SPAWN;
6295 }
6296 cfun->calls_cilk_spawn = 1;
6297 postfix_expression =
6298 cp_parser_postfix_expression (parser, false, false,
6299 false, false, &idk);
6300 if (!flag_cilkplus)
6301 {
6302 error_at (token->location, "-fcilkplus must be enabled to use"
6303 " %<_Cilk_spawn%>");
6304 cfun->calls_cilk_spawn = 0;
6305 }
6306 else if (saved_in_statement & IN_CILK_SPAWN)
6307 {
6308 error_at (token->location, "consecutive %<_Cilk_spawn%> keywords "
6309 "are not permitted");
6310 postfix_expression = error_mark_node;
6311 cfun->calls_cilk_spawn = 0;
6312 }
6313 else
6314 {
6315 postfix_expression = build_cilk_spawn (token->location,
6316 postfix_expression);
6317 if (postfix_expression != error_mark_node)
6318 SET_EXPR_LOCATION (postfix_expression, input_location);
6319 parser->in_statement = parser->in_statement & ~IN_CILK_SPAWN;
6320 }
6321 break;
6322 }
6323
6324 case RID_BUILTIN_SHUFFLE:
6325 {
6326 vec<tree, va_gc> *vec;
6327 unsigned int i;
6328 tree p;
6329
6330 cp_lexer_consume_token (parser->lexer);
6331 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
6332 /*cast_p=*/false, /*allow_expansion_p=*/true,
6333 /*non_constant_p=*/NULL);
6334 if (vec == NULL)
6335 return error_mark_node;
6336
6337 FOR_EACH_VEC_ELT (*vec, i, p)
6338 mark_exp_read (p);
6339
6340 if (vec->length () == 2)
6341 return build_x_vec_perm_expr (loc, (*vec)[0], NULL_TREE, (*vec)[1],
6342 tf_warning_or_error);
6343 else if (vec->length () == 3)
6344 return build_x_vec_perm_expr (loc, (*vec)[0], (*vec)[1], (*vec)[2],
6345 tf_warning_or_error);
6346 else
6347 {
6348 error_at (loc, "wrong number of arguments to "
6349 "%<__builtin_shuffle%>");
6350 return error_mark_node;
6351 }
6352 break;
6353 }
6354
6355 default:
6356 {
6357 tree type;
6358
6359 /* If the next thing is a simple-type-specifier, we may be
6360 looking at a functional cast. We could also be looking at
6361 an id-expression. So, we try the functional cast, and if
6362 that doesn't work we fall back to the primary-expression. */
6363 cp_parser_parse_tentatively (parser);
6364 /* Look for the simple-type-specifier. */
6365 ++parser->prevent_constrained_type_specifiers;
6366 type = cp_parser_simple_type_specifier (parser,
6367 /*decl_specs=*/NULL,
6368 CP_PARSER_FLAGS_NONE);
6369 --parser->prevent_constrained_type_specifiers;
6370 /* Parse the cast itself. */
6371 if (!cp_parser_error_occurred (parser))
6372 postfix_expression
6373 = cp_parser_functional_cast (parser, type);
6374 /* If that worked, we're done. */
6375 if (cp_parser_parse_definitely (parser))
6376 break;
6377
6378 /* If the functional-cast didn't work out, try a
6379 compound-literal. */
6380 if (cp_parser_allow_gnu_extensions_p (parser)
6381 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
6382 {
6383 tree initializer = NULL_TREE;
6384
6385 cp_parser_parse_tentatively (parser);
6386
6387 /* Avoid calling cp_parser_type_id pointlessly, see comment
6388 in cp_parser_cast_expression about c++/29234. */
6389 if (!cp_parser_compound_literal_p (parser))
6390 cp_parser_simulate_error (parser);
6391 else
6392 {
6393 /* Parse the type. */
6394 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
6395 parser->in_type_id_in_expr_p = true;
6396 type = cp_parser_type_id (parser);
6397 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
6398 /* Look for the `)'. */
6399 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
6400 }
6401
6402 /* If things aren't going well, there's no need to
6403 keep going. */
6404 if (!cp_parser_error_occurred (parser))
6405 {
6406 bool non_constant_p;
6407 /* Parse the brace-enclosed initializer list. */
6408 initializer = cp_parser_braced_list (parser,
6409 &non_constant_p);
6410 }
6411 /* If that worked, we're definitely looking at a
6412 compound-literal expression. */
6413 if (cp_parser_parse_definitely (parser))
6414 {
6415 /* Warn the user that a compound literal is not
6416 allowed in standard C++. */
6417 pedwarn (input_location, OPT_Wpedantic,
6418 "ISO C++ forbids compound-literals");
6419 /* For simplicity, we disallow compound literals in
6420 constant-expressions. We could
6421 allow compound literals of integer type, whose
6422 initializer was a constant, in constant
6423 expressions. Permitting that usage, as a further
6424 extension, would not change the meaning of any
6425 currently accepted programs. (Of course, as
6426 compound literals are not part of ISO C++, the
6427 standard has nothing to say.) */
6428 if (cp_parser_non_integral_constant_expression (parser,
6429 NIC_NCC))
6430 {
6431 postfix_expression = error_mark_node;
6432 break;
6433 }
6434 /* Form the representation of the compound-literal. */
6435 postfix_expression
6436 = finish_compound_literal (type, initializer,
6437 tf_warning_or_error);
6438 break;
6439 }
6440 }
6441
6442 /* It must be a primary-expression. */
6443 postfix_expression
6444 = cp_parser_primary_expression (parser, address_p, cast_p,
6445 /*template_arg_p=*/false,
6446 decltype_p,
6447 &idk);
6448 }
6449 break;
6450 }
6451
6452 /* Note that we don't need to worry about calling build_cplus_new on a
6453 class-valued CALL_EXPR in decltype when it isn't the end of the
6454 postfix-expression; unary_complex_lvalue will take care of that for
6455 all these cases. */
6456
6457 /* Keep looping until the postfix-expression is complete. */
6458 while (true)
6459 {
6460 if (idk == CP_ID_KIND_UNQUALIFIED
6461 && identifier_p (postfix_expression)
6462 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
6463 /* It is not a Koenig lookup function call. */
6464 postfix_expression
6465 = unqualified_name_lookup_error (postfix_expression);
6466
6467 /* Peek at the next token. */
6468 token = cp_lexer_peek_token (parser->lexer);
6469
6470 switch (token->type)
6471 {
6472 case CPP_OPEN_SQUARE:
6473 if (cp_next_tokens_can_be_std_attribute_p (parser))
6474 {
6475 cp_parser_error (parser,
6476 "two consecutive %<[%> shall "
6477 "only introduce an attribute");
6478 return error_mark_node;
6479 }
6480 postfix_expression
6481 = cp_parser_postfix_open_square_expression (parser,
6482 postfix_expression,
6483 false,
6484 decltype_p);
6485 idk = CP_ID_KIND_NONE;
6486 is_member_access = false;
6487 break;
6488
6489 case CPP_OPEN_PAREN:
6490 /* postfix-expression ( expression-list [opt] ) */
6491 {
6492 bool koenig_p;
6493 bool is_builtin_constant_p;
6494 bool saved_integral_constant_expression_p = false;
6495 bool saved_non_integral_constant_expression_p = false;
6496 tsubst_flags_t complain = complain_flags (decltype_p);
6497 vec<tree, va_gc> *args;
6498
6499 is_member_access = false;
6500
6501 is_builtin_constant_p
6502 = DECL_IS_BUILTIN_CONSTANT_P (postfix_expression);
6503 if (is_builtin_constant_p)
6504 {
6505 /* The whole point of __builtin_constant_p is to allow
6506 non-constant expressions to appear as arguments. */
6507 saved_integral_constant_expression_p
6508 = parser->integral_constant_expression_p;
6509 saved_non_integral_constant_expression_p
6510 = parser->non_integral_constant_expression_p;
6511 parser->integral_constant_expression_p = false;
6512 }
6513 args = (cp_parser_parenthesized_expression_list
6514 (parser, non_attr,
6515 /*cast_p=*/false, /*allow_expansion_p=*/true,
6516 /*non_constant_p=*/NULL));
6517 if (is_builtin_constant_p)
6518 {
6519 parser->integral_constant_expression_p
6520 = saved_integral_constant_expression_p;
6521 parser->non_integral_constant_expression_p
6522 = saved_non_integral_constant_expression_p;
6523 }
6524
6525 if (args == NULL)
6526 {
6527 postfix_expression = error_mark_node;
6528 break;
6529 }
6530
6531 /* Function calls are not permitted in
6532 constant-expressions. */
6533 if (! builtin_valid_in_constant_expr_p (postfix_expression)
6534 && cp_parser_non_integral_constant_expression (parser,
6535 NIC_FUNC_CALL))
6536 {
6537 postfix_expression = error_mark_node;
6538 release_tree_vector (args);
6539 break;
6540 }
6541
6542 koenig_p = false;
6543 if (idk == CP_ID_KIND_UNQUALIFIED
6544 || idk == CP_ID_KIND_TEMPLATE_ID)
6545 {
6546 if (identifier_p (postfix_expression))
6547 {
6548 if (!args->is_empty ())
6549 {
6550 koenig_p = true;
6551 if (!any_type_dependent_arguments_p (args))
6552 postfix_expression
6553 = perform_koenig_lookup (postfix_expression, args,
6554 complain);
6555 }
6556 else
6557 postfix_expression
6558 = unqualified_fn_lookup_error (postfix_expression);
6559 }
6560 /* We do not perform argument-dependent lookup if
6561 normal lookup finds a non-function, in accordance
6562 with the expected resolution of DR 218. */
6563 else if (!args->is_empty ()
6564 && is_overloaded_fn (postfix_expression))
6565 {
6566 tree fn = get_first_fn (postfix_expression);
6567 fn = STRIP_TEMPLATE (fn);
6568
6569 /* Do not do argument dependent lookup if regular
6570 lookup finds a member function or a block-scope
6571 function declaration. [basic.lookup.argdep]/3 */
6572 if (!DECL_FUNCTION_MEMBER_P (fn)
6573 && !DECL_LOCAL_FUNCTION_P (fn))
6574 {
6575 koenig_p = true;
6576 if (!any_type_dependent_arguments_p (args))
6577 postfix_expression
6578 = perform_koenig_lookup (postfix_expression, args,
6579 complain);
6580 }
6581 }
6582 }
6583
6584 if (warn_memset_transposed_args)
6585 {
6586 if (TREE_CODE (postfix_expression) == FUNCTION_DECL
6587 && DECL_BUILT_IN_CLASS (postfix_expression) == BUILT_IN_NORMAL
6588 && DECL_FUNCTION_CODE (postfix_expression) == BUILT_IN_MEMSET
6589 && vec_safe_length (args) == 3
6590 && TREE_CODE ((*args)[2]) == INTEGER_CST
6591 && integer_zerop ((*args)[2])
6592 && !(TREE_CODE ((*args)[1]) == INTEGER_CST
6593 && integer_zerop ((*args)[1])))
6594 warning (OPT_Wmemset_transposed_args,
6595 "%<memset%> used with constant zero length "
6596 "parameter; this could be due to transposed "
6597 "parameters");
6598 }
6599
6600 if (TREE_CODE (postfix_expression) == COMPONENT_REF)
6601 {
6602 tree instance = TREE_OPERAND (postfix_expression, 0);
6603 tree fn = TREE_OPERAND (postfix_expression, 1);
6604
6605 if (processing_template_decl
6606 && (type_dependent_expression_p (instance)
6607 || (!BASELINK_P (fn)
6608 && TREE_CODE (fn) != FIELD_DECL)
6609 || type_dependent_expression_p (fn)
6610 || any_type_dependent_arguments_p (args)))
6611 {
6612 postfix_expression
6613 = build_nt_call_vec (postfix_expression, args);
6614 release_tree_vector (args);
6615 break;
6616 }
6617
6618 if (BASELINK_P (fn))
6619 {
6620 postfix_expression
6621 = (build_new_method_call
6622 (instance, fn, &args, NULL_TREE,
6623 (idk == CP_ID_KIND_QUALIFIED
6624 ? LOOKUP_NORMAL|LOOKUP_NONVIRTUAL
6625 : LOOKUP_NORMAL),
6626 /*fn_p=*/NULL,
6627 complain));
6628 }
6629 else
6630 postfix_expression
6631 = finish_call_expr (postfix_expression, &args,
6632 /*disallow_virtual=*/false,
6633 /*koenig_p=*/false,
6634 complain);
6635 }
6636 else if (TREE_CODE (postfix_expression) == OFFSET_REF
6637 || TREE_CODE (postfix_expression) == MEMBER_REF
6638 || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
6639 postfix_expression = (build_offset_ref_call_from_tree
6640 (postfix_expression, &args,
6641 complain));
6642 else if (idk == CP_ID_KIND_QUALIFIED)
6643 /* A call to a static class member, or a namespace-scope
6644 function. */
6645 postfix_expression
6646 = finish_call_expr (postfix_expression, &args,
6647 /*disallow_virtual=*/true,
6648 koenig_p,
6649 complain);
6650 else
6651 /* All other function calls. */
6652 postfix_expression
6653 = finish_call_expr (postfix_expression, &args,
6654 /*disallow_virtual=*/false,
6655 koenig_p,
6656 complain);
6657
6658 protected_set_expr_location (postfix_expression, token->location);
6659
6660 /* The POSTFIX_EXPRESSION is certainly no longer an id. */
6661 idk = CP_ID_KIND_NONE;
6662
6663 release_tree_vector (args);
6664 }
6665 break;
6666
6667 case CPP_DOT:
6668 case CPP_DEREF:
6669 /* postfix-expression . template [opt] id-expression
6670 postfix-expression . pseudo-destructor-name
6671 postfix-expression -> template [opt] id-expression
6672 postfix-expression -> pseudo-destructor-name */
6673
6674 /* Consume the `.' or `->' operator. */
6675 cp_lexer_consume_token (parser->lexer);
6676
6677 postfix_expression
6678 = cp_parser_postfix_dot_deref_expression (parser, token->type,
6679 postfix_expression,
6680 false, &idk, loc);
6681
6682 is_member_access = true;
6683 break;
6684
6685 case CPP_PLUS_PLUS:
6686 /* postfix-expression ++ */
6687 /* Consume the `++' token. */
6688 cp_lexer_consume_token (parser->lexer);
6689 /* Generate a representation for the complete expression. */
6690 postfix_expression
6691 = finish_increment_expr (postfix_expression,
6692 POSTINCREMENT_EXPR);
6693 /* Increments may not appear in constant-expressions. */
6694 if (cp_parser_non_integral_constant_expression (parser, NIC_INC))
6695 postfix_expression = error_mark_node;
6696 idk = CP_ID_KIND_NONE;
6697 is_member_access = false;
6698 break;
6699
6700 case CPP_MINUS_MINUS:
6701 /* postfix-expression -- */
6702 /* Consume the `--' token. */
6703 cp_lexer_consume_token (parser->lexer);
6704 /* Generate a representation for the complete expression. */
6705 postfix_expression
6706 = finish_increment_expr (postfix_expression,
6707 POSTDECREMENT_EXPR);
6708 /* Decrements may not appear in constant-expressions. */
6709 if (cp_parser_non_integral_constant_expression (parser, NIC_DEC))
6710 postfix_expression = error_mark_node;
6711 idk = CP_ID_KIND_NONE;
6712 is_member_access = false;
6713 break;
6714
6715 default:
6716 if (pidk_return != NULL)
6717 * pidk_return = idk;
6718 if (member_access_only_p)
6719 return is_member_access? postfix_expression : error_mark_node;
6720 else
6721 return postfix_expression;
6722 }
6723 }
6724
6725 /* We should never get here. */
6726 gcc_unreachable ();
6727 return error_mark_node;
6728 }
6729
6730 /* This function parses Cilk Plus array notations. If a normal array expr. is
6731 parsed then the array index is passed back to the caller through *INIT_INDEX
6732 and the function returns a NULL_TREE. If array notation expr. is parsed,
6733 then *INIT_INDEX is ignored by the caller and the function returns
6734 a tree of type ARRAY_NOTATION_REF. If some error occurred it returns
6735 error_mark_node. */
6736
6737 static tree
6738 cp_parser_array_notation (location_t loc, cp_parser *parser, tree *init_index,
6739 tree array_value)
6740 {
6741 cp_token *token = NULL;
6742 tree length_index, stride = NULL_TREE, value_tree, array_type;
6743 if (!array_value || array_value == error_mark_node)
6744 {
6745 cp_parser_skip_to_end_of_statement (parser);
6746 return error_mark_node;
6747 }
6748
6749 array_type = TREE_TYPE (array_value);
6750
6751 bool saved_colon_corrects = parser->colon_corrects_to_scope_p;
6752 parser->colon_corrects_to_scope_p = false;
6753 token = cp_lexer_peek_token (parser->lexer);
6754
6755 if (!token)
6756 {
6757 cp_parser_error (parser, "expected %<:%> or numeral");
6758 return error_mark_node;
6759 }
6760 else if (token->type == CPP_COLON)
6761 {
6762 /* Consume the ':'. */
6763 cp_lexer_consume_token (parser->lexer);
6764
6765 /* If we are here, then we have a case like this A[:]. */
6766 if (cp_lexer_peek_token (parser->lexer)->type != CPP_CLOSE_SQUARE)
6767 {
6768 cp_parser_error (parser, "expected %<]%>");
6769 cp_parser_skip_to_end_of_statement (parser);
6770 return error_mark_node;
6771 }
6772 *init_index = NULL_TREE;
6773 stride = NULL_TREE;
6774 length_index = NULL_TREE;
6775 }
6776 else
6777 {
6778 /* If we are here, then there are three valid possibilities:
6779 1. ARRAY [ EXP ]
6780 2. ARRAY [ EXP : EXP ]
6781 3. ARRAY [ EXP : EXP : EXP ] */
6782
6783 *init_index = cp_parser_expression (parser);
6784 if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
6785 {
6786 /* This indicates that we have a normal array expression. */
6787 parser->colon_corrects_to_scope_p = saved_colon_corrects;
6788 return NULL_TREE;
6789 }
6790
6791 /* Consume the ':'. */
6792 cp_lexer_consume_token (parser->lexer);
6793 length_index = cp_parser_expression (parser);
6794 if (cp_lexer_peek_token (parser->lexer)->type == CPP_COLON)
6795 {
6796 cp_lexer_consume_token (parser->lexer);
6797 stride = cp_parser_expression (parser);
6798 }
6799 }
6800 parser->colon_corrects_to_scope_p = saved_colon_corrects;
6801
6802 if (*init_index == error_mark_node || length_index == error_mark_node
6803 || stride == error_mark_node || array_type == error_mark_node)
6804 {
6805 if (cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_SQUARE)
6806 cp_lexer_consume_token (parser->lexer);
6807 return error_mark_node;
6808 }
6809 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
6810
6811 value_tree = build_array_notation_ref (loc, array_value, *init_index,
6812 length_index, stride, array_type);
6813 return value_tree;
6814 }
6815
6816 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
6817 by cp_parser_builtin_offsetof. We're looking for
6818
6819 postfix-expression [ expression ]
6820 postfix-expression [ braced-init-list ] (C++11)
6821
6822 FOR_OFFSETOF is set if we're being called in that context, which
6823 changes how we deal with integer constant expressions. */
6824
6825 static tree
6826 cp_parser_postfix_open_square_expression (cp_parser *parser,
6827 tree postfix_expression,
6828 bool for_offsetof,
6829 bool decltype_p)
6830 {
6831 tree index = NULL_TREE;
6832 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
6833 bool saved_greater_than_is_operator_p;
6834
6835 /* Consume the `[' token. */
6836 cp_lexer_consume_token (parser->lexer);
6837
6838 saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
6839 parser->greater_than_is_operator_p = true;
6840
6841 /* Parse the index expression. */
6842 /* ??? For offsetof, there is a question of what to allow here. If
6843 offsetof is not being used in an integral constant expression context,
6844 then we *could* get the right answer by computing the value at runtime.
6845 If we are in an integral constant expression context, then we might
6846 could accept any constant expression; hard to say without analysis.
6847 Rather than open the barn door too wide right away, allow only integer
6848 constant expressions here. */
6849 if (for_offsetof)
6850 index = cp_parser_constant_expression (parser);
6851 else
6852 {
6853 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
6854 {
6855 bool expr_nonconst_p;
6856 cp_lexer_set_source_position (parser->lexer);
6857 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
6858 index = cp_parser_braced_list (parser, &expr_nonconst_p);
6859 if (flag_cilkplus
6860 && cp_lexer_peek_token (parser->lexer)->type == CPP_COLON)
6861 {
6862 error_at (cp_lexer_peek_token (parser->lexer)->location,
6863 "braced list index is not allowed with array "
6864 "notation");
6865 cp_parser_skip_to_end_of_statement (parser);
6866 return error_mark_node;
6867 }
6868 }
6869 else if (flag_cilkplus)
6870 {
6871 /* Here are have these two options:
6872 ARRAY[EXP : EXP] - Array notation expr with default
6873 stride of 1.
6874 ARRAY[EXP : EXP : EXP] - Array Notation with user-defined
6875 stride. */
6876 tree an_exp = cp_parser_array_notation (loc, parser, &index,
6877 postfix_expression);
6878 if (an_exp)
6879 return an_exp;
6880 }
6881 else
6882 index = cp_parser_expression (parser);
6883 }
6884
6885 parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
6886
6887 /* Look for the closing `]'. */
6888 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
6889
6890 /* Build the ARRAY_REF. */
6891 postfix_expression = grok_array_decl (loc, postfix_expression,
6892 index, decltype_p);
6893
6894 /* When not doing offsetof, array references are not permitted in
6895 constant-expressions. */
6896 if (!for_offsetof
6897 && (cp_parser_non_integral_constant_expression (parser, NIC_ARRAY_REF)))
6898 postfix_expression = error_mark_node;
6899
6900 return postfix_expression;
6901 }
6902
6903 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
6904 by cp_parser_builtin_offsetof. We're looking for
6905
6906 postfix-expression . template [opt] id-expression
6907 postfix-expression . pseudo-destructor-name
6908 postfix-expression -> template [opt] id-expression
6909 postfix-expression -> pseudo-destructor-name
6910
6911 FOR_OFFSETOF is set if we're being called in that context. That sorta
6912 limits what of the above we'll actually accept, but nevermind.
6913 TOKEN_TYPE is the "." or "->" token, which will already have been
6914 removed from the stream. */
6915
6916 static tree
6917 cp_parser_postfix_dot_deref_expression (cp_parser *parser,
6918 enum cpp_ttype token_type,
6919 tree postfix_expression,
6920 bool for_offsetof, cp_id_kind *idk,
6921 location_t location)
6922 {
6923 tree name;
6924 bool dependent_p;
6925 bool pseudo_destructor_p;
6926 tree scope = NULL_TREE;
6927
6928 /* If this is a `->' operator, dereference the pointer. */
6929 if (token_type == CPP_DEREF)
6930 postfix_expression = build_x_arrow (location, postfix_expression,
6931 tf_warning_or_error);
6932 /* Check to see whether or not the expression is type-dependent. */
6933 dependent_p = type_dependent_expression_p (postfix_expression);
6934 /* The identifier following the `->' or `.' is not qualified. */
6935 parser->scope = NULL_TREE;
6936 parser->qualifying_scope = NULL_TREE;
6937 parser->object_scope = NULL_TREE;
6938 *idk = CP_ID_KIND_NONE;
6939
6940 /* Enter the scope corresponding to the type of the object
6941 given by the POSTFIX_EXPRESSION. */
6942 if (!dependent_p && TREE_TYPE (postfix_expression) != NULL_TREE)
6943 {
6944 scope = TREE_TYPE (postfix_expression);
6945 /* According to the standard, no expression should ever have
6946 reference type. Unfortunately, we do not currently match
6947 the standard in this respect in that our internal representation
6948 of an expression may have reference type even when the standard
6949 says it does not. Therefore, we have to manually obtain the
6950 underlying type here. */
6951 scope = non_reference (scope);
6952 /* The type of the POSTFIX_EXPRESSION must be complete. */
6953 if (scope == unknown_type_node)
6954 {
6955 error_at (location, "%qE does not have class type",
6956 postfix_expression);
6957 scope = NULL_TREE;
6958 }
6959 /* Unlike the object expression in other contexts, *this is not
6960 required to be of complete type for purposes of class member
6961 access (5.2.5) outside the member function body. */
6962 else if (postfix_expression != current_class_ref
6963 && !(processing_template_decl && scope == current_class_type))
6964 scope = complete_type_or_else (scope, NULL_TREE);
6965 /* Let the name lookup machinery know that we are processing a
6966 class member access expression. */
6967 parser->context->object_type = scope;
6968 /* If something went wrong, we want to be able to discern that case,
6969 as opposed to the case where there was no SCOPE due to the type
6970 of expression being dependent. */
6971 if (!scope)
6972 scope = error_mark_node;
6973 /* If the SCOPE was erroneous, make the various semantic analysis
6974 functions exit quickly -- and without issuing additional error
6975 messages. */
6976 if (scope == error_mark_node)
6977 postfix_expression = error_mark_node;
6978 }
6979
6980 /* Assume this expression is not a pseudo-destructor access. */
6981 pseudo_destructor_p = false;
6982
6983 /* If the SCOPE is a scalar type, then, if this is a valid program,
6984 we must be looking at a pseudo-destructor-name. If POSTFIX_EXPRESSION
6985 is type dependent, it can be pseudo-destructor-name or something else.
6986 Try to parse it as pseudo-destructor-name first. */
6987 if ((scope && SCALAR_TYPE_P (scope)) || dependent_p)
6988 {
6989 tree s;
6990 tree type;
6991
6992 cp_parser_parse_tentatively (parser);
6993 /* Parse the pseudo-destructor-name. */
6994 s = NULL_TREE;
6995 cp_parser_pseudo_destructor_name (parser, postfix_expression,
6996 &s, &type);
6997 if (dependent_p
6998 && (cp_parser_error_occurred (parser)
6999 || !SCALAR_TYPE_P (type)))
7000 cp_parser_abort_tentative_parse (parser);
7001 else if (cp_parser_parse_definitely (parser))
7002 {
7003 pseudo_destructor_p = true;
7004 postfix_expression
7005 = finish_pseudo_destructor_expr (postfix_expression,
7006 s, type, location);
7007 }
7008 }
7009
7010 if (!pseudo_destructor_p)
7011 {
7012 /* If the SCOPE is not a scalar type, we are looking at an
7013 ordinary class member access expression, rather than a
7014 pseudo-destructor-name. */
7015 bool template_p;
7016 cp_token *token = cp_lexer_peek_token (parser->lexer);
7017 /* Parse the id-expression. */
7018 name = (cp_parser_id_expression
7019 (parser,
7020 cp_parser_optional_template_keyword (parser),
7021 /*check_dependency_p=*/true,
7022 &template_p,
7023 /*declarator_p=*/false,
7024 /*optional_p=*/false));
7025 /* In general, build a SCOPE_REF if the member name is qualified.
7026 However, if the name was not dependent and has already been
7027 resolved; there is no need to build the SCOPE_REF. For example;
7028
7029 struct X { void f(); };
7030 template <typename T> void f(T* t) { t->X::f(); }
7031
7032 Even though "t" is dependent, "X::f" is not and has been resolved
7033 to a BASELINK; there is no need to include scope information. */
7034
7035 /* But we do need to remember that there was an explicit scope for
7036 virtual function calls. */
7037 if (parser->scope)
7038 *idk = CP_ID_KIND_QUALIFIED;
7039
7040 /* If the name is a template-id that names a type, we will get a
7041 TYPE_DECL here. That is invalid code. */
7042 if (TREE_CODE (name) == TYPE_DECL)
7043 {
7044 error_at (token->location, "invalid use of %qD", name);
7045 postfix_expression = error_mark_node;
7046 }
7047 else
7048 {
7049 if (name != error_mark_node && !BASELINK_P (name) && parser->scope)
7050 {
7051 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
7052 {
7053 error_at (token->location, "%<%D::%D%> is not a class member",
7054 parser->scope, name);
7055 postfix_expression = error_mark_node;
7056 }
7057 else
7058 name = build_qualified_name (/*type=*/NULL_TREE,
7059 parser->scope,
7060 name,
7061 template_p);
7062 parser->scope = NULL_TREE;
7063 parser->qualifying_scope = NULL_TREE;
7064 parser->object_scope = NULL_TREE;
7065 }
7066 if (parser->scope && name && BASELINK_P (name))
7067 adjust_result_of_qualified_name_lookup
7068 (name, parser->scope, scope);
7069 postfix_expression
7070 = finish_class_member_access_expr (postfix_expression, name,
7071 template_p,
7072 tf_warning_or_error);
7073 }
7074 }
7075
7076 /* We no longer need to look up names in the scope of the object on
7077 the left-hand side of the `.' or `->' operator. */
7078 parser->context->object_type = NULL_TREE;
7079
7080 /* Outside of offsetof, these operators may not appear in
7081 constant-expressions. */
7082 if (!for_offsetof
7083 && (cp_parser_non_integral_constant_expression
7084 (parser, token_type == CPP_DEREF ? NIC_ARROW : NIC_POINT)))
7085 postfix_expression = error_mark_node;
7086
7087 return postfix_expression;
7088 }
7089
7090 /* Parse a parenthesized expression-list.
7091
7092 expression-list:
7093 assignment-expression
7094 expression-list, assignment-expression
7095
7096 attribute-list:
7097 expression-list
7098 identifier
7099 identifier, expression-list
7100
7101 CAST_P is true if this expression is the target of a cast.
7102
7103 ALLOW_EXPANSION_P is true if this expression allows expansion of an
7104 argument pack.
7105
7106 Returns a vector of trees. Each element is a representation of an
7107 assignment-expression. NULL is returned if the ( and or ) are
7108 missing. An empty, but allocated, vector is returned on no
7109 expressions. The parentheses are eaten. IS_ATTRIBUTE_LIST is id_attr
7110 if we are parsing an attribute list for an attribute that wants a
7111 plain identifier argument, normal_attr for an attribute that wants
7112 an expression, or non_attr if we aren't parsing an attribute list. If
7113 NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P indicates whether or
7114 not all of the expressions in the list were constant. */
7115
7116 static vec<tree, va_gc> *
7117 cp_parser_parenthesized_expression_list (cp_parser* parser,
7118 int is_attribute_list,
7119 bool cast_p,
7120 bool allow_expansion_p,
7121 bool *non_constant_p)
7122 {
7123 vec<tree, va_gc> *expression_list;
7124 bool fold_expr_p = is_attribute_list != non_attr;
7125 tree identifier = NULL_TREE;
7126 bool saved_greater_than_is_operator_p;
7127
7128 /* Assume all the expressions will be constant. */
7129 if (non_constant_p)
7130 *non_constant_p = false;
7131
7132 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
7133 return NULL;
7134
7135 expression_list = make_tree_vector ();
7136
7137 /* Within a parenthesized expression, a `>' token is always
7138 the greater-than operator. */
7139 saved_greater_than_is_operator_p
7140 = parser->greater_than_is_operator_p;
7141 parser->greater_than_is_operator_p = true;
7142
7143 /* Consume expressions until there are no more. */
7144 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
7145 while (true)
7146 {
7147 tree expr;
7148
7149 /* At the beginning of attribute lists, check to see if the
7150 next token is an identifier. */
7151 if (is_attribute_list == id_attr
7152 && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
7153 {
7154 cp_token *token;
7155
7156 /* Consume the identifier. */
7157 token = cp_lexer_consume_token (parser->lexer);
7158 /* Save the identifier. */
7159 identifier = token->u.value;
7160 }
7161 else
7162 {
7163 bool expr_non_constant_p;
7164
7165 /* Parse the next assignment-expression. */
7166 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7167 {
7168 /* A braced-init-list. */
7169 cp_lexer_set_source_position (parser->lexer);
7170 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
7171 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
7172 if (non_constant_p && expr_non_constant_p)
7173 *non_constant_p = true;
7174 }
7175 else if (non_constant_p)
7176 {
7177 expr = (cp_parser_constant_expression
7178 (parser, /*allow_non_constant_p=*/true,
7179 &expr_non_constant_p));
7180 if (expr_non_constant_p)
7181 *non_constant_p = true;
7182 }
7183 else
7184 expr = cp_parser_assignment_expression (parser, /*pidk=*/NULL,
7185 cast_p);
7186
7187 if (fold_expr_p)
7188 expr = instantiate_non_dependent_expr (expr);
7189
7190 /* If we have an ellipsis, then this is an expression
7191 expansion. */
7192 if (allow_expansion_p
7193 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
7194 {
7195 /* Consume the `...'. */
7196 cp_lexer_consume_token (parser->lexer);
7197
7198 /* Build the argument pack. */
7199 expr = make_pack_expansion (expr);
7200 }
7201
7202 /* Add it to the list. We add error_mark_node
7203 expressions to the list, so that we can still tell if
7204 the correct form for a parenthesized expression-list
7205 is found. That gives better errors. */
7206 vec_safe_push (expression_list, expr);
7207
7208 if (expr == error_mark_node)
7209 goto skip_comma;
7210 }
7211
7212 /* After the first item, attribute lists look the same as
7213 expression lists. */
7214 is_attribute_list = non_attr;
7215
7216 get_comma:;
7217 /* If the next token isn't a `,', then we are done. */
7218 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
7219 break;
7220
7221 /* Otherwise, consume the `,' and keep going. */
7222 cp_lexer_consume_token (parser->lexer);
7223 }
7224
7225 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
7226 {
7227 int ending;
7228
7229 skip_comma:;
7230 /* We try and resync to an unnested comma, as that will give the
7231 user better diagnostics. */
7232 ending = cp_parser_skip_to_closing_parenthesis (parser,
7233 /*recovering=*/true,
7234 /*or_comma=*/true,
7235 /*consume_paren=*/true);
7236 if (ending < 0)
7237 goto get_comma;
7238 if (!ending)
7239 {
7240 parser->greater_than_is_operator_p
7241 = saved_greater_than_is_operator_p;
7242 return NULL;
7243 }
7244 }
7245
7246 parser->greater_than_is_operator_p
7247 = saved_greater_than_is_operator_p;
7248
7249 if (identifier)
7250 vec_safe_insert (expression_list, 0, identifier);
7251
7252 return expression_list;
7253 }
7254
7255 /* Parse a pseudo-destructor-name.
7256
7257 pseudo-destructor-name:
7258 :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
7259 :: [opt] nested-name-specifier template template-id :: ~ type-name
7260 :: [opt] nested-name-specifier [opt] ~ type-name
7261
7262 If either of the first two productions is used, sets *SCOPE to the
7263 TYPE specified before the final `::'. Otherwise, *SCOPE is set to
7264 NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name,
7265 or ERROR_MARK_NODE if the parse fails. */
7266
7267 static void
7268 cp_parser_pseudo_destructor_name (cp_parser* parser,
7269 tree object,
7270 tree* scope,
7271 tree* type)
7272 {
7273 bool nested_name_specifier_p;
7274
7275 /* Handle ~auto. */
7276 if (cp_lexer_next_token_is (parser->lexer, CPP_COMPL)
7277 && cp_lexer_nth_token_is_keyword (parser->lexer, 2, RID_AUTO)
7278 && !type_dependent_expression_p (object))
7279 {
7280 if (cxx_dialect < cxx14)
7281 pedwarn (input_location, 0,
7282 "%<~auto%> only available with "
7283 "-std=c++14 or -std=gnu++14");
7284 cp_lexer_consume_token (parser->lexer);
7285 cp_lexer_consume_token (parser->lexer);
7286 *scope = NULL_TREE;
7287 *type = TREE_TYPE (object);
7288 return;
7289 }
7290
7291 /* Assume that things will not work out. */
7292 *type = error_mark_node;
7293
7294 /* Look for the optional `::' operator. */
7295 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
7296 /* Look for the optional nested-name-specifier. */
7297 nested_name_specifier_p
7298 = (cp_parser_nested_name_specifier_opt (parser,
7299 /*typename_keyword_p=*/false,
7300 /*check_dependency_p=*/true,
7301 /*type_p=*/false,
7302 /*is_declaration=*/false)
7303 != NULL_TREE);
7304 /* Now, if we saw a nested-name-specifier, we might be doing the
7305 second production. */
7306 if (nested_name_specifier_p
7307 && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
7308 {
7309 /* Consume the `template' keyword. */
7310 cp_lexer_consume_token (parser->lexer);
7311 /* Parse the template-id. */
7312 cp_parser_template_id (parser,
7313 /*template_keyword_p=*/true,
7314 /*check_dependency_p=*/false,
7315 class_type,
7316 /*is_declaration=*/true);
7317 /* Look for the `::' token. */
7318 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
7319 }
7320 /* If the next token is not a `~', then there might be some
7321 additional qualification. */
7322 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
7323 {
7324 /* At this point, we're looking for "type-name :: ~". The type-name
7325 must not be a class-name, since this is a pseudo-destructor. So,
7326 it must be either an enum-name, or a typedef-name -- both of which
7327 are just identifiers. So, we peek ahead to check that the "::"
7328 and "~" tokens are present; if they are not, then we can avoid
7329 calling type_name. */
7330 if (cp_lexer_peek_token (parser->lexer)->type != CPP_NAME
7331 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE
7332 || cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_COMPL)
7333 {
7334 cp_parser_error (parser, "non-scalar type");
7335 return;
7336 }
7337
7338 /* Look for the type-name. */
7339 *scope = TREE_TYPE (cp_parser_nonclass_name (parser));
7340 if (*scope == error_mark_node)
7341 return;
7342
7343 /* Look for the `::' token. */
7344 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
7345 }
7346 else
7347 *scope = NULL_TREE;
7348
7349 /* Look for the `~'. */
7350 cp_parser_require (parser, CPP_COMPL, RT_COMPL);
7351
7352 /* Once we see the ~, this has to be a pseudo-destructor. */
7353 if (!processing_template_decl && !cp_parser_error_occurred (parser))
7354 cp_parser_commit_to_topmost_tentative_parse (parser);
7355
7356 /* Look for the type-name again. We are not responsible for
7357 checking that it matches the first type-name. */
7358 *type = TREE_TYPE (cp_parser_nonclass_name (parser));
7359 }
7360
7361 /* Parse a unary-expression.
7362
7363 unary-expression:
7364 postfix-expression
7365 ++ cast-expression
7366 -- cast-expression
7367 unary-operator cast-expression
7368 sizeof unary-expression
7369 sizeof ( type-id )
7370 alignof ( type-id ) [C++0x]
7371 new-expression
7372 delete-expression
7373
7374 GNU Extensions:
7375
7376 unary-expression:
7377 __extension__ cast-expression
7378 __alignof__ unary-expression
7379 __alignof__ ( type-id )
7380 alignof unary-expression [C++0x]
7381 __real__ cast-expression
7382 __imag__ cast-expression
7383 && identifier
7384 sizeof ( type-id ) { initializer-list , [opt] }
7385 alignof ( type-id ) { initializer-list , [opt] } [C++0x]
7386 __alignof__ ( type-id ) { initializer-list , [opt] }
7387
7388 ADDRESS_P is true iff the unary-expression is appearing as the
7389 operand of the `&' operator. CAST_P is true if this expression is
7390 the target of a cast.
7391
7392 Returns a representation of the expression. */
7393
7394 static tree
7395 cp_parser_unary_expression (cp_parser *parser, cp_id_kind * pidk,
7396 bool address_p, bool cast_p, bool decltype_p)
7397 {
7398 cp_token *token;
7399 enum tree_code unary_operator;
7400
7401 /* Peek at the next token. */
7402 token = cp_lexer_peek_token (parser->lexer);
7403 /* Some keywords give away the kind of expression. */
7404 if (token->type == CPP_KEYWORD)
7405 {
7406 enum rid keyword = token->keyword;
7407
7408 switch (keyword)
7409 {
7410 case RID_ALIGNOF:
7411 case RID_SIZEOF:
7412 {
7413 tree operand, ret;
7414 enum tree_code op;
7415 location_t first_loc;
7416
7417 op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
7418 /* Consume the token. */
7419 cp_lexer_consume_token (parser->lexer);
7420 first_loc = cp_lexer_peek_token (parser->lexer)->location;
7421 /* Parse the operand. */
7422 operand = cp_parser_sizeof_operand (parser, keyword);
7423
7424 if (TYPE_P (operand))
7425 ret = cxx_sizeof_or_alignof_type (operand, op, true);
7426 else
7427 {
7428 /* ISO C++ defines alignof only with types, not with
7429 expressions. So pedwarn if alignof is used with a non-
7430 type expression. However, __alignof__ is ok. */
7431 if (!strcmp (IDENTIFIER_POINTER (token->u.value), "alignof"))
7432 pedwarn (token->location, OPT_Wpedantic,
7433 "ISO C++ does not allow %<alignof%> "
7434 "with a non-type");
7435
7436 ret = cxx_sizeof_or_alignof_expr (operand, op, true);
7437 }
7438 /* For SIZEOF_EXPR, just issue diagnostics, but keep
7439 SIZEOF_EXPR with the original operand. */
7440 if (op == SIZEOF_EXPR && ret != error_mark_node)
7441 {
7442 if (TREE_CODE (ret) != SIZEOF_EXPR || TYPE_P (operand))
7443 {
7444 if (!processing_template_decl && TYPE_P (operand))
7445 {
7446 ret = build_min (SIZEOF_EXPR, size_type_node,
7447 build1 (NOP_EXPR, operand,
7448 error_mark_node));
7449 SIZEOF_EXPR_TYPE_P (ret) = 1;
7450 }
7451 else
7452 ret = build_min (SIZEOF_EXPR, size_type_node, operand);
7453 TREE_SIDE_EFFECTS (ret) = 0;
7454 TREE_READONLY (ret) = 1;
7455 }
7456 SET_EXPR_LOCATION (ret, first_loc);
7457 }
7458 return ret;
7459 }
7460
7461 case RID_NEW:
7462 return cp_parser_new_expression (parser);
7463
7464 case RID_DELETE:
7465 return cp_parser_delete_expression (parser);
7466
7467 case RID_EXTENSION:
7468 {
7469 /* The saved value of the PEDANTIC flag. */
7470 int saved_pedantic;
7471 tree expr;
7472
7473 /* Save away the PEDANTIC flag. */
7474 cp_parser_extension_opt (parser, &saved_pedantic);
7475 /* Parse the cast-expression. */
7476 expr = cp_parser_simple_cast_expression (parser);
7477 /* Restore the PEDANTIC flag. */
7478 pedantic = saved_pedantic;
7479
7480 return expr;
7481 }
7482
7483 case RID_REALPART:
7484 case RID_IMAGPART:
7485 {
7486 tree expression;
7487
7488 /* Consume the `__real__' or `__imag__' token. */
7489 cp_lexer_consume_token (parser->lexer);
7490 /* Parse the cast-expression. */
7491 expression = cp_parser_simple_cast_expression (parser);
7492 /* Create the complete representation. */
7493 return build_x_unary_op (token->location,
7494 (keyword == RID_REALPART
7495 ? REALPART_EXPR : IMAGPART_EXPR),
7496 expression,
7497 tf_warning_or_error);
7498 }
7499 break;
7500
7501 case RID_TRANSACTION_ATOMIC:
7502 case RID_TRANSACTION_RELAXED:
7503 return cp_parser_transaction_expression (parser, keyword);
7504
7505 case RID_NOEXCEPT:
7506 {
7507 tree expr;
7508 const char *saved_message;
7509 bool saved_integral_constant_expression_p;
7510 bool saved_non_integral_constant_expression_p;
7511 bool saved_greater_than_is_operator_p;
7512
7513 cp_lexer_consume_token (parser->lexer);
7514 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
7515
7516 saved_message = parser->type_definition_forbidden_message;
7517 parser->type_definition_forbidden_message
7518 = G_("types may not be defined in %<noexcept%> expressions");
7519
7520 saved_integral_constant_expression_p
7521 = parser->integral_constant_expression_p;
7522 saved_non_integral_constant_expression_p
7523 = parser->non_integral_constant_expression_p;
7524 parser->integral_constant_expression_p = false;
7525
7526 saved_greater_than_is_operator_p
7527 = parser->greater_than_is_operator_p;
7528 parser->greater_than_is_operator_p = true;
7529
7530 ++cp_unevaluated_operand;
7531 ++c_inhibit_evaluation_warnings;
7532 ++cp_noexcept_operand;
7533 expr = cp_parser_expression (parser);
7534 --cp_noexcept_operand;
7535 --c_inhibit_evaluation_warnings;
7536 --cp_unevaluated_operand;
7537
7538 parser->greater_than_is_operator_p
7539 = saved_greater_than_is_operator_p;
7540
7541 parser->integral_constant_expression_p
7542 = saved_integral_constant_expression_p;
7543 parser->non_integral_constant_expression_p
7544 = saved_non_integral_constant_expression_p;
7545
7546 parser->type_definition_forbidden_message = saved_message;
7547
7548 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
7549 return finish_noexcept_expr (expr, tf_warning_or_error);
7550 }
7551
7552 default:
7553 break;
7554 }
7555 }
7556
7557 /* Look for the `:: new' and `:: delete', which also signal the
7558 beginning of a new-expression, or delete-expression,
7559 respectively. If the next token is `::', then it might be one of
7560 these. */
7561 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
7562 {
7563 enum rid keyword;
7564
7565 /* See if the token after the `::' is one of the keywords in
7566 which we're interested. */
7567 keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
7568 /* If it's `new', we have a new-expression. */
7569 if (keyword == RID_NEW)
7570 return cp_parser_new_expression (parser);
7571 /* Similarly, for `delete'. */
7572 else if (keyword == RID_DELETE)
7573 return cp_parser_delete_expression (parser);
7574 }
7575
7576 /* Look for a unary operator. */
7577 unary_operator = cp_parser_unary_operator (token);
7578 /* The `++' and `--' operators can be handled similarly, even though
7579 they are not technically unary-operators in the grammar. */
7580 if (unary_operator == ERROR_MARK)
7581 {
7582 if (token->type == CPP_PLUS_PLUS)
7583 unary_operator = PREINCREMENT_EXPR;
7584 else if (token->type == CPP_MINUS_MINUS)
7585 unary_operator = PREDECREMENT_EXPR;
7586 /* Handle the GNU address-of-label extension. */
7587 else if (cp_parser_allow_gnu_extensions_p (parser)
7588 && token->type == CPP_AND_AND)
7589 {
7590 tree identifier;
7591 tree expression;
7592 location_t loc = token->location;
7593
7594 /* Consume the '&&' token. */
7595 cp_lexer_consume_token (parser->lexer);
7596 /* Look for the identifier. */
7597 identifier = cp_parser_identifier (parser);
7598 /* Create an expression representing the address. */
7599 expression = finish_label_address_expr (identifier, loc);
7600 if (cp_parser_non_integral_constant_expression (parser,
7601 NIC_ADDR_LABEL))
7602 expression = error_mark_node;
7603 return expression;
7604 }
7605 }
7606 if (unary_operator != ERROR_MARK)
7607 {
7608 tree cast_expression;
7609 tree expression = error_mark_node;
7610 non_integral_constant non_constant_p = NIC_NONE;
7611 location_t loc = token->location;
7612 tsubst_flags_t complain = complain_flags (decltype_p);
7613
7614 /* Consume the operator token. */
7615 token = cp_lexer_consume_token (parser->lexer);
7616 enum cpp_ttype op_ttype = cp_lexer_peek_token (parser->lexer)->type;
7617
7618 /* Parse the cast-expression. */
7619 cast_expression
7620 = cp_parser_cast_expression (parser,
7621 unary_operator == ADDR_EXPR,
7622 /*cast_p=*/false,
7623 /*decltype*/false,
7624 pidk);
7625 /* Now, build an appropriate representation. */
7626 switch (unary_operator)
7627 {
7628 case INDIRECT_REF:
7629 non_constant_p = NIC_STAR;
7630 expression = build_x_indirect_ref (loc, cast_expression,
7631 RO_UNARY_STAR,
7632 complain);
7633 break;
7634
7635 case ADDR_EXPR:
7636 non_constant_p = NIC_ADDR;
7637 /* Fall through. */
7638 case BIT_NOT_EXPR:
7639 expression = build_x_unary_op (loc, unary_operator,
7640 cast_expression,
7641 complain);
7642 break;
7643
7644 case PREINCREMENT_EXPR:
7645 case PREDECREMENT_EXPR:
7646 non_constant_p = unary_operator == PREINCREMENT_EXPR
7647 ? NIC_PREINCREMENT : NIC_PREDECREMENT;
7648 /* Fall through. */
7649 case NEGATE_EXPR:
7650 /* Immediately fold negation of a constant, unless the constant is 0
7651 (since -0 == 0) or it would overflow. */
7652 if (unary_operator == NEGATE_EXPR && op_ttype == CPP_NUMBER
7653 && CONSTANT_CLASS_P (cast_expression)
7654 && !integer_zerop (cast_expression)
7655 && !TREE_OVERFLOW (cast_expression))
7656 {
7657 tree folded = fold_build1 (unary_operator,
7658 TREE_TYPE (cast_expression),
7659 cast_expression);
7660 if (CONSTANT_CLASS_P (folded) && !TREE_OVERFLOW (folded))
7661 {
7662 expression = folded;
7663 break;
7664 }
7665 }
7666 /* Fall through. */
7667 case UNARY_PLUS_EXPR:
7668 case TRUTH_NOT_EXPR:
7669 expression = finish_unary_op_expr (loc, unary_operator,
7670 cast_expression, complain);
7671 break;
7672
7673 default:
7674 gcc_unreachable ();
7675 }
7676
7677 if (non_constant_p != NIC_NONE
7678 && cp_parser_non_integral_constant_expression (parser,
7679 non_constant_p))
7680 expression = error_mark_node;
7681
7682 return expression;
7683 }
7684
7685 return cp_parser_postfix_expression (parser, address_p, cast_p,
7686 /*member_access_only_p=*/false,
7687 decltype_p,
7688 pidk);
7689 }
7690
7691 /* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a
7692 unary-operator, the corresponding tree code is returned. */
7693
7694 static enum tree_code
7695 cp_parser_unary_operator (cp_token* token)
7696 {
7697 switch (token->type)
7698 {
7699 case CPP_MULT:
7700 return INDIRECT_REF;
7701
7702 case CPP_AND:
7703 return ADDR_EXPR;
7704
7705 case CPP_PLUS:
7706 return UNARY_PLUS_EXPR;
7707
7708 case CPP_MINUS:
7709 return NEGATE_EXPR;
7710
7711 case CPP_NOT:
7712 return TRUTH_NOT_EXPR;
7713
7714 case CPP_COMPL:
7715 return BIT_NOT_EXPR;
7716
7717 default:
7718 return ERROR_MARK;
7719 }
7720 }
7721
7722 /* Parse a new-expression.
7723
7724 new-expression:
7725 :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
7726 :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
7727
7728 Returns a representation of the expression. */
7729
7730 static tree
7731 cp_parser_new_expression (cp_parser* parser)
7732 {
7733 bool global_scope_p;
7734 vec<tree, va_gc> *placement;
7735 tree type;
7736 vec<tree, va_gc> *initializer;
7737 tree nelts = NULL_TREE;
7738 tree ret;
7739
7740 /* Look for the optional `::' operator. */
7741 global_scope_p
7742 = (cp_parser_global_scope_opt (parser,
7743 /*current_scope_valid_p=*/false)
7744 != NULL_TREE);
7745 /* Look for the `new' operator. */
7746 cp_parser_require_keyword (parser, RID_NEW, RT_NEW);
7747 /* There's no easy way to tell a new-placement from the
7748 `( type-id )' construct. */
7749 cp_parser_parse_tentatively (parser);
7750 /* Look for a new-placement. */
7751 placement = cp_parser_new_placement (parser);
7752 /* If that didn't work out, there's no new-placement. */
7753 if (!cp_parser_parse_definitely (parser))
7754 {
7755 if (placement != NULL)
7756 release_tree_vector (placement);
7757 placement = NULL;
7758 }
7759
7760 /* If the next token is a `(', then we have a parenthesized
7761 type-id. */
7762 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
7763 {
7764 cp_token *token;
7765 const char *saved_message = parser->type_definition_forbidden_message;
7766
7767 /* Consume the `('. */
7768 cp_lexer_consume_token (parser->lexer);
7769
7770 /* Parse the type-id. */
7771 parser->type_definition_forbidden_message
7772 = G_("types may not be defined in a new-expression");
7773 type = cp_parser_type_id (parser);
7774 parser->type_definition_forbidden_message = saved_message;
7775
7776 /* Look for the closing `)'. */
7777 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
7778 token = cp_lexer_peek_token (parser->lexer);
7779 /* There should not be a direct-new-declarator in this production,
7780 but GCC used to allowed this, so we check and emit a sensible error
7781 message for this case. */
7782 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
7783 {
7784 error_at (token->location,
7785 "array bound forbidden after parenthesized type-id");
7786 inform (token->location,
7787 "try removing the parentheses around the type-id");
7788 cp_parser_direct_new_declarator (parser);
7789 }
7790 }
7791 /* Otherwise, there must be a new-type-id. */
7792 else
7793 type = cp_parser_new_type_id (parser, &nelts);
7794
7795 /* If the next token is a `(' or '{', then we have a new-initializer. */
7796 cp_token *token = cp_lexer_peek_token (parser->lexer);
7797 if (token->type == CPP_OPEN_PAREN
7798 || token->type == CPP_OPEN_BRACE)
7799 initializer = cp_parser_new_initializer (parser);
7800 else
7801 initializer = NULL;
7802
7803 /* A new-expression may not appear in an integral constant
7804 expression. */
7805 if (cp_parser_non_integral_constant_expression (parser, NIC_NEW))
7806 ret = error_mark_node;
7807 /* 5.3.4/2: "If the auto type-specifier appears in the type-specifier-seq
7808 of a new-type-id or type-id of a new-expression, the new-expression shall
7809 contain a new-initializer of the form ( assignment-expression )".
7810 Additionally, consistently with the spirit of DR 1467, we want to accept
7811 'new auto { 2 }' too. */
7812 else if (type_uses_auto (type)
7813 && (vec_safe_length (initializer) != 1
7814 || (BRACE_ENCLOSED_INITIALIZER_P ((*initializer)[0])
7815 && CONSTRUCTOR_NELTS ((*initializer)[0]) != 1)))
7816 {
7817 error_at (token->location,
7818 "initialization of new-expression for type %<auto%> "
7819 "requires exactly one element");
7820 ret = error_mark_node;
7821 }
7822 else
7823 {
7824 /* Create a representation of the new-expression. */
7825 ret = build_new (&placement, type, nelts, &initializer, global_scope_p,
7826 tf_warning_or_error);
7827 }
7828
7829 if (placement != NULL)
7830 release_tree_vector (placement);
7831 if (initializer != NULL)
7832 release_tree_vector (initializer);
7833
7834 return ret;
7835 }
7836
7837 /* Parse a new-placement.
7838
7839 new-placement:
7840 ( expression-list )
7841
7842 Returns the same representation as for an expression-list. */
7843
7844 static vec<tree, va_gc> *
7845 cp_parser_new_placement (cp_parser* parser)
7846 {
7847 vec<tree, va_gc> *expression_list;
7848
7849 /* Parse the expression-list. */
7850 expression_list = (cp_parser_parenthesized_expression_list
7851 (parser, non_attr, /*cast_p=*/false,
7852 /*allow_expansion_p=*/true,
7853 /*non_constant_p=*/NULL));
7854
7855 if (expression_list && expression_list->is_empty ())
7856 error ("expected expression-list or type-id");
7857
7858 return expression_list;
7859 }
7860
7861 /* Parse a new-type-id.
7862
7863 new-type-id:
7864 type-specifier-seq new-declarator [opt]
7865
7866 Returns the TYPE allocated. If the new-type-id indicates an array
7867 type, *NELTS is set to the number of elements in the last array
7868 bound; the TYPE will not include the last array bound. */
7869
7870 static tree
7871 cp_parser_new_type_id (cp_parser* parser, tree *nelts)
7872 {
7873 cp_decl_specifier_seq type_specifier_seq;
7874 cp_declarator *new_declarator;
7875 cp_declarator *declarator;
7876 cp_declarator *outer_declarator;
7877 const char *saved_message;
7878
7879 /* The type-specifier sequence must not contain type definitions.
7880 (It cannot contain declarations of new types either, but if they
7881 are not definitions we will catch that because they are not
7882 complete.) */
7883 saved_message = parser->type_definition_forbidden_message;
7884 parser->type_definition_forbidden_message
7885 = G_("types may not be defined in a new-type-id");
7886 /* Parse the type-specifier-seq. */
7887 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
7888 /*is_trailing_return=*/false,
7889 &type_specifier_seq);
7890 /* Restore the old message. */
7891 parser->type_definition_forbidden_message = saved_message;
7892
7893 if (type_specifier_seq.type == error_mark_node)
7894 return error_mark_node;
7895
7896 /* Parse the new-declarator. */
7897 new_declarator = cp_parser_new_declarator_opt (parser);
7898
7899 /* Determine the number of elements in the last array dimension, if
7900 any. */
7901 *nelts = NULL_TREE;
7902 /* Skip down to the last array dimension. */
7903 declarator = new_declarator;
7904 outer_declarator = NULL;
7905 while (declarator && (declarator->kind == cdk_pointer
7906 || declarator->kind == cdk_ptrmem))
7907 {
7908 outer_declarator = declarator;
7909 declarator = declarator->declarator;
7910 }
7911 while (declarator
7912 && declarator->kind == cdk_array
7913 && declarator->declarator
7914 && declarator->declarator->kind == cdk_array)
7915 {
7916 outer_declarator = declarator;
7917 declarator = declarator->declarator;
7918 }
7919
7920 if (declarator && declarator->kind == cdk_array)
7921 {
7922 *nelts = declarator->u.array.bounds;
7923 if (*nelts == error_mark_node)
7924 *nelts = integer_one_node;
7925
7926 if (outer_declarator)
7927 outer_declarator->declarator = declarator->declarator;
7928 else
7929 new_declarator = NULL;
7930 }
7931
7932 return groktypename (&type_specifier_seq, new_declarator, false);
7933 }
7934
7935 /* Parse an (optional) new-declarator.
7936
7937 new-declarator:
7938 ptr-operator new-declarator [opt]
7939 direct-new-declarator
7940
7941 Returns the declarator. */
7942
7943 static cp_declarator *
7944 cp_parser_new_declarator_opt (cp_parser* parser)
7945 {
7946 enum tree_code code;
7947 tree type, std_attributes = NULL_TREE;
7948 cp_cv_quals cv_quals;
7949
7950 /* We don't know if there's a ptr-operator next, or not. */
7951 cp_parser_parse_tentatively (parser);
7952 /* Look for a ptr-operator. */
7953 code = cp_parser_ptr_operator (parser, &type, &cv_quals, &std_attributes);
7954 /* If that worked, look for more new-declarators. */
7955 if (cp_parser_parse_definitely (parser))
7956 {
7957 cp_declarator *declarator;
7958
7959 /* Parse another optional declarator. */
7960 declarator = cp_parser_new_declarator_opt (parser);
7961
7962 declarator = cp_parser_make_indirect_declarator
7963 (code, type, cv_quals, declarator, std_attributes);
7964
7965 return declarator;
7966 }
7967
7968 /* If the next token is a `[', there is a direct-new-declarator. */
7969 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
7970 return cp_parser_direct_new_declarator (parser);
7971
7972 return NULL;
7973 }
7974
7975 /* Parse a direct-new-declarator.
7976
7977 direct-new-declarator:
7978 [ expression ]
7979 direct-new-declarator [constant-expression]
7980
7981 */
7982
7983 static cp_declarator *
7984 cp_parser_direct_new_declarator (cp_parser* parser)
7985 {
7986 cp_declarator *declarator = NULL;
7987
7988 while (true)
7989 {
7990 tree expression;
7991 cp_token *token;
7992
7993 /* Look for the opening `['. */
7994 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
7995
7996 token = cp_lexer_peek_token (parser->lexer);
7997 expression = cp_parser_expression (parser);
7998 /* The standard requires that the expression have integral
7999 type. DR 74 adds enumeration types. We believe that the
8000 real intent is that these expressions be handled like the
8001 expression in a `switch' condition, which also allows
8002 classes with a single conversion to integral or
8003 enumeration type. */
8004 if (!processing_template_decl)
8005 {
8006 expression
8007 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
8008 expression,
8009 /*complain=*/true);
8010 if (!expression)
8011 {
8012 error_at (token->location,
8013 "expression in new-declarator must have integral "
8014 "or enumeration type");
8015 expression = error_mark_node;
8016 }
8017 }
8018
8019 /* Look for the closing `]'. */
8020 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
8021
8022 /* Add this bound to the declarator. */
8023 declarator = make_array_declarator (declarator, expression);
8024
8025 /* If the next token is not a `[', then there are no more
8026 bounds. */
8027 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
8028 break;
8029 }
8030
8031 return declarator;
8032 }
8033
8034 /* Parse a new-initializer.
8035
8036 new-initializer:
8037 ( expression-list [opt] )
8038 braced-init-list
8039
8040 Returns a representation of the expression-list. */
8041
8042 static vec<tree, va_gc> *
8043 cp_parser_new_initializer (cp_parser* parser)
8044 {
8045 vec<tree, va_gc> *expression_list;
8046
8047 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
8048 {
8049 tree t;
8050 bool expr_non_constant_p;
8051 cp_lexer_set_source_position (parser->lexer);
8052 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
8053 t = cp_parser_braced_list (parser, &expr_non_constant_p);
8054 CONSTRUCTOR_IS_DIRECT_INIT (t) = 1;
8055 expression_list = make_tree_vector_single (t);
8056 }
8057 else
8058 expression_list = (cp_parser_parenthesized_expression_list
8059 (parser, non_attr, /*cast_p=*/false,
8060 /*allow_expansion_p=*/true,
8061 /*non_constant_p=*/NULL));
8062
8063 return expression_list;
8064 }
8065
8066 /* Parse a delete-expression.
8067
8068 delete-expression:
8069 :: [opt] delete cast-expression
8070 :: [opt] delete [ ] cast-expression
8071
8072 Returns a representation of the expression. */
8073
8074 static tree
8075 cp_parser_delete_expression (cp_parser* parser)
8076 {
8077 bool global_scope_p;
8078 bool array_p;
8079 tree expression;
8080
8081 /* Look for the optional `::' operator. */
8082 global_scope_p
8083 = (cp_parser_global_scope_opt (parser,
8084 /*current_scope_valid_p=*/false)
8085 != NULL_TREE);
8086 /* Look for the `delete' keyword. */
8087 cp_parser_require_keyword (parser, RID_DELETE, RT_DELETE);
8088 /* See if the array syntax is in use. */
8089 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
8090 {
8091 /* Consume the `[' token. */
8092 cp_lexer_consume_token (parser->lexer);
8093 /* Look for the `]' token. */
8094 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
8095 /* Remember that this is the `[]' construct. */
8096 array_p = true;
8097 }
8098 else
8099 array_p = false;
8100
8101 /* Parse the cast-expression. */
8102 expression = cp_parser_simple_cast_expression (parser);
8103
8104 /* A delete-expression may not appear in an integral constant
8105 expression. */
8106 if (cp_parser_non_integral_constant_expression (parser, NIC_DEL))
8107 return error_mark_node;
8108
8109 return delete_sanity (expression, NULL_TREE, array_p, global_scope_p,
8110 tf_warning_or_error);
8111 }
8112
8113 /* Returns 1 if TOKEN may start a cast-expression and isn't '++', '--',
8114 neither '[' in C++11; -1 if TOKEN is '++', '--', or '[' in C++11;
8115 0 otherwise. */
8116
8117 static int
8118 cp_parser_tokens_start_cast_expression (cp_parser *parser)
8119 {
8120 cp_token *token = cp_lexer_peek_token (parser->lexer);
8121 switch (token->type)
8122 {
8123 case CPP_COMMA:
8124 case CPP_SEMICOLON:
8125 case CPP_QUERY:
8126 case CPP_COLON:
8127 case CPP_CLOSE_SQUARE:
8128 case CPP_CLOSE_PAREN:
8129 case CPP_CLOSE_BRACE:
8130 case CPP_OPEN_BRACE:
8131 case CPP_DOT:
8132 case CPP_DOT_STAR:
8133 case CPP_DEREF:
8134 case CPP_DEREF_STAR:
8135 case CPP_DIV:
8136 case CPP_MOD:
8137 case CPP_LSHIFT:
8138 case CPP_RSHIFT:
8139 case CPP_LESS:
8140 case CPP_GREATER:
8141 case CPP_LESS_EQ:
8142 case CPP_GREATER_EQ:
8143 case CPP_EQ_EQ:
8144 case CPP_NOT_EQ:
8145 case CPP_EQ:
8146 case CPP_MULT_EQ:
8147 case CPP_DIV_EQ:
8148 case CPP_MOD_EQ:
8149 case CPP_PLUS_EQ:
8150 case CPP_MINUS_EQ:
8151 case CPP_RSHIFT_EQ:
8152 case CPP_LSHIFT_EQ:
8153 case CPP_AND_EQ:
8154 case CPP_XOR_EQ:
8155 case CPP_OR_EQ:
8156 case CPP_XOR:
8157 case CPP_OR:
8158 case CPP_OR_OR:
8159 case CPP_EOF:
8160 case CPP_ELLIPSIS:
8161 return 0;
8162
8163 case CPP_OPEN_PAREN:
8164 /* In ((type ()) () the last () isn't a valid cast-expression,
8165 so the whole must be parsed as postfix-expression. */
8166 return cp_lexer_peek_nth_token (parser->lexer, 2)->type
8167 != CPP_CLOSE_PAREN;
8168
8169 case CPP_OPEN_SQUARE:
8170 /* '[' may start a primary-expression in obj-c++ and in C++11,
8171 as a lambda-expression, eg, '(void)[]{}'. */
8172 if (cxx_dialect >= cxx11)
8173 return -1;
8174 return c_dialect_objc ();
8175
8176 case CPP_PLUS_PLUS:
8177 case CPP_MINUS_MINUS:
8178 /* '++' and '--' may or may not start a cast-expression:
8179
8180 struct T { void operator++(int); };
8181 void f() { (T())++; }
8182
8183 vs
8184
8185 int a;
8186 (int)++a; */
8187 return -1;
8188
8189 default:
8190 return 1;
8191 }
8192 }
8193
8194 /* Parse a cast-expression.
8195
8196 cast-expression:
8197 unary-expression
8198 ( type-id ) cast-expression
8199
8200 ADDRESS_P is true iff the unary-expression is appearing as the
8201 operand of the `&' operator. CAST_P is true if this expression is
8202 the target of a cast.
8203
8204 Returns a representation of the expression. */
8205
8206 static tree
8207 cp_parser_cast_expression (cp_parser *parser, bool address_p, bool cast_p,
8208 bool decltype_p, cp_id_kind * pidk)
8209 {
8210 /* If it's a `(', then we might be looking at a cast. */
8211 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
8212 {
8213 tree type = NULL_TREE;
8214 tree expr = NULL_TREE;
8215 int cast_expression = 0;
8216 const char *saved_message;
8217
8218 /* There's no way to know yet whether or not this is a cast.
8219 For example, `(int (3))' is a unary-expression, while `(int)
8220 3' is a cast. So, we resort to parsing tentatively. */
8221 cp_parser_parse_tentatively (parser);
8222 /* Types may not be defined in a cast. */
8223 saved_message = parser->type_definition_forbidden_message;
8224 parser->type_definition_forbidden_message
8225 = G_("types may not be defined in casts");
8226 /* Consume the `('. */
8227 cp_lexer_consume_token (parser->lexer);
8228 /* A very tricky bit is that `(struct S) { 3 }' is a
8229 compound-literal (which we permit in C++ as an extension).
8230 But, that construct is not a cast-expression -- it is a
8231 postfix-expression. (The reason is that `(struct S) { 3 }.i'
8232 is legal; if the compound-literal were a cast-expression,
8233 you'd need an extra set of parentheses.) But, if we parse
8234 the type-id, and it happens to be a class-specifier, then we
8235 will commit to the parse at that point, because we cannot
8236 undo the action that is done when creating a new class. So,
8237 then we cannot back up and do a postfix-expression.
8238
8239 Another tricky case is the following (c++/29234):
8240
8241 struct S { void operator () (); };
8242
8243 void foo ()
8244 {
8245 ( S()() );
8246 }
8247
8248 As a type-id we parse the parenthesized S()() as a function
8249 returning a function, groktypename complains and we cannot
8250 back up in this case either.
8251
8252 Therefore, we scan ahead to the closing `)', and check to see
8253 if the tokens after the `)' can start a cast-expression. Otherwise
8254 we are dealing with an unary-expression, a postfix-expression
8255 or something else.
8256
8257 Yet another tricky case, in C++11, is the following (c++/54891):
8258
8259 (void)[]{};
8260
8261 The issue is that usually, besides the case of lambda-expressions,
8262 the parenthesized type-id cannot be followed by '[', and, eg, we
8263 want to parse '(C ())[2];' in parse/pr26997.C as unary-expression.
8264 Thus, if cp_parser_tokens_start_cast_expression returns -1, below
8265 we don't commit, we try a cast-expression, then an unary-expression.
8266
8267 Save tokens so that we can put them back. */
8268 cp_lexer_save_tokens (parser->lexer);
8269
8270 /* We may be looking at a cast-expression. */
8271 if (cp_parser_skip_to_closing_parenthesis (parser, false, false,
8272 /*consume_paren=*/true))
8273 cast_expression
8274 = cp_parser_tokens_start_cast_expression (parser);
8275
8276 /* Roll back the tokens we skipped. */
8277 cp_lexer_rollback_tokens (parser->lexer);
8278 /* If we aren't looking at a cast-expression, simulate an error so
8279 that the call to cp_parser_error_occurred below returns true. */
8280 if (!cast_expression)
8281 cp_parser_simulate_error (parser);
8282 else
8283 {
8284 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
8285 parser->in_type_id_in_expr_p = true;
8286 /* Look for the type-id. */
8287 type = cp_parser_type_id (parser);
8288 /* Look for the closing `)'. */
8289 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
8290 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
8291 }
8292
8293 /* Restore the saved message. */
8294 parser->type_definition_forbidden_message = saved_message;
8295
8296 /* At this point this can only be either a cast or a
8297 parenthesized ctor such as `(T ())' that looks like a cast to
8298 function returning T. */
8299 if (!cp_parser_error_occurred (parser))
8300 {
8301 /* Only commit if the cast-expression doesn't start with
8302 '++', '--', or '[' in C++11. */
8303 if (cast_expression > 0)
8304 cp_parser_commit_to_topmost_tentative_parse (parser);
8305
8306 expr = cp_parser_cast_expression (parser,
8307 /*address_p=*/false,
8308 /*cast_p=*/true,
8309 /*decltype_p=*/false,
8310 pidk);
8311
8312 if (cp_parser_parse_definitely (parser))
8313 {
8314 /* Warn about old-style casts, if so requested. */
8315 if (warn_old_style_cast
8316 && !in_system_header_at (input_location)
8317 && !VOID_TYPE_P (type)
8318 && current_lang_name != lang_name_c)
8319 warning (OPT_Wold_style_cast, "use of old-style cast");
8320
8321 /* Only type conversions to integral or enumeration types
8322 can be used in constant-expressions. */
8323 if (!cast_valid_in_integral_constant_expression_p (type)
8324 && cp_parser_non_integral_constant_expression (parser,
8325 NIC_CAST))
8326 return error_mark_node;
8327
8328 /* Perform the cast. */
8329 expr = build_c_cast (input_location, type, expr);
8330 return expr;
8331 }
8332 }
8333 else
8334 cp_parser_abort_tentative_parse (parser);
8335 }
8336
8337 /* If we get here, then it's not a cast, so it must be a
8338 unary-expression. */
8339 return cp_parser_unary_expression (parser, pidk, address_p,
8340 cast_p, decltype_p);
8341 }
8342
8343 /* Parse a binary expression of the general form:
8344
8345 pm-expression:
8346 cast-expression
8347 pm-expression .* cast-expression
8348 pm-expression ->* cast-expression
8349
8350 multiplicative-expression:
8351 pm-expression
8352 multiplicative-expression * pm-expression
8353 multiplicative-expression / pm-expression
8354 multiplicative-expression % pm-expression
8355
8356 additive-expression:
8357 multiplicative-expression
8358 additive-expression + multiplicative-expression
8359 additive-expression - multiplicative-expression
8360
8361 shift-expression:
8362 additive-expression
8363 shift-expression << additive-expression
8364 shift-expression >> additive-expression
8365
8366 relational-expression:
8367 shift-expression
8368 relational-expression < shift-expression
8369 relational-expression > shift-expression
8370 relational-expression <= shift-expression
8371 relational-expression >= shift-expression
8372
8373 GNU Extension:
8374
8375 relational-expression:
8376 relational-expression <? shift-expression
8377 relational-expression >? shift-expression
8378
8379 equality-expression:
8380 relational-expression
8381 equality-expression == relational-expression
8382 equality-expression != relational-expression
8383
8384 and-expression:
8385 equality-expression
8386 and-expression & equality-expression
8387
8388 exclusive-or-expression:
8389 and-expression
8390 exclusive-or-expression ^ and-expression
8391
8392 inclusive-or-expression:
8393 exclusive-or-expression
8394 inclusive-or-expression | exclusive-or-expression
8395
8396 logical-and-expression:
8397 inclusive-or-expression
8398 logical-and-expression && inclusive-or-expression
8399
8400 logical-or-expression:
8401 logical-and-expression
8402 logical-or-expression || logical-and-expression
8403
8404 All these are implemented with a single function like:
8405
8406 binary-expression:
8407 simple-cast-expression
8408 binary-expression <token> binary-expression
8409
8410 CAST_P is true if this expression is the target of a cast.
8411
8412 The binops_by_token map is used to get the tree codes for each <token> type.
8413 binary-expressions are associated according to a precedence table. */
8414
8415 #define TOKEN_PRECEDENCE(token) \
8416 (((token->type == CPP_GREATER \
8417 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT)) \
8418 && !parser->greater_than_is_operator_p) \
8419 ? PREC_NOT_OPERATOR \
8420 : binops_by_token[token->type].prec)
8421
8422 static tree
8423 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
8424 bool no_toplevel_fold_p,
8425 bool decltype_p,
8426 enum cp_parser_prec prec,
8427 cp_id_kind * pidk)
8428 {
8429 cp_parser_expression_stack stack;
8430 cp_parser_expression_stack_entry *sp = &stack[0];
8431 cp_parser_expression_stack_entry current;
8432 tree rhs;
8433 cp_token *token;
8434 enum tree_code rhs_type;
8435 enum cp_parser_prec new_prec, lookahead_prec;
8436 tree overload;
8437
8438 /* Parse the first expression. */
8439 current.lhs_type = (cp_lexer_next_token_is (parser->lexer, CPP_NOT)
8440 ? TRUTH_NOT_EXPR : ERROR_MARK);
8441 current.lhs = cp_parser_cast_expression (parser, /*address_p=*/false,
8442 cast_p, decltype_p, pidk);
8443 current.prec = prec;
8444
8445 if (cp_parser_error_occurred (parser))
8446 return error_mark_node;
8447
8448 for (;;)
8449 {
8450 /* Get an operator token. */
8451 token = cp_lexer_peek_token (parser->lexer);
8452
8453 if (warn_cxx11_compat
8454 && token->type == CPP_RSHIFT
8455 && !parser->greater_than_is_operator_p)
8456 {
8457 if (warning_at (token->location, OPT_Wc__11_compat,
8458 "%<>>%> operator is treated"
8459 " as two right angle brackets in C++11"))
8460 inform (token->location,
8461 "suggest parentheses around %<>>%> expression");
8462 }
8463
8464 new_prec = TOKEN_PRECEDENCE (token);
8465 if (new_prec != PREC_NOT_OPERATOR
8466 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS))
8467 /* This is a fold-expression; handle it later. */
8468 new_prec = PREC_NOT_OPERATOR;
8469
8470 /* Popping an entry off the stack means we completed a subexpression:
8471 - either we found a token which is not an operator (`>' where it is not
8472 an operator, or prec == PREC_NOT_OPERATOR), in which case popping
8473 will happen repeatedly;
8474 - or, we found an operator which has lower priority. This is the case
8475 where the recursive descent *ascends*, as in `3 * 4 + 5' after
8476 parsing `3 * 4'. */
8477 if (new_prec <= current.prec)
8478 {
8479 if (sp == stack)
8480 break;
8481 else
8482 goto pop;
8483 }
8484
8485 get_rhs:
8486 current.tree_type = binops_by_token[token->type].tree_type;
8487 current.loc = token->location;
8488
8489 /* We used the operator token. */
8490 cp_lexer_consume_token (parser->lexer);
8491
8492 /* For "false && x" or "true || x", x will never be executed;
8493 disable warnings while evaluating it. */
8494 if (current.tree_type == TRUTH_ANDIF_EXPR)
8495 c_inhibit_evaluation_warnings +=
8496 cp_fully_fold (current.lhs) == truthvalue_false_node;
8497 else if (current.tree_type == TRUTH_ORIF_EXPR)
8498 c_inhibit_evaluation_warnings +=
8499 cp_fully_fold (current.lhs) == truthvalue_true_node;
8500
8501 /* Extract another operand. It may be the RHS of this expression
8502 or the LHS of a new, higher priority expression. */
8503 rhs_type = (cp_lexer_next_token_is (parser->lexer, CPP_NOT)
8504 ? TRUTH_NOT_EXPR : ERROR_MARK);
8505 rhs = cp_parser_simple_cast_expression (parser);
8506
8507 /* Get another operator token. Look up its precedence to avoid
8508 building a useless (immediately popped) stack entry for common
8509 cases such as 3 + 4 + 5 or 3 * 4 + 5. */
8510 token = cp_lexer_peek_token (parser->lexer);
8511 lookahead_prec = TOKEN_PRECEDENCE (token);
8512 if (lookahead_prec != PREC_NOT_OPERATOR
8513 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS))
8514 lookahead_prec = PREC_NOT_OPERATOR;
8515 if (lookahead_prec > new_prec)
8516 {
8517 /* ... and prepare to parse the RHS of the new, higher priority
8518 expression. Since precedence levels on the stack are
8519 monotonically increasing, we do not have to care about
8520 stack overflows. */
8521 *sp = current;
8522 ++sp;
8523 current.lhs = rhs;
8524 current.lhs_type = rhs_type;
8525 current.prec = new_prec;
8526 new_prec = lookahead_prec;
8527 goto get_rhs;
8528
8529 pop:
8530 lookahead_prec = new_prec;
8531 /* If the stack is not empty, we have parsed into LHS the right side
8532 (`4' in the example above) of an expression we had suspended.
8533 We can use the information on the stack to recover the LHS (`3')
8534 from the stack together with the tree code (`MULT_EXPR'), and
8535 the precedence of the higher level subexpression
8536 (`PREC_ADDITIVE_EXPRESSION'). TOKEN is the CPP_PLUS token,
8537 which will be used to actually build the additive expression. */
8538 rhs = current.lhs;
8539 rhs_type = current.lhs_type;
8540 --sp;
8541 current = *sp;
8542 }
8543
8544 /* Undo the disabling of warnings done above. */
8545 if (current.tree_type == TRUTH_ANDIF_EXPR)
8546 c_inhibit_evaluation_warnings -=
8547 cp_fully_fold (current.lhs) == truthvalue_false_node;
8548 else if (current.tree_type == TRUTH_ORIF_EXPR)
8549 c_inhibit_evaluation_warnings -=
8550 cp_fully_fold (current.lhs) == truthvalue_true_node;
8551
8552 if (warn_logical_not_paren
8553 && TREE_CODE_CLASS (current.tree_type) == tcc_comparison
8554 && current.lhs_type == TRUTH_NOT_EXPR
8555 /* Avoid warning for !!x == y. */
8556 && (TREE_CODE (current.lhs) != NE_EXPR
8557 || !integer_zerop (TREE_OPERAND (current.lhs, 1)))
8558 && (TREE_CODE (current.lhs) != TRUTH_NOT_EXPR
8559 || (TREE_CODE (TREE_OPERAND (current.lhs, 0)) != TRUTH_NOT_EXPR
8560 /* Avoid warning for !b == y where b is boolean. */
8561 && (TREE_TYPE (TREE_OPERAND (current.lhs, 0)) == NULL_TREE
8562 || (TREE_CODE (TREE_TYPE (TREE_OPERAND (current.lhs, 0)))
8563 != BOOLEAN_TYPE))))
8564 /* Avoid warning for !!b == y where b is boolean. */
8565 && (!DECL_P (current.lhs)
8566 || TREE_TYPE (current.lhs) == NULL_TREE
8567 || TREE_CODE (TREE_TYPE (current.lhs)) != BOOLEAN_TYPE))
8568 warn_logical_not_parentheses (current.loc, current.tree_type,
8569 maybe_constant_value (rhs));
8570
8571 overload = NULL;
8572 /* ??? Currently we pass lhs_type == ERROR_MARK and rhs_type ==
8573 ERROR_MARK for everything that is not a binary expression.
8574 This makes warn_about_parentheses miss some warnings that
8575 involve unary operators. For unary expressions we should
8576 pass the correct tree_code unless the unary expression was
8577 surrounded by parentheses.
8578 */
8579 if (no_toplevel_fold_p
8580 && lookahead_prec <= current.prec
8581 && sp == stack)
8582 current.lhs = build2 (current.tree_type,
8583 TREE_CODE_CLASS (current.tree_type)
8584 == tcc_comparison
8585 ? boolean_type_node : TREE_TYPE (current.lhs),
8586 current.lhs, rhs);
8587 else
8588 current.lhs = build_x_binary_op (current.loc, current.tree_type,
8589 current.lhs, current.lhs_type,
8590 rhs, rhs_type, &overload,
8591 complain_flags (decltype_p));
8592 current.lhs_type = current.tree_type;
8593 protected_set_expr_location (current.lhs, current.loc);
8594
8595 /* If the binary operator required the use of an overloaded operator,
8596 then this expression cannot be an integral constant-expression.
8597 An overloaded operator can be used even if both operands are
8598 otherwise permissible in an integral constant-expression if at
8599 least one of the operands is of enumeration type. */
8600
8601 if (overload
8602 && cp_parser_non_integral_constant_expression (parser,
8603 NIC_OVERLOADED))
8604 return error_mark_node;
8605 }
8606
8607 return current.lhs;
8608 }
8609
8610 static tree
8611 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
8612 bool no_toplevel_fold_p,
8613 enum cp_parser_prec prec,
8614 cp_id_kind * pidk)
8615 {
8616 return cp_parser_binary_expression (parser, cast_p, no_toplevel_fold_p,
8617 /*decltype*/false, prec, pidk);
8618 }
8619
8620 /* Parse the `? expression : assignment-expression' part of a
8621 conditional-expression. The LOGICAL_OR_EXPR is the
8622 logical-or-expression that started the conditional-expression.
8623 Returns a representation of the entire conditional-expression.
8624
8625 This routine is used by cp_parser_assignment_expression.
8626
8627 ? expression : assignment-expression
8628
8629 GNU Extensions:
8630
8631 ? : assignment-expression */
8632
8633 static tree
8634 cp_parser_question_colon_clause (cp_parser* parser, tree logical_or_expr)
8635 {
8636 tree expr, folded_logical_or_expr = cp_fully_fold (logical_or_expr);
8637 tree assignment_expr;
8638 struct cp_token *token;
8639 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
8640
8641 /* Consume the `?' token. */
8642 cp_lexer_consume_token (parser->lexer);
8643 token = cp_lexer_peek_token (parser->lexer);
8644 if (cp_parser_allow_gnu_extensions_p (parser)
8645 && token->type == CPP_COLON)
8646 {
8647 pedwarn (token->location, OPT_Wpedantic,
8648 "ISO C++ does not allow ?: with omitted middle operand");
8649 /* Implicit true clause. */
8650 expr = NULL_TREE;
8651 c_inhibit_evaluation_warnings +=
8652 folded_logical_or_expr == truthvalue_true_node;
8653 warn_for_omitted_condop (token->location, logical_or_expr);
8654 }
8655 else
8656 {
8657 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
8658 parser->colon_corrects_to_scope_p = false;
8659 /* Parse the expression. */
8660 c_inhibit_evaluation_warnings +=
8661 folded_logical_or_expr == truthvalue_false_node;
8662 expr = cp_parser_expression (parser);
8663 c_inhibit_evaluation_warnings +=
8664 ((folded_logical_or_expr == truthvalue_true_node)
8665 - (folded_logical_or_expr == truthvalue_false_node));
8666 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
8667 }
8668
8669 /* The next token should be a `:'. */
8670 cp_parser_require (parser, CPP_COLON, RT_COLON);
8671 /* Parse the assignment-expression. */
8672 assignment_expr = cp_parser_assignment_expression (parser);
8673 c_inhibit_evaluation_warnings -=
8674 folded_logical_or_expr == truthvalue_true_node;
8675
8676 /* Build the conditional-expression. */
8677 return build_x_conditional_expr (loc, logical_or_expr,
8678 expr,
8679 assignment_expr,
8680 tf_warning_or_error);
8681 }
8682
8683 /* Parse an assignment-expression.
8684
8685 assignment-expression:
8686 conditional-expression
8687 logical-or-expression assignment-operator assignment_expression
8688 throw-expression
8689
8690 CAST_P is true if this expression is the target of a cast.
8691 DECLTYPE_P is true if this expression is the operand of decltype.
8692
8693 Returns a representation for the expression. */
8694
8695 static tree
8696 cp_parser_assignment_expression (cp_parser* parser, cp_id_kind * pidk,
8697 bool cast_p, bool decltype_p)
8698 {
8699 tree expr;
8700
8701 /* If the next token is the `throw' keyword, then we're looking at
8702 a throw-expression. */
8703 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
8704 expr = cp_parser_throw_expression (parser);
8705 /* Otherwise, it must be that we are looking at a
8706 logical-or-expression. */
8707 else
8708 {
8709 /* Parse the binary expressions (logical-or-expression). */
8710 expr = cp_parser_binary_expression (parser, cast_p, false,
8711 decltype_p,
8712 PREC_NOT_OPERATOR, pidk);
8713 /* If the next token is a `?' then we're actually looking at a
8714 conditional-expression. */
8715 if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
8716 return cp_parser_question_colon_clause (parser, expr);
8717 else
8718 {
8719 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
8720
8721 /* If it's an assignment-operator, we're using the second
8722 production. */
8723 enum tree_code assignment_operator
8724 = cp_parser_assignment_operator_opt (parser);
8725 if (assignment_operator != ERROR_MARK)
8726 {
8727 bool non_constant_p;
8728 location_t saved_input_location;
8729
8730 /* Parse the right-hand side of the assignment. */
8731 tree rhs = cp_parser_initializer_clause (parser, &non_constant_p);
8732
8733 if (BRACE_ENCLOSED_INITIALIZER_P (rhs))
8734 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
8735
8736 /* An assignment may not appear in a
8737 constant-expression. */
8738 if (cp_parser_non_integral_constant_expression (parser,
8739 NIC_ASSIGNMENT))
8740 return error_mark_node;
8741 /* Build the assignment expression. Its default
8742 location is the location of the '=' token. */
8743 saved_input_location = input_location;
8744 input_location = loc;
8745 expr = build_x_modify_expr (loc, expr,
8746 assignment_operator,
8747 rhs,
8748 complain_flags (decltype_p));
8749 input_location = saved_input_location;
8750 }
8751 }
8752 }
8753
8754 return expr;
8755 }
8756
8757 /* Parse an (optional) assignment-operator.
8758
8759 assignment-operator: one of
8760 = *= /= %= += -= >>= <<= &= ^= |=
8761
8762 GNU Extension:
8763
8764 assignment-operator: one of
8765 <?= >?=
8766
8767 If the next token is an assignment operator, the corresponding tree
8768 code is returned, and the token is consumed. For example, for
8769 `+=', PLUS_EXPR is returned. For `=' itself, the code returned is
8770 NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%',
8771 TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment
8772 operator, ERROR_MARK is returned. */
8773
8774 static enum tree_code
8775 cp_parser_assignment_operator_opt (cp_parser* parser)
8776 {
8777 enum tree_code op;
8778 cp_token *token;
8779
8780 /* Peek at the next token. */
8781 token = cp_lexer_peek_token (parser->lexer);
8782
8783 switch (token->type)
8784 {
8785 case CPP_EQ:
8786 op = NOP_EXPR;
8787 break;
8788
8789 case CPP_MULT_EQ:
8790 op = MULT_EXPR;
8791 break;
8792
8793 case CPP_DIV_EQ:
8794 op = TRUNC_DIV_EXPR;
8795 break;
8796
8797 case CPP_MOD_EQ:
8798 op = TRUNC_MOD_EXPR;
8799 break;
8800
8801 case CPP_PLUS_EQ:
8802 op = PLUS_EXPR;
8803 break;
8804
8805 case CPP_MINUS_EQ:
8806 op = MINUS_EXPR;
8807 break;
8808
8809 case CPP_RSHIFT_EQ:
8810 op = RSHIFT_EXPR;
8811 break;
8812
8813 case CPP_LSHIFT_EQ:
8814 op = LSHIFT_EXPR;
8815 break;
8816
8817 case CPP_AND_EQ:
8818 op = BIT_AND_EXPR;
8819 break;
8820
8821 case CPP_XOR_EQ:
8822 op = BIT_XOR_EXPR;
8823 break;
8824
8825 case CPP_OR_EQ:
8826 op = BIT_IOR_EXPR;
8827 break;
8828
8829 default:
8830 /* Nothing else is an assignment operator. */
8831 op = ERROR_MARK;
8832 }
8833
8834 /* An operator followed by ... is a fold-expression, handled elsewhere. */
8835 if (op != ERROR_MARK
8836 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS))
8837 op = ERROR_MARK;
8838
8839 /* If it was an assignment operator, consume it. */
8840 if (op != ERROR_MARK)
8841 cp_lexer_consume_token (parser->lexer);
8842
8843 return op;
8844 }
8845
8846 /* Parse an expression.
8847
8848 expression:
8849 assignment-expression
8850 expression , assignment-expression
8851
8852 CAST_P is true if this expression is the target of a cast.
8853 DECLTYPE_P is true if this expression is the immediate operand of decltype,
8854 except possibly parenthesized or on the RHS of a comma (N3276).
8855
8856 Returns a representation of the expression. */
8857
8858 static tree
8859 cp_parser_expression (cp_parser* parser, cp_id_kind * pidk,
8860 bool cast_p, bool decltype_p)
8861 {
8862 tree expression = NULL_TREE;
8863 location_t loc = UNKNOWN_LOCATION;
8864
8865 while (true)
8866 {
8867 tree assignment_expression;
8868
8869 /* Parse the next assignment-expression. */
8870 assignment_expression
8871 = cp_parser_assignment_expression (parser, pidk, cast_p, decltype_p);
8872
8873 /* We don't create a temporary for a call that is the immediate operand
8874 of decltype or on the RHS of a comma. But when we see a comma, we
8875 need to create a temporary for a call on the LHS. */
8876 if (decltype_p && !processing_template_decl
8877 && TREE_CODE (assignment_expression) == CALL_EXPR
8878 && CLASS_TYPE_P (TREE_TYPE (assignment_expression))
8879 && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
8880 assignment_expression
8881 = build_cplus_new (TREE_TYPE (assignment_expression),
8882 assignment_expression, tf_warning_or_error);
8883
8884 /* If this is the first assignment-expression, we can just
8885 save it away. */
8886 if (!expression)
8887 expression = assignment_expression;
8888 else
8889 expression = build_x_compound_expr (loc, expression,
8890 assignment_expression,
8891 complain_flags (decltype_p));
8892 /* If the next token is not a comma, or we're in a fold-expression, then
8893 we are done with the expression. */
8894 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA)
8895 || cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS))
8896 break;
8897 /* Consume the `,'. */
8898 loc = cp_lexer_peek_token (parser->lexer)->location;
8899 cp_lexer_consume_token (parser->lexer);
8900 /* A comma operator cannot appear in a constant-expression. */
8901 if (cp_parser_non_integral_constant_expression (parser, NIC_COMMA))
8902 expression = error_mark_node;
8903 }
8904
8905 return expression;
8906 }
8907
8908 /* Parse a constant-expression.
8909
8910 constant-expression:
8911 conditional-expression
8912
8913 If ALLOW_NON_CONSTANT_P a non-constant expression is silently
8914 accepted. If ALLOW_NON_CONSTANT_P is true and the expression is not
8915 constant, *NON_CONSTANT_P is set to TRUE. If ALLOW_NON_CONSTANT_P
8916 is false, NON_CONSTANT_P should be NULL. */
8917
8918 static tree
8919 cp_parser_constant_expression (cp_parser* parser,
8920 bool allow_non_constant_p,
8921 bool *non_constant_p)
8922 {
8923 bool saved_integral_constant_expression_p;
8924 bool saved_allow_non_integral_constant_expression_p;
8925 bool saved_non_integral_constant_expression_p;
8926 tree expression;
8927
8928 /* It might seem that we could simply parse the
8929 conditional-expression, and then check to see if it were
8930 TREE_CONSTANT. However, an expression that is TREE_CONSTANT is
8931 one that the compiler can figure out is constant, possibly after
8932 doing some simplifications or optimizations. The standard has a
8933 precise definition of constant-expression, and we must honor
8934 that, even though it is somewhat more restrictive.
8935
8936 For example:
8937
8938 int i[(2, 3)];
8939
8940 is not a legal declaration, because `(2, 3)' is not a
8941 constant-expression. The `,' operator is forbidden in a
8942 constant-expression. However, GCC's constant-folding machinery
8943 will fold this operation to an INTEGER_CST for `3'. */
8944
8945 /* Save the old settings. */
8946 saved_integral_constant_expression_p = parser->integral_constant_expression_p;
8947 saved_allow_non_integral_constant_expression_p
8948 = parser->allow_non_integral_constant_expression_p;
8949 saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
8950 /* We are now parsing a constant-expression. */
8951 parser->integral_constant_expression_p = true;
8952 parser->allow_non_integral_constant_expression_p
8953 = (allow_non_constant_p || cxx_dialect >= cxx11);
8954 parser->non_integral_constant_expression_p = false;
8955 /* Although the grammar says "conditional-expression", we parse an
8956 "assignment-expression", which also permits "throw-expression"
8957 and the use of assignment operators. In the case that
8958 ALLOW_NON_CONSTANT_P is false, we get better errors than we would
8959 otherwise. In the case that ALLOW_NON_CONSTANT_P is true, it is
8960 actually essential that we look for an assignment-expression.
8961 For example, cp_parser_initializer_clauses uses this function to
8962 determine whether a particular assignment-expression is in fact
8963 constant. */
8964 expression = cp_parser_assignment_expression (parser);
8965 /* Restore the old settings. */
8966 parser->integral_constant_expression_p
8967 = saved_integral_constant_expression_p;
8968 parser->allow_non_integral_constant_expression_p
8969 = saved_allow_non_integral_constant_expression_p;
8970 if (cxx_dialect >= cxx11)
8971 {
8972 /* Require an rvalue constant expression here; that's what our
8973 callers expect. Reference constant expressions are handled
8974 separately in e.g. cp_parser_template_argument. */
8975 bool is_const = potential_rvalue_constant_expression (expression);
8976 parser->non_integral_constant_expression_p = !is_const;
8977 if (!is_const && !allow_non_constant_p)
8978 require_potential_rvalue_constant_expression (expression);
8979 }
8980 if (allow_non_constant_p)
8981 *non_constant_p = parser->non_integral_constant_expression_p;
8982 parser->non_integral_constant_expression_p
8983 = saved_non_integral_constant_expression_p;
8984
8985 return expression;
8986 }
8987
8988 /* Parse __builtin_offsetof.
8989
8990 offsetof-expression:
8991 "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
8992
8993 offsetof-member-designator:
8994 id-expression
8995 | offsetof-member-designator "." id-expression
8996 | offsetof-member-designator "[" expression "]"
8997 | offsetof-member-designator "->" id-expression */
8998
8999 static tree
9000 cp_parser_builtin_offsetof (cp_parser *parser)
9001 {
9002 int save_ice_p, save_non_ice_p;
9003 tree type, expr;
9004 cp_id_kind dummy;
9005 cp_token *token;
9006
9007 /* We're about to accept non-integral-constant things, but will
9008 definitely yield an integral constant expression. Save and
9009 restore these values around our local parsing. */
9010 save_ice_p = parser->integral_constant_expression_p;
9011 save_non_ice_p = parser->non_integral_constant_expression_p;
9012
9013 /* Consume the "__builtin_offsetof" token. */
9014 cp_lexer_consume_token (parser->lexer);
9015 /* Consume the opening `('. */
9016 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
9017 /* Parse the type-id. */
9018 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
9019 type = cp_parser_type_id (parser);
9020 /* Look for the `,'. */
9021 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
9022 token = cp_lexer_peek_token (parser->lexer);
9023
9024 /* Build the (type *)null that begins the traditional offsetof macro. */
9025 expr = build_static_cast (build_pointer_type (type), null_pointer_node,
9026 tf_warning_or_error);
9027
9028 /* Parse the offsetof-member-designator. We begin as if we saw "expr->". */
9029 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, expr,
9030 true, &dummy, token->location);
9031 while (true)
9032 {
9033 token = cp_lexer_peek_token (parser->lexer);
9034 switch (token->type)
9035 {
9036 case CPP_OPEN_SQUARE:
9037 /* offsetof-member-designator "[" expression "]" */
9038 expr = cp_parser_postfix_open_square_expression (parser, expr,
9039 true, false);
9040 break;
9041
9042 case CPP_DEREF:
9043 /* offsetof-member-designator "->" identifier */
9044 expr = grok_array_decl (token->location, expr,
9045 integer_zero_node, false);
9046 /* FALLTHRU */
9047
9048 case CPP_DOT:
9049 /* offsetof-member-designator "." identifier */
9050 cp_lexer_consume_token (parser->lexer);
9051 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT,
9052 expr, true, &dummy,
9053 token->location);
9054 break;
9055
9056 case CPP_CLOSE_PAREN:
9057 /* Consume the ")" token. */
9058 cp_lexer_consume_token (parser->lexer);
9059 goto success;
9060
9061 default:
9062 /* Error. We know the following require will fail, but
9063 that gives the proper error message. */
9064 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
9065 cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
9066 expr = error_mark_node;
9067 goto failure;
9068 }
9069 }
9070
9071 success:
9072 expr = finish_offsetof (expr, loc);
9073
9074 failure:
9075 parser->integral_constant_expression_p = save_ice_p;
9076 parser->non_integral_constant_expression_p = save_non_ice_p;
9077
9078 return expr;
9079 }
9080
9081 /* Parse a trait expression.
9082
9083 Returns a representation of the expression, the underlying type
9084 of the type at issue when KEYWORD is RID_UNDERLYING_TYPE. */
9085
9086 static tree
9087 cp_parser_trait_expr (cp_parser* parser, enum rid keyword)
9088 {
9089 cp_trait_kind kind;
9090 tree type1, type2 = NULL_TREE;
9091 bool binary = false;
9092 bool variadic = false;
9093
9094 switch (keyword)
9095 {
9096 case RID_HAS_NOTHROW_ASSIGN:
9097 kind = CPTK_HAS_NOTHROW_ASSIGN;
9098 break;
9099 case RID_HAS_NOTHROW_CONSTRUCTOR:
9100 kind = CPTK_HAS_NOTHROW_CONSTRUCTOR;
9101 break;
9102 case RID_HAS_NOTHROW_COPY:
9103 kind = CPTK_HAS_NOTHROW_COPY;
9104 break;
9105 case RID_HAS_TRIVIAL_ASSIGN:
9106 kind = CPTK_HAS_TRIVIAL_ASSIGN;
9107 break;
9108 case RID_HAS_TRIVIAL_CONSTRUCTOR:
9109 kind = CPTK_HAS_TRIVIAL_CONSTRUCTOR;
9110 break;
9111 case RID_HAS_TRIVIAL_COPY:
9112 kind = CPTK_HAS_TRIVIAL_COPY;
9113 break;
9114 case RID_HAS_TRIVIAL_DESTRUCTOR:
9115 kind = CPTK_HAS_TRIVIAL_DESTRUCTOR;
9116 break;
9117 case RID_HAS_VIRTUAL_DESTRUCTOR:
9118 kind = CPTK_HAS_VIRTUAL_DESTRUCTOR;
9119 break;
9120 case RID_IS_ABSTRACT:
9121 kind = CPTK_IS_ABSTRACT;
9122 break;
9123 case RID_IS_BASE_OF:
9124 kind = CPTK_IS_BASE_OF;
9125 binary = true;
9126 break;
9127 case RID_IS_CLASS:
9128 kind = CPTK_IS_CLASS;
9129 break;
9130 case RID_IS_EMPTY:
9131 kind = CPTK_IS_EMPTY;
9132 break;
9133 case RID_IS_ENUM:
9134 kind = CPTK_IS_ENUM;
9135 break;
9136 case RID_IS_FINAL:
9137 kind = CPTK_IS_FINAL;
9138 break;
9139 case RID_IS_LITERAL_TYPE:
9140 kind = CPTK_IS_LITERAL_TYPE;
9141 break;
9142 case RID_IS_POD:
9143 kind = CPTK_IS_POD;
9144 break;
9145 case RID_IS_POLYMORPHIC:
9146 kind = CPTK_IS_POLYMORPHIC;
9147 break;
9148 case RID_IS_SAME_AS:
9149 kind = CPTK_IS_SAME_AS;
9150 binary = true;
9151 break;
9152 case RID_IS_STD_LAYOUT:
9153 kind = CPTK_IS_STD_LAYOUT;
9154 break;
9155 case RID_IS_TRIVIAL:
9156 kind = CPTK_IS_TRIVIAL;
9157 break;
9158 case RID_IS_TRIVIALLY_ASSIGNABLE:
9159 kind = CPTK_IS_TRIVIALLY_ASSIGNABLE;
9160 binary = true;
9161 break;
9162 case RID_IS_TRIVIALLY_CONSTRUCTIBLE:
9163 kind = CPTK_IS_TRIVIALLY_CONSTRUCTIBLE;
9164 variadic = true;
9165 break;
9166 case RID_IS_TRIVIALLY_COPYABLE:
9167 kind = CPTK_IS_TRIVIALLY_COPYABLE;
9168 break;
9169 case RID_IS_UNION:
9170 kind = CPTK_IS_UNION;
9171 break;
9172 case RID_UNDERLYING_TYPE:
9173 kind = CPTK_UNDERLYING_TYPE;
9174 break;
9175 case RID_BASES:
9176 kind = CPTK_BASES;
9177 break;
9178 case RID_DIRECT_BASES:
9179 kind = CPTK_DIRECT_BASES;
9180 break;
9181 default:
9182 gcc_unreachable ();
9183 }
9184
9185 /* Consume the token. */
9186 cp_lexer_consume_token (parser->lexer);
9187
9188 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
9189
9190 type1 = cp_parser_type_id (parser);
9191
9192 if (type1 == error_mark_node)
9193 return error_mark_node;
9194
9195 if (binary)
9196 {
9197 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
9198
9199 type2 = cp_parser_type_id (parser);
9200
9201 if (type2 == error_mark_node)
9202 return error_mark_node;
9203 }
9204 else if (variadic)
9205 {
9206 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
9207 {
9208 cp_lexer_consume_token (parser->lexer);
9209 tree elt = cp_parser_type_id (parser);
9210 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
9211 {
9212 cp_lexer_consume_token (parser->lexer);
9213 elt = make_pack_expansion (elt);
9214 }
9215 if (elt == error_mark_node)
9216 return error_mark_node;
9217 type2 = tree_cons (NULL_TREE, elt, type2);
9218 }
9219 }
9220
9221 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
9222
9223 /* Complete the trait expression, which may mean either processing
9224 the trait expr now or saving it for template instantiation. */
9225 switch(kind)
9226 {
9227 case CPTK_UNDERLYING_TYPE:
9228 return finish_underlying_type (type1);
9229 case CPTK_BASES:
9230 return finish_bases (type1, false);
9231 case CPTK_DIRECT_BASES:
9232 return finish_bases (type1, true);
9233 default:
9234 return finish_trait_expr (kind, type1, type2);
9235 }
9236 }
9237
9238 /* Lambdas that appear in variable initializer or default argument scope
9239 get that in their mangling, so we need to record it. We might as well
9240 use the count for function and namespace scopes as well. */
9241 static GTY(()) tree lambda_scope;
9242 static GTY(()) int lambda_count;
9243 struct GTY(()) tree_int
9244 {
9245 tree t;
9246 int i;
9247 };
9248 static GTY(()) vec<tree_int, va_gc> *lambda_scope_stack;
9249
9250 static void
9251 start_lambda_scope (tree decl)
9252 {
9253 tree_int ti;
9254 gcc_assert (decl);
9255 /* Once we're inside a function, we ignore other scopes and just push
9256 the function again so that popping works properly. */
9257 if (current_function_decl && TREE_CODE (decl) != FUNCTION_DECL)
9258 decl = current_function_decl;
9259 ti.t = lambda_scope;
9260 ti.i = lambda_count;
9261 vec_safe_push (lambda_scope_stack, ti);
9262 if (lambda_scope != decl)
9263 {
9264 /* Don't reset the count if we're still in the same function. */
9265 lambda_scope = decl;
9266 lambda_count = 0;
9267 }
9268 }
9269
9270 static void
9271 record_lambda_scope (tree lambda)
9272 {
9273 LAMBDA_EXPR_EXTRA_SCOPE (lambda) = lambda_scope;
9274 LAMBDA_EXPR_DISCRIMINATOR (lambda) = lambda_count++;
9275 }
9276
9277 static void
9278 finish_lambda_scope (void)
9279 {
9280 tree_int *p = &lambda_scope_stack->last ();
9281 if (lambda_scope != p->t)
9282 {
9283 lambda_scope = p->t;
9284 lambda_count = p->i;
9285 }
9286 lambda_scope_stack->pop ();
9287 }
9288
9289 /* Parse a lambda expression.
9290
9291 lambda-expression:
9292 lambda-introducer lambda-declarator [opt] compound-statement
9293
9294 Returns a representation of the expression. */
9295
9296 static tree
9297 cp_parser_lambda_expression (cp_parser* parser)
9298 {
9299 tree lambda_expr = build_lambda_expr ();
9300 tree type;
9301 bool ok = true;
9302 cp_token *token = cp_lexer_peek_token (parser->lexer);
9303 cp_token_position start = 0;
9304
9305 LAMBDA_EXPR_LOCATION (lambda_expr) = token->location;
9306
9307 if (cp_unevaluated_operand)
9308 {
9309 if (!token->error_reported)
9310 {
9311 error_at (LAMBDA_EXPR_LOCATION (lambda_expr),
9312 "lambda-expression in unevaluated context");
9313 token->error_reported = true;
9314 }
9315 ok = false;
9316 }
9317 else if (parser->in_template_argument_list_p)
9318 {
9319 if (!token->error_reported)
9320 {
9321 error_at (token->location, "lambda-expression in template-argument");
9322 token->error_reported = true;
9323 }
9324 ok = false;
9325 }
9326
9327 /* We may be in the middle of deferred access check. Disable
9328 it now. */
9329 push_deferring_access_checks (dk_no_deferred);
9330
9331 cp_parser_lambda_introducer (parser, lambda_expr);
9332
9333 type = begin_lambda_type (lambda_expr);
9334 if (type == error_mark_node)
9335 return error_mark_node;
9336
9337 record_lambda_scope (lambda_expr);
9338
9339 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
9340 determine_visibility (TYPE_NAME (type));
9341
9342 /* Now that we've started the type, add the capture fields for any
9343 explicit captures. */
9344 register_capture_members (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr));
9345
9346 {
9347 /* Inside the class, surrounding template-parameter-lists do not apply. */
9348 unsigned int saved_num_template_parameter_lists
9349 = parser->num_template_parameter_lists;
9350 unsigned char in_statement = parser->in_statement;
9351 bool in_switch_statement_p = parser->in_switch_statement_p;
9352 bool fully_implicit_function_template_p
9353 = parser->fully_implicit_function_template_p;
9354 tree implicit_template_parms = parser->implicit_template_parms;
9355 cp_binding_level* implicit_template_scope = parser->implicit_template_scope;
9356 bool auto_is_implicit_function_template_parm_p
9357 = parser->auto_is_implicit_function_template_parm_p;
9358
9359 parser->num_template_parameter_lists = 0;
9360 parser->in_statement = 0;
9361 parser->in_switch_statement_p = false;
9362 parser->fully_implicit_function_template_p = false;
9363 parser->implicit_template_parms = 0;
9364 parser->implicit_template_scope = 0;
9365 parser->auto_is_implicit_function_template_parm_p = false;
9366
9367 /* By virtue of defining a local class, a lambda expression has access to
9368 the private variables of enclosing classes. */
9369
9370 ok &= cp_parser_lambda_declarator_opt (parser, lambda_expr);
9371
9372 if (ok)
9373 {
9374 if (!cp_parser_error_occurred (parser)
9375 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
9376 && cp_parser_start_tentative_firewall (parser))
9377 start = token;
9378 cp_parser_lambda_body (parser, lambda_expr);
9379 }
9380 else if (cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
9381 {
9382 if (cp_parser_skip_to_closing_brace (parser))
9383 cp_lexer_consume_token (parser->lexer);
9384 }
9385
9386 /* The capture list was built up in reverse order; fix that now. */
9387 LAMBDA_EXPR_CAPTURE_LIST (lambda_expr)
9388 = nreverse (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr));
9389
9390 if (ok)
9391 maybe_add_lambda_conv_op (type);
9392
9393 type = finish_struct (type, /*attributes=*/NULL_TREE);
9394
9395 parser->num_template_parameter_lists = saved_num_template_parameter_lists;
9396 parser->in_statement = in_statement;
9397 parser->in_switch_statement_p = in_switch_statement_p;
9398 parser->fully_implicit_function_template_p
9399 = fully_implicit_function_template_p;
9400 parser->implicit_template_parms = implicit_template_parms;
9401 parser->implicit_template_scope = implicit_template_scope;
9402 parser->auto_is_implicit_function_template_parm_p
9403 = auto_is_implicit_function_template_parm_p;
9404 }
9405
9406 pop_deferring_access_checks ();
9407
9408 /* This field is only used during parsing of the lambda. */
9409 LAMBDA_EXPR_THIS_CAPTURE (lambda_expr) = NULL_TREE;
9410
9411 /* This lambda shouldn't have any proxies left at this point. */
9412 gcc_assert (LAMBDA_EXPR_PENDING_PROXIES (lambda_expr) == NULL);
9413 /* And now that we're done, push proxies for an enclosing lambda. */
9414 insert_pending_capture_proxies ();
9415
9416 if (ok)
9417 lambda_expr = build_lambda_object (lambda_expr);
9418 else
9419 lambda_expr = error_mark_node;
9420
9421 cp_parser_end_tentative_firewall (parser, start, lambda_expr);
9422
9423 return lambda_expr;
9424 }
9425
9426 /* Parse the beginning of a lambda expression.
9427
9428 lambda-introducer:
9429 [ lambda-capture [opt] ]
9430
9431 LAMBDA_EXPR is the current representation of the lambda expression. */
9432
9433 static void
9434 cp_parser_lambda_introducer (cp_parser* parser, tree lambda_expr)
9435 {
9436 /* Need commas after the first capture. */
9437 bool first = true;
9438
9439 /* Eat the leading `['. */
9440 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
9441
9442 /* Record default capture mode. "[&" "[=" "[&," "[=," */
9443 if (cp_lexer_next_token_is (parser->lexer, CPP_AND)
9444 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_NAME)
9445 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_REFERENCE;
9446 else if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
9447 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_COPY;
9448
9449 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE)
9450 {
9451 cp_lexer_consume_token (parser->lexer);
9452 first = false;
9453 }
9454
9455 while (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_SQUARE))
9456 {
9457 cp_token* capture_token;
9458 tree capture_id;
9459 tree capture_init_expr;
9460 cp_id_kind idk = CP_ID_KIND_NONE;
9461 bool explicit_init_p = false;
9462
9463 enum capture_kind_type
9464 {
9465 BY_COPY,
9466 BY_REFERENCE
9467 };
9468 enum capture_kind_type capture_kind = BY_COPY;
9469
9470 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
9471 {
9472 error ("expected end of capture-list");
9473 return;
9474 }
9475
9476 if (first)
9477 first = false;
9478 else
9479 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
9480
9481 /* Possibly capture `this'. */
9482 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THIS))
9483 {
9484 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
9485 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_COPY)
9486 pedwarn (loc, 0, "explicit by-copy capture of %<this%> redundant "
9487 "with by-copy capture default");
9488 cp_lexer_consume_token (parser->lexer);
9489 add_capture (lambda_expr,
9490 /*id=*/this_identifier,
9491 /*initializer=*/finish_this_expr(),
9492 /*by_reference_p=*/false,
9493 explicit_init_p);
9494 continue;
9495 }
9496
9497 /* Remember whether we want to capture as a reference or not. */
9498 if (cp_lexer_next_token_is (parser->lexer, CPP_AND))
9499 {
9500 capture_kind = BY_REFERENCE;
9501 cp_lexer_consume_token (parser->lexer);
9502 }
9503
9504 /* Get the identifier. */
9505 capture_token = cp_lexer_peek_token (parser->lexer);
9506 capture_id = cp_parser_identifier (parser);
9507
9508 if (capture_id == error_mark_node)
9509 /* Would be nice to have a cp_parser_skip_to_closing_x for general
9510 delimiters, but I modified this to stop on unnested ']' as well. It
9511 was already changed to stop on unnested '}', so the
9512 "closing_parenthesis" name is no more misleading with my change. */
9513 {
9514 cp_parser_skip_to_closing_parenthesis (parser,
9515 /*recovering=*/true,
9516 /*or_comma=*/true,
9517 /*consume_paren=*/true);
9518 break;
9519 }
9520
9521 /* Find the initializer for this capture. */
9522 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ)
9523 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
9524 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
9525 {
9526 bool direct, non_constant;
9527 /* An explicit initializer exists. */
9528 if (cxx_dialect < cxx14)
9529 pedwarn (input_location, 0,
9530 "lambda capture initializers "
9531 "only available with -std=c++14 or -std=gnu++14");
9532 capture_init_expr = cp_parser_initializer (parser, &direct,
9533 &non_constant);
9534 explicit_init_p = true;
9535 if (capture_init_expr == NULL_TREE)
9536 {
9537 error ("empty initializer for lambda init-capture");
9538 capture_init_expr = error_mark_node;
9539 }
9540 }
9541 else
9542 {
9543 const char* error_msg;
9544
9545 /* Turn the identifier into an id-expression. */
9546 capture_init_expr
9547 = cp_parser_lookup_name_simple (parser, capture_id,
9548 capture_token->location);
9549
9550 if (capture_init_expr == error_mark_node)
9551 {
9552 unqualified_name_lookup_error (capture_id);
9553 continue;
9554 }
9555 else if (DECL_P (capture_init_expr)
9556 && (!VAR_P (capture_init_expr)
9557 && TREE_CODE (capture_init_expr) != PARM_DECL))
9558 {
9559 error_at (capture_token->location,
9560 "capture of non-variable %qD ",
9561 capture_init_expr);
9562 inform (DECL_SOURCE_LOCATION (capture_init_expr),
9563 "%q#D declared here", capture_init_expr);
9564 continue;
9565 }
9566 if (VAR_P (capture_init_expr)
9567 && decl_storage_duration (capture_init_expr) != dk_auto)
9568 {
9569 if (pedwarn (capture_token->location, 0, "capture of variable "
9570 "%qD with non-automatic storage duration",
9571 capture_init_expr))
9572 inform (DECL_SOURCE_LOCATION (capture_init_expr),
9573 "%q#D declared here", capture_init_expr);
9574 continue;
9575 }
9576
9577 capture_init_expr
9578 = finish_id_expression
9579 (capture_id,
9580 capture_init_expr,
9581 parser->scope,
9582 &idk,
9583 /*integral_constant_expression_p=*/false,
9584 /*allow_non_integral_constant_expression_p=*/false,
9585 /*non_integral_constant_expression_p=*/NULL,
9586 /*template_p=*/false,
9587 /*done=*/true,
9588 /*address_p=*/false,
9589 /*template_arg_p=*/false,
9590 &error_msg,
9591 capture_token->location);
9592
9593 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
9594 {
9595 cp_lexer_consume_token (parser->lexer);
9596 capture_init_expr = make_pack_expansion (capture_init_expr);
9597 }
9598 else
9599 check_for_bare_parameter_packs (capture_init_expr);
9600 }
9601
9602 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE
9603 && !explicit_init_p)
9604 {
9605 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_COPY
9606 && capture_kind == BY_COPY)
9607 pedwarn (capture_token->location, 0, "explicit by-copy capture "
9608 "of %qD redundant with by-copy capture default",
9609 capture_id);
9610 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_REFERENCE
9611 && capture_kind == BY_REFERENCE)
9612 pedwarn (capture_token->location, 0, "explicit by-reference "
9613 "capture of %qD redundant with by-reference capture "
9614 "default", capture_id);
9615 }
9616
9617 add_capture (lambda_expr,
9618 capture_id,
9619 capture_init_expr,
9620 /*by_reference_p=*/capture_kind == BY_REFERENCE,
9621 explicit_init_p);
9622 }
9623
9624 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
9625 }
9626
9627 /* Parse the (optional) middle of a lambda expression.
9628
9629 lambda-declarator:
9630 < template-parameter-list [opt] >
9631 ( parameter-declaration-clause [opt] )
9632 attribute-specifier [opt]
9633 mutable [opt]
9634 exception-specification [opt]
9635 lambda-return-type-clause [opt]
9636
9637 LAMBDA_EXPR is the current representation of the lambda expression. */
9638
9639 static bool
9640 cp_parser_lambda_declarator_opt (cp_parser* parser, tree lambda_expr)
9641 {
9642 /* 5.1.1.4 of the standard says:
9643 If a lambda-expression does not include a lambda-declarator, it is as if
9644 the lambda-declarator were ().
9645 This means an empty parameter list, no attributes, and no exception
9646 specification. */
9647 tree param_list = void_list_node;
9648 tree attributes = NULL_TREE;
9649 tree exception_spec = NULL_TREE;
9650 tree template_param_list = NULL_TREE;
9651 tree tx_qual = NULL_TREE;
9652
9653 /* The template-parameter-list is optional, but must begin with
9654 an opening angle if present. */
9655 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
9656 {
9657 if (cxx_dialect < cxx14)
9658 pedwarn (parser->lexer->next_token->location, 0,
9659 "lambda templates are only available with "
9660 "-std=c++14 or -std=gnu++14");
9661
9662 cp_lexer_consume_token (parser->lexer);
9663
9664 template_param_list = cp_parser_template_parameter_list (parser);
9665
9666 cp_parser_skip_to_end_of_template_parameter_list (parser);
9667
9668 /* We just processed one more parameter list. */
9669 ++parser->num_template_parameter_lists;
9670 }
9671
9672 /* The parameter-declaration-clause is optional (unless
9673 template-parameter-list was given), but must begin with an
9674 opening parenthesis if present. */
9675 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
9676 {
9677 cp_lexer_consume_token (parser->lexer);
9678
9679 begin_scope (sk_function_parms, /*entity=*/NULL_TREE);
9680
9681 /* Parse parameters. */
9682 param_list = cp_parser_parameter_declaration_clause (parser);
9683
9684 /* Default arguments shall not be specified in the
9685 parameter-declaration-clause of a lambda-declarator. */
9686 for (tree t = param_list; t; t = TREE_CHAIN (t))
9687 if (TREE_PURPOSE (t) && cxx_dialect < cxx14)
9688 pedwarn (DECL_SOURCE_LOCATION (TREE_VALUE (t)), OPT_Wpedantic,
9689 "default argument specified for lambda parameter");
9690
9691 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
9692
9693 attributes = cp_parser_attributes_opt (parser);
9694
9695 /* Parse optional `mutable' keyword. */
9696 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_MUTABLE))
9697 {
9698 cp_lexer_consume_token (parser->lexer);
9699 LAMBDA_EXPR_MUTABLE_P (lambda_expr) = 1;
9700 }
9701
9702 tx_qual = cp_parser_tx_qualifier_opt (parser);
9703
9704 /* Parse optional exception specification. */
9705 exception_spec = cp_parser_exception_specification_opt (parser);
9706
9707 /* Parse optional trailing return type. */
9708 if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
9709 {
9710 cp_lexer_consume_token (parser->lexer);
9711 LAMBDA_EXPR_RETURN_TYPE (lambda_expr)
9712 = cp_parser_trailing_type_id (parser);
9713 }
9714
9715 /* The function parameters must be in scope all the way until after the
9716 trailing-return-type in case of decltype. */
9717 pop_bindings_and_leave_scope ();
9718 }
9719 else if (template_param_list != NULL_TREE) // generate diagnostic
9720 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
9721
9722 /* Create the function call operator.
9723
9724 Messing with declarators like this is no uglier than building up the
9725 FUNCTION_DECL by hand, and this is less likely to get out of sync with
9726 other code. */
9727 {
9728 cp_decl_specifier_seq return_type_specs;
9729 cp_declarator* declarator;
9730 tree fco;
9731 int quals;
9732 void *p;
9733
9734 clear_decl_specs (&return_type_specs);
9735 if (LAMBDA_EXPR_RETURN_TYPE (lambda_expr))
9736 return_type_specs.type = LAMBDA_EXPR_RETURN_TYPE (lambda_expr);
9737 else
9738 /* Maybe we will deduce the return type later. */
9739 return_type_specs.type = make_auto ();
9740
9741 p = obstack_alloc (&declarator_obstack, 0);
9742
9743 declarator = make_id_declarator (NULL_TREE, ansi_opname (CALL_EXPR),
9744 sfk_none);
9745
9746 quals = (LAMBDA_EXPR_MUTABLE_P (lambda_expr)
9747 ? TYPE_UNQUALIFIED : TYPE_QUAL_CONST);
9748 declarator = make_call_declarator (declarator, param_list, quals,
9749 VIRT_SPEC_UNSPECIFIED,
9750 REF_QUAL_NONE,
9751 tx_qual,
9752 exception_spec,
9753 /*late_return_type=*/NULL_TREE,
9754 /*requires_clause*/NULL_TREE);
9755 declarator->id_loc = LAMBDA_EXPR_LOCATION (lambda_expr);
9756
9757 fco = grokmethod (&return_type_specs,
9758 declarator,
9759 attributes);
9760 if (fco != error_mark_node)
9761 {
9762 DECL_INITIALIZED_IN_CLASS_P (fco) = 1;
9763 DECL_ARTIFICIAL (fco) = 1;
9764 /* Give the object parameter a different name. */
9765 DECL_NAME (DECL_ARGUMENTS (fco)) = get_identifier ("__closure");
9766 if (LAMBDA_EXPR_RETURN_TYPE (lambda_expr))
9767 TYPE_HAS_LATE_RETURN_TYPE (TREE_TYPE (fco)) = 1;
9768 }
9769 if (template_param_list)
9770 {
9771 fco = finish_member_template_decl (fco);
9772 finish_template_decl (template_param_list);
9773 --parser->num_template_parameter_lists;
9774 }
9775 else if (parser->fully_implicit_function_template_p)
9776 fco = finish_fully_implicit_template (parser, fco);
9777
9778 finish_member_declaration (fco);
9779
9780 obstack_free (&declarator_obstack, p);
9781
9782 return (fco != error_mark_node);
9783 }
9784 }
9785
9786 /* Parse the body of a lambda expression, which is simply
9787
9788 compound-statement
9789
9790 but which requires special handling.
9791 LAMBDA_EXPR is the current representation of the lambda expression. */
9792
9793 static void
9794 cp_parser_lambda_body (cp_parser* parser, tree lambda_expr)
9795 {
9796 bool nested = (current_function_decl != NULL_TREE);
9797 bool local_variables_forbidden_p = parser->local_variables_forbidden_p;
9798 if (nested)
9799 push_function_context ();
9800 else
9801 /* Still increment function_depth so that we don't GC in the
9802 middle of an expression. */
9803 ++function_depth;
9804 vec<tree> omp_privatization_save;
9805 save_omp_privatization_clauses (omp_privatization_save);
9806 /* Clear this in case we're in the middle of a default argument. */
9807 parser->local_variables_forbidden_p = false;
9808
9809 /* Finish the function call operator
9810 - class_specifier
9811 + late_parsing_for_member
9812 + function_definition_after_declarator
9813 + ctor_initializer_opt_and_function_body */
9814 {
9815 tree fco = lambda_function (lambda_expr);
9816 tree body;
9817 bool done = false;
9818 tree compound_stmt;
9819 tree cap;
9820
9821 /* Let the front end know that we are going to be defining this
9822 function. */
9823 start_preparsed_function (fco,
9824 NULL_TREE,
9825 SF_PRE_PARSED | SF_INCLASS_INLINE);
9826
9827 start_lambda_scope (fco);
9828 body = begin_function_body ();
9829
9830 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
9831 goto out;
9832
9833 /* Push the proxies for any explicit captures. */
9834 for (cap = LAMBDA_EXPR_CAPTURE_LIST (lambda_expr); cap;
9835 cap = TREE_CHAIN (cap))
9836 build_capture_proxy (TREE_PURPOSE (cap));
9837
9838 compound_stmt = begin_compound_stmt (0);
9839
9840 /* 5.1.1.4 of the standard says:
9841 If a lambda-expression does not include a trailing-return-type, it
9842 is as if the trailing-return-type denotes the following type:
9843 * if the compound-statement is of the form
9844 { return attribute-specifier [opt] expression ; }
9845 the type of the returned expression after lvalue-to-rvalue
9846 conversion (_conv.lval_ 4.1), array-to-pointer conversion
9847 (_conv.array_ 4.2), and function-to-pointer conversion
9848 (_conv.func_ 4.3);
9849 * otherwise, void. */
9850
9851 /* In a lambda that has neither a lambda-return-type-clause
9852 nor a deducible form, errors should be reported for return statements
9853 in the body. Since we used void as the placeholder return type, parsing
9854 the body as usual will give such desired behavior. */
9855 if (!LAMBDA_EXPR_RETURN_TYPE (lambda_expr)
9856 && cp_lexer_peek_nth_token (parser->lexer, 1)->keyword == RID_RETURN
9857 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SEMICOLON)
9858 {
9859 tree expr = NULL_TREE;
9860 cp_id_kind idk = CP_ID_KIND_NONE;
9861
9862 /* Parse tentatively in case there's more after the initial return
9863 statement. */
9864 cp_parser_parse_tentatively (parser);
9865
9866 cp_parser_require_keyword (parser, RID_RETURN, RT_RETURN);
9867
9868 expr = cp_parser_expression (parser, &idk);
9869
9870 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9871 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
9872
9873 if (cp_parser_parse_definitely (parser))
9874 {
9875 if (!processing_template_decl)
9876 {
9877 tree type = lambda_return_type (expr);
9878 apply_deduced_return_type (fco, type);
9879 if (type == error_mark_node)
9880 expr = error_mark_node;
9881 }
9882
9883 /* Will get error here if type not deduced yet. */
9884 finish_return_stmt (expr);
9885
9886 done = true;
9887 }
9888 }
9889
9890 if (!done)
9891 {
9892 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
9893 cp_parser_label_declaration (parser);
9894 cp_parser_statement_seq_opt (parser, NULL_TREE);
9895 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
9896 }
9897
9898 finish_compound_stmt (compound_stmt);
9899
9900 out:
9901 finish_function_body (body);
9902 finish_lambda_scope ();
9903
9904 /* Finish the function and generate code for it if necessary. */
9905 tree fn = finish_function (/*inline*/2);
9906
9907 /* Only expand if the call op is not a template. */
9908 if (!DECL_TEMPLATE_INFO (fco))
9909 expand_or_defer_fn (fn);
9910 }
9911
9912 restore_omp_privatization_clauses (omp_privatization_save);
9913 parser->local_variables_forbidden_p = local_variables_forbidden_p;
9914 if (nested)
9915 pop_function_context();
9916 else
9917 --function_depth;
9918 }
9919
9920 /* Statements [gram.stmt.stmt] */
9921
9922 /* Parse a statement.
9923
9924 statement:
9925 labeled-statement
9926 expression-statement
9927 compound-statement
9928 selection-statement
9929 iteration-statement
9930 jump-statement
9931 declaration-statement
9932 try-block
9933
9934 C++11:
9935
9936 statement:
9937 labeled-statement
9938 attribute-specifier-seq (opt) expression-statement
9939 attribute-specifier-seq (opt) compound-statement
9940 attribute-specifier-seq (opt) selection-statement
9941 attribute-specifier-seq (opt) iteration-statement
9942 attribute-specifier-seq (opt) jump-statement
9943 declaration-statement
9944 attribute-specifier-seq (opt) try-block
9945
9946 TM Extension:
9947
9948 statement:
9949 atomic-statement
9950
9951 IN_COMPOUND is true when the statement is nested inside a
9952 cp_parser_compound_statement; this matters for certain pragmas.
9953
9954 If IF_P is not NULL, *IF_P is set to indicate whether the statement
9955 is a (possibly labeled) if statement which is not enclosed in braces
9956 and has an else clause. This is used to implement -Wparentheses.
9957
9958 CHAIN is a vector of if-else-if conditions. */
9959
9960 static void
9961 cp_parser_statement (cp_parser* parser, tree in_statement_expr,
9962 bool in_compound, bool *if_p, vec<tree> *chain)
9963 {
9964 tree statement, std_attrs = NULL_TREE;
9965 cp_token *token;
9966 location_t statement_location, attrs_location;
9967
9968 restart:
9969 if (if_p != NULL)
9970 *if_p = false;
9971 /* There is no statement yet. */
9972 statement = NULL_TREE;
9973
9974 saved_token_sentinel saved_tokens (parser->lexer);
9975 attrs_location = cp_lexer_peek_token (parser->lexer)->location;
9976 if (c_dialect_objc ())
9977 /* In obj-c++, seeing '[[' might be the either the beginning of
9978 c++11 attributes, or a nested objc-message-expression. So
9979 let's parse the c++11 attributes tentatively. */
9980 cp_parser_parse_tentatively (parser);
9981 std_attrs = cp_parser_std_attribute_spec_seq (parser);
9982 if (c_dialect_objc ())
9983 {
9984 if (!cp_parser_parse_definitely (parser))
9985 std_attrs = NULL_TREE;
9986 }
9987
9988 /* Peek at the next token. */
9989 token = cp_lexer_peek_token (parser->lexer);
9990 /* Remember the location of the first token in the statement. */
9991 statement_location = token->location;
9992 /* If this is a keyword, then that will often determine what kind of
9993 statement we have. */
9994 if (token->type == CPP_KEYWORD)
9995 {
9996 enum rid keyword = token->keyword;
9997
9998 switch (keyword)
9999 {
10000 case RID_CASE:
10001 case RID_DEFAULT:
10002 /* Looks like a labeled-statement with a case label.
10003 Parse the label, and then use tail recursion to parse
10004 the statement. */
10005 cp_parser_label_for_labeled_statement (parser, std_attrs);
10006 in_compound = false;
10007 goto restart;
10008
10009 case RID_IF:
10010 case RID_SWITCH:
10011 statement = cp_parser_selection_statement (parser, if_p, chain);
10012 break;
10013
10014 case RID_WHILE:
10015 case RID_DO:
10016 case RID_FOR:
10017 statement = cp_parser_iteration_statement (parser, false);
10018 break;
10019
10020 case RID_CILK_FOR:
10021 if (!flag_cilkplus)
10022 {
10023 error_at (cp_lexer_peek_token (parser->lexer)->location,
10024 "-fcilkplus must be enabled to use %<_Cilk_for%>");
10025 cp_lexer_consume_token (parser->lexer);
10026 statement = error_mark_node;
10027 }
10028 else
10029 statement = cp_parser_cilk_for (parser, integer_zero_node);
10030 break;
10031
10032 case RID_BREAK:
10033 case RID_CONTINUE:
10034 case RID_RETURN:
10035 case RID_GOTO:
10036 statement = cp_parser_jump_statement (parser);
10037 break;
10038
10039 case RID_CILK_SYNC:
10040 cp_lexer_consume_token (parser->lexer);
10041 if (flag_cilkplus)
10042 {
10043 tree sync_expr = build_cilk_sync ();
10044 SET_EXPR_LOCATION (sync_expr,
10045 token->location);
10046 statement = finish_expr_stmt (sync_expr);
10047 }
10048 else
10049 {
10050 error_at (token->location, "-fcilkplus must be enabled to use"
10051 " %<_Cilk_sync%>");
10052 statement = error_mark_node;
10053 }
10054 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10055 break;
10056
10057 /* Objective-C++ exception-handling constructs. */
10058 case RID_AT_TRY:
10059 case RID_AT_CATCH:
10060 case RID_AT_FINALLY:
10061 case RID_AT_SYNCHRONIZED:
10062 case RID_AT_THROW:
10063 statement = cp_parser_objc_statement (parser);
10064 break;
10065
10066 case RID_TRY:
10067 statement = cp_parser_try_block (parser);
10068 break;
10069
10070 case RID_NAMESPACE:
10071 /* This must be a namespace alias definition. */
10072 cp_parser_declaration_statement (parser);
10073 return;
10074
10075 case RID_TRANSACTION_ATOMIC:
10076 case RID_TRANSACTION_RELAXED:
10077 case RID_SYNCHRONIZED:
10078 case RID_ATOMIC_NOEXCEPT:
10079 case RID_ATOMIC_CANCEL:
10080 statement = cp_parser_transaction (parser, token);
10081 break;
10082 case RID_TRANSACTION_CANCEL:
10083 statement = cp_parser_transaction_cancel (parser);
10084 break;
10085
10086 default:
10087 /* It might be a keyword like `int' that can start a
10088 declaration-statement. */
10089 break;
10090 }
10091 }
10092 else if (token->type == CPP_NAME)
10093 {
10094 /* If the next token is a `:', then we are looking at a
10095 labeled-statement. */
10096 token = cp_lexer_peek_nth_token (parser->lexer, 2);
10097 if (token->type == CPP_COLON)
10098 {
10099 /* Looks like a labeled-statement with an ordinary label.
10100 Parse the label, and then use tail recursion to parse
10101 the statement. */
10102
10103 cp_parser_label_for_labeled_statement (parser, std_attrs);
10104 in_compound = false;
10105 goto restart;
10106 }
10107 }
10108 /* Anything that starts with a `{' must be a compound-statement. */
10109 else if (token->type == CPP_OPEN_BRACE)
10110 statement = cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
10111 /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
10112 a statement all its own. */
10113 else if (token->type == CPP_PRAGMA)
10114 {
10115 /* Only certain OpenMP pragmas are attached to statements, and thus
10116 are considered statements themselves. All others are not. In
10117 the context of a compound, accept the pragma as a "statement" and
10118 return so that we can check for a close brace. Otherwise we
10119 require a real statement and must go back and read one. */
10120 if (in_compound)
10121 cp_parser_pragma (parser, pragma_compound);
10122 else if (!cp_parser_pragma (parser, pragma_stmt))
10123 goto restart;
10124 return;
10125 }
10126 else if (token->type == CPP_EOF)
10127 {
10128 cp_parser_error (parser, "expected statement");
10129 return;
10130 }
10131
10132 /* Everything else must be a declaration-statement or an
10133 expression-statement. Try for the declaration-statement
10134 first, unless we are looking at a `;', in which case we know that
10135 we have an expression-statement. */
10136 if (!statement)
10137 {
10138 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
10139 {
10140 if (std_attrs != NULL_TREE)
10141 {
10142 /* Attributes should be parsed as part of the the
10143 declaration, so let's un-parse them. */
10144 saved_tokens.rollback();
10145 std_attrs = NULL_TREE;
10146 }
10147
10148 cp_parser_parse_tentatively (parser);
10149 /* Try to parse the declaration-statement. */
10150 cp_parser_declaration_statement (parser);
10151 /* If that worked, we're done. */
10152 if (cp_parser_parse_definitely (parser))
10153 return;
10154 }
10155 /* Look for an expression-statement instead. */
10156 statement = cp_parser_expression_statement (parser, in_statement_expr);
10157 }
10158
10159 /* Set the line number for the statement. */
10160 if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
10161 SET_EXPR_LOCATION (statement, statement_location);
10162
10163 /* Note that for now, we don't do anything with c++11 statements
10164 parsed at this level. */
10165 if (std_attrs != NULL_TREE)
10166 warning_at (attrs_location,
10167 OPT_Wattributes,
10168 "attributes at the beginning of statement are ignored");
10169 }
10170
10171 /* Parse the label for a labeled-statement, i.e.
10172
10173 identifier :
10174 case constant-expression :
10175 default :
10176
10177 GNU Extension:
10178 case constant-expression ... constant-expression : statement
10179
10180 When a label is parsed without errors, the label is added to the
10181 parse tree by the finish_* functions, so this function doesn't
10182 have to return the label. */
10183
10184 static void
10185 cp_parser_label_for_labeled_statement (cp_parser* parser, tree attributes)
10186 {
10187 cp_token *token;
10188 tree label = NULL_TREE;
10189 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
10190
10191 /* The next token should be an identifier. */
10192 token = cp_lexer_peek_token (parser->lexer);
10193 if (token->type != CPP_NAME
10194 && token->type != CPP_KEYWORD)
10195 {
10196 cp_parser_error (parser, "expected labeled-statement");
10197 return;
10198 }
10199
10200 parser->colon_corrects_to_scope_p = false;
10201 switch (token->keyword)
10202 {
10203 case RID_CASE:
10204 {
10205 tree expr, expr_hi;
10206 cp_token *ellipsis;
10207
10208 /* Consume the `case' token. */
10209 cp_lexer_consume_token (parser->lexer);
10210 /* Parse the constant-expression. */
10211 expr = cp_parser_constant_expression (parser);
10212 if (check_for_bare_parameter_packs (expr))
10213 expr = error_mark_node;
10214
10215 ellipsis = cp_lexer_peek_token (parser->lexer);
10216 if (ellipsis->type == CPP_ELLIPSIS)
10217 {
10218 /* Consume the `...' token. */
10219 cp_lexer_consume_token (parser->lexer);
10220 expr_hi = cp_parser_constant_expression (parser);
10221 if (check_for_bare_parameter_packs (expr_hi))
10222 expr_hi = error_mark_node;
10223
10224 /* We don't need to emit warnings here, as the common code
10225 will do this for us. */
10226 }
10227 else
10228 expr_hi = NULL_TREE;
10229
10230 if (parser->in_switch_statement_p)
10231 finish_case_label (token->location, expr, expr_hi);
10232 else
10233 error_at (token->location,
10234 "case label %qE not within a switch statement",
10235 expr);
10236 }
10237 break;
10238
10239 case RID_DEFAULT:
10240 /* Consume the `default' token. */
10241 cp_lexer_consume_token (parser->lexer);
10242
10243 if (parser->in_switch_statement_p)
10244 finish_case_label (token->location, NULL_TREE, NULL_TREE);
10245 else
10246 error_at (token->location, "case label not within a switch statement");
10247 break;
10248
10249 default:
10250 /* Anything else must be an ordinary label. */
10251 label = finish_label_stmt (cp_parser_identifier (parser));
10252 break;
10253 }
10254
10255 /* Require the `:' token. */
10256 cp_parser_require (parser, CPP_COLON, RT_COLON);
10257
10258 /* An ordinary label may optionally be followed by attributes.
10259 However, this is only permitted if the attributes are then
10260 followed by a semicolon. This is because, for backward
10261 compatibility, when parsing
10262 lab: __attribute__ ((unused)) int i;
10263 we want the attribute to attach to "i", not "lab". */
10264 if (label != NULL_TREE
10265 && cp_next_tokens_can_be_gnu_attribute_p (parser))
10266 {
10267 tree attrs;
10268 cp_parser_parse_tentatively (parser);
10269 attrs = cp_parser_gnu_attributes_opt (parser);
10270 if (attrs == NULL_TREE
10271 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
10272 cp_parser_abort_tentative_parse (parser);
10273 else if (!cp_parser_parse_definitely (parser))
10274 ;
10275 else
10276 attributes = chainon (attributes, attrs);
10277 }
10278
10279 if (attributes != NULL_TREE)
10280 cplus_decl_attributes (&label, attributes, 0);
10281
10282 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
10283 }
10284
10285 /* Parse an expression-statement.
10286
10287 expression-statement:
10288 expression [opt] ;
10289
10290 Returns the new EXPR_STMT -- or NULL_TREE if the expression
10291 statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
10292 indicates whether this expression-statement is part of an
10293 expression statement. */
10294
10295 static tree
10296 cp_parser_expression_statement (cp_parser* parser, tree in_statement_expr)
10297 {
10298 tree statement = NULL_TREE;
10299 cp_token *token = cp_lexer_peek_token (parser->lexer);
10300
10301 /* If the next token is a ';', then there is no expression
10302 statement. */
10303 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
10304 {
10305 statement = cp_parser_expression (parser);
10306 if (statement == error_mark_node
10307 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
10308 {
10309 cp_parser_skip_to_end_of_block_or_statement (parser);
10310 return error_mark_node;
10311 }
10312 }
10313
10314 /* Give a helpful message for "A<T>::type t;" and the like. */
10315 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
10316 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
10317 {
10318 if (TREE_CODE (statement) == SCOPE_REF)
10319 error_at (token->location, "need %<typename%> before %qE because "
10320 "%qT is a dependent scope",
10321 statement, TREE_OPERAND (statement, 0));
10322 else if (is_overloaded_fn (statement)
10323 && DECL_CONSTRUCTOR_P (get_first_fn (statement)))
10324 {
10325 /* A::A a; */
10326 tree fn = get_first_fn (statement);
10327 error_at (token->location,
10328 "%<%T::%D%> names the constructor, not the type",
10329 DECL_CONTEXT (fn), DECL_NAME (fn));
10330 }
10331 }
10332
10333 /* Consume the final `;'. */
10334 cp_parser_consume_semicolon_at_end_of_statement (parser);
10335
10336 if (in_statement_expr
10337 && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
10338 /* This is the final expression statement of a statement
10339 expression. */
10340 statement = finish_stmt_expr_expr (statement, in_statement_expr);
10341 else if (statement)
10342 statement = finish_expr_stmt (statement);
10343
10344 return statement;
10345 }
10346
10347 /* Parse a compound-statement.
10348
10349 compound-statement:
10350 { statement-seq [opt] }
10351
10352 GNU extension:
10353
10354 compound-statement:
10355 { label-declaration-seq [opt] statement-seq [opt] }
10356
10357 label-declaration-seq:
10358 label-declaration
10359 label-declaration-seq label-declaration
10360
10361 Returns a tree representing the statement. */
10362
10363 static tree
10364 cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr,
10365 int bcs_flags, bool function_body)
10366 {
10367 tree compound_stmt;
10368
10369 /* Consume the `{'. */
10370 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
10371 return error_mark_node;
10372 if (DECL_DECLARED_CONSTEXPR_P (current_function_decl)
10373 && !function_body && cxx_dialect < cxx14)
10374 pedwarn (input_location, OPT_Wpedantic,
10375 "compound-statement in constexpr function");
10376 /* Begin the compound-statement. */
10377 compound_stmt = begin_compound_stmt (bcs_flags);
10378 /* If the next keyword is `__label__' we have a label declaration. */
10379 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
10380 cp_parser_label_declaration (parser);
10381 /* Parse an (optional) statement-seq. */
10382 cp_parser_statement_seq_opt (parser, in_statement_expr);
10383 /* Finish the compound-statement. */
10384 finish_compound_stmt (compound_stmt);
10385 /* Consume the `}'. */
10386 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
10387
10388 return compound_stmt;
10389 }
10390
10391 /* Parse an (optional) statement-seq.
10392
10393 statement-seq:
10394 statement
10395 statement-seq [opt] statement */
10396
10397 static void
10398 cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr)
10399 {
10400 /* Scan statements until there aren't any more. */
10401 while (true)
10402 {
10403 cp_token *token = cp_lexer_peek_token (parser->lexer);
10404
10405 /* If we are looking at a `}', then we have run out of
10406 statements; the same is true if we have reached the end
10407 of file, or have stumbled upon a stray '@end'. */
10408 if (token->type == CPP_CLOSE_BRACE
10409 || token->type == CPP_EOF
10410 || token->type == CPP_PRAGMA_EOL
10411 || (token->type == CPP_KEYWORD && token->keyword == RID_AT_END))
10412 break;
10413
10414 /* If we are in a compound statement and find 'else' then
10415 something went wrong. */
10416 else if (token->type == CPP_KEYWORD && token->keyword == RID_ELSE)
10417 {
10418 if (parser->in_statement & IN_IF_STMT)
10419 break;
10420 else
10421 {
10422 token = cp_lexer_consume_token (parser->lexer);
10423 error_at (token->location, "%<else%> without a previous %<if%>");
10424 }
10425 }
10426
10427 /* Parse the statement. */
10428 cp_parser_statement (parser, in_statement_expr, true, NULL);
10429 }
10430 }
10431
10432 /* Parse a selection-statement.
10433
10434 selection-statement:
10435 if ( condition ) statement
10436 if ( condition ) statement else statement
10437 switch ( condition ) statement
10438
10439 Returns the new IF_STMT or SWITCH_STMT.
10440
10441 If IF_P is not NULL, *IF_P is set to indicate whether the statement
10442 is a (possibly labeled) if statement which is not enclosed in
10443 braces and has an else clause. This is used to implement
10444 -Wparentheses.
10445
10446 CHAIN is a vector of if-else-if conditions. This is used to implement
10447 -Wduplicated-cond. */
10448
10449 static tree
10450 cp_parser_selection_statement (cp_parser* parser, bool *if_p,
10451 vec<tree> *chain)
10452 {
10453 cp_token *token;
10454 enum rid keyword;
10455 token_indent_info guard_tinfo;
10456
10457 if (if_p != NULL)
10458 *if_p = false;
10459
10460 /* Peek at the next token. */
10461 token = cp_parser_require (parser, CPP_KEYWORD, RT_SELECT);
10462 guard_tinfo = get_token_indent_info (token);
10463
10464 /* See what kind of keyword it is. */
10465 keyword = token->keyword;
10466 switch (keyword)
10467 {
10468 case RID_IF:
10469 case RID_SWITCH:
10470 {
10471 tree statement;
10472 tree condition;
10473
10474 /* Look for the `('. */
10475 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
10476 {
10477 cp_parser_skip_to_end_of_statement (parser);
10478 return error_mark_node;
10479 }
10480
10481 /* Begin the selection-statement. */
10482 if (keyword == RID_IF)
10483 statement = begin_if_stmt ();
10484 else
10485 statement = begin_switch_stmt ();
10486
10487 /* Parse the condition. */
10488 condition = cp_parser_condition (parser);
10489 /* Look for the `)'. */
10490 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
10491 cp_parser_skip_to_closing_parenthesis (parser, true, false,
10492 /*consume_paren=*/true);
10493
10494 if (keyword == RID_IF)
10495 {
10496 bool nested_if;
10497 unsigned char in_statement;
10498
10499 /* Add the condition. */
10500 finish_if_stmt_cond (condition, statement);
10501
10502 if (warn_duplicated_cond)
10503 warn_duplicated_cond_add_or_warn (token->location, condition,
10504 &chain);
10505
10506 /* Parse the then-clause. */
10507 in_statement = parser->in_statement;
10508 parser->in_statement |= IN_IF_STMT;
10509 cp_parser_implicitly_scoped_statement (parser, &nested_if,
10510 guard_tinfo);
10511 parser->in_statement = in_statement;
10512
10513 finish_then_clause (statement);
10514
10515 /* If the next token is `else', parse the else-clause. */
10516 if (cp_lexer_next_token_is_keyword (parser->lexer,
10517 RID_ELSE))
10518 {
10519 guard_tinfo
10520 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
10521 /* Consume the `else' keyword. */
10522 cp_lexer_consume_token (parser->lexer);
10523 if (warn_duplicated_cond)
10524 {
10525 if (cp_lexer_next_token_is_keyword (parser->lexer,
10526 RID_IF)
10527 && chain == NULL)
10528 {
10529 /* We've got "if (COND) else if (COND2)". Start
10530 the condition chain and add COND as the first
10531 element. */
10532 chain = new vec<tree> ();
10533 if (!CONSTANT_CLASS_P (condition)
10534 && !TREE_SIDE_EFFECTS (condition))
10535 {
10536 /* Wrap it in a NOP_EXPR so that we can set the
10537 location of the condition. */
10538 tree e = build1 (NOP_EXPR, TREE_TYPE (condition),
10539 condition);
10540 SET_EXPR_LOCATION (e, token->location);
10541 chain->safe_push (e);
10542 }
10543 }
10544 else if (!cp_lexer_next_token_is_keyword (parser->lexer,
10545 RID_IF))
10546 {
10547 /* This is if-else without subsequent if. Zap the
10548 condition chain; we would have already warned at
10549 this point. */
10550 delete chain;
10551 chain = NULL;
10552 }
10553 }
10554 begin_else_clause (statement);
10555 /* Parse the else-clause. */
10556 cp_parser_implicitly_scoped_statement (parser, NULL,
10557 guard_tinfo, chain);
10558
10559 finish_else_clause (statement);
10560
10561 /* If we are currently parsing a then-clause, then
10562 IF_P will not be NULL. We set it to true to
10563 indicate that this if statement has an else clause.
10564 This may trigger the Wparentheses warning below
10565 when we get back up to the parent if statement. */
10566 if (if_p != NULL)
10567 *if_p = true;
10568 }
10569 else
10570 {
10571 /* This if statement does not have an else clause. If
10572 NESTED_IF is true, then the then-clause is an if
10573 statement which does have an else clause. We warn
10574 about the potential ambiguity. */
10575 if (nested_if)
10576 warning_at (EXPR_LOCATION (statement), OPT_Wparentheses,
10577 "suggest explicit braces to avoid ambiguous"
10578 " %<else%>");
10579 if (warn_duplicated_cond)
10580 {
10581 /* We don't need the condition chain anymore. */
10582 delete chain;
10583 chain = NULL;
10584 }
10585 }
10586
10587 /* Now we're all done with the if-statement. */
10588 finish_if_stmt (statement);
10589 }
10590 else
10591 {
10592 bool in_switch_statement_p;
10593 unsigned char in_statement;
10594
10595 /* Add the condition. */
10596 finish_switch_cond (condition, statement);
10597
10598 /* Parse the body of the switch-statement. */
10599 in_switch_statement_p = parser->in_switch_statement_p;
10600 in_statement = parser->in_statement;
10601 parser->in_switch_statement_p = true;
10602 parser->in_statement |= IN_SWITCH_STMT;
10603 cp_parser_implicitly_scoped_statement (parser, NULL,
10604 guard_tinfo);
10605 parser->in_switch_statement_p = in_switch_statement_p;
10606 parser->in_statement = in_statement;
10607
10608 /* Now we're all done with the switch-statement. */
10609 finish_switch_stmt (statement);
10610 }
10611
10612 return statement;
10613 }
10614 break;
10615
10616 default:
10617 cp_parser_error (parser, "expected selection-statement");
10618 return error_mark_node;
10619 }
10620 }
10621
10622 /* Parse a condition.
10623
10624 condition:
10625 expression
10626 type-specifier-seq declarator = initializer-clause
10627 type-specifier-seq declarator braced-init-list
10628
10629 GNU Extension:
10630
10631 condition:
10632 type-specifier-seq declarator asm-specification [opt]
10633 attributes [opt] = assignment-expression
10634
10635 Returns the expression that should be tested. */
10636
10637 static tree
10638 cp_parser_condition (cp_parser* parser)
10639 {
10640 cp_decl_specifier_seq type_specifiers;
10641 const char *saved_message;
10642 int declares_class_or_enum;
10643
10644 /* Try the declaration first. */
10645 cp_parser_parse_tentatively (parser);
10646 /* New types are not allowed in the type-specifier-seq for a
10647 condition. */
10648 saved_message = parser->type_definition_forbidden_message;
10649 parser->type_definition_forbidden_message
10650 = G_("types may not be defined in conditions");
10651 /* Parse the type-specifier-seq. */
10652 cp_parser_decl_specifier_seq (parser,
10653 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR,
10654 &type_specifiers,
10655 &declares_class_or_enum);
10656 /* Restore the saved message. */
10657 parser->type_definition_forbidden_message = saved_message;
10658 /* If all is well, we might be looking at a declaration. */
10659 if (!cp_parser_error_occurred (parser))
10660 {
10661 tree decl;
10662 tree asm_specification;
10663 tree attributes;
10664 cp_declarator *declarator;
10665 tree initializer = NULL_TREE;
10666
10667 /* Parse the declarator. */
10668 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
10669 /*ctor_dtor_or_conv_p=*/NULL,
10670 /*parenthesized_p=*/NULL,
10671 /*member_p=*/false,
10672 /*friend_p=*/false);
10673 /* Parse the attributes. */
10674 attributes = cp_parser_attributes_opt (parser);
10675 /* Parse the asm-specification. */
10676 asm_specification = cp_parser_asm_specification_opt (parser);
10677 /* If the next token is not an `=' or '{', then we might still be
10678 looking at an expression. For example:
10679
10680 if (A(a).x)
10681
10682 looks like a decl-specifier-seq and a declarator -- but then
10683 there is no `=', so this is an expression. */
10684 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
10685 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
10686 cp_parser_simulate_error (parser);
10687
10688 /* If we did see an `=' or '{', then we are looking at a declaration
10689 for sure. */
10690 if (cp_parser_parse_definitely (parser))
10691 {
10692 tree pushed_scope;
10693 bool non_constant_p;
10694 bool flags = LOOKUP_ONLYCONVERTING;
10695
10696 /* Create the declaration. */
10697 decl = start_decl (declarator, &type_specifiers,
10698 /*initialized_p=*/true,
10699 attributes, /*prefix_attributes=*/NULL_TREE,
10700 &pushed_scope);
10701
10702 /* Parse the initializer. */
10703 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
10704 {
10705 initializer = cp_parser_braced_list (parser, &non_constant_p);
10706 CONSTRUCTOR_IS_DIRECT_INIT (initializer) = 1;
10707 flags = 0;
10708 }
10709 else
10710 {
10711 /* Consume the `='. */
10712 cp_parser_require (parser, CPP_EQ, RT_EQ);
10713 initializer = cp_parser_initializer_clause (parser, &non_constant_p);
10714 }
10715 if (BRACE_ENCLOSED_INITIALIZER_P (initializer))
10716 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
10717
10718 /* Process the initializer. */
10719 cp_finish_decl (decl,
10720 initializer, !non_constant_p,
10721 asm_specification,
10722 flags);
10723
10724 if (pushed_scope)
10725 pop_scope (pushed_scope);
10726
10727 return convert_from_reference (decl);
10728 }
10729 }
10730 /* If we didn't even get past the declarator successfully, we are
10731 definitely not looking at a declaration. */
10732 else
10733 cp_parser_abort_tentative_parse (parser);
10734
10735 /* Otherwise, we are looking at an expression. */
10736 return cp_parser_expression (parser);
10737 }
10738
10739 /* Parses a for-statement or range-for-statement until the closing ')',
10740 not included. */
10741
10742 static tree
10743 cp_parser_for (cp_parser *parser, bool ivdep)
10744 {
10745 tree init, scope, decl;
10746 bool is_range_for;
10747
10748 /* Begin the for-statement. */
10749 scope = begin_for_scope (&init);
10750
10751 /* Parse the initialization. */
10752 is_range_for = cp_parser_for_init_statement (parser, &decl);
10753
10754 if (is_range_for)
10755 return cp_parser_range_for (parser, scope, init, decl, ivdep);
10756 else
10757 return cp_parser_c_for (parser, scope, init, ivdep);
10758 }
10759
10760 static tree
10761 cp_parser_c_for (cp_parser *parser, tree scope, tree init, bool ivdep)
10762 {
10763 /* Normal for loop */
10764 tree condition = NULL_TREE;
10765 tree expression = NULL_TREE;
10766 tree stmt;
10767
10768 stmt = begin_for_stmt (scope, init);
10769 /* The for-init-statement has already been parsed in
10770 cp_parser_for_init_statement, so no work is needed here. */
10771 finish_for_init_stmt (stmt);
10772
10773 /* If there's a condition, process it. */
10774 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
10775 condition = cp_parser_condition (parser);
10776 else if (ivdep)
10777 {
10778 cp_parser_error (parser, "missing loop condition in loop with "
10779 "%<GCC ivdep%> pragma");
10780 condition = error_mark_node;
10781 }
10782 finish_for_cond (condition, stmt, ivdep);
10783 /* Look for the `;'. */
10784 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10785
10786 /* If there's an expression, process it. */
10787 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
10788 expression = cp_parser_expression (parser);
10789 finish_for_expr (expression, stmt);
10790
10791 return stmt;
10792 }
10793
10794 /* Tries to parse a range-based for-statement:
10795
10796 range-based-for:
10797 decl-specifier-seq declarator : expression
10798
10799 The decl-specifier-seq declarator and the `:' are already parsed by
10800 cp_parser_for_init_statement. If processing_template_decl it returns a
10801 newly created RANGE_FOR_STMT; if not, it is converted to a
10802 regular FOR_STMT. */
10803
10804 static tree
10805 cp_parser_range_for (cp_parser *parser, tree scope, tree init, tree range_decl,
10806 bool ivdep)
10807 {
10808 tree stmt, range_expr;
10809
10810 /* Get the range declaration momentarily out of the way so that
10811 the range expression doesn't clash with it. */
10812 if (range_decl != error_mark_node)
10813 pop_binding (DECL_NAME (range_decl), range_decl);
10814
10815 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
10816 {
10817 bool expr_non_constant_p;
10818 range_expr = cp_parser_braced_list (parser, &expr_non_constant_p);
10819 }
10820 else
10821 range_expr = cp_parser_expression (parser);
10822
10823 /* Put the range declaration back into scope. */
10824 if (range_decl != error_mark_node)
10825 push_binding (DECL_NAME (range_decl), range_decl, current_binding_level);
10826
10827 /* If in template, STMT is converted to a normal for-statement
10828 at instantiation. If not, it is done just ahead. */
10829 if (processing_template_decl)
10830 {
10831 if (check_for_bare_parameter_packs (range_expr))
10832 range_expr = error_mark_node;
10833 stmt = begin_range_for_stmt (scope, init);
10834 if (ivdep)
10835 RANGE_FOR_IVDEP (stmt) = 1;
10836 finish_range_for_decl (stmt, range_decl, range_expr);
10837 if (!type_dependent_expression_p (range_expr)
10838 /* do_auto_deduction doesn't mess with template init-lists. */
10839 && !BRACE_ENCLOSED_INITIALIZER_P (range_expr))
10840 do_range_for_auto_deduction (range_decl, range_expr);
10841 }
10842 else
10843 {
10844 stmt = begin_for_stmt (scope, init);
10845 stmt = cp_convert_range_for (stmt, range_decl, range_expr, ivdep);
10846 }
10847 return stmt;
10848 }
10849
10850 /* Subroutine of cp_convert_range_for: given the initializer expression,
10851 builds up the range temporary. */
10852
10853 static tree
10854 build_range_temp (tree range_expr)
10855 {
10856 tree range_type, range_temp;
10857
10858 /* Find out the type deduced by the declaration
10859 `auto &&__range = range_expr'. */
10860 range_type = cp_build_reference_type (make_auto (), true);
10861 range_type = do_auto_deduction (range_type, range_expr,
10862 type_uses_auto (range_type));
10863
10864 /* Create the __range variable. */
10865 range_temp = build_decl (input_location, VAR_DECL,
10866 get_identifier ("__for_range"), range_type);
10867 TREE_USED (range_temp) = 1;
10868 DECL_ARTIFICIAL (range_temp) = 1;
10869
10870 return range_temp;
10871 }
10872
10873 /* Used by cp_parser_range_for in template context: we aren't going to
10874 do a full conversion yet, but we still need to resolve auto in the
10875 type of the for-range-declaration if present. This is basically
10876 a shortcut version of cp_convert_range_for. */
10877
10878 static void
10879 do_range_for_auto_deduction (tree decl, tree range_expr)
10880 {
10881 tree auto_node = type_uses_auto (TREE_TYPE (decl));
10882 if (auto_node)
10883 {
10884 tree begin_dummy, end_dummy, range_temp, iter_type, iter_decl;
10885 range_temp = convert_from_reference (build_range_temp (range_expr));
10886 iter_type = (cp_parser_perform_range_for_lookup
10887 (range_temp, &begin_dummy, &end_dummy));
10888 if (iter_type)
10889 {
10890 iter_decl = build_decl (input_location, VAR_DECL, NULL_TREE,
10891 iter_type);
10892 iter_decl = build_x_indirect_ref (input_location, iter_decl, RO_NULL,
10893 tf_warning_or_error);
10894 TREE_TYPE (decl) = do_auto_deduction (TREE_TYPE (decl),
10895 iter_decl, auto_node);
10896 }
10897 }
10898 }
10899
10900 /* Converts a range-based for-statement into a normal
10901 for-statement, as per the definition.
10902
10903 for (RANGE_DECL : RANGE_EXPR)
10904 BLOCK
10905
10906 should be equivalent to:
10907
10908 {
10909 auto &&__range = RANGE_EXPR;
10910 for (auto __begin = BEGIN_EXPR, end = END_EXPR;
10911 __begin != __end;
10912 ++__begin)
10913 {
10914 RANGE_DECL = *__begin;
10915 BLOCK
10916 }
10917 }
10918
10919 If RANGE_EXPR is an array:
10920 BEGIN_EXPR = __range
10921 END_EXPR = __range + ARRAY_SIZE(__range)
10922 Else if RANGE_EXPR has a member 'begin' or 'end':
10923 BEGIN_EXPR = __range.begin()
10924 END_EXPR = __range.end()
10925 Else:
10926 BEGIN_EXPR = begin(__range)
10927 END_EXPR = end(__range);
10928
10929 If __range has a member 'begin' but not 'end', or vice versa, we must
10930 still use the second alternative (it will surely fail, however).
10931 When calling begin()/end() in the third alternative we must use
10932 argument dependent lookup, but always considering 'std' as an associated
10933 namespace. */
10934
10935 tree
10936 cp_convert_range_for (tree statement, tree range_decl, tree range_expr,
10937 bool ivdep)
10938 {
10939 tree begin, end;
10940 tree iter_type, begin_expr, end_expr;
10941 tree condition, expression;
10942
10943 if (range_decl == error_mark_node || range_expr == error_mark_node)
10944 /* If an error happened previously do nothing or else a lot of
10945 unhelpful errors would be issued. */
10946 begin_expr = end_expr = iter_type = error_mark_node;
10947 else
10948 {
10949 tree range_temp;
10950
10951 if (VAR_P (range_expr)
10952 && array_of_runtime_bound_p (TREE_TYPE (range_expr)))
10953 /* Can't bind a reference to an array of runtime bound. */
10954 range_temp = range_expr;
10955 else
10956 {
10957 range_temp = build_range_temp (range_expr);
10958 pushdecl (range_temp);
10959 cp_finish_decl (range_temp, range_expr,
10960 /*is_constant_init*/false, NULL_TREE,
10961 LOOKUP_ONLYCONVERTING);
10962 range_temp = convert_from_reference (range_temp);
10963 }
10964 iter_type = cp_parser_perform_range_for_lookup (range_temp,
10965 &begin_expr, &end_expr);
10966 }
10967
10968 /* The new for initialization statement. */
10969 begin = build_decl (input_location, VAR_DECL,
10970 get_identifier ("__for_begin"), iter_type);
10971 TREE_USED (begin) = 1;
10972 DECL_ARTIFICIAL (begin) = 1;
10973 pushdecl (begin);
10974 cp_finish_decl (begin, begin_expr,
10975 /*is_constant_init*/false, NULL_TREE,
10976 LOOKUP_ONLYCONVERTING);
10977
10978 end = build_decl (input_location, VAR_DECL,
10979 get_identifier ("__for_end"), iter_type);
10980 TREE_USED (end) = 1;
10981 DECL_ARTIFICIAL (end) = 1;
10982 pushdecl (end);
10983 cp_finish_decl (end, end_expr,
10984 /*is_constant_init*/false, NULL_TREE,
10985 LOOKUP_ONLYCONVERTING);
10986
10987 finish_for_init_stmt (statement);
10988
10989 /* The new for condition. */
10990 condition = build_x_binary_op (input_location, NE_EXPR,
10991 begin, ERROR_MARK,
10992 end, ERROR_MARK,
10993 NULL, tf_warning_or_error);
10994 finish_for_cond (condition, statement, ivdep);
10995
10996 /* The new increment expression. */
10997 expression = finish_unary_op_expr (input_location,
10998 PREINCREMENT_EXPR, begin,
10999 tf_warning_or_error);
11000 finish_for_expr (expression, statement);
11001
11002 /* The declaration is initialized with *__begin inside the loop body. */
11003 cp_finish_decl (range_decl,
11004 build_x_indirect_ref (input_location, begin, RO_NULL,
11005 tf_warning_or_error),
11006 /*is_constant_init*/false, NULL_TREE,
11007 LOOKUP_ONLYCONVERTING);
11008
11009 return statement;
11010 }
11011
11012 /* Solves BEGIN_EXPR and END_EXPR as described in cp_convert_range_for.
11013 We need to solve both at the same time because the method used
11014 depends on the existence of members begin or end.
11015 Returns the type deduced for the iterator expression. */
11016
11017 static tree
11018 cp_parser_perform_range_for_lookup (tree range, tree *begin, tree *end)
11019 {
11020 if (error_operand_p (range))
11021 {
11022 *begin = *end = error_mark_node;
11023 return error_mark_node;
11024 }
11025
11026 if (!COMPLETE_TYPE_P (complete_type (TREE_TYPE (range))))
11027 {
11028 error ("range-based %<for%> expression of type %qT "
11029 "has incomplete type", TREE_TYPE (range));
11030 *begin = *end = error_mark_node;
11031 return error_mark_node;
11032 }
11033 if (TREE_CODE (TREE_TYPE (range)) == ARRAY_TYPE)
11034 {
11035 /* If RANGE is an array, we will use pointer arithmetic. */
11036 *begin = range;
11037 *end = build_binary_op (input_location, PLUS_EXPR,
11038 range,
11039 array_type_nelts_top (TREE_TYPE (range)),
11040 0);
11041 return build_pointer_type (TREE_TYPE (TREE_TYPE (range)));
11042 }
11043 else
11044 {
11045 /* If it is not an array, we must do a bit of magic. */
11046 tree id_begin, id_end;
11047 tree member_begin, member_end;
11048
11049 *begin = *end = error_mark_node;
11050
11051 id_begin = get_identifier ("begin");
11052 id_end = get_identifier ("end");
11053 member_begin = lookup_member (TREE_TYPE (range), id_begin,
11054 /*protect=*/2, /*want_type=*/false,
11055 tf_warning_or_error);
11056 member_end = lookup_member (TREE_TYPE (range), id_end,
11057 /*protect=*/2, /*want_type=*/false,
11058 tf_warning_or_error);
11059
11060 if (member_begin != NULL_TREE || member_end != NULL_TREE)
11061 {
11062 /* Use the member functions. */
11063 if (member_begin != NULL_TREE)
11064 *begin = cp_parser_range_for_member_function (range, id_begin);
11065 else
11066 error ("range-based %<for%> expression of type %qT has an "
11067 "%<end%> member but not a %<begin%>", TREE_TYPE (range));
11068
11069 if (member_end != NULL_TREE)
11070 *end = cp_parser_range_for_member_function (range, id_end);
11071 else
11072 error ("range-based %<for%> expression of type %qT has a "
11073 "%<begin%> member but not an %<end%>", TREE_TYPE (range));
11074 }
11075 else
11076 {
11077 /* Use global functions with ADL. */
11078 vec<tree, va_gc> *vec;
11079 vec = make_tree_vector ();
11080
11081 vec_safe_push (vec, range);
11082
11083 member_begin = perform_koenig_lookup (id_begin, vec,
11084 tf_warning_or_error);
11085 *begin = finish_call_expr (member_begin, &vec, false, true,
11086 tf_warning_or_error);
11087 member_end = perform_koenig_lookup (id_end, vec,
11088 tf_warning_or_error);
11089 *end = finish_call_expr (member_end, &vec, false, true,
11090 tf_warning_or_error);
11091
11092 release_tree_vector (vec);
11093 }
11094
11095 /* Last common checks. */
11096 if (*begin == error_mark_node || *end == error_mark_node)
11097 {
11098 /* If one of the expressions is an error do no more checks. */
11099 *begin = *end = error_mark_node;
11100 return error_mark_node;
11101 }
11102 else if (type_dependent_expression_p (*begin)
11103 || type_dependent_expression_p (*end))
11104 /* Can happen, when, eg, in a template context, Koenig lookup
11105 can't resolve begin/end (c++/58503). */
11106 return NULL_TREE;
11107 else
11108 {
11109 tree iter_type = cv_unqualified (TREE_TYPE (*begin));
11110 /* The unqualified type of the __begin and __end temporaries should
11111 be the same, as required by the multiple auto declaration. */
11112 if (!same_type_p (iter_type, cv_unqualified (TREE_TYPE (*end))))
11113 error ("inconsistent begin/end types in range-based %<for%> "
11114 "statement: %qT and %qT",
11115 TREE_TYPE (*begin), TREE_TYPE (*end));
11116 return iter_type;
11117 }
11118 }
11119 }
11120
11121 /* Helper function for cp_parser_perform_range_for_lookup.
11122 Builds a tree for RANGE.IDENTIFIER(). */
11123
11124 static tree
11125 cp_parser_range_for_member_function (tree range, tree identifier)
11126 {
11127 tree member, res;
11128 vec<tree, va_gc> *vec;
11129
11130 member = finish_class_member_access_expr (range, identifier,
11131 false, tf_warning_or_error);
11132 if (member == error_mark_node)
11133 return error_mark_node;
11134
11135 vec = make_tree_vector ();
11136 res = finish_call_expr (member, &vec,
11137 /*disallow_virtual=*/false,
11138 /*koenig_p=*/false,
11139 tf_warning_or_error);
11140 release_tree_vector (vec);
11141 return res;
11142 }
11143
11144 /* Parse an iteration-statement.
11145
11146 iteration-statement:
11147 while ( condition ) statement
11148 do statement while ( expression ) ;
11149 for ( for-init-statement condition [opt] ; expression [opt] )
11150 statement
11151
11152 Returns the new WHILE_STMT, DO_STMT, FOR_STMT or RANGE_FOR_STMT. */
11153
11154 static tree
11155 cp_parser_iteration_statement (cp_parser* parser, bool ivdep)
11156 {
11157 cp_token *token;
11158 enum rid keyword;
11159 tree statement;
11160 unsigned char in_statement;
11161 token_indent_info guard_tinfo;
11162
11163 /* Peek at the next token. */
11164 token = cp_parser_require (parser, CPP_KEYWORD, RT_INTERATION);
11165 if (!token)
11166 return error_mark_node;
11167
11168 guard_tinfo = get_token_indent_info (token);
11169
11170 /* Remember whether or not we are already within an iteration
11171 statement. */
11172 in_statement = parser->in_statement;
11173
11174 /* See what kind of keyword it is. */
11175 keyword = token->keyword;
11176 switch (keyword)
11177 {
11178 case RID_WHILE:
11179 {
11180 tree condition;
11181
11182 /* Begin the while-statement. */
11183 statement = begin_while_stmt ();
11184 /* Look for the `('. */
11185 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
11186 /* Parse the condition. */
11187 condition = cp_parser_condition (parser);
11188 finish_while_stmt_cond (condition, statement, ivdep);
11189 /* Look for the `)'. */
11190 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
11191 /* Parse the dependent statement. */
11192 parser->in_statement = IN_ITERATION_STMT;
11193 cp_parser_already_scoped_statement (parser, guard_tinfo);
11194 parser->in_statement = in_statement;
11195 /* We're done with the while-statement. */
11196 finish_while_stmt (statement);
11197 }
11198 break;
11199
11200 case RID_DO:
11201 {
11202 tree expression;
11203
11204 /* Begin the do-statement. */
11205 statement = begin_do_stmt ();
11206 /* Parse the body of the do-statement. */
11207 parser->in_statement = IN_ITERATION_STMT;
11208 cp_parser_implicitly_scoped_statement (parser, NULL, guard_tinfo);
11209 parser->in_statement = in_statement;
11210 finish_do_body (statement);
11211 /* Look for the `while' keyword. */
11212 cp_parser_require_keyword (parser, RID_WHILE, RT_WHILE);
11213 /* Look for the `('. */
11214 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
11215 /* Parse the expression. */
11216 expression = cp_parser_expression (parser);
11217 /* We're done with the do-statement. */
11218 finish_do_stmt (expression, statement, ivdep);
11219 /* Look for the `)'. */
11220 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
11221 /* Look for the `;'. */
11222 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
11223 }
11224 break;
11225
11226 case RID_FOR:
11227 {
11228 /* Look for the `('. */
11229 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
11230
11231 statement = cp_parser_for (parser, ivdep);
11232
11233 /* Look for the `)'. */
11234 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
11235
11236 /* Parse the body of the for-statement. */
11237 parser->in_statement = IN_ITERATION_STMT;
11238 cp_parser_already_scoped_statement (parser, guard_tinfo);
11239 parser->in_statement = in_statement;
11240
11241 /* We're done with the for-statement. */
11242 finish_for_stmt (statement);
11243 }
11244 break;
11245
11246 default:
11247 cp_parser_error (parser, "expected iteration-statement");
11248 statement = error_mark_node;
11249 break;
11250 }
11251
11252 return statement;
11253 }
11254
11255 /* Parse a for-init-statement or the declarator of a range-based-for.
11256 Returns true if a range-based-for declaration is seen.
11257
11258 for-init-statement:
11259 expression-statement
11260 simple-declaration */
11261
11262 static bool
11263 cp_parser_for_init_statement (cp_parser* parser, tree *decl)
11264 {
11265 /* If the next token is a `;', then we have an empty
11266 expression-statement. Grammatically, this is also a
11267 simple-declaration, but an invalid one, because it does not
11268 declare anything. Therefore, if we did not handle this case
11269 specially, we would issue an error message about an invalid
11270 declaration. */
11271 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
11272 {
11273 bool is_range_for = false;
11274 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
11275
11276 /* A colon is used in range-based for. */
11277 parser->colon_corrects_to_scope_p = false;
11278
11279 /* We're going to speculatively look for a declaration, falling back
11280 to an expression, if necessary. */
11281 cp_parser_parse_tentatively (parser);
11282 /* Parse the declaration. */
11283 cp_parser_simple_declaration (parser,
11284 /*function_definition_allowed_p=*/false,
11285 decl);
11286 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
11287 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
11288 {
11289 /* It is a range-for, consume the ':' */
11290 cp_lexer_consume_token (parser->lexer);
11291 is_range_for = true;
11292 if (cxx_dialect < cxx11)
11293 {
11294 pedwarn (cp_lexer_peek_token (parser->lexer)->location, 0,
11295 "range-based %<for%> loops only available with "
11296 "-std=c++11 or -std=gnu++11");
11297 *decl = error_mark_node;
11298 }
11299 }
11300 else
11301 /* The ';' is not consumed yet because we told
11302 cp_parser_simple_declaration not to. */
11303 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
11304
11305 if (cp_parser_parse_definitely (parser))
11306 return is_range_for;
11307 /* If the tentative parse failed, then we shall need to look for an
11308 expression-statement. */
11309 }
11310 /* If we are here, it is an expression-statement. */
11311 cp_parser_expression_statement (parser, NULL_TREE);
11312 return false;
11313 }
11314
11315 /* Parse a jump-statement.
11316
11317 jump-statement:
11318 break ;
11319 continue ;
11320 return expression [opt] ;
11321 return braced-init-list ;
11322 goto identifier ;
11323
11324 GNU extension:
11325
11326 jump-statement:
11327 goto * expression ;
11328
11329 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR. */
11330
11331 static tree
11332 cp_parser_jump_statement (cp_parser* parser)
11333 {
11334 tree statement = error_mark_node;
11335 cp_token *token;
11336 enum rid keyword;
11337 unsigned char in_statement;
11338
11339 /* Peek at the next token. */
11340 token = cp_parser_require (parser, CPP_KEYWORD, RT_JUMP);
11341 if (!token)
11342 return error_mark_node;
11343
11344 /* See what kind of keyword it is. */
11345 keyword = token->keyword;
11346 switch (keyword)
11347 {
11348 case RID_BREAK:
11349 in_statement = parser->in_statement & ~IN_IF_STMT;
11350 switch (in_statement)
11351 {
11352 case 0:
11353 error_at (token->location, "break statement not within loop or switch");
11354 break;
11355 default:
11356 gcc_assert ((in_statement & IN_SWITCH_STMT)
11357 || in_statement == IN_ITERATION_STMT);
11358 statement = finish_break_stmt ();
11359 if (in_statement == IN_ITERATION_STMT)
11360 break_maybe_infinite_loop ();
11361 break;
11362 case IN_OMP_BLOCK:
11363 error_at (token->location, "invalid exit from OpenMP structured block");
11364 break;
11365 case IN_OMP_FOR:
11366 error_at (token->location, "break statement used with OpenMP for loop");
11367 break;
11368 case IN_CILK_SIMD_FOR:
11369 error_at (token->location, "break statement used with Cilk Plus for loop");
11370 break;
11371 }
11372 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
11373 break;
11374
11375 case RID_CONTINUE:
11376 switch (parser->in_statement & ~(IN_SWITCH_STMT | IN_IF_STMT))
11377 {
11378 case 0:
11379 error_at (token->location, "continue statement not within a loop");
11380 break;
11381 case IN_CILK_SIMD_FOR:
11382 error_at (token->location,
11383 "continue statement within %<#pragma simd%> loop body");
11384 /* Fall through. */
11385 case IN_ITERATION_STMT:
11386 case IN_OMP_FOR:
11387 statement = finish_continue_stmt ();
11388 break;
11389 case IN_OMP_BLOCK:
11390 error_at (token->location, "invalid exit from OpenMP structured block");
11391 break;
11392 default:
11393 gcc_unreachable ();
11394 }
11395 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
11396 break;
11397
11398 case RID_RETURN:
11399 {
11400 tree expr;
11401 bool expr_non_constant_p;
11402
11403 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
11404 {
11405 cp_lexer_set_source_position (parser->lexer);
11406 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
11407 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
11408 }
11409 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
11410 expr = cp_parser_expression (parser);
11411 else
11412 /* If the next token is a `;', then there is no
11413 expression. */
11414 expr = NULL_TREE;
11415 /* Build the return-statement. */
11416 statement = finish_return_stmt (expr);
11417 /* Look for the final `;'. */
11418 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
11419 }
11420 break;
11421
11422 case RID_GOTO:
11423 if (parser->in_function_body
11424 && DECL_DECLARED_CONSTEXPR_P (current_function_decl))
11425 {
11426 error ("%<goto%> in %<constexpr%> function");
11427 cp_function_chain->invalid_constexpr = true;
11428 }
11429
11430 /* Create the goto-statement. */
11431 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
11432 {
11433 /* Issue a warning about this use of a GNU extension. */
11434 pedwarn (token->location, OPT_Wpedantic, "ISO C++ forbids computed gotos");
11435 /* Consume the '*' token. */
11436 cp_lexer_consume_token (parser->lexer);
11437 /* Parse the dependent expression. */
11438 finish_goto_stmt (cp_parser_expression (parser));
11439 }
11440 else
11441 finish_goto_stmt (cp_parser_identifier (parser));
11442 /* Look for the final `;'. */
11443 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
11444 break;
11445
11446 default:
11447 cp_parser_error (parser, "expected jump-statement");
11448 break;
11449 }
11450
11451 return statement;
11452 }
11453
11454 /* Parse a declaration-statement.
11455
11456 declaration-statement:
11457 block-declaration */
11458
11459 static void
11460 cp_parser_declaration_statement (cp_parser* parser)
11461 {
11462 void *p;
11463
11464 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
11465 p = obstack_alloc (&declarator_obstack, 0);
11466
11467 /* Parse the block-declaration. */
11468 cp_parser_block_declaration (parser, /*statement_p=*/true);
11469
11470 /* Free any declarators allocated. */
11471 obstack_free (&declarator_obstack, p);
11472 }
11473
11474 /* Some dependent statements (like `if (cond) statement'), are
11475 implicitly in their own scope. In other words, if the statement is
11476 a single statement (as opposed to a compound-statement), it is
11477 none-the-less treated as if it were enclosed in braces. Any
11478 declarations appearing in the dependent statement are out of scope
11479 after control passes that point. This function parses a statement,
11480 but ensures that is in its own scope, even if it is not a
11481 compound-statement.
11482
11483 If IF_P is not NULL, *IF_P is set to indicate whether the statement
11484 is a (possibly labeled) if statement which is not enclosed in
11485 braces and has an else clause. This is used to implement
11486 -Wparentheses.
11487
11488 CHAIN is a vector of if-else-if conditions. This is used to implement
11489 -Wduplicated-cond.
11490
11491 Returns the new statement. */
11492
11493 static tree
11494 cp_parser_implicitly_scoped_statement (cp_parser* parser, bool *if_p,
11495 const token_indent_info &guard_tinfo,
11496 vec<tree> *chain)
11497 {
11498 tree statement;
11499 location_t body_loc = cp_lexer_peek_token (parser->lexer)->location;
11500 token_indent_info body_tinfo
11501 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
11502
11503 if (if_p != NULL)
11504 *if_p = false;
11505
11506 /* Mark if () ; with a special NOP_EXPR. */
11507 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
11508 {
11509 cp_lexer_consume_token (parser->lexer);
11510 statement = add_stmt (build_empty_stmt (body_loc));
11511
11512 if (guard_tinfo.keyword == RID_IF
11513 && !cp_lexer_next_token_is_keyword (parser->lexer, RID_ELSE))
11514 warning_at (body_loc, OPT_Wempty_body,
11515 "suggest braces around empty body in an %<if%> statement");
11516 else if (guard_tinfo.keyword == RID_ELSE)
11517 warning_at (body_loc, OPT_Wempty_body,
11518 "suggest braces around empty body in an %<else%> statement");
11519 }
11520 /* if a compound is opened, we simply parse the statement directly. */
11521 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
11522 statement = cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
11523 /* If the token is not a `{', then we must take special action. */
11524 else
11525 {
11526 /* Create a compound-statement. */
11527 statement = begin_compound_stmt (0);
11528 /* Parse the dependent-statement. */
11529 cp_parser_statement (parser, NULL_TREE, false, if_p, chain);
11530 /* Finish the dummy compound-statement. */
11531 finish_compound_stmt (statement);
11532 }
11533
11534 token_indent_info next_tinfo
11535 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
11536 warn_for_misleading_indentation (guard_tinfo, body_tinfo, next_tinfo);
11537
11538 /* Return the statement. */
11539 return statement;
11540 }
11541
11542 /* For some dependent statements (like `while (cond) statement'), we
11543 have already created a scope. Therefore, even if the dependent
11544 statement is a compound-statement, we do not want to create another
11545 scope. */
11546
11547 static void
11548 cp_parser_already_scoped_statement (cp_parser* parser,
11549 const token_indent_info &guard_tinfo)
11550 {
11551 /* If the token is a `{', then we must take special action. */
11552 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
11553 {
11554 token_indent_info body_tinfo
11555 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
11556
11557 cp_parser_statement (parser, NULL_TREE, false, NULL);
11558 token_indent_info next_tinfo
11559 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
11560 warn_for_misleading_indentation (guard_tinfo, body_tinfo, next_tinfo);
11561 }
11562 else
11563 {
11564 /* Avoid calling cp_parser_compound_statement, so that we
11565 don't create a new scope. Do everything else by hand. */
11566 cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
11567 /* If the next keyword is `__label__' we have a label declaration. */
11568 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
11569 cp_parser_label_declaration (parser);
11570 /* Parse an (optional) statement-seq. */
11571 cp_parser_statement_seq_opt (parser, NULL_TREE);
11572 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
11573 }
11574 }
11575
11576 /* Declarations [gram.dcl.dcl] */
11577
11578 /* Parse an optional declaration-sequence.
11579
11580 declaration-seq:
11581 declaration
11582 declaration-seq declaration */
11583
11584 static void
11585 cp_parser_declaration_seq_opt (cp_parser* parser)
11586 {
11587 while (true)
11588 {
11589 cp_token *token;
11590
11591 token = cp_lexer_peek_token (parser->lexer);
11592
11593 if (token->type == CPP_CLOSE_BRACE
11594 || token->type == CPP_EOF
11595 || token->type == CPP_PRAGMA_EOL)
11596 break;
11597
11598 if (token->type == CPP_SEMICOLON)
11599 {
11600 /* A declaration consisting of a single semicolon is
11601 invalid. Allow it unless we're being pedantic. */
11602 cp_lexer_consume_token (parser->lexer);
11603 if (!in_system_header_at (input_location))
11604 pedwarn (input_location, OPT_Wpedantic, "extra %<;%>");
11605 continue;
11606 }
11607
11608 /* If we're entering or exiting a region that's implicitly
11609 extern "C", modify the lang context appropriately. */
11610 if (!parser->implicit_extern_c && token->implicit_extern_c)
11611 {
11612 push_lang_context (lang_name_c);
11613 parser->implicit_extern_c = true;
11614 }
11615 else if (parser->implicit_extern_c && !token->implicit_extern_c)
11616 {
11617 pop_lang_context ();
11618 parser->implicit_extern_c = false;
11619 }
11620
11621 if (token->type == CPP_PRAGMA)
11622 {
11623 /* A top-level declaration can consist solely of a #pragma.
11624 A nested declaration cannot, so this is done here and not
11625 in cp_parser_declaration. (A #pragma at block scope is
11626 handled in cp_parser_statement.) */
11627 cp_parser_pragma (parser, pragma_external);
11628 continue;
11629 }
11630
11631 /* Parse the declaration itself. */
11632 cp_parser_declaration (parser);
11633 }
11634 }
11635
11636 /* Parse a declaration.
11637
11638 declaration:
11639 block-declaration
11640 function-definition
11641 template-declaration
11642 explicit-instantiation
11643 explicit-specialization
11644 linkage-specification
11645 namespace-definition
11646
11647 GNU extension:
11648
11649 declaration:
11650 __extension__ declaration */
11651
11652 static void
11653 cp_parser_declaration (cp_parser* parser)
11654 {
11655 cp_token token1;
11656 cp_token token2;
11657 int saved_pedantic;
11658 void *p;
11659 tree attributes = NULL_TREE;
11660
11661 /* Check for the `__extension__' keyword. */
11662 if (cp_parser_extension_opt (parser, &saved_pedantic))
11663 {
11664 /* Parse the qualified declaration. */
11665 cp_parser_declaration (parser);
11666 /* Restore the PEDANTIC flag. */
11667 pedantic = saved_pedantic;
11668
11669 return;
11670 }
11671
11672 /* Try to figure out what kind of declaration is present. */
11673 token1 = *cp_lexer_peek_token (parser->lexer);
11674
11675 if (token1.type != CPP_EOF)
11676 token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
11677 else
11678 {
11679 token2.type = CPP_EOF;
11680 token2.keyword = RID_MAX;
11681 }
11682
11683 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
11684 p = obstack_alloc (&declarator_obstack, 0);
11685
11686 /* If the next token is `extern' and the following token is a string
11687 literal, then we have a linkage specification. */
11688 if (token1.keyword == RID_EXTERN
11689 && cp_parser_is_pure_string_literal (&token2))
11690 cp_parser_linkage_specification (parser);
11691 /* If the next token is `template', then we have either a template
11692 declaration, an explicit instantiation, or an explicit
11693 specialization. */
11694 else if (token1.keyword == RID_TEMPLATE)
11695 {
11696 /* `template <>' indicates a template specialization. */
11697 if (token2.type == CPP_LESS
11698 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
11699 cp_parser_explicit_specialization (parser);
11700 /* `template <' indicates a template declaration. */
11701 else if (token2.type == CPP_LESS)
11702 cp_parser_template_declaration (parser, /*member_p=*/false);
11703 /* Anything else must be an explicit instantiation. */
11704 else
11705 cp_parser_explicit_instantiation (parser);
11706 }
11707 /* If the next token is `export', then we have a template
11708 declaration. */
11709 else if (token1.keyword == RID_EXPORT)
11710 cp_parser_template_declaration (parser, /*member_p=*/false);
11711 /* If the next token is `extern', 'static' or 'inline' and the one
11712 after that is `template', we have a GNU extended explicit
11713 instantiation directive. */
11714 else if (cp_parser_allow_gnu_extensions_p (parser)
11715 && (token1.keyword == RID_EXTERN
11716 || token1.keyword == RID_STATIC
11717 || token1.keyword == RID_INLINE)
11718 && token2.keyword == RID_TEMPLATE)
11719 cp_parser_explicit_instantiation (parser);
11720 /* If the next token is `namespace', check for a named or unnamed
11721 namespace definition. */
11722 else if (token1.keyword == RID_NAMESPACE
11723 && (/* A named namespace definition. */
11724 (token2.type == CPP_NAME
11725 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
11726 != CPP_EQ))
11727 || (token2.type == CPP_OPEN_SQUARE
11728 && cp_lexer_peek_nth_token (parser->lexer, 3)->type
11729 == CPP_OPEN_SQUARE)
11730 /* An unnamed namespace definition. */
11731 || token2.type == CPP_OPEN_BRACE
11732 || token2.keyword == RID_ATTRIBUTE))
11733 cp_parser_namespace_definition (parser);
11734 /* An inline (associated) namespace definition. */
11735 else if (token1.keyword == RID_INLINE
11736 && token2.keyword == RID_NAMESPACE)
11737 cp_parser_namespace_definition (parser);
11738 /* Objective-C++ declaration/definition. */
11739 else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1.keyword))
11740 cp_parser_objc_declaration (parser, NULL_TREE);
11741 else if (c_dialect_objc ()
11742 && token1.keyword == RID_ATTRIBUTE
11743 && cp_parser_objc_valid_prefix_attributes (parser, &attributes))
11744 cp_parser_objc_declaration (parser, attributes);
11745 /* At this point we may have a template declared by a concept
11746 introduction. */
11747 else if (flag_concepts
11748 && cp_parser_template_declaration_after_export (parser,
11749 /*member_p=*/false))
11750 /* We did. */;
11751 else
11752 /* Try to parse a block-declaration, or a function-definition. */
11753 cp_parser_block_declaration (parser, /*statement_p=*/false);
11754
11755 /* Free any declarators allocated. */
11756 obstack_free (&declarator_obstack, p);
11757 }
11758
11759 /* Parse a block-declaration.
11760
11761 block-declaration:
11762 simple-declaration
11763 asm-definition
11764 namespace-alias-definition
11765 using-declaration
11766 using-directive
11767
11768 GNU Extension:
11769
11770 block-declaration:
11771 __extension__ block-declaration
11772
11773 C++0x Extension:
11774
11775 block-declaration:
11776 static_assert-declaration
11777
11778 If STATEMENT_P is TRUE, then this block-declaration is occurring as
11779 part of a declaration-statement. */
11780
11781 static void
11782 cp_parser_block_declaration (cp_parser *parser,
11783 bool statement_p)
11784 {
11785 cp_token *token1;
11786 int saved_pedantic;
11787
11788 /* Check for the `__extension__' keyword. */
11789 if (cp_parser_extension_opt (parser, &saved_pedantic))
11790 {
11791 /* Parse the qualified declaration. */
11792 cp_parser_block_declaration (parser, statement_p);
11793 /* Restore the PEDANTIC flag. */
11794 pedantic = saved_pedantic;
11795
11796 return;
11797 }
11798
11799 /* Peek at the next token to figure out which kind of declaration is
11800 present. */
11801 token1 = cp_lexer_peek_token (parser->lexer);
11802
11803 /* If the next keyword is `asm', we have an asm-definition. */
11804 if (token1->keyword == RID_ASM)
11805 {
11806 if (statement_p)
11807 cp_parser_commit_to_tentative_parse (parser);
11808 cp_parser_asm_definition (parser);
11809 }
11810 /* If the next keyword is `namespace', we have a
11811 namespace-alias-definition. */
11812 else if (token1->keyword == RID_NAMESPACE)
11813 cp_parser_namespace_alias_definition (parser);
11814 /* If the next keyword is `using', we have a
11815 using-declaration, a using-directive, or an alias-declaration. */
11816 else if (token1->keyword == RID_USING)
11817 {
11818 cp_token *token2;
11819
11820 if (statement_p)
11821 cp_parser_commit_to_tentative_parse (parser);
11822 /* If the token after `using' is `namespace', then we have a
11823 using-directive. */
11824 token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
11825 if (token2->keyword == RID_NAMESPACE)
11826 cp_parser_using_directive (parser);
11827 /* If the second token after 'using' is '=', then we have an
11828 alias-declaration. */
11829 else if (cxx_dialect >= cxx11
11830 && token2->type == CPP_NAME
11831 && ((cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_EQ)
11832 || (cp_nth_tokens_can_be_attribute_p (parser, 3))))
11833 cp_parser_alias_declaration (parser);
11834 /* Otherwise, it's a using-declaration. */
11835 else
11836 cp_parser_using_declaration (parser,
11837 /*access_declaration_p=*/false);
11838 }
11839 /* If the next keyword is `__label__' we have a misplaced label
11840 declaration. */
11841 else if (token1->keyword == RID_LABEL)
11842 {
11843 cp_lexer_consume_token (parser->lexer);
11844 error_at (token1->location, "%<__label__%> not at the beginning of a block");
11845 cp_parser_skip_to_end_of_statement (parser);
11846 /* If the next token is now a `;', consume it. */
11847 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
11848 cp_lexer_consume_token (parser->lexer);
11849 }
11850 /* If the next token is `static_assert' we have a static assertion. */
11851 else if (token1->keyword == RID_STATIC_ASSERT)
11852 cp_parser_static_assert (parser, /*member_p=*/false);
11853 /* Anything else must be a simple-declaration. */
11854 else
11855 cp_parser_simple_declaration (parser, !statement_p,
11856 /*maybe_range_for_decl*/NULL);
11857 }
11858
11859 /* Parse a simple-declaration.
11860
11861 simple-declaration:
11862 decl-specifier-seq [opt] init-declarator-list [opt] ;
11863
11864 init-declarator-list:
11865 init-declarator
11866 init-declarator-list , init-declarator
11867
11868 If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
11869 function-definition as a simple-declaration.
11870
11871 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
11872 parsed declaration if it is an uninitialized single declarator not followed
11873 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
11874 if present, will not be consumed. */
11875
11876 static void
11877 cp_parser_simple_declaration (cp_parser* parser,
11878 bool function_definition_allowed_p,
11879 tree *maybe_range_for_decl)
11880 {
11881 cp_decl_specifier_seq decl_specifiers;
11882 int declares_class_or_enum;
11883 bool saw_declarator;
11884 location_t comma_loc = UNKNOWN_LOCATION;
11885 location_t init_loc = UNKNOWN_LOCATION;
11886
11887 if (maybe_range_for_decl)
11888 *maybe_range_for_decl = NULL_TREE;
11889
11890 /* Defer access checks until we know what is being declared; the
11891 checks for names appearing in the decl-specifier-seq should be
11892 done as if we were in the scope of the thing being declared. */
11893 push_deferring_access_checks (dk_deferred);
11894
11895 /* Parse the decl-specifier-seq. We have to keep track of whether
11896 or not the decl-specifier-seq declares a named class or
11897 enumeration type, since that is the only case in which the
11898 init-declarator-list is allowed to be empty.
11899
11900 [dcl.dcl]
11901
11902 In a simple-declaration, the optional init-declarator-list can be
11903 omitted only when declaring a class or enumeration, that is when
11904 the decl-specifier-seq contains either a class-specifier, an
11905 elaborated-type-specifier, or an enum-specifier. */
11906 cp_parser_decl_specifier_seq (parser,
11907 CP_PARSER_FLAGS_OPTIONAL,
11908 &decl_specifiers,
11909 &declares_class_or_enum);
11910 /* We no longer need to defer access checks. */
11911 stop_deferring_access_checks ();
11912
11913 /* In a block scope, a valid declaration must always have a
11914 decl-specifier-seq. By not trying to parse declarators, we can
11915 resolve the declaration/expression ambiguity more quickly. */
11916 if (!function_definition_allowed_p
11917 && !decl_specifiers.any_specifiers_p)
11918 {
11919 cp_parser_error (parser, "expected declaration");
11920 goto done;
11921 }
11922
11923 /* If the next two tokens are both identifiers, the code is
11924 erroneous. The usual cause of this situation is code like:
11925
11926 T t;
11927
11928 where "T" should name a type -- but does not. */
11929 if (!decl_specifiers.any_type_specifiers_p
11930 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
11931 {
11932 /* If parsing tentatively, we should commit; we really are
11933 looking at a declaration. */
11934 cp_parser_commit_to_tentative_parse (parser);
11935 /* Give up. */
11936 goto done;
11937 }
11938
11939 /* If we have seen at least one decl-specifier, and the next token
11940 is not a parenthesis, then we must be looking at a declaration.
11941 (After "int (" we might be looking at a functional cast.) */
11942 if (decl_specifiers.any_specifiers_p
11943 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN)
11944 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
11945 && !cp_parser_error_occurred (parser))
11946 cp_parser_commit_to_tentative_parse (parser);
11947
11948 /* Keep going until we hit the `;' at the end of the simple
11949 declaration. */
11950 saw_declarator = false;
11951 while (cp_lexer_next_token_is_not (parser->lexer,
11952 CPP_SEMICOLON))
11953 {
11954 cp_token *token;
11955 bool function_definition_p;
11956 tree decl;
11957
11958 if (saw_declarator)
11959 {
11960 /* If we are processing next declarator, comma is expected */
11961 token = cp_lexer_peek_token (parser->lexer);
11962 gcc_assert (token->type == CPP_COMMA);
11963 cp_lexer_consume_token (parser->lexer);
11964 if (maybe_range_for_decl)
11965 {
11966 *maybe_range_for_decl = error_mark_node;
11967 if (comma_loc == UNKNOWN_LOCATION)
11968 comma_loc = token->location;
11969 }
11970 }
11971 else
11972 saw_declarator = true;
11973
11974 /* Parse the init-declarator. */
11975 decl = cp_parser_init_declarator (parser, &decl_specifiers,
11976 /*checks=*/NULL,
11977 function_definition_allowed_p,
11978 /*member_p=*/false,
11979 declares_class_or_enum,
11980 &function_definition_p,
11981 maybe_range_for_decl,
11982 &init_loc);
11983 /* If an error occurred while parsing tentatively, exit quickly.
11984 (That usually happens when in the body of a function; each
11985 statement is treated as a declaration-statement until proven
11986 otherwise.) */
11987 if (cp_parser_error_occurred (parser))
11988 goto done;
11989 /* Handle function definitions specially. */
11990 if (function_definition_p)
11991 {
11992 /* If the next token is a `,', then we are probably
11993 processing something like:
11994
11995 void f() {}, *p;
11996
11997 which is erroneous. */
11998 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
11999 {
12000 cp_token *token = cp_lexer_peek_token (parser->lexer);
12001 error_at (token->location,
12002 "mixing"
12003 " declarations and function-definitions is forbidden");
12004 }
12005 /* Otherwise, we're done with the list of declarators. */
12006 else
12007 {
12008 pop_deferring_access_checks ();
12009 return;
12010 }
12011 }
12012 if (maybe_range_for_decl && *maybe_range_for_decl == NULL_TREE)
12013 *maybe_range_for_decl = decl;
12014 /* The next token should be either a `,' or a `;'. */
12015 token = cp_lexer_peek_token (parser->lexer);
12016 /* If it's a `,', there are more declarators to come. */
12017 if (token->type == CPP_COMMA)
12018 /* will be consumed next time around */;
12019 /* If it's a `;', we are done. */
12020 else if (token->type == CPP_SEMICOLON || maybe_range_for_decl)
12021 break;
12022 /* Anything else is an error. */
12023 else
12024 {
12025 /* If we have already issued an error message we don't need
12026 to issue another one. */
12027 if ((decl != error_mark_node
12028 && DECL_INITIAL (decl) != error_mark_node)
12029 || cp_parser_uncommitted_to_tentative_parse_p (parser))
12030 cp_parser_error (parser, "expected %<,%> or %<;%>");
12031 /* Skip tokens until we reach the end of the statement. */
12032 cp_parser_skip_to_end_of_statement (parser);
12033 /* If the next token is now a `;', consume it. */
12034 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
12035 cp_lexer_consume_token (parser->lexer);
12036 goto done;
12037 }
12038 /* After the first time around, a function-definition is not
12039 allowed -- even if it was OK at first. For example:
12040
12041 int i, f() {}
12042
12043 is not valid. */
12044 function_definition_allowed_p = false;
12045 }
12046
12047 /* Issue an error message if no declarators are present, and the
12048 decl-specifier-seq does not itself declare a class or
12049 enumeration: [dcl.dcl]/3. */
12050 if (!saw_declarator)
12051 {
12052 if (cp_parser_declares_only_class_p (parser))
12053 {
12054 if (!declares_class_or_enum
12055 && decl_specifiers.type
12056 && OVERLOAD_TYPE_P (decl_specifiers.type))
12057 /* Ensure an error is issued anyway when finish_decltype_type,
12058 called via cp_parser_decl_specifier_seq, returns a class or
12059 an enumeration (c++/51786). */
12060 decl_specifiers.type = NULL_TREE;
12061 shadow_tag (&decl_specifiers);
12062 }
12063 /* Perform any deferred access checks. */
12064 perform_deferred_access_checks (tf_warning_or_error);
12065 }
12066
12067 /* Consume the `;'. */
12068 if (!maybe_range_for_decl)
12069 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12070 else if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
12071 {
12072 if (init_loc != UNKNOWN_LOCATION)
12073 error_at (init_loc, "initializer in range-based %<for%> loop");
12074 if (comma_loc != UNKNOWN_LOCATION)
12075 error_at (comma_loc,
12076 "multiple declarations in range-based %<for%> loop");
12077 }
12078
12079 done:
12080 pop_deferring_access_checks ();
12081 }
12082
12083 /* Parse a decl-specifier-seq.
12084
12085 decl-specifier-seq:
12086 decl-specifier-seq [opt] decl-specifier
12087 decl-specifier attribute-specifier-seq [opt] (C++11)
12088
12089 decl-specifier:
12090 storage-class-specifier
12091 type-specifier
12092 function-specifier
12093 friend
12094 typedef
12095
12096 GNU Extension:
12097
12098 decl-specifier:
12099 attributes
12100
12101 Concepts Extension:
12102
12103 decl-specifier:
12104 concept
12105
12106 Set *DECL_SPECS to a representation of the decl-specifier-seq.
12107
12108 The parser flags FLAGS is used to control type-specifier parsing.
12109
12110 *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
12111 flags:
12112
12113 1: one of the decl-specifiers is an elaborated-type-specifier
12114 (i.e., a type declaration)
12115 2: one of the decl-specifiers is an enum-specifier or a
12116 class-specifier (i.e., a type definition)
12117
12118 */
12119
12120 static void
12121 cp_parser_decl_specifier_seq (cp_parser* parser,
12122 cp_parser_flags flags,
12123 cp_decl_specifier_seq *decl_specs,
12124 int* declares_class_or_enum)
12125 {
12126 bool constructor_possible_p = !parser->in_declarator_p;
12127 bool found_decl_spec = false;
12128 cp_token *start_token = NULL;
12129 cp_decl_spec ds;
12130
12131 /* Clear DECL_SPECS. */
12132 clear_decl_specs (decl_specs);
12133
12134 /* Assume no class or enumeration type is declared. */
12135 *declares_class_or_enum = 0;
12136
12137 /* Keep reading specifiers until there are no more to read. */
12138 while (true)
12139 {
12140 bool constructor_p;
12141 cp_token *token;
12142 ds = ds_last;
12143
12144 /* Peek at the next token. */
12145 token = cp_lexer_peek_token (parser->lexer);
12146
12147 /* Save the first token of the decl spec list for error
12148 reporting. */
12149 if (!start_token)
12150 start_token = token;
12151 /* Handle attributes. */
12152 if (cp_next_tokens_can_be_attribute_p (parser))
12153 {
12154 /* Parse the attributes. */
12155 tree attrs = cp_parser_attributes_opt (parser);
12156
12157 /* In a sequence of declaration specifiers, c++11 attributes
12158 appertain to the type that precede them. In that case
12159 [dcl.spec]/1 says:
12160
12161 The attribute-specifier-seq affects the type only for
12162 the declaration it appears in, not other declarations
12163 involving the same type.
12164
12165 But for now let's force the user to position the
12166 attribute either at the beginning of the declaration or
12167 after the declarator-id, which would clearly mean that it
12168 applies to the declarator. */
12169 if (cxx11_attribute_p (attrs))
12170 {
12171 if (!found_decl_spec)
12172 /* The c++11 attribute is at the beginning of the
12173 declaration. It appertains to the entity being
12174 declared. */;
12175 else
12176 {
12177 if (decl_specs->type && CLASS_TYPE_P (decl_specs->type))
12178 {
12179 /* This is an attribute following a
12180 class-specifier. */
12181 if (decl_specs->type_definition_p)
12182 warn_misplaced_attr_for_class_type (token->location,
12183 decl_specs->type);
12184 attrs = NULL_TREE;
12185 }
12186 else
12187 {
12188 decl_specs->std_attributes
12189 = chainon (decl_specs->std_attributes,
12190 attrs);
12191 if (decl_specs->locations[ds_std_attribute] == 0)
12192 decl_specs->locations[ds_std_attribute] = token->location;
12193 }
12194 continue;
12195 }
12196 }
12197
12198 decl_specs->attributes
12199 = chainon (decl_specs->attributes,
12200 attrs);
12201 if (decl_specs->locations[ds_attribute] == 0)
12202 decl_specs->locations[ds_attribute] = token->location;
12203 continue;
12204 }
12205 /* Assume we will find a decl-specifier keyword. */
12206 found_decl_spec = true;
12207 /* If the next token is an appropriate keyword, we can simply
12208 add it to the list. */
12209 switch (token->keyword)
12210 {
12211 /* decl-specifier:
12212 friend
12213 constexpr */
12214 case RID_FRIEND:
12215 if (!at_class_scope_p ())
12216 {
12217 error_at (token->location, "%<friend%> used outside of class");
12218 cp_lexer_purge_token (parser->lexer);
12219 }
12220 else
12221 {
12222 ds = ds_friend;
12223 /* Consume the token. */
12224 cp_lexer_consume_token (parser->lexer);
12225 }
12226 break;
12227
12228 case RID_CONSTEXPR:
12229 ds = ds_constexpr;
12230 cp_lexer_consume_token (parser->lexer);
12231 break;
12232
12233 case RID_CONCEPT:
12234 ds = ds_concept;
12235 cp_lexer_consume_token (parser->lexer);
12236 break;
12237
12238 /* function-specifier:
12239 inline
12240 virtual
12241 explicit */
12242 case RID_INLINE:
12243 case RID_VIRTUAL:
12244 case RID_EXPLICIT:
12245 cp_parser_function_specifier_opt (parser, decl_specs);
12246 break;
12247
12248 /* decl-specifier:
12249 typedef */
12250 case RID_TYPEDEF:
12251 ds = ds_typedef;
12252 /* Consume the token. */
12253 cp_lexer_consume_token (parser->lexer);
12254 /* A constructor declarator cannot appear in a typedef. */
12255 constructor_possible_p = false;
12256 /* The "typedef" keyword can only occur in a declaration; we
12257 may as well commit at this point. */
12258 cp_parser_commit_to_tentative_parse (parser);
12259
12260 if (decl_specs->storage_class != sc_none)
12261 decl_specs->conflicting_specifiers_p = true;
12262 break;
12263
12264 /* storage-class-specifier:
12265 auto
12266 register
12267 static
12268 extern
12269 mutable
12270
12271 GNU Extension:
12272 thread */
12273 case RID_AUTO:
12274 if (cxx_dialect == cxx98)
12275 {
12276 /* Consume the token. */
12277 cp_lexer_consume_token (parser->lexer);
12278
12279 /* Complain about `auto' as a storage specifier, if
12280 we're complaining about C++0x compatibility. */
12281 warning_at (token->location, OPT_Wc__11_compat, "%<auto%>"
12282 " changes meaning in C++11; please remove it");
12283
12284 /* Set the storage class anyway. */
12285 cp_parser_set_storage_class (parser, decl_specs, RID_AUTO,
12286 token);
12287 }
12288 else
12289 /* C++0x auto type-specifier. */
12290 found_decl_spec = false;
12291 break;
12292
12293 case RID_REGISTER:
12294 case RID_STATIC:
12295 case RID_EXTERN:
12296 case RID_MUTABLE:
12297 /* Consume the token. */
12298 cp_lexer_consume_token (parser->lexer);
12299 cp_parser_set_storage_class (parser, decl_specs, token->keyword,
12300 token);
12301 break;
12302 case RID_THREAD:
12303 /* Consume the token. */
12304 ds = ds_thread;
12305 cp_lexer_consume_token (parser->lexer);
12306 break;
12307
12308 default:
12309 /* We did not yet find a decl-specifier yet. */
12310 found_decl_spec = false;
12311 break;
12312 }
12313
12314 if (found_decl_spec
12315 && (flags & CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR)
12316 && token->keyword != RID_CONSTEXPR)
12317 error ("decl-specifier invalid in condition");
12318
12319 if (ds != ds_last)
12320 set_and_check_decl_spec_loc (decl_specs, ds, token);
12321
12322 /* Constructors are a special case. The `S' in `S()' is not a
12323 decl-specifier; it is the beginning of the declarator. */
12324 constructor_p
12325 = (!found_decl_spec
12326 && constructor_possible_p
12327 && (cp_parser_constructor_declarator_p
12328 (parser, decl_spec_seq_has_spec_p (decl_specs, ds_friend))));
12329
12330 /* If we don't have a DECL_SPEC yet, then we must be looking at
12331 a type-specifier. */
12332 if (!found_decl_spec && !constructor_p)
12333 {
12334 int decl_spec_declares_class_or_enum;
12335 bool is_cv_qualifier;
12336 tree type_spec;
12337
12338 type_spec
12339 = cp_parser_type_specifier (parser, flags,
12340 decl_specs,
12341 /*is_declaration=*/true,
12342 &decl_spec_declares_class_or_enum,
12343 &is_cv_qualifier);
12344 *declares_class_or_enum |= decl_spec_declares_class_or_enum;
12345
12346 /* If this type-specifier referenced a user-defined type
12347 (a typedef, class-name, etc.), then we can't allow any
12348 more such type-specifiers henceforth.
12349
12350 [dcl.spec]
12351
12352 The longest sequence of decl-specifiers that could
12353 possibly be a type name is taken as the
12354 decl-specifier-seq of a declaration. The sequence shall
12355 be self-consistent as described below.
12356
12357 [dcl.type]
12358
12359 As a general rule, at most one type-specifier is allowed
12360 in the complete decl-specifier-seq of a declaration. The
12361 only exceptions are the following:
12362
12363 -- const or volatile can be combined with any other
12364 type-specifier.
12365
12366 -- signed or unsigned can be combined with char, long,
12367 short, or int.
12368
12369 -- ..
12370
12371 Example:
12372
12373 typedef char* Pc;
12374 void g (const int Pc);
12375
12376 Here, Pc is *not* part of the decl-specifier seq; it's
12377 the declarator. Therefore, once we see a type-specifier
12378 (other than a cv-qualifier), we forbid any additional
12379 user-defined types. We *do* still allow things like `int
12380 int' to be considered a decl-specifier-seq, and issue the
12381 error message later. */
12382 if (type_spec && !is_cv_qualifier)
12383 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
12384 /* A constructor declarator cannot follow a type-specifier. */
12385 if (type_spec)
12386 {
12387 constructor_possible_p = false;
12388 found_decl_spec = true;
12389 if (!is_cv_qualifier)
12390 decl_specs->any_type_specifiers_p = true;
12391 }
12392 }
12393
12394 /* If we still do not have a DECL_SPEC, then there are no more
12395 decl-specifiers. */
12396 if (!found_decl_spec)
12397 break;
12398
12399 decl_specs->any_specifiers_p = true;
12400 /* After we see one decl-specifier, further decl-specifiers are
12401 always optional. */
12402 flags |= CP_PARSER_FLAGS_OPTIONAL;
12403 }
12404
12405 /* Don't allow a friend specifier with a class definition. */
12406 if (decl_spec_seq_has_spec_p (decl_specs, ds_friend)
12407 && (*declares_class_or_enum & 2))
12408 error_at (decl_specs->locations[ds_friend],
12409 "class definition may not be declared a friend");
12410 }
12411
12412 /* Parse an (optional) storage-class-specifier.
12413
12414 storage-class-specifier:
12415 auto
12416 register
12417 static
12418 extern
12419 mutable
12420
12421 GNU Extension:
12422
12423 storage-class-specifier:
12424 thread
12425
12426 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
12427
12428 static tree
12429 cp_parser_storage_class_specifier_opt (cp_parser* parser)
12430 {
12431 switch (cp_lexer_peek_token (parser->lexer)->keyword)
12432 {
12433 case RID_AUTO:
12434 if (cxx_dialect != cxx98)
12435 return NULL_TREE;
12436 /* Fall through for C++98. */
12437
12438 case RID_REGISTER:
12439 case RID_STATIC:
12440 case RID_EXTERN:
12441 case RID_MUTABLE:
12442 case RID_THREAD:
12443 /* Consume the token. */
12444 return cp_lexer_consume_token (parser->lexer)->u.value;
12445
12446 default:
12447 return NULL_TREE;
12448 }
12449 }
12450
12451 /* Parse an (optional) function-specifier.
12452
12453 function-specifier:
12454 inline
12455 virtual
12456 explicit
12457
12458 Returns an IDENTIFIER_NODE corresponding to the keyword used.
12459 Updates DECL_SPECS, if it is non-NULL. */
12460
12461 static tree
12462 cp_parser_function_specifier_opt (cp_parser* parser,
12463 cp_decl_specifier_seq *decl_specs)
12464 {
12465 cp_token *token = cp_lexer_peek_token (parser->lexer);
12466 switch (token->keyword)
12467 {
12468 case RID_INLINE:
12469 set_and_check_decl_spec_loc (decl_specs, ds_inline, token);
12470 break;
12471
12472 case RID_VIRTUAL:
12473 /* 14.5.2.3 [temp.mem]
12474
12475 A member function template shall not be virtual. */
12476 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
12477 error_at (token->location, "templates may not be %<virtual%>");
12478 else
12479 set_and_check_decl_spec_loc (decl_specs, ds_virtual, token);
12480 break;
12481
12482 case RID_EXPLICIT:
12483 set_and_check_decl_spec_loc (decl_specs, ds_explicit, token);
12484 break;
12485
12486 default:
12487 return NULL_TREE;
12488 }
12489
12490 /* Consume the token. */
12491 return cp_lexer_consume_token (parser->lexer)->u.value;
12492 }
12493
12494 /* Parse a linkage-specification.
12495
12496 linkage-specification:
12497 extern string-literal { declaration-seq [opt] }
12498 extern string-literal declaration */
12499
12500 static void
12501 cp_parser_linkage_specification (cp_parser* parser)
12502 {
12503 tree linkage;
12504
12505 /* Look for the `extern' keyword. */
12506 cp_parser_require_keyword (parser, RID_EXTERN, RT_EXTERN);
12507
12508 /* Look for the string-literal. */
12509 linkage = cp_parser_string_literal (parser, false, false);
12510
12511 /* Transform the literal into an identifier. If the literal is a
12512 wide-character string, or contains embedded NULs, then we can't
12513 handle it as the user wants. */
12514 if (strlen (TREE_STRING_POINTER (linkage))
12515 != (size_t) (TREE_STRING_LENGTH (linkage) - 1))
12516 {
12517 cp_parser_error (parser, "invalid linkage-specification");
12518 /* Assume C++ linkage. */
12519 linkage = lang_name_cplusplus;
12520 }
12521 else
12522 linkage = get_identifier (TREE_STRING_POINTER (linkage));
12523
12524 /* We're now using the new linkage. */
12525 push_lang_context (linkage);
12526
12527 /* If the next token is a `{', then we're using the first
12528 production. */
12529 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12530 {
12531 cp_ensure_no_omp_declare_simd (parser);
12532 cp_ensure_no_oacc_routine (parser);
12533
12534 /* Consume the `{' token. */
12535 cp_lexer_consume_token (parser->lexer);
12536 /* Parse the declarations. */
12537 cp_parser_declaration_seq_opt (parser);
12538 /* Look for the closing `}'. */
12539 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
12540 }
12541 /* Otherwise, there's just one declaration. */
12542 else
12543 {
12544 bool saved_in_unbraced_linkage_specification_p;
12545
12546 saved_in_unbraced_linkage_specification_p
12547 = parser->in_unbraced_linkage_specification_p;
12548 parser->in_unbraced_linkage_specification_p = true;
12549 cp_parser_declaration (parser);
12550 parser->in_unbraced_linkage_specification_p
12551 = saved_in_unbraced_linkage_specification_p;
12552 }
12553
12554 /* We're done with the linkage-specification. */
12555 pop_lang_context ();
12556 }
12557
12558 /* Parse a static_assert-declaration.
12559
12560 static_assert-declaration:
12561 static_assert ( constant-expression , string-literal ) ;
12562 static_assert ( constant-expression ) ; (C++1Z)
12563
12564 If MEMBER_P, this static_assert is a class member. */
12565
12566 static void
12567 cp_parser_static_assert(cp_parser *parser, bool member_p)
12568 {
12569 tree condition;
12570 tree message;
12571 cp_token *token;
12572 location_t saved_loc;
12573 bool dummy;
12574
12575 /* Peek at the `static_assert' token so we can keep track of exactly
12576 where the static assertion started. */
12577 token = cp_lexer_peek_token (parser->lexer);
12578 saved_loc = token->location;
12579
12580 /* Look for the `static_assert' keyword. */
12581 if (!cp_parser_require_keyword (parser, RID_STATIC_ASSERT,
12582 RT_STATIC_ASSERT))
12583 return;
12584
12585 /* We know we are in a static assertion; commit to any tentative
12586 parse. */
12587 if (cp_parser_parsing_tentatively (parser))
12588 cp_parser_commit_to_tentative_parse (parser);
12589
12590 /* Parse the `(' starting the static assertion condition. */
12591 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
12592
12593 /* Parse the constant-expression. Allow a non-constant expression
12594 here in order to give better diagnostics in finish_static_assert. */
12595 condition =
12596 cp_parser_constant_expression (parser,
12597 /*allow_non_constant_p=*/true,
12598 /*non_constant_p=*/&dummy);
12599
12600 if (cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
12601 {
12602 if (cxx_dialect < cxx1z)
12603 pedwarn (input_location, OPT_Wpedantic,
12604 "static_assert without a message "
12605 "only available with -std=c++1z or -std=gnu++1z");
12606 /* Eat the ')' */
12607 cp_lexer_consume_token (parser->lexer);
12608 message = build_string (1, "");
12609 TREE_TYPE (message) = char_array_type_node;
12610 fix_string_type (message);
12611 }
12612 else
12613 {
12614 /* Parse the separating `,'. */
12615 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
12616
12617 /* Parse the string-literal message. */
12618 message = cp_parser_string_literal (parser,
12619 /*translate=*/false,
12620 /*wide_ok=*/true);
12621
12622 /* A `)' completes the static assertion. */
12623 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
12624 cp_parser_skip_to_closing_parenthesis (parser,
12625 /*recovering=*/true,
12626 /*or_comma=*/false,
12627 /*consume_paren=*/true);
12628 }
12629
12630 /* A semicolon terminates the declaration. */
12631 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12632
12633 /* Complete the static assertion, which may mean either processing
12634 the static assert now or saving it for template instantiation. */
12635 finish_static_assert (condition, message, saved_loc, member_p);
12636 }
12637
12638 /* Parse the expression in decltype ( expression ). */
12639
12640 static tree
12641 cp_parser_decltype_expr (cp_parser *parser,
12642 bool &id_expression_or_member_access_p)
12643 {
12644 cp_token *id_expr_start_token;
12645 tree expr;
12646
12647 /* First, try parsing an id-expression. */
12648 id_expr_start_token = cp_lexer_peek_token (parser->lexer);
12649 cp_parser_parse_tentatively (parser);
12650 expr = cp_parser_id_expression (parser,
12651 /*template_keyword_p=*/false,
12652 /*check_dependency_p=*/true,
12653 /*template_p=*/NULL,
12654 /*declarator_p=*/false,
12655 /*optional_p=*/false);
12656
12657 if (!cp_parser_error_occurred (parser) && expr != error_mark_node)
12658 {
12659 bool non_integral_constant_expression_p = false;
12660 tree id_expression = expr;
12661 cp_id_kind idk;
12662 const char *error_msg;
12663
12664 if (identifier_p (expr))
12665 /* Lookup the name we got back from the id-expression. */
12666 expr = cp_parser_lookup_name_simple (parser, expr,
12667 id_expr_start_token->location);
12668
12669 if (expr
12670 && expr != error_mark_node
12671 && TREE_CODE (expr) != TYPE_DECL
12672 && (TREE_CODE (expr) != BIT_NOT_EXPR
12673 || !TYPE_P (TREE_OPERAND (expr, 0)))
12674 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
12675 {
12676 /* Complete lookup of the id-expression. */
12677 expr = (finish_id_expression
12678 (id_expression, expr, parser->scope, &idk,
12679 /*integral_constant_expression_p=*/false,
12680 /*allow_non_integral_constant_expression_p=*/true,
12681 &non_integral_constant_expression_p,
12682 /*template_p=*/false,
12683 /*done=*/true,
12684 /*address_p=*/false,
12685 /*template_arg_p=*/false,
12686 &error_msg,
12687 id_expr_start_token->location));
12688
12689 if (expr == error_mark_node)
12690 /* We found an id-expression, but it was something that we
12691 should not have found. This is an error, not something
12692 we can recover from, so note that we found an
12693 id-expression and we'll recover as gracefully as
12694 possible. */
12695 id_expression_or_member_access_p = true;
12696 }
12697
12698 if (expr
12699 && expr != error_mark_node
12700 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
12701 /* We have an id-expression. */
12702 id_expression_or_member_access_p = true;
12703 }
12704
12705 if (!id_expression_or_member_access_p)
12706 {
12707 /* Abort the id-expression parse. */
12708 cp_parser_abort_tentative_parse (parser);
12709
12710 /* Parsing tentatively, again. */
12711 cp_parser_parse_tentatively (parser);
12712
12713 /* Parse a class member access. */
12714 expr = cp_parser_postfix_expression (parser, /*address_p=*/false,
12715 /*cast_p=*/false, /*decltype*/true,
12716 /*member_access_only_p=*/true, NULL);
12717
12718 if (expr
12719 && expr != error_mark_node
12720 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
12721 /* We have an id-expression. */
12722 id_expression_or_member_access_p = true;
12723 }
12724
12725 if (id_expression_or_member_access_p)
12726 /* We have parsed the complete id-expression or member access. */
12727 cp_parser_parse_definitely (parser);
12728 else
12729 {
12730 /* Abort our attempt to parse an id-expression or member access
12731 expression. */
12732 cp_parser_abort_tentative_parse (parser);
12733
12734 /* Parse a full expression. */
12735 expr = cp_parser_expression (parser, /*pidk=*/NULL, /*cast_p=*/false,
12736 /*decltype_p=*/true);
12737 }
12738
12739 return expr;
12740 }
12741
12742 /* Parse a `decltype' type. Returns the type.
12743
12744 simple-type-specifier:
12745 decltype ( expression )
12746 C++14 proposal:
12747 decltype ( auto ) */
12748
12749 static tree
12750 cp_parser_decltype (cp_parser *parser)
12751 {
12752 tree expr;
12753 bool id_expression_or_member_access_p = false;
12754 const char *saved_message;
12755 bool saved_integral_constant_expression_p;
12756 bool saved_non_integral_constant_expression_p;
12757 bool saved_greater_than_is_operator_p;
12758 cp_token *start_token = cp_lexer_peek_token (parser->lexer);
12759
12760 if (start_token->type == CPP_DECLTYPE)
12761 {
12762 /* Already parsed. */
12763 cp_lexer_consume_token (parser->lexer);
12764 return start_token->u.value;
12765 }
12766
12767 /* Look for the `decltype' token. */
12768 if (!cp_parser_require_keyword (parser, RID_DECLTYPE, RT_DECLTYPE))
12769 return error_mark_node;
12770
12771 /* Parse the opening `('. */
12772 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
12773 return error_mark_node;
12774
12775 /* decltype (auto) */
12776 if (cxx_dialect >= cxx14
12777 && cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
12778 {
12779 cp_lexer_consume_token (parser->lexer);
12780 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
12781 return error_mark_node;
12782 expr = make_decltype_auto ();
12783 AUTO_IS_DECLTYPE (expr) = true;
12784 goto rewrite;
12785 }
12786
12787 /* Types cannot be defined in a `decltype' expression. Save away the
12788 old message. */
12789 saved_message = parser->type_definition_forbidden_message;
12790
12791 /* And create the new one. */
12792 parser->type_definition_forbidden_message
12793 = G_("types may not be defined in %<decltype%> expressions");
12794
12795 /* The restrictions on constant-expressions do not apply inside
12796 decltype expressions. */
12797 saved_integral_constant_expression_p
12798 = parser->integral_constant_expression_p;
12799 saved_non_integral_constant_expression_p
12800 = parser->non_integral_constant_expression_p;
12801 parser->integral_constant_expression_p = false;
12802
12803 /* Within a parenthesized expression, a `>' token is always
12804 the greater-than operator. */
12805 saved_greater_than_is_operator_p
12806 = parser->greater_than_is_operator_p;
12807 parser->greater_than_is_operator_p = true;
12808
12809 /* Do not actually evaluate the expression. */
12810 ++cp_unevaluated_operand;
12811
12812 /* Do not warn about problems with the expression. */
12813 ++c_inhibit_evaluation_warnings;
12814
12815 expr = cp_parser_decltype_expr (parser, id_expression_or_member_access_p);
12816
12817 /* Go back to evaluating expressions. */
12818 --cp_unevaluated_operand;
12819 --c_inhibit_evaluation_warnings;
12820
12821 /* The `>' token might be the end of a template-id or
12822 template-parameter-list now. */
12823 parser->greater_than_is_operator_p
12824 = saved_greater_than_is_operator_p;
12825
12826 /* Restore the old message and the integral constant expression
12827 flags. */
12828 parser->type_definition_forbidden_message = saved_message;
12829 parser->integral_constant_expression_p
12830 = saved_integral_constant_expression_p;
12831 parser->non_integral_constant_expression_p
12832 = saved_non_integral_constant_expression_p;
12833
12834 /* Parse to the closing `)'. */
12835 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
12836 {
12837 cp_parser_skip_to_closing_parenthesis (parser, true, false,
12838 /*consume_paren=*/true);
12839 return error_mark_node;
12840 }
12841
12842 expr = finish_decltype_type (expr, id_expression_or_member_access_p,
12843 tf_warning_or_error);
12844
12845 rewrite:
12846 /* Replace the decltype with a CPP_DECLTYPE so we don't need to parse
12847 it again. */
12848 start_token->type = CPP_DECLTYPE;
12849 start_token->u.value = expr;
12850 start_token->keyword = RID_MAX;
12851 cp_lexer_purge_tokens_after (parser->lexer, start_token);
12852
12853 return expr;
12854 }
12855
12856 /* Special member functions [gram.special] */
12857
12858 /* Parse a conversion-function-id.
12859
12860 conversion-function-id:
12861 operator conversion-type-id
12862
12863 Returns an IDENTIFIER_NODE representing the operator. */
12864
12865 static tree
12866 cp_parser_conversion_function_id (cp_parser* parser)
12867 {
12868 tree type;
12869 tree saved_scope;
12870 tree saved_qualifying_scope;
12871 tree saved_object_scope;
12872 tree pushed_scope = NULL_TREE;
12873
12874 /* Look for the `operator' token. */
12875 if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
12876 return error_mark_node;
12877 /* When we parse the conversion-type-id, the current scope will be
12878 reset. However, we need that information in able to look up the
12879 conversion function later, so we save it here. */
12880 saved_scope = parser->scope;
12881 saved_qualifying_scope = parser->qualifying_scope;
12882 saved_object_scope = parser->object_scope;
12883 /* We must enter the scope of the class so that the names of
12884 entities declared within the class are available in the
12885 conversion-type-id. For example, consider:
12886
12887 struct S {
12888 typedef int I;
12889 operator I();
12890 };
12891
12892 S::operator I() { ... }
12893
12894 In order to see that `I' is a type-name in the definition, we
12895 must be in the scope of `S'. */
12896 if (saved_scope)
12897 pushed_scope = push_scope (saved_scope);
12898 /* Parse the conversion-type-id. */
12899 type = cp_parser_conversion_type_id (parser);
12900 /* Leave the scope of the class, if any. */
12901 if (pushed_scope)
12902 pop_scope (pushed_scope);
12903 /* Restore the saved scope. */
12904 parser->scope = saved_scope;
12905 parser->qualifying_scope = saved_qualifying_scope;
12906 parser->object_scope = saved_object_scope;
12907 /* If the TYPE is invalid, indicate failure. */
12908 if (type == error_mark_node)
12909 return error_mark_node;
12910 return mangle_conv_op_name_for_type (type);
12911 }
12912
12913 /* Parse a conversion-type-id:
12914
12915 conversion-type-id:
12916 type-specifier-seq conversion-declarator [opt]
12917
12918 Returns the TYPE specified. */
12919
12920 static tree
12921 cp_parser_conversion_type_id (cp_parser* parser)
12922 {
12923 tree attributes;
12924 cp_decl_specifier_seq type_specifiers;
12925 cp_declarator *declarator;
12926 tree type_specified;
12927 const char *saved_message;
12928
12929 /* Parse the attributes. */
12930 attributes = cp_parser_attributes_opt (parser);
12931
12932 saved_message = parser->type_definition_forbidden_message;
12933 parser->type_definition_forbidden_message
12934 = G_("types may not be defined in a conversion-type-id");
12935
12936 /* Parse the type-specifiers. */
12937 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
12938 /*is_trailing_return=*/false,
12939 &type_specifiers);
12940
12941 parser->type_definition_forbidden_message = saved_message;
12942
12943 /* If that didn't work, stop. */
12944 if (type_specifiers.type == error_mark_node)
12945 return error_mark_node;
12946 /* Parse the conversion-declarator. */
12947 declarator = cp_parser_conversion_declarator_opt (parser);
12948
12949 type_specified = grokdeclarator (declarator, &type_specifiers, TYPENAME,
12950 /*initialized=*/0, &attributes);
12951 if (attributes)
12952 cplus_decl_attributes (&type_specified, attributes, /*flags=*/0);
12953
12954 /* Don't give this error when parsing tentatively. This happens to
12955 work because we always parse this definitively once. */
12956 if (! cp_parser_uncommitted_to_tentative_parse_p (parser)
12957 && type_uses_auto (type_specified))
12958 {
12959 if (cxx_dialect < cxx14)
12960 {
12961 error ("invalid use of %<auto%> in conversion operator");
12962 return error_mark_node;
12963 }
12964 else if (template_parm_scope_p ())
12965 warning (0, "use of %<auto%> in member template "
12966 "conversion operator can never be deduced");
12967 }
12968
12969 return type_specified;
12970 }
12971
12972 /* Parse an (optional) conversion-declarator.
12973
12974 conversion-declarator:
12975 ptr-operator conversion-declarator [opt]
12976
12977 */
12978
12979 static cp_declarator *
12980 cp_parser_conversion_declarator_opt (cp_parser* parser)
12981 {
12982 enum tree_code code;
12983 tree class_type, std_attributes = NULL_TREE;
12984 cp_cv_quals cv_quals;
12985
12986 /* We don't know if there's a ptr-operator next, or not. */
12987 cp_parser_parse_tentatively (parser);
12988 /* Try the ptr-operator. */
12989 code = cp_parser_ptr_operator (parser, &class_type, &cv_quals,
12990 &std_attributes);
12991 /* If it worked, look for more conversion-declarators. */
12992 if (cp_parser_parse_definitely (parser))
12993 {
12994 cp_declarator *declarator;
12995
12996 /* Parse another optional declarator. */
12997 declarator = cp_parser_conversion_declarator_opt (parser);
12998
12999 declarator = cp_parser_make_indirect_declarator
13000 (code, class_type, cv_quals, declarator, std_attributes);
13001
13002 return declarator;
13003 }
13004
13005 return NULL;
13006 }
13007
13008 /* Parse an (optional) ctor-initializer.
13009
13010 ctor-initializer:
13011 : mem-initializer-list
13012
13013 Returns TRUE iff the ctor-initializer was actually present. */
13014
13015 static bool
13016 cp_parser_ctor_initializer_opt (cp_parser* parser)
13017 {
13018 /* If the next token is not a `:', then there is no
13019 ctor-initializer. */
13020 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
13021 {
13022 /* Do default initialization of any bases and members. */
13023 if (DECL_CONSTRUCTOR_P (current_function_decl))
13024 finish_mem_initializers (NULL_TREE);
13025
13026 return false;
13027 }
13028
13029 /* Consume the `:' token. */
13030 cp_lexer_consume_token (parser->lexer);
13031 /* And the mem-initializer-list. */
13032 cp_parser_mem_initializer_list (parser);
13033
13034 return true;
13035 }
13036
13037 /* Parse a mem-initializer-list.
13038
13039 mem-initializer-list:
13040 mem-initializer ... [opt]
13041 mem-initializer ... [opt] , mem-initializer-list */
13042
13043 static void
13044 cp_parser_mem_initializer_list (cp_parser* parser)
13045 {
13046 tree mem_initializer_list = NULL_TREE;
13047 tree target_ctor = error_mark_node;
13048 cp_token *token = cp_lexer_peek_token (parser->lexer);
13049
13050 /* Let the semantic analysis code know that we are starting the
13051 mem-initializer-list. */
13052 if (!DECL_CONSTRUCTOR_P (current_function_decl))
13053 error_at (token->location,
13054 "only constructors take member initializers");
13055
13056 /* Loop through the list. */
13057 while (true)
13058 {
13059 tree mem_initializer;
13060
13061 token = cp_lexer_peek_token (parser->lexer);
13062 /* Parse the mem-initializer. */
13063 mem_initializer = cp_parser_mem_initializer (parser);
13064 /* If the next token is a `...', we're expanding member initializers. */
13065 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
13066 {
13067 /* Consume the `...'. */
13068 cp_lexer_consume_token (parser->lexer);
13069
13070 /* The TREE_PURPOSE must be a _TYPE, because base-specifiers
13071 can be expanded but members cannot. */
13072 if (mem_initializer != error_mark_node
13073 && !TYPE_P (TREE_PURPOSE (mem_initializer)))
13074 {
13075 error_at (token->location,
13076 "cannot expand initializer for member %<%D%>",
13077 TREE_PURPOSE (mem_initializer));
13078 mem_initializer = error_mark_node;
13079 }
13080
13081 /* Construct the pack expansion type. */
13082 if (mem_initializer != error_mark_node)
13083 mem_initializer = make_pack_expansion (mem_initializer);
13084 }
13085 if (target_ctor != error_mark_node
13086 && mem_initializer != error_mark_node)
13087 {
13088 error ("mem-initializer for %qD follows constructor delegation",
13089 TREE_PURPOSE (mem_initializer));
13090 mem_initializer = error_mark_node;
13091 }
13092 /* Look for a target constructor. */
13093 if (mem_initializer != error_mark_node
13094 && CLASS_TYPE_P (TREE_PURPOSE (mem_initializer))
13095 && same_type_p (TREE_PURPOSE (mem_initializer), current_class_type))
13096 {
13097 maybe_warn_cpp0x (CPP0X_DELEGATING_CTORS);
13098 if (mem_initializer_list)
13099 {
13100 error ("constructor delegation follows mem-initializer for %qD",
13101 TREE_PURPOSE (mem_initializer_list));
13102 mem_initializer = error_mark_node;
13103 }
13104 target_ctor = mem_initializer;
13105 }
13106 /* Add it to the list, unless it was erroneous. */
13107 if (mem_initializer != error_mark_node)
13108 {
13109 TREE_CHAIN (mem_initializer) = mem_initializer_list;
13110 mem_initializer_list = mem_initializer;
13111 }
13112 /* If the next token is not a `,', we're done. */
13113 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
13114 break;
13115 /* Consume the `,' token. */
13116 cp_lexer_consume_token (parser->lexer);
13117 }
13118
13119 /* Perform semantic analysis. */
13120 if (DECL_CONSTRUCTOR_P (current_function_decl))
13121 finish_mem_initializers (mem_initializer_list);
13122 }
13123
13124 /* Parse a mem-initializer.
13125
13126 mem-initializer:
13127 mem-initializer-id ( expression-list [opt] )
13128 mem-initializer-id braced-init-list
13129
13130 GNU extension:
13131
13132 mem-initializer:
13133 ( expression-list [opt] )
13134
13135 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
13136 class) or FIELD_DECL (for a non-static data member) to initialize;
13137 the TREE_VALUE is the expression-list. An empty initialization
13138 list is represented by void_list_node. */
13139
13140 static tree
13141 cp_parser_mem_initializer (cp_parser* parser)
13142 {
13143 tree mem_initializer_id;
13144 tree expression_list;
13145 tree member;
13146 cp_token *token = cp_lexer_peek_token (parser->lexer);
13147
13148 /* Find out what is being initialized. */
13149 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
13150 {
13151 permerror (token->location,
13152 "anachronistic old-style base class initializer");
13153 mem_initializer_id = NULL_TREE;
13154 }
13155 else
13156 {
13157 mem_initializer_id = cp_parser_mem_initializer_id (parser);
13158 if (mem_initializer_id == error_mark_node)
13159 return mem_initializer_id;
13160 }
13161 member = expand_member_init (mem_initializer_id);
13162 if (member && !DECL_P (member))
13163 in_base_initializer = 1;
13164
13165 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
13166 {
13167 bool expr_non_constant_p;
13168 cp_lexer_set_source_position (parser->lexer);
13169 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
13170 expression_list = cp_parser_braced_list (parser, &expr_non_constant_p);
13171 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
13172 expression_list = build_tree_list (NULL_TREE, expression_list);
13173 }
13174 else
13175 {
13176 vec<tree, va_gc> *vec;
13177 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
13178 /*cast_p=*/false,
13179 /*allow_expansion_p=*/true,
13180 /*non_constant_p=*/NULL);
13181 if (vec == NULL)
13182 return error_mark_node;
13183 expression_list = build_tree_list_vec (vec);
13184 release_tree_vector (vec);
13185 }
13186
13187 if (expression_list == error_mark_node)
13188 return error_mark_node;
13189 if (!expression_list)
13190 expression_list = void_type_node;
13191
13192 in_base_initializer = 0;
13193
13194 return member ? build_tree_list (member, expression_list) : error_mark_node;
13195 }
13196
13197 /* Parse a mem-initializer-id.
13198
13199 mem-initializer-id:
13200 :: [opt] nested-name-specifier [opt] class-name
13201 decltype-specifier (C++11)
13202 identifier
13203
13204 Returns a TYPE indicating the class to be initialized for the first
13205 production (and the second in C++11). Returns an IDENTIFIER_NODE
13206 indicating the data member to be initialized for the last production. */
13207
13208 static tree
13209 cp_parser_mem_initializer_id (cp_parser* parser)
13210 {
13211 bool global_scope_p;
13212 bool nested_name_specifier_p;
13213 bool template_p = false;
13214 tree id;
13215
13216 cp_token *token = cp_lexer_peek_token (parser->lexer);
13217
13218 /* `typename' is not allowed in this context ([temp.res]). */
13219 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
13220 {
13221 error_at (token->location,
13222 "keyword %<typename%> not allowed in this context (a qualified "
13223 "member initializer is implicitly a type)");
13224 cp_lexer_consume_token (parser->lexer);
13225 }
13226 /* Look for the optional `::' operator. */
13227 global_scope_p
13228 = (cp_parser_global_scope_opt (parser,
13229 /*current_scope_valid_p=*/false)
13230 != NULL_TREE);
13231 /* Look for the optional nested-name-specifier. The simplest way to
13232 implement:
13233
13234 [temp.res]
13235
13236 The keyword `typename' is not permitted in a base-specifier or
13237 mem-initializer; in these contexts a qualified name that
13238 depends on a template-parameter is implicitly assumed to be a
13239 type name.
13240
13241 is to assume that we have seen the `typename' keyword at this
13242 point. */
13243 nested_name_specifier_p
13244 = (cp_parser_nested_name_specifier_opt (parser,
13245 /*typename_keyword_p=*/true,
13246 /*check_dependency_p=*/true,
13247 /*type_p=*/true,
13248 /*is_declaration=*/true)
13249 != NULL_TREE);
13250 if (nested_name_specifier_p)
13251 template_p = cp_parser_optional_template_keyword (parser);
13252 /* If there is a `::' operator or a nested-name-specifier, then we
13253 are definitely looking for a class-name. */
13254 if (global_scope_p || nested_name_specifier_p)
13255 return cp_parser_class_name (parser,
13256 /*typename_keyword_p=*/true,
13257 /*template_keyword_p=*/template_p,
13258 typename_type,
13259 /*check_dependency_p=*/true,
13260 /*class_head_p=*/false,
13261 /*is_declaration=*/true);
13262 /* Otherwise, we could also be looking for an ordinary identifier. */
13263 cp_parser_parse_tentatively (parser);
13264 if (cp_lexer_next_token_is_decltype (parser->lexer))
13265 /* Try a decltype-specifier. */
13266 id = cp_parser_decltype (parser);
13267 else
13268 /* Otherwise, try a class-name. */
13269 id = cp_parser_class_name (parser,
13270 /*typename_keyword_p=*/true,
13271 /*template_keyword_p=*/false,
13272 none_type,
13273 /*check_dependency_p=*/true,
13274 /*class_head_p=*/false,
13275 /*is_declaration=*/true);
13276 /* If we found one, we're done. */
13277 if (cp_parser_parse_definitely (parser))
13278 return id;
13279 /* Otherwise, look for an ordinary identifier. */
13280 return cp_parser_identifier (parser);
13281 }
13282
13283 /* Overloading [gram.over] */
13284
13285 /* Parse an operator-function-id.
13286
13287 operator-function-id:
13288 operator operator
13289
13290 Returns an IDENTIFIER_NODE for the operator which is a
13291 human-readable spelling of the identifier, e.g., `operator +'. */
13292
13293 static tree
13294 cp_parser_operator_function_id (cp_parser* parser)
13295 {
13296 /* Look for the `operator' keyword. */
13297 if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
13298 return error_mark_node;
13299 /* And then the name of the operator itself. */
13300 return cp_parser_operator (parser);
13301 }
13302
13303 /* Return an identifier node for a user-defined literal operator.
13304 The suffix identifier is chained to the operator name identifier. */
13305
13306 static tree
13307 cp_literal_operator_id (const char* name)
13308 {
13309 tree identifier;
13310 char *buffer = XNEWVEC (char, strlen (UDLIT_OP_ANSI_PREFIX)
13311 + strlen (name) + 10);
13312 sprintf (buffer, UDLIT_OP_ANSI_FORMAT, name);
13313 identifier = get_identifier (buffer);
13314
13315 return identifier;
13316 }
13317
13318 /* Parse an operator.
13319
13320 operator:
13321 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
13322 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
13323 || ++ -- , ->* -> () []
13324
13325 GNU Extensions:
13326
13327 operator:
13328 <? >? <?= >?=
13329
13330 Returns an IDENTIFIER_NODE for the operator which is a
13331 human-readable spelling of the identifier, e.g., `operator +'. */
13332
13333 static tree
13334 cp_parser_operator (cp_parser* parser)
13335 {
13336 tree id = NULL_TREE;
13337 cp_token *token;
13338 bool utf8 = false;
13339
13340 /* Peek at the next token. */
13341 token = cp_lexer_peek_token (parser->lexer);
13342 /* Figure out which operator we have. */
13343 switch (token->type)
13344 {
13345 case CPP_KEYWORD:
13346 {
13347 enum tree_code op;
13348
13349 /* The keyword should be either `new' or `delete'. */
13350 if (token->keyword == RID_NEW)
13351 op = NEW_EXPR;
13352 else if (token->keyword == RID_DELETE)
13353 op = DELETE_EXPR;
13354 else
13355 break;
13356
13357 /* Consume the `new' or `delete' token. */
13358 cp_lexer_consume_token (parser->lexer);
13359
13360 /* Peek at the next token. */
13361 token = cp_lexer_peek_token (parser->lexer);
13362 /* If it's a `[' token then this is the array variant of the
13363 operator. */
13364 if (token->type == CPP_OPEN_SQUARE)
13365 {
13366 /* Consume the `[' token. */
13367 cp_lexer_consume_token (parser->lexer);
13368 /* Look for the `]' token. */
13369 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
13370 id = ansi_opname (op == NEW_EXPR
13371 ? VEC_NEW_EXPR : VEC_DELETE_EXPR);
13372 }
13373 /* Otherwise, we have the non-array variant. */
13374 else
13375 id = ansi_opname (op);
13376
13377 return id;
13378 }
13379
13380 case CPP_PLUS:
13381 id = ansi_opname (PLUS_EXPR);
13382 break;
13383
13384 case CPP_MINUS:
13385 id = ansi_opname (MINUS_EXPR);
13386 break;
13387
13388 case CPP_MULT:
13389 id = ansi_opname (MULT_EXPR);
13390 break;
13391
13392 case CPP_DIV:
13393 id = ansi_opname (TRUNC_DIV_EXPR);
13394 break;
13395
13396 case CPP_MOD:
13397 id = ansi_opname (TRUNC_MOD_EXPR);
13398 break;
13399
13400 case CPP_XOR:
13401 id = ansi_opname (BIT_XOR_EXPR);
13402 break;
13403
13404 case CPP_AND:
13405 id = ansi_opname (BIT_AND_EXPR);
13406 break;
13407
13408 case CPP_OR:
13409 id = ansi_opname (BIT_IOR_EXPR);
13410 break;
13411
13412 case CPP_COMPL:
13413 id = ansi_opname (BIT_NOT_EXPR);
13414 break;
13415
13416 case CPP_NOT:
13417 id = ansi_opname (TRUTH_NOT_EXPR);
13418 break;
13419
13420 case CPP_EQ:
13421 id = ansi_assopname (NOP_EXPR);
13422 break;
13423
13424 case CPP_LESS:
13425 id = ansi_opname (LT_EXPR);
13426 break;
13427
13428 case CPP_GREATER:
13429 id = ansi_opname (GT_EXPR);
13430 break;
13431
13432 case CPP_PLUS_EQ:
13433 id = ansi_assopname (PLUS_EXPR);
13434 break;
13435
13436 case CPP_MINUS_EQ:
13437 id = ansi_assopname (MINUS_EXPR);
13438 break;
13439
13440 case CPP_MULT_EQ:
13441 id = ansi_assopname (MULT_EXPR);
13442 break;
13443
13444 case CPP_DIV_EQ:
13445 id = ansi_assopname (TRUNC_DIV_EXPR);
13446 break;
13447
13448 case CPP_MOD_EQ:
13449 id = ansi_assopname (TRUNC_MOD_EXPR);
13450 break;
13451
13452 case CPP_XOR_EQ:
13453 id = ansi_assopname (BIT_XOR_EXPR);
13454 break;
13455
13456 case CPP_AND_EQ:
13457 id = ansi_assopname (BIT_AND_EXPR);
13458 break;
13459
13460 case CPP_OR_EQ:
13461 id = ansi_assopname (BIT_IOR_EXPR);
13462 break;
13463
13464 case CPP_LSHIFT:
13465 id = ansi_opname (LSHIFT_EXPR);
13466 break;
13467
13468 case CPP_RSHIFT:
13469 id = ansi_opname (RSHIFT_EXPR);
13470 break;
13471
13472 case CPP_LSHIFT_EQ:
13473 id = ansi_assopname (LSHIFT_EXPR);
13474 break;
13475
13476 case CPP_RSHIFT_EQ:
13477 id = ansi_assopname (RSHIFT_EXPR);
13478 break;
13479
13480 case CPP_EQ_EQ:
13481 id = ansi_opname (EQ_EXPR);
13482 break;
13483
13484 case CPP_NOT_EQ:
13485 id = ansi_opname (NE_EXPR);
13486 break;
13487
13488 case CPP_LESS_EQ:
13489 id = ansi_opname (LE_EXPR);
13490 break;
13491
13492 case CPP_GREATER_EQ:
13493 id = ansi_opname (GE_EXPR);
13494 break;
13495
13496 case CPP_AND_AND:
13497 id = ansi_opname (TRUTH_ANDIF_EXPR);
13498 break;
13499
13500 case CPP_OR_OR:
13501 id = ansi_opname (TRUTH_ORIF_EXPR);
13502 break;
13503
13504 case CPP_PLUS_PLUS:
13505 id = ansi_opname (POSTINCREMENT_EXPR);
13506 break;
13507
13508 case CPP_MINUS_MINUS:
13509 id = ansi_opname (PREDECREMENT_EXPR);
13510 break;
13511
13512 case CPP_COMMA:
13513 id = ansi_opname (COMPOUND_EXPR);
13514 break;
13515
13516 case CPP_DEREF_STAR:
13517 id = ansi_opname (MEMBER_REF);
13518 break;
13519
13520 case CPP_DEREF:
13521 id = ansi_opname (COMPONENT_REF);
13522 break;
13523
13524 case CPP_OPEN_PAREN:
13525 /* Consume the `('. */
13526 cp_lexer_consume_token (parser->lexer);
13527 /* Look for the matching `)'. */
13528 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
13529 return ansi_opname (CALL_EXPR);
13530
13531 case CPP_OPEN_SQUARE:
13532 /* Consume the `['. */
13533 cp_lexer_consume_token (parser->lexer);
13534 /* Look for the matching `]'. */
13535 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
13536 return ansi_opname (ARRAY_REF);
13537
13538 case CPP_UTF8STRING:
13539 case CPP_UTF8STRING_USERDEF:
13540 utf8 = true;
13541 case CPP_STRING:
13542 case CPP_WSTRING:
13543 case CPP_STRING16:
13544 case CPP_STRING32:
13545 case CPP_STRING_USERDEF:
13546 case CPP_WSTRING_USERDEF:
13547 case CPP_STRING16_USERDEF:
13548 case CPP_STRING32_USERDEF:
13549 {
13550 tree str, string_tree;
13551 int sz, len;
13552
13553 if (cxx_dialect == cxx98)
13554 maybe_warn_cpp0x (CPP0X_USER_DEFINED_LITERALS);
13555
13556 /* Consume the string. */
13557 str = cp_parser_string_literal (parser, /*translate=*/true,
13558 /*wide_ok=*/true, /*lookup_udlit=*/false);
13559 if (str == error_mark_node)
13560 return error_mark_node;
13561 else if (TREE_CODE (str) == USERDEF_LITERAL)
13562 {
13563 string_tree = USERDEF_LITERAL_VALUE (str);
13564 id = USERDEF_LITERAL_SUFFIX_ID (str);
13565 }
13566 else
13567 {
13568 string_tree = str;
13569 /* Look for the suffix identifier. */
13570 token = cp_lexer_peek_token (parser->lexer);
13571 if (token->type == CPP_NAME)
13572 id = cp_parser_identifier (parser);
13573 else if (token->type == CPP_KEYWORD)
13574 {
13575 error ("unexpected keyword;"
13576 " remove space between quotes and suffix identifier");
13577 return error_mark_node;
13578 }
13579 else
13580 {
13581 error ("expected suffix identifier");
13582 return error_mark_node;
13583 }
13584 }
13585 sz = TREE_INT_CST_LOW (TYPE_SIZE_UNIT
13586 (TREE_TYPE (TREE_TYPE (string_tree))));
13587 len = TREE_STRING_LENGTH (string_tree) / sz - 1;
13588 if (len != 0)
13589 {
13590 error ("expected empty string after %<operator%> keyword");
13591 return error_mark_node;
13592 }
13593 if (utf8 || TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (string_tree)))
13594 != char_type_node)
13595 {
13596 error ("invalid encoding prefix in literal operator");
13597 return error_mark_node;
13598 }
13599 if (id != error_mark_node)
13600 {
13601 const char *name = IDENTIFIER_POINTER (id);
13602 id = cp_literal_operator_id (name);
13603 }
13604 return id;
13605 }
13606
13607 default:
13608 /* Anything else is an error. */
13609 break;
13610 }
13611
13612 /* If we have selected an identifier, we need to consume the
13613 operator token. */
13614 if (id)
13615 cp_lexer_consume_token (parser->lexer);
13616 /* Otherwise, no valid operator name was present. */
13617 else
13618 {
13619 cp_parser_error (parser, "expected operator");
13620 id = error_mark_node;
13621 }
13622
13623 return id;
13624 }
13625
13626 /* Parse a template-declaration.
13627
13628 template-declaration:
13629 export [opt] template < template-parameter-list > declaration
13630
13631 If MEMBER_P is TRUE, this template-declaration occurs within a
13632 class-specifier.
13633
13634 The grammar rule given by the standard isn't correct. What
13635 is really meant is:
13636
13637 template-declaration:
13638 export [opt] template-parameter-list-seq
13639 decl-specifier-seq [opt] init-declarator [opt] ;
13640 export [opt] template-parameter-list-seq
13641 function-definition
13642
13643 template-parameter-list-seq:
13644 template-parameter-list-seq [opt]
13645 template < template-parameter-list >
13646
13647 Concept Extensions:
13648
13649 template-parameter-list-seq:
13650 template < template-parameter-list > requires-clause [opt]
13651
13652 requires-clause:
13653 requires logical-or-expression */
13654
13655 static void
13656 cp_parser_template_declaration (cp_parser* parser, bool member_p)
13657 {
13658 /* Check for `export'. */
13659 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
13660 {
13661 /* Consume the `export' token. */
13662 cp_lexer_consume_token (parser->lexer);
13663 /* Warn that we do not support `export'. */
13664 warning (0, "keyword %<export%> not implemented, and will be ignored");
13665 }
13666
13667 cp_parser_template_declaration_after_export (parser, member_p);
13668 }
13669
13670 /* Parse a template-parameter-list.
13671
13672 template-parameter-list:
13673 template-parameter
13674 template-parameter-list , template-parameter
13675
13676 Returns a TREE_LIST. Each node represents a template parameter.
13677 The nodes are connected via their TREE_CHAINs. */
13678
13679 static tree
13680 cp_parser_template_parameter_list (cp_parser* parser)
13681 {
13682 tree parameter_list = NULL_TREE;
13683
13684 begin_template_parm_list ();
13685
13686 /* The loop below parses the template parms. We first need to know
13687 the total number of template parms to be able to compute proper
13688 canonical types of each dependent type. So after the loop, when
13689 we know the total number of template parms,
13690 end_template_parm_list computes the proper canonical types and
13691 fixes up the dependent types accordingly. */
13692 while (true)
13693 {
13694 tree parameter;
13695 bool is_non_type;
13696 bool is_parameter_pack;
13697 location_t parm_loc;
13698
13699 /* Parse the template-parameter. */
13700 parm_loc = cp_lexer_peek_token (parser->lexer)->location;
13701 parameter = cp_parser_template_parameter (parser,
13702 &is_non_type,
13703 &is_parameter_pack);
13704 /* Add it to the list. */
13705 if (parameter != error_mark_node)
13706 parameter_list = process_template_parm (parameter_list,
13707 parm_loc,
13708 parameter,
13709 is_non_type,
13710 is_parameter_pack);
13711 else
13712 {
13713 tree err_parm = build_tree_list (parameter, parameter);
13714 parameter_list = chainon (parameter_list, err_parm);
13715 }
13716
13717 /* If the next token is not a `,', we're done. */
13718 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
13719 break;
13720 /* Otherwise, consume the `,' token. */
13721 cp_lexer_consume_token (parser->lexer);
13722 }
13723
13724 return end_template_parm_list (parameter_list);
13725 }
13726
13727 /* Parse a introduction-list.
13728
13729 introduction-list:
13730 introduced-parameter
13731 introduction-list , introduced-parameter
13732
13733 introduced-parameter:
13734 ...[opt] identifier
13735
13736 Returns a TREE_VEC of WILDCARD_DECLs. If the parameter is a pack
13737 then the introduced parm will have WILDCARD_PACK_P set. In addition, the
13738 WILDCARD_DECL will also have DECL_NAME set and token location in
13739 DECL_SOURCE_LOCATION. */
13740
13741 static tree
13742 cp_parser_introduction_list (cp_parser *parser)
13743 {
13744 vec<tree, va_gc> *introduction_vec = make_tree_vector ();
13745
13746 while (true)
13747 {
13748 bool is_pack = cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS);
13749 if (is_pack)
13750 cp_lexer_consume_token (parser->lexer);
13751
13752 /* Build placeholder. */
13753 tree parm = build_nt (WILDCARD_DECL);
13754 DECL_SOURCE_LOCATION (parm)
13755 = cp_lexer_peek_token (parser->lexer)->location;
13756 DECL_NAME (parm) = cp_parser_identifier (parser);
13757 WILDCARD_PACK_P (parm) = is_pack;
13758 vec_safe_push (introduction_vec, parm);
13759
13760 /* If the next token is not a `,', we're done. */
13761 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
13762 break;
13763 /* Otherwise, consume the `,' token. */
13764 cp_lexer_consume_token (parser->lexer);
13765 }
13766
13767 /* Convert the vec into a TREE_VEC. */
13768 tree introduction_list = make_tree_vec (introduction_vec->length ());
13769 unsigned int n;
13770 tree parm;
13771 FOR_EACH_VEC_ELT (*introduction_vec, n, parm)
13772 TREE_VEC_ELT (introduction_list, n) = parm;
13773
13774 release_tree_vector (introduction_vec);
13775 return introduction_list;
13776 }
13777
13778 /* Given a declarator, get the declarator-id part, or NULL_TREE if this
13779 is an abstract declarator. */
13780
13781 static inline cp_declarator*
13782 get_id_declarator (cp_declarator *declarator)
13783 {
13784 cp_declarator *d = declarator;
13785 while (d && d->kind != cdk_id)
13786 d = d->declarator;
13787 return d;
13788 }
13789
13790 /* Get the unqualified-id from the DECLARATOR or NULL_TREE if this
13791 is an abstract declarator. */
13792
13793 static inline tree
13794 get_unqualified_id (cp_declarator *declarator)
13795 {
13796 declarator = get_id_declarator (declarator);
13797 if (declarator)
13798 return declarator->u.id.unqualified_name;
13799 else
13800 return NULL_TREE;
13801 }
13802
13803 /* Returns true if DECL represents a constrained-parameter. */
13804
13805 static inline bool
13806 is_constrained_parameter (tree decl)
13807 {
13808 return (decl
13809 && TREE_CODE (decl) == TYPE_DECL
13810 && CONSTRAINED_PARM_CONCEPT (decl)
13811 && DECL_P (CONSTRAINED_PARM_CONCEPT (decl)));
13812 }
13813
13814 /* Returns true if PARM declares a constrained-parameter. */
13815
13816 static inline bool
13817 is_constrained_parameter (cp_parameter_declarator *parm)
13818 {
13819 return is_constrained_parameter (parm->decl_specifiers.type);
13820 }
13821
13822 /* Check that the type parameter is only a declarator-id, and that its
13823 type is not cv-qualified. */
13824
13825 bool
13826 cp_parser_check_constrained_type_parm (cp_parser *parser,
13827 cp_parameter_declarator *parm)
13828 {
13829 if (!parm->declarator)
13830 return true;
13831
13832 if (parm->declarator->kind != cdk_id)
13833 {
13834 cp_parser_error (parser, "invalid constrained type parameter");
13835 return false;
13836 }
13837
13838 /* Don't allow cv-qualified type parameters. */
13839 if (decl_spec_seq_has_spec_p (&parm->decl_specifiers, ds_const)
13840 || decl_spec_seq_has_spec_p (&parm->decl_specifiers, ds_volatile))
13841 {
13842 cp_parser_error (parser, "cv-qualified type parameter");
13843 return false;
13844 }
13845
13846 return true;
13847 }
13848
13849 /* Finish parsing/processing a template type parameter and checking
13850 various restrictions. */
13851
13852 static inline tree
13853 cp_parser_constrained_type_template_parm (cp_parser *parser,
13854 tree id,
13855 cp_parameter_declarator* parmdecl)
13856 {
13857 if (cp_parser_check_constrained_type_parm (parser, parmdecl))
13858 return finish_template_type_parm (class_type_node, id);
13859 else
13860 return error_mark_node;
13861 }
13862
13863 static tree
13864 finish_constrained_template_template_parm (tree proto, tree id)
13865 {
13866 /* FIXME: This should probably be copied, and we may need to adjust
13867 the template parameter depths. */
13868 tree saved_parms = current_template_parms;
13869 begin_template_parm_list ();
13870 current_template_parms = DECL_TEMPLATE_PARMS (proto);
13871 end_template_parm_list ();
13872
13873 tree parm = finish_template_template_parm (class_type_node, id);
13874 current_template_parms = saved_parms;
13875
13876 return parm;
13877 }
13878
13879 /* Finish parsing/processing a template template parameter by borrowing
13880 the template parameter list from the prototype parameter. */
13881
13882 static tree
13883 cp_parser_constrained_template_template_parm (cp_parser *parser,
13884 tree proto,
13885 tree id,
13886 cp_parameter_declarator *parmdecl)
13887 {
13888 if (!cp_parser_check_constrained_type_parm (parser, parmdecl))
13889 return error_mark_node;
13890 return finish_constrained_template_template_parm (proto, id);
13891 }
13892
13893 /* Create a new non-type template parameter from the given PARM
13894 declarator. */
13895
13896 static tree
13897 constrained_non_type_template_parm (bool *is_non_type,
13898 cp_parameter_declarator *parm)
13899 {
13900 *is_non_type = true;
13901 cp_declarator *decl = parm->declarator;
13902 cp_decl_specifier_seq *specs = &parm->decl_specifiers;
13903 specs->type = TREE_TYPE (DECL_INITIAL (specs->type));
13904 return grokdeclarator (decl, specs, TPARM, 0, NULL);
13905 }
13906
13907 /* Build a constrained template parameter based on the PARMDECL
13908 declarator. The type of PARMDECL is the constrained type, which
13909 refers to the prototype template parameter that ultimately
13910 specifies the type of the declared parameter. */
13911
13912 static tree
13913 finish_constrained_parameter (cp_parser *parser,
13914 cp_parameter_declarator *parmdecl,
13915 bool *is_non_type,
13916 bool *is_parameter_pack)
13917 {
13918 tree decl = parmdecl->decl_specifiers.type;
13919 tree id = get_unqualified_id (parmdecl->declarator);
13920 tree def = parmdecl->default_argument;
13921 tree proto = DECL_INITIAL (decl);
13922
13923 /* A template parameter constrained by a variadic concept shall also
13924 be declared as a template parameter pack. */
13925 bool is_variadic = template_parameter_pack_p (proto);
13926 if (is_variadic && !*is_parameter_pack)
13927 cp_parser_error (parser, "variadic constraint introduced without %<...%>");
13928
13929 /* Build the parameter. Return an error if the declarator was invalid. */
13930 tree parm;
13931 if (TREE_CODE (proto) == TYPE_DECL)
13932 parm = cp_parser_constrained_type_template_parm (parser, id, parmdecl);
13933 else if (TREE_CODE (proto) == TEMPLATE_DECL)
13934 parm = cp_parser_constrained_template_template_parm (parser, proto, id,
13935 parmdecl);
13936 else
13937 parm = constrained_non_type_template_parm (is_non_type, parmdecl);
13938 if (parm == error_mark_node)
13939 return error_mark_node;
13940
13941 /* Finish the parameter decl and create a node attaching the
13942 default argument and constraint. */
13943 parm = build_tree_list (def, parm);
13944 TEMPLATE_PARM_CONSTRAINTS (parm) = decl;
13945
13946 return parm;
13947 }
13948
13949 /* Returns true if the parsed type actually represents the declaration
13950 of a type template-parameter. */
13951
13952 static inline bool
13953 declares_constrained_type_template_parameter (tree type)
13954 {
13955 return (is_constrained_parameter (type)
13956 && TREE_CODE (TREE_TYPE (type)) == TEMPLATE_TYPE_PARM);
13957 }
13958
13959
13960 /* Returns true if the parsed type actually represents the declaration of
13961 a template template-parameter. */
13962
13963 static bool
13964 declares_constrained_template_template_parameter (tree type)
13965 {
13966 return (is_constrained_parameter (type)
13967 && TREE_CODE (TREE_TYPE (type)) == TEMPLATE_TEMPLATE_PARM);
13968 }
13969
13970 /* Parse a default argument for a type template-parameter.
13971 Note that diagnostics are handled in cp_parser_template_parameter. */
13972
13973 static tree
13974 cp_parser_default_type_template_argument (cp_parser *parser)
13975 {
13976 gcc_assert (cp_lexer_next_token_is (parser->lexer, CPP_EQ));
13977
13978 /* Consume the `=' token. */
13979 cp_lexer_consume_token (parser->lexer);
13980
13981 /* Parse the default-argument. */
13982 push_deferring_access_checks (dk_no_deferred);
13983 tree default_argument = cp_parser_type_id (parser);
13984 pop_deferring_access_checks ();
13985
13986 return default_argument;
13987 }
13988
13989 /* Parse a default argument for a template template-parameter. */
13990
13991 static tree
13992 cp_parser_default_template_template_argument (cp_parser *parser)
13993 {
13994 gcc_assert (cp_lexer_next_token_is (parser->lexer, CPP_EQ));
13995
13996 bool is_template;
13997
13998 /* Consume the `='. */
13999 cp_lexer_consume_token (parser->lexer);
14000 /* Parse the id-expression. */
14001 push_deferring_access_checks (dk_no_deferred);
14002 /* save token before parsing the id-expression, for error
14003 reporting */
14004 const cp_token* token = cp_lexer_peek_token (parser->lexer);
14005 tree default_argument
14006 = cp_parser_id_expression (parser,
14007 /*template_keyword_p=*/false,
14008 /*check_dependency_p=*/true,
14009 /*template_p=*/&is_template,
14010 /*declarator_p=*/false,
14011 /*optional_p=*/false);
14012 if (TREE_CODE (default_argument) == TYPE_DECL)
14013 /* If the id-expression was a template-id that refers to
14014 a template-class, we already have the declaration here,
14015 so no further lookup is needed. */
14016 ;
14017 else
14018 /* Look up the name. */
14019 default_argument
14020 = cp_parser_lookup_name (parser, default_argument,
14021 none_type,
14022 /*is_template=*/is_template,
14023 /*is_namespace=*/false,
14024 /*check_dependency=*/true,
14025 /*ambiguous_decls=*/NULL,
14026 token->location);
14027 /* See if the default argument is valid. */
14028 default_argument = check_template_template_default_arg (default_argument);
14029 pop_deferring_access_checks ();
14030 return default_argument;
14031 }
14032
14033 /* Parse a template-parameter.
14034
14035 template-parameter:
14036 type-parameter
14037 parameter-declaration
14038
14039 If all goes well, returns a TREE_LIST. The TREE_VALUE represents
14040 the parameter. The TREE_PURPOSE is the default value, if any.
14041 Returns ERROR_MARK_NODE on failure. *IS_NON_TYPE is set to true
14042 iff this parameter is a non-type parameter. *IS_PARAMETER_PACK is
14043 set to true iff this parameter is a parameter pack. */
14044
14045 static tree
14046 cp_parser_template_parameter (cp_parser* parser, bool *is_non_type,
14047 bool *is_parameter_pack)
14048 {
14049 cp_token *token;
14050 cp_parameter_declarator *parameter_declarator;
14051 tree parm;
14052
14053 /* Assume it is a type parameter or a template parameter. */
14054 *is_non_type = false;
14055 /* Assume it not a parameter pack. */
14056 *is_parameter_pack = false;
14057 /* Peek at the next token. */
14058 token = cp_lexer_peek_token (parser->lexer);
14059 /* If it is `class' or `template', we have a type-parameter. */
14060 if (token->keyword == RID_TEMPLATE)
14061 return cp_parser_type_parameter (parser, is_parameter_pack);
14062 /* If it is `class' or `typename' we do not know yet whether it is a
14063 type parameter or a non-type parameter. Consider:
14064
14065 template <typename T, typename T::X X> ...
14066
14067 or:
14068
14069 template <class C, class D*> ...
14070
14071 Here, the first parameter is a type parameter, and the second is
14072 a non-type parameter. We can tell by looking at the token after
14073 the identifier -- if it is a `,', `=', or `>' then we have a type
14074 parameter. */
14075 if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
14076 {
14077 /* Peek at the token after `class' or `typename'. */
14078 token = cp_lexer_peek_nth_token (parser->lexer, 2);
14079 /* If it's an ellipsis, we have a template type parameter
14080 pack. */
14081 if (token->type == CPP_ELLIPSIS)
14082 return cp_parser_type_parameter (parser, is_parameter_pack);
14083 /* If it's an identifier, skip it. */
14084 if (token->type == CPP_NAME)
14085 token = cp_lexer_peek_nth_token (parser->lexer, 3);
14086 /* Now, see if the token looks like the end of a template
14087 parameter. */
14088 if (token->type == CPP_COMMA
14089 || token->type == CPP_EQ
14090 || token->type == CPP_GREATER)
14091 return cp_parser_type_parameter (parser, is_parameter_pack);
14092 }
14093
14094 /* Otherwise, it is a non-type parameter or a constrained parameter.
14095
14096 [temp.param]
14097
14098 When parsing a default template-argument for a non-type
14099 template-parameter, the first non-nested `>' is taken as the end
14100 of the template parameter-list rather than a greater-than
14101 operator. */
14102 parameter_declarator
14103 = cp_parser_parameter_declaration (parser, /*template_parm_p=*/true,
14104 /*parenthesized_p=*/NULL);
14105
14106 if (!parameter_declarator)
14107 return error_mark_node;
14108
14109 /* If the parameter declaration is marked as a parameter pack, set
14110 *IS_PARAMETER_PACK to notify the caller. */
14111 if (parameter_declarator->template_parameter_pack_p)
14112 *is_parameter_pack = true;
14113
14114 if (parameter_declarator->default_argument)
14115 {
14116 /* Can happen in some cases of erroneous input (c++/34892). */
14117 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
14118 /* Consume the `...' for better error recovery. */
14119 cp_lexer_consume_token (parser->lexer);
14120 }
14121
14122 // The parameter may have been constrained.
14123 if (is_constrained_parameter (parameter_declarator))
14124 return finish_constrained_parameter (parser,
14125 parameter_declarator,
14126 is_non_type,
14127 is_parameter_pack);
14128
14129 // Now we're sure that the parameter is a non-type parameter.
14130 *is_non_type = true;
14131
14132 parm = grokdeclarator (parameter_declarator->declarator,
14133 &parameter_declarator->decl_specifiers,
14134 TPARM, /*initialized=*/0,
14135 /*attrlist=*/NULL);
14136 if (parm == error_mark_node)
14137 return error_mark_node;
14138
14139 return build_tree_list (parameter_declarator->default_argument, parm);
14140 }
14141
14142 /* Parse a type-parameter.
14143
14144 type-parameter:
14145 class identifier [opt]
14146 class identifier [opt] = type-id
14147 typename identifier [opt]
14148 typename identifier [opt] = type-id
14149 template < template-parameter-list > class identifier [opt]
14150 template < template-parameter-list > class identifier [opt]
14151 = id-expression
14152
14153 GNU Extension (variadic templates):
14154
14155 type-parameter:
14156 class ... identifier [opt]
14157 typename ... identifier [opt]
14158
14159 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
14160 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
14161 the declaration of the parameter.
14162
14163 Sets *IS_PARAMETER_PACK if this is a template parameter pack. */
14164
14165 static tree
14166 cp_parser_type_parameter (cp_parser* parser, bool *is_parameter_pack)
14167 {
14168 cp_token *token;
14169 tree parameter;
14170
14171 /* Look for a keyword to tell us what kind of parameter this is. */
14172 token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_TYPENAME_TEMPLATE);
14173 if (!token)
14174 return error_mark_node;
14175
14176 switch (token->keyword)
14177 {
14178 case RID_CLASS:
14179 case RID_TYPENAME:
14180 {
14181 tree identifier;
14182 tree default_argument;
14183
14184 /* If the next token is an ellipsis, we have a template
14185 argument pack. */
14186 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
14187 {
14188 /* Consume the `...' token. */
14189 cp_lexer_consume_token (parser->lexer);
14190 maybe_warn_variadic_templates ();
14191
14192 *is_parameter_pack = true;
14193 }
14194
14195 /* If the next token is an identifier, then it names the
14196 parameter. */
14197 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
14198 identifier = cp_parser_identifier (parser);
14199 else
14200 identifier = NULL_TREE;
14201
14202 /* Create the parameter. */
14203 parameter = finish_template_type_parm (class_type_node, identifier);
14204
14205 /* If the next token is an `=', we have a default argument. */
14206 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
14207 {
14208 default_argument
14209 = cp_parser_default_type_template_argument (parser);
14210
14211 /* Template parameter packs cannot have default
14212 arguments. */
14213 if (*is_parameter_pack)
14214 {
14215 if (identifier)
14216 error_at (token->location,
14217 "template parameter pack %qD cannot have a "
14218 "default argument", identifier);
14219 else
14220 error_at (token->location,
14221 "template parameter packs cannot have "
14222 "default arguments");
14223 default_argument = NULL_TREE;
14224 }
14225 else if (check_for_bare_parameter_packs (default_argument))
14226 default_argument = error_mark_node;
14227 }
14228 else
14229 default_argument = NULL_TREE;
14230
14231 /* Create the combined representation of the parameter and the
14232 default argument. */
14233 parameter = build_tree_list (default_argument, parameter);
14234 }
14235 break;
14236
14237 case RID_TEMPLATE:
14238 {
14239 tree identifier;
14240 tree default_argument;
14241
14242 /* Look for the `<'. */
14243 cp_parser_require (parser, CPP_LESS, RT_LESS);
14244 /* Parse the template-parameter-list. */
14245 cp_parser_template_parameter_list (parser);
14246 /* Look for the `>'. */
14247 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
14248
14249 // If template requirements are present, parse them.
14250 if (flag_concepts)
14251 {
14252 tree reqs = get_shorthand_constraints (current_template_parms);
14253 if (tree r = cp_parser_requires_clause_opt (parser))
14254 reqs = conjoin_constraints (reqs, make_predicate_constraint (r));
14255 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) = reqs;
14256 }
14257
14258 /* Look for the `class' or 'typename' keywords. */
14259 cp_parser_type_parameter_key (parser);
14260 /* If the next token is an ellipsis, we have a template
14261 argument pack. */
14262 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
14263 {
14264 /* Consume the `...' token. */
14265 cp_lexer_consume_token (parser->lexer);
14266 maybe_warn_variadic_templates ();
14267
14268 *is_parameter_pack = true;
14269 }
14270 /* If the next token is an `=', then there is a
14271 default-argument. If the next token is a `>', we are at
14272 the end of the parameter-list. If the next token is a `,',
14273 then we are at the end of this parameter. */
14274 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
14275 && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
14276 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
14277 {
14278 identifier = cp_parser_identifier (parser);
14279 /* Treat invalid names as if the parameter were nameless. */
14280 if (identifier == error_mark_node)
14281 identifier = NULL_TREE;
14282 }
14283 else
14284 identifier = NULL_TREE;
14285
14286 /* Create the template parameter. */
14287 parameter = finish_template_template_parm (class_type_node,
14288 identifier);
14289
14290 /* If the next token is an `=', then there is a
14291 default-argument. */
14292 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
14293 {
14294 default_argument
14295 = cp_parser_default_template_template_argument (parser);
14296
14297 /* Template parameter packs cannot have default
14298 arguments. */
14299 if (*is_parameter_pack)
14300 {
14301 if (identifier)
14302 error_at (token->location,
14303 "template parameter pack %qD cannot "
14304 "have a default argument",
14305 identifier);
14306 else
14307 error_at (token->location, "template parameter packs cannot "
14308 "have default arguments");
14309 default_argument = NULL_TREE;
14310 }
14311 }
14312 else
14313 default_argument = NULL_TREE;
14314
14315 /* Create the combined representation of the parameter and the
14316 default argument. */
14317 parameter = build_tree_list (default_argument, parameter);
14318 }
14319 break;
14320
14321 default:
14322 gcc_unreachable ();
14323 break;
14324 }
14325
14326 return parameter;
14327 }
14328
14329 /* Parse a template-id.
14330
14331 template-id:
14332 template-name < template-argument-list [opt] >
14333
14334 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
14335 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
14336 returned. Otherwise, if the template-name names a function, or set
14337 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
14338 names a class, returns a TYPE_DECL for the specialization.
14339
14340 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
14341 uninstantiated templates. */
14342
14343 static tree
14344 cp_parser_template_id (cp_parser *parser,
14345 bool template_keyword_p,
14346 bool check_dependency_p,
14347 enum tag_types tag_type,
14348 bool is_declaration)
14349 {
14350 int i;
14351 tree templ;
14352 tree arguments;
14353 tree template_id;
14354 cp_token_position start_of_id = 0;
14355 deferred_access_check *chk;
14356 vec<deferred_access_check, va_gc> *access_check;
14357 cp_token *next_token = NULL, *next_token_2 = NULL;
14358 bool is_identifier;
14359
14360 /* If the next token corresponds to a template-id, there is no need
14361 to reparse it. */
14362 next_token = cp_lexer_peek_token (parser->lexer);
14363 if (next_token->type == CPP_TEMPLATE_ID)
14364 {
14365 struct tree_check *check_value;
14366
14367 /* Get the stored value. */
14368 check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
14369 /* Perform any access checks that were deferred. */
14370 access_check = check_value->checks;
14371 if (access_check)
14372 {
14373 FOR_EACH_VEC_ELT (*access_check, i, chk)
14374 perform_or_defer_access_check (chk->binfo,
14375 chk->decl,
14376 chk->diag_decl,
14377 tf_warning_or_error);
14378 }
14379 /* Return the stored value. */
14380 return check_value->value;
14381 }
14382
14383 /* Avoid performing name lookup if there is no possibility of
14384 finding a template-id. */
14385 if ((next_token->type != CPP_NAME && next_token->keyword != RID_OPERATOR)
14386 || (next_token->type == CPP_NAME
14387 && !cp_parser_nth_token_starts_template_argument_list_p
14388 (parser, 2)))
14389 {
14390 cp_parser_error (parser, "expected template-id");
14391 return error_mark_node;
14392 }
14393
14394 /* Remember where the template-id starts. */
14395 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
14396 start_of_id = cp_lexer_token_position (parser->lexer, false);
14397
14398 push_deferring_access_checks (dk_deferred);
14399
14400 /* Parse the template-name. */
14401 is_identifier = false;
14402 templ = cp_parser_template_name (parser, template_keyword_p,
14403 check_dependency_p,
14404 is_declaration,
14405 tag_type,
14406 &is_identifier);
14407 if (templ == error_mark_node || is_identifier)
14408 {
14409 pop_deferring_access_checks ();
14410 return templ;
14411 }
14412
14413 /* If we find the sequence `[:' after a template-name, it's probably
14414 a digraph-typo for `< ::'. Substitute the tokens and check if we can
14415 parse correctly the argument list. */
14416 next_token = cp_lexer_peek_token (parser->lexer);
14417 next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2);
14418 if (next_token->type == CPP_OPEN_SQUARE
14419 && next_token->flags & DIGRAPH
14420 && next_token_2->type == CPP_COLON
14421 && !(next_token_2->flags & PREV_WHITE))
14422 {
14423 cp_parser_parse_tentatively (parser);
14424 /* Change `:' into `::'. */
14425 next_token_2->type = CPP_SCOPE;
14426 /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
14427 CPP_LESS. */
14428 cp_lexer_consume_token (parser->lexer);
14429
14430 /* Parse the arguments. */
14431 arguments = cp_parser_enclosed_template_argument_list (parser);
14432 if (!cp_parser_parse_definitely (parser))
14433 {
14434 /* If we couldn't parse an argument list, then we revert our changes
14435 and return simply an error. Maybe this is not a template-id
14436 after all. */
14437 next_token_2->type = CPP_COLON;
14438 cp_parser_error (parser, "expected %<<%>");
14439 pop_deferring_access_checks ();
14440 return error_mark_node;
14441 }
14442 /* Otherwise, emit an error about the invalid digraph, but continue
14443 parsing because we got our argument list. */
14444 if (permerror (next_token->location,
14445 "%<<::%> cannot begin a template-argument list"))
14446 {
14447 static bool hint = false;
14448 inform (next_token->location,
14449 "%<<:%> is an alternate spelling for %<[%>."
14450 " Insert whitespace between %<<%> and %<::%>");
14451 if (!hint && !flag_permissive)
14452 {
14453 inform (next_token->location, "(if you use %<-fpermissive%> "
14454 "or %<-std=c++11%>, or %<-std=gnu++11%> G++ will "
14455 "accept your code)");
14456 hint = true;
14457 }
14458 }
14459 }
14460 else
14461 {
14462 /* Look for the `<' that starts the template-argument-list. */
14463 if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
14464 {
14465 pop_deferring_access_checks ();
14466 return error_mark_node;
14467 }
14468 /* Parse the arguments. */
14469 arguments = cp_parser_enclosed_template_argument_list (parser);
14470 }
14471
14472 /* Build a representation of the specialization. */
14473 if (identifier_p (templ))
14474 template_id = build_min_nt_loc (next_token->location,
14475 TEMPLATE_ID_EXPR,
14476 templ, arguments);
14477 else if (DECL_TYPE_TEMPLATE_P (templ)
14478 || DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
14479 {
14480 bool entering_scope;
14481 /* In "template <typename T> ... A<T>::", A<T> is the abstract A
14482 template (rather than some instantiation thereof) only if
14483 is not nested within some other construct. For example, in
14484 "template <typename T> void f(T) { A<T>::", A<T> is just an
14485 instantiation of A. */
14486 entering_scope = (template_parm_scope_p ()
14487 && cp_lexer_next_token_is (parser->lexer,
14488 CPP_SCOPE));
14489 template_id
14490 = finish_template_type (templ, arguments, entering_scope);
14491 }
14492 /* A template-like identifier may be a partial concept id. */
14493 else if (flag_concepts
14494 && (template_id = (cp_parser_maybe_partial_concept_id
14495 (parser, templ, arguments))))
14496 return template_id;
14497 else if (variable_template_p (templ))
14498 {
14499 template_id = lookup_template_variable (templ, arguments);
14500 if (TREE_CODE (template_id) == TEMPLATE_ID_EXPR)
14501 SET_EXPR_LOCATION (template_id, next_token->location);
14502 }
14503 else
14504 {
14505 /* If it's not a class-template or a template-template, it should be
14506 a function-template. */
14507 gcc_assert ((DECL_FUNCTION_TEMPLATE_P (templ)
14508 || TREE_CODE (templ) == OVERLOAD
14509 || BASELINK_P (templ)));
14510
14511 template_id = lookup_template_function (templ, arguments);
14512 if (TREE_CODE (template_id) == TEMPLATE_ID_EXPR)
14513 SET_EXPR_LOCATION (template_id, next_token->location);
14514 }
14515
14516 /* If parsing tentatively, replace the sequence of tokens that makes
14517 up the template-id with a CPP_TEMPLATE_ID token. That way,
14518 should we re-parse the token stream, we will not have to repeat
14519 the effort required to do the parse, nor will we issue duplicate
14520 error messages about problems during instantiation of the
14521 template. */
14522 if (start_of_id
14523 /* Don't do this if we had a parse error in a declarator; re-parsing
14524 might succeed if a name changes meaning (60361). */
14525 && !(cp_parser_error_occurred (parser)
14526 && cp_parser_parsing_tentatively (parser)
14527 && parser->in_declarator_p))
14528 {
14529 cp_token *token = cp_lexer_token_at (parser->lexer, start_of_id);
14530
14531 /* Reset the contents of the START_OF_ID token. */
14532 token->type = CPP_TEMPLATE_ID;
14533 /* Retrieve any deferred checks. Do not pop this access checks yet
14534 so the memory will not be reclaimed during token replacing below. */
14535 token->u.tree_check_value = ggc_cleared_alloc<struct tree_check> ();
14536 token->u.tree_check_value->value = template_id;
14537 token->u.tree_check_value->checks = get_deferred_access_checks ();
14538 token->keyword = RID_MAX;
14539
14540 /* Purge all subsequent tokens. */
14541 cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
14542
14543 /* ??? Can we actually assume that, if template_id ==
14544 error_mark_node, we will have issued a diagnostic to the
14545 user, as opposed to simply marking the tentative parse as
14546 failed? */
14547 if (cp_parser_error_occurred (parser) && template_id != error_mark_node)
14548 error_at (token->location, "parse error in template argument list");
14549 }
14550
14551 pop_to_parent_deferring_access_checks ();
14552 return template_id;
14553 }
14554
14555 /* Parse a template-name.
14556
14557 template-name:
14558 identifier
14559
14560 The standard should actually say:
14561
14562 template-name:
14563 identifier
14564 operator-function-id
14565
14566 A defect report has been filed about this issue.
14567
14568 A conversion-function-id cannot be a template name because they cannot
14569 be part of a template-id. In fact, looking at this code:
14570
14571 a.operator K<int>()
14572
14573 the conversion-function-id is "operator K<int>", and K<int> is a type-id.
14574 It is impossible to call a templated conversion-function-id with an
14575 explicit argument list, since the only allowed template parameter is
14576 the type to which it is converting.
14577
14578 If TEMPLATE_KEYWORD_P is true, then we have just seen the
14579 `template' keyword, in a construction like:
14580
14581 T::template f<3>()
14582
14583 In that case `f' is taken to be a template-name, even though there
14584 is no way of knowing for sure.
14585
14586 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
14587 name refers to a set of overloaded functions, at least one of which
14588 is a template, or an IDENTIFIER_NODE with the name of the template,
14589 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
14590 names are looked up inside uninstantiated templates. */
14591
14592 static tree
14593 cp_parser_template_name (cp_parser* parser,
14594 bool template_keyword_p,
14595 bool check_dependency_p,
14596 bool is_declaration,
14597 enum tag_types tag_type,
14598 bool *is_identifier)
14599 {
14600 tree identifier;
14601 tree decl;
14602 tree fns;
14603 cp_token *token = cp_lexer_peek_token (parser->lexer);
14604
14605 /* If the next token is `operator', then we have either an
14606 operator-function-id or a conversion-function-id. */
14607 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
14608 {
14609 /* We don't know whether we're looking at an
14610 operator-function-id or a conversion-function-id. */
14611 cp_parser_parse_tentatively (parser);
14612 /* Try an operator-function-id. */
14613 identifier = cp_parser_operator_function_id (parser);
14614 /* If that didn't work, try a conversion-function-id. */
14615 if (!cp_parser_parse_definitely (parser))
14616 {
14617 cp_parser_error (parser, "expected template-name");
14618 return error_mark_node;
14619 }
14620 }
14621 /* Look for the identifier. */
14622 else
14623 identifier = cp_parser_identifier (parser);
14624
14625 /* If we didn't find an identifier, we don't have a template-id. */
14626 if (identifier == error_mark_node)
14627 return error_mark_node;
14628
14629 /* If the name immediately followed the `template' keyword, then it
14630 is a template-name. However, if the next token is not `<', then
14631 we do not treat it as a template-name, since it is not being used
14632 as part of a template-id. This enables us to handle constructs
14633 like:
14634
14635 template <typename T> struct S { S(); };
14636 template <typename T> S<T>::S();
14637
14638 correctly. We would treat `S' as a template -- if it were `S<T>'
14639 -- but we do not if there is no `<'. */
14640
14641 if (processing_template_decl
14642 && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
14643 {
14644 /* In a declaration, in a dependent context, we pretend that the
14645 "template" keyword was present in order to improve error
14646 recovery. For example, given:
14647
14648 template <typename T> void f(T::X<int>);
14649
14650 we want to treat "X<int>" as a template-id. */
14651 if (is_declaration
14652 && !template_keyword_p
14653 && parser->scope && TYPE_P (parser->scope)
14654 && check_dependency_p
14655 && dependent_scope_p (parser->scope)
14656 /* Do not do this for dtors (or ctors), since they never
14657 need the template keyword before their name. */
14658 && !constructor_name_p (identifier, parser->scope))
14659 {
14660 cp_token_position start = 0;
14661
14662 /* Explain what went wrong. */
14663 error_at (token->location, "non-template %qD used as template",
14664 identifier);
14665 inform (token->location, "use %<%T::template %D%> to indicate that it is a template",
14666 parser->scope, identifier);
14667 /* If parsing tentatively, find the location of the "<" token. */
14668 if (cp_parser_simulate_error (parser))
14669 start = cp_lexer_token_position (parser->lexer, true);
14670 /* Parse the template arguments so that we can issue error
14671 messages about them. */
14672 cp_lexer_consume_token (parser->lexer);
14673 cp_parser_enclosed_template_argument_list (parser);
14674 /* Skip tokens until we find a good place from which to
14675 continue parsing. */
14676 cp_parser_skip_to_closing_parenthesis (parser,
14677 /*recovering=*/true,
14678 /*or_comma=*/true,
14679 /*consume_paren=*/false);
14680 /* If parsing tentatively, permanently remove the
14681 template argument list. That will prevent duplicate
14682 error messages from being issued about the missing
14683 "template" keyword. */
14684 if (start)
14685 cp_lexer_purge_tokens_after (parser->lexer, start);
14686 if (is_identifier)
14687 *is_identifier = true;
14688 return identifier;
14689 }
14690
14691 /* If the "template" keyword is present, then there is generally
14692 no point in doing name-lookup, so we just return IDENTIFIER.
14693 But, if the qualifying scope is non-dependent then we can
14694 (and must) do name-lookup normally. */
14695 if (template_keyword_p
14696 && (!parser->scope
14697 || (TYPE_P (parser->scope)
14698 && dependent_type_p (parser->scope))))
14699 return identifier;
14700 }
14701
14702 /* Look up the name. */
14703 decl = cp_parser_lookup_name (parser, identifier,
14704 tag_type,
14705 /*is_template=*/true,
14706 /*is_namespace=*/false,
14707 check_dependency_p,
14708 /*ambiguous_decls=*/NULL,
14709 token->location);
14710
14711 decl = strip_using_decl (decl);
14712
14713 /* If DECL is a template, then the name was a template-name. */
14714 if (TREE_CODE (decl) == TEMPLATE_DECL)
14715 {
14716 if (TREE_DEPRECATED (decl)
14717 && deprecated_state != DEPRECATED_SUPPRESS)
14718 warn_deprecated_use (decl, NULL_TREE);
14719 }
14720 else
14721 {
14722 tree fn = NULL_TREE;
14723
14724 /* The standard does not explicitly indicate whether a name that
14725 names a set of overloaded declarations, some of which are
14726 templates, is a template-name. However, such a name should
14727 be a template-name; otherwise, there is no way to form a
14728 template-id for the overloaded templates. */
14729 fns = BASELINK_P (decl) ? BASELINK_FUNCTIONS (decl) : decl;
14730 if (TREE_CODE (fns) == OVERLOAD)
14731 for (fn = fns; fn; fn = OVL_NEXT (fn))
14732 if (TREE_CODE (OVL_CURRENT (fn)) == TEMPLATE_DECL)
14733 break;
14734
14735 if (!fn)
14736 {
14737 /* The name does not name a template. */
14738 cp_parser_error (parser, "expected template-name");
14739 return error_mark_node;
14740 }
14741 }
14742
14743 /* If DECL is dependent, and refers to a function, then just return
14744 its name; we will look it up again during template instantiation. */
14745 if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
14746 {
14747 tree scope = ovl_scope (decl);
14748 if (TYPE_P (scope) && dependent_type_p (scope))
14749 return identifier;
14750 }
14751
14752 return decl;
14753 }
14754
14755 /* Parse a template-argument-list.
14756
14757 template-argument-list:
14758 template-argument ... [opt]
14759 template-argument-list , template-argument ... [opt]
14760
14761 Returns a TREE_VEC containing the arguments. */
14762
14763 static tree
14764 cp_parser_template_argument_list (cp_parser* parser)
14765 {
14766 tree fixed_args[10];
14767 unsigned n_args = 0;
14768 unsigned alloced = 10;
14769 tree *arg_ary = fixed_args;
14770 tree vec;
14771 bool saved_in_template_argument_list_p;
14772 bool saved_ice_p;
14773 bool saved_non_ice_p;
14774
14775 saved_in_template_argument_list_p = parser->in_template_argument_list_p;
14776 parser->in_template_argument_list_p = true;
14777 /* Even if the template-id appears in an integral
14778 constant-expression, the contents of the argument list do
14779 not. */
14780 saved_ice_p = parser->integral_constant_expression_p;
14781 parser->integral_constant_expression_p = false;
14782 saved_non_ice_p = parser->non_integral_constant_expression_p;
14783 parser->non_integral_constant_expression_p = false;
14784
14785 /* Parse the arguments. */
14786 do
14787 {
14788 tree argument;
14789
14790 if (n_args)
14791 /* Consume the comma. */
14792 cp_lexer_consume_token (parser->lexer);
14793
14794 /* Parse the template-argument. */
14795 argument = cp_parser_template_argument (parser);
14796
14797 /* If the next token is an ellipsis, we're expanding a template
14798 argument pack. */
14799 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
14800 {
14801 if (argument == error_mark_node)
14802 {
14803 cp_token *token = cp_lexer_peek_token (parser->lexer);
14804 error_at (token->location,
14805 "expected parameter pack before %<...%>");
14806 }
14807 /* Consume the `...' token. */
14808 cp_lexer_consume_token (parser->lexer);
14809
14810 /* Make the argument into a TYPE_PACK_EXPANSION or
14811 EXPR_PACK_EXPANSION. */
14812 argument = make_pack_expansion (argument);
14813 }
14814
14815 if (n_args == alloced)
14816 {
14817 alloced *= 2;
14818
14819 if (arg_ary == fixed_args)
14820 {
14821 arg_ary = XNEWVEC (tree, alloced);
14822 memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
14823 }
14824 else
14825 arg_ary = XRESIZEVEC (tree, arg_ary, alloced);
14826 }
14827 arg_ary[n_args++] = argument;
14828 }
14829 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
14830
14831 vec = make_tree_vec (n_args);
14832
14833 while (n_args--)
14834 TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
14835
14836 if (arg_ary != fixed_args)
14837 free (arg_ary);
14838 parser->non_integral_constant_expression_p = saved_non_ice_p;
14839 parser->integral_constant_expression_p = saved_ice_p;
14840 parser->in_template_argument_list_p = saved_in_template_argument_list_p;
14841 if (CHECKING_P)
14842 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec, TREE_VEC_LENGTH (vec));
14843 return vec;
14844 }
14845
14846 /* Parse a template-argument.
14847
14848 template-argument:
14849 assignment-expression
14850 type-id
14851 id-expression
14852
14853 The representation is that of an assignment-expression, type-id, or
14854 id-expression -- except that the qualified id-expression is
14855 evaluated, so that the value returned is either a DECL or an
14856 OVERLOAD.
14857
14858 Although the standard says "assignment-expression", it forbids
14859 throw-expressions or assignments in the template argument.
14860 Therefore, we use "conditional-expression" instead. */
14861
14862 static tree
14863 cp_parser_template_argument (cp_parser* parser)
14864 {
14865 tree argument;
14866 bool template_p;
14867 bool address_p;
14868 bool maybe_type_id = false;
14869 cp_token *token = NULL, *argument_start_token = NULL;
14870 location_t loc = 0;
14871 cp_id_kind idk;
14872
14873 /* There's really no way to know what we're looking at, so we just
14874 try each alternative in order.
14875
14876 [temp.arg]
14877
14878 In a template-argument, an ambiguity between a type-id and an
14879 expression is resolved to a type-id, regardless of the form of
14880 the corresponding template-parameter.
14881
14882 Therefore, we try a type-id first. */
14883 cp_parser_parse_tentatively (parser);
14884 argument = cp_parser_template_type_arg (parser);
14885 /* If there was no error parsing the type-id but the next token is a
14886 '>>', our behavior depends on which dialect of C++ we're
14887 parsing. In C++98, we probably found a typo for '> >'. But there
14888 are type-id which are also valid expressions. For instance:
14889
14890 struct X { int operator >> (int); };
14891 template <int V> struct Foo {};
14892 Foo<X () >> 5> r;
14893
14894 Here 'X()' is a valid type-id of a function type, but the user just
14895 wanted to write the expression "X() >> 5". Thus, we remember that we
14896 found a valid type-id, but we still try to parse the argument as an
14897 expression to see what happens.
14898
14899 In C++0x, the '>>' will be considered two separate '>'
14900 tokens. */
14901 if (!cp_parser_error_occurred (parser)
14902 && cxx_dialect == cxx98
14903 && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
14904 {
14905 maybe_type_id = true;
14906 cp_parser_abort_tentative_parse (parser);
14907 }
14908 else
14909 {
14910 /* If the next token isn't a `,' or a `>', then this argument wasn't
14911 really finished. This means that the argument is not a valid
14912 type-id. */
14913 if (!cp_parser_next_token_ends_template_argument_p (parser))
14914 cp_parser_error (parser, "expected template-argument");
14915 /* If that worked, we're done. */
14916 if (cp_parser_parse_definitely (parser))
14917 return argument;
14918 }
14919 /* We're still not sure what the argument will be. */
14920 cp_parser_parse_tentatively (parser);
14921 /* Try a template. */
14922 argument_start_token = cp_lexer_peek_token (parser->lexer);
14923 argument = cp_parser_id_expression (parser,
14924 /*template_keyword_p=*/false,
14925 /*check_dependency_p=*/true,
14926 &template_p,
14927 /*declarator_p=*/false,
14928 /*optional_p=*/false);
14929 /* If the next token isn't a `,' or a `>', then this argument wasn't
14930 really finished. */
14931 if (!cp_parser_next_token_ends_template_argument_p (parser))
14932 cp_parser_error (parser, "expected template-argument");
14933 if (!cp_parser_error_occurred (parser))
14934 {
14935 /* Figure out what is being referred to. If the id-expression
14936 was for a class template specialization, then we will have a
14937 TYPE_DECL at this point. There is no need to do name lookup
14938 at this point in that case. */
14939 if (TREE_CODE (argument) != TYPE_DECL)
14940 argument = cp_parser_lookup_name (parser, argument,
14941 none_type,
14942 /*is_template=*/template_p,
14943 /*is_namespace=*/false,
14944 /*check_dependency=*/true,
14945 /*ambiguous_decls=*/NULL,
14946 argument_start_token->location);
14947 /* Handle a constrained-type-specifier for a non-type template
14948 parameter. */
14949 if (tree decl = cp_parser_maybe_concept_name (parser, argument))
14950 argument = decl;
14951 else if (TREE_CODE (argument) != TEMPLATE_DECL
14952 && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
14953 cp_parser_error (parser, "expected template-name");
14954 }
14955 if (cp_parser_parse_definitely (parser))
14956 {
14957 if (TREE_DEPRECATED (argument))
14958 warn_deprecated_use (argument, NULL_TREE);
14959 return argument;
14960 }
14961 /* It must be a non-type argument. In C++17 any constant-expression is
14962 allowed. */
14963 if (cxx_dialect > cxx14)
14964 goto general_expr;
14965
14966 /* Otherwise, the permitted cases are given in [temp.arg.nontype]:
14967
14968 -- an integral constant-expression of integral or enumeration
14969 type; or
14970
14971 -- the name of a non-type template-parameter; or
14972
14973 -- the name of an object or function with external linkage...
14974
14975 -- the address of an object or function with external linkage...
14976
14977 -- a pointer to member... */
14978 /* Look for a non-type template parameter. */
14979 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
14980 {
14981 cp_parser_parse_tentatively (parser);
14982 argument = cp_parser_primary_expression (parser,
14983 /*address_p=*/false,
14984 /*cast_p=*/false,
14985 /*template_arg_p=*/true,
14986 &idk);
14987 if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
14988 || !cp_parser_next_token_ends_template_argument_p (parser))
14989 cp_parser_simulate_error (parser);
14990 if (cp_parser_parse_definitely (parser))
14991 return argument;
14992 }
14993
14994 /* If the next token is "&", the argument must be the address of an
14995 object or function with external linkage. */
14996 address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
14997 if (address_p)
14998 {
14999 loc = cp_lexer_peek_token (parser->lexer)->location;
15000 cp_lexer_consume_token (parser->lexer);
15001 }
15002 /* See if we might have an id-expression. */
15003 token = cp_lexer_peek_token (parser->lexer);
15004 if (token->type == CPP_NAME
15005 || token->keyword == RID_OPERATOR
15006 || token->type == CPP_SCOPE
15007 || token->type == CPP_TEMPLATE_ID
15008 || token->type == CPP_NESTED_NAME_SPECIFIER)
15009 {
15010 cp_parser_parse_tentatively (parser);
15011 argument = cp_parser_primary_expression (parser,
15012 address_p,
15013 /*cast_p=*/false,
15014 /*template_arg_p=*/true,
15015 &idk);
15016 if (cp_parser_error_occurred (parser)
15017 || !cp_parser_next_token_ends_template_argument_p (parser))
15018 cp_parser_abort_tentative_parse (parser);
15019 else
15020 {
15021 tree probe;
15022
15023 if (INDIRECT_REF_P (argument))
15024 {
15025 /* Strip the dereference temporarily. */
15026 gcc_assert (REFERENCE_REF_P (argument));
15027 argument = TREE_OPERAND (argument, 0);
15028 }
15029
15030 /* If we're in a template, we represent a qualified-id referring
15031 to a static data member as a SCOPE_REF even if the scope isn't
15032 dependent so that we can check access control later. */
15033 probe = argument;
15034 if (TREE_CODE (probe) == SCOPE_REF)
15035 probe = TREE_OPERAND (probe, 1);
15036 if (VAR_P (probe))
15037 {
15038 /* A variable without external linkage might still be a
15039 valid constant-expression, so no error is issued here
15040 if the external-linkage check fails. */
15041 if (!address_p && !DECL_EXTERNAL_LINKAGE_P (probe))
15042 cp_parser_simulate_error (parser);
15043 }
15044 else if (is_overloaded_fn (argument))
15045 /* All overloaded functions are allowed; if the external
15046 linkage test does not pass, an error will be issued
15047 later. */
15048 ;
15049 else if (address_p
15050 && (TREE_CODE (argument) == OFFSET_REF
15051 || TREE_CODE (argument) == SCOPE_REF))
15052 /* A pointer-to-member. */
15053 ;
15054 else if (TREE_CODE (argument) == TEMPLATE_PARM_INDEX)
15055 ;
15056 else
15057 cp_parser_simulate_error (parser);
15058
15059 if (cp_parser_parse_definitely (parser))
15060 {
15061 if (address_p)
15062 argument = build_x_unary_op (loc, ADDR_EXPR, argument,
15063 tf_warning_or_error);
15064 else
15065 argument = convert_from_reference (argument);
15066 return argument;
15067 }
15068 }
15069 }
15070 /* If the argument started with "&", there are no other valid
15071 alternatives at this point. */
15072 if (address_p)
15073 {
15074 cp_parser_error (parser, "invalid non-type template argument");
15075 return error_mark_node;
15076 }
15077
15078 general_expr:
15079 /* If the argument wasn't successfully parsed as a type-id followed
15080 by '>>', the argument can only be a constant expression now.
15081 Otherwise, we try parsing the constant-expression tentatively,
15082 because the argument could really be a type-id. */
15083 if (maybe_type_id)
15084 cp_parser_parse_tentatively (parser);
15085 argument = cp_parser_constant_expression (parser);
15086
15087 if (!maybe_type_id)
15088 return argument;
15089 if (!cp_parser_next_token_ends_template_argument_p (parser))
15090 cp_parser_error (parser, "expected template-argument");
15091 if (cp_parser_parse_definitely (parser))
15092 return argument;
15093 /* We did our best to parse the argument as a non type-id, but that
15094 was the only alternative that matched (albeit with a '>' after
15095 it). We can assume it's just a typo from the user, and a
15096 diagnostic will then be issued. */
15097 return cp_parser_template_type_arg (parser);
15098 }
15099
15100 /* Parse an explicit-instantiation.
15101
15102 explicit-instantiation:
15103 template declaration
15104
15105 Although the standard says `declaration', what it really means is:
15106
15107 explicit-instantiation:
15108 template decl-specifier-seq [opt] declarator [opt] ;
15109
15110 Things like `template int S<int>::i = 5, int S<double>::j;' are not
15111 supposed to be allowed. A defect report has been filed about this
15112 issue.
15113
15114 GNU Extension:
15115
15116 explicit-instantiation:
15117 storage-class-specifier template
15118 decl-specifier-seq [opt] declarator [opt] ;
15119 function-specifier template
15120 decl-specifier-seq [opt] declarator [opt] ; */
15121
15122 static void
15123 cp_parser_explicit_instantiation (cp_parser* parser)
15124 {
15125 int declares_class_or_enum;
15126 cp_decl_specifier_seq decl_specifiers;
15127 tree extension_specifier = NULL_TREE;
15128
15129 timevar_push (TV_TEMPLATE_INST);
15130
15131 /* Look for an (optional) storage-class-specifier or
15132 function-specifier. */
15133 if (cp_parser_allow_gnu_extensions_p (parser))
15134 {
15135 extension_specifier
15136 = cp_parser_storage_class_specifier_opt (parser);
15137 if (!extension_specifier)
15138 extension_specifier
15139 = cp_parser_function_specifier_opt (parser,
15140 /*decl_specs=*/NULL);
15141 }
15142
15143 /* Look for the `template' keyword. */
15144 cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
15145 /* Let the front end know that we are processing an explicit
15146 instantiation. */
15147 begin_explicit_instantiation ();
15148 /* [temp.explicit] says that we are supposed to ignore access
15149 control while processing explicit instantiation directives. */
15150 push_deferring_access_checks (dk_no_check);
15151 /* Parse a decl-specifier-seq. */
15152 cp_parser_decl_specifier_seq (parser,
15153 CP_PARSER_FLAGS_OPTIONAL,
15154 &decl_specifiers,
15155 &declares_class_or_enum);
15156 /* If there was exactly one decl-specifier, and it declared a class,
15157 and there's no declarator, then we have an explicit type
15158 instantiation. */
15159 if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
15160 {
15161 tree type;
15162
15163 type = check_tag_decl (&decl_specifiers,
15164 /*explicit_type_instantiation_p=*/true);
15165 /* Turn access control back on for names used during
15166 template instantiation. */
15167 pop_deferring_access_checks ();
15168 if (type)
15169 do_type_instantiation (type, extension_specifier,
15170 /*complain=*/tf_error);
15171 }
15172 else
15173 {
15174 cp_declarator *declarator;
15175 tree decl;
15176
15177 /* Parse the declarator. */
15178 declarator
15179 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
15180 /*ctor_dtor_or_conv_p=*/NULL,
15181 /*parenthesized_p=*/NULL,
15182 /*member_p=*/false,
15183 /*friend_p=*/false);
15184 if (declares_class_or_enum & 2)
15185 cp_parser_check_for_definition_in_return_type (declarator,
15186 decl_specifiers.type,
15187 decl_specifiers.locations[ds_type_spec]);
15188 if (declarator != cp_error_declarator)
15189 {
15190 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_inline))
15191 permerror (decl_specifiers.locations[ds_inline],
15192 "explicit instantiation shall not use"
15193 " %<inline%> specifier");
15194 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_constexpr))
15195 permerror (decl_specifiers.locations[ds_constexpr],
15196 "explicit instantiation shall not use"
15197 " %<constexpr%> specifier");
15198
15199 decl = grokdeclarator (declarator, &decl_specifiers,
15200 NORMAL, 0, &decl_specifiers.attributes);
15201 /* Turn access control back on for names used during
15202 template instantiation. */
15203 pop_deferring_access_checks ();
15204 /* Do the explicit instantiation. */
15205 do_decl_instantiation (decl, extension_specifier);
15206 }
15207 else
15208 {
15209 pop_deferring_access_checks ();
15210 /* Skip the body of the explicit instantiation. */
15211 cp_parser_skip_to_end_of_statement (parser);
15212 }
15213 }
15214 /* We're done with the instantiation. */
15215 end_explicit_instantiation ();
15216
15217 cp_parser_consume_semicolon_at_end_of_statement (parser);
15218
15219 timevar_pop (TV_TEMPLATE_INST);
15220 }
15221
15222 /* Parse an explicit-specialization.
15223
15224 explicit-specialization:
15225 template < > declaration
15226
15227 Although the standard says `declaration', what it really means is:
15228
15229 explicit-specialization:
15230 template <> decl-specifier [opt] init-declarator [opt] ;
15231 template <> function-definition
15232 template <> explicit-specialization
15233 template <> template-declaration */
15234
15235 static void
15236 cp_parser_explicit_specialization (cp_parser* parser)
15237 {
15238 bool need_lang_pop;
15239 cp_token *token = cp_lexer_peek_token (parser->lexer);
15240
15241 /* Look for the `template' keyword. */
15242 cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
15243 /* Look for the `<'. */
15244 cp_parser_require (parser, CPP_LESS, RT_LESS);
15245 /* Look for the `>'. */
15246 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
15247 /* We have processed another parameter list. */
15248 ++parser->num_template_parameter_lists;
15249 /* [temp]
15250
15251 A template ... explicit specialization ... shall not have C
15252 linkage. */
15253 if (current_lang_name == lang_name_c)
15254 {
15255 error_at (token->location, "template specialization with C linkage");
15256 /* Give it C++ linkage to avoid confusing other parts of the
15257 front end. */
15258 push_lang_context (lang_name_cplusplus);
15259 need_lang_pop = true;
15260 }
15261 else
15262 need_lang_pop = false;
15263 /* Let the front end know that we are beginning a specialization. */
15264 if (!begin_specialization ())
15265 {
15266 end_specialization ();
15267 return;
15268 }
15269
15270 /* If the next keyword is `template', we need to figure out whether
15271 or not we're looking a template-declaration. */
15272 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
15273 {
15274 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
15275 && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
15276 cp_parser_template_declaration_after_export (parser,
15277 /*member_p=*/false);
15278 else
15279 cp_parser_explicit_specialization (parser);
15280 }
15281 else
15282 /* Parse the dependent declaration. */
15283 cp_parser_single_declaration (parser,
15284 /*checks=*/NULL,
15285 /*member_p=*/false,
15286 /*explicit_specialization_p=*/true,
15287 /*friend_p=*/NULL);
15288 /* We're done with the specialization. */
15289 end_specialization ();
15290 /* For the erroneous case of a template with C linkage, we pushed an
15291 implicit C++ linkage scope; exit that scope now. */
15292 if (need_lang_pop)
15293 pop_lang_context ();
15294 /* We're done with this parameter list. */
15295 --parser->num_template_parameter_lists;
15296 }
15297
15298 /* Parse a type-specifier.
15299
15300 type-specifier:
15301 simple-type-specifier
15302 class-specifier
15303 enum-specifier
15304 elaborated-type-specifier
15305 cv-qualifier
15306
15307 GNU Extension:
15308
15309 type-specifier:
15310 __complex__
15311
15312 Returns a representation of the type-specifier. For a
15313 class-specifier, enum-specifier, or elaborated-type-specifier, a
15314 TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
15315
15316 The parser flags FLAGS is used to control type-specifier parsing.
15317
15318 If IS_DECLARATION is TRUE, then this type-specifier is appearing
15319 in a decl-specifier-seq.
15320
15321 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
15322 class-specifier, enum-specifier, or elaborated-type-specifier, then
15323 *DECLARES_CLASS_OR_ENUM is set to a nonzero value. The value is 1
15324 if a type is declared; 2 if it is defined. Otherwise, it is set to
15325 zero.
15326
15327 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
15328 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
15329 is set to FALSE. */
15330
15331 static tree
15332 cp_parser_type_specifier (cp_parser* parser,
15333 cp_parser_flags flags,
15334 cp_decl_specifier_seq *decl_specs,
15335 bool is_declaration,
15336 int* declares_class_or_enum,
15337 bool* is_cv_qualifier)
15338 {
15339 tree type_spec = NULL_TREE;
15340 cp_token *token;
15341 enum rid keyword;
15342 cp_decl_spec ds = ds_last;
15343
15344 /* Assume this type-specifier does not declare a new type. */
15345 if (declares_class_or_enum)
15346 *declares_class_or_enum = 0;
15347 /* And that it does not specify a cv-qualifier. */
15348 if (is_cv_qualifier)
15349 *is_cv_qualifier = false;
15350 /* Peek at the next token. */
15351 token = cp_lexer_peek_token (parser->lexer);
15352
15353 /* If we're looking at a keyword, we can use that to guide the
15354 production we choose. */
15355 keyword = token->keyword;
15356 switch (keyword)
15357 {
15358 case RID_ENUM:
15359 if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
15360 goto elaborated_type_specifier;
15361
15362 /* Look for the enum-specifier. */
15363 type_spec = cp_parser_enum_specifier (parser);
15364 /* If that worked, we're done. */
15365 if (type_spec)
15366 {
15367 if (declares_class_or_enum)
15368 *declares_class_or_enum = 2;
15369 if (decl_specs)
15370 cp_parser_set_decl_spec_type (decl_specs,
15371 type_spec,
15372 token,
15373 /*type_definition_p=*/true);
15374 return type_spec;
15375 }
15376 else
15377 goto elaborated_type_specifier;
15378
15379 /* Any of these indicate either a class-specifier, or an
15380 elaborated-type-specifier. */
15381 case RID_CLASS:
15382 case RID_STRUCT:
15383 case RID_UNION:
15384 if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
15385 goto elaborated_type_specifier;
15386
15387 /* Parse tentatively so that we can back up if we don't find a
15388 class-specifier. */
15389 cp_parser_parse_tentatively (parser);
15390 /* Look for the class-specifier. */
15391 type_spec = cp_parser_class_specifier (parser);
15392 invoke_plugin_callbacks (PLUGIN_FINISH_TYPE, type_spec);
15393 /* If that worked, we're done. */
15394 if (cp_parser_parse_definitely (parser))
15395 {
15396 if (declares_class_or_enum)
15397 *declares_class_or_enum = 2;
15398 if (decl_specs)
15399 cp_parser_set_decl_spec_type (decl_specs,
15400 type_spec,
15401 token,
15402 /*type_definition_p=*/true);
15403 return type_spec;
15404 }
15405
15406 /* Fall through. */
15407 elaborated_type_specifier:
15408 /* We're declaring (not defining) a class or enum. */
15409 if (declares_class_or_enum)
15410 *declares_class_or_enum = 1;
15411
15412 /* Fall through. */
15413 case RID_TYPENAME:
15414 /* Look for an elaborated-type-specifier. */
15415 type_spec
15416 = (cp_parser_elaborated_type_specifier
15417 (parser,
15418 decl_spec_seq_has_spec_p (decl_specs, ds_friend),
15419 is_declaration));
15420 if (decl_specs)
15421 cp_parser_set_decl_spec_type (decl_specs,
15422 type_spec,
15423 token,
15424 /*type_definition_p=*/false);
15425 return type_spec;
15426
15427 case RID_CONST:
15428 ds = ds_const;
15429 if (is_cv_qualifier)
15430 *is_cv_qualifier = true;
15431 break;
15432
15433 case RID_VOLATILE:
15434 ds = ds_volatile;
15435 if (is_cv_qualifier)
15436 *is_cv_qualifier = true;
15437 break;
15438
15439 case RID_RESTRICT:
15440 ds = ds_restrict;
15441 if (is_cv_qualifier)
15442 *is_cv_qualifier = true;
15443 break;
15444
15445 case RID_COMPLEX:
15446 /* The `__complex__' keyword is a GNU extension. */
15447 ds = ds_complex;
15448 break;
15449
15450 default:
15451 break;
15452 }
15453
15454 /* Handle simple keywords. */
15455 if (ds != ds_last)
15456 {
15457 if (decl_specs)
15458 {
15459 set_and_check_decl_spec_loc (decl_specs, ds, token);
15460 decl_specs->any_specifiers_p = true;
15461 }
15462 return cp_lexer_consume_token (parser->lexer)->u.value;
15463 }
15464
15465 /* If we do not already have a type-specifier, assume we are looking
15466 at a simple-type-specifier. */
15467 type_spec = cp_parser_simple_type_specifier (parser,
15468 decl_specs,
15469 flags);
15470
15471 /* If we didn't find a type-specifier, and a type-specifier was not
15472 optional in this context, issue an error message. */
15473 if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
15474 {
15475 cp_parser_error (parser, "expected type specifier");
15476 return error_mark_node;
15477 }
15478
15479 return type_spec;
15480 }
15481
15482 /* Parse a simple-type-specifier.
15483
15484 simple-type-specifier:
15485 :: [opt] nested-name-specifier [opt] type-name
15486 :: [opt] nested-name-specifier template template-id
15487 char
15488 wchar_t
15489 bool
15490 short
15491 int
15492 long
15493 signed
15494 unsigned
15495 float
15496 double
15497 void
15498
15499 C++0x Extension:
15500
15501 simple-type-specifier:
15502 auto
15503 decltype ( expression )
15504 char16_t
15505 char32_t
15506 __underlying_type ( type-id )
15507
15508 GNU Extension:
15509
15510 simple-type-specifier:
15511 __int128
15512 __typeof__ unary-expression
15513 __typeof__ ( type-id )
15514 __typeof__ ( type-id ) { initializer-list , [opt] }
15515
15516 Concepts Extension:
15517
15518 simple-type-specifier:
15519 constrained-type-specifier
15520
15521 Returns the indicated TYPE_DECL. If DECL_SPECS is not NULL, it is
15522 appropriately updated. */
15523
15524 static tree
15525 cp_parser_simple_type_specifier (cp_parser* parser,
15526 cp_decl_specifier_seq *decl_specs,
15527 cp_parser_flags flags)
15528 {
15529 tree type = NULL_TREE;
15530 cp_token *token;
15531 int idx;
15532
15533 /* Peek at the next token. */
15534 token = cp_lexer_peek_token (parser->lexer);
15535
15536 /* If we're looking at a keyword, things are easy. */
15537 switch (token->keyword)
15538 {
15539 case RID_CHAR:
15540 if (decl_specs)
15541 decl_specs->explicit_char_p = true;
15542 type = char_type_node;
15543 break;
15544 case RID_CHAR16:
15545 type = char16_type_node;
15546 break;
15547 case RID_CHAR32:
15548 type = char32_type_node;
15549 break;
15550 case RID_WCHAR:
15551 type = wchar_type_node;
15552 break;
15553 case RID_BOOL:
15554 type = boolean_type_node;
15555 break;
15556 case RID_SHORT:
15557 set_and_check_decl_spec_loc (decl_specs, ds_short, token);
15558 type = short_integer_type_node;
15559 break;
15560 case RID_INT:
15561 if (decl_specs)
15562 decl_specs->explicit_int_p = true;
15563 type = integer_type_node;
15564 break;
15565 case RID_INT_N_0:
15566 case RID_INT_N_1:
15567 case RID_INT_N_2:
15568 case RID_INT_N_3:
15569 idx = token->keyword - RID_INT_N_0;
15570 if (! int_n_enabled_p [idx])
15571 break;
15572 if (decl_specs)
15573 {
15574 decl_specs->explicit_intN_p = true;
15575 decl_specs->int_n_idx = idx;
15576 }
15577 type = int_n_trees [idx].signed_type;
15578 break;
15579 case RID_LONG:
15580 if (decl_specs)
15581 set_and_check_decl_spec_loc (decl_specs, ds_long, token);
15582 type = long_integer_type_node;
15583 break;
15584 case RID_SIGNED:
15585 set_and_check_decl_spec_loc (decl_specs, ds_signed, token);
15586 type = integer_type_node;
15587 break;
15588 case RID_UNSIGNED:
15589 set_and_check_decl_spec_loc (decl_specs, ds_unsigned, token);
15590 type = unsigned_type_node;
15591 break;
15592 case RID_FLOAT:
15593 type = float_type_node;
15594 break;
15595 case RID_DOUBLE:
15596 type = double_type_node;
15597 break;
15598 case RID_VOID:
15599 type = void_type_node;
15600 break;
15601
15602 case RID_AUTO:
15603 maybe_warn_cpp0x (CPP0X_AUTO);
15604 if (parser->auto_is_implicit_function_template_parm_p)
15605 {
15606 /* The 'auto' might be the placeholder return type for a function decl
15607 with trailing return type. */
15608 bool have_trailing_return_fn_decl = false;
15609 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type
15610 == CPP_OPEN_PAREN)
15611 {
15612 cp_parser_parse_tentatively (parser);
15613 cp_lexer_consume_token (parser->lexer);
15614 cp_lexer_consume_token (parser->lexer);
15615 if (cp_parser_skip_to_closing_parenthesis (parser,
15616 /*recovering*/false,
15617 /*or_comma*/false,
15618 /*consume_paren*/true))
15619 have_trailing_return_fn_decl
15620 = cp_lexer_next_token_is (parser->lexer, CPP_DEREF);
15621 cp_parser_abort_tentative_parse (parser);
15622 }
15623
15624 if (have_trailing_return_fn_decl)
15625 {
15626 type = make_auto ();
15627 break;
15628 }
15629
15630 if (cxx_dialect >= cxx14)
15631 {
15632 type = synthesize_implicit_template_parm (parser, NULL_TREE);
15633 type = TREE_TYPE (type);
15634 }
15635 else
15636 type = error_mark_node;
15637
15638 if (current_class_type && LAMBDA_TYPE_P (current_class_type))
15639 {
15640 if (cxx_dialect < cxx14)
15641 error_at (token->location,
15642 "use of %<auto%> in lambda parameter declaration "
15643 "only available with "
15644 "-std=c++14 or -std=gnu++14");
15645 }
15646 else if (cxx_dialect < cxx14)
15647 error_at (token->location,
15648 "use of %<auto%> in parameter declaration "
15649 "only available with "
15650 "-std=c++14 or -std=gnu++14");
15651 else
15652 pedwarn (token->location, OPT_Wpedantic,
15653 "ISO C++ forbids use of %<auto%> in parameter "
15654 "declaration");
15655 }
15656 else
15657 type = make_auto ();
15658 break;
15659
15660 case RID_DECLTYPE:
15661 /* Since DR 743, decltype can either be a simple-type-specifier by
15662 itself or begin a nested-name-specifier. Parsing it will replace
15663 it with a CPP_DECLTYPE, so just rewind and let the CPP_DECLTYPE
15664 handling below decide what to do. */
15665 cp_parser_decltype (parser);
15666 cp_lexer_set_token_position (parser->lexer, token);
15667 break;
15668
15669 case RID_TYPEOF:
15670 /* Consume the `typeof' token. */
15671 cp_lexer_consume_token (parser->lexer);
15672 /* Parse the operand to `typeof'. */
15673 type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
15674 /* If it is not already a TYPE, take its type. */
15675 if (!TYPE_P (type))
15676 type = finish_typeof (type);
15677
15678 if (decl_specs)
15679 cp_parser_set_decl_spec_type (decl_specs, type,
15680 token,
15681 /*type_definition_p=*/false);
15682
15683 return type;
15684
15685 case RID_UNDERLYING_TYPE:
15686 type = cp_parser_trait_expr (parser, RID_UNDERLYING_TYPE);
15687 if (decl_specs)
15688 cp_parser_set_decl_spec_type (decl_specs, type,
15689 token,
15690 /*type_definition_p=*/false);
15691
15692 return type;
15693
15694 case RID_BASES:
15695 case RID_DIRECT_BASES:
15696 type = cp_parser_trait_expr (parser, token->keyword);
15697 if (decl_specs)
15698 cp_parser_set_decl_spec_type (decl_specs, type,
15699 token,
15700 /*type_definition_p=*/false);
15701 return type;
15702 default:
15703 break;
15704 }
15705
15706 /* If token is an already-parsed decltype not followed by ::,
15707 it's a simple-type-specifier. */
15708 if (token->type == CPP_DECLTYPE
15709 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
15710 {
15711 type = token->u.value;
15712 if (decl_specs)
15713 {
15714 cp_parser_set_decl_spec_type (decl_specs, type,
15715 token,
15716 /*type_definition_p=*/false);
15717 /* Remember that we are handling a decltype in order to
15718 implement the resolution of DR 1510 when the argument
15719 isn't instantiation dependent. */
15720 decl_specs->decltype_p = true;
15721 }
15722 cp_lexer_consume_token (parser->lexer);
15723 return type;
15724 }
15725
15726 /* If the type-specifier was for a built-in type, we're done. */
15727 if (type)
15728 {
15729 /* Record the type. */
15730 if (decl_specs
15731 && (token->keyword != RID_SIGNED
15732 && token->keyword != RID_UNSIGNED
15733 && token->keyword != RID_SHORT
15734 && token->keyword != RID_LONG))
15735 cp_parser_set_decl_spec_type (decl_specs,
15736 type,
15737 token,
15738 /*type_definition_p=*/false);
15739 if (decl_specs)
15740 decl_specs->any_specifiers_p = true;
15741
15742 /* Consume the token. */
15743 cp_lexer_consume_token (parser->lexer);
15744
15745 if (type == error_mark_node)
15746 return error_mark_node;
15747
15748 /* There is no valid C++ program where a non-template type is
15749 followed by a "<". That usually indicates that the user thought
15750 that the type was a template. */
15751 cp_parser_check_for_invalid_template_id (parser, type, none_type,
15752 token->location);
15753
15754 return TYPE_NAME (type);
15755 }
15756
15757 /* The type-specifier must be a user-defined type. */
15758 if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
15759 {
15760 bool qualified_p;
15761 bool global_p;
15762
15763 /* Don't gobble tokens or issue error messages if this is an
15764 optional type-specifier. */
15765 if (flags & CP_PARSER_FLAGS_OPTIONAL)
15766 cp_parser_parse_tentatively (parser);
15767
15768 /* Look for the optional `::' operator. */
15769 global_p
15770 = (cp_parser_global_scope_opt (parser,
15771 /*current_scope_valid_p=*/false)
15772 != NULL_TREE);
15773 /* Look for the nested-name specifier. */
15774 qualified_p
15775 = (cp_parser_nested_name_specifier_opt (parser,
15776 /*typename_keyword_p=*/false,
15777 /*check_dependency_p=*/true,
15778 /*type_p=*/false,
15779 /*is_declaration=*/false)
15780 != NULL_TREE);
15781 token = cp_lexer_peek_token (parser->lexer);
15782 /* If we have seen a nested-name-specifier, and the next token
15783 is `template', then we are using the template-id production. */
15784 if (parser->scope
15785 && cp_parser_optional_template_keyword (parser))
15786 {
15787 /* Look for the template-id. */
15788 type = cp_parser_template_id (parser,
15789 /*template_keyword_p=*/true,
15790 /*check_dependency_p=*/true,
15791 none_type,
15792 /*is_declaration=*/false);
15793 /* If the template-id did not name a type, we are out of
15794 luck. */
15795 if (TREE_CODE (type) != TYPE_DECL)
15796 {
15797 cp_parser_error (parser, "expected template-id for type");
15798 type = NULL_TREE;
15799 }
15800 }
15801 /* Otherwise, look for a type-name. */
15802 else
15803 type = cp_parser_type_name (parser);
15804 /* Keep track of all name-lookups performed in class scopes. */
15805 if (type
15806 && !global_p
15807 && !qualified_p
15808 && TREE_CODE (type) == TYPE_DECL
15809 && identifier_p (DECL_NAME (type)))
15810 maybe_note_name_used_in_class (DECL_NAME (type), type);
15811 /* If it didn't work out, we don't have a TYPE. */
15812 if ((flags & CP_PARSER_FLAGS_OPTIONAL)
15813 && !cp_parser_parse_definitely (parser))
15814 type = NULL_TREE;
15815 if (type && decl_specs)
15816 cp_parser_set_decl_spec_type (decl_specs, type,
15817 token,
15818 /*type_definition_p=*/false);
15819 }
15820
15821 /* If we didn't get a type-name, issue an error message. */
15822 if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
15823 {
15824 cp_parser_error (parser, "expected type-name");
15825 return error_mark_node;
15826 }
15827
15828 if (type && type != error_mark_node)
15829 {
15830 /* See if TYPE is an Objective-C type, and if so, parse and
15831 accept any protocol references following it. Do this before
15832 the cp_parser_check_for_invalid_template_id() call, because
15833 Objective-C types can be followed by '<...>' which would
15834 enclose protocol names rather than template arguments, and so
15835 everything is fine. */
15836 if (c_dialect_objc () && !parser->scope
15837 && (objc_is_id (type) || objc_is_class_name (type)))
15838 {
15839 tree protos = cp_parser_objc_protocol_refs_opt (parser);
15840 tree qual_type = objc_get_protocol_qualified_type (type, protos);
15841
15842 /* Clobber the "unqualified" type previously entered into
15843 DECL_SPECS with the new, improved protocol-qualified version. */
15844 if (decl_specs)
15845 decl_specs->type = qual_type;
15846
15847 return qual_type;
15848 }
15849
15850 /* There is no valid C++ program where a non-template type is
15851 followed by a "<". That usually indicates that the user
15852 thought that the type was a template. */
15853 cp_parser_check_for_invalid_template_id (parser, TREE_TYPE (type),
15854 none_type,
15855 token->location);
15856 }
15857
15858 return type;
15859 }
15860
15861 /* Parse a type-name.
15862
15863 type-name:
15864 class-name
15865 enum-name
15866 typedef-name
15867 simple-template-id [in c++0x]
15868
15869 enum-name:
15870 identifier
15871
15872 typedef-name:
15873 identifier
15874
15875 Concepts:
15876
15877 type-name:
15878 concept-name
15879 partial-concept-id
15880
15881 concept-name:
15882 identifier
15883
15884 Returns a TYPE_DECL for the type. */
15885
15886 static tree
15887 cp_parser_type_name (cp_parser* parser)
15888 {
15889 return cp_parser_type_name (parser, /*typename_keyword_p=*/false);
15890 }
15891
15892 /* See above. */
15893 static tree
15894 cp_parser_type_name (cp_parser* parser, bool typename_keyword_p)
15895 {
15896 tree type_decl;
15897
15898 /* We can't know yet whether it is a class-name or not. */
15899 cp_parser_parse_tentatively (parser);
15900 /* Try a class-name. */
15901 type_decl = cp_parser_class_name (parser,
15902 typename_keyword_p,
15903 /*template_keyword_p=*/false,
15904 none_type,
15905 /*check_dependency_p=*/true,
15906 /*class_head_p=*/false,
15907 /*is_declaration=*/false);
15908 /* If it's not a class-name, keep looking. */
15909 if (!cp_parser_parse_definitely (parser))
15910 {
15911 if (cxx_dialect < cxx11)
15912 /* It must be a typedef-name or an enum-name. */
15913 return cp_parser_nonclass_name (parser);
15914
15915 cp_parser_parse_tentatively (parser);
15916 /* It is either a simple-template-id representing an
15917 instantiation of an alias template... */
15918 type_decl = cp_parser_template_id (parser,
15919 /*template_keyword_p=*/false,
15920 /*check_dependency_p=*/true,
15921 none_type,
15922 /*is_declaration=*/false);
15923 /* Note that this must be an instantiation of an alias template
15924 because [temp.names]/6 says:
15925
15926 A template-id that names an alias template specialization
15927 is a type-name.
15928
15929 Whereas [temp.names]/7 says:
15930
15931 A simple-template-id that names a class template
15932 specialization is a class-name.
15933
15934 With concepts, this could also be a partial-concept-id that
15935 declares a non-type template parameter. */
15936 if (type_decl != NULL_TREE
15937 && TREE_CODE (type_decl) == TYPE_DECL
15938 && TYPE_DECL_ALIAS_P (type_decl))
15939 gcc_assert (DECL_TEMPLATE_INSTANTIATION (type_decl));
15940 else if (is_constrained_parameter (type_decl))
15941 /* Don't do anything. */ ;
15942 else
15943 cp_parser_simulate_error (parser);
15944
15945 if (!cp_parser_parse_definitely (parser))
15946 /* ... Or a typedef-name or an enum-name. */
15947 return cp_parser_nonclass_name (parser);
15948 }
15949
15950 return type_decl;
15951 }
15952
15953 /* Check if DECL and ARGS can form a constrained-type-specifier.
15954 If ARGS is non-null, we try to form a concept check of the
15955 form DECL<?, ARGS> where ? is a wildcard that matches any
15956 kind of template argument. If ARGS is NULL, then we try to
15957 form a concept check of the form DECL<?>. */
15958
15959 static tree
15960 cp_parser_maybe_constrained_type_specifier (cp_parser *parser,
15961 tree decl, tree args)
15962 {
15963 gcc_assert (args ? TREE_CODE (args) == TREE_VEC : true);
15964
15965 /* If we a constrained-type-specifier cannot be deduced. */
15966 if (parser->prevent_constrained_type_specifiers)
15967 return NULL_TREE;
15968
15969 /* A constrained type specifier can only be found in an
15970 overload set or as a reference to a template declaration.
15971
15972 FIXME: This might be masking a bug. It's possible that
15973 that the deduction below is causing template specializations
15974 to be formed with the wildcard as an argument. */
15975 if (TREE_CODE (decl) != OVERLOAD && TREE_CODE (decl) != TEMPLATE_DECL)
15976 return NULL_TREE;
15977
15978 /* Try to build a call expression that evaluates the
15979 concept. This can fail if the overload set refers
15980 only to non-templates. */
15981 tree placeholder = build_nt (WILDCARD_DECL);
15982 tree check = build_concept_check (decl, placeholder, args);
15983 if (check == error_mark_node)
15984 return NULL_TREE;
15985
15986 /* Deduce the checked constraint and the prototype parameter.
15987
15988 FIXME: In certain cases, failure to deduce should be a
15989 diagnosable error. */
15990 tree conc;
15991 tree proto;
15992 if (!deduce_constrained_parameter (check, conc, proto))
15993 return NULL_TREE;
15994
15995 /* In template parameter scope, this results in a constrained
15996 parameter. Return a descriptor of that parm. */
15997 if (processing_template_parmlist)
15998 return build_constrained_parameter (conc, proto, args);
15999
16000 /* In a parameter-declaration-clause, constrained-type
16001 specifiers result in invented template parameters. */
16002 if (parser->auto_is_implicit_function_template_parm_p)
16003 {
16004 tree x = build_constrained_parameter (conc, proto, args);
16005 return synthesize_implicit_template_parm (parser, x);
16006 }
16007 else
16008 {
16009 /* Otherwise, we're in a context where the constrained
16010 type name is deduced and the constraint applies
16011 after deduction. */
16012 return make_constrained_auto (conc, args);
16013 }
16014
16015 return NULL_TREE;
16016 }
16017
16018 /* If DECL refers to a concept, return a TYPE_DECL representing
16019 the result of using the constrained type specifier in the
16020 current context. DECL refers to a concept if
16021
16022 - it is an overload set containing a function concept taking a single
16023 type argument, or
16024
16025 - it is a variable concept taking a single type argument. */
16026
16027 static tree
16028 cp_parser_maybe_concept_name (cp_parser* parser, tree decl)
16029 {
16030 if (flag_concepts
16031 && (TREE_CODE (decl) == OVERLOAD
16032 || BASELINK_P (decl)
16033 || variable_concept_p (decl)))
16034 return cp_parser_maybe_constrained_type_specifier (parser, decl, NULL_TREE);
16035 else
16036 return NULL_TREE;
16037 }
16038
16039 /* Check if DECL and ARGS form a partial-concept-id. If so,
16040 assign ID to the resulting constrained placeholder.
16041
16042 Returns true if the partial-concept-id designates a placeholder
16043 and false otherwise. Note that *id is set to NULL_TREE in
16044 this case. */
16045
16046 static tree
16047 cp_parser_maybe_partial_concept_id (cp_parser *parser, tree decl, tree args)
16048 {
16049 return cp_parser_maybe_constrained_type_specifier (parser, decl, args);
16050 }
16051
16052 /* Parse a non-class type-name, that is, either an enum-name, a typedef-name,
16053 or a concept-name.
16054
16055 enum-name:
16056 identifier
16057
16058 typedef-name:
16059 identifier
16060
16061 concept-name:
16062 identifier
16063
16064 Returns a TYPE_DECL for the type. */
16065
16066 static tree
16067 cp_parser_nonclass_name (cp_parser* parser)
16068 {
16069 tree type_decl;
16070 tree identifier;
16071
16072 cp_token *token = cp_lexer_peek_token (parser->lexer);
16073 identifier = cp_parser_identifier (parser);
16074 if (identifier == error_mark_node)
16075 return error_mark_node;
16076
16077 /* Look up the type-name. */
16078 type_decl = cp_parser_lookup_name_simple (parser, identifier, token->location);
16079
16080 type_decl = strip_using_decl (type_decl);
16081
16082 /* If we found an overload set, then it may refer to a concept-name. */
16083 if (tree decl = cp_parser_maybe_concept_name (parser, type_decl))
16084 type_decl = decl;
16085
16086 if (TREE_CODE (type_decl) != TYPE_DECL
16087 && (objc_is_id (identifier) || objc_is_class_name (identifier)))
16088 {
16089 /* See if this is an Objective-C type. */
16090 tree protos = cp_parser_objc_protocol_refs_opt (parser);
16091 tree type = objc_get_protocol_qualified_type (identifier, protos);
16092 if (type)
16093 type_decl = TYPE_NAME (type);
16094 }
16095
16096 /* Issue an error if we did not find a type-name. */
16097 if (TREE_CODE (type_decl) != TYPE_DECL
16098 /* In Objective-C, we have the complication that class names are
16099 normally type names and start declarations (eg, the
16100 "NSObject" in "NSObject *object;"), but can be used in an
16101 Objective-C 2.0 dot-syntax (as in "NSObject.version") which
16102 is an expression. So, a classname followed by a dot is not a
16103 valid type-name. */
16104 || (objc_is_class_name (TREE_TYPE (type_decl))
16105 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT))
16106 {
16107 if (!cp_parser_simulate_error (parser))
16108 cp_parser_name_lookup_error (parser, identifier, type_decl,
16109 NLE_TYPE, token->location);
16110 return error_mark_node;
16111 }
16112 /* Remember that the name was used in the definition of the
16113 current class so that we can check later to see if the
16114 meaning would have been different after the class was
16115 entirely defined. */
16116 else if (type_decl != error_mark_node
16117 && !parser->scope)
16118 maybe_note_name_used_in_class (identifier, type_decl);
16119
16120 return type_decl;
16121 }
16122
16123 /* Parse an elaborated-type-specifier. Note that the grammar given
16124 here incorporates the resolution to DR68.
16125
16126 elaborated-type-specifier:
16127 class-key :: [opt] nested-name-specifier [opt] identifier
16128 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
16129 enum-key :: [opt] nested-name-specifier [opt] identifier
16130 typename :: [opt] nested-name-specifier identifier
16131 typename :: [opt] nested-name-specifier template [opt]
16132 template-id
16133
16134 GNU extension:
16135
16136 elaborated-type-specifier:
16137 class-key attributes :: [opt] nested-name-specifier [opt] identifier
16138 class-key attributes :: [opt] nested-name-specifier [opt]
16139 template [opt] template-id
16140 enum attributes :: [opt] nested-name-specifier [opt] identifier
16141
16142 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
16143 declared `friend'. If IS_DECLARATION is TRUE, then this
16144 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
16145 something is being declared.
16146
16147 Returns the TYPE specified. */
16148
16149 static tree
16150 cp_parser_elaborated_type_specifier (cp_parser* parser,
16151 bool is_friend,
16152 bool is_declaration)
16153 {
16154 enum tag_types tag_type;
16155 tree identifier;
16156 tree type = NULL_TREE;
16157 tree attributes = NULL_TREE;
16158 tree globalscope;
16159 cp_token *token = NULL;
16160
16161 /* See if we're looking at the `enum' keyword. */
16162 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
16163 {
16164 /* Consume the `enum' token. */
16165 cp_lexer_consume_token (parser->lexer);
16166 /* Remember that it's an enumeration type. */
16167 tag_type = enum_type;
16168 /* Issue a warning if the `struct' or `class' key (for C++0x scoped
16169 enums) is used here. */
16170 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
16171 || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
16172 {
16173 pedwarn (input_location, 0, "elaborated-type-specifier "
16174 "for a scoped enum must not use the %<%D%> keyword",
16175 cp_lexer_peek_token (parser->lexer)->u.value);
16176 /* Consume the `struct' or `class' and parse it anyway. */
16177 cp_lexer_consume_token (parser->lexer);
16178 }
16179 /* Parse the attributes. */
16180 attributes = cp_parser_attributes_opt (parser);
16181 }
16182 /* Or, it might be `typename'. */
16183 else if (cp_lexer_next_token_is_keyword (parser->lexer,
16184 RID_TYPENAME))
16185 {
16186 /* Consume the `typename' token. */
16187 cp_lexer_consume_token (parser->lexer);
16188 /* Remember that it's a `typename' type. */
16189 tag_type = typename_type;
16190 }
16191 /* Otherwise it must be a class-key. */
16192 else
16193 {
16194 tag_type = cp_parser_class_key (parser);
16195 if (tag_type == none_type)
16196 return error_mark_node;
16197 /* Parse the attributes. */
16198 attributes = cp_parser_attributes_opt (parser);
16199 }
16200
16201 /* Look for the `::' operator. */
16202 globalscope = cp_parser_global_scope_opt (parser,
16203 /*current_scope_valid_p=*/false);
16204 /* Look for the nested-name-specifier. */
16205 if (tag_type == typename_type && !globalscope)
16206 {
16207 if (!cp_parser_nested_name_specifier (parser,
16208 /*typename_keyword_p=*/true,
16209 /*check_dependency_p=*/true,
16210 /*type_p=*/true,
16211 is_declaration))
16212 return error_mark_node;
16213 }
16214 else
16215 /* Even though `typename' is not present, the proposed resolution
16216 to Core Issue 180 says that in `class A<T>::B', `B' should be
16217 considered a type-name, even if `A<T>' is dependent. */
16218 cp_parser_nested_name_specifier_opt (parser,
16219 /*typename_keyword_p=*/true,
16220 /*check_dependency_p=*/true,
16221 /*type_p=*/true,
16222 is_declaration);
16223 /* For everything but enumeration types, consider a template-id.
16224 For an enumeration type, consider only a plain identifier. */
16225 if (tag_type != enum_type)
16226 {
16227 bool template_p = false;
16228 tree decl;
16229
16230 /* Allow the `template' keyword. */
16231 template_p = cp_parser_optional_template_keyword (parser);
16232 /* If we didn't see `template', we don't know if there's a
16233 template-id or not. */
16234 if (!template_p)
16235 cp_parser_parse_tentatively (parser);
16236 /* Parse the template-id. */
16237 token = cp_lexer_peek_token (parser->lexer);
16238 decl = cp_parser_template_id (parser, template_p,
16239 /*check_dependency_p=*/true,
16240 tag_type,
16241 is_declaration);
16242 /* If we didn't find a template-id, look for an ordinary
16243 identifier. */
16244 if (!template_p && !cp_parser_parse_definitely (parser))
16245 ;
16246 /* We can get here when cp_parser_template_id, called by
16247 cp_parser_class_name with tag_type == none_type, succeeds
16248 and caches a BASELINK. Then, when called again here,
16249 instead of failing and returning an error_mark_node
16250 returns it (see template/typename17.C in C++11).
16251 ??? Could we diagnose this earlier? */
16252 else if (tag_type == typename_type && BASELINK_P (decl))
16253 {
16254 cp_parser_diagnose_invalid_type_name (parser, decl, token->location);
16255 type = error_mark_node;
16256 }
16257 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
16258 in effect, then we must assume that, upon instantiation, the
16259 template will correspond to a class. */
16260 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
16261 && tag_type == typename_type)
16262 type = make_typename_type (parser->scope, decl,
16263 typename_type,
16264 /*complain=*/tf_error);
16265 /* If the `typename' keyword is in effect and DECL is not a type
16266 decl, then type is non existent. */
16267 else if (tag_type == typename_type && TREE_CODE (decl) != TYPE_DECL)
16268 ;
16269 else if (TREE_CODE (decl) == TYPE_DECL)
16270 type = check_elaborated_type_specifier (tag_type, decl,
16271 /*allow_template_p=*/true);
16272 else if (decl == error_mark_node)
16273 type = error_mark_node;
16274 }
16275
16276 if (!type)
16277 {
16278 token = cp_lexer_peek_token (parser->lexer);
16279 identifier = cp_parser_identifier (parser);
16280
16281 if (identifier == error_mark_node)
16282 {
16283 parser->scope = NULL_TREE;
16284 return error_mark_node;
16285 }
16286
16287 /* For a `typename', we needn't call xref_tag. */
16288 if (tag_type == typename_type
16289 && TREE_CODE (parser->scope) != NAMESPACE_DECL)
16290 return cp_parser_make_typename_type (parser, identifier,
16291 token->location);
16292
16293 /* Template parameter lists apply only if we are not within a
16294 function parameter list. */
16295 bool template_parm_lists_apply
16296 = parser->num_template_parameter_lists;
16297 if (template_parm_lists_apply)
16298 for (cp_binding_level *s = current_binding_level;
16299 s && s->kind != sk_template_parms;
16300 s = s->level_chain)
16301 if (s->kind == sk_function_parms)
16302 template_parm_lists_apply = false;
16303
16304 /* Look up a qualified name in the usual way. */
16305 if (parser->scope)
16306 {
16307 tree decl;
16308 tree ambiguous_decls;
16309
16310 decl = cp_parser_lookup_name (parser, identifier,
16311 tag_type,
16312 /*is_template=*/false,
16313 /*is_namespace=*/false,
16314 /*check_dependency=*/true,
16315 &ambiguous_decls,
16316 token->location);
16317
16318 /* If the lookup was ambiguous, an error will already have been
16319 issued. */
16320 if (ambiguous_decls)
16321 return error_mark_node;
16322
16323 /* If we are parsing friend declaration, DECL may be a
16324 TEMPLATE_DECL tree node here. However, we need to check
16325 whether this TEMPLATE_DECL results in valid code. Consider
16326 the following example:
16327
16328 namespace N {
16329 template <class T> class C {};
16330 }
16331 class X {
16332 template <class T> friend class N::C; // #1, valid code
16333 };
16334 template <class T> class Y {
16335 friend class N::C; // #2, invalid code
16336 };
16337
16338 For both case #1 and #2, we arrive at a TEMPLATE_DECL after
16339 name lookup of `N::C'. We see that friend declaration must
16340 be template for the code to be valid. Note that
16341 processing_template_decl does not work here since it is
16342 always 1 for the above two cases. */
16343
16344 decl = (cp_parser_maybe_treat_template_as_class
16345 (decl, /*tag_name_p=*/is_friend
16346 && template_parm_lists_apply));
16347
16348 if (TREE_CODE (decl) != TYPE_DECL)
16349 {
16350 cp_parser_diagnose_invalid_type_name (parser,
16351 identifier,
16352 token->location);
16353 return error_mark_node;
16354 }
16355
16356 if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
16357 {
16358 bool allow_template = (template_parm_lists_apply
16359 || DECL_SELF_REFERENCE_P (decl));
16360 type = check_elaborated_type_specifier (tag_type, decl,
16361 allow_template);
16362
16363 if (type == error_mark_node)
16364 return error_mark_node;
16365 }
16366
16367 /* Forward declarations of nested types, such as
16368
16369 class C1::C2;
16370 class C1::C2::C3;
16371
16372 are invalid unless all components preceding the final '::'
16373 are complete. If all enclosing types are complete, these
16374 declarations become merely pointless.
16375
16376 Invalid forward declarations of nested types are errors
16377 caught elsewhere in parsing. Those that are pointless arrive
16378 here. */
16379
16380 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
16381 && !is_friend && !processing_explicit_instantiation)
16382 warning (0, "declaration %qD does not declare anything", decl);
16383
16384 type = TREE_TYPE (decl);
16385 }
16386 else
16387 {
16388 /* An elaborated-type-specifier sometimes introduces a new type and
16389 sometimes names an existing type. Normally, the rule is that it
16390 introduces a new type only if there is not an existing type of
16391 the same name already in scope. For example, given:
16392
16393 struct S {};
16394 void f() { struct S s; }
16395
16396 the `struct S' in the body of `f' is the same `struct S' as in
16397 the global scope; the existing definition is used. However, if
16398 there were no global declaration, this would introduce a new
16399 local class named `S'.
16400
16401 An exception to this rule applies to the following code:
16402
16403 namespace N { struct S; }
16404
16405 Here, the elaborated-type-specifier names a new type
16406 unconditionally; even if there is already an `S' in the
16407 containing scope this declaration names a new type.
16408 This exception only applies if the elaborated-type-specifier
16409 forms the complete declaration:
16410
16411 [class.name]
16412
16413 A declaration consisting solely of `class-key identifier ;' is
16414 either a redeclaration of the name in the current scope or a
16415 forward declaration of the identifier as a class name. It
16416 introduces the name into the current scope.
16417
16418 We are in this situation precisely when the next token is a `;'.
16419
16420 An exception to the exception is that a `friend' declaration does
16421 *not* name a new type; i.e., given:
16422
16423 struct S { friend struct T; };
16424
16425 `T' is not a new type in the scope of `S'.
16426
16427 Also, `new struct S' or `sizeof (struct S)' never results in the
16428 definition of a new type; a new type can only be declared in a
16429 declaration context. */
16430
16431 tag_scope ts;
16432 bool template_p;
16433
16434 if (is_friend)
16435 /* Friends have special name lookup rules. */
16436 ts = ts_within_enclosing_non_class;
16437 else if (is_declaration
16438 && cp_lexer_next_token_is (parser->lexer,
16439 CPP_SEMICOLON))
16440 /* This is a `class-key identifier ;' */
16441 ts = ts_current;
16442 else
16443 ts = ts_global;
16444
16445 template_p =
16446 (template_parm_lists_apply
16447 && (cp_parser_next_token_starts_class_definition_p (parser)
16448 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)));
16449 /* An unqualified name was used to reference this type, so
16450 there were no qualifying templates. */
16451 if (template_parm_lists_apply
16452 && !cp_parser_check_template_parameters (parser,
16453 /*num_templates=*/0,
16454 token->location,
16455 /*declarator=*/NULL))
16456 return error_mark_node;
16457 type = xref_tag (tag_type, identifier, ts, template_p);
16458 }
16459 }
16460
16461 if (type == error_mark_node)
16462 return error_mark_node;
16463
16464 /* Allow attributes on forward declarations of classes. */
16465 if (attributes)
16466 {
16467 if (TREE_CODE (type) == TYPENAME_TYPE)
16468 warning (OPT_Wattributes,
16469 "attributes ignored on uninstantiated type");
16470 else if (tag_type != enum_type && CLASSTYPE_TEMPLATE_INSTANTIATION (type)
16471 && ! processing_explicit_instantiation)
16472 warning (OPT_Wattributes,
16473 "attributes ignored on template instantiation");
16474 else if (is_declaration && cp_parser_declares_only_class_p (parser))
16475 cplus_decl_attributes (&type, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
16476 else
16477 warning (OPT_Wattributes,
16478 "attributes ignored on elaborated-type-specifier that is not a forward declaration");
16479 }
16480
16481 if (tag_type != enum_type)
16482 {
16483 /* Indicate whether this class was declared as a `class' or as a
16484 `struct'. */
16485 if (TREE_CODE (type) == RECORD_TYPE)
16486 CLASSTYPE_DECLARED_CLASS (type) = (tag_type == class_type);
16487 cp_parser_check_class_key (tag_type, type);
16488 }
16489
16490 /* A "<" cannot follow an elaborated type specifier. If that
16491 happens, the user was probably trying to form a template-id. */
16492 cp_parser_check_for_invalid_template_id (parser, type, tag_type,
16493 token->location);
16494
16495 return type;
16496 }
16497
16498 /* Parse an enum-specifier.
16499
16500 enum-specifier:
16501 enum-head { enumerator-list [opt] }
16502 enum-head { enumerator-list , } [C++0x]
16503
16504 enum-head:
16505 enum-key identifier [opt] enum-base [opt]
16506 enum-key nested-name-specifier identifier enum-base [opt]
16507
16508 enum-key:
16509 enum
16510 enum class [C++0x]
16511 enum struct [C++0x]
16512
16513 enum-base: [C++0x]
16514 : type-specifier-seq
16515
16516 opaque-enum-specifier:
16517 enum-key identifier enum-base [opt] ;
16518
16519 GNU Extensions:
16520 enum-key attributes[opt] identifier [opt] enum-base [opt]
16521 { enumerator-list [opt] }attributes[opt]
16522 enum-key attributes[opt] identifier [opt] enum-base [opt]
16523 { enumerator-list, }attributes[opt] [C++0x]
16524
16525 Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
16526 if the token stream isn't an enum-specifier after all. */
16527
16528 static tree
16529 cp_parser_enum_specifier (cp_parser* parser)
16530 {
16531 tree identifier;
16532 tree type = NULL_TREE;
16533 tree prev_scope;
16534 tree nested_name_specifier = NULL_TREE;
16535 tree attributes;
16536 bool scoped_enum_p = false;
16537 bool has_underlying_type = false;
16538 bool nested_being_defined = false;
16539 bool new_value_list = false;
16540 bool is_new_type = false;
16541 bool is_anonymous = false;
16542 tree underlying_type = NULL_TREE;
16543 cp_token *type_start_token = NULL;
16544 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
16545
16546 parser->colon_corrects_to_scope_p = false;
16547
16548 /* Parse tentatively so that we can back up if we don't find a
16549 enum-specifier. */
16550 cp_parser_parse_tentatively (parser);
16551
16552 /* Caller guarantees that the current token is 'enum', an identifier
16553 possibly follows, and the token after that is an opening brace.
16554 If we don't have an identifier, fabricate an anonymous name for
16555 the enumeration being defined. */
16556 cp_lexer_consume_token (parser->lexer);
16557
16558 /* Parse the "class" or "struct", which indicates a scoped
16559 enumeration type in C++0x. */
16560 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
16561 || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
16562 {
16563 if (cxx_dialect < cxx11)
16564 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
16565
16566 /* Consume the `struct' or `class' token. */
16567 cp_lexer_consume_token (parser->lexer);
16568
16569 scoped_enum_p = true;
16570 }
16571
16572 attributes = cp_parser_attributes_opt (parser);
16573
16574 /* Clear the qualification. */
16575 parser->scope = NULL_TREE;
16576 parser->qualifying_scope = NULL_TREE;
16577 parser->object_scope = NULL_TREE;
16578
16579 /* Figure out in what scope the declaration is being placed. */
16580 prev_scope = current_scope ();
16581
16582 type_start_token = cp_lexer_peek_token (parser->lexer);
16583
16584 push_deferring_access_checks (dk_no_check);
16585 nested_name_specifier
16586 = cp_parser_nested_name_specifier_opt (parser,
16587 /*typename_keyword_p=*/true,
16588 /*check_dependency_p=*/false,
16589 /*type_p=*/false,
16590 /*is_declaration=*/false);
16591
16592 if (nested_name_specifier)
16593 {
16594 tree name;
16595
16596 identifier = cp_parser_identifier (parser);
16597 name = cp_parser_lookup_name (parser, identifier,
16598 enum_type,
16599 /*is_template=*/false,
16600 /*is_namespace=*/false,
16601 /*check_dependency=*/true,
16602 /*ambiguous_decls=*/NULL,
16603 input_location);
16604 if (name && name != error_mark_node)
16605 {
16606 type = TREE_TYPE (name);
16607 if (TREE_CODE (type) == TYPENAME_TYPE)
16608 {
16609 /* Are template enums allowed in ISO? */
16610 if (template_parm_scope_p ())
16611 pedwarn (type_start_token->location, OPT_Wpedantic,
16612 "%qD is an enumeration template", name);
16613 /* ignore a typename reference, for it will be solved by name
16614 in start_enum. */
16615 type = NULL_TREE;
16616 }
16617 }
16618 else if (nested_name_specifier == error_mark_node)
16619 /* We already issued an error. */;
16620 else
16621 {
16622 error_at (type_start_token->location,
16623 "%qD does not name an enumeration in %qT",
16624 identifier, nested_name_specifier);
16625 nested_name_specifier = error_mark_node;
16626 }
16627 }
16628 else
16629 {
16630 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
16631 identifier = cp_parser_identifier (parser);
16632 else
16633 {
16634 identifier = make_anon_name ();
16635 is_anonymous = true;
16636 if (scoped_enum_p)
16637 error_at (type_start_token->location,
16638 "anonymous scoped enum is not allowed");
16639 }
16640 }
16641 pop_deferring_access_checks ();
16642
16643 /* Check for the `:' that denotes a specified underlying type in C++0x.
16644 Note that a ':' could also indicate a bitfield width, however. */
16645 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
16646 {
16647 cp_decl_specifier_seq type_specifiers;
16648
16649 /* Consume the `:'. */
16650 cp_lexer_consume_token (parser->lexer);
16651
16652 /* Parse the type-specifier-seq. */
16653 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
16654 /*is_trailing_return=*/false,
16655 &type_specifiers);
16656
16657 /* At this point this is surely not elaborated type specifier. */
16658 if (!cp_parser_parse_definitely (parser))
16659 return NULL_TREE;
16660
16661 if (cxx_dialect < cxx11)
16662 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
16663
16664 has_underlying_type = true;
16665
16666 /* If that didn't work, stop. */
16667 if (type_specifiers.type != error_mark_node)
16668 {
16669 underlying_type = grokdeclarator (NULL, &type_specifiers, TYPENAME,
16670 /*initialized=*/0, NULL);
16671 if (underlying_type == error_mark_node
16672 || check_for_bare_parameter_packs (underlying_type))
16673 underlying_type = NULL_TREE;
16674 }
16675 }
16676
16677 /* Look for the `{' but don't consume it yet. */
16678 if (!cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
16679 {
16680 if (cxx_dialect < cxx11 || (!scoped_enum_p && !underlying_type))
16681 {
16682 cp_parser_error (parser, "expected %<{%>");
16683 if (has_underlying_type)
16684 {
16685 type = NULL_TREE;
16686 goto out;
16687 }
16688 }
16689 /* An opaque-enum-specifier must have a ';' here. */
16690 if ((scoped_enum_p || underlying_type)
16691 && cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
16692 {
16693 cp_parser_error (parser, "expected %<;%> or %<{%>");
16694 if (has_underlying_type)
16695 {
16696 type = NULL_TREE;
16697 goto out;
16698 }
16699 }
16700 }
16701
16702 if (!has_underlying_type && !cp_parser_parse_definitely (parser))
16703 return NULL_TREE;
16704
16705 if (nested_name_specifier)
16706 {
16707 if (CLASS_TYPE_P (nested_name_specifier))
16708 {
16709 nested_being_defined = TYPE_BEING_DEFINED (nested_name_specifier);
16710 TYPE_BEING_DEFINED (nested_name_specifier) = 1;
16711 push_scope (nested_name_specifier);
16712 }
16713 else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
16714 {
16715 push_nested_namespace (nested_name_specifier);
16716 }
16717 }
16718
16719 /* Issue an error message if type-definitions are forbidden here. */
16720 if (!cp_parser_check_type_definition (parser))
16721 type = error_mark_node;
16722 else
16723 /* Create the new type. We do this before consuming the opening
16724 brace so the enum will be recorded as being on the line of its
16725 tag (or the 'enum' keyword, if there is no tag). */
16726 type = start_enum (identifier, type, underlying_type,
16727 scoped_enum_p, &is_new_type);
16728
16729 /* If the next token is not '{' it is an opaque-enum-specifier or an
16730 elaborated-type-specifier. */
16731 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
16732 {
16733 timevar_push (TV_PARSE_ENUM);
16734 if (nested_name_specifier
16735 && nested_name_specifier != error_mark_node)
16736 {
16737 /* The following catches invalid code such as:
16738 enum class S<int>::E { A, B, C }; */
16739 if (!processing_specialization
16740 && CLASS_TYPE_P (nested_name_specifier)
16741 && CLASSTYPE_USE_TEMPLATE (nested_name_specifier))
16742 error_at (type_start_token->location, "cannot add an enumerator "
16743 "list to a template instantiation");
16744
16745 if (TREE_CODE (nested_name_specifier) == TYPENAME_TYPE)
16746 {
16747 error_at (type_start_token->location,
16748 "%<%T::%E%> has not been declared",
16749 TYPE_CONTEXT (nested_name_specifier),
16750 nested_name_specifier);
16751 type = error_mark_node;
16752 }
16753 else if (TREE_CODE (nested_name_specifier) != NAMESPACE_DECL
16754 && !CLASS_TYPE_P (nested_name_specifier))
16755 {
16756 error_at (type_start_token->location, "nested name specifier "
16757 "%qT for enum declaration does not name a class "
16758 "or namespace", nested_name_specifier);
16759 type = error_mark_node;
16760 }
16761 /* If that scope does not contain the scope in which the
16762 class was originally declared, the program is invalid. */
16763 else if (prev_scope && !is_ancestor (prev_scope,
16764 nested_name_specifier))
16765 {
16766 if (at_namespace_scope_p ())
16767 error_at (type_start_token->location,
16768 "declaration of %qD in namespace %qD which does not "
16769 "enclose %qD",
16770 type, prev_scope, nested_name_specifier);
16771 else
16772 error_at (type_start_token->location,
16773 "declaration of %qD in %qD which does not "
16774 "enclose %qD",
16775 type, prev_scope, nested_name_specifier);
16776 type = error_mark_node;
16777 }
16778 }
16779
16780 if (scoped_enum_p)
16781 begin_scope (sk_scoped_enum, type);
16782
16783 /* Consume the opening brace. */
16784 cp_lexer_consume_token (parser->lexer);
16785
16786 if (type == error_mark_node)
16787 ; /* Nothing to add */
16788 else if (OPAQUE_ENUM_P (type)
16789 || (cxx_dialect > cxx98 && processing_specialization))
16790 {
16791 new_value_list = true;
16792 SET_OPAQUE_ENUM_P (type, false);
16793 DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
16794 }
16795 else
16796 {
16797 error_at (type_start_token->location,
16798 "multiple definition of %q#T", type);
16799 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)),
16800 "previous definition here");
16801 type = error_mark_node;
16802 }
16803
16804 if (type == error_mark_node)
16805 cp_parser_skip_to_end_of_block_or_statement (parser);
16806 /* If the next token is not '}', then there are some enumerators. */
16807 else if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
16808 {
16809 if (is_anonymous && !scoped_enum_p)
16810 pedwarn (type_start_token->location, OPT_Wpedantic,
16811 "ISO C++ forbids empty anonymous enum");
16812 }
16813 else
16814 cp_parser_enumerator_list (parser, type);
16815
16816 /* Consume the final '}'. */
16817 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
16818
16819 if (scoped_enum_p)
16820 finish_scope ();
16821 timevar_pop (TV_PARSE_ENUM);
16822 }
16823 else
16824 {
16825 /* If a ';' follows, then it is an opaque-enum-specifier
16826 and additional restrictions apply. */
16827 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
16828 {
16829 if (is_anonymous)
16830 error_at (type_start_token->location,
16831 "opaque-enum-specifier without name");
16832 else if (nested_name_specifier)
16833 error_at (type_start_token->location,
16834 "opaque-enum-specifier must use a simple identifier");
16835 }
16836 }
16837
16838 /* Look for trailing attributes to apply to this enumeration, and
16839 apply them if appropriate. */
16840 if (cp_parser_allow_gnu_extensions_p (parser))
16841 {
16842 tree trailing_attr = cp_parser_gnu_attributes_opt (parser);
16843 trailing_attr = chainon (trailing_attr, attributes);
16844 cplus_decl_attributes (&type,
16845 trailing_attr,
16846 (int) ATTR_FLAG_TYPE_IN_PLACE);
16847 }
16848
16849 /* Finish up the enumeration. */
16850 if (type != error_mark_node)
16851 {
16852 if (new_value_list)
16853 finish_enum_value_list (type);
16854 if (is_new_type)
16855 finish_enum (type);
16856 }
16857
16858 if (nested_name_specifier)
16859 {
16860 if (CLASS_TYPE_P (nested_name_specifier))
16861 {
16862 TYPE_BEING_DEFINED (nested_name_specifier) = nested_being_defined;
16863 pop_scope (nested_name_specifier);
16864 }
16865 else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
16866 {
16867 pop_nested_namespace (nested_name_specifier);
16868 }
16869 }
16870 out:
16871 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
16872 return type;
16873 }
16874
16875 /* Parse an enumerator-list. The enumerators all have the indicated
16876 TYPE.
16877
16878 enumerator-list:
16879 enumerator-definition
16880 enumerator-list , enumerator-definition */
16881
16882 static void
16883 cp_parser_enumerator_list (cp_parser* parser, tree type)
16884 {
16885 while (true)
16886 {
16887 /* Parse an enumerator-definition. */
16888 cp_parser_enumerator_definition (parser, type);
16889
16890 /* If the next token is not a ',', we've reached the end of
16891 the list. */
16892 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
16893 break;
16894 /* Otherwise, consume the `,' and keep going. */
16895 cp_lexer_consume_token (parser->lexer);
16896 /* If the next token is a `}', there is a trailing comma. */
16897 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
16898 {
16899 if (cxx_dialect < cxx11 && !in_system_header_at (input_location))
16900 pedwarn (input_location, OPT_Wpedantic,
16901 "comma at end of enumerator list");
16902 break;
16903 }
16904 }
16905 }
16906
16907 /* Parse an enumerator-definition. The enumerator has the indicated
16908 TYPE.
16909
16910 enumerator-definition:
16911 enumerator
16912 enumerator = constant-expression
16913
16914 enumerator:
16915 identifier
16916
16917 GNU Extensions:
16918
16919 enumerator-definition:
16920 enumerator attributes [opt]
16921 enumerator attributes [opt] = constant-expression */
16922
16923 static void
16924 cp_parser_enumerator_definition (cp_parser* parser, tree type)
16925 {
16926 tree identifier;
16927 tree value;
16928 location_t loc;
16929
16930 /* Save the input location because we are interested in the location
16931 of the identifier and not the location of the explicit value. */
16932 loc = cp_lexer_peek_token (parser->lexer)->location;
16933
16934 /* Look for the identifier. */
16935 identifier = cp_parser_identifier (parser);
16936 if (identifier == error_mark_node)
16937 return;
16938
16939 /* Parse any specified attributes. */
16940 tree attrs = cp_parser_attributes_opt (parser);
16941
16942 /* If the next token is an '=', then there is an explicit value. */
16943 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
16944 {
16945 /* Consume the `=' token. */
16946 cp_lexer_consume_token (parser->lexer);
16947 /* Parse the value. */
16948 value = cp_parser_constant_expression (parser);
16949 }
16950 else
16951 value = NULL_TREE;
16952
16953 /* If we are processing a template, make sure the initializer of the
16954 enumerator doesn't contain any bare template parameter pack. */
16955 if (check_for_bare_parameter_packs (value))
16956 value = error_mark_node;
16957
16958 /* Create the enumerator. */
16959 build_enumerator (identifier, value, type, attrs, loc);
16960 }
16961
16962 /* Parse a namespace-name.
16963
16964 namespace-name:
16965 original-namespace-name
16966 namespace-alias
16967
16968 Returns the NAMESPACE_DECL for the namespace. */
16969
16970 static tree
16971 cp_parser_namespace_name (cp_parser* parser)
16972 {
16973 tree identifier;
16974 tree namespace_decl;
16975
16976 cp_token *token = cp_lexer_peek_token (parser->lexer);
16977
16978 /* Get the name of the namespace. */
16979 identifier = cp_parser_identifier (parser);
16980 if (identifier == error_mark_node)
16981 return error_mark_node;
16982
16983 /* Look up the identifier in the currently active scope. Look only
16984 for namespaces, due to:
16985
16986 [basic.lookup.udir]
16987
16988 When looking up a namespace-name in a using-directive or alias
16989 definition, only namespace names are considered.
16990
16991 And:
16992
16993 [basic.lookup.qual]
16994
16995 During the lookup of a name preceding the :: scope resolution
16996 operator, object, function, and enumerator names are ignored.
16997
16998 (Note that cp_parser_qualifying_entity only calls this
16999 function if the token after the name is the scope resolution
17000 operator.) */
17001 namespace_decl = cp_parser_lookup_name (parser, identifier,
17002 none_type,
17003 /*is_template=*/false,
17004 /*is_namespace=*/true,
17005 /*check_dependency=*/true,
17006 /*ambiguous_decls=*/NULL,
17007 token->location);
17008 /* If it's not a namespace, issue an error. */
17009 if (namespace_decl == error_mark_node
17010 || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
17011 {
17012 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
17013 error_at (token->location, "%qD is not a namespace-name", identifier);
17014 cp_parser_error (parser, "expected namespace-name");
17015 namespace_decl = error_mark_node;
17016 }
17017
17018 return namespace_decl;
17019 }
17020
17021 /* Parse a namespace-definition.
17022
17023 namespace-definition:
17024 named-namespace-definition
17025 unnamed-namespace-definition
17026
17027 named-namespace-definition:
17028 original-namespace-definition
17029 extension-namespace-definition
17030
17031 original-namespace-definition:
17032 namespace identifier { namespace-body }
17033
17034 extension-namespace-definition:
17035 namespace original-namespace-name { namespace-body }
17036
17037 unnamed-namespace-definition:
17038 namespace { namespace-body } */
17039
17040 static void
17041 cp_parser_namespace_definition (cp_parser* parser)
17042 {
17043 tree identifier, attribs;
17044 bool has_visibility;
17045 bool is_inline;
17046 cp_token* token;
17047 int nested_definition_count = 0;
17048
17049 cp_ensure_no_omp_declare_simd (parser);
17050 cp_ensure_no_oacc_routine (parser);
17051 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_INLINE))
17052 {
17053 maybe_warn_cpp0x (CPP0X_INLINE_NAMESPACES);
17054 is_inline = true;
17055 cp_lexer_consume_token (parser->lexer);
17056 }
17057 else
17058 is_inline = false;
17059
17060 /* Look for the `namespace' keyword. */
17061 token = cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
17062
17063 /* Parse any specified attributes before the identifier. */
17064 attribs = cp_parser_attributes_opt (parser);
17065
17066 /* Get the name of the namespace. We do not attempt to distinguish
17067 between an original-namespace-definition and an
17068 extension-namespace-definition at this point. The semantic
17069 analysis routines are responsible for that. */
17070 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
17071 identifier = cp_parser_identifier (parser);
17072 else
17073 identifier = NULL_TREE;
17074
17075 /* Parse any specified attributes after the identifier. */
17076 tree post_ident_attribs = cp_parser_attributes_opt (parser);
17077 if (post_ident_attribs)
17078 {
17079 if (attribs)
17080 attribs = chainon (attribs, post_ident_attribs);
17081 else
17082 attribs = post_ident_attribs;
17083 }
17084
17085 /* Start the namespace. */
17086 push_namespace (identifier);
17087
17088 /* Parse any nested namespace definition. */
17089 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
17090 {
17091 if (attribs)
17092 error_at (token->location, "a nested namespace definition cannot have attributes");
17093 if (cxx_dialect < cxx1z)
17094 pedwarn (input_location, OPT_Wpedantic,
17095 "nested namespace definitions only available with "
17096 "-std=c++1z or -std=gnu++1z");
17097 if (is_inline)
17098 error_at (token->location, "a nested namespace definition cannot be inline");
17099 while (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
17100 {
17101 cp_lexer_consume_token (parser->lexer);
17102 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
17103 identifier = cp_parser_identifier (parser);
17104 else
17105 {
17106 cp_parser_error (parser, "nested identifier required");
17107 break;
17108 }
17109 ++nested_definition_count;
17110 push_namespace (identifier);
17111 }
17112 }
17113
17114 /* Look for the `{' to validate starting the namespace. */
17115 cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
17116
17117 /* "inline namespace" is equivalent to a stub namespace definition
17118 followed by a strong using directive. */
17119 if (is_inline)
17120 {
17121 tree name_space = current_namespace;
17122 /* Set up namespace association. */
17123 DECL_NAMESPACE_ASSOCIATIONS (name_space)
17124 = tree_cons (CP_DECL_CONTEXT (name_space), NULL_TREE,
17125 DECL_NAMESPACE_ASSOCIATIONS (name_space));
17126 /* Import the contents of the inline namespace. */
17127 pop_namespace ();
17128 do_using_directive (name_space);
17129 push_namespace (identifier);
17130 }
17131
17132 has_visibility = handle_namespace_attrs (current_namespace, attribs);
17133
17134 warning (OPT_Wnamespaces, "namespace %qD entered", current_namespace);
17135
17136 /* Parse the body of the namespace. */
17137 cp_parser_namespace_body (parser);
17138
17139 if (has_visibility)
17140 pop_visibility (1);
17141
17142 /* Finish the nested namespace definitions. */
17143 while (nested_definition_count--)
17144 pop_namespace ();
17145
17146 /* Finish the namespace. */
17147 pop_namespace ();
17148 /* Look for the final `}'. */
17149 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
17150 }
17151
17152 /* Parse a namespace-body.
17153
17154 namespace-body:
17155 declaration-seq [opt] */
17156
17157 static void
17158 cp_parser_namespace_body (cp_parser* parser)
17159 {
17160 cp_parser_declaration_seq_opt (parser);
17161 }
17162
17163 /* Parse a namespace-alias-definition.
17164
17165 namespace-alias-definition:
17166 namespace identifier = qualified-namespace-specifier ; */
17167
17168 static void
17169 cp_parser_namespace_alias_definition (cp_parser* parser)
17170 {
17171 tree identifier;
17172 tree namespace_specifier;
17173
17174 cp_token *token = cp_lexer_peek_token (parser->lexer);
17175
17176 /* Look for the `namespace' keyword. */
17177 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
17178 /* Look for the identifier. */
17179 identifier = cp_parser_identifier (parser);
17180 if (identifier == error_mark_node)
17181 return;
17182 /* Look for the `=' token. */
17183 if (!cp_parser_uncommitted_to_tentative_parse_p (parser)
17184 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
17185 {
17186 error_at (token->location, "%<namespace%> definition is not allowed here");
17187 /* Skip the definition. */
17188 cp_lexer_consume_token (parser->lexer);
17189 if (cp_parser_skip_to_closing_brace (parser))
17190 cp_lexer_consume_token (parser->lexer);
17191 return;
17192 }
17193 cp_parser_require (parser, CPP_EQ, RT_EQ);
17194 /* Look for the qualified-namespace-specifier. */
17195 namespace_specifier
17196 = cp_parser_qualified_namespace_specifier (parser);
17197 /* Look for the `;' token. */
17198 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
17199
17200 /* Register the alias in the symbol table. */
17201 do_namespace_alias (identifier, namespace_specifier);
17202 }
17203
17204 /* Parse a qualified-namespace-specifier.
17205
17206 qualified-namespace-specifier:
17207 :: [opt] nested-name-specifier [opt] namespace-name
17208
17209 Returns a NAMESPACE_DECL corresponding to the specified
17210 namespace. */
17211
17212 static tree
17213 cp_parser_qualified_namespace_specifier (cp_parser* parser)
17214 {
17215 /* Look for the optional `::'. */
17216 cp_parser_global_scope_opt (parser,
17217 /*current_scope_valid_p=*/false);
17218
17219 /* Look for the optional nested-name-specifier. */
17220 cp_parser_nested_name_specifier_opt (parser,
17221 /*typename_keyword_p=*/false,
17222 /*check_dependency_p=*/true,
17223 /*type_p=*/false,
17224 /*is_declaration=*/true);
17225
17226 return cp_parser_namespace_name (parser);
17227 }
17228
17229 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
17230 access declaration.
17231
17232 using-declaration:
17233 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
17234 using :: unqualified-id ;
17235
17236 access-declaration:
17237 qualified-id ;
17238
17239 */
17240
17241 static bool
17242 cp_parser_using_declaration (cp_parser* parser,
17243 bool access_declaration_p)
17244 {
17245 cp_token *token;
17246 bool typename_p = false;
17247 bool global_scope_p;
17248 tree decl;
17249 tree identifier;
17250 tree qscope;
17251 int oldcount = errorcount;
17252 cp_token *diag_token = NULL;
17253
17254 if (access_declaration_p)
17255 {
17256 diag_token = cp_lexer_peek_token (parser->lexer);
17257 cp_parser_parse_tentatively (parser);
17258 }
17259 else
17260 {
17261 /* Look for the `using' keyword. */
17262 cp_parser_require_keyword (parser, RID_USING, RT_USING);
17263
17264 /* Peek at the next token. */
17265 token = cp_lexer_peek_token (parser->lexer);
17266 /* See if it's `typename'. */
17267 if (token->keyword == RID_TYPENAME)
17268 {
17269 /* Remember that we've seen it. */
17270 typename_p = true;
17271 /* Consume the `typename' token. */
17272 cp_lexer_consume_token (parser->lexer);
17273 }
17274 }
17275
17276 /* Look for the optional global scope qualification. */
17277 global_scope_p
17278 = (cp_parser_global_scope_opt (parser,
17279 /*current_scope_valid_p=*/false)
17280 != NULL_TREE);
17281
17282 /* If we saw `typename', or didn't see `::', then there must be a
17283 nested-name-specifier present. */
17284 if (typename_p || !global_scope_p)
17285 {
17286 qscope = cp_parser_nested_name_specifier (parser, typename_p,
17287 /*check_dependency_p=*/true,
17288 /*type_p=*/false,
17289 /*is_declaration=*/true);
17290 if (!qscope && !cp_parser_uncommitted_to_tentative_parse_p (parser))
17291 {
17292 cp_parser_skip_to_end_of_block_or_statement (parser);
17293 return false;
17294 }
17295 }
17296 /* Otherwise, we could be in either of the two productions. In that
17297 case, treat the nested-name-specifier as optional. */
17298 else
17299 qscope = cp_parser_nested_name_specifier_opt (parser,
17300 /*typename_keyword_p=*/false,
17301 /*check_dependency_p=*/true,
17302 /*type_p=*/false,
17303 /*is_declaration=*/true);
17304 if (!qscope)
17305 qscope = global_namespace;
17306 else if (UNSCOPED_ENUM_P (qscope))
17307 qscope = CP_TYPE_CONTEXT (qscope);
17308
17309 if (access_declaration_p && cp_parser_error_occurred (parser))
17310 /* Something has already gone wrong; there's no need to parse
17311 further. Since an error has occurred, the return value of
17312 cp_parser_parse_definitely will be false, as required. */
17313 return cp_parser_parse_definitely (parser);
17314
17315 token = cp_lexer_peek_token (parser->lexer);
17316 /* Parse the unqualified-id. */
17317 identifier = cp_parser_unqualified_id (parser,
17318 /*template_keyword_p=*/false,
17319 /*check_dependency_p=*/true,
17320 /*declarator_p=*/true,
17321 /*optional_p=*/false);
17322
17323 if (access_declaration_p)
17324 {
17325 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
17326 cp_parser_simulate_error (parser);
17327 if (!cp_parser_parse_definitely (parser))
17328 return false;
17329 }
17330
17331 /* The function we call to handle a using-declaration is different
17332 depending on what scope we are in. */
17333 if (qscope == error_mark_node || identifier == error_mark_node)
17334 ;
17335 else if (!identifier_p (identifier)
17336 && TREE_CODE (identifier) != BIT_NOT_EXPR)
17337 /* [namespace.udecl]
17338
17339 A using declaration shall not name a template-id. */
17340 error_at (token->location,
17341 "a template-id may not appear in a using-declaration");
17342 else
17343 {
17344 if (at_class_scope_p ())
17345 {
17346 /* Create the USING_DECL. */
17347 decl = do_class_using_decl (parser->scope, identifier);
17348
17349 if (decl && typename_p)
17350 USING_DECL_TYPENAME_P (decl) = 1;
17351
17352 if (check_for_bare_parameter_packs (decl))
17353 {
17354 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
17355 return false;
17356 }
17357 else
17358 /* Add it to the list of members in this class. */
17359 finish_member_declaration (decl);
17360 }
17361 else
17362 {
17363 decl = cp_parser_lookup_name_simple (parser,
17364 identifier,
17365 token->location);
17366 if (decl == error_mark_node)
17367 cp_parser_name_lookup_error (parser, identifier,
17368 decl, NLE_NULL,
17369 token->location);
17370 else if (check_for_bare_parameter_packs (decl))
17371 {
17372 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
17373 return false;
17374 }
17375 else if (!at_namespace_scope_p ())
17376 do_local_using_decl (decl, qscope, identifier);
17377 else
17378 do_toplevel_using_decl (decl, qscope, identifier);
17379 }
17380 }
17381
17382 /* Look for the final `;'. */
17383 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
17384
17385 if (access_declaration_p && errorcount == oldcount)
17386 warning_at (diag_token->location, OPT_Wdeprecated,
17387 "access declarations are deprecated "
17388 "in favour of using-declarations; "
17389 "suggestion: add the %<using%> keyword");
17390
17391 return true;
17392 }
17393
17394 /* Parse an alias-declaration.
17395
17396 alias-declaration:
17397 using identifier attribute-specifier-seq [opt] = type-id */
17398
17399 static tree
17400 cp_parser_alias_declaration (cp_parser* parser)
17401 {
17402 tree id, type, decl, pushed_scope = NULL_TREE, attributes;
17403 location_t id_location;
17404 cp_declarator *declarator;
17405 cp_decl_specifier_seq decl_specs;
17406 bool member_p;
17407 const char *saved_message = NULL;
17408
17409 /* Look for the `using' keyword. */
17410 cp_token *using_token
17411 = cp_parser_require_keyword (parser, RID_USING, RT_USING);
17412 if (using_token == NULL)
17413 return error_mark_node;
17414
17415 id_location = cp_lexer_peek_token (parser->lexer)->location;
17416 id = cp_parser_identifier (parser);
17417 if (id == error_mark_node)
17418 return error_mark_node;
17419
17420 cp_token *attrs_token = cp_lexer_peek_token (parser->lexer);
17421 attributes = cp_parser_attributes_opt (parser);
17422 if (attributes == error_mark_node)
17423 return error_mark_node;
17424
17425 cp_parser_require (parser, CPP_EQ, RT_EQ);
17426
17427 if (cp_parser_error_occurred (parser))
17428 return error_mark_node;
17429
17430 cp_parser_commit_to_tentative_parse (parser);
17431
17432 /* Now we are going to parse the type-id of the declaration. */
17433
17434 /*
17435 [dcl.type]/3 says:
17436
17437 "A type-specifier-seq shall not define a class or enumeration
17438 unless it appears in the type-id of an alias-declaration (7.1.3) that
17439 is not the declaration of a template-declaration."
17440
17441 In other words, if we currently are in an alias template, the
17442 type-id should not define a type.
17443
17444 So let's set parser->type_definition_forbidden_message in that
17445 case; cp_parser_check_type_definition (called by
17446 cp_parser_class_specifier) will then emit an error if a type is
17447 defined in the type-id. */
17448 if (parser->num_template_parameter_lists)
17449 {
17450 saved_message = parser->type_definition_forbidden_message;
17451 parser->type_definition_forbidden_message =
17452 G_("types may not be defined in alias template declarations");
17453 }
17454
17455 type = cp_parser_type_id (parser);
17456
17457 /* Restore the error message if need be. */
17458 if (parser->num_template_parameter_lists)
17459 parser->type_definition_forbidden_message = saved_message;
17460
17461 if (type == error_mark_node
17462 || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
17463 {
17464 cp_parser_skip_to_end_of_block_or_statement (parser);
17465 return error_mark_node;
17466 }
17467
17468 /* A typedef-name can also be introduced by an alias-declaration. The
17469 identifier following the using keyword becomes a typedef-name. It has
17470 the same semantics as if it were introduced by the typedef
17471 specifier. In particular, it does not define a new type and it shall
17472 not appear in the type-id. */
17473
17474 clear_decl_specs (&decl_specs);
17475 decl_specs.type = type;
17476 if (attributes != NULL_TREE)
17477 {
17478 decl_specs.attributes = attributes;
17479 set_and_check_decl_spec_loc (&decl_specs,
17480 ds_attribute,
17481 attrs_token);
17482 }
17483 set_and_check_decl_spec_loc (&decl_specs,
17484 ds_typedef,
17485 using_token);
17486 set_and_check_decl_spec_loc (&decl_specs,
17487 ds_alias,
17488 using_token);
17489
17490 declarator = make_id_declarator (NULL_TREE, id, sfk_none);
17491 declarator->id_loc = id_location;
17492
17493 member_p = at_class_scope_p ();
17494 if (member_p)
17495 decl = grokfield (declarator, &decl_specs, NULL_TREE, false,
17496 NULL_TREE, attributes);
17497 else
17498 decl = start_decl (declarator, &decl_specs, 0,
17499 attributes, NULL_TREE, &pushed_scope);
17500 if (decl == error_mark_node)
17501 return decl;
17502
17503 // Attach constraints to the alias declaration.
17504 if (flag_concepts && current_template_parms)
17505 {
17506 tree reqs = TEMPLATE_PARMS_CONSTRAINTS (current_template_parms);
17507 tree constr = build_constraints (reqs, NULL_TREE);
17508 set_constraints (decl, constr);
17509 }
17510
17511 cp_finish_decl (decl, NULL_TREE, 0, NULL_TREE, 0);
17512
17513 if (pushed_scope)
17514 pop_scope (pushed_scope);
17515
17516 /* If decl is a template, return its TEMPLATE_DECL so that it gets
17517 added into the symbol table; otherwise, return the TYPE_DECL. */
17518 if (DECL_LANG_SPECIFIC (decl)
17519 && DECL_TEMPLATE_INFO (decl)
17520 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl)))
17521 {
17522 decl = DECL_TI_TEMPLATE (decl);
17523 if (member_p)
17524 check_member_template (decl);
17525 }
17526
17527 return decl;
17528 }
17529
17530 /* Parse a using-directive.
17531
17532 using-directive:
17533 using namespace :: [opt] nested-name-specifier [opt]
17534 namespace-name ; */
17535
17536 static void
17537 cp_parser_using_directive (cp_parser* parser)
17538 {
17539 tree namespace_decl;
17540 tree attribs;
17541
17542 /* Look for the `using' keyword. */
17543 cp_parser_require_keyword (parser, RID_USING, RT_USING);
17544 /* And the `namespace' keyword. */
17545 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
17546 /* Look for the optional `::' operator. */
17547 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
17548 /* And the optional nested-name-specifier. */
17549 cp_parser_nested_name_specifier_opt (parser,
17550 /*typename_keyword_p=*/false,
17551 /*check_dependency_p=*/true,
17552 /*type_p=*/false,
17553 /*is_declaration=*/true);
17554 /* Get the namespace being used. */
17555 namespace_decl = cp_parser_namespace_name (parser);
17556 /* And any specified attributes. */
17557 attribs = cp_parser_attributes_opt (parser);
17558 /* Update the symbol table. */
17559 parse_using_directive (namespace_decl, attribs);
17560 /* Look for the final `;'. */
17561 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
17562 }
17563
17564 /* Parse an asm-definition.
17565
17566 asm-definition:
17567 asm ( string-literal ) ;
17568
17569 GNU Extension:
17570
17571 asm-definition:
17572 asm volatile [opt] ( string-literal ) ;
17573 asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
17574 asm volatile [opt] ( string-literal : asm-operand-list [opt]
17575 : asm-operand-list [opt] ) ;
17576 asm volatile [opt] ( string-literal : asm-operand-list [opt]
17577 : asm-operand-list [opt]
17578 : asm-clobber-list [opt] ) ;
17579 asm volatile [opt] goto ( string-literal : : asm-operand-list [opt]
17580 : asm-clobber-list [opt]
17581 : asm-goto-list ) ; */
17582
17583 static void
17584 cp_parser_asm_definition (cp_parser* parser)
17585 {
17586 tree string;
17587 tree outputs = NULL_TREE;
17588 tree inputs = NULL_TREE;
17589 tree clobbers = NULL_TREE;
17590 tree labels = NULL_TREE;
17591 tree asm_stmt;
17592 bool volatile_p = false;
17593 bool extended_p = false;
17594 bool invalid_inputs_p = false;
17595 bool invalid_outputs_p = false;
17596 bool goto_p = false;
17597 required_token missing = RT_NONE;
17598
17599 /* Look for the `asm' keyword. */
17600 cp_parser_require_keyword (parser, RID_ASM, RT_ASM);
17601
17602 if (parser->in_function_body
17603 && DECL_DECLARED_CONSTEXPR_P (current_function_decl))
17604 {
17605 error ("%<asm%> in %<constexpr%> function");
17606 cp_function_chain->invalid_constexpr = true;
17607 }
17608
17609 /* See if the next token is `volatile'. */
17610 if (cp_parser_allow_gnu_extensions_p (parser)
17611 && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
17612 {
17613 /* Remember that we saw the `volatile' keyword. */
17614 volatile_p = true;
17615 /* Consume the token. */
17616 cp_lexer_consume_token (parser->lexer);
17617 }
17618 if (cp_parser_allow_gnu_extensions_p (parser)
17619 && parser->in_function_body
17620 && cp_lexer_next_token_is_keyword (parser->lexer, RID_GOTO))
17621 {
17622 /* Remember that we saw the `goto' keyword. */
17623 goto_p = true;
17624 /* Consume the token. */
17625 cp_lexer_consume_token (parser->lexer);
17626 }
17627 /* Look for the opening `('. */
17628 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
17629 return;
17630 /* Look for the string. */
17631 string = cp_parser_string_literal (parser, false, false);
17632 if (string == error_mark_node)
17633 {
17634 cp_parser_skip_to_closing_parenthesis (parser, true, false,
17635 /*consume_paren=*/true);
17636 return;
17637 }
17638
17639 /* If we're allowing GNU extensions, check for the extended assembly
17640 syntax. Unfortunately, the `:' tokens need not be separated by
17641 a space in C, and so, for compatibility, we tolerate that here
17642 too. Doing that means that we have to treat the `::' operator as
17643 two `:' tokens. */
17644 if (cp_parser_allow_gnu_extensions_p (parser)
17645 && parser->in_function_body
17646 && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
17647 || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
17648 {
17649 bool inputs_p = false;
17650 bool clobbers_p = false;
17651 bool labels_p = false;
17652
17653 /* The extended syntax was used. */
17654 extended_p = true;
17655
17656 /* Look for outputs. */
17657 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
17658 {
17659 /* Consume the `:'. */
17660 cp_lexer_consume_token (parser->lexer);
17661 /* Parse the output-operands. */
17662 if (cp_lexer_next_token_is_not (parser->lexer,
17663 CPP_COLON)
17664 && cp_lexer_next_token_is_not (parser->lexer,
17665 CPP_SCOPE)
17666 && cp_lexer_next_token_is_not (parser->lexer,
17667 CPP_CLOSE_PAREN)
17668 && !goto_p)
17669 {
17670 outputs = cp_parser_asm_operand_list (parser);
17671 if (outputs == error_mark_node)
17672 invalid_outputs_p = true;
17673 }
17674 }
17675 /* If the next token is `::', there are no outputs, and the
17676 next token is the beginning of the inputs. */
17677 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
17678 /* The inputs are coming next. */
17679 inputs_p = true;
17680
17681 /* Look for inputs. */
17682 if (inputs_p
17683 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
17684 {
17685 /* Consume the `:' or `::'. */
17686 cp_lexer_consume_token (parser->lexer);
17687 /* Parse the output-operands. */
17688 if (cp_lexer_next_token_is_not (parser->lexer,
17689 CPP_COLON)
17690 && cp_lexer_next_token_is_not (parser->lexer,
17691 CPP_SCOPE)
17692 && cp_lexer_next_token_is_not (parser->lexer,
17693 CPP_CLOSE_PAREN))
17694 {
17695 inputs = cp_parser_asm_operand_list (parser);
17696 if (inputs == error_mark_node)
17697 invalid_inputs_p = true;
17698 }
17699 }
17700 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
17701 /* The clobbers are coming next. */
17702 clobbers_p = true;
17703
17704 /* Look for clobbers. */
17705 if (clobbers_p
17706 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
17707 {
17708 clobbers_p = true;
17709 /* Consume the `:' or `::'. */
17710 cp_lexer_consume_token (parser->lexer);
17711 /* Parse the clobbers. */
17712 if (cp_lexer_next_token_is_not (parser->lexer,
17713 CPP_COLON)
17714 && cp_lexer_next_token_is_not (parser->lexer,
17715 CPP_CLOSE_PAREN))
17716 clobbers = cp_parser_asm_clobber_list (parser);
17717 }
17718 else if (goto_p
17719 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
17720 /* The labels are coming next. */
17721 labels_p = true;
17722
17723 /* Look for labels. */
17724 if (labels_p
17725 || (goto_p && cp_lexer_next_token_is (parser->lexer, CPP_COLON)))
17726 {
17727 labels_p = true;
17728 /* Consume the `:' or `::'. */
17729 cp_lexer_consume_token (parser->lexer);
17730 /* Parse the labels. */
17731 labels = cp_parser_asm_label_list (parser);
17732 }
17733
17734 if (goto_p && !labels_p)
17735 missing = clobbers_p ? RT_COLON : RT_COLON_SCOPE;
17736 }
17737 else if (goto_p)
17738 missing = RT_COLON_SCOPE;
17739
17740 /* Look for the closing `)'. */
17741 if (!cp_parser_require (parser, missing ? CPP_COLON : CPP_CLOSE_PAREN,
17742 missing ? missing : RT_CLOSE_PAREN))
17743 cp_parser_skip_to_closing_parenthesis (parser, true, false,
17744 /*consume_paren=*/true);
17745 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
17746
17747 if (!invalid_inputs_p && !invalid_outputs_p)
17748 {
17749 /* Create the ASM_EXPR. */
17750 if (parser->in_function_body)
17751 {
17752 asm_stmt = finish_asm_stmt (volatile_p, string, outputs,
17753 inputs, clobbers, labels);
17754 /* If the extended syntax was not used, mark the ASM_EXPR. */
17755 if (!extended_p)
17756 {
17757 tree temp = asm_stmt;
17758 if (TREE_CODE (temp) == CLEANUP_POINT_EXPR)
17759 temp = TREE_OPERAND (temp, 0);
17760
17761 ASM_INPUT_P (temp) = 1;
17762 }
17763 }
17764 else
17765 symtab->finalize_toplevel_asm (string);
17766 }
17767 }
17768
17769 /* Declarators [gram.dcl.decl] */
17770
17771 /* Parse an init-declarator.
17772
17773 init-declarator:
17774 declarator initializer [opt]
17775
17776 GNU Extension:
17777
17778 init-declarator:
17779 declarator asm-specification [opt] attributes [opt] initializer [opt]
17780
17781 function-definition:
17782 decl-specifier-seq [opt] declarator ctor-initializer [opt]
17783 function-body
17784 decl-specifier-seq [opt] declarator function-try-block
17785
17786 GNU Extension:
17787
17788 function-definition:
17789 __extension__ function-definition
17790
17791 TM Extension:
17792
17793 function-definition:
17794 decl-specifier-seq [opt] declarator function-transaction-block
17795
17796 The DECL_SPECIFIERS apply to this declarator. Returns a
17797 representation of the entity declared. If MEMBER_P is TRUE, then
17798 this declarator appears in a class scope. The new DECL created by
17799 this declarator is returned.
17800
17801 The CHECKS are access checks that should be performed once we know
17802 what entity is being declared (and, therefore, what classes have
17803 befriended it).
17804
17805 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
17806 for a function-definition here as well. If the declarator is a
17807 declarator for a function-definition, *FUNCTION_DEFINITION_P will
17808 be TRUE upon return. By that point, the function-definition will
17809 have been completely parsed.
17810
17811 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
17812 is FALSE.
17813
17814 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
17815 parsed declaration if it is an uninitialized single declarator not followed
17816 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
17817 if present, will not be consumed. If returned, this declarator will be
17818 created with SD_INITIALIZED but will not call cp_finish_decl.
17819
17820 If INIT_LOC is not NULL, and *INIT_LOC is equal to UNKNOWN_LOCATION,
17821 and there is an initializer, the pointed location_t is set to the
17822 location of the '=' or `(', or '{' in C++11 token introducing the
17823 initializer. */
17824
17825 static tree
17826 cp_parser_init_declarator (cp_parser* parser,
17827 cp_decl_specifier_seq *decl_specifiers,
17828 vec<deferred_access_check, va_gc> *checks,
17829 bool function_definition_allowed_p,
17830 bool member_p,
17831 int declares_class_or_enum,
17832 bool* function_definition_p,
17833 tree* maybe_range_for_decl,
17834 location_t* init_loc)
17835 {
17836 cp_token *token = NULL, *asm_spec_start_token = NULL,
17837 *attributes_start_token = NULL;
17838 cp_declarator *declarator;
17839 tree prefix_attributes;
17840 tree attributes = NULL;
17841 tree asm_specification;
17842 tree initializer;
17843 tree decl = NULL_TREE;
17844 tree scope;
17845 int is_initialized;
17846 /* Only valid if IS_INITIALIZED is true. In that case, CPP_EQ if
17847 initialized with "= ..", CPP_OPEN_PAREN if initialized with
17848 "(...)". */
17849 enum cpp_ttype initialization_kind;
17850 bool is_direct_init = false;
17851 bool is_non_constant_init;
17852 int ctor_dtor_or_conv_p;
17853 bool friend_p = cp_parser_friend_p (decl_specifiers);
17854 tree pushed_scope = NULL_TREE;
17855 bool range_for_decl_p = false;
17856 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
17857 location_t tmp_init_loc = UNKNOWN_LOCATION;
17858
17859 /* Gather the attributes that were provided with the
17860 decl-specifiers. */
17861 prefix_attributes = decl_specifiers->attributes;
17862
17863 /* Assume that this is not the declarator for a function
17864 definition. */
17865 if (function_definition_p)
17866 *function_definition_p = false;
17867
17868 /* Default arguments are only permitted for function parameters. */
17869 if (decl_spec_seq_has_spec_p (decl_specifiers, ds_typedef))
17870 parser->default_arg_ok_p = false;
17871
17872 /* Defer access checks while parsing the declarator; we cannot know
17873 what names are accessible until we know what is being
17874 declared. */
17875 resume_deferring_access_checks ();
17876
17877 /* Parse the declarator. */
17878 token = cp_lexer_peek_token (parser->lexer);
17879 declarator
17880 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
17881 &ctor_dtor_or_conv_p,
17882 /*parenthesized_p=*/NULL,
17883 member_p, friend_p);
17884 /* Gather up the deferred checks. */
17885 stop_deferring_access_checks ();
17886
17887 parser->default_arg_ok_p = saved_default_arg_ok_p;
17888
17889 /* If the DECLARATOR was erroneous, there's no need to go
17890 further. */
17891 if (declarator == cp_error_declarator)
17892 return error_mark_node;
17893
17894 /* Check that the number of template-parameter-lists is OK. */
17895 if (!cp_parser_check_declarator_template_parameters (parser, declarator,
17896 token->location))
17897 return error_mark_node;
17898
17899 if (declares_class_or_enum & 2)
17900 cp_parser_check_for_definition_in_return_type (declarator,
17901 decl_specifiers->type,
17902 decl_specifiers->locations[ds_type_spec]);
17903
17904 /* Figure out what scope the entity declared by the DECLARATOR is
17905 located in. `grokdeclarator' sometimes changes the scope, so
17906 we compute it now. */
17907 scope = get_scope_of_declarator (declarator);
17908
17909 /* Perform any lookups in the declared type which were thought to be
17910 dependent, but are not in the scope of the declarator. */
17911 decl_specifiers->type
17912 = maybe_update_decl_type (decl_specifiers->type, scope);
17913
17914 /* If we're allowing GNU extensions, look for an
17915 asm-specification. */
17916 if (cp_parser_allow_gnu_extensions_p (parser))
17917 {
17918 /* Look for an asm-specification. */
17919 asm_spec_start_token = cp_lexer_peek_token (parser->lexer);
17920 asm_specification = cp_parser_asm_specification_opt (parser);
17921 }
17922 else
17923 asm_specification = NULL_TREE;
17924
17925 /* Look for attributes. */
17926 attributes_start_token = cp_lexer_peek_token (parser->lexer);
17927 attributes = cp_parser_attributes_opt (parser);
17928
17929 /* Peek at the next token. */
17930 token = cp_lexer_peek_token (parser->lexer);
17931
17932 bool bogus_implicit_tmpl = false;
17933
17934 if (function_declarator_p (declarator))
17935 {
17936 /* Check to see if the token indicates the start of a
17937 function-definition. */
17938 if (cp_parser_token_starts_function_definition_p (token))
17939 {
17940 if (!function_definition_allowed_p)
17941 {
17942 /* If a function-definition should not appear here, issue an
17943 error message. */
17944 cp_parser_error (parser,
17945 "a function-definition is not allowed here");
17946 return error_mark_node;
17947 }
17948
17949 location_t func_brace_location
17950 = cp_lexer_peek_token (parser->lexer)->location;
17951
17952 /* Neither attributes nor an asm-specification are allowed
17953 on a function-definition. */
17954 if (asm_specification)
17955 error_at (asm_spec_start_token->location,
17956 "an asm-specification is not allowed "
17957 "on a function-definition");
17958 if (attributes)
17959 error_at (attributes_start_token->location,
17960 "attributes are not allowed "
17961 "on a function-definition");
17962 /* This is a function-definition. */
17963 *function_definition_p = true;
17964
17965 /* Parse the function definition. */
17966 if (member_p)
17967 decl = cp_parser_save_member_function_body (parser,
17968 decl_specifiers,
17969 declarator,
17970 prefix_attributes);
17971 else
17972 decl =
17973 (cp_parser_function_definition_from_specifiers_and_declarator
17974 (parser, decl_specifiers, prefix_attributes, declarator));
17975
17976 if (decl != error_mark_node && DECL_STRUCT_FUNCTION (decl))
17977 {
17978 /* This is where the prologue starts... */
17979 DECL_STRUCT_FUNCTION (decl)->function_start_locus
17980 = func_brace_location;
17981 }
17982
17983 return decl;
17984 }
17985 }
17986 else if (parser->fully_implicit_function_template_p)
17987 {
17988 /* A non-template declaration involving a function parameter list
17989 containing an implicit template parameter will be made into a
17990 template. If the resulting declaration is not going to be an
17991 actual function then finish the template scope here to prevent it.
17992 An error message will be issued once we have a decl to talk about.
17993
17994 FIXME probably we should do type deduction rather than create an
17995 implicit template, but the standard currently doesn't allow it. */
17996 bogus_implicit_tmpl = true;
17997 finish_fully_implicit_template (parser, NULL_TREE);
17998 }
17999
18000 /* [dcl.dcl]
18001
18002 Only in function declarations for constructors, destructors, and
18003 type conversions can the decl-specifier-seq be omitted.
18004
18005 We explicitly postpone this check past the point where we handle
18006 function-definitions because we tolerate function-definitions
18007 that are missing their return types in some modes. */
18008 if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
18009 {
18010 cp_parser_error (parser,
18011 "expected constructor, destructor, or type conversion");
18012 return error_mark_node;
18013 }
18014
18015 /* An `=' or an `(', or an '{' in C++0x, indicates an initializer. */
18016 if (token->type == CPP_EQ
18017 || token->type == CPP_OPEN_PAREN
18018 || token->type == CPP_OPEN_BRACE)
18019 {
18020 is_initialized = SD_INITIALIZED;
18021 initialization_kind = token->type;
18022 if (maybe_range_for_decl)
18023 *maybe_range_for_decl = error_mark_node;
18024 tmp_init_loc = token->location;
18025 if (init_loc && *init_loc == UNKNOWN_LOCATION)
18026 *init_loc = tmp_init_loc;
18027
18028 if (token->type == CPP_EQ
18029 && function_declarator_p (declarator))
18030 {
18031 cp_token *t2 = cp_lexer_peek_nth_token (parser->lexer, 2);
18032 if (t2->keyword == RID_DEFAULT)
18033 is_initialized = SD_DEFAULTED;
18034 else if (t2->keyword == RID_DELETE)
18035 is_initialized = SD_DELETED;
18036 }
18037 }
18038 else
18039 {
18040 /* If the init-declarator isn't initialized and isn't followed by a
18041 `,' or `;', it's not a valid init-declarator. */
18042 if (token->type != CPP_COMMA
18043 && token->type != CPP_SEMICOLON)
18044 {
18045 if (maybe_range_for_decl && *maybe_range_for_decl != error_mark_node)
18046 range_for_decl_p = true;
18047 else
18048 {
18049 if (!maybe_range_for_decl)
18050 cp_parser_error (parser, "expected initializer");
18051 return error_mark_node;
18052 }
18053 }
18054 is_initialized = SD_UNINITIALIZED;
18055 initialization_kind = CPP_EOF;
18056 }
18057
18058 /* Because start_decl has side-effects, we should only call it if we
18059 know we're going ahead. By this point, we know that we cannot
18060 possibly be looking at any other construct. */
18061 cp_parser_commit_to_tentative_parse (parser);
18062
18063 /* Enter the newly declared entry in the symbol table. If we're
18064 processing a declaration in a class-specifier, we wait until
18065 after processing the initializer. */
18066 if (!member_p)
18067 {
18068 if (parser->in_unbraced_linkage_specification_p)
18069 decl_specifiers->storage_class = sc_extern;
18070 decl = start_decl (declarator, decl_specifiers,
18071 range_for_decl_p? SD_INITIALIZED : is_initialized,
18072 attributes, prefix_attributes, &pushed_scope);
18073 cp_finalize_omp_declare_simd (parser, decl);
18074 cp_finalize_oacc_routine (parser, decl, false);
18075 /* Adjust location of decl if declarator->id_loc is more appropriate:
18076 set, and decl wasn't merged with another decl, in which case its
18077 location would be different from input_location, and more accurate. */
18078 if (DECL_P (decl)
18079 && declarator->id_loc != UNKNOWN_LOCATION
18080 && DECL_SOURCE_LOCATION (decl) == input_location)
18081 DECL_SOURCE_LOCATION (decl) = declarator->id_loc;
18082 }
18083 else if (scope)
18084 /* Enter the SCOPE. That way unqualified names appearing in the
18085 initializer will be looked up in SCOPE. */
18086 pushed_scope = push_scope (scope);
18087
18088 /* Perform deferred access control checks, now that we know in which
18089 SCOPE the declared entity resides. */
18090 if (!member_p && decl)
18091 {
18092 tree saved_current_function_decl = NULL_TREE;
18093
18094 /* If the entity being declared is a function, pretend that we
18095 are in its scope. If it is a `friend', it may have access to
18096 things that would not otherwise be accessible. */
18097 if (TREE_CODE (decl) == FUNCTION_DECL)
18098 {
18099 saved_current_function_decl = current_function_decl;
18100 current_function_decl = decl;
18101 }
18102
18103 /* Perform access checks for template parameters. */
18104 cp_parser_perform_template_parameter_access_checks (checks);
18105
18106 /* Perform the access control checks for the declarator and the
18107 decl-specifiers. */
18108 perform_deferred_access_checks (tf_warning_or_error);
18109
18110 /* Restore the saved value. */
18111 if (TREE_CODE (decl) == FUNCTION_DECL)
18112 current_function_decl = saved_current_function_decl;
18113 }
18114
18115 /* Parse the initializer. */
18116 initializer = NULL_TREE;
18117 is_direct_init = false;
18118 is_non_constant_init = true;
18119 if (is_initialized)
18120 {
18121 if (function_declarator_p (declarator))
18122 {
18123 if (initialization_kind == CPP_EQ)
18124 initializer = cp_parser_pure_specifier (parser);
18125 else
18126 {
18127 /* If the declaration was erroneous, we don't really
18128 know what the user intended, so just silently
18129 consume the initializer. */
18130 if (decl != error_mark_node)
18131 error_at (tmp_init_loc, "initializer provided for function");
18132 cp_parser_skip_to_closing_parenthesis (parser,
18133 /*recovering=*/true,
18134 /*or_comma=*/false,
18135 /*consume_paren=*/true);
18136 }
18137 }
18138 else
18139 {
18140 /* We want to record the extra mangling scope for in-class
18141 initializers of class members and initializers of static data
18142 member templates. The former involves deferring
18143 parsing of the initializer until end of class as with default
18144 arguments. So right here we only handle the latter. */
18145 if (!member_p && processing_template_decl)
18146 start_lambda_scope (decl);
18147 initializer = cp_parser_initializer (parser,
18148 &is_direct_init,
18149 &is_non_constant_init);
18150 if (!member_p && processing_template_decl)
18151 finish_lambda_scope ();
18152 if (initializer == error_mark_node)
18153 cp_parser_skip_to_end_of_statement (parser);
18154 }
18155 }
18156
18157 /* The old parser allows attributes to appear after a parenthesized
18158 initializer. Mark Mitchell proposed removing this functionality
18159 on the GCC mailing lists on 2002-08-13. This parser accepts the
18160 attributes -- but ignores them. */
18161 if (cp_parser_allow_gnu_extensions_p (parser)
18162 && initialization_kind == CPP_OPEN_PAREN)
18163 if (cp_parser_attributes_opt (parser))
18164 warning (OPT_Wattributes,
18165 "attributes after parenthesized initializer ignored");
18166
18167 /* And now complain about a non-function implicit template. */
18168 if (bogus_implicit_tmpl && decl != error_mark_node)
18169 error_at (DECL_SOURCE_LOCATION (decl),
18170 "non-function %qD declared as implicit template", decl);
18171
18172 /* For an in-class declaration, use `grokfield' to create the
18173 declaration. */
18174 if (member_p)
18175 {
18176 if (pushed_scope)
18177 {
18178 pop_scope (pushed_scope);
18179 pushed_scope = NULL_TREE;
18180 }
18181 decl = grokfield (declarator, decl_specifiers,
18182 initializer, !is_non_constant_init,
18183 /*asmspec=*/NULL_TREE,
18184 chainon (attributes, prefix_attributes));
18185 if (decl && TREE_CODE (decl) == FUNCTION_DECL)
18186 cp_parser_save_default_args (parser, decl);
18187 cp_finalize_omp_declare_simd (parser, decl);
18188 cp_finalize_oacc_routine (parser, decl, false);
18189 }
18190
18191 /* Finish processing the declaration. But, skip member
18192 declarations. */
18193 if (!member_p && decl && decl != error_mark_node && !range_for_decl_p)
18194 {
18195 cp_finish_decl (decl,
18196 initializer, !is_non_constant_init,
18197 asm_specification,
18198 /* If the initializer is in parentheses, then this is
18199 a direct-initialization, which means that an
18200 `explicit' constructor is OK. Otherwise, an
18201 `explicit' constructor cannot be used. */
18202 ((is_direct_init || !is_initialized)
18203 ? LOOKUP_NORMAL : LOOKUP_IMPLICIT));
18204 }
18205 else if ((cxx_dialect != cxx98) && friend_p
18206 && decl && TREE_CODE (decl) == FUNCTION_DECL)
18207 /* Core issue #226 (C++0x only): A default template-argument
18208 shall not be specified in a friend class template
18209 declaration. */
18210 check_default_tmpl_args (decl, current_template_parms, /*is_primary=*/true,
18211 /*is_partial=*/false, /*is_friend_decl=*/1);
18212
18213 if (!friend_p && pushed_scope)
18214 pop_scope (pushed_scope);
18215
18216 if (function_declarator_p (declarator)
18217 && parser->fully_implicit_function_template_p)
18218 {
18219 if (member_p)
18220 decl = finish_fully_implicit_template (parser, decl);
18221 else
18222 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
18223 }
18224
18225 return decl;
18226 }
18227
18228 /* Parse a declarator.
18229
18230 declarator:
18231 direct-declarator
18232 ptr-operator declarator
18233
18234 abstract-declarator:
18235 ptr-operator abstract-declarator [opt]
18236 direct-abstract-declarator
18237
18238 GNU Extensions:
18239
18240 declarator:
18241 attributes [opt] direct-declarator
18242 attributes [opt] ptr-operator declarator
18243
18244 abstract-declarator:
18245 attributes [opt] ptr-operator abstract-declarator [opt]
18246 attributes [opt] direct-abstract-declarator
18247
18248 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
18249 detect constructor, destructor or conversion operators. It is set
18250 to -1 if the declarator is a name, and +1 if it is a
18251 function. Otherwise it is set to zero. Usually you just want to
18252 test for >0, but internally the negative value is used.
18253
18254 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
18255 a decl-specifier-seq unless it declares a constructor, destructor,
18256 or conversion. It might seem that we could check this condition in
18257 semantic analysis, rather than parsing, but that makes it difficult
18258 to handle something like `f()'. We want to notice that there are
18259 no decl-specifiers, and therefore realize that this is an
18260 expression, not a declaration.)
18261
18262 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
18263 the declarator is a direct-declarator of the form "(...)".
18264
18265 MEMBER_P is true iff this declarator is a member-declarator.
18266
18267 FRIEND_P is true iff this declarator is a friend. */
18268
18269 static cp_declarator *
18270 cp_parser_declarator (cp_parser* parser,
18271 cp_parser_declarator_kind dcl_kind,
18272 int* ctor_dtor_or_conv_p,
18273 bool* parenthesized_p,
18274 bool member_p, bool friend_p)
18275 {
18276 cp_declarator *declarator;
18277 enum tree_code code;
18278 cp_cv_quals cv_quals;
18279 tree class_type;
18280 tree gnu_attributes = NULL_TREE, std_attributes = NULL_TREE;
18281
18282 /* Assume this is not a constructor, destructor, or type-conversion
18283 operator. */
18284 if (ctor_dtor_or_conv_p)
18285 *ctor_dtor_or_conv_p = 0;
18286
18287 if (cp_parser_allow_gnu_extensions_p (parser))
18288 gnu_attributes = cp_parser_gnu_attributes_opt (parser);
18289
18290 /* Check for the ptr-operator production. */
18291 cp_parser_parse_tentatively (parser);
18292 /* Parse the ptr-operator. */
18293 code = cp_parser_ptr_operator (parser,
18294 &class_type,
18295 &cv_quals,
18296 &std_attributes);
18297
18298 /* If that worked, then we have a ptr-operator. */
18299 if (cp_parser_parse_definitely (parser))
18300 {
18301 /* If a ptr-operator was found, then this declarator was not
18302 parenthesized. */
18303 if (parenthesized_p)
18304 *parenthesized_p = true;
18305 /* The dependent declarator is optional if we are parsing an
18306 abstract-declarator. */
18307 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
18308 cp_parser_parse_tentatively (parser);
18309
18310 /* Parse the dependent declarator. */
18311 declarator = cp_parser_declarator (parser, dcl_kind,
18312 /*ctor_dtor_or_conv_p=*/NULL,
18313 /*parenthesized_p=*/NULL,
18314 /*member_p=*/false,
18315 friend_p);
18316
18317 /* If we are parsing an abstract-declarator, we must handle the
18318 case where the dependent declarator is absent. */
18319 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
18320 && !cp_parser_parse_definitely (parser))
18321 declarator = NULL;
18322
18323 declarator = cp_parser_make_indirect_declarator
18324 (code, class_type, cv_quals, declarator, std_attributes);
18325 }
18326 /* Everything else is a direct-declarator. */
18327 else
18328 {
18329 if (parenthesized_p)
18330 *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
18331 CPP_OPEN_PAREN);
18332 declarator = cp_parser_direct_declarator (parser, dcl_kind,
18333 ctor_dtor_or_conv_p,
18334 member_p, friend_p);
18335 }
18336
18337 if (gnu_attributes && declarator && declarator != cp_error_declarator)
18338 declarator->attributes = gnu_attributes;
18339 return declarator;
18340 }
18341
18342 /* Parse a direct-declarator or direct-abstract-declarator.
18343
18344 direct-declarator:
18345 declarator-id
18346 direct-declarator ( parameter-declaration-clause )
18347 cv-qualifier-seq [opt]
18348 ref-qualifier [opt]
18349 exception-specification [opt]
18350 direct-declarator [ constant-expression [opt] ]
18351 ( declarator )
18352
18353 direct-abstract-declarator:
18354 direct-abstract-declarator [opt]
18355 ( parameter-declaration-clause )
18356 cv-qualifier-seq [opt]
18357 ref-qualifier [opt]
18358 exception-specification [opt]
18359 direct-abstract-declarator [opt] [ constant-expression [opt] ]
18360 ( abstract-declarator )
18361
18362 Returns a representation of the declarator. DCL_KIND is
18363 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
18364 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
18365 we are parsing a direct-declarator. It is
18366 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
18367 of ambiguity we prefer an abstract declarator, as per
18368 [dcl.ambig.res]. CTOR_DTOR_OR_CONV_P, MEMBER_P, and FRIEND_P are
18369 as for cp_parser_declarator. */
18370
18371 static cp_declarator *
18372 cp_parser_direct_declarator (cp_parser* parser,
18373 cp_parser_declarator_kind dcl_kind,
18374 int* ctor_dtor_or_conv_p,
18375 bool member_p, bool friend_p)
18376 {
18377 cp_token *token;
18378 cp_declarator *declarator = NULL;
18379 tree scope = NULL_TREE;
18380 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
18381 bool saved_in_declarator_p = parser->in_declarator_p;
18382 bool first = true;
18383 tree pushed_scope = NULL_TREE;
18384
18385 while (true)
18386 {
18387 /* Peek at the next token. */
18388 token = cp_lexer_peek_token (parser->lexer);
18389 if (token->type == CPP_OPEN_PAREN)
18390 {
18391 /* This is either a parameter-declaration-clause, or a
18392 parenthesized declarator. When we know we are parsing a
18393 named declarator, it must be a parenthesized declarator
18394 if FIRST is true. For instance, `(int)' is a
18395 parameter-declaration-clause, with an omitted
18396 direct-abstract-declarator. But `((*))', is a
18397 parenthesized abstract declarator. Finally, when T is a
18398 template parameter `(T)' is a
18399 parameter-declaration-clause, and not a parenthesized
18400 named declarator.
18401
18402 We first try and parse a parameter-declaration-clause,
18403 and then try a nested declarator (if FIRST is true).
18404
18405 It is not an error for it not to be a
18406 parameter-declaration-clause, even when FIRST is
18407 false. Consider,
18408
18409 int i (int);
18410 int i (3);
18411
18412 The first is the declaration of a function while the
18413 second is the definition of a variable, including its
18414 initializer.
18415
18416 Having seen only the parenthesis, we cannot know which of
18417 these two alternatives should be selected. Even more
18418 complex are examples like:
18419
18420 int i (int (a));
18421 int i (int (3));
18422
18423 The former is a function-declaration; the latter is a
18424 variable initialization.
18425
18426 Thus again, we try a parameter-declaration-clause, and if
18427 that fails, we back out and return. */
18428
18429 if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
18430 {
18431 tree params;
18432 bool is_declarator = false;
18433
18434 /* In a member-declarator, the only valid interpretation
18435 of a parenthesis is the start of a
18436 parameter-declaration-clause. (It is invalid to
18437 initialize a static data member with a parenthesized
18438 initializer; only the "=" form of initialization is
18439 permitted.) */
18440 if (!member_p)
18441 cp_parser_parse_tentatively (parser);
18442
18443 /* Consume the `('. */
18444 cp_lexer_consume_token (parser->lexer);
18445 if (first)
18446 {
18447 /* If this is going to be an abstract declarator, we're
18448 in a declarator and we can't have default args. */
18449 parser->default_arg_ok_p = false;
18450 parser->in_declarator_p = true;
18451 }
18452
18453 begin_scope (sk_function_parms, NULL_TREE);
18454
18455 /* Parse the parameter-declaration-clause. */
18456 params = cp_parser_parameter_declaration_clause (parser);
18457
18458 /* Consume the `)'. */
18459 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
18460
18461 /* If all went well, parse the cv-qualifier-seq,
18462 ref-qualifier and the exception-specification. */
18463 if (member_p || cp_parser_parse_definitely (parser))
18464 {
18465 cp_cv_quals cv_quals;
18466 cp_virt_specifiers virt_specifiers;
18467 cp_ref_qualifier ref_qual;
18468 tree exception_specification;
18469 tree late_return;
18470 tree attrs;
18471 bool memfn = (member_p || (pushed_scope
18472 && CLASS_TYPE_P (pushed_scope)));
18473
18474 is_declarator = true;
18475
18476 if (ctor_dtor_or_conv_p)
18477 *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
18478 first = false;
18479
18480 /* Parse the cv-qualifier-seq. */
18481 cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
18482 /* Parse the ref-qualifier. */
18483 ref_qual = cp_parser_ref_qualifier_opt (parser);
18484 /* Parse the tx-qualifier. */
18485 tree tx_qual = cp_parser_tx_qualifier_opt (parser);
18486 /* And the exception-specification. */
18487 exception_specification
18488 = cp_parser_exception_specification_opt (parser);
18489
18490 attrs = cp_parser_std_attribute_spec_seq (parser);
18491
18492 /* In here, we handle cases where attribute is used after
18493 the function declaration. For example:
18494 void func (int x) __attribute__((vector(..))); */
18495 if (flag_cilkplus
18496 && cp_next_tokens_can_be_gnu_attribute_p (parser))
18497 {
18498 cp_parser_parse_tentatively (parser);
18499 tree attr = cp_parser_gnu_attributes_opt (parser);
18500 if (cp_lexer_next_token_is_not (parser->lexer,
18501 CPP_SEMICOLON)
18502 && cp_lexer_next_token_is_not (parser->lexer,
18503 CPP_OPEN_BRACE))
18504 cp_parser_abort_tentative_parse (parser);
18505 else if (!cp_parser_parse_definitely (parser))
18506 ;
18507 else
18508 attrs = chainon (attr, attrs);
18509 }
18510 tree requires_clause = NULL_TREE;
18511 late_return = (cp_parser_late_return_type_opt
18512 (parser, declarator, requires_clause,
18513 memfn ? cv_quals : -1));
18514
18515 /* Parse the virt-specifier-seq. */
18516 virt_specifiers = cp_parser_virt_specifier_seq_opt (parser);
18517
18518 /* Create the function-declarator. */
18519 declarator = make_call_declarator (declarator,
18520 params,
18521 cv_quals,
18522 virt_specifiers,
18523 ref_qual,
18524 tx_qual,
18525 exception_specification,
18526 late_return,
18527 requires_clause);
18528 declarator->std_attributes = attrs;
18529 /* Any subsequent parameter lists are to do with
18530 return type, so are not those of the declared
18531 function. */
18532 parser->default_arg_ok_p = false;
18533 }
18534
18535 /* Remove the function parms from scope. */
18536 pop_bindings_and_leave_scope ();
18537
18538 if (is_declarator)
18539 /* Repeat the main loop. */
18540 continue;
18541 }
18542
18543 /* If this is the first, we can try a parenthesized
18544 declarator. */
18545 if (first)
18546 {
18547 bool saved_in_type_id_in_expr_p;
18548
18549 parser->default_arg_ok_p = saved_default_arg_ok_p;
18550 parser->in_declarator_p = saved_in_declarator_p;
18551
18552 /* Consume the `('. */
18553 cp_lexer_consume_token (parser->lexer);
18554 /* Parse the nested declarator. */
18555 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
18556 parser->in_type_id_in_expr_p = true;
18557 declarator
18558 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
18559 /*parenthesized_p=*/NULL,
18560 member_p, friend_p);
18561 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
18562 first = false;
18563 /* Expect a `)'. */
18564 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
18565 declarator = cp_error_declarator;
18566 if (declarator == cp_error_declarator)
18567 break;
18568
18569 goto handle_declarator;
18570 }
18571 /* Otherwise, we must be done. */
18572 else
18573 break;
18574 }
18575 else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
18576 && token->type == CPP_OPEN_SQUARE
18577 && !cp_next_tokens_can_be_attribute_p (parser))
18578 {
18579 /* Parse an array-declarator. */
18580 tree bounds, attrs;
18581
18582 if (ctor_dtor_or_conv_p)
18583 *ctor_dtor_or_conv_p = 0;
18584
18585 first = false;
18586 parser->default_arg_ok_p = false;
18587 parser->in_declarator_p = true;
18588 /* Consume the `['. */
18589 cp_lexer_consume_token (parser->lexer);
18590 /* Peek at the next token. */
18591 token = cp_lexer_peek_token (parser->lexer);
18592 /* If the next token is `]', then there is no
18593 constant-expression. */
18594 if (token->type != CPP_CLOSE_SQUARE)
18595 {
18596 bool non_constant_p;
18597 bounds
18598 = cp_parser_constant_expression (parser,
18599 /*allow_non_constant=*/true,
18600 &non_constant_p);
18601 if (!non_constant_p)
18602 /* OK */;
18603 else if (error_operand_p (bounds))
18604 /* Already gave an error. */;
18605 else if (!parser->in_function_body
18606 || current_binding_level->kind == sk_function_parms)
18607 {
18608 /* Normally, the array bound must be an integral constant
18609 expression. However, as an extension, we allow VLAs
18610 in function scopes as long as they aren't part of a
18611 parameter declaration. */
18612 cp_parser_error (parser,
18613 "array bound is not an integer constant");
18614 bounds = error_mark_node;
18615 }
18616 else if (processing_template_decl
18617 && !type_dependent_expression_p (bounds))
18618 {
18619 /* Remember this wasn't a constant-expression. */
18620 bounds = build_nop (TREE_TYPE (bounds), bounds);
18621 TREE_SIDE_EFFECTS (bounds) = 1;
18622 }
18623 }
18624 else
18625 bounds = NULL_TREE;
18626 /* Look for the closing `]'. */
18627 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
18628 {
18629 declarator = cp_error_declarator;
18630 break;
18631 }
18632
18633 attrs = cp_parser_std_attribute_spec_seq (parser);
18634 declarator = make_array_declarator (declarator, bounds);
18635 declarator->std_attributes = attrs;
18636 }
18637 else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
18638 {
18639 {
18640 tree qualifying_scope;
18641 tree unqualified_name;
18642 tree attrs;
18643 special_function_kind sfk;
18644 bool abstract_ok;
18645 bool pack_expansion_p = false;
18646 cp_token *declarator_id_start_token;
18647
18648 /* Parse a declarator-id */
18649 abstract_ok = (dcl_kind == CP_PARSER_DECLARATOR_EITHER);
18650 if (abstract_ok)
18651 {
18652 cp_parser_parse_tentatively (parser);
18653
18654 /* If we see an ellipsis, we should be looking at a
18655 parameter pack. */
18656 if (token->type == CPP_ELLIPSIS)
18657 {
18658 /* Consume the `...' */
18659 cp_lexer_consume_token (parser->lexer);
18660
18661 pack_expansion_p = true;
18662 }
18663 }
18664
18665 declarator_id_start_token = cp_lexer_peek_token (parser->lexer);
18666 unqualified_name
18667 = cp_parser_declarator_id (parser, /*optional_p=*/abstract_ok);
18668 qualifying_scope = parser->scope;
18669 if (abstract_ok)
18670 {
18671 bool okay = false;
18672
18673 if (!unqualified_name && pack_expansion_p)
18674 {
18675 /* Check whether an error occurred. */
18676 okay = !cp_parser_error_occurred (parser);
18677
18678 /* We already consumed the ellipsis to mark a
18679 parameter pack, but we have no way to report it,
18680 so abort the tentative parse. We will be exiting
18681 immediately anyway. */
18682 cp_parser_abort_tentative_parse (parser);
18683 }
18684 else
18685 okay = cp_parser_parse_definitely (parser);
18686
18687 if (!okay)
18688 unqualified_name = error_mark_node;
18689 else if (unqualified_name
18690 && (qualifying_scope
18691 || (!identifier_p (unqualified_name))))
18692 {
18693 cp_parser_error (parser, "expected unqualified-id");
18694 unqualified_name = error_mark_node;
18695 }
18696 }
18697
18698 if (!unqualified_name)
18699 return NULL;
18700 if (unqualified_name == error_mark_node)
18701 {
18702 declarator = cp_error_declarator;
18703 pack_expansion_p = false;
18704 declarator->parameter_pack_p = false;
18705 break;
18706 }
18707
18708 attrs = cp_parser_std_attribute_spec_seq (parser);
18709
18710 if (qualifying_scope && at_namespace_scope_p ()
18711 && TREE_CODE (qualifying_scope) == TYPENAME_TYPE)
18712 {
18713 /* In the declaration of a member of a template class
18714 outside of the class itself, the SCOPE will sometimes
18715 be a TYPENAME_TYPE. For example, given:
18716
18717 template <typename T>
18718 int S<T>::R::i = 3;
18719
18720 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
18721 this context, we must resolve S<T>::R to an ordinary
18722 type, rather than a typename type.
18723
18724 The reason we normally avoid resolving TYPENAME_TYPEs
18725 is that a specialization of `S' might render
18726 `S<T>::R' not a type. However, if `S' is
18727 specialized, then this `i' will not be used, so there
18728 is no harm in resolving the types here. */
18729 tree type;
18730
18731 /* Resolve the TYPENAME_TYPE. */
18732 type = resolve_typename_type (qualifying_scope,
18733 /*only_current_p=*/false);
18734 /* If that failed, the declarator is invalid. */
18735 if (TREE_CODE (type) == TYPENAME_TYPE)
18736 {
18737 if (typedef_variant_p (type))
18738 error_at (declarator_id_start_token->location,
18739 "cannot define member of dependent typedef "
18740 "%qT", type);
18741 else
18742 error_at (declarator_id_start_token->location,
18743 "%<%T::%E%> is not a type",
18744 TYPE_CONTEXT (qualifying_scope),
18745 TYPE_IDENTIFIER (qualifying_scope));
18746 }
18747 qualifying_scope = type;
18748 }
18749
18750 sfk = sfk_none;
18751
18752 if (unqualified_name)
18753 {
18754 tree class_type;
18755
18756 if (qualifying_scope
18757 && CLASS_TYPE_P (qualifying_scope))
18758 class_type = qualifying_scope;
18759 else
18760 class_type = current_class_type;
18761
18762 if (TREE_CODE (unqualified_name) == TYPE_DECL)
18763 {
18764 tree name_type = TREE_TYPE (unqualified_name);
18765 if (class_type && same_type_p (name_type, class_type))
18766 {
18767 if (qualifying_scope
18768 && CLASSTYPE_USE_TEMPLATE (name_type))
18769 {
18770 error_at (declarator_id_start_token->location,
18771 "invalid use of constructor as a template");
18772 inform (declarator_id_start_token->location,
18773 "use %<%T::%D%> instead of %<%T::%D%> to "
18774 "name the constructor in a qualified name",
18775 class_type,
18776 DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
18777 class_type, name_type);
18778 declarator = cp_error_declarator;
18779 break;
18780 }
18781 else
18782 unqualified_name = constructor_name (class_type);
18783 }
18784 else
18785 {
18786 /* We do not attempt to print the declarator
18787 here because we do not have enough
18788 information about its original syntactic
18789 form. */
18790 cp_parser_error (parser, "invalid declarator");
18791 declarator = cp_error_declarator;
18792 break;
18793 }
18794 }
18795
18796 if (class_type)
18797 {
18798 if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
18799 sfk = sfk_destructor;
18800 else if (IDENTIFIER_TYPENAME_P (unqualified_name))
18801 sfk = sfk_conversion;
18802 else if (/* There's no way to declare a constructor
18803 for an anonymous type, even if the type
18804 got a name for linkage purposes. */
18805 !TYPE_WAS_ANONYMOUS (class_type)
18806 /* Handle correctly (c++/19200):
18807
18808 struct S {
18809 struct T{};
18810 friend void S(T);
18811 };
18812
18813 and also:
18814
18815 namespace N {
18816 void S();
18817 }
18818
18819 struct S {
18820 friend void N::S();
18821 }; */
18822 && !(friend_p
18823 && class_type != qualifying_scope)
18824 && constructor_name_p (unqualified_name,
18825 class_type))
18826 {
18827 unqualified_name = constructor_name (class_type);
18828 sfk = sfk_constructor;
18829 }
18830 else if (is_overloaded_fn (unqualified_name)
18831 && DECL_CONSTRUCTOR_P (get_first_fn
18832 (unqualified_name)))
18833 sfk = sfk_constructor;
18834
18835 if (ctor_dtor_or_conv_p && sfk != sfk_none)
18836 *ctor_dtor_or_conv_p = -1;
18837 }
18838 }
18839 declarator = make_id_declarator (qualifying_scope,
18840 unqualified_name,
18841 sfk);
18842 declarator->std_attributes = attrs;
18843 declarator->id_loc = token->location;
18844 declarator->parameter_pack_p = pack_expansion_p;
18845
18846 if (pack_expansion_p)
18847 maybe_warn_variadic_templates ();
18848 }
18849
18850 handle_declarator:;
18851 scope = get_scope_of_declarator (declarator);
18852 if (scope)
18853 {
18854 /* Any names that appear after the declarator-id for a
18855 member are looked up in the containing scope. */
18856 if (at_function_scope_p ())
18857 {
18858 /* But declarations with qualified-ids can't appear in a
18859 function. */
18860 cp_parser_error (parser, "qualified-id in declaration");
18861 declarator = cp_error_declarator;
18862 break;
18863 }
18864 pushed_scope = push_scope (scope);
18865 }
18866 parser->in_declarator_p = true;
18867 if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
18868 || (declarator && declarator->kind == cdk_id))
18869 /* Default args are only allowed on function
18870 declarations. */
18871 parser->default_arg_ok_p = saved_default_arg_ok_p;
18872 else
18873 parser->default_arg_ok_p = false;
18874
18875 first = false;
18876 }
18877 /* We're done. */
18878 else
18879 break;
18880 }
18881
18882 /* For an abstract declarator, we might wind up with nothing at this
18883 point. That's an error; the declarator is not optional. */
18884 if (!declarator)
18885 cp_parser_error (parser, "expected declarator");
18886
18887 /* If we entered a scope, we must exit it now. */
18888 if (pushed_scope)
18889 pop_scope (pushed_scope);
18890
18891 parser->default_arg_ok_p = saved_default_arg_ok_p;
18892 parser->in_declarator_p = saved_in_declarator_p;
18893
18894 return declarator;
18895 }
18896
18897 /* Parse a ptr-operator.
18898
18899 ptr-operator:
18900 * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
18901 * cv-qualifier-seq [opt]
18902 &
18903 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
18904 nested-name-specifier * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
18905
18906 GNU Extension:
18907
18908 ptr-operator:
18909 & cv-qualifier-seq [opt]
18910
18911 Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
18912 Returns ADDR_EXPR if a reference was used, or NON_LVALUE_EXPR for
18913 an rvalue reference. In the case of a pointer-to-member, *TYPE is
18914 filled in with the TYPE containing the member. *CV_QUALS is
18915 filled in with the cv-qualifier-seq, or TYPE_UNQUALIFIED, if there
18916 are no cv-qualifiers. Returns ERROR_MARK if an error occurred.
18917 Note that the tree codes returned by this function have nothing
18918 to do with the types of trees that will be eventually be created
18919 to represent the pointer or reference type being parsed. They are
18920 just constants with suggestive names. */
18921 static enum tree_code
18922 cp_parser_ptr_operator (cp_parser* parser,
18923 tree* type,
18924 cp_cv_quals *cv_quals,
18925 tree *attributes)
18926 {
18927 enum tree_code code = ERROR_MARK;
18928 cp_token *token;
18929 tree attrs = NULL_TREE;
18930
18931 /* Assume that it's not a pointer-to-member. */
18932 *type = NULL_TREE;
18933 /* And that there are no cv-qualifiers. */
18934 *cv_quals = TYPE_UNQUALIFIED;
18935
18936 /* Peek at the next token. */
18937 token = cp_lexer_peek_token (parser->lexer);
18938
18939 /* If it's a `*', `&' or `&&' we have a pointer or reference. */
18940 if (token->type == CPP_MULT)
18941 code = INDIRECT_REF;
18942 else if (token->type == CPP_AND)
18943 code = ADDR_EXPR;
18944 else if ((cxx_dialect != cxx98) &&
18945 token->type == CPP_AND_AND) /* C++0x only */
18946 code = NON_LVALUE_EXPR;
18947
18948 if (code != ERROR_MARK)
18949 {
18950 /* Consume the `*', `&' or `&&'. */
18951 cp_lexer_consume_token (parser->lexer);
18952
18953 /* A `*' can be followed by a cv-qualifier-seq, and so can a
18954 `&', if we are allowing GNU extensions. (The only qualifier
18955 that can legally appear after `&' is `restrict', but that is
18956 enforced during semantic analysis. */
18957 if (code == INDIRECT_REF
18958 || cp_parser_allow_gnu_extensions_p (parser))
18959 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
18960
18961 attrs = cp_parser_std_attribute_spec_seq (parser);
18962 if (attributes != NULL)
18963 *attributes = attrs;
18964 }
18965 else
18966 {
18967 /* Try the pointer-to-member case. */
18968 cp_parser_parse_tentatively (parser);
18969 /* Look for the optional `::' operator. */
18970 cp_parser_global_scope_opt (parser,
18971 /*current_scope_valid_p=*/false);
18972 /* Look for the nested-name specifier. */
18973 token = cp_lexer_peek_token (parser->lexer);
18974 cp_parser_nested_name_specifier (parser,
18975 /*typename_keyword_p=*/false,
18976 /*check_dependency_p=*/true,
18977 /*type_p=*/false,
18978 /*is_declaration=*/false);
18979 /* If we found it, and the next token is a `*', then we are
18980 indeed looking at a pointer-to-member operator. */
18981 if (!cp_parser_error_occurred (parser)
18982 && cp_parser_require (parser, CPP_MULT, RT_MULT))
18983 {
18984 /* Indicate that the `*' operator was used. */
18985 code = INDIRECT_REF;
18986
18987 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
18988 error_at (token->location, "%qD is a namespace", parser->scope);
18989 else if (TREE_CODE (parser->scope) == ENUMERAL_TYPE)
18990 error_at (token->location, "cannot form pointer to member of "
18991 "non-class %q#T", parser->scope);
18992 else
18993 {
18994 /* The type of which the member is a member is given by the
18995 current SCOPE. */
18996 *type = parser->scope;
18997 /* The next name will not be qualified. */
18998 parser->scope = NULL_TREE;
18999 parser->qualifying_scope = NULL_TREE;
19000 parser->object_scope = NULL_TREE;
19001 /* Look for optional c++11 attributes. */
19002 attrs = cp_parser_std_attribute_spec_seq (parser);
19003 if (attributes != NULL)
19004 *attributes = attrs;
19005 /* Look for the optional cv-qualifier-seq. */
19006 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
19007 }
19008 }
19009 /* If that didn't work we don't have a ptr-operator. */
19010 if (!cp_parser_parse_definitely (parser))
19011 cp_parser_error (parser, "expected ptr-operator");
19012 }
19013
19014 return code;
19015 }
19016
19017 /* Parse an (optional) cv-qualifier-seq.
19018
19019 cv-qualifier-seq:
19020 cv-qualifier cv-qualifier-seq [opt]
19021
19022 cv-qualifier:
19023 const
19024 volatile
19025
19026 GNU Extension:
19027
19028 cv-qualifier:
19029 __restrict__
19030
19031 Returns a bitmask representing the cv-qualifiers. */
19032
19033 static cp_cv_quals
19034 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
19035 {
19036 cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
19037
19038 while (true)
19039 {
19040 cp_token *token;
19041 cp_cv_quals cv_qualifier;
19042
19043 /* Peek at the next token. */
19044 token = cp_lexer_peek_token (parser->lexer);
19045 /* See if it's a cv-qualifier. */
19046 switch (token->keyword)
19047 {
19048 case RID_CONST:
19049 cv_qualifier = TYPE_QUAL_CONST;
19050 break;
19051
19052 case RID_VOLATILE:
19053 cv_qualifier = TYPE_QUAL_VOLATILE;
19054 break;
19055
19056 case RID_RESTRICT:
19057 cv_qualifier = TYPE_QUAL_RESTRICT;
19058 break;
19059
19060 default:
19061 cv_qualifier = TYPE_UNQUALIFIED;
19062 break;
19063 }
19064
19065 if (!cv_qualifier)
19066 break;
19067
19068 if (cv_quals & cv_qualifier)
19069 {
19070 error_at (token->location, "duplicate cv-qualifier");
19071 cp_lexer_purge_token (parser->lexer);
19072 }
19073 else
19074 {
19075 cp_lexer_consume_token (parser->lexer);
19076 cv_quals |= cv_qualifier;
19077 }
19078 }
19079
19080 return cv_quals;
19081 }
19082
19083 /* Parse an (optional) ref-qualifier
19084
19085 ref-qualifier:
19086 &
19087 &&
19088
19089 Returns cp_ref_qualifier representing ref-qualifier. */
19090
19091 static cp_ref_qualifier
19092 cp_parser_ref_qualifier_opt (cp_parser* parser)
19093 {
19094 cp_ref_qualifier ref_qual = REF_QUAL_NONE;
19095
19096 /* Don't try to parse bitwise '&' as a ref-qualifier (c++/57532). */
19097 if (cxx_dialect < cxx11 && cp_parser_parsing_tentatively (parser))
19098 return ref_qual;
19099
19100 while (true)
19101 {
19102 cp_ref_qualifier curr_ref_qual = REF_QUAL_NONE;
19103 cp_token *token = cp_lexer_peek_token (parser->lexer);
19104
19105 switch (token->type)
19106 {
19107 case CPP_AND:
19108 curr_ref_qual = REF_QUAL_LVALUE;
19109 break;
19110
19111 case CPP_AND_AND:
19112 curr_ref_qual = REF_QUAL_RVALUE;
19113 break;
19114
19115 default:
19116 curr_ref_qual = REF_QUAL_NONE;
19117 break;
19118 }
19119
19120 if (!curr_ref_qual)
19121 break;
19122 else if (ref_qual)
19123 {
19124 error_at (token->location, "multiple ref-qualifiers");
19125 cp_lexer_purge_token (parser->lexer);
19126 }
19127 else
19128 {
19129 ref_qual = curr_ref_qual;
19130 cp_lexer_consume_token (parser->lexer);
19131 }
19132 }
19133
19134 return ref_qual;
19135 }
19136
19137 /* Parse an optional tx-qualifier.
19138
19139 tx-qualifier:
19140 transaction_safe
19141 transaction_safe_dynamic */
19142
19143 static tree
19144 cp_parser_tx_qualifier_opt (cp_parser *parser)
19145 {
19146 cp_token *token = cp_lexer_peek_token (parser->lexer);
19147 if (token->type == CPP_NAME)
19148 {
19149 tree name = token->u.value;
19150 const char *p = IDENTIFIER_POINTER (name);
19151 const int len = strlen ("transaction_safe");
19152 if (!strncmp (p, "transaction_safe", len))
19153 {
19154 p += len;
19155 if (*p == '\0'
19156 || !strcmp (p, "_dynamic"))
19157 {
19158 cp_lexer_consume_token (parser->lexer);
19159 if (!flag_tm)
19160 {
19161 error ("%E requires %<-fgnu-tm%>", name);
19162 return NULL_TREE;
19163 }
19164 else
19165 return name;
19166 }
19167 }
19168 }
19169 return NULL_TREE;
19170 }
19171
19172 /* Parse an (optional) virt-specifier-seq.
19173
19174 virt-specifier-seq:
19175 virt-specifier virt-specifier-seq [opt]
19176
19177 virt-specifier:
19178 override
19179 final
19180
19181 Returns a bitmask representing the virt-specifiers. */
19182
19183 static cp_virt_specifiers
19184 cp_parser_virt_specifier_seq_opt (cp_parser* parser)
19185 {
19186 cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
19187
19188 while (true)
19189 {
19190 cp_token *token;
19191 cp_virt_specifiers virt_specifier;
19192
19193 /* Peek at the next token. */
19194 token = cp_lexer_peek_token (parser->lexer);
19195 /* See if it's a virt-specifier-qualifier. */
19196 if (token->type != CPP_NAME)
19197 break;
19198 if (!strcmp (IDENTIFIER_POINTER(token->u.value), "override"))
19199 {
19200 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS);
19201 virt_specifier = VIRT_SPEC_OVERRIDE;
19202 }
19203 else if (!strcmp (IDENTIFIER_POINTER(token->u.value), "final"))
19204 {
19205 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS);
19206 virt_specifier = VIRT_SPEC_FINAL;
19207 }
19208 else if (!strcmp (IDENTIFIER_POINTER(token->u.value), "__final"))
19209 {
19210 virt_specifier = VIRT_SPEC_FINAL;
19211 }
19212 else
19213 break;
19214
19215 if (virt_specifiers & virt_specifier)
19216 {
19217 error_at (token->location, "duplicate virt-specifier");
19218 cp_lexer_purge_token (parser->lexer);
19219 }
19220 else
19221 {
19222 cp_lexer_consume_token (parser->lexer);
19223 virt_specifiers |= virt_specifier;
19224 }
19225 }
19226 return virt_specifiers;
19227 }
19228
19229 /* Used by handling of trailing-return-types and NSDMI, in which 'this'
19230 is in scope even though it isn't real. */
19231
19232 void
19233 inject_this_parameter (tree ctype, cp_cv_quals quals)
19234 {
19235 tree this_parm;
19236
19237 if (current_class_ptr)
19238 {
19239 /* We don't clear this between NSDMIs. Is it already what we want? */
19240 tree type = TREE_TYPE (TREE_TYPE (current_class_ptr));
19241 if (same_type_ignoring_top_level_qualifiers_p (ctype, type)
19242 && cp_type_quals (type) == quals)
19243 return;
19244 }
19245
19246 this_parm = build_this_parm (ctype, quals);
19247 /* Clear this first to avoid shortcut in cp_build_indirect_ref. */
19248 current_class_ptr = NULL_TREE;
19249 current_class_ref
19250 = cp_build_indirect_ref (this_parm, RO_NULL, tf_warning_or_error);
19251 current_class_ptr = this_parm;
19252 }
19253
19254 /* Return true iff our current scope is a non-static data member
19255 initializer. */
19256
19257 bool
19258 parsing_nsdmi (void)
19259 {
19260 /* We recognize NSDMI context by the context-less 'this' pointer set up
19261 by the function above. */
19262 if (current_class_ptr
19263 && TREE_CODE (current_class_ptr) == PARM_DECL
19264 && DECL_CONTEXT (current_class_ptr) == NULL_TREE)
19265 return true;
19266 return false;
19267 }
19268
19269 /* Parse a late-specified return type, if any. This is not a separate
19270 non-terminal, but part of a function declarator, which looks like
19271
19272 -> trailing-type-specifier-seq abstract-declarator(opt)
19273
19274 Returns the type indicated by the type-id.
19275
19276 In addition to this, parse any queued up omp declare simd
19277 clauses and Cilk Plus SIMD-enabled function's vector attributes.
19278
19279 QUALS is either a bitmask of cv_qualifiers or -1 for a non-member
19280 function. */
19281
19282 static tree
19283 cp_parser_late_return_type_opt (cp_parser* parser, cp_declarator *declarator,
19284 tree& requires_clause, cp_cv_quals quals)
19285 {
19286 cp_token *token;
19287 tree type = NULL_TREE;
19288 bool declare_simd_p = (parser->omp_declare_simd
19289 && declarator
19290 && declarator->kind == cdk_id);
19291
19292 bool cilk_simd_fn_vector_p = (parser->cilk_simd_fn_info
19293 && declarator && declarator->kind == cdk_id);
19294
19295 bool oacc_routine_p = (parser->oacc_routine
19296 && declarator
19297 && declarator->kind == cdk_id);
19298
19299 /* Peek at the next token. */
19300 token = cp_lexer_peek_token (parser->lexer);
19301 /* A late-specified return type is indicated by an initial '->'. */
19302 if (token->type != CPP_DEREF
19303 && token->keyword != RID_REQUIRES
19304 && !(declare_simd_p || cilk_simd_fn_vector_p || oacc_routine_p))
19305 return NULL_TREE;
19306
19307 tree save_ccp = current_class_ptr;
19308 tree save_ccr = current_class_ref;
19309 if (quals >= 0)
19310 {
19311 /* DR 1207: 'this' is in scope in the trailing return type. */
19312 inject_this_parameter (current_class_type, quals);
19313 }
19314
19315 if (token->type == CPP_DEREF)
19316 {
19317 /* Consume the ->. */
19318 cp_lexer_consume_token (parser->lexer);
19319
19320 type = cp_parser_trailing_type_id (parser);
19321 }
19322
19323 /* Function declarations may be followed by a trailing
19324 requires-clause. */
19325 requires_clause = cp_parser_requires_clause_opt (parser);
19326
19327 if (cilk_simd_fn_vector_p)
19328 declarator->std_attributes
19329 = cp_parser_late_parsing_cilk_simd_fn_info (parser,
19330 declarator->std_attributes);
19331 if (declare_simd_p)
19332 declarator->std_attributes
19333 = cp_parser_late_parsing_omp_declare_simd (parser,
19334 declarator->std_attributes);
19335 if (oacc_routine_p)
19336 declarator->std_attributes
19337 = cp_parser_late_parsing_oacc_routine (parser,
19338 declarator->std_attributes);
19339
19340 if (quals >= 0)
19341 {
19342 current_class_ptr = save_ccp;
19343 current_class_ref = save_ccr;
19344 }
19345
19346 return type;
19347 }
19348
19349 /* Parse a declarator-id.
19350
19351 declarator-id:
19352 id-expression
19353 :: [opt] nested-name-specifier [opt] type-name
19354
19355 In the `id-expression' case, the value returned is as for
19356 cp_parser_id_expression if the id-expression was an unqualified-id.
19357 If the id-expression was a qualified-id, then a SCOPE_REF is
19358 returned. The first operand is the scope (either a NAMESPACE_DECL
19359 or TREE_TYPE), but the second is still just a representation of an
19360 unqualified-id. */
19361
19362 static tree
19363 cp_parser_declarator_id (cp_parser* parser, bool optional_p)
19364 {
19365 tree id;
19366 /* The expression must be an id-expression. Assume that qualified
19367 names are the names of types so that:
19368
19369 template <class T>
19370 int S<T>::R::i = 3;
19371
19372 will work; we must treat `S<T>::R' as the name of a type.
19373 Similarly, assume that qualified names are templates, where
19374 required, so that:
19375
19376 template <class T>
19377 int S<T>::R<T>::i = 3;
19378
19379 will work, too. */
19380 id = cp_parser_id_expression (parser,
19381 /*template_keyword_p=*/false,
19382 /*check_dependency_p=*/false,
19383 /*template_p=*/NULL,
19384 /*declarator_p=*/true,
19385 optional_p);
19386 if (id && BASELINK_P (id))
19387 id = BASELINK_FUNCTIONS (id);
19388 return id;
19389 }
19390
19391 /* Parse a type-id.
19392
19393 type-id:
19394 type-specifier-seq abstract-declarator [opt]
19395
19396 Returns the TYPE specified. */
19397
19398 static tree
19399 cp_parser_type_id_1 (cp_parser* parser, bool is_template_arg,
19400 bool is_trailing_return)
19401 {
19402 cp_decl_specifier_seq type_specifier_seq;
19403 cp_declarator *abstract_declarator;
19404
19405 /* Parse the type-specifier-seq. */
19406 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
19407 is_trailing_return,
19408 &type_specifier_seq);
19409 if (type_specifier_seq.type == error_mark_node)
19410 return error_mark_node;
19411
19412 /* There might or might not be an abstract declarator. */
19413 cp_parser_parse_tentatively (parser);
19414 /* Look for the declarator. */
19415 abstract_declarator
19416 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
19417 /*parenthesized_p=*/NULL,
19418 /*member_p=*/false,
19419 /*friend_p=*/false);
19420 /* Check to see if there really was a declarator. */
19421 if (!cp_parser_parse_definitely (parser))
19422 abstract_declarator = NULL;
19423
19424 if (type_specifier_seq.type
19425 /* The concepts TS allows 'auto' as a type-id. */
19426 && !flag_concepts
19427 /* None of the valid uses of 'auto' in C++14 involve the type-id
19428 nonterminal, but it is valid in a trailing-return-type. */
19429 && !(cxx_dialect >= cxx14 && is_trailing_return)
19430 && type_uses_auto (type_specifier_seq.type))
19431 {
19432 /* A type-id with type 'auto' is only ok if the abstract declarator
19433 is a function declarator with a late-specified return type.
19434
19435 A type-id with 'auto' is also valid in a trailing-return-type
19436 in a compound-requirement. */
19437 if (abstract_declarator
19438 && abstract_declarator->kind == cdk_function
19439 && abstract_declarator->u.function.late_return_type)
19440 /* OK */;
19441 else if (parser->in_result_type_constraint_p)
19442 /* OK */;
19443 else
19444 {
19445 error ("invalid use of %<auto%>");
19446 return error_mark_node;
19447 }
19448 }
19449
19450 return groktypename (&type_specifier_seq, abstract_declarator,
19451 is_template_arg);
19452 }
19453
19454 static tree
19455 cp_parser_type_id (cp_parser *parser)
19456 {
19457 return cp_parser_type_id_1 (parser, false, false);
19458 }
19459
19460 static tree
19461 cp_parser_template_type_arg (cp_parser *parser)
19462 {
19463 tree r;
19464 const char *saved_message = parser->type_definition_forbidden_message;
19465 parser->type_definition_forbidden_message
19466 = G_("types may not be defined in template arguments");
19467 r = cp_parser_type_id_1 (parser, true, false);
19468 parser->type_definition_forbidden_message = saved_message;
19469 if (cxx_dialect >= cxx14 && !flag_concepts && type_uses_auto (r))
19470 {
19471 error ("invalid use of %<auto%> in template argument");
19472 r = error_mark_node;
19473 }
19474 return r;
19475 }
19476
19477 static tree
19478 cp_parser_trailing_type_id (cp_parser *parser)
19479 {
19480 return cp_parser_type_id_1 (parser, false, true);
19481 }
19482
19483 /* Parse a type-specifier-seq.
19484
19485 type-specifier-seq:
19486 type-specifier type-specifier-seq [opt]
19487
19488 GNU extension:
19489
19490 type-specifier-seq:
19491 attributes type-specifier-seq [opt]
19492
19493 If IS_DECLARATION is true, we are at the start of a "condition" or
19494 exception-declaration, so we might be followed by a declarator-id.
19495
19496 If IS_TRAILING_RETURN is true, we are in a trailing-return-type,
19497 i.e. we've just seen "->".
19498
19499 Sets *TYPE_SPECIFIER_SEQ to represent the sequence. */
19500
19501 static void
19502 cp_parser_type_specifier_seq (cp_parser* parser,
19503 bool is_declaration,
19504 bool is_trailing_return,
19505 cp_decl_specifier_seq *type_specifier_seq)
19506 {
19507 bool seen_type_specifier = false;
19508 cp_parser_flags flags = CP_PARSER_FLAGS_OPTIONAL;
19509 cp_token *start_token = NULL;
19510
19511 /* Clear the TYPE_SPECIFIER_SEQ. */
19512 clear_decl_specs (type_specifier_seq);
19513
19514 /* In the context of a trailing return type, enum E { } is an
19515 elaborated-type-specifier followed by a function-body, not an
19516 enum-specifier. */
19517 if (is_trailing_return)
19518 flags |= CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS;
19519
19520 /* Parse the type-specifiers and attributes. */
19521 while (true)
19522 {
19523 tree type_specifier;
19524 bool is_cv_qualifier;
19525
19526 /* Check for attributes first. */
19527 if (cp_next_tokens_can_be_attribute_p (parser))
19528 {
19529 type_specifier_seq->attributes =
19530 chainon (type_specifier_seq->attributes,
19531 cp_parser_attributes_opt (parser));
19532 continue;
19533 }
19534
19535 /* record the token of the beginning of the type specifier seq,
19536 for error reporting purposes*/
19537 if (!start_token)
19538 start_token = cp_lexer_peek_token (parser->lexer);
19539
19540 /* Look for the type-specifier. */
19541 type_specifier = cp_parser_type_specifier (parser,
19542 flags,
19543 type_specifier_seq,
19544 /*is_declaration=*/false,
19545 NULL,
19546 &is_cv_qualifier);
19547 if (!type_specifier)
19548 {
19549 /* If the first type-specifier could not be found, this is not a
19550 type-specifier-seq at all. */
19551 if (!seen_type_specifier)
19552 {
19553 /* Set in_declarator_p to avoid skipping to the semicolon. */
19554 int in_decl = parser->in_declarator_p;
19555 parser->in_declarator_p = true;
19556
19557 if (cp_parser_uncommitted_to_tentative_parse_p (parser)
19558 || !cp_parser_parse_and_diagnose_invalid_type_name (parser))
19559 cp_parser_error (parser, "expected type-specifier");
19560
19561 parser->in_declarator_p = in_decl;
19562
19563 type_specifier_seq->type = error_mark_node;
19564 return;
19565 }
19566 /* If subsequent type-specifiers could not be found, the
19567 type-specifier-seq is complete. */
19568 break;
19569 }
19570
19571 seen_type_specifier = true;
19572 /* The standard says that a condition can be:
19573
19574 type-specifier-seq declarator = assignment-expression
19575
19576 However, given:
19577
19578 struct S {};
19579 if (int S = ...)
19580
19581 we should treat the "S" as a declarator, not as a
19582 type-specifier. The standard doesn't say that explicitly for
19583 type-specifier-seq, but it does say that for
19584 decl-specifier-seq in an ordinary declaration. Perhaps it
19585 would be clearer just to allow a decl-specifier-seq here, and
19586 then add a semantic restriction that if any decl-specifiers
19587 that are not type-specifiers appear, the program is invalid. */
19588 if (is_declaration && !is_cv_qualifier)
19589 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
19590 }
19591 }
19592
19593 /* Return whether the function currently being declared has an associated
19594 template parameter list. */
19595
19596 static bool
19597 function_being_declared_is_template_p (cp_parser* parser)
19598 {
19599 if (!current_template_parms || processing_template_parmlist)
19600 return false;
19601
19602 if (parser->implicit_template_scope)
19603 return true;
19604
19605 if (at_class_scope_p ()
19606 && TYPE_BEING_DEFINED (current_class_type))
19607 return parser->num_template_parameter_lists != 0;
19608
19609 return ((int) parser->num_template_parameter_lists > template_class_depth
19610 (current_class_type));
19611 }
19612
19613 /* Parse a parameter-declaration-clause.
19614
19615 parameter-declaration-clause:
19616 parameter-declaration-list [opt] ... [opt]
19617 parameter-declaration-list , ...
19618
19619 Returns a representation for the parameter declarations. A return
19620 value of NULL indicates a parameter-declaration-clause consisting
19621 only of an ellipsis. */
19622
19623 static tree
19624 cp_parser_parameter_declaration_clause (cp_parser* parser)
19625 {
19626 tree parameters;
19627 cp_token *token;
19628 bool ellipsis_p;
19629 bool is_error;
19630
19631 struct cleanup {
19632 cp_parser* parser;
19633 int auto_is_implicit_function_template_parm_p;
19634 ~cleanup() {
19635 parser->auto_is_implicit_function_template_parm_p
19636 = auto_is_implicit_function_template_parm_p;
19637 }
19638 } cleanup = { parser, parser->auto_is_implicit_function_template_parm_p };
19639
19640 (void) cleanup;
19641
19642 if (!processing_specialization
19643 && !processing_template_parmlist
19644 && !processing_explicit_instantiation)
19645 if (!current_function_decl
19646 || (current_class_type && LAMBDA_TYPE_P (current_class_type)))
19647 parser->auto_is_implicit_function_template_parm_p = true;
19648
19649 /* Peek at the next token. */
19650 token = cp_lexer_peek_token (parser->lexer);
19651 /* Check for trivial parameter-declaration-clauses. */
19652 if (token->type == CPP_ELLIPSIS)
19653 {
19654 /* Consume the `...' token. */
19655 cp_lexer_consume_token (parser->lexer);
19656 return NULL_TREE;
19657 }
19658 else if (token->type == CPP_CLOSE_PAREN)
19659 /* There are no parameters. */
19660 {
19661 #ifndef NO_IMPLICIT_EXTERN_C
19662 if (in_system_header_at (input_location)
19663 && current_class_type == NULL
19664 && current_lang_name == lang_name_c)
19665 return NULL_TREE;
19666 else
19667 #endif
19668 return void_list_node;
19669 }
19670 /* Check for `(void)', too, which is a special case. */
19671 else if (token->keyword == RID_VOID
19672 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
19673 == CPP_CLOSE_PAREN))
19674 {
19675 /* Consume the `void' token. */
19676 cp_lexer_consume_token (parser->lexer);
19677 /* There are no parameters. */
19678 return void_list_node;
19679 }
19680
19681 /* Parse the parameter-declaration-list. */
19682 parameters = cp_parser_parameter_declaration_list (parser, &is_error);
19683 /* If a parse error occurred while parsing the
19684 parameter-declaration-list, then the entire
19685 parameter-declaration-clause is erroneous. */
19686 if (is_error)
19687 return NULL;
19688
19689 /* Peek at the next token. */
19690 token = cp_lexer_peek_token (parser->lexer);
19691 /* If it's a `,', the clause should terminate with an ellipsis. */
19692 if (token->type == CPP_COMMA)
19693 {
19694 /* Consume the `,'. */
19695 cp_lexer_consume_token (parser->lexer);
19696 /* Expect an ellipsis. */
19697 ellipsis_p
19698 = (cp_parser_require (parser, CPP_ELLIPSIS, RT_ELLIPSIS) != NULL);
19699 }
19700 /* It might also be `...' if the optional trailing `,' was
19701 omitted. */
19702 else if (token->type == CPP_ELLIPSIS)
19703 {
19704 /* Consume the `...' token. */
19705 cp_lexer_consume_token (parser->lexer);
19706 /* And remember that we saw it. */
19707 ellipsis_p = true;
19708 }
19709 else
19710 ellipsis_p = false;
19711
19712 /* Finish the parameter list. */
19713 if (!ellipsis_p)
19714 parameters = chainon (parameters, void_list_node);
19715
19716 return parameters;
19717 }
19718
19719 /* Parse a parameter-declaration-list.
19720
19721 parameter-declaration-list:
19722 parameter-declaration
19723 parameter-declaration-list , parameter-declaration
19724
19725 Returns a representation of the parameter-declaration-list, as for
19726 cp_parser_parameter_declaration_clause. However, the
19727 `void_list_node' is never appended to the list. Upon return,
19728 *IS_ERROR will be true iff an error occurred. */
19729
19730 static tree
19731 cp_parser_parameter_declaration_list (cp_parser* parser, bool *is_error)
19732 {
19733 tree parameters = NULL_TREE;
19734 tree *tail = &parameters;
19735 bool saved_in_unbraced_linkage_specification_p;
19736 int index = 0;
19737
19738 /* Assume all will go well. */
19739 *is_error = false;
19740 /* The special considerations that apply to a function within an
19741 unbraced linkage specifications do not apply to the parameters
19742 to the function. */
19743 saved_in_unbraced_linkage_specification_p
19744 = parser->in_unbraced_linkage_specification_p;
19745 parser->in_unbraced_linkage_specification_p = false;
19746
19747 /* Look for more parameters. */
19748 while (true)
19749 {
19750 cp_parameter_declarator *parameter;
19751 tree decl = error_mark_node;
19752 bool parenthesized_p = false;
19753 int template_parm_idx = (function_being_declared_is_template_p (parser)?
19754 TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS
19755 (current_template_parms)) : 0);
19756
19757 /* Parse the parameter. */
19758 parameter
19759 = cp_parser_parameter_declaration (parser,
19760 /*template_parm_p=*/false,
19761 &parenthesized_p);
19762
19763 /* We don't know yet if the enclosing context is deprecated, so wait
19764 and warn in grokparms if appropriate. */
19765 deprecated_state = DEPRECATED_SUPPRESS;
19766
19767 if (parameter)
19768 {
19769 /* If a function parameter pack was specified and an implicit template
19770 parameter was introduced during cp_parser_parameter_declaration,
19771 change any implicit parameters introduced into packs. */
19772 if (parser->implicit_template_parms
19773 && parameter->declarator
19774 && parameter->declarator->parameter_pack_p)
19775 {
19776 int latest_template_parm_idx = TREE_VEC_LENGTH
19777 (INNERMOST_TEMPLATE_PARMS (current_template_parms));
19778
19779 if (latest_template_parm_idx != template_parm_idx)
19780 parameter->decl_specifiers.type = convert_generic_types_to_packs
19781 (parameter->decl_specifiers.type,
19782 template_parm_idx, latest_template_parm_idx);
19783 }
19784
19785 decl = grokdeclarator (parameter->declarator,
19786 &parameter->decl_specifiers,
19787 PARM,
19788 parameter->default_argument != NULL_TREE,
19789 &parameter->decl_specifiers.attributes);
19790 }
19791
19792 deprecated_state = DEPRECATED_NORMAL;
19793
19794 /* If a parse error occurred parsing the parameter declaration,
19795 then the entire parameter-declaration-list is erroneous. */
19796 if (decl == error_mark_node)
19797 {
19798 *is_error = true;
19799 parameters = error_mark_node;
19800 break;
19801 }
19802
19803 if (parameter->decl_specifiers.attributes)
19804 cplus_decl_attributes (&decl,
19805 parameter->decl_specifiers.attributes,
19806 0);
19807 if (DECL_NAME (decl))
19808 decl = pushdecl (decl);
19809
19810 if (decl != error_mark_node)
19811 {
19812 retrofit_lang_decl (decl);
19813 DECL_PARM_INDEX (decl) = ++index;
19814 DECL_PARM_LEVEL (decl) = function_parm_depth ();
19815 }
19816
19817 /* Add the new parameter to the list. */
19818 *tail = build_tree_list (parameter->default_argument, decl);
19819 tail = &TREE_CHAIN (*tail);
19820
19821 /* Peek at the next token. */
19822 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
19823 || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
19824 /* These are for Objective-C++ */
19825 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
19826 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
19827 /* The parameter-declaration-list is complete. */
19828 break;
19829 else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
19830 {
19831 cp_token *token;
19832
19833 /* Peek at the next token. */
19834 token = cp_lexer_peek_nth_token (parser->lexer, 2);
19835 /* If it's an ellipsis, then the list is complete. */
19836 if (token->type == CPP_ELLIPSIS)
19837 break;
19838 /* Otherwise, there must be more parameters. Consume the
19839 `,'. */
19840 cp_lexer_consume_token (parser->lexer);
19841 /* When parsing something like:
19842
19843 int i(float f, double d)
19844
19845 we can tell after seeing the declaration for "f" that we
19846 are not looking at an initialization of a variable "i",
19847 but rather at the declaration of a function "i".
19848
19849 Due to the fact that the parsing of template arguments
19850 (as specified to a template-id) requires backtracking we
19851 cannot use this technique when inside a template argument
19852 list. */
19853 if (!parser->in_template_argument_list_p
19854 && !parser->in_type_id_in_expr_p
19855 && cp_parser_uncommitted_to_tentative_parse_p (parser)
19856 /* However, a parameter-declaration of the form
19857 "float(f)" (which is a valid declaration of a
19858 parameter "f") can also be interpreted as an
19859 expression (the conversion of "f" to "float"). */
19860 && !parenthesized_p)
19861 cp_parser_commit_to_tentative_parse (parser);
19862 }
19863 else
19864 {
19865 cp_parser_error (parser, "expected %<,%> or %<...%>");
19866 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
19867 cp_parser_skip_to_closing_parenthesis (parser,
19868 /*recovering=*/true,
19869 /*or_comma=*/false,
19870 /*consume_paren=*/false);
19871 break;
19872 }
19873 }
19874
19875 parser->in_unbraced_linkage_specification_p
19876 = saved_in_unbraced_linkage_specification_p;
19877
19878 /* Reset implicit_template_scope if we are about to leave the function
19879 parameter list that introduced it. Note that for out-of-line member
19880 definitions, there will be one or more class scopes before we get to
19881 the template parameter scope. */
19882
19883 if (cp_binding_level *its = parser->implicit_template_scope)
19884 if (cp_binding_level *maybe_its = current_binding_level->level_chain)
19885 {
19886 while (maybe_its->kind == sk_class)
19887 maybe_its = maybe_its->level_chain;
19888 if (maybe_its == its)
19889 {
19890 parser->implicit_template_parms = 0;
19891 parser->implicit_template_scope = 0;
19892 }
19893 }
19894
19895 return parameters;
19896 }
19897
19898 /* Parse a parameter declaration.
19899
19900 parameter-declaration:
19901 decl-specifier-seq ... [opt] declarator
19902 decl-specifier-seq declarator = assignment-expression
19903 decl-specifier-seq ... [opt] abstract-declarator [opt]
19904 decl-specifier-seq abstract-declarator [opt] = assignment-expression
19905
19906 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
19907 declares a template parameter. (In that case, a non-nested `>'
19908 token encountered during the parsing of the assignment-expression
19909 is not interpreted as a greater-than operator.)
19910
19911 Returns a representation of the parameter, or NULL if an error
19912 occurs. If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
19913 true iff the declarator is of the form "(p)". */
19914
19915 static cp_parameter_declarator *
19916 cp_parser_parameter_declaration (cp_parser *parser,
19917 bool template_parm_p,
19918 bool *parenthesized_p)
19919 {
19920 int declares_class_or_enum;
19921 cp_decl_specifier_seq decl_specifiers;
19922 cp_declarator *declarator;
19923 tree default_argument;
19924 cp_token *token = NULL, *declarator_token_start = NULL;
19925 const char *saved_message;
19926 bool template_parameter_pack_p = false;
19927
19928 /* In a template parameter, `>' is not an operator.
19929
19930 [temp.param]
19931
19932 When parsing a default template-argument for a non-type
19933 template-parameter, the first non-nested `>' is taken as the end
19934 of the template parameter-list rather than a greater-than
19935 operator. */
19936
19937 /* Type definitions may not appear in parameter types. */
19938 saved_message = parser->type_definition_forbidden_message;
19939 parser->type_definition_forbidden_message
19940 = G_("types may not be defined in parameter types");
19941
19942 /* Parse the declaration-specifiers. */
19943 cp_parser_decl_specifier_seq (parser,
19944 CP_PARSER_FLAGS_NONE,
19945 &decl_specifiers,
19946 &declares_class_or_enum);
19947
19948 /* Complain about missing 'typename' or other invalid type names. */
19949 if (!decl_specifiers.any_type_specifiers_p
19950 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
19951 decl_specifiers.type = error_mark_node;
19952
19953 /* If an error occurred, there's no reason to attempt to parse the
19954 rest of the declaration. */
19955 if (cp_parser_error_occurred (parser))
19956 {
19957 parser->type_definition_forbidden_message = saved_message;
19958 return NULL;
19959 }
19960
19961 /* Peek at the next token. */
19962 token = cp_lexer_peek_token (parser->lexer);
19963
19964 /* If the next token is a `)', `,', `=', `>', or `...', then there
19965 is no declarator. However, when variadic templates are enabled,
19966 there may be a declarator following `...'. */
19967 if (token->type == CPP_CLOSE_PAREN
19968 || token->type == CPP_COMMA
19969 || token->type == CPP_EQ
19970 || token->type == CPP_GREATER)
19971 {
19972 declarator = NULL;
19973 if (parenthesized_p)
19974 *parenthesized_p = false;
19975 }
19976 /* Otherwise, there should be a declarator. */
19977 else
19978 {
19979 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
19980 parser->default_arg_ok_p = false;
19981
19982 /* After seeing a decl-specifier-seq, if the next token is not a
19983 "(", there is no possibility that the code is a valid
19984 expression. Therefore, if parsing tentatively, we commit at
19985 this point. */
19986 if (!parser->in_template_argument_list_p
19987 /* In an expression context, having seen:
19988
19989 (int((char ...
19990
19991 we cannot be sure whether we are looking at a
19992 function-type (taking a "char" as a parameter) or a cast
19993 of some object of type "char" to "int". */
19994 && !parser->in_type_id_in_expr_p
19995 && cp_parser_uncommitted_to_tentative_parse_p (parser)
19996 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
19997 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
19998 cp_parser_commit_to_tentative_parse (parser);
19999 /* Parse the declarator. */
20000 declarator_token_start = token;
20001 declarator = cp_parser_declarator (parser,
20002 CP_PARSER_DECLARATOR_EITHER,
20003 /*ctor_dtor_or_conv_p=*/NULL,
20004 parenthesized_p,
20005 /*member_p=*/false,
20006 /*friend_p=*/false);
20007 parser->default_arg_ok_p = saved_default_arg_ok_p;
20008 /* After the declarator, allow more attributes. */
20009 decl_specifiers.attributes
20010 = chainon (decl_specifiers.attributes,
20011 cp_parser_attributes_opt (parser));
20012
20013 /* If the declarator is a template parameter pack, remember that and
20014 clear the flag in the declarator itself so we don't get errors
20015 from grokdeclarator. */
20016 if (template_parm_p && declarator && declarator->parameter_pack_p)
20017 {
20018 declarator->parameter_pack_p = false;
20019 template_parameter_pack_p = true;
20020 }
20021 }
20022
20023 /* If the next token is an ellipsis, and we have not seen a declarator
20024 name, and if either the type of the declarator contains parameter
20025 packs but it is not a TYPE_PACK_EXPANSION or is null (this happens
20026 for, eg, abbreviated integral type names), then we actually have a
20027 parameter pack expansion expression. Otherwise, leave the ellipsis
20028 for a C-style variadic function. */
20029 token = cp_lexer_peek_token (parser->lexer);
20030 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
20031 {
20032 tree type = decl_specifiers.type;
20033
20034 if (type && DECL_P (type))
20035 type = TREE_TYPE (type);
20036
20037 if (((type
20038 && TREE_CODE (type) != TYPE_PACK_EXPANSION
20039 && (template_parm_p || uses_parameter_packs (type)))
20040 || (!type && template_parm_p))
20041 && declarator_can_be_parameter_pack (declarator))
20042 {
20043 /* Consume the `...'. */
20044 cp_lexer_consume_token (parser->lexer);
20045 maybe_warn_variadic_templates ();
20046
20047 /* Build a pack expansion type */
20048 if (template_parm_p)
20049 template_parameter_pack_p = true;
20050 else if (declarator)
20051 declarator->parameter_pack_p = true;
20052 else
20053 decl_specifiers.type = make_pack_expansion (type);
20054 }
20055 }
20056
20057 /* The restriction on defining new types applies only to the type
20058 of the parameter, not to the default argument. */
20059 parser->type_definition_forbidden_message = saved_message;
20060
20061 /* If the next token is `=', then process a default argument. */
20062 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
20063 {
20064 tree type = decl_specifiers.type;
20065 token = cp_lexer_peek_token (parser->lexer);
20066 /* If we are defining a class, then the tokens that make up the
20067 default argument must be saved and processed later. */
20068 if (!template_parm_p && at_class_scope_p ()
20069 && TYPE_BEING_DEFINED (current_class_type)
20070 && !LAMBDA_TYPE_P (current_class_type))
20071 default_argument = cp_parser_cache_defarg (parser, /*nsdmi=*/false);
20072
20073 // A constrained-type-specifier may declare a type template-parameter.
20074 else if (declares_constrained_type_template_parameter (type))
20075 default_argument
20076 = cp_parser_default_type_template_argument (parser);
20077
20078 // A constrained-type-specifier may declare a template-template-parameter.
20079 else if (declares_constrained_template_template_parameter (type))
20080 default_argument
20081 = cp_parser_default_template_template_argument (parser);
20082
20083 /* Outside of a class definition, we can just parse the
20084 assignment-expression. */
20085 else
20086 default_argument
20087 = cp_parser_default_argument (parser, template_parm_p);
20088
20089 if (!parser->default_arg_ok_p)
20090 {
20091 permerror (token->location,
20092 "default arguments are only "
20093 "permitted for function parameters");
20094 }
20095 else if ((declarator && declarator->parameter_pack_p)
20096 || template_parameter_pack_p
20097 || (decl_specifiers.type
20098 && PACK_EXPANSION_P (decl_specifiers.type)))
20099 {
20100 /* Find the name of the parameter pack. */
20101 cp_declarator *id_declarator = declarator;
20102 while (id_declarator && id_declarator->kind != cdk_id)
20103 id_declarator = id_declarator->declarator;
20104
20105 if (id_declarator && id_declarator->kind == cdk_id)
20106 error_at (declarator_token_start->location,
20107 template_parm_p
20108 ? G_("template parameter pack %qD "
20109 "cannot have a default argument")
20110 : G_("parameter pack %qD cannot have "
20111 "a default argument"),
20112 id_declarator->u.id.unqualified_name);
20113 else
20114 error_at (declarator_token_start->location,
20115 template_parm_p
20116 ? G_("template parameter pack cannot have "
20117 "a default argument")
20118 : G_("parameter pack cannot have a "
20119 "default argument"));
20120
20121 default_argument = NULL_TREE;
20122 }
20123 }
20124 else
20125 default_argument = NULL_TREE;
20126
20127 return make_parameter_declarator (&decl_specifiers,
20128 declarator,
20129 default_argument,
20130 template_parameter_pack_p);
20131 }
20132
20133 /* Parse a default argument and return it.
20134
20135 TEMPLATE_PARM_P is true if this is a default argument for a
20136 non-type template parameter. */
20137 static tree
20138 cp_parser_default_argument (cp_parser *parser, bool template_parm_p)
20139 {
20140 tree default_argument = NULL_TREE;
20141 bool saved_greater_than_is_operator_p;
20142 bool saved_local_variables_forbidden_p;
20143 bool non_constant_p, is_direct_init;
20144
20145 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
20146 set correctly. */
20147 saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
20148 parser->greater_than_is_operator_p = !template_parm_p;
20149 /* Local variable names (and the `this' keyword) may not
20150 appear in a default argument. */
20151 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
20152 parser->local_variables_forbidden_p = true;
20153 /* Parse the assignment-expression. */
20154 if (template_parm_p)
20155 push_deferring_access_checks (dk_no_deferred);
20156 tree saved_class_ptr = NULL_TREE;
20157 tree saved_class_ref = NULL_TREE;
20158 /* The "this" pointer is not valid in a default argument. */
20159 if (cfun)
20160 {
20161 saved_class_ptr = current_class_ptr;
20162 cp_function_chain->x_current_class_ptr = NULL_TREE;
20163 saved_class_ref = current_class_ref;
20164 cp_function_chain->x_current_class_ref = NULL_TREE;
20165 }
20166 default_argument
20167 = cp_parser_initializer (parser, &is_direct_init, &non_constant_p);
20168 /* Restore the "this" pointer. */
20169 if (cfun)
20170 {
20171 cp_function_chain->x_current_class_ptr = saved_class_ptr;
20172 cp_function_chain->x_current_class_ref = saved_class_ref;
20173 }
20174 if (BRACE_ENCLOSED_INITIALIZER_P (default_argument))
20175 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
20176 if (template_parm_p)
20177 pop_deferring_access_checks ();
20178 parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
20179 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
20180
20181 return default_argument;
20182 }
20183
20184 /* Parse a function-body.
20185
20186 function-body:
20187 compound_statement */
20188
20189 static void
20190 cp_parser_function_body (cp_parser *parser, bool in_function_try_block)
20191 {
20192 cp_parser_compound_statement (parser, NULL, (in_function_try_block
20193 ? BCS_TRY_BLOCK : BCS_NORMAL),
20194 true);
20195 }
20196
20197 /* Parse a ctor-initializer-opt followed by a function-body. Return
20198 true if a ctor-initializer was present. When IN_FUNCTION_TRY_BLOCK
20199 is true we are parsing a function-try-block. */
20200
20201 static bool
20202 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser,
20203 bool in_function_try_block)
20204 {
20205 tree body, list;
20206 bool ctor_initializer_p;
20207 const bool check_body_p =
20208 DECL_CONSTRUCTOR_P (current_function_decl)
20209 && DECL_DECLARED_CONSTEXPR_P (current_function_decl);
20210 tree last = NULL;
20211
20212 /* Begin the function body. */
20213 body = begin_function_body ();
20214 /* Parse the optional ctor-initializer. */
20215 ctor_initializer_p = cp_parser_ctor_initializer_opt (parser);
20216
20217 /* If we're parsing a constexpr constructor definition, we need
20218 to check that the constructor body is indeed empty. However,
20219 before we get to cp_parser_function_body lot of junk has been
20220 generated, so we can't just check that we have an empty block.
20221 Rather we take a snapshot of the outermost block, and check whether
20222 cp_parser_function_body changed its state. */
20223 if (check_body_p)
20224 {
20225 list = cur_stmt_list;
20226 if (STATEMENT_LIST_TAIL (list))
20227 last = STATEMENT_LIST_TAIL (list)->stmt;
20228 }
20229 /* Parse the function-body. */
20230 cp_parser_function_body (parser, in_function_try_block);
20231 if (check_body_p)
20232 check_constexpr_ctor_body (last, list, /*complain=*/true);
20233 /* Finish the function body. */
20234 finish_function_body (body);
20235
20236 return ctor_initializer_p;
20237 }
20238
20239 /* Parse an initializer.
20240
20241 initializer:
20242 = initializer-clause
20243 ( expression-list )
20244
20245 Returns an expression representing the initializer. If no
20246 initializer is present, NULL_TREE is returned.
20247
20248 *IS_DIRECT_INIT is set to FALSE if the `= initializer-clause'
20249 production is used, and TRUE otherwise. *IS_DIRECT_INIT is
20250 set to TRUE if there is no initializer present. If there is an
20251 initializer, and it is not a constant-expression, *NON_CONSTANT_P
20252 is set to true; otherwise it is set to false. */
20253
20254 static tree
20255 cp_parser_initializer (cp_parser* parser, bool* is_direct_init,
20256 bool* non_constant_p)
20257 {
20258 cp_token *token;
20259 tree init;
20260
20261 /* Peek at the next token. */
20262 token = cp_lexer_peek_token (parser->lexer);
20263
20264 /* Let our caller know whether or not this initializer was
20265 parenthesized. */
20266 *is_direct_init = (token->type != CPP_EQ);
20267 /* Assume that the initializer is constant. */
20268 *non_constant_p = false;
20269
20270 if (token->type == CPP_EQ)
20271 {
20272 /* Consume the `='. */
20273 cp_lexer_consume_token (parser->lexer);
20274 /* Parse the initializer-clause. */
20275 init = cp_parser_initializer_clause (parser, non_constant_p);
20276 }
20277 else if (token->type == CPP_OPEN_PAREN)
20278 {
20279 vec<tree, va_gc> *vec;
20280 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
20281 /*cast_p=*/false,
20282 /*allow_expansion_p=*/true,
20283 non_constant_p);
20284 if (vec == NULL)
20285 return error_mark_node;
20286 init = build_tree_list_vec (vec);
20287 release_tree_vector (vec);
20288 }
20289 else if (token->type == CPP_OPEN_BRACE)
20290 {
20291 cp_lexer_set_source_position (parser->lexer);
20292 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
20293 init = cp_parser_braced_list (parser, non_constant_p);
20294 CONSTRUCTOR_IS_DIRECT_INIT (init) = 1;
20295 }
20296 else
20297 {
20298 /* Anything else is an error. */
20299 cp_parser_error (parser, "expected initializer");
20300 init = error_mark_node;
20301 }
20302
20303 return init;
20304 }
20305
20306 /* Parse an initializer-clause.
20307
20308 initializer-clause:
20309 assignment-expression
20310 braced-init-list
20311
20312 Returns an expression representing the initializer.
20313
20314 If the `assignment-expression' production is used the value
20315 returned is simply a representation for the expression.
20316
20317 Otherwise, calls cp_parser_braced_list. */
20318
20319 static tree
20320 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
20321 {
20322 tree initializer;
20323
20324 /* Assume the expression is constant. */
20325 *non_constant_p = false;
20326
20327 /* If it is not a `{', then we are looking at an
20328 assignment-expression. */
20329 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
20330 {
20331 initializer
20332 = cp_parser_constant_expression (parser,
20333 /*allow_non_constant_p=*/true,
20334 non_constant_p);
20335 }
20336 else
20337 initializer = cp_parser_braced_list (parser, non_constant_p);
20338
20339 return initializer;
20340 }
20341
20342 /* Parse a brace-enclosed initializer list.
20343
20344 braced-init-list:
20345 { initializer-list , [opt] }
20346 { }
20347
20348 Returns a CONSTRUCTOR. The CONSTRUCTOR_ELTS will be
20349 the elements of the initializer-list (or NULL, if the last
20350 production is used). The TREE_TYPE for the CONSTRUCTOR will be
20351 NULL_TREE. There is no way to detect whether or not the optional
20352 trailing `,' was provided. NON_CONSTANT_P is as for
20353 cp_parser_initializer. */
20354
20355 static tree
20356 cp_parser_braced_list (cp_parser* parser, bool* non_constant_p)
20357 {
20358 tree initializer;
20359
20360 /* Consume the `{' token. */
20361 cp_lexer_consume_token (parser->lexer);
20362 /* Create a CONSTRUCTOR to represent the braced-initializer. */
20363 initializer = make_node (CONSTRUCTOR);
20364 /* If it's not a `}', then there is a non-trivial initializer. */
20365 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
20366 {
20367 /* Parse the initializer list. */
20368 CONSTRUCTOR_ELTS (initializer)
20369 = cp_parser_initializer_list (parser, non_constant_p);
20370 /* A trailing `,' token is allowed. */
20371 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
20372 cp_lexer_consume_token (parser->lexer);
20373 }
20374 else
20375 *non_constant_p = false;
20376 /* Now, there should be a trailing `}'. */
20377 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
20378 TREE_TYPE (initializer) = init_list_type_node;
20379 return initializer;
20380 }
20381
20382 /* Consume tokens up to, and including, the next non-nested closing `]'.
20383 Returns true iff we found a closing `]'. */
20384
20385 static bool
20386 cp_parser_skip_to_closing_square_bracket (cp_parser *parser)
20387 {
20388 unsigned square_depth = 0;
20389
20390 while (true)
20391 {
20392 cp_token * token = cp_lexer_peek_token (parser->lexer);
20393
20394 switch (token->type)
20395 {
20396 case CPP_EOF:
20397 case CPP_PRAGMA_EOL:
20398 /* If we've run out of tokens, then there is no closing `]'. */
20399 return false;
20400
20401 case CPP_OPEN_SQUARE:
20402 ++square_depth;
20403 break;
20404
20405 case CPP_CLOSE_SQUARE:
20406 if (!square_depth--)
20407 {
20408 cp_lexer_consume_token (parser->lexer);
20409 return true;
20410 }
20411 break;
20412
20413 default:
20414 break;
20415 }
20416
20417 /* Consume the token. */
20418 cp_lexer_consume_token (parser->lexer);
20419 }
20420 }
20421
20422 /* Return true if we are looking at an array-designator, false otherwise. */
20423
20424 static bool
20425 cp_parser_array_designator_p (cp_parser *parser)
20426 {
20427 /* Consume the `['. */
20428 cp_lexer_consume_token (parser->lexer);
20429
20430 cp_lexer_save_tokens (parser->lexer);
20431
20432 /* Skip tokens until the next token is a closing square bracket.
20433 If we find the closing `]', and the next token is a `=', then
20434 we are looking at an array designator. */
20435 bool array_designator_p
20436 = (cp_parser_skip_to_closing_square_bracket (parser)
20437 && cp_lexer_next_token_is (parser->lexer, CPP_EQ));
20438
20439 /* Roll back the tokens we skipped. */
20440 cp_lexer_rollback_tokens (parser->lexer);
20441
20442 return array_designator_p;
20443 }
20444
20445 /* Parse an initializer-list.
20446
20447 initializer-list:
20448 initializer-clause ... [opt]
20449 initializer-list , initializer-clause ... [opt]
20450
20451 GNU Extension:
20452
20453 initializer-list:
20454 designation initializer-clause ...[opt]
20455 initializer-list , designation initializer-clause ...[opt]
20456
20457 designation:
20458 . identifier =
20459 identifier :
20460 [ constant-expression ] =
20461
20462 Returns a vec of constructor_elt. The VALUE of each elt is an expression
20463 for the initializer. If the INDEX of the elt is non-NULL, it is the
20464 IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is
20465 as for cp_parser_initializer. */
20466
20467 static vec<constructor_elt, va_gc> *
20468 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
20469 {
20470 vec<constructor_elt, va_gc> *v = NULL;
20471
20472 /* Assume all of the expressions are constant. */
20473 *non_constant_p = false;
20474
20475 /* Parse the rest of the list. */
20476 while (true)
20477 {
20478 cp_token *token;
20479 tree designator;
20480 tree initializer;
20481 bool clause_non_constant_p;
20482
20483 /* If the next token is an identifier and the following one is a
20484 colon, we are looking at the GNU designated-initializer
20485 syntax. */
20486 if (cp_parser_allow_gnu_extensions_p (parser)
20487 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
20488 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
20489 {
20490 /* Warn the user that they are using an extension. */
20491 pedwarn (input_location, OPT_Wpedantic,
20492 "ISO C++ does not allow designated initializers");
20493 /* Consume the identifier. */
20494 designator = cp_lexer_consume_token (parser->lexer)->u.value;
20495 /* Consume the `:'. */
20496 cp_lexer_consume_token (parser->lexer);
20497 }
20498 /* Also handle the C99 syntax, '. id ='. */
20499 else if (cp_parser_allow_gnu_extensions_p (parser)
20500 && cp_lexer_next_token_is (parser->lexer, CPP_DOT)
20501 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME
20502 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_EQ)
20503 {
20504 /* Warn the user that they are using an extension. */
20505 pedwarn (input_location, OPT_Wpedantic,
20506 "ISO C++ does not allow C99 designated initializers");
20507 /* Consume the `.'. */
20508 cp_lexer_consume_token (parser->lexer);
20509 /* Consume the identifier. */
20510 designator = cp_lexer_consume_token (parser->lexer)->u.value;
20511 /* Consume the `='. */
20512 cp_lexer_consume_token (parser->lexer);
20513 }
20514 /* Also handle C99 array designators, '[ const ] ='. */
20515 else if (cp_parser_allow_gnu_extensions_p (parser)
20516 && !c_dialect_objc ()
20517 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
20518 {
20519 /* In C++11, [ could start a lambda-introducer. */
20520 bool non_const = false;
20521
20522 cp_parser_parse_tentatively (parser);
20523
20524 if (!cp_parser_array_designator_p (parser))
20525 {
20526 cp_parser_simulate_error (parser);
20527 designator = NULL_TREE;
20528 }
20529 else
20530 {
20531 designator = cp_parser_constant_expression (parser, true,
20532 &non_const);
20533 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
20534 cp_parser_require (parser, CPP_EQ, RT_EQ);
20535 }
20536
20537 if (!cp_parser_parse_definitely (parser))
20538 designator = NULL_TREE;
20539 else if (non_const)
20540 require_potential_rvalue_constant_expression (designator);
20541 }
20542 else
20543 designator = NULL_TREE;
20544
20545 /* Parse the initializer. */
20546 initializer = cp_parser_initializer_clause (parser,
20547 &clause_non_constant_p);
20548 /* If any clause is non-constant, so is the entire initializer. */
20549 if (clause_non_constant_p)
20550 *non_constant_p = true;
20551
20552 /* If we have an ellipsis, this is an initializer pack
20553 expansion. */
20554 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
20555 {
20556 /* Consume the `...'. */
20557 cp_lexer_consume_token (parser->lexer);
20558
20559 /* Turn the initializer into an initializer expansion. */
20560 initializer = make_pack_expansion (initializer);
20561 }
20562
20563 /* Add it to the vector. */
20564 CONSTRUCTOR_APPEND_ELT (v, designator, initializer);
20565
20566 /* If the next token is not a comma, we have reached the end of
20567 the list. */
20568 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
20569 break;
20570
20571 /* Peek at the next token. */
20572 token = cp_lexer_peek_nth_token (parser->lexer, 2);
20573 /* If the next token is a `}', then we're still done. An
20574 initializer-clause can have a trailing `,' after the
20575 initializer-list and before the closing `}'. */
20576 if (token->type == CPP_CLOSE_BRACE)
20577 break;
20578
20579 /* Consume the `,' token. */
20580 cp_lexer_consume_token (parser->lexer);
20581 }
20582
20583 return v;
20584 }
20585
20586 /* Classes [gram.class] */
20587
20588 /* Parse a class-name.
20589
20590 class-name:
20591 identifier
20592 template-id
20593
20594 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
20595 to indicate that names looked up in dependent types should be
20596 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
20597 keyword has been used to indicate that the name that appears next
20598 is a template. TAG_TYPE indicates the explicit tag given before
20599 the type name, if any. If CHECK_DEPENDENCY_P is FALSE, names are
20600 looked up in dependent scopes. If CLASS_HEAD_P is TRUE, this class
20601 is the class being defined in a class-head. If ENUM_OK is TRUE,
20602 enum-names are also accepted.
20603
20604 Returns the TYPE_DECL representing the class. */
20605
20606 static tree
20607 cp_parser_class_name (cp_parser *parser,
20608 bool typename_keyword_p,
20609 bool template_keyword_p,
20610 enum tag_types tag_type,
20611 bool check_dependency_p,
20612 bool class_head_p,
20613 bool is_declaration,
20614 bool enum_ok)
20615 {
20616 tree decl;
20617 tree scope;
20618 bool typename_p;
20619 cp_token *token;
20620 tree identifier = NULL_TREE;
20621
20622 /* All class-names start with an identifier. */
20623 token = cp_lexer_peek_token (parser->lexer);
20624 if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
20625 {
20626 cp_parser_error (parser, "expected class-name");
20627 return error_mark_node;
20628 }
20629
20630 /* PARSER->SCOPE can be cleared when parsing the template-arguments
20631 to a template-id, so we save it here. */
20632 scope = parser->scope;
20633 if (scope == error_mark_node)
20634 return error_mark_node;
20635
20636 /* Any name names a type if we're following the `typename' keyword
20637 in a qualified name where the enclosing scope is type-dependent. */
20638 typename_p = (typename_keyword_p && scope && TYPE_P (scope)
20639 && dependent_type_p (scope));
20640 /* Handle the common case (an identifier, but not a template-id)
20641 efficiently. */
20642 if (token->type == CPP_NAME
20643 && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
20644 {
20645 cp_token *identifier_token;
20646 bool ambiguous_p;
20647
20648 /* Look for the identifier. */
20649 identifier_token = cp_lexer_peek_token (parser->lexer);
20650 ambiguous_p = identifier_token->error_reported;
20651 identifier = cp_parser_identifier (parser);
20652 /* If the next token isn't an identifier, we are certainly not
20653 looking at a class-name. */
20654 if (identifier == error_mark_node)
20655 decl = error_mark_node;
20656 /* If we know this is a type-name, there's no need to look it
20657 up. */
20658 else if (typename_p)
20659 decl = identifier;
20660 else
20661 {
20662 tree ambiguous_decls;
20663 /* If we already know that this lookup is ambiguous, then
20664 we've already issued an error message; there's no reason
20665 to check again. */
20666 if (ambiguous_p)
20667 {
20668 cp_parser_simulate_error (parser);
20669 return error_mark_node;
20670 }
20671 /* If the next token is a `::', then the name must be a type
20672 name.
20673
20674 [basic.lookup.qual]
20675
20676 During the lookup for a name preceding the :: scope
20677 resolution operator, object, function, and enumerator
20678 names are ignored. */
20679 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
20680 tag_type = typename_type;
20681 /* Look up the name. */
20682 decl = cp_parser_lookup_name (parser, identifier,
20683 tag_type,
20684 /*is_template=*/false,
20685 /*is_namespace=*/false,
20686 check_dependency_p,
20687 &ambiguous_decls,
20688 identifier_token->location);
20689 if (ambiguous_decls)
20690 {
20691 if (cp_parser_parsing_tentatively (parser))
20692 cp_parser_simulate_error (parser);
20693 return error_mark_node;
20694 }
20695 }
20696 }
20697 else
20698 {
20699 /* Try a template-id. */
20700 decl = cp_parser_template_id (parser, template_keyword_p,
20701 check_dependency_p,
20702 tag_type,
20703 is_declaration);
20704 if (decl == error_mark_node)
20705 return error_mark_node;
20706 }
20707
20708 decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
20709
20710 /* If this is a typename, create a TYPENAME_TYPE. */
20711 if (typename_p && decl != error_mark_node)
20712 {
20713 decl = make_typename_type (scope, decl, typename_type,
20714 /*complain=*/tf_error);
20715 if (decl != error_mark_node)
20716 decl = TYPE_NAME (decl);
20717 }
20718
20719 decl = strip_using_decl (decl);
20720
20721 /* Check to see that it is really the name of a class. */
20722 if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
20723 && identifier_p (TREE_OPERAND (decl, 0))
20724 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
20725 /* Situations like this:
20726
20727 template <typename T> struct A {
20728 typename T::template X<int>::I i;
20729 };
20730
20731 are problematic. Is `T::template X<int>' a class-name? The
20732 standard does not seem to be definitive, but there is no other
20733 valid interpretation of the following `::'. Therefore, those
20734 names are considered class-names. */
20735 {
20736 decl = make_typename_type (scope, decl, tag_type, tf_error);
20737 if (decl != error_mark_node)
20738 decl = TYPE_NAME (decl);
20739 }
20740 else if (TREE_CODE (decl) != TYPE_DECL
20741 || TREE_TYPE (decl) == error_mark_node
20742 || !(MAYBE_CLASS_TYPE_P (TREE_TYPE (decl))
20743 || (enum_ok && TREE_CODE (TREE_TYPE (decl)) == ENUMERAL_TYPE))
20744 /* In Objective-C 2.0, a classname followed by '.' starts a
20745 dot-syntax expression, and it's not a type-name. */
20746 || (c_dialect_objc ()
20747 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT
20748 && objc_is_class_name (decl)))
20749 decl = error_mark_node;
20750
20751 if (decl == error_mark_node)
20752 cp_parser_error (parser, "expected class-name");
20753 else if (identifier && !parser->scope)
20754 maybe_note_name_used_in_class (identifier, decl);
20755
20756 return decl;
20757 }
20758
20759 /* Parse a class-specifier.
20760
20761 class-specifier:
20762 class-head { member-specification [opt] }
20763
20764 Returns the TREE_TYPE representing the class. */
20765
20766 static tree
20767 cp_parser_class_specifier_1 (cp_parser* parser)
20768 {
20769 tree type;
20770 tree attributes = NULL_TREE;
20771 bool nested_name_specifier_p;
20772 unsigned saved_num_template_parameter_lists;
20773 bool saved_in_function_body;
20774 unsigned char in_statement;
20775 bool in_switch_statement_p;
20776 bool saved_in_unbraced_linkage_specification_p;
20777 tree old_scope = NULL_TREE;
20778 tree scope = NULL_TREE;
20779 cp_token *closing_brace;
20780
20781 push_deferring_access_checks (dk_no_deferred);
20782
20783 /* Parse the class-head. */
20784 type = cp_parser_class_head (parser,
20785 &nested_name_specifier_p);
20786 /* If the class-head was a semantic disaster, skip the entire body
20787 of the class. */
20788 if (!type)
20789 {
20790 cp_parser_skip_to_end_of_block_or_statement (parser);
20791 pop_deferring_access_checks ();
20792 return error_mark_node;
20793 }
20794
20795 /* Look for the `{'. */
20796 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
20797 {
20798 pop_deferring_access_checks ();
20799 return error_mark_node;
20800 }
20801
20802 cp_ensure_no_omp_declare_simd (parser);
20803 cp_ensure_no_oacc_routine (parser);
20804
20805 /* Issue an error message if type-definitions are forbidden here. */
20806 cp_parser_check_type_definition (parser);
20807 /* Remember that we are defining one more class. */
20808 ++parser->num_classes_being_defined;
20809 /* Inside the class, surrounding template-parameter-lists do not
20810 apply. */
20811 saved_num_template_parameter_lists
20812 = parser->num_template_parameter_lists;
20813 parser->num_template_parameter_lists = 0;
20814 /* We are not in a function body. */
20815 saved_in_function_body = parser->in_function_body;
20816 parser->in_function_body = false;
20817 /* Or in a loop. */
20818 in_statement = parser->in_statement;
20819 parser->in_statement = 0;
20820 /* Or in a switch. */
20821 in_switch_statement_p = parser->in_switch_statement_p;
20822 parser->in_switch_statement_p = false;
20823 /* We are not immediately inside an extern "lang" block. */
20824 saved_in_unbraced_linkage_specification_p
20825 = parser->in_unbraced_linkage_specification_p;
20826 parser->in_unbraced_linkage_specification_p = false;
20827
20828 // Associate constraints with the type.
20829 if (flag_concepts)
20830 type = associate_classtype_constraints (type);
20831
20832 /* Start the class. */
20833 if (nested_name_specifier_p)
20834 {
20835 scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
20836 old_scope = push_inner_scope (scope);
20837 }
20838 type = begin_class_definition (type);
20839
20840 if (type == error_mark_node)
20841 /* If the type is erroneous, skip the entire body of the class. */
20842 cp_parser_skip_to_closing_brace (parser);
20843 else
20844 /* Parse the member-specification. */
20845 cp_parser_member_specification_opt (parser);
20846
20847 /* Look for the trailing `}'. */
20848 closing_brace = cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
20849 /* Look for trailing attributes to apply to this class. */
20850 if (cp_parser_allow_gnu_extensions_p (parser))
20851 attributes = cp_parser_gnu_attributes_opt (parser);
20852 if (type != error_mark_node)
20853 type = finish_struct (type, attributes);
20854 if (nested_name_specifier_p)
20855 pop_inner_scope (old_scope, scope);
20856
20857 /* We've finished a type definition. Check for the common syntax
20858 error of forgetting a semicolon after the definition. We need to
20859 be careful, as we can't just check for not-a-semicolon and be done
20860 with it; the user might have typed:
20861
20862 class X { } c = ...;
20863 class X { } *p = ...;
20864
20865 and so forth. Instead, enumerate all the possible tokens that
20866 might follow this production; if we don't see one of them, then
20867 complain and silently insert the semicolon. */
20868 {
20869 cp_token *token = cp_lexer_peek_token (parser->lexer);
20870 bool want_semicolon = true;
20871
20872 if (cp_next_tokens_can_be_std_attribute_p (parser))
20873 /* Don't try to parse c++11 attributes here. As per the
20874 grammar, that should be a task for
20875 cp_parser_decl_specifier_seq. */
20876 want_semicolon = false;
20877
20878 switch (token->type)
20879 {
20880 case CPP_NAME:
20881 case CPP_SEMICOLON:
20882 case CPP_MULT:
20883 case CPP_AND:
20884 case CPP_OPEN_PAREN:
20885 case CPP_CLOSE_PAREN:
20886 case CPP_COMMA:
20887 want_semicolon = false;
20888 break;
20889
20890 /* While it's legal for type qualifiers and storage class
20891 specifiers to follow type definitions in the grammar, only
20892 compiler testsuites contain code like that. Assume that if
20893 we see such code, then what we're really seeing is a case
20894 like:
20895
20896 class X { }
20897 const <type> var = ...;
20898
20899 or
20900
20901 class Y { }
20902 static <type> func (...) ...
20903
20904 i.e. the qualifier or specifier applies to the next
20905 declaration. To do so, however, we need to look ahead one
20906 more token to see if *that* token is a type specifier.
20907
20908 This code could be improved to handle:
20909
20910 class Z { }
20911 static const <type> var = ...; */
20912 case CPP_KEYWORD:
20913 if (keyword_is_decl_specifier (token->keyword))
20914 {
20915 cp_token *lookahead = cp_lexer_peek_nth_token (parser->lexer, 2);
20916
20917 /* Handling user-defined types here would be nice, but very
20918 tricky. */
20919 want_semicolon
20920 = (lookahead->type == CPP_KEYWORD
20921 && keyword_begins_type_specifier (lookahead->keyword));
20922 }
20923 break;
20924 default:
20925 break;
20926 }
20927
20928 /* If we don't have a type, then something is very wrong and we
20929 shouldn't try to do anything clever. Likewise for not seeing the
20930 closing brace. */
20931 if (closing_brace && TYPE_P (type) && want_semicolon)
20932 {
20933 cp_token_position prev
20934 = cp_lexer_previous_token_position (parser->lexer);
20935 cp_token *prev_token = cp_lexer_token_at (parser->lexer, prev);
20936 location_t loc = prev_token->location;
20937
20938 if (CLASSTYPE_DECLARED_CLASS (type))
20939 error_at (loc, "expected %<;%> after class definition");
20940 else if (TREE_CODE (type) == RECORD_TYPE)
20941 error_at (loc, "expected %<;%> after struct definition");
20942 else if (TREE_CODE (type) == UNION_TYPE)
20943 error_at (loc, "expected %<;%> after union definition");
20944 else
20945 gcc_unreachable ();
20946
20947 /* Unget one token and smash it to look as though we encountered
20948 a semicolon in the input stream. */
20949 cp_lexer_set_token_position (parser->lexer, prev);
20950 token = cp_lexer_peek_token (parser->lexer);
20951 token->type = CPP_SEMICOLON;
20952 token->keyword = RID_MAX;
20953 }
20954 }
20955
20956 /* If this class is not itself within the scope of another class,
20957 then we need to parse the bodies of all of the queued function
20958 definitions. Note that the queued functions defined in a class
20959 are not always processed immediately following the
20960 class-specifier for that class. Consider:
20961
20962 struct A {
20963 struct B { void f() { sizeof (A); } };
20964 };
20965
20966 If `f' were processed before the processing of `A' were
20967 completed, there would be no way to compute the size of `A'.
20968 Note that the nesting we are interested in here is lexical --
20969 not the semantic nesting given by TYPE_CONTEXT. In particular,
20970 for:
20971
20972 struct A { struct B; };
20973 struct A::B { void f() { } };
20974
20975 there is no need to delay the parsing of `A::B::f'. */
20976 if (--parser->num_classes_being_defined == 0)
20977 {
20978 tree decl;
20979 tree class_type = NULL_TREE;
20980 tree pushed_scope = NULL_TREE;
20981 unsigned ix;
20982 cp_default_arg_entry *e;
20983 tree save_ccp, save_ccr;
20984
20985 /* In a first pass, parse default arguments to the functions.
20986 Then, in a second pass, parse the bodies of the functions.
20987 This two-phased approach handles cases like:
20988
20989 struct S {
20990 void f() { g(); }
20991 void g(int i = 3);
20992 };
20993
20994 */
20995 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_default_args, ix, e)
20996 {
20997 decl = e->decl;
20998 /* If there are default arguments that have not yet been processed,
20999 take care of them now. */
21000 if (class_type != e->class_type)
21001 {
21002 if (pushed_scope)
21003 pop_scope (pushed_scope);
21004 class_type = e->class_type;
21005 pushed_scope = push_scope (class_type);
21006 }
21007 /* Make sure that any template parameters are in scope. */
21008 maybe_begin_member_template_processing (decl);
21009 /* Parse the default argument expressions. */
21010 cp_parser_late_parsing_default_args (parser, decl);
21011 /* Remove any template parameters from the symbol table. */
21012 maybe_end_member_template_processing ();
21013 }
21014 vec_safe_truncate (unparsed_funs_with_default_args, 0);
21015 /* Now parse any NSDMIs. */
21016 save_ccp = current_class_ptr;
21017 save_ccr = current_class_ref;
21018 FOR_EACH_VEC_SAFE_ELT (unparsed_nsdmis, ix, decl)
21019 {
21020 if (class_type != DECL_CONTEXT (decl))
21021 {
21022 if (pushed_scope)
21023 pop_scope (pushed_scope);
21024 class_type = DECL_CONTEXT (decl);
21025 pushed_scope = push_scope (class_type);
21026 }
21027 inject_this_parameter (class_type, TYPE_UNQUALIFIED);
21028 cp_parser_late_parsing_nsdmi (parser, decl);
21029 }
21030 vec_safe_truncate (unparsed_nsdmis, 0);
21031 current_class_ptr = save_ccp;
21032 current_class_ref = save_ccr;
21033 if (pushed_scope)
21034 pop_scope (pushed_scope);
21035
21036 /* Now do some post-NSDMI bookkeeping. */
21037 FOR_EACH_VEC_SAFE_ELT (unparsed_classes, ix, class_type)
21038 after_nsdmi_defaulted_late_checks (class_type);
21039 vec_safe_truncate (unparsed_classes, 0);
21040 after_nsdmi_defaulted_late_checks (type);
21041
21042 /* Now parse the body of the functions. */
21043 if (flag_openmp)
21044 {
21045 /* OpenMP UDRs need to be parsed before all other functions. */
21046 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
21047 if (DECL_OMP_DECLARE_REDUCTION_P (decl))
21048 cp_parser_late_parsing_for_member (parser, decl);
21049 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
21050 if (!DECL_OMP_DECLARE_REDUCTION_P (decl))
21051 cp_parser_late_parsing_for_member (parser, decl);
21052 }
21053 else
21054 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
21055 cp_parser_late_parsing_for_member (parser, decl);
21056 vec_safe_truncate (unparsed_funs_with_definitions, 0);
21057 }
21058 else
21059 vec_safe_push (unparsed_classes, type);
21060
21061 /* Put back any saved access checks. */
21062 pop_deferring_access_checks ();
21063
21064 /* Restore saved state. */
21065 parser->in_switch_statement_p = in_switch_statement_p;
21066 parser->in_statement = in_statement;
21067 parser->in_function_body = saved_in_function_body;
21068 parser->num_template_parameter_lists
21069 = saved_num_template_parameter_lists;
21070 parser->in_unbraced_linkage_specification_p
21071 = saved_in_unbraced_linkage_specification_p;
21072
21073 return type;
21074 }
21075
21076 static tree
21077 cp_parser_class_specifier (cp_parser* parser)
21078 {
21079 tree ret;
21080 timevar_push (TV_PARSE_STRUCT);
21081 ret = cp_parser_class_specifier_1 (parser);
21082 timevar_pop (TV_PARSE_STRUCT);
21083 return ret;
21084 }
21085
21086 /* Parse a class-head.
21087
21088 class-head:
21089 class-key identifier [opt] base-clause [opt]
21090 class-key nested-name-specifier identifier class-virt-specifier [opt] base-clause [opt]
21091 class-key nested-name-specifier [opt] template-id
21092 base-clause [opt]
21093
21094 class-virt-specifier:
21095 final
21096
21097 GNU Extensions:
21098 class-key attributes identifier [opt] base-clause [opt]
21099 class-key attributes nested-name-specifier identifier base-clause [opt]
21100 class-key attributes nested-name-specifier [opt] template-id
21101 base-clause [opt]
21102
21103 Upon return BASES is initialized to the list of base classes (or
21104 NULL, if there are none) in the same form returned by
21105 cp_parser_base_clause.
21106
21107 Returns the TYPE of the indicated class. Sets
21108 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
21109 involving a nested-name-specifier was used, and FALSE otherwise.
21110
21111 Returns error_mark_node if this is not a class-head.
21112
21113 Returns NULL_TREE if the class-head is syntactically valid, but
21114 semantically invalid in a way that means we should skip the entire
21115 body of the class. */
21116
21117 static tree
21118 cp_parser_class_head (cp_parser* parser,
21119 bool* nested_name_specifier_p)
21120 {
21121 tree nested_name_specifier;
21122 enum tag_types class_key;
21123 tree id = NULL_TREE;
21124 tree type = NULL_TREE;
21125 tree attributes;
21126 tree bases;
21127 cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
21128 bool template_id_p = false;
21129 bool qualified_p = false;
21130 bool invalid_nested_name_p = false;
21131 bool invalid_explicit_specialization_p = false;
21132 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
21133 tree pushed_scope = NULL_TREE;
21134 unsigned num_templates;
21135 cp_token *type_start_token = NULL, *nested_name_specifier_token_start = NULL;
21136 /* Assume no nested-name-specifier will be present. */
21137 *nested_name_specifier_p = false;
21138 /* Assume no template parameter lists will be used in defining the
21139 type. */
21140 num_templates = 0;
21141 parser->colon_corrects_to_scope_p = false;
21142
21143 /* Look for the class-key. */
21144 class_key = cp_parser_class_key (parser);
21145 if (class_key == none_type)
21146 return error_mark_node;
21147
21148 /* Parse the attributes. */
21149 attributes = cp_parser_attributes_opt (parser);
21150
21151 /* If the next token is `::', that is invalid -- but sometimes
21152 people do try to write:
21153
21154 struct ::S {};
21155
21156 Handle this gracefully by accepting the extra qualifier, and then
21157 issuing an error about it later if this really is a
21158 class-head. If it turns out just to be an elaborated type
21159 specifier, remain silent. */
21160 if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
21161 qualified_p = true;
21162
21163 push_deferring_access_checks (dk_no_check);
21164
21165 /* Determine the name of the class. Begin by looking for an
21166 optional nested-name-specifier. */
21167 nested_name_specifier_token_start = cp_lexer_peek_token (parser->lexer);
21168 nested_name_specifier
21169 = cp_parser_nested_name_specifier_opt (parser,
21170 /*typename_keyword_p=*/false,
21171 /*check_dependency_p=*/false,
21172 /*type_p=*/true,
21173 /*is_declaration=*/false);
21174 /* If there was a nested-name-specifier, then there *must* be an
21175 identifier. */
21176 if (nested_name_specifier)
21177 {
21178 type_start_token = cp_lexer_peek_token (parser->lexer);
21179 /* Although the grammar says `identifier', it really means
21180 `class-name' or `template-name'. You are only allowed to
21181 define a class that has already been declared with this
21182 syntax.
21183
21184 The proposed resolution for Core Issue 180 says that wherever
21185 you see `class T::X' you should treat `X' as a type-name.
21186
21187 It is OK to define an inaccessible class; for example:
21188
21189 class A { class B; };
21190 class A::B {};
21191
21192 We do not know if we will see a class-name, or a
21193 template-name. We look for a class-name first, in case the
21194 class-name is a template-id; if we looked for the
21195 template-name first we would stop after the template-name. */
21196 cp_parser_parse_tentatively (parser);
21197 type = cp_parser_class_name (parser,
21198 /*typename_keyword_p=*/false,
21199 /*template_keyword_p=*/false,
21200 class_type,
21201 /*check_dependency_p=*/false,
21202 /*class_head_p=*/true,
21203 /*is_declaration=*/false);
21204 /* If that didn't work, ignore the nested-name-specifier. */
21205 if (!cp_parser_parse_definitely (parser))
21206 {
21207 invalid_nested_name_p = true;
21208 type_start_token = cp_lexer_peek_token (parser->lexer);
21209 id = cp_parser_identifier (parser);
21210 if (id == error_mark_node)
21211 id = NULL_TREE;
21212 }
21213 /* If we could not find a corresponding TYPE, treat this
21214 declaration like an unqualified declaration. */
21215 if (type == error_mark_node)
21216 nested_name_specifier = NULL_TREE;
21217 /* Otherwise, count the number of templates used in TYPE and its
21218 containing scopes. */
21219 else
21220 {
21221 tree scope;
21222
21223 for (scope = TREE_TYPE (type);
21224 scope && TREE_CODE (scope) != NAMESPACE_DECL;
21225 scope = get_containing_scope (scope))
21226 if (TYPE_P (scope)
21227 && CLASS_TYPE_P (scope)
21228 && CLASSTYPE_TEMPLATE_INFO (scope)
21229 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
21230 && (!CLASSTYPE_TEMPLATE_SPECIALIZATION (scope)
21231 || uses_template_parms (CLASSTYPE_TI_ARGS (scope))))
21232 ++num_templates;
21233 }
21234 }
21235 /* Otherwise, the identifier is optional. */
21236 else
21237 {
21238 /* We don't know whether what comes next is a template-id,
21239 an identifier, or nothing at all. */
21240 cp_parser_parse_tentatively (parser);
21241 /* Check for a template-id. */
21242 type_start_token = cp_lexer_peek_token (parser->lexer);
21243 id = cp_parser_template_id (parser,
21244 /*template_keyword_p=*/false,
21245 /*check_dependency_p=*/true,
21246 class_key,
21247 /*is_declaration=*/true);
21248 /* If that didn't work, it could still be an identifier. */
21249 if (!cp_parser_parse_definitely (parser))
21250 {
21251 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
21252 {
21253 type_start_token = cp_lexer_peek_token (parser->lexer);
21254 id = cp_parser_identifier (parser);
21255 }
21256 else
21257 id = NULL_TREE;
21258 }
21259 else
21260 {
21261 template_id_p = true;
21262 ++num_templates;
21263 }
21264 }
21265
21266 pop_deferring_access_checks ();
21267
21268 if (id)
21269 {
21270 cp_parser_check_for_invalid_template_id (parser, id,
21271 class_key,
21272 type_start_token->location);
21273 }
21274 virt_specifiers = cp_parser_virt_specifier_seq_opt (parser);
21275
21276 /* If it's not a `:' or a `{' then we can't really be looking at a
21277 class-head, since a class-head only appears as part of a
21278 class-specifier. We have to detect this situation before calling
21279 xref_tag, since that has irreversible side-effects. */
21280 if (!cp_parser_next_token_starts_class_definition_p (parser))
21281 {
21282 cp_parser_error (parser, "expected %<{%> or %<:%>");
21283 type = error_mark_node;
21284 goto out;
21285 }
21286
21287 /* At this point, we're going ahead with the class-specifier, even
21288 if some other problem occurs. */
21289 cp_parser_commit_to_tentative_parse (parser);
21290 if (virt_specifiers & VIRT_SPEC_OVERRIDE)
21291 {
21292 cp_parser_error (parser,
21293 "cannot specify %<override%> for a class");
21294 type = error_mark_node;
21295 goto out;
21296 }
21297 /* Issue the error about the overly-qualified name now. */
21298 if (qualified_p)
21299 {
21300 cp_parser_error (parser,
21301 "global qualification of class name is invalid");
21302 type = error_mark_node;
21303 goto out;
21304 }
21305 else if (invalid_nested_name_p)
21306 {
21307 cp_parser_error (parser,
21308 "qualified name does not name a class");
21309 type = error_mark_node;
21310 goto out;
21311 }
21312 else if (nested_name_specifier)
21313 {
21314 tree scope;
21315
21316 /* Reject typedef-names in class heads. */
21317 if (!DECL_IMPLICIT_TYPEDEF_P (type))
21318 {
21319 error_at (type_start_token->location,
21320 "invalid class name in declaration of %qD",
21321 type);
21322 type = NULL_TREE;
21323 goto done;
21324 }
21325
21326 /* Figure out in what scope the declaration is being placed. */
21327 scope = current_scope ();
21328 /* If that scope does not contain the scope in which the
21329 class was originally declared, the program is invalid. */
21330 if (scope && !is_ancestor (scope, nested_name_specifier))
21331 {
21332 if (at_namespace_scope_p ())
21333 error_at (type_start_token->location,
21334 "declaration of %qD in namespace %qD which does not "
21335 "enclose %qD",
21336 type, scope, nested_name_specifier);
21337 else
21338 error_at (type_start_token->location,
21339 "declaration of %qD in %qD which does not enclose %qD",
21340 type, scope, nested_name_specifier);
21341 type = NULL_TREE;
21342 goto done;
21343 }
21344 /* [dcl.meaning]
21345
21346 A declarator-id shall not be qualified except for the
21347 definition of a ... nested class outside of its class
21348 ... [or] the definition or explicit instantiation of a
21349 class member of a namespace outside of its namespace. */
21350 if (scope == nested_name_specifier)
21351 {
21352 permerror (nested_name_specifier_token_start->location,
21353 "extra qualification not allowed");
21354 nested_name_specifier = NULL_TREE;
21355 num_templates = 0;
21356 }
21357 }
21358 /* An explicit-specialization must be preceded by "template <>". If
21359 it is not, try to recover gracefully. */
21360 if (at_namespace_scope_p ()
21361 && parser->num_template_parameter_lists == 0
21362 && template_id_p)
21363 {
21364 error_at (type_start_token->location,
21365 "an explicit specialization must be preceded by %<template <>%>");
21366 invalid_explicit_specialization_p = true;
21367 /* Take the same action that would have been taken by
21368 cp_parser_explicit_specialization. */
21369 ++parser->num_template_parameter_lists;
21370 begin_specialization ();
21371 }
21372 /* There must be no "return" statements between this point and the
21373 end of this function; set "type "to the correct return value and
21374 use "goto done;" to return. */
21375 /* Make sure that the right number of template parameters were
21376 present. */
21377 if (!cp_parser_check_template_parameters (parser, num_templates,
21378 type_start_token->location,
21379 /*declarator=*/NULL))
21380 {
21381 /* If something went wrong, there is no point in even trying to
21382 process the class-definition. */
21383 type = NULL_TREE;
21384 goto done;
21385 }
21386
21387 /* Look up the type. */
21388 if (template_id_p)
21389 {
21390 if (TREE_CODE (id) == TEMPLATE_ID_EXPR
21391 && (DECL_FUNCTION_TEMPLATE_P (TREE_OPERAND (id, 0))
21392 || TREE_CODE (TREE_OPERAND (id, 0)) == OVERLOAD))
21393 {
21394 error_at (type_start_token->location,
21395 "function template %qD redeclared as a class template", id);
21396 type = error_mark_node;
21397 }
21398 else
21399 {
21400 type = TREE_TYPE (id);
21401 type = maybe_process_partial_specialization (type);
21402 }
21403 if (nested_name_specifier)
21404 pushed_scope = push_scope (nested_name_specifier);
21405 }
21406 else if (nested_name_specifier)
21407 {
21408 tree class_type;
21409
21410 /* Given:
21411
21412 template <typename T> struct S { struct T };
21413 template <typename T> struct S<T>::T { };
21414
21415 we will get a TYPENAME_TYPE when processing the definition of
21416 `S::T'. We need to resolve it to the actual type before we
21417 try to define it. */
21418 if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
21419 {
21420 class_type = resolve_typename_type (TREE_TYPE (type),
21421 /*only_current_p=*/false);
21422 if (TREE_CODE (class_type) != TYPENAME_TYPE)
21423 type = TYPE_NAME (class_type);
21424 else
21425 {
21426 cp_parser_error (parser, "could not resolve typename type");
21427 type = error_mark_node;
21428 }
21429 }
21430
21431 if (maybe_process_partial_specialization (TREE_TYPE (type))
21432 == error_mark_node)
21433 {
21434 type = NULL_TREE;
21435 goto done;
21436 }
21437
21438 class_type = current_class_type;
21439 /* Enter the scope indicated by the nested-name-specifier. */
21440 pushed_scope = push_scope (nested_name_specifier);
21441 /* Get the canonical version of this type. */
21442 type = TYPE_MAIN_DECL (TREE_TYPE (type));
21443 /* Call push_template_decl if it seems like we should be defining a
21444 template either from the template headers or the type we're
21445 defining, so that we diagnose both extra and missing headers. */
21446 if ((PROCESSING_REAL_TEMPLATE_DECL_P ()
21447 || CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (type)))
21448 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
21449 {
21450 type = push_template_decl (type);
21451 if (type == error_mark_node)
21452 {
21453 type = NULL_TREE;
21454 goto done;
21455 }
21456 }
21457
21458 type = TREE_TYPE (type);
21459 *nested_name_specifier_p = true;
21460 }
21461 else /* The name is not a nested name. */
21462 {
21463 /* If the class was unnamed, create a dummy name. */
21464 if (!id)
21465 id = make_anon_name ();
21466 type = xref_tag (class_key, id, /*tag_scope=*/ts_current,
21467 parser->num_template_parameter_lists);
21468 }
21469
21470 /* Indicate whether this class was declared as a `class' or as a
21471 `struct'. */
21472 if (TREE_CODE (type) == RECORD_TYPE)
21473 CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
21474 cp_parser_check_class_key (class_key, type);
21475
21476 /* If this type was already complete, and we see another definition,
21477 that's an error. */
21478 if (type != error_mark_node && COMPLETE_TYPE_P (type))
21479 {
21480 error_at (type_start_token->location, "redefinition of %q#T",
21481 type);
21482 error_at (type_start_token->location, "previous definition of %q+#T",
21483 type);
21484 type = NULL_TREE;
21485 goto done;
21486 }
21487 else if (type == error_mark_node)
21488 type = NULL_TREE;
21489
21490 if (type)
21491 {
21492 /* Apply attributes now, before any use of the class as a template
21493 argument in its base list. */
21494 cplus_decl_attributes (&type, attributes, (int)ATTR_FLAG_TYPE_IN_PLACE);
21495 fixup_attribute_variants (type);
21496 }
21497
21498 /* We will have entered the scope containing the class; the names of
21499 base classes should be looked up in that context. For example:
21500
21501 struct A { struct B {}; struct C; };
21502 struct A::C : B {};
21503
21504 is valid. */
21505
21506 /* Get the list of base-classes, if there is one. */
21507 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
21508 {
21509 /* PR59482: enter the class scope so that base-specifiers are looked
21510 up correctly. */
21511 if (type)
21512 pushclass (type);
21513 bases = cp_parser_base_clause (parser);
21514 /* PR59482: get out of the previously pushed class scope so that the
21515 subsequent pops pop the right thing. */
21516 if (type)
21517 popclass ();
21518 }
21519 else
21520 bases = NULL_TREE;
21521
21522 /* If we're really defining a class, process the base classes.
21523 If they're invalid, fail. */
21524 if (type && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
21525 && !xref_basetypes (type, bases))
21526 type = NULL_TREE;
21527
21528 done:
21529 /* Leave the scope given by the nested-name-specifier. We will
21530 enter the class scope itself while processing the members. */
21531 if (pushed_scope)
21532 pop_scope (pushed_scope);
21533
21534 if (invalid_explicit_specialization_p)
21535 {
21536 end_specialization ();
21537 --parser->num_template_parameter_lists;
21538 }
21539
21540 if (type)
21541 DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
21542 if (type && (virt_specifiers & VIRT_SPEC_FINAL))
21543 CLASSTYPE_FINAL (type) = 1;
21544 out:
21545 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
21546 return type;
21547 }
21548
21549 /* Parse a class-key.
21550
21551 class-key:
21552 class
21553 struct
21554 union
21555
21556 Returns the kind of class-key specified, or none_type to indicate
21557 error. */
21558
21559 static enum tag_types
21560 cp_parser_class_key (cp_parser* parser)
21561 {
21562 cp_token *token;
21563 enum tag_types tag_type;
21564
21565 /* Look for the class-key. */
21566 token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_KEY);
21567 if (!token)
21568 return none_type;
21569
21570 /* Check to see if the TOKEN is a class-key. */
21571 tag_type = cp_parser_token_is_class_key (token);
21572 if (!tag_type)
21573 cp_parser_error (parser, "expected class-key");
21574 return tag_type;
21575 }
21576
21577 /* Parse a type-parameter-key.
21578
21579 type-parameter-key:
21580 class
21581 typename
21582 */
21583
21584 static void
21585 cp_parser_type_parameter_key (cp_parser* parser)
21586 {
21587 /* Look for the type-parameter-key. */
21588 enum tag_types tag_type = none_type;
21589 cp_token *token = cp_lexer_peek_token (parser->lexer);
21590 if ((tag_type = cp_parser_token_is_type_parameter_key (token)) != none_type)
21591 {
21592 cp_lexer_consume_token (parser->lexer);
21593 if (pedantic && tag_type == typename_type && cxx_dialect < cxx1z)
21594 /* typename is not allowed in a template template parameter
21595 by the standard until C++1Z. */
21596 pedwarn (token->location, OPT_Wpedantic,
21597 "ISO C++ forbids typename key in template template parameter;"
21598 " use -std=c++1z or -std=gnu++1z");
21599 }
21600 else
21601 cp_parser_error (parser, "expected %<class%> or %<typename%>");
21602
21603 return;
21604 }
21605
21606 /* Parse an (optional) member-specification.
21607
21608 member-specification:
21609 member-declaration member-specification [opt]
21610 access-specifier : member-specification [opt] */
21611
21612 static void
21613 cp_parser_member_specification_opt (cp_parser* parser)
21614 {
21615 while (true)
21616 {
21617 cp_token *token;
21618 enum rid keyword;
21619
21620 /* Peek at the next token. */
21621 token = cp_lexer_peek_token (parser->lexer);
21622 /* If it's a `}', or EOF then we've seen all the members. */
21623 if (token->type == CPP_CLOSE_BRACE
21624 || token->type == CPP_EOF
21625 || token->type == CPP_PRAGMA_EOL)
21626 break;
21627
21628 /* See if this token is a keyword. */
21629 keyword = token->keyword;
21630 switch (keyword)
21631 {
21632 case RID_PUBLIC:
21633 case RID_PROTECTED:
21634 case RID_PRIVATE:
21635 /* Consume the access-specifier. */
21636 cp_lexer_consume_token (parser->lexer);
21637 /* Remember which access-specifier is active. */
21638 current_access_specifier = token->u.value;
21639 /* Look for the `:'. */
21640 cp_parser_require (parser, CPP_COLON, RT_COLON);
21641 break;
21642
21643 default:
21644 /* Accept #pragmas at class scope. */
21645 if (token->type == CPP_PRAGMA)
21646 {
21647 cp_parser_pragma (parser, pragma_member);
21648 break;
21649 }
21650
21651 /* Otherwise, the next construction must be a
21652 member-declaration. */
21653 cp_parser_member_declaration (parser);
21654 }
21655 }
21656 }
21657
21658 /* Parse a member-declaration.
21659
21660 member-declaration:
21661 decl-specifier-seq [opt] member-declarator-list [opt] ;
21662 function-definition ; [opt]
21663 :: [opt] nested-name-specifier template [opt] unqualified-id ;
21664 using-declaration
21665 template-declaration
21666 alias-declaration
21667
21668 member-declarator-list:
21669 member-declarator
21670 member-declarator-list , member-declarator
21671
21672 member-declarator:
21673 declarator pure-specifier [opt]
21674 declarator constant-initializer [opt]
21675 identifier [opt] : constant-expression
21676
21677 GNU Extensions:
21678
21679 member-declaration:
21680 __extension__ member-declaration
21681
21682 member-declarator:
21683 declarator attributes [opt] pure-specifier [opt]
21684 declarator attributes [opt] constant-initializer [opt]
21685 identifier [opt] attributes [opt] : constant-expression
21686
21687 C++0x Extensions:
21688
21689 member-declaration:
21690 static_assert-declaration */
21691
21692 static void
21693 cp_parser_member_declaration (cp_parser* parser)
21694 {
21695 cp_decl_specifier_seq decl_specifiers;
21696 tree prefix_attributes;
21697 tree decl;
21698 int declares_class_or_enum;
21699 bool friend_p;
21700 cp_token *token = NULL;
21701 cp_token *decl_spec_token_start = NULL;
21702 cp_token *initializer_token_start = NULL;
21703 int saved_pedantic;
21704 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
21705
21706 /* Check for the `__extension__' keyword. */
21707 if (cp_parser_extension_opt (parser, &saved_pedantic))
21708 {
21709 /* Recurse. */
21710 cp_parser_member_declaration (parser);
21711 /* Restore the old value of the PEDANTIC flag. */
21712 pedantic = saved_pedantic;
21713
21714 return;
21715 }
21716
21717 /* Check for a template-declaration. */
21718 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
21719 {
21720 /* An explicit specialization here is an error condition, and we
21721 expect the specialization handler to detect and report this. */
21722 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
21723 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
21724 cp_parser_explicit_specialization (parser);
21725 else
21726 cp_parser_template_declaration (parser, /*member_p=*/true);
21727
21728 return;
21729 }
21730 /* Check for a template introduction. */
21731 else if (cp_parser_template_declaration_after_export (parser, true))
21732 return;
21733
21734 /* Check for a using-declaration. */
21735 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
21736 {
21737 if (cxx_dialect < cxx11)
21738 {
21739 /* Parse the using-declaration. */
21740 cp_parser_using_declaration (parser,
21741 /*access_declaration_p=*/false);
21742 return;
21743 }
21744 else
21745 {
21746 tree decl;
21747 bool alias_decl_expected;
21748 cp_parser_parse_tentatively (parser);
21749 decl = cp_parser_alias_declaration (parser);
21750 /* Note that if we actually see the '=' token after the
21751 identifier, cp_parser_alias_declaration commits the
21752 tentative parse. In that case, we really expect an
21753 alias-declaration. Otherwise, we expect a using
21754 declaration. */
21755 alias_decl_expected =
21756 !cp_parser_uncommitted_to_tentative_parse_p (parser);
21757 cp_parser_parse_definitely (parser);
21758
21759 if (alias_decl_expected)
21760 finish_member_declaration (decl);
21761 else
21762 cp_parser_using_declaration (parser,
21763 /*access_declaration_p=*/false);
21764 return;
21765 }
21766 }
21767
21768 /* Check for @defs. */
21769 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_DEFS))
21770 {
21771 tree ivar, member;
21772 tree ivar_chains = cp_parser_objc_defs_expression (parser);
21773 ivar = ivar_chains;
21774 while (ivar)
21775 {
21776 member = ivar;
21777 ivar = TREE_CHAIN (member);
21778 TREE_CHAIN (member) = NULL_TREE;
21779 finish_member_declaration (member);
21780 }
21781 return;
21782 }
21783
21784 /* If the next token is `static_assert' we have a static assertion. */
21785 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC_ASSERT))
21786 {
21787 cp_parser_static_assert (parser, /*member_p=*/true);
21788 return;
21789 }
21790
21791 parser->colon_corrects_to_scope_p = false;
21792
21793 if (cp_parser_using_declaration (parser, /*access_declaration=*/true))
21794 goto out;
21795
21796 /* Parse the decl-specifier-seq. */
21797 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
21798 cp_parser_decl_specifier_seq (parser,
21799 CP_PARSER_FLAGS_OPTIONAL,
21800 &decl_specifiers,
21801 &declares_class_or_enum);
21802 /* Check for an invalid type-name. */
21803 if (!decl_specifiers.any_type_specifiers_p
21804 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
21805 goto out;
21806 /* If there is no declarator, then the decl-specifier-seq should
21807 specify a type. */
21808 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
21809 {
21810 /* If there was no decl-specifier-seq, and the next token is a
21811 `;', then we have something like:
21812
21813 struct S { ; };
21814
21815 [class.mem]
21816
21817 Each member-declaration shall declare at least one member
21818 name of the class. */
21819 if (!decl_specifiers.any_specifiers_p)
21820 {
21821 cp_token *token = cp_lexer_peek_token (parser->lexer);
21822 if (!in_system_header_at (token->location))
21823 pedwarn (token->location, OPT_Wpedantic, "extra %<;%>");
21824 }
21825 else
21826 {
21827 tree type;
21828
21829 /* See if this declaration is a friend. */
21830 friend_p = cp_parser_friend_p (&decl_specifiers);
21831 /* If there were decl-specifiers, check to see if there was
21832 a class-declaration. */
21833 type = check_tag_decl (&decl_specifiers,
21834 /*explicit_type_instantiation_p=*/false);
21835 /* Nested classes have already been added to the class, but
21836 a `friend' needs to be explicitly registered. */
21837 if (friend_p)
21838 {
21839 /* If the `friend' keyword was present, the friend must
21840 be introduced with a class-key. */
21841 if (!declares_class_or_enum && cxx_dialect < cxx11)
21842 pedwarn (decl_spec_token_start->location, OPT_Wpedantic,
21843 "in C++03 a class-key must be used "
21844 "when declaring a friend");
21845 /* In this case:
21846
21847 template <typename T> struct A {
21848 friend struct A<T>::B;
21849 };
21850
21851 A<T>::B will be represented by a TYPENAME_TYPE, and
21852 therefore not recognized by check_tag_decl. */
21853 if (!type)
21854 {
21855 type = decl_specifiers.type;
21856 if (type && TREE_CODE (type) == TYPE_DECL)
21857 type = TREE_TYPE (type);
21858 }
21859 if (!type || !TYPE_P (type))
21860 error_at (decl_spec_token_start->location,
21861 "friend declaration does not name a class or "
21862 "function");
21863 else
21864 make_friend_class (current_class_type, type,
21865 /*complain=*/true);
21866 }
21867 /* If there is no TYPE, an error message will already have
21868 been issued. */
21869 else if (!type || type == error_mark_node)
21870 ;
21871 /* An anonymous aggregate has to be handled specially; such
21872 a declaration really declares a data member (with a
21873 particular type), as opposed to a nested class. */
21874 else if (ANON_AGGR_TYPE_P (type))
21875 {
21876 /* C++11 9.5/6. */
21877 if (decl_specifiers.storage_class != sc_none)
21878 error_at (decl_spec_token_start->location,
21879 "a storage class on an anonymous aggregate "
21880 "in class scope is not allowed");
21881
21882 /* Remove constructors and such from TYPE, now that we
21883 know it is an anonymous aggregate. */
21884 fixup_anonymous_aggr (type);
21885 /* And make the corresponding data member. */
21886 decl = build_decl (decl_spec_token_start->location,
21887 FIELD_DECL, NULL_TREE, type);
21888 /* Add it to the class. */
21889 finish_member_declaration (decl);
21890 }
21891 else
21892 cp_parser_check_access_in_redeclaration
21893 (TYPE_NAME (type),
21894 decl_spec_token_start->location);
21895 }
21896 }
21897 else
21898 {
21899 bool assume_semicolon = false;
21900
21901 /* Clear attributes from the decl_specifiers but keep them
21902 around as prefix attributes that apply them to the entity
21903 being declared. */
21904 prefix_attributes = decl_specifiers.attributes;
21905 decl_specifiers.attributes = NULL_TREE;
21906
21907 /* See if these declarations will be friends. */
21908 friend_p = cp_parser_friend_p (&decl_specifiers);
21909
21910 /* Keep going until we hit the `;' at the end of the
21911 declaration. */
21912 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
21913 {
21914 tree attributes = NULL_TREE;
21915 tree first_attribute;
21916
21917 /* Peek at the next token. */
21918 token = cp_lexer_peek_token (parser->lexer);
21919
21920 /* Check for a bitfield declaration. */
21921 if (token->type == CPP_COLON
21922 || (token->type == CPP_NAME
21923 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
21924 == CPP_COLON))
21925 {
21926 tree identifier;
21927 tree width;
21928
21929 /* Get the name of the bitfield. Note that we cannot just
21930 check TOKEN here because it may have been invalidated by
21931 the call to cp_lexer_peek_nth_token above. */
21932 if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
21933 identifier = cp_parser_identifier (parser);
21934 else
21935 identifier = NULL_TREE;
21936
21937 /* Consume the `:' token. */
21938 cp_lexer_consume_token (parser->lexer);
21939 /* Get the width of the bitfield. */
21940 width
21941 = cp_parser_constant_expression (parser);
21942
21943 /* Look for attributes that apply to the bitfield. */
21944 attributes = cp_parser_attributes_opt (parser);
21945 /* Remember which attributes are prefix attributes and
21946 which are not. */
21947 first_attribute = attributes;
21948 /* Combine the attributes. */
21949 attributes = chainon (prefix_attributes, attributes);
21950
21951 /* Create the bitfield declaration. */
21952 decl = grokbitfield (identifier
21953 ? make_id_declarator (NULL_TREE,
21954 identifier,
21955 sfk_none)
21956 : NULL,
21957 &decl_specifiers,
21958 width,
21959 attributes);
21960 }
21961 else
21962 {
21963 cp_declarator *declarator;
21964 tree initializer;
21965 tree asm_specification;
21966 int ctor_dtor_or_conv_p;
21967
21968 /* Parse the declarator. */
21969 declarator
21970 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
21971 &ctor_dtor_or_conv_p,
21972 /*parenthesized_p=*/NULL,
21973 /*member_p=*/true,
21974 friend_p);
21975
21976 /* If something went wrong parsing the declarator, make sure
21977 that we at least consume some tokens. */
21978 if (declarator == cp_error_declarator)
21979 {
21980 /* Skip to the end of the statement. */
21981 cp_parser_skip_to_end_of_statement (parser);
21982 /* If the next token is not a semicolon, that is
21983 probably because we just skipped over the body of
21984 a function. So, we consume a semicolon if
21985 present, but do not issue an error message if it
21986 is not present. */
21987 if (cp_lexer_next_token_is (parser->lexer,
21988 CPP_SEMICOLON))
21989 cp_lexer_consume_token (parser->lexer);
21990 goto out;
21991 }
21992
21993 if (declares_class_or_enum & 2)
21994 cp_parser_check_for_definition_in_return_type
21995 (declarator, decl_specifiers.type,
21996 decl_specifiers.locations[ds_type_spec]);
21997
21998 /* Look for an asm-specification. */
21999 asm_specification = cp_parser_asm_specification_opt (parser);
22000 /* Look for attributes that apply to the declaration. */
22001 attributes = cp_parser_attributes_opt (parser);
22002 /* Remember which attributes are prefix attributes and
22003 which are not. */
22004 first_attribute = attributes;
22005 /* Combine the attributes. */
22006 attributes = chainon (prefix_attributes, attributes);
22007
22008 /* If it's an `=', then we have a constant-initializer or a
22009 pure-specifier. It is not correct to parse the
22010 initializer before registering the member declaration
22011 since the member declaration should be in scope while
22012 its initializer is processed. However, the rest of the
22013 front end does not yet provide an interface that allows
22014 us to handle this correctly. */
22015 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
22016 {
22017 /* In [class.mem]:
22018
22019 A pure-specifier shall be used only in the declaration of
22020 a virtual function.
22021
22022 A member-declarator can contain a constant-initializer
22023 only if it declares a static member of integral or
22024 enumeration type.
22025
22026 Therefore, if the DECLARATOR is for a function, we look
22027 for a pure-specifier; otherwise, we look for a
22028 constant-initializer. When we call `grokfield', it will
22029 perform more stringent semantics checks. */
22030 initializer_token_start = cp_lexer_peek_token (parser->lexer);
22031 if (function_declarator_p (declarator)
22032 || (decl_specifiers.type
22033 && TREE_CODE (decl_specifiers.type) == TYPE_DECL
22034 && declarator->kind == cdk_id
22035 && (TREE_CODE (TREE_TYPE (decl_specifiers.type))
22036 == FUNCTION_TYPE)))
22037 initializer = cp_parser_pure_specifier (parser);
22038 else if (decl_specifiers.storage_class != sc_static)
22039 initializer = cp_parser_save_nsdmi (parser);
22040 else if (cxx_dialect >= cxx11)
22041 {
22042 bool nonconst;
22043 /* Don't require a constant rvalue in C++11, since we
22044 might want a reference constant. We'll enforce
22045 constancy later. */
22046 cp_lexer_consume_token (parser->lexer);
22047 /* Parse the initializer. */
22048 initializer = cp_parser_initializer_clause (parser,
22049 &nonconst);
22050 }
22051 else
22052 /* Parse the initializer. */
22053 initializer = cp_parser_constant_initializer (parser);
22054 }
22055 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
22056 && !function_declarator_p (declarator))
22057 {
22058 bool x;
22059 if (decl_specifiers.storage_class != sc_static)
22060 initializer = cp_parser_save_nsdmi (parser);
22061 else
22062 initializer = cp_parser_initializer (parser, &x, &x);
22063 }
22064 /* Otherwise, there is no initializer. */
22065 else
22066 initializer = NULL_TREE;
22067
22068 /* See if we are probably looking at a function
22069 definition. We are certainly not looking at a
22070 member-declarator. Calling `grokfield' has
22071 side-effects, so we must not do it unless we are sure
22072 that we are looking at a member-declarator. */
22073 if (cp_parser_token_starts_function_definition_p
22074 (cp_lexer_peek_token (parser->lexer)))
22075 {
22076 /* The grammar does not allow a pure-specifier to be
22077 used when a member function is defined. (It is
22078 possible that this fact is an oversight in the
22079 standard, since a pure function may be defined
22080 outside of the class-specifier. */
22081 if (initializer && initializer_token_start)
22082 error_at (initializer_token_start->location,
22083 "pure-specifier on function-definition");
22084 decl = cp_parser_save_member_function_body (parser,
22085 &decl_specifiers,
22086 declarator,
22087 attributes);
22088 if (parser->fully_implicit_function_template_p)
22089 decl = finish_fully_implicit_template (parser, decl);
22090 /* If the member was not a friend, declare it here. */
22091 if (!friend_p)
22092 finish_member_declaration (decl);
22093 /* Peek at the next token. */
22094 token = cp_lexer_peek_token (parser->lexer);
22095 /* If the next token is a semicolon, consume it. */
22096 if (token->type == CPP_SEMICOLON)
22097 cp_lexer_consume_token (parser->lexer);
22098 goto out;
22099 }
22100 else
22101 if (declarator->kind == cdk_function)
22102 declarator->id_loc = token->location;
22103 /* Create the declaration. */
22104 decl = grokfield (declarator, &decl_specifiers,
22105 initializer, /*init_const_expr_p=*/true,
22106 asm_specification, attributes);
22107 if (parser->fully_implicit_function_template_p)
22108 {
22109 if (friend_p)
22110 finish_fully_implicit_template (parser, 0);
22111 else
22112 decl = finish_fully_implicit_template (parser, decl);
22113 }
22114 }
22115
22116 cp_finalize_omp_declare_simd (parser, decl);
22117 cp_finalize_oacc_routine (parser, decl, false);
22118
22119 /* Reset PREFIX_ATTRIBUTES. */
22120 while (attributes && TREE_CHAIN (attributes) != first_attribute)
22121 attributes = TREE_CHAIN (attributes);
22122 if (attributes)
22123 TREE_CHAIN (attributes) = NULL_TREE;
22124
22125 /* If there is any qualification still in effect, clear it
22126 now; we will be starting fresh with the next declarator. */
22127 parser->scope = NULL_TREE;
22128 parser->qualifying_scope = NULL_TREE;
22129 parser->object_scope = NULL_TREE;
22130 /* If it's a `,', then there are more declarators. */
22131 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
22132 {
22133 cp_lexer_consume_token (parser->lexer);
22134 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
22135 {
22136 cp_token *token = cp_lexer_previous_token (parser->lexer);
22137 error_at (token->location,
22138 "stray %<,%> at end of member declaration");
22139 }
22140 }
22141 /* If the next token isn't a `;', then we have a parse error. */
22142 else if (cp_lexer_next_token_is_not (parser->lexer,
22143 CPP_SEMICOLON))
22144 {
22145 /* The next token might be a ways away from where the
22146 actual semicolon is missing. Find the previous token
22147 and use that for our error position. */
22148 cp_token *token = cp_lexer_previous_token (parser->lexer);
22149 error_at (token->location,
22150 "expected %<;%> at end of member declaration");
22151
22152 /* Assume that the user meant to provide a semicolon. If
22153 we were to cp_parser_skip_to_end_of_statement, we might
22154 skip to a semicolon inside a member function definition
22155 and issue nonsensical error messages. */
22156 assume_semicolon = true;
22157 }
22158
22159 if (decl)
22160 {
22161 /* Add DECL to the list of members. */
22162 if (!friend_p
22163 /* Explicitly include, eg, NSDMIs, for better error
22164 recovery (c++/58650). */
22165 || !DECL_DECLARES_FUNCTION_P (decl))
22166 finish_member_declaration (decl);
22167
22168 if (TREE_CODE (decl) == FUNCTION_DECL)
22169 cp_parser_save_default_args (parser, decl);
22170 else if (TREE_CODE (decl) == FIELD_DECL
22171 && !DECL_C_BIT_FIELD (decl)
22172 && DECL_INITIAL (decl))
22173 /* Add DECL to the queue of NSDMI to be parsed later. */
22174 vec_safe_push (unparsed_nsdmis, decl);
22175 }
22176
22177 if (assume_semicolon)
22178 goto out;
22179 }
22180 }
22181
22182 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
22183 out:
22184 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
22185 }
22186
22187 /* Parse a pure-specifier.
22188
22189 pure-specifier:
22190 = 0
22191
22192 Returns INTEGER_ZERO_NODE if a pure specifier is found.
22193 Otherwise, ERROR_MARK_NODE is returned. */
22194
22195 static tree
22196 cp_parser_pure_specifier (cp_parser* parser)
22197 {
22198 cp_token *token;
22199
22200 /* Look for the `=' token. */
22201 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
22202 return error_mark_node;
22203 /* Look for the `0' token. */
22204 token = cp_lexer_peek_token (parser->lexer);
22205
22206 if (token->type == CPP_EOF
22207 || token->type == CPP_PRAGMA_EOL)
22208 return error_mark_node;
22209
22210 cp_lexer_consume_token (parser->lexer);
22211
22212 /* Accept = default or = delete in c++0x mode. */
22213 if (token->keyword == RID_DEFAULT
22214 || token->keyword == RID_DELETE)
22215 {
22216 maybe_warn_cpp0x (CPP0X_DEFAULTED_DELETED);
22217 return token->u.value;
22218 }
22219
22220 /* c_lex_with_flags marks a single digit '0' with PURE_ZERO. */
22221 if (token->type != CPP_NUMBER || !(token->flags & PURE_ZERO))
22222 {
22223 cp_parser_error (parser,
22224 "invalid pure specifier (only %<= 0%> is allowed)");
22225 cp_parser_skip_to_end_of_statement (parser);
22226 return error_mark_node;
22227 }
22228 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
22229 {
22230 error_at (token->location, "templates may not be %<virtual%>");
22231 return error_mark_node;
22232 }
22233
22234 return integer_zero_node;
22235 }
22236
22237 /* Parse a constant-initializer.
22238
22239 constant-initializer:
22240 = constant-expression
22241
22242 Returns a representation of the constant-expression. */
22243
22244 static tree
22245 cp_parser_constant_initializer (cp_parser* parser)
22246 {
22247 /* Look for the `=' token. */
22248 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
22249 return error_mark_node;
22250
22251 /* It is invalid to write:
22252
22253 struct S { static const int i = { 7 }; };
22254
22255 */
22256 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
22257 {
22258 cp_parser_error (parser,
22259 "a brace-enclosed initializer is not allowed here");
22260 /* Consume the opening brace. */
22261 cp_lexer_consume_token (parser->lexer);
22262 /* Skip the initializer. */
22263 cp_parser_skip_to_closing_brace (parser);
22264 /* Look for the trailing `}'. */
22265 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
22266
22267 return error_mark_node;
22268 }
22269
22270 return cp_parser_constant_expression (parser);
22271 }
22272
22273 /* Derived classes [gram.class.derived] */
22274
22275 /* Parse a base-clause.
22276
22277 base-clause:
22278 : base-specifier-list
22279
22280 base-specifier-list:
22281 base-specifier ... [opt]
22282 base-specifier-list , base-specifier ... [opt]
22283
22284 Returns a TREE_LIST representing the base-classes, in the order in
22285 which they were declared. The representation of each node is as
22286 described by cp_parser_base_specifier.
22287
22288 In the case that no bases are specified, this function will return
22289 NULL_TREE, not ERROR_MARK_NODE. */
22290
22291 static tree
22292 cp_parser_base_clause (cp_parser* parser)
22293 {
22294 tree bases = NULL_TREE;
22295
22296 /* Look for the `:' that begins the list. */
22297 cp_parser_require (parser, CPP_COLON, RT_COLON);
22298
22299 /* Scan the base-specifier-list. */
22300 while (true)
22301 {
22302 cp_token *token;
22303 tree base;
22304 bool pack_expansion_p = false;
22305
22306 /* Look for the base-specifier. */
22307 base = cp_parser_base_specifier (parser);
22308 /* Look for the (optional) ellipsis. */
22309 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
22310 {
22311 /* Consume the `...'. */
22312 cp_lexer_consume_token (parser->lexer);
22313
22314 pack_expansion_p = true;
22315 }
22316
22317 /* Add BASE to the front of the list. */
22318 if (base && base != error_mark_node)
22319 {
22320 if (pack_expansion_p)
22321 /* Make this a pack expansion type. */
22322 TREE_VALUE (base) = make_pack_expansion (TREE_VALUE (base));
22323
22324 if (!check_for_bare_parameter_packs (TREE_VALUE (base)))
22325 {
22326 TREE_CHAIN (base) = bases;
22327 bases = base;
22328 }
22329 }
22330 /* Peek at the next token. */
22331 token = cp_lexer_peek_token (parser->lexer);
22332 /* If it's not a comma, then the list is complete. */
22333 if (token->type != CPP_COMMA)
22334 break;
22335 /* Consume the `,'. */
22336 cp_lexer_consume_token (parser->lexer);
22337 }
22338
22339 /* PARSER->SCOPE may still be non-NULL at this point, if the last
22340 base class had a qualified name. However, the next name that
22341 appears is certainly not qualified. */
22342 parser->scope = NULL_TREE;
22343 parser->qualifying_scope = NULL_TREE;
22344 parser->object_scope = NULL_TREE;
22345
22346 return nreverse (bases);
22347 }
22348
22349 /* Parse a base-specifier.
22350
22351 base-specifier:
22352 :: [opt] nested-name-specifier [opt] class-name
22353 virtual access-specifier [opt] :: [opt] nested-name-specifier
22354 [opt] class-name
22355 access-specifier virtual [opt] :: [opt] nested-name-specifier
22356 [opt] class-name
22357
22358 Returns a TREE_LIST. The TREE_PURPOSE will be one of
22359 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
22360 indicate the specifiers provided. The TREE_VALUE will be a TYPE
22361 (or the ERROR_MARK_NODE) indicating the type that was specified. */
22362
22363 static tree
22364 cp_parser_base_specifier (cp_parser* parser)
22365 {
22366 cp_token *token;
22367 bool done = false;
22368 bool virtual_p = false;
22369 bool duplicate_virtual_error_issued_p = false;
22370 bool duplicate_access_error_issued_p = false;
22371 bool class_scope_p, template_p;
22372 tree access = access_default_node;
22373 tree type;
22374
22375 /* Process the optional `virtual' and `access-specifier'. */
22376 while (!done)
22377 {
22378 /* Peek at the next token. */
22379 token = cp_lexer_peek_token (parser->lexer);
22380 /* Process `virtual'. */
22381 switch (token->keyword)
22382 {
22383 case RID_VIRTUAL:
22384 /* If `virtual' appears more than once, issue an error. */
22385 if (virtual_p && !duplicate_virtual_error_issued_p)
22386 {
22387 cp_parser_error (parser,
22388 "%<virtual%> specified more than once in base-specified");
22389 duplicate_virtual_error_issued_p = true;
22390 }
22391
22392 virtual_p = true;
22393
22394 /* Consume the `virtual' token. */
22395 cp_lexer_consume_token (parser->lexer);
22396
22397 break;
22398
22399 case RID_PUBLIC:
22400 case RID_PROTECTED:
22401 case RID_PRIVATE:
22402 /* If more than one access specifier appears, issue an
22403 error. */
22404 if (access != access_default_node
22405 && !duplicate_access_error_issued_p)
22406 {
22407 cp_parser_error (parser,
22408 "more than one access specifier in base-specified");
22409 duplicate_access_error_issued_p = true;
22410 }
22411
22412 access = ridpointers[(int) token->keyword];
22413
22414 /* Consume the access-specifier. */
22415 cp_lexer_consume_token (parser->lexer);
22416
22417 break;
22418
22419 default:
22420 done = true;
22421 break;
22422 }
22423 }
22424 /* It is not uncommon to see programs mechanically, erroneously, use
22425 the 'typename' keyword to denote (dependent) qualified types
22426 as base classes. */
22427 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
22428 {
22429 token = cp_lexer_peek_token (parser->lexer);
22430 if (!processing_template_decl)
22431 error_at (token->location,
22432 "keyword %<typename%> not allowed outside of templates");
22433 else
22434 error_at (token->location,
22435 "keyword %<typename%> not allowed in this context "
22436 "(the base class is implicitly a type)");
22437 cp_lexer_consume_token (parser->lexer);
22438 }
22439
22440 /* Look for the optional `::' operator. */
22441 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
22442 /* Look for the nested-name-specifier. The simplest way to
22443 implement:
22444
22445 [temp.res]
22446
22447 The keyword `typename' is not permitted in a base-specifier or
22448 mem-initializer; in these contexts a qualified name that
22449 depends on a template-parameter is implicitly assumed to be a
22450 type name.
22451
22452 is to pretend that we have seen the `typename' keyword at this
22453 point. */
22454 cp_parser_nested_name_specifier_opt (parser,
22455 /*typename_keyword_p=*/true,
22456 /*check_dependency_p=*/true,
22457 typename_type,
22458 /*is_declaration=*/true);
22459 /* If the base class is given by a qualified name, assume that names
22460 we see are type names or templates, as appropriate. */
22461 class_scope_p = (parser->scope && TYPE_P (parser->scope));
22462 template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
22463
22464 if (!parser->scope
22465 && cp_lexer_next_token_is_decltype (parser->lexer))
22466 /* DR 950 allows decltype as a base-specifier. */
22467 type = cp_parser_decltype (parser);
22468 else
22469 {
22470 /* Otherwise, look for the class-name. */
22471 type = cp_parser_class_name (parser,
22472 class_scope_p,
22473 template_p,
22474 typename_type,
22475 /*check_dependency_p=*/true,
22476 /*class_head_p=*/false,
22477 /*is_declaration=*/true);
22478 type = TREE_TYPE (type);
22479 }
22480
22481 if (type == error_mark_node)
22482 return error_mark_node;
22483
22484 return finish_base_specifier (type, access, virtual_p);
22485 }
22486
22487 /* Exception handling [gram.exception] */
22488
22489 /* Parse an (optional) noexcept-specification.
22490
22491 noexcept-specification:
22492 noexcept ( constant-expression ) [opt]
22493
22494 If no noexcept-specification is present, returns NULL_TREE.
22495 Otherwise, if REQUIRE_CONSTEXPR is false, then either parse and return any
22496 expression if parentheses follow noexcept, or return BOOLEAN_TRUE_NODE if
22497 there are no parentheses. CONSUMED_EXPR will be set accordingly.
22498 Otherwise, returns a noexcept specification unless RETURN_COND is true,
22499 in which case a boolean condition is returned instead. */
22500
22501 static tree
22502 cp_parser_noexcept_specification_opt (cp_parser* parser,
22503 bool require_constexpr,
22504 bool* consumed_expr,
22505 bool return_cond)
22506 {
22507 cp_token *token;
22508 const char *saved_message;
22509
22510 /* Peek at the next token. */
22511 token = cp_lexer_peek_token (parser->lexer);
22512
22513 /* Is it a noexcept-specification? */
22514 if (cp_parser_is_keyword (token, RID_NOEXCEPT))
22515 {
22516 tree expr;
22517 cp_lexer_consume_token (parser->lexer);
22518
22519 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
22520 {
22521 cp_lexer_consume_token (parser->lexer);
22522
22523 if (require_constexpr)
22524 {
22525 /* Types may not be defined in an exception-specification. */
22526 saved_message = parser->type_definition_forbidden_message;
22527 parser->type_definition_forbidden_message
22528 = G_("types may not be defined in an exception-specification");
22529
22530 expr = cp_parser_constant_expression (parser);
22531
22532 /* Restore the saved message. */
22533 parser->type_definition_forbidden_message = saved_message;
22534 }
22535 else
22536 {
22537 expr = cp_parser_expression (parser);
22538 *consumed_expr = true;
22539 }
22540
22541 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
22542 }
22543 else
22544 {
22545 expr = boolean_true_node;
22546 if (!require_constexpr)
22547 *consumed_expr = false;
22548 }
22549
22550 /* We cannot build a noexcept-spec right away because this will check
22551 that expr is a constexpr. */
22552 if (!return_cond)
22553 return build_noexcept_spec (expr, tf_warning_or_error);
22554 else
22555 return expr;
22556 }
22557 else
22558 return NULL_TREE;
22559 }
22560
22561 /* Parse an (optional) exception-specification.
22562
22563 exception-specification:
22564 throw ( type-id-list [opt] )
22565
22566 Returns a TREE_LIST representing the exception-specification. The
22567 TREE_VALUE of each node is a type. */
22568
22569 static tree
22570 cp_parser_exception_specification_opt (cp_parser* parser)
22571 {
22572 cp_token *token;
22573 tree type_id_list;
22574 const char *saved_message;
22575
22576 /* Peek at the next token. */
22577 token = cp_lexer_peek_token (parser->lexer);
22578
22579 /* Is it a noexcept-specification? */
22580 type_id_list = cp_parser_noexcept_specification_opt(parser, true, NULL,
22581 false);
22582 if (type_id_list != NULL_TREE)
22583 return type_id_list;
22584
22585 /* If it's not `throw', then there's no exception-specification. */
22586 if (!cp_parser_is_keyword (token, RID_THROW))
22587 return NULL_TREE;
22588
22589 #if 0
22590 /* Enable this once a lot of code has transitioned to noexcept? */
22591 if (cxx_dialect >= cxx11 && !in_system_header_at (input_location))
22592 warning (OPT_Wdeprecated, "dynamic exception specifications are "
22593 "deprecated in C++0x; use %<noexcept%> instead");
22594 #endif
22595
22596 /* Consume the `throw'. */
22597 cp_lexer_consume_token (parser->lexer);
22598
22599 /* Look for the `('. */
22600 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
22601
22602 /* Peek at the next token. */
22603 token = cp_lexer_peek_token (parser->lexer);
22604 /* If it's not a `)', then there is a type-id-list. */
22605 if (token->type != CPP_CLOSE_PAREN)
22606 {
22607 /* Types may not be defined in an exception-specification. */
22608 saved_message = parser->type_definition_forbidden_message;
22609 parser->type_definition_forbidden_message
22610 = G_("types may not be defined in an exception-specification");
22611 /* Parse the type-id-list. */
22612 type_id_list = cp_parser_type_id_list (parser);
22613 /* Restore the saved message. */
22614 parser->type_definition_forbidden_message = saved_message;
22615 }
22616 else
22617 type_id_list = empty_except_spec;
22618
22619 /* Look for the `)'. */
22620 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
22621
22622 return type_id_list;
22623 }
22624
22625 /* Parse an (optional) type-id-list.
22626
22627 type-id-list:
22628 type-id ... [opt]
22629 type-id-list , type-id ... [opt]
22630
22631 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
22632 in the order that the types were presented. */
22633
22634 static tree
22635 cp_parser_type_id_list (cp_parser* parser)
22636 {
22637 tree types = NULL_TREE;
22638
22639 while (true)
22640 {
22641 cp_token *token;
22642 tree type;
22643
22644 /* Get the next type-id. */
22645 type = cp_parser_type_id (parser);
22646 /* Parse the optional ellipsis. */
22647 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
22648 {
22649 /* Consume the `...'. */
22650 cp_lexer_consume_token (parser->lexer);
22651
22652 /* Turn the type into a pack expansion expression. */
22653 type = make_pack_expansion (type);
22654 }
22655 /* Add it to the list. */
22656 types = add_exception_specifier (types, type, /*complain=*/1);
22657 /* Peek at the next token. */
22658 token = cp_lexer_peek_token (parser->lexer);
22659 /* If it is not a `,', we are done. */
22660 if (token->type != CPP_COMMA)
22661 break;
22662 /* Consume the `,'. */
22663 cp_lexer_consume_token (parser->lexer);
22664 }
22665
22666 return nreverse (types);
22667 }
22668
22669 /* Parse a try-block.
22670
22671 try-block:
22672 try compound-statement handler-seq */
22673
22674 static tree
22675 cp_parser_try_block (cp_parser* parser)
22676 {
22677 tree try_block;
22678
22679 cp_parser_require_keyword (parser, RID_TRY, RT_TRY);
22680 if (parser->in_function_body
22681 && DECL_DECLARED_CONSTEXPR_P (current_function_decl))
22682 error ("%<try%> in %<constexpr%> function");
22683
22684 try_block = begin_try_block ();
22685 cp_parser_compound_statement (parser, NULL, BCS_TRY_BLOCK, false);
22686 finish_try_block (try_block);
22687 cp_parser_handler_seq (parser);
22688 finish_handler_sequence (try_block);
22689
22690 return try_block;
22691 }
22692
22693 /* Parse a function-try-block.
22694
22695 function-try-block:
22696 try ctor-initializer [opt] function-body handler-seq */
22697
22698 static bool
22699 cp_parser_function_try_block (cp_parser* parser)
22700 {
22701 tree compound_stmt;
22702 tree try_block;
22703 bool ctor_initializer_p;
22704
22705 /* Look for the `try' keyword. */
22706 if (!cp_parser_require_keyword (parser, RID_TRY, RT_TRY))
22707 return false;
22708 /* Let the rest of the front end know where we are. */
22709 try_block = begin_function_try_block (&compound_stmt);
22710 /* Parse the function-body. */
22711 ctor_initializer_p = cp_parser_ctor_initializer_opt_and_function_body
22712 (parser, /*in_function_try_block=*/true);
22713 /* We're done with the `try' part. */
22714 finish_function_try_block (try_block);
22715 /* Parse the handlers. */
22716 cp_parser_handler_seq (parser);
22717 /* We're done with the handlers. */
22718 finish_function_handler_sequence (try_block, compound_stmt);
22719
22720 return ctor_initializer_p;
22721 }
22722
22723 /* Parse a handler-seq.
22724
22725 handler-seq:
22726 handler handler-seq [opt] */
22727
22728 static void
22729 cp_parser_handler_seq (cp_parser* parser)
22730 {
22731 while (true)
22732 {
22733 cp_token *token;
22734
22735 /* Parse the handler. */
22736 cp_parser_handler (parser);
22737 /* Peek at the next token. */
22738 token = cp_lexer_peek_token (parser->lexer);
22739 /* If it's not `catch' then there are no more handlers. */
22740 if (!cp_parser_is_keyword (token, RID_CATCH))
22741 break;
22742 }
22743 }
22744
22745 /* Parse a handler.
22746
22747 handler:
22748 catch ( exception-declaration ) compound-statement */
22749
22750 static void
22751 cp_parser_handler (cp_parser* parser)
22752 {
22753 tree handler;
22754 tree declaration;
22755
22756 cp_parser_require_keyword (parser, RID_CATCH, RT_CATCH);
22757 handler = begin_handler ();
22758 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
22759 declaration = cp_parser_exception_declaration (parser);
22760 finish_handler_parms (declaration, handler);
22761 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
22762 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
22763 finish_handler (handler);
22764 }
22765
22766 /* Parse an exception-declaration.
22767
22768 exception-declaration:
22769 type-specifier-seq declarator
22770 type-specifier-seq abstract-declarator
22771 type-specifier-seq
22772 ...
22773
22774 Returns a VAR_DECL for the declaration, or NULL_TREE if the
22775 ellipsis variant is used. */
22776
22777 static tree
22778 cp_parser_exception_declaration (cp_parser* parser)
22779 {
22780 cp_decl_specifier_seq type_specifiers;
22781 cp_declarator *declarator;
22782 const char *saved_message;
22783
22784 /* If it's an ellipsis, it's easy to handle. */
22785 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
22786 {
22787 /* Consume the `...' token. */
22788 cp_lexer_consume_token (parser->lexer);
22789 return NULL_TREE;
22790 }
22791
22792 /* Types may not be defined in exception-declarations. */
22793 saved_message = parser->type_definition_forbidden_message;
22794 parser->type_definition_forbidden_message
22795 = G_("types may not be defined in exception-declarations");
22796
22797 /* Parse the type-specifier-seq. */
22798 cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
22799 /*is_trailing_return=*/false,
22800 &type_specifiers);
22801 /* If it's a `)', then there is no declarator. */
22802 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
22803 declarator = NULL;
22804 else
22805 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
22806 /*ctor_dtor_or_conv_p=*/NULL,
22807 /*parenthesized_p=*/NULL,
22808 /*member_p=*/false,
22809 /*friend_p=*/false);
22810
22811 /* Restore the saved message. */
22812 parser->type_definition_forbidden_message = saved_message;
22813
22814 if (!type_specifiers.any_specifiers_p)
22815 return error_mark_node;
22816
22817 return grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
22818 }
22819
22820 /* Parse a throw-expression.
22821
22822 throw-expression:
22823 throw assignment-expression [opt]
22824
22825 Returns a THROW_EXPR representing the throw-expression. */
22826
22827 static tree
22828 cp_parser_throw_expression (cp_parser* parser)
22829 {
22830 tree expression;
22831 cp_token* token;
22832
22833 cp_parser_require_keyword (parser, RID_THROW, RT_THROW);
22834 token = cp_lexer_peek_token (parser->lexer);
22835 /* Figure out whether or not there is an assignment-expression
22836 following the "throw" keyword. */
22837 if (token->type == CPP_COMMA
22838 || token->type == CPP_SEMICOLON
22839 || token->type == CPP_CLOSE_PAREN
22840 || token->type == CPP_CLOSE_SQUARE
22841 || token->type == CPP_CLOSE_BRACE
22842 || token->type == CPP_COLON)
22843 expression = NULL_TREE;
22844 else
22845 expression = cp_parser_assignment_expression (parser);
22846
22847 return build_throw (expression);
22848 }
22849
22850 /* GNU Extensions */
22851
22852 /* Parse an (optional) asm-specification.
22853
22854 asm-specification:
22855 asm ( string-literal )
22856
22857 If the asm-specification is present, returns a STRING_CST
22858 corresponding to the string-literal. Otherwise, returns
22859 NULL_TREE. */
22860
22861 static tree
22862 cp_parser_asm_specification_opt (cp_parser* parser)
22863 {
22864 cp_token *token;
22865 tree asm_specification;
22866
22867 /* Peek at the next token. */
22868 token = cp_lexer_peek_token (parser->lexer);
22869 /* If the next token isn't the `asm' keyword, then there's no
22870 asm-specification. */
22871 if (!cp_parser_is_keyword (token, RID_ASM))
22872 return NULL_TREE;
22873
22874 /* Consume the `asm' token. */
22875 cp_lexer_consume_token (parser->lexer);
22876 /* Look for the `('. */
22877 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
22878
22879 /* Look for the string-literal. */
22880 asm_specification = cp_parser_string_literal (parser, false, false);
22881
22882 /* Look for the `)'. */
22883 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
22884
22885 return asm_specification;
22886 }
22887
22888 /* Parse an asm-operand-list.
22889
22890 asm-operand-list:
22891 asm-operand
22892 asm-operand-list , asm-operand
22893
22894 asm-operand:
22895 string-literal ( expression )
22896 [ string-literal ] string-literal ( expression )
22897
22898 Returns a TREE_LIST representing the operands. The TREE_VALUE of
22899 each node is the expression. The TREE_PURPOSE is itself a
22900 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
22901 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
22902 is a STRING_CST for the string literal before the parenthesis. Returns
22903 ERROR_MARK_NODE if any of the operands are invalid. */
22904
22905 static tree
22906 cp_parser_asm_operand_list (cp_parser* parser)
22907 {
22908 tree asm_operands = NULL_TREE;
22909 bool invalid_operands = false;
22910
22911 while (true)
22912 {
22913 tree string_literal;
22914 tree expression;
22915 tree name;
22916
22917 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
22918 {
22919 /* Consume the `[' token. */
22920 cp_lexer_consume_token (parser->lexer);
22921 /* Read the operand name. */
22922 name = cp_parser_identifier (parser);
22923 if (name != error_mark_node)
22924 name = build_string (IDENTIFIER_LENGTH (name),
22925 IDENTIFIER_POINTER (name));
22926 /* Look for the closing `]'. */
22927 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
22928 }
22929 else
22930 name = NULL_TREE;
22931 /* Look for the string-literal. */
22932 string_literal = cp_parser_string_literal (parser, false, false);
22933
22934 /* Look for the `('. */
22935 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
22936 /* Parse the expression. */
22937 expression = cp_parser_expression (parser);
22938 /* Look for the `)'. */
22939 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
22940
22941 if (name == error_mark_node
22942 || string_literal == error_mark_node
22943 || expression == error_mark_node)
22944 invalid_operands = true;
22945
22946 /* Add this operand to the list. */
22947 asm_operands = tree_cons (build_tree_list (name, string_literal),
22948 expression,
22949 asm_operands);
22950 /* If the next token is not a `,', there are no more
22951 operands. */
22952 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
22953 break;
22954 /* Consume the `,'. */
22955 cp_lexer_consume_token (parser->lexer);
22956 }
22957
22958 return invalid_operands ? error_mark_node : nreverse (asm_operands);
22959 }
22960
22961 /* Parse an asm-clobber-list.
22962
22963 asm-clobber-list:
22964 string-literal
22965 asm-clobber-list , string-literal
22966
22967 Returns a TREE_LIST, indicating the clobbers in the order that they
22968 appeared. The TREE_VALUE of each node is a STRING_CST. */
22969
22970 static tree
22971 cp_parser_asm_clobber_list (cp_parser* parser)
22972 {
22973 tree clobbers = NULL_TREE;
22974
22975 while (true)
22976 {
22977 tree string_literal;
22978
22979 /* Look for the string literal. */
22980 string_literal = cp_parser_string_literal (parser, false, false);
22981 /* Add it to the list. */
22982 clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
22983 /* If the next token is not a `,', then the list is
22984 complete. */
22985 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
22986 break;
22987 /* Consume the `,' token. */
22988 cp_lexer_consume_token (parser->lexer);
22989 }
22990
22991 return clobbers;
22992 }
22993
22994 /* Parse an asm-label-list.
22995
22996 asm-label-list:
22997 identifier
22998 asm-label-list , identifier
22999
23000 Returns a TREE_LIST, indicating the labels in the order that they
23001 appeared. The TREE_VALUE of each node is a label. */
23002
23003 static tree
23004 cp_parser_asm_label_list (cp_parser* parser)
23005 {
23006 tree labels = NULL_TREE;
23007
23008 while (true)
23009 {
23010 tree identifier, label, name;
23011
23012 /* Look for the identifier. */
23013 identifier = cp_parser_identifier (parser);
23014 if (!error_operand_p (identifier))
23015 {
23016 label = lookup_label (identifier);
23017 if (TREE_CODE (label) == LABEL_DECL)
23018 {
23019 TREE_USED (label) = 1;
23020 check_goto (label);
23021 name = build_string (IDENTIFIER_LENGTH (identifier),
23022 IDENTIFIER_POINTER (identifier));
23023 labels = tree_cons (name, label, labels);
23024 }
23025 }
23026 /* If the next token is not a `,', then the list is
23027 complete. */
23028 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
23029 break;
23030 /* Consume the `,' token. */
23031 cp_lexer_consume_token (parser->lexer);
23032 }
23033
23034 return nreverse (labels);
23035 }
23036
23037 /* Return TRUE iff the next tokens in the stream are possibly the
23038 beginning of a GNU extension attribute. */
23039
23040 static bool
23041 cp_next_tokens_can_be_gnu_attribute_p (cp_parser *parser)
23042 {
23043 return cp_nth_tokens_can_be_gnu_attribute_p (parser, 1);
23044 }
23045
23046 /* Return TRUE iff the next tokens in the stream are possibly the
23047 beginning of a standard C++-11 attribute specifier. */
23048
23049 static bool
23050 cp_next_tokens_can_be_std_attribute_p (cp_parser *parser)
23051 {
23052 return cp_nth_tokens_can_be_std_attribute_p (parser, 1);
23053 }
23054
23055 /* Return TRUE iff the next Nth tokens in the stream are possibly the
23056 beginning of a standard C++-11 attribute specifier. */
23057
23058 static bool
23059 cp_nth_tokens_can_be_std_attribute_p (cp_parser *parser, size_t n)
23060 {
23061 cp_token *token = cp_lexer_peek_nth_token (parser->lexer, n);
23062
23063 return (cxx_dialect >= cxx11
23064 && ((token->type == CPP_KEYWORD && token->keyword == RID_ALIGNAS)
23065 || (token->type == CPP_OPEN_SQUARE
23066 && (token = cp_lexer_peek_nth_token (parser->lexer, n + 1))
23067 && token->type == CPP_OPEN_SQUARE)));
23068 }
23069
23070 /* Return TRUE iff the next Nth tokens in the stream are possibly the
23071 beginning of a GNU extension attribute. */
23072
23073 static bool
23074 cp_nth_tokens_can_be_gnu_attribute_p (cp_parser *parser, size_t n)
23075 {
23076 cp_token *token = cp_lexer_peek_nth_token (parser->lexer, n);
23077
23078 return token->type == CPP_KEYWORD && token->keyword == RID_ATTRIBUTE;
23079 }
23080
23081 /* Return true iff the next tokens can be the beginning of either a
23082 GNU attribute list, or a standard C++11 attribute sequence. */
23083
23084 static bool
23085 cp_next_tokens_can_be_attribute_p (cp_parser *parser)
23086 {
23087 return (cp_next_tokens_can_be_gnu_attribute_p (parser)
23088 || cp_next_tokens_can_be_std_attribute_p (parser));
23089 }
23090
23091 /* Return true iff the next Nth tokens can be the beginning of either
23092 a GNU attribute list, or a standard C++11 attribute sequence. */
23093
23094 static bool
23095 cp_nth_tokens_can_be_attribute_p (cp_parser *parser, size_t n)
23096 {
23097 return (cp_nth_tokens_can_be_gnu_attribute_p (parser, n)
23098 || cp_nth_tokens_can_be_std_attribute_p (parser, n));
23099 }
23100
23101 /* Parse either a standard C++-11 attribute-specifier-seq, or a series
23102 of GNU attributes, or return NULL. */
23103
23104 static tree
23105 cp_parser_attributes_opt (cp_parser *parser)
23106 {
23107 if (cp_next_tokens_can_be_gnu_attribute_p (parser))
23108 return cp_parser_gnu_attributes_opt (parser);
23109 return cp_parser_std_attribute_spec_seq (parser);
23110 }
23111
23112 #define CILK_SIMD_FN_CLAUSE_MASK \
23113 ((OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_VECTORLENGTH) \
23114 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_LINEAR) \
23115 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_UNIFORM) \
23116 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_MASK) \
23117 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_NOMASK))
23118
23119 /* Parses the Cilk Plus SIMD-enabled function's attribute. Syntax:
23120 vector [(<clauses>)] */
23121
23122 static void
23123 cp_parser_cilk_simd_fn_vector_attrs (cp_parser *parser, cp_token *v_token)
23124 {
23125 bool first_p = parser->cilk_simd_fn_info == NULL;
23126 cp_token *token = v_token;
23127 if (first_p)
23128 {
23129 parser->cilk_simd_fn_info = XNEW (cp_omp_declare_simd_data);
23130 parser->cilk_simd_fn_info->error_seen = false;
23131 parser->cilk_simd_fn_info->fndecl_seen = false;
23132 parser->cilk_simd_fn_info->tokens = vNULL;
23133 }
23134 int paren_scope = 0;
23135 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
23136 {
23137 cp_lexer_consume_token (parser->lexer);
23138 v_token = cp_lexer_peek_token (parser->lexer);
23139 paren_scope++;
23140 }
23141 while (paren_scope > 0)
23142 {
23143 token = cp_lexer_peek_token (parser->lexer);
23144 if (token->type == CPP_OPEN_PAREN)
23145 paren_scope++;
23146 else if (token->type == CPP_CLOSE_PAREN)
23147 paren_scope--;
23148 /* Do not push the last ')' */
23149 if (!(token->type == CPP_CLOSE_PAREN && paren_scope == 0))
23150 cp_lexer_consume_token (parser->lexer);
23151 }
23152
23153 token->type = CPP_PRAGMA_EOL;
23154 parser->lexer->next_token = token;
23155 cp_lexer_consume_token (parser->lexer);
23156
23157 struct cp_token_cache *cp
23158 = cp_token_cache_new (v_token, cp_lexer_peek_token (parser->lexer));
23159 parser->cilk_simd_fn_info->tokens.safe_push (cp);
23160 }
23161
23162 /* Parse an (optional) series of attributes.
23163
23164 attributes:
23165 attributes attribute
23166
23167 attribute:
23168 __attribute__ (( attribute-list [opt] ))
23169
23170 The return value is as for cp_parser_gnu_attribute_list. */
23171
23172 static tree
23173 cp_parser_gnu_attributes_opt (cp_parser* parser)
23174 {
23175 tree attributes = NULL_TREE;
23176
23177 while (true)
23178 {
23179 cp_token *token;
23180 tree attribute_list;
23181 bool ok = true;
23182
23183 /* Peek at the next token. */
23184 token = cp_lexer_peek_token (parser->lexer);
23185 /* If it's not `__attribute__', then we're done. */
23186 if (token->keyword != RID_ATTRIBUTE)
23187 break;
23188
23189 /* Consume the `__attribute__' keyword. */
23190 cp_lexer_consume_token (parser->lexer);
23191 /* Look for the two `(' tokens. */
23192 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
23193 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
23194
23195 /* Peek at the next token. */
23196 token = cp_lexer_peek_token (parser->lexer);
23197 if (token->type != CPP_CLOSE_PAREN)
23198 /* Parse the attribute-list. */
23199 attribute_list = cp_parser_gnu_attribute_list (parser);
23200 else
23201 /* If the next token is a `)', then there is no attribute
23202 list. */
23203 attribute_list = NULL;
23204
23205 /* Look for the two `)' tokens. */
23206 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
23207 ok = false;
23208 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
23209 ok = false;
23210 if (!ok)
23211 cp_parser_skip_to_end_of_statement (parser);
23212
23213 /* Add these new attributes to the list. */
23214 attributes = chainon (attributes, attribute_list);
23215 }
23216
23217 return attributes;
23218 }
23219
23220 /* Parse a GNU attribute-list.
23221
23222 attribute-list:
23223 attribute
23224 attribute-list , attribute
23225
23226 attribute:
23227 identifier
23228 identifier ( identifier )
23229 identifier ( identifier , expression-list )
23230 identifier ( expression-list )
23231
23232 Returns a TREE_LIST, or NULL_TREE on error. Each node corresponds
23233 to an attribute. The TREE_PURPOSE of each node is the identifier
23234 indicating which attribute is in use. The TREE_VALUE represents
23235 the arguments, if any. */
23236
23237 static tree
23238 cp_parser_gnu_attribute_list (cp_parser* parser)
23239 {
23240 tree attribute_list = NULL_TREE;
23241 bool save_translate_strings_p = parser->translate_strings_p;
23242
23243 parser->translate_strings_p = false;
23244 while (true)
23245 {
23246 cp_token *token;
23247 tree identifier;
23248 tree attribute;
23249
23250 /* Look for the identifier. We also allow keywords here; for
23251 example `__attribute__ ((const))' is legal. */
23252 token = cp_lexer_peek_token (parser->lexer);
23253 if (token->type == CPP_NAME
23254 || token->type == CPP_KEYWORD)
23255 {
23256 tree arguments = NULL_TREE;
23257
23258 /* Consume the token, but save it since we need it for the
23259 SIMD enabled function parsing. */
23260 cp_token *id_token = cp_lexer_consume_token (parser->lexer);
23261
23262 /* Save away the identifier that indicates which attribute
23263 this is. */
23264 identifier = (token->type == CPP_KEYWORD)
23265 /* For keywords, use the canonical spelling, not the
23266 parsed identifier. */
23267 ? ridpointers[(int) token->keyword]
23268 : id_token->u.value;
23269
23270 attribute = build_tree_list (identifier, NULL_TREE);
23271
23272 /* Peek at the next token. */
23273 token = cp_lexer_peek_token (parser->lexer);
23274 /* If it's an `(', then parse the attribute arguments. */
23275 if (token->type == CPP_OPEN_PAREN)
23276 {
23277 vec<tree, va_gc> *vec;
23278 int attr_flag = (attribute_takes_identifier_p (identifier)
23279 ? id_attr : normal_attr);
23280 if (is_cilkplus_vector_p (identifier))
23281 {
23282 cp_parser_cilk_simd_fn_vector_attrs (parser, id_token);
23283 continue;
23284 }
23285 else
23286 vec = cp_parser_parenthesized_expression_list
23287 (parser, attr_flag, /*cast_p=*/false,
23288 /*allow_expansion_p=*/false,
23289 /*non_constant_p=*/NULL);
23290 if (vec == NULL)
23291 arguments = error_mark_node;
23292 else
23293 {
23294 arguments = build_tree_list_vec (vec);
23295 release_tree_vector (vec);
23296 }
23297 /* Save the arguments away. */
23298 TREE_VALUE (attribute) = arguments;
23299 }
23300 else if (is_cilkplus_vector_p (identifier))
23301 {
23302 cp_parser_cilk_simd_fn_vector_attrs (parser, id_token);
23303 continue;
23304 }
23305
23306 if (arguments != error_mark_node)
23307 {
23308 /* Add this attribute to the list. */
23309 TREE_CHAIN (attribute) = attribute_list;
23310 attribute_list = attribute;
23311 }
23312
23313 token = cp_lexer_peek_token (parser->lexer);
23314 }
23315 /* Now, look for more attributes. If the next token isn't a
23316 `,', we're done. */
23317 if (token->type != CPP_COMMA)
23318 break;
23319
23320 /* Consume the comma and keep going. */
23321 cp_lexer_consume_token (parser->lexer);
23322 }
23323 parser->translate_strings_p = save_translate_strings_p;
23324
23325 /* We built up the list in reverse order. */
23326 return nreverse (attribute_list);
23327 }
23328
23329 /* Parse a standard C++11 attribute.
23330
23331 The returned representation is a TREE_LIST which TREE_PURPOSE is
23332 the scoped name of the attribute, and the TREE_VALUE is its
23333 arguments list.
23334
23335 Note that the scoped name of the attribute is itself a TREE_LIST
23336 which TREE_PURPOSE is the namespace of the attribute, and
23337 TREE_VALUE its name. This is unlike a GNU attribute -- as parsed
23338 by cp_parser_gnu_attribute_list -- that doesn't have any namespace
23339 and which TREE_PURPOSE is directly the attribute name.
23340
23341 Clients of the attribute code should use get_attribute_namespace
23342 and get_attribute_name to get the actual namespace and name of
23343 attributes, regardless of their being GNU or C++11 attributes.
23344
23345 attribute:
23346 attribute-token attribute-argument-clause [opt]
23347
23348 attribute-token:
23349 identifier
23350 attribute-scoped-token
23351
23352 attribute-scoped-token:
23353 attribute-namespace :: identifier
23354
23355 attribute-namespace:
23356 identifier
23357
23358 attribute-argument-clause:
23359 ( balanced-token-seq )
23360
23361 balanced-token-seq:
23362 balanced-token [opt]
23363 balanced-token-seq balanced-token
23364
23365 balanced-token:
23366 ( balanced-token-seq )
23367 [ balanced-token-seq ]
23368 { balanced-token-seq }. */
23369
23370 static tree
23371 cp_parser_std_attribute (cp_parser *parser)
23372 {
23373 tree attribute, attr_ns = NULL_TREE, attr_id = NULL_TREE, arguments;
23374 cp_token *token;
23375
23376 /* First, parse name of the attribute, a.k.a attribute-token. */
23377
23378 token = cp_lexer_peek_token (parser->lexer);
23379 if (token->type == CPP_NAME)
23380 attr_id = token->u.value;
23381 else if (token->type == CPP_KEYWORD)
23382 attr_id = ridpointers[(int) token->keyword];
23383 else if (token->flags & NAMED_OP)
23384 attr_id = get_identifier (cpp_type2name (token->type, token->flags));
23385
23386 if (attr_id == NULL_TREE)
23387 return NULL_TREE;
23388
23389 cp_lexer_consume_token (parser->lexer);
23390
23391 token = cp_lexer_peek_token (parser->lexer);
23392 if (token->type == CPP_SCOPE)
23393 {
23394 /* We are seeing a scoped attribute token. */
23395
23396 cp_lexer_consume_token (parser->lexer);
23397 attr_ns = attr_id;
23398
23399 token = cp_lexer_consume_token (parser->lexer);
23400 if (token->type == CPP_NAME)
23401 attr_id = token->u.value;
23402 else if (token->type == CPP_KEYWORD)
23403 attr_id = ridpointers[(int) token->keyword];
23404 else
23405 {
23406 error_at (token->location,
23407 "expected an identifier for the attribute name");
23408 return error_mark_node;
23409 }
23410 attribute = build_tree_list (build_tree_list (attr_ns, attr_id),
23411 NULL_TREE);
23412 token = cp_lexer_peek_token (parser->lexer);
23413 }
23414 else
23415 {
23416 attribute = build_tree_list (build_tree_list (NULL_TREE, attr_id),
23417 NULL_TREE);
23418 /* C++11 noreturn attribute is equivalent to GNU's. */
23419 if (is_attribute_p ("noreturn", attr_id))
23420 TREE_PURPOSE (TREE_PURPOSE (attribute)) = get_identifier ("gnu");
23421 /* C++14 deprecated attribute is equivalent to GNU's. */
23422 else if (cxx_dialect >= cxx11 && is_attribute_p ("deprecated", attr_id))
23423 {
23424 if (cxx_dialect == cxx11)
23425 pedwarn (token->location, OPT_Wpedantic,
23426 "%<deprecated%> is a C++14 feature;"
23427 " use %<gnu::deprecated%>");
23428 TREE_PURPOSE (TREE_PURPOSE (attribute)) = get_identifier ("gnu");
23429 }
23430 /* Transactional Memory TS optimize_for_synchronized attribute is
23431 equivalent to GNU transaction_callable. */
23432 else if (is_attribute_p ("optimize_for_synchronized", attr_id))
23433 TREE_PURPOSE (attribute)
23434 = get_identifier ("transaction_callable");
23435 /* Transactional Memory attributes are GNU attributes. */
23436 else if (tm_attr_to_mask (attr_id))
23437 TREE_PURPOSE (attribute) = attr_id;
23438 }
23439
23440 /* Now parse the optional argument clause of the attribute. */
23441
23442 if (token->type != CPP_OPEN_PAREN)
23443 return attribute;
23444
23445 {
23446 vec<tree, va_gc> *vec;
23447 int attr_flag = normal_attr;
23448
23449 if (attr_ns == get_identifier ("gnu")
23450 && attribute_takes_identifier_p (attr_id))
23451 /* A GNU attribute that takes an identifier in parameter. */
23452 attr_flag = id_attr;
23453
23454 vec = cp_parser_parenthesized_expression_list
23455 (parser, attr_flag, /*cast_p=*/false,
23456 /*allow_expansion_p=*/true,
23457 /*non_constant_p=*/NULL);
23458 if (vec == NULL)
23459 arguments = error_mark_node;
23460 else
23461 {
23462 arguments = build_tree_list_vec (vec);
23463 release_tree_vector (vec);
23464 }
23465
23466 if (arguments == error_mark_node)
23467 attribute = error_mark_node;
23468 else
23469 TREE_VALUE (attribute) = arguments;
23470 }
23471
23472 return attribute;
23473 }
23474
23475 /* Check that the attribute ATTRIBUTE appears at most once in the
23476 attribute-list ATTRIBUTES. This is enforced for noreturn (7.6.3)
23477 and deprecated (7.6.5). Note that carries_dependency (7.6.4)
23478 isn't implemented yet in GCC. */
23479
23480 static void
23481 cp_parser_check_std_attribute (tree attributes, tree attribute)
23482 {
23483 if (attributes)
23484 {
23485 tree name = get_attribute_name (attribute);
23486 if (is_attribute_p ("noreturn", name)
23487 && lookup_attribute ("noreturn", attributes))
23488 error ("attribute noreturn can appear at most once "
23489 "in an attribute-list");
23490 else if (is_attribute_p ("deprecated", name)
23491 && lookup_attribute ("deprecated", attributes))
23492 error ("attribute deprecated can appear at most once "
23493 "in an attribute-list");
23494 }
23495 }
23496
23497 /* Parse a list of standard C++-11 attributes.
23498
23499 attribute-list:
23500 attribute [opt]
23501 attribute-list , attribute[opt]
23502 attribute ...
23503 attribute-list , attribute ...
23504 */
23505
23506 static tree
23507 cp_parser_std_attribute_list (cp_parser *parser)
23508 {
23509 tree attributes = NULL_TREE, attribute = NULL_TREE;
23510 cp_token *token = NULL;
23511
23512 while (true)
23513 {
23514 attribute = cp_parser_std_attribute (parser);
23515 if (attribute == error_mark_node)
23516 break;
23517 if (attribute != NULL_TREE)
23518 {
23519 cp_parser_check_std_attribute (attributes, attribute);
23520 TREE_CHAIN (attribute) = attributes;
23521 attributes = attribute;
23522 }
23523 token = cp_lexer_peek_token (parser->lexer);
23524 if (token->type == CPP_ELLIPSIS)
23525 {
23526 cp_lexer_consume_token (parser->lexer);
23527 TREE_VALUE (attribute)
23528 = make_pack_expansion (TREE_VALUE (attribute));
23529 token = cp_lexer_peek_token (parser->lexer);
23530 }
23531 if (token->type != CPP_COMMA)
23532 break;
23533 cp_lexer_consume_token (parser->lexer);
23534 }
23535 attributes = nreverse (attributes);
23536 return attributes;
23537 }
23538
23539 /* Parse a standard C++-11 attribute specifier.
23540
23541 attribute-specifier:
23542 [ [ attribute-list ] ]
23543 alignment-specifier
23544
23545 alignment-specifier:
23546 alignas ( type-id ... [opt] )
23547 alignas ( alignment-expression ... [opt] ). */
23548
23549 static tree
23550 cp_parser_std_attribute_spec (cp_parser *parser)
23551 {
23552 tree attributes = NULL_TREE;
23553 cp_token *token = cp_lexer_peek_token (parser->lexer);
23554
23555 if (token->type == CPP_OPEN_SQUARE
23556 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_OPEN_SQUARE)
23557 {
23558 cp_lexer_consume_token (parser->lexer);
23559 cp_lexer_consume_token (parser->lexer);
23560
23561 attributes = cp_parser_std_attribute_list (parser);
23562
23563 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE)
23564 || !cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
23565 cp_parser_skip_to_end_of_statement (parser);
23566 else
23567 /* Warn about parsing c++11 attribute in non-c++1 mode, only
23568 when we are sure that we have actually parsed them. */
23569 maybe_warn_cpp0x (CPP0X_ATTRIBUTES);
23570 }
23571 else
23572 {
23573 tree alignas_expr;
23574
23575 /* Look for an alignment-specifier. */
23576
23577 token = cp_lexer_peek_token (parser->lexer);
23578
23579 if (token->type != CPP_KEYWORD
23580 || token->keyword != RID_ALIGNAS)
23581 return NULL_TREE;
23582
23583 cp_lexer_consume_token (parser->lexer);
23584 maybe_warn_cpp0x (CPP0X_ATTRIBUTES);
23585
23586 if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN) == NULL)
23587 {
23588 cp_parser_error (parser, "expected %<(%>");
23589 return error_mark_node;
23590 }
23591
23592 cp_parser_parse_tentatively (parser);
23593 alignas_expr = cp_parser_type_id (parser);
23594
23595 if (!cp_parser_parse_definitely (parser))
23596 {
23597 gcc_assert (alignas_expr == error_mark_node
23598 || alignas_expr == NULL_TREE);
23599
23600 alignas_expr =
23601 cp_parser_assignment_expression (parser);
23602 if (alignas_expr == error_mark_node)
23603 cp_parser_skip_to_end_of_statement (parser);
23604 if (alignas_expr == NULL_TREE
23605 || alignas_expr == error_mark_node)
23606 return alignas_expr;
23607 }
23608
23609 alignas_expr = cxx_alignas_expr (alignas_expr);
23610 alignas_expr = build_tree_list (NULL_TREE, alignas_expr);
23611
23612 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
23613 {
23614 cp_lexer_consume_token (parser->lexer);
23615 alignas_expr = make_pack_expansion (alignas_expr);
23616 }
23617
23618 if (cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN) == NULL)
23619 {
23620 cp_parser_error (parser, "expected %<)%>");
23621 return error_mark_node;
23622 }
23623
23624 /* Build the C++-11 representation of an 'aligned'
23625 attribute. */
23626 attributes =
23627 build_tree_list (build_tree_list (get_identifier ("gnu"),
23628 get_identifier ("aligned")),
23629 alignas_expr);
23630 }
23631
23632 return attributes;
23633 }
23634
23635 /* Parse a standard C++-11 attribute-specifier-seq.
23636
23637 attribute-specifier-seq:
23638 attribute-specifier-seq [opt] attribute-specifier
23639 */
23640
23641 static tree
23642 cp_parser_std_attribute_spec_seq (cp_parser *parser)
23643 {
23644 tree attr_specs = NULL;
23645
23646 while (true)
23647 {
23648 tree attr_spec = cp_parser_std_attribute_spec (parser);
23649 if (attr_spec == NULL_TREE)
23650 break;
23651 if (attr_spec == error_mark_node)
23652 return error_mark_node;
23653
23654 TREE_CHAIN (attr_spec) = attr_specs;
23655 attr_specs = attr_spec;
23656 }
23657
23658 attr_specs = nreverse (attr_specs);
23659 return attr_specs;
23660 }
23661
23662 /* Parse an optional `__extension__' keyword. Returns TRUE if it is
23663 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
23664 current value of the PEDANTIC flag, regardless of whether or not
23665 the `__extension__' keyword is present. The caller is responsible
23666 for restoring the value of the PEDANTIC flag. */
23667
23668 static bool
23669 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
23670 {
23671 /* Save the old value of the PEDANTIC flag. */
23672 *saved_pedantic = pedantic;
23673
23674 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
23675 {
23676 /* Consume the `__extension__' token. */
23677 cp_lexer_consume_token (parser->lexer);
23678 /* We're not being pedantic while the `__extension__' keyword is
23679 in effect. */
23680 pedantic = 0;
23681
23682 return true;
23683 }
23684
23685 return false;
23686 }
23687
23688 /* Parse a label declaration.
23689
23690 label-declaration:
23691 __label__ label-declarator-seq ;
23692
23693 label-declarator-seq:
23694 identifier , label-declarator-seq
23695 identifier */
23696
23697 static void
23698 cp_parser_label_declaration (cp_parser* parser)
23699 {
23700 /* Look for the `__label__' keyword. */
23701 cp_parser_require_keyword (parser, RID_LABEL, RT_LABEL);
23702
23703 while (true)
23704 {
23705 tree identifier;
23706
23707 /* Look for an identifier. */
23708 identifier = cp_parser_identifier (parser);
23709 /* If we failed, stop. */
23710 if (identifier == error_mark_node)
23711 break;
23712 /* Declare it as a label. */
23713 finish_label_decl (identifier);
23714 /* If the next token is a `;', stop. */
23715 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
23716 break;
23717 /* Look for the `,' separating the label declarations. */
23718 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
23719 }
23720
23721 /* Look for the final `;'. */
23722 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
23723 }
23724
23725 // -------------------------------------------------------------------------- //
23726 // Requires Clause
23727
23728 // Parse a requires clause.
23729 //
23730 // requires-clause:
23731 // 'requires' logical-or-expression
23732 //
23733 // The required logical-or-expression must be a constant expression. Note
23734 // that we don't check that the expression is constepxr here. We defer until
23735 // we analyze constraints and then, we only check atomic constraints.
23736 static tree
23737 cp_parser_requires_clause (cp_parser *parser)
23738 {
23739 // Parse the requires clause so that it is not automatically folded.
23740 ++processing_template_decl;
23741 tree expr = cp_parser_binary_expression (parser, false, false,
23742 PREC_NOT_OPERATOR, NULL);
23743 --processing_template_decl;
23744 return expr;
23745 }
23746
23747 // Optionally parse a requires clause:
23748 static tree
23749 cp_parser_requires_clause_opt (cp_parser *parser)
23750 {
23751 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_REQUIRES))
23752 return NULL_TREE;
23753 cp_lexer_consume_token (parser->lexer);
23754 return cp_parser_requires_clause (parser);
23755 }
23756
23757
23758 /*---------------------------------------------------------------------------
23759 Requires expressions
23760 ---------------------------------------------------------------------------*/
23761
23762 /* Parse a requires expression
23763
23764 requirement-expression:
23765 'requires' requirement-parameter-list [opt] requirement-body */
23766 static tree
23767 cp_parser_requires_expression (cp_parser *parser)
23768 {
23769 gcc_assert (cp_lexer_next_token_is_keyword (parser->lexer, RID_REQUIRES));
23770 location_t loc = cp_lexer_consume_token (parser->lexer)->location;
23771
23772 /* A requires-expression shall appear only within a concept
23773 definition or a requires-clause.
23774
23775 TODO: Implement this diagnostic correctly. */
23776 if (!processing_template_decl)
23777 {
23778 error_at (loc, "a requires expression cannot appear outside a template");
23779 cp_parser_skip_to_end_of_statement (parser);
23780 return error_mark_node;
23781 }
23782
23783 tree parms, reqs;
23784 {
23785 /* Local parameters are delared as variables within the scope
23786 of the expression. They are not visible past the end of
23787 the expression. Expressions within the requires-expression
23788 are unevaluated. */
23789 struct scope_sentinel
23790 {
23791 scope_sentinel ()
23792 {
23793 ++cp_unevaluated_operand;
23794 begin_scope (sk_block, NULL_TREE);
23795 }
23796
23797 ~scope_sentinel ()
23798 {
23799 pop_bindings_and_leave_scope ();
23800 --cp_unevaluated_operand;
23801 }
23802 } s;
23803
23804 /* Parse the optional parameter list. */
23805 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
23806 {
23807 parms = cp_parser_requirement_parameter_list (parser);
23808 if (parms == error_mark_node)
23809 return error_mark_node;
23810 }
23811 else
23812 parms = NULL_TREE;
23813
23814 /* Parse the requirement body. */
23815 reqs = cp_parser_requirement_body (parser);
23816 if (reqs == error_mark_node)
23817 return error_mark_node;
23818 }
23819
23820 /* This needs to happen after pop_bindings_and_leave_scope, as it reverses
23821 the parm chain. */
23822 grokparms (parms, &parms);
23823 return finish_requires_expr (parms, reqs);
23824 }
23825
23826 /* Parse a parameterized requirement.
23827
23828 requirement-parameter-list:
23829 '(' parameter-declaration-clause ')' */
23830 static tree
23831 cp_parser_requirement_parameter_list (cp_parser *parser)
23832 {
23833 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
23834 return error_mark_node;
23835
23836 tree parms = cp_parser_parameter_declaration_clause (parser);
23837
23838 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
23839 return error_mark_node;
23840
23841 return parms;
23842 }
23843
23844 /* Parse the body of a requirement.
23845
23846 requirement-body:
23847 '{' requirement-list '}' */
23848 static tree
23849 cp_parser_requirement_body (cp_parser *parser)
23850 {
23851 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
23852 return error_mark_node;
23853
23854 tree reqs = cp_parser_requirement_list (parser);
23855
23856 if (!cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE))
23857 return error_mark_node;
23858
23859 return reqs;
23860 }
23861
23862 /* Parse a list of requirements.
23863
23864 requirement-list:
23865 requirement
23866 requirement-list ';' requirement[opt] */
23867 static tree
23868 cp_parser_requirement_list (cp_parser *parser)
23869 {
23870 tree result = NULL_TREE;
23871 while (true)
23872 {
23873 tree req = cp_parser_requirement (parser);
23874 if (req == error_mark_node)
23875 return error_mark_node;
23876
23877 result = tree_cons (NULL_TREE, req, result);
23878
23879 /* If we see a semi-colon, consume it. */
23880 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
23881 cp_lexer_consume_token (parser->lexer);
23882
23883 /* Stop processing at the end of the list. */
23884 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
23885 break;
23886 }
23887
23888 /* Reverse the order of requirements so they are analyzed in
23889 declaration order. */
23890 return nreverse (result);
23891 }
23892
23893 /* Parse a syntactic requirement or type requirement.
23894
23895 requirement:
23896 simple-requirement
23897 compound-requirement
23898 type-requirement
23899 nested-requirement */
23900 static tree
23901 cp_parser_requirement (cp_parser *parser)
23902 {
23903 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
23904 return cp_parser_compound_requirement (parser);
23905 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
23906 return cp_parser_type_requirement (parser);
23907 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_REQUIRES))
23908 return cp_parser_nested_requirement (parser);
23909 else
23910 return cp_parser_simple_requirement (parser);
23911 }
23912
23913 /* Parse a simple requirement.
23914
23915 simple-requirement:
23916 expression ';' */
23917 static tree
23918 cp_parser_simple_requirement (cp_parser *parser)
23919 {
23920 tree expr = cp_parser_expression (parser, NULL, false, false);
23921 if (!expr || expr == error_mark_node)
23922 return error_mark_node;
23923
23924 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
23925 return error_mark_node;
23926
23927 return finish_simple_requirement (expr);
23928 }
23929
23930 /* Parse a type requirement
23931
23932 type-requirement
23933 nested-name-specifier [opt] required-type-name ';'
23934
23935 required-type-name:
23936 type-name
23937 'template' [opt] simple-template-id */
23938 static tree
23939 cp_parser_type_requirement (cp_parser *parser)
23940 {
23941 cp_lexer_consume_token (parser->lexer);
23942
23943 // Save the scope before parsing name specifiers.
23944 tree saved_scope = parser->scope;
23945 tree saved_object_scope = parser->object_scope;
23946 tree saved_qualifying_scope = parser->qualifying_scope;
23947 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
23948 cp_parser_nested_name_specifier_opt (parser,
23949 /*typename_keyword_p=*/true,
23950 /*check_dependency_p=*/false,
23951 /*type_p=*/true,
23952 /*is_declaration=*/false);
23953
23954 tree type;
23955 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
23956 {
23957 cp_lexer_consume_token (parser->lexer);
23958 type = cp_parser_template_id (parser,
23959 /*template_keyword_p=*/true,
23960 /*check_dependency=*/false,
23961 /*tag_type=*/none_type,
23962 /*is_declaration=*/false);
23963 type = make_typename_type (parser->scope, type, typename_type,
23964 /*complain=*/tf_error);
23965 }
23966 else
23967 type = cp_parser_type_name (parser, /*typename_keyword_p=*/true);
23968
23969 if (TREE_CODE (type) == TYPE_DECL)
23970 type = TREE_TYPE (type);
23971
23972 parser->scope = saved_scope;
23973 parser->object_scope = saved_object_scope;
23974 parser->qualifying_scope = saved_qualifying_scope;
23975
23976 if (type == error_mark_node)
23977 cp_parser_skip_to_end_of_statement (parser);
23978
23979 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
23980 return error_mark_node;
23981 if (type == error_mark_node)
23982 return error_mark_node;
23983
23984 return finish_type_requirement (type);
23985 }
23986
23987 /* Parse a compound requirement
23988
23989 compound-requirement:
23990 '{' expression '}' 'noexcept' [opt] trailing-return-type [opt] ';' */
23991 static tree
23992 cp_parser_compound_requirement (cp_parser *parser)
23993 {
23994 /* Parse an expression enclosed in '{ }'s. */
23995 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
23996 return error_mark_node;
23997
23998 tree expr = cp_parser_expression (parser, NULL, false, false);
23999 if (!expr || expr == error_mark_node)
24000 return error_mark_node;
24001
24002 if (!cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE))
24003 return error_mark_node;
24004
24005 /* Parse the optional noexcept. */
24006 bool noexcept_p = false;
24007 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_NOEXCEPT))
24008 {
24009 cp_lexer_consume_token (parser->lexer);
24010 noexcept_p = true;
24011 }
24012
24013 /* Parse the optional trailing return type. */
24014 tree type = NULL_TREE;
24015 if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
24016 {
24017 cp_lexer_consume_token (parser->lexer);
24018 bool saved_result_type_constraint_p = parser->in_result_type_constraint_p;
24019 parser->in_result_type_constraint_p = true;
24020 type = cp_parser_trailing_type_id (parser);
24021 parser->in_result_type_constraint_p = saved_result_type_constraint_p;
24022 if (type == error_mark_node)
24023 return error_mark_node;
24024 }
24025
24026 return finish_compound_requirement (expr, type, noexcept_p);
24027 }
24028
24029 /* Parse a nested requirement. This is the same as a requires clause.
24030
24031 nested-requirement:
24032 requires-clause */
24033 static tree
24034 cp_parser_nested_requirement (cp_parser *parser)
24035 {
24036 cp_lexer_consume_token (parser->lexer);
24037 tree req = cp_parser_requires_clause (parser);
24038 if (req == error_mark_node)
24039 return error_mark_node;
24040 return finish_nested_requirement (req);
24041 }
24042
24043 /* Support Functions */
24044
24045 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
24046 NAME should have one of the representations used for an
24047 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
24048 is returned. If PARSER->SCOPE is a dependent type, then a
24049 SCOPE_REF is returned.
24050
24051 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
24052 returned; the name was already resolved when the TEMPLATE_ID_EXPR
24053 was formed. Abstractly, such entities should not be passed to this
24054 function, because they do not need to be looked up, but it is
24055 simpler to check for this special case here, rather than at the
24056 call-sites.
24057
24058 In cases not explicitly covered above, this function returns a
24059 DECL, OVERLOAD, or baselink representing the result of the lookup.
24060 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
24061 is returned.
24062
24063 If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
24064 (e.g., "struct") that was used. In that case bindings that do not
24065 refer to types are ignored.
24066
24067 If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
24068 ignored.
24069
24070 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
24071 are ignored.
24072
24073 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
24074 types.
24075
24076 If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
24077 TREE_LIST of candidates if name-lookup results in an ambiguity, and
24078 NULL_TREE otherwise. */
24079
24080 static tree
24081 cp_parser_lookup_name (cp_parser *parser, tree name,
24082 enum tag_types tag_type,
24083 bool is_template,
24084 bool is_namespace,
24085 bool check_dependency,
24086 tree *ambiguous_decls,
24087 location_t name_location)
24088 {
24089 tree decl;
24090 tree object_type = parser->context->object_type;
24091
24092 /* Assume that the lookup will be unambiguous. */
24093 if (ambiguous_decls)
24094 *ambiguous_decls = NULL_TREE;
24095
24096 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
24097 no longer valid. Note that if we are parsing tentatively, and
24098 the parse fails, OBJECT_TYPE will be automatically restored. */
24099 parser->context->object_type = NULL_TREE;
24100
24101 if (name == error_mark_node)
24102 return error_mark_node;
24103
24104 /* A template-id has already been resolved; there is no lookup to
24105 do. */
24106 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
24107 return name;
24108 if (BASELINK_P (name))
24109 {
24110 gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
24111 == TEMPLATE_ID_EXPR);
24112 return name;
24113 }
24114
24115 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
24116 it should already have been checked to make sure that the name
24117 used matches the type being destroyed. */
24118 if (TREE_CODE (name) == BIT_NOT_EXPR)
24119 {
24120 tree type;
24121
24122 /* Figure out to which type this destructor applies. */
24123 if (parser->scope)
24124 type = parser->scope;
24125 else if (object_type)
24126 type = object_type;
24127 else
24128 type = current_class_type;
24129 /* If that's not a class type, there is no destructor. */
24130 if (!type || !CLASS_TYPE_P (type))
24131 return error_mark_node;
24132 if (CLASSTYPE_LAZY_DESTRUCTOR (type))
24133 lazily_declare_fn (sfk_destructor, type);
24134 if (!CLASSTYPE_DESTRUCTORS (type))
24135 return error_mark_node;
24136 /* If it was a class type, return the destructor. */
24137 return CLASSTYPE_DESTRUCTORS (type);
24138 }
24139
24140 /* By this point, the NAME should be an ordinary identifier. If
24141 the id-expression was a qualified name, the qualifying scope is
24142 stored in PARSER->SCOPE at this point. */
24143 gcc_assert (identifier_p (name));
24144
24145 /* Perform the lookup. */
24146 if (parser->scope)
24147 {
24148 bool dependent_p;
24149
24150 if (parser->scope == error_mark_node)
24151 return error_mark_node;
24152
24153 /* If the SCOPE is dependent, the lookup must be deferred until
24154 the template is instantiated -- unless we are explicitly
24155 looking up names in uninstantiated templates. Even then, we
24156 cannot look up the name if the scope is not a class type; it
24157 might, for example, be a template type parameter. */
24158 dependent_p = (TYPE_P (parser->scope)
24159 && dependent_scope_p (parser->scope));
24160 if ((check_dependency || !CLASS_TYPE_P (parser->scope))
24161 && dependent_p)
24162 /* Defer lookup. */
24163 decl = error_mark_node;
24164 else
24165 {
24166 tree pushed_scope = NULL_TREE;
24167
24168 /* If PARSER->SCOPE is a dependent type, then it must be a
24169 class type, and we must not be checking dependencies;
24170 otherwise, we would have processed this lookup above. So
24171 that PARSER->SCOPE is not considered a dependent base by
24172 lookup_member, we must enter the scope here. */
24173 if (dependent_p)
24174 pushed_scope = push_scope (parser->scope);
24175
24176 /* If the PARSER->SCOPE is a template specialization, it
24177 may be instantiated during name lookup. In that case,
24178 errors may be issued. Even if we rollback the current
24179 tentative parse, those errors are valid. */
24180 decl = lookup_qualified_name (parser->scope, name,
24181 tag_type != none_type,
24182 /*complain=*/true);
24183
24184 /* 3.4.3.1: In a lookup in which the constructor is an acceptable
24185 lookup result and the nested-name-specifier nominates a class C:
24186 * if the name specified after the nested-name-specifier, when
24187 looked up in C, is the injected-class-name of C (Clause 9), or
24188 * if the name specified after the nested-name-specifier is the
24189 same as the identifier or the simple-template-id's template-
24190 name in the last component of the nested-name-specifier,
24191 the name is instead considered to name the constructor of
24192 class C. [ Note: for example, the constructor is not an
24193 acceptable lookup result in an elaborated-type-specifier so
24194 the constructor would not be used in place of the
24195 injected-class-name. --end note ] Such a constructor name
24196 shall be used only in the declarator-id of a declaration that
24197 names a constructor or in a using-declaration. */
24198 if (tag_type == none_type
24199 && DECL_SELF_REFERENCE_P (decl)
24200 && same_type_p (DECL_CONTEXT (decl), parser->scope))
24201 decl = lookup_qualified_name (parser->scope, ctor_identifier,
24202 tag_type != none_type,
24203 /*complain=*/true);
24204
24205 /* If we have a single function from a using decl, pull it out. */
24206 if (TREE_CODE (decl) == OVERLOAD
24207 && !really_overloaded_fn (decl))
24208 decl = OVL_FUNCTION (decl);
24209
24210 if (pushed_scope)
24211 pop_scope (pushed_scope);
24212 }
24213
24214 /* If the scope is a dependent type and either we deferred lookup or
24215 we did lookup but didn't find the name, rememeber the name. */
24216 if (decl == error_mark_node && TYPE_P (parser->scope)
24217 && dependent_type_p (parser->scope))
24218 {
24219 if (tag_type)
24220 {
24221 tree type;
24222
24223 /* The resolution to Core Issue 180 says that `struct
24224 A::B' should be considered a type-name, even if `A'
24225 is dependent. */
24226 type = make_typename_type (parser->scope, name, tag_type,
24227 /*complain=*/tf_error);
24228 if (type != error_mark_node)
24229 decl = TYPE_NAME (type);
24230 }
24231 else if (is_template
24232 && (cp_parser_next_token_ends_template_argument_p (parser)
24233 || cp_lexer_next_token_is (parser->lexer,
24234 CPP_CLOSE_PAREN)))
24235 decl = make_unbound_class_template (parser->scope,
24236 name, NULL_TREE,
24237 /*complain=*/tf_error);
24238 else
24239 decl = build_qualified_name (/*type=*/NULL_TREE,
24240 parser->scope, name,
24241 is_template);
24242 }
24243 parser->qualifying_scope = parser->scope;
24244 parser->object_scope = NULL_TREE;
24245 }
24246 else if (object_type)
24247 {
24248 /* Look up the name in the scope of the OBJECT_TYPE, unless the
24249 OBJECT_TYPE is not a class. */
24250 if (CLASS_TYPE_P (object_type))
24251 /* If the OBJECT_TYPE is a template specialization, it may
24252 be instantiated during name lookup. In that case, errors
24253 may be issued. Even if we rollback the current tentative
24254 parse, those errors are valid. */
24255 decl = lookup_member (object_type,
24256 name,
24257 /*protect=*/0,
24258 tag_type != none_type,
24259 tf_warning_or_error);
24260 else
24261 decl = NULL_TREE;
24262
24263 if (!decl)
24264 /* Look it up in the enclosing context. */
24265 decl = lookup_name_real (name, tag_type != none_type,
24266 /*nonclass=*/0,
24267 /*block_p=*/true, is_namespace, 0);
24268 parser->object_scope = object_type;
24269 parser->qualifying_scope = NULL_TREE;
24270 }
24271 else
24272 {
24273 decl = lookup_name_real (name, tag_type != none_type,
24274 /*nonclass=*/0,
24275 /*block_p=*/true, is_namespace, 0);
24276 parser->qualifying_scope = NULL_TREE;
24277 parser->object_scope = NULL_TREE;
24278 }
24279
24280 /* If the lookup failed, let our caller know. */
24281 if (!decl || decl == error_mark_node)
24282 return error_mark_node;
24283
24284 /* Pull out the template from an injected-class-name (or multiple). */
24285 if (is_template)
24286 decl = maybe_get_template_decl_from_type_decl (decl);
24287
24288 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
24289 if (TREE_CODE (decl) == TREE_LIST)
24290 {
24291 if (ambiguous_decls)
24292 *ambiguous_decls = decl;
24293 /* The error message we have to print is too complicated for
24294 cp_parser_error, so we incorporate its actions directly. */
24295 if (!cp_parser_simulate_error (parser))
24296 {
24297 error_at (name_location, "reference to %qD is ambiguous",
24298 name);
24299 print_candidates (decl);
24300 }
24301 return error_mark_node;
24302 }
24303
24304 gcc_assert (DECL_P (decl)
24305 || TREE_CODE (decl) == OVERLOAD
24306 || TREE_CODE (decl) == SCOPE_REF
24307 || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
24308 || BASELINK_P (decl));
24309
24310 /* If we have resolved the name of a member declaration, check to
24311 see if the declaration is accessible. When the name resolves to
24312 set of overloaded functions, accessibility is checked when
24313 overload resolution is done.
24314
24315 During an explicit instantiation, access is not checked at all,
24316 as per [temp.explicit]. */
24317 if (DECL_P (decl))
24318 check_accessibility_of_qualified_id (decl, object_type, parser->scope);
24319
24320 maybe_record_typedef_use (decl);
24321
24322 return decl;
24323 }
24324
24325 /* Like cp_parser_lookup_name, but for use in the typical case where
24326 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
24327 IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE. */
24328
24329 static tree
24330 cp_parser_lookup_name_simple (cp_parser* parser, tree name, location_t location)
24331 {
24332 return cp_parser_lookup_name (parser, name,
24333 none_type,
24334 /*is_template=*/false,
24335 /*is_namespace=*/false,
24336 /*check_dependency=*/true,
24337 /*ambiguous_decls=*/NULL,
24338 location);
24339 }
24340
24341 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
24342 the current context, return the TYPE_DECL. If TAG_NAME_P is
24343 true, the DECL indicates the class being defined in a class-head,
24344 or declared in an elaborated-type-specifier.
24345
24346 Otherwise, return DECL. */
24347
24348 static tree
24349 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
24350 {
24351 /* If the TEMPLATE_DECL is being declared as part of a class-head,
24352 the translation from TEMPLATE_DECL to TYPE_DECL occurs:
24353
24354 struct A {
24355 template <typename T> struct B;
24356 };
24357
24358 template <typename T> struct A::B {};
24359
24360 Similarly, in an elaborated-type-specifier:
24361
24362 namespace N { struct X{}; }
24363
24364 struct A {
24365 template <typename T> friend struct N::X;
24366 };
24367
24368 However, if the DECL refers to a class type, and we are in
24369 the scope of the class, then the name lookup automatically
24370 finds the TYPE_DECL created by build_self_reference rather
24371 than a TEMPLATE_DECL. For example, in:
24372
24373 template <class T> struct S {
24374 S s;
24375 };
24376
24377 there is no need to handle such case. */
24378
24379 if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
24380 return DECL_TEMPLATE_RESULT (decl);
24381
24382 return decl;
24383 }
24384
24385 /* If too many, or too few, template-parameter lists apply to the
24386 declarator, issue an error message. Returns TRUE if all went well,
24387 and FALSE otherwise. */
24388
24389 static bool
24390 cp_parser_check_declarator_template_parameters (cp_parser* parser,
24391 cp_declarator *declarator,
24392 location_t declarator_location)
24393 {
24394 switch (declarator->kind)
24395 {
24396 case cdk_id:
24397 {
24398 unsigned num_templates = 0;
24399 tree scope = declarator->u.id.qualifying_scope;
24400
24401 if (scope)
24402 num_templates = num_template_headers_for_class (scope);
24403 else if (TREE_CODE (declarator->u.id.unqualified_name)
24404 == TEMPLATE_ID_EXPR)
24405 /* If the DECLARATOR has the form `X<y>' then it uses one
24406 additional level of template parameters. */
24407 ++num_templates;
24408
24409 return cp_parser_check_template_parameters
24410 (parser, num_templates, declarator_location, declarator);
24411 }
24412
24413 case cdk_function:
24414 case cdk_array:
24415 case cdk_pointer:
24416 case cdk_reference:
24417 case cdk_ptrmem:
24418 return (cp_parser_check_declarator_template_parameters
24419 (parser, declarator->declarator, declarator_location));
24420
24421 case cdk_error:
24422 return true;
24423
24424 default:
24425 gcc_unreachable ();
24426 }
24427 return false;
24428 }
24429
24430 /* NUM_TEMPLATES were used in the current declaration. If that is
24431 invalid, return FALSE and issue an error messages. Otherwise,
24432 return TRUE. If DECLARATOR is non-NULL, then we are checking a
24433 declarator and we can print more accurate diagnostics. */
24434
24435 static bool
24436 cp_parser_check_template_parameters (cp_parser* parser,
24437 unsigned num_templates,
24438 location_t location,
24439 cp_declarator *declarator)
24440 {
24441 /* If there are the same number of template classes and parameter
24442 lists, that's OK. */
24443 if (parser->num_template_parameter_lists == num_templates)
24444 return true;
24445 /* If there are more, but only one more, then we are referring to a
24446 member template. That's OK too. */
24447 if (parser->num_template_parameter_lists == num_templates + 1)
24448 return true;
24449 /* If there are more template classes than parameter lists, we have
24450 something like:
24451
24452 template <class T> void S<T>::R<T>::f (); */
24453 if (parser->num_template_parameter_lists < num_templates)
24454 {
24455 if (declarator && !current_function_decl)
24456 error_at (location, "specializing member %<%T::%E%> "
24457 "requires %<template<>%> syntax",
24458 declarator->u.id.qualifying_scope,
24459 declarator->u.id.unqualified_name);
24460 else if (declarator)
24461 error_at (location, "invalid declaration of %<%T::%E%>",
24462 declarator->u.id.qualifying_scope,
24463 declarator->u.id.unqualified_name);
24464 else
24465 error_at (location, "too few template-parameter-lists");
24466 return false;
24467 }
24468 /* Otherwise, there are too many template parameter lists. We have
24469 something like:
24470
24471 template <class T> template <class U> void S::f(); */
24472 error_at (location, "too many template-parameter-lists");
24473 return false;
24474 }
24475
24476 /* Parse an optional `::' token indicating that the following name is
24477 from the global namespace. If so, PARSER->SCOPE is set to the
24478 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
24479 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
24480 Returns the new value of PARSER->SCOPE, if the `::' token is
24481 present, and NULL_TREE otherwise. */
24482
24483 static tree
24484 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
24485 {
24486 cp_token *token;
24487
24488 /* Peek at the next token. */
24489 token = cp_lexer_peek_token (parser->lexer);
24490 /* If we're looking at a `::' token then we're starting from the
24491 global namespace, not our current location. */
24492 if (token->type == CPP_SCOPE)
24493 {
24494 /* Consume the `::' token. */
24495 cp_lexer_consume_token (parser->lexer);
24496 /* Set the SCOPE so that we know where to start the lookup. */
24497 parser->scope = global_namespace;
24498 parser->qualifying_scope = global_namespace;
24499 parser->object_scope = NULL_TREE;
24500
24501 return parser->scope;
24502 }
24503 else if (!current_scope_valid_p)
24504 {
24505 parser->scope = NULL_TREE;
24506 parser->qualifying_scope = NULL_TREE;
24507 parser->object_scope = NULL_TREE;
24508 }
24509
24510 return NULL_TREE;
24511 }
24512
24513 /* Returns TRUE if the upcoming token sequence is the start of a
24514 constructor declarator. If FRIEND_P is true, the declarator is
24515 preceded by the `friend' specifier. */
24516
24517 static bool
24518 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
24519 {
24520 bool constructor_p;
24521 bool outside_class_specifier_p;
24522 tree nested_name_specifier;
24523 cp_token *next_token;
24524
24525 /* The common case is that this is not a constructor declarator, so
24526 try to avoid doing lots of work if at all possible. It's not
24527 valid declare a constructor at function scope. */
24528 if (parser->in_function_body)
24529 return false;
24530 /* And only certain tokens can begin a constructor declarator. */
24531 next_token = cp_lexer_peek_token (parser->lexer);
24532 if (next_token->type != CPP_NAME
24533 && next_token->type != CPP_SCOPE
24534 && next_token->type != CPP_NESTED_NAME_SPECIFIER
24535 && next_token->type != CPP_TEMPLATE_ID)
24536 return false;
24537
24538 /* Parse tentatively; we are going to roll back all of the tokens
24539 consumed here. */
24540 cp_parser_parse_tentatively (parser);
24541 /* Assume that we are looking at a constructor declarator. */
24542 constructor_p = true;
24543
24544 /* Look for the optional `::' operator. */
24545 cp_parser_global_scope_opt (parser,
24546 /*current_scope_valid_p=*/false);
24547 /* Look for the nested-name-specifier. */
24548 nested_name_specifier
24549 = (cp_parser_nested_name_specifier_opt (parser,
24550 /*typename_keyword_p=*/false,
24551 /*check_dependency_p=*/false,
24552 /*type_p=*/false,
24553 /*is_declaration=*/false));
24554
24555 outside_class_specifier_p = (!at_class_scope_p ()
24556 || !TYPE_BEING_DEFINED (current_class_type)
24557 || friend_p);
24558
24559 /* Outside of a class-specifier, there must be a
24560 nested-name-specifier. */
24561 if (!nested_name_specifier && outside_class_specifier_p)
24562 constructor_p = false;
24563 else if (nested_name_specifier == error_mark_node)
24564 constructor_p = false;
24565
24566 /* If we have a class scope, this is easy; DR 147 says that S::S always
24567 names the constructor, and no other qualified name could. */
24568 if (constructor_p && nested_name_specifier
24569 && CLASS_TYPE_P (nested_name_specifier))
24570 {
24571 tree id = cp_parser_unqualified_id (parser,
24572 /*template_keyword_p=*/false,
24573 /*check_dependency_p=*/false,
24574 /*declarator_p=*/true,
24575 /*optional_p=*/false);
24576 if (is_overloaded_fn (id))
24577 id = DECL_NAME (get_first_fn (id));
24578 if (!constructor_name_p (id, nested_name_specifier))
24579 constructor_p = false;
24580 }
24581 /* If we still think that this might be a constructor-declarator,
24582 look for a class-name. */
24583 else if (constructor_p)
24584 {
24585 /* If we have:
24586
24587 template <typename T> struct S {
24588 S();
24589 };
24590
24591 we must recognize that the nested `S' names a class. */
24592 tree type_decl;
24593 type_decl = cp_parser_class_name (parser,
24594 /*typename_keyword_p=*/false,
24595 /*template_keyword_p=*/false,
24596 none_type,
24597 /*check_dependency_p=*/false,
24598 /*class_head_p=*/false,
24599 /*is_declaration=*/false);
24600 /* If there was no class-name, then this is not a constructor.
24601 Otherwise, if we are in a class-specifier and we aren't
24602 handling a friend declaration, check that its type matches
24603 current_class_type (c++/38313). Note: error_mark_node
24604 is left alone for error recovery purposes. */
24605 constructor_p = (!cp_parser_error_occurred (parser)
24606 && (outside_class_specifier_p
24607 || type_decl == error_mark_node
24608 || same_type_p (current_class_type,
24609 TREE_TYPE (type_decl))));
24610
24611 /* If we're still considering a constructor, we have to see a `(',
24612 to begin the parameter-declaration-clause, followed by either a
24613 `)', an `...', or a decl-specifier. We need to check for a
24614 type-specifier to avoid being fooled into thinking that:
24615
24616 S (f) (int);
24617
24618 is a constructor. (It is actually a function named `f' that
24619 takes one parameter (of type `int') and returns a value of type
24620 `S'. */
24621 if (constructor_p
24622 && !cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
24623 constructor_p = false;
24624
24625 if (constructor_p
24626 && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
24627 && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
24628 /* A parameter declaration begins with a decl-specifier,
24629 which is either the "attribute" keyword, a storage class
24630 specifier, or (usually) a type-specifier. */
24631 && !cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer))
24632 {
24633 tree type;
24634 tree pushed_scope = NULL_TREE;
24635 unsigned saved_num_template_parameter_lists;
24636
24637 /* Names appearing in the type-specifier should be looked up
24638 in the scope of the class. */
24639 if (current_class_type)
24640 type = NULL_TREE;
24641 else
24642 {
24643 type = TREE_TYPE (type_decl);
24644 if (TREE_CODE (type) == TYPENAME_TYPE)
24645 {
24646 type = resolve_typename_type (type,
24647 /*only_current_p=*/false);
24648 if (TREE_CODE (type) == TYPENAME_TYPE)
24649 {
24650 cp_parser_abort_tentative_parse (parser);
24651 return false;
24652 }
24653 }
24654 pushed_scope = push_scope (type);
24655 }
24656
24657 /* Inside the constructor parameter list, surrounding
24658 template-parameter-lists do not apply. */
24659 saved_num_template_parameter_lists
24660 = parser->num_template_parameter_lists;
24661 parser->num_template_parameter_lists = 0;
24662
24663 /* Look for the type-specifier. */
24664 cp_parser_type_specifier (parser,
24665 CP_PARSER_FLAGS_NONE,
24666 /*decl_specs=*/NULL,
24667 /*is_declarator=*/true,
24668 /*declares_class_or_enum=*/NULL,
24669 /*is_cv_qualifier=*/NULL);
24670
24671 parser->num_template_parameter_lists
24672 = saved_num_template_parameter_lists;
24673
24674 /* Leave the scope of the class. */
24675 if (pushed_scope)
24676 pop_scope (pushed_scope);
24677
24678 constructor_p = !cp_parser_error_occurred (parser);
24679 }
24680 }
24681
24682 /* We did not really want to consume any tokens. */
24683 cp_parser_abort_tentative_parse (parser);
24684
24685 return constructor_p;
24686 }
24687
24688 /* Parse the definition of the function given by the DECL_SPECIFIERS,
24689 ATTRIBUTES, and DECLARATOR. The access checks have been deferred;
24690 they must be performed once we are in the scope of the function.
24691
24692 Returns the function defined. */
24693
24694 static tree
24695 cp_parser_function_definition_from_specifiers_and_declarator
24696 (cp_parser* parser,
24697 cp_decl_specifier_seq *decl_specifiers,
24698 tree attributes,
24699 const cp_declarator *declarator)
24700 {
24701 tree fn;
24702 bool success_p;
24703
24704 /* Begin the function-definition. */
24705 success_p = start_function (decl_specifiers, declarator, attributes);
24706
24707 /* The things we're about to see are not directly qualified by any
24708 template headers we've seen thus far. */
24709 reset_specialization ();
24710
24711 /* If there were names looked up in the decl-specifier-seq that we
24712 did not check, check them now. We must wait until we are in the
24713 scope of the function to perform the checks, since the function
24714 might be a friend. */
24715 perform_deferred_access_checks (tf_warning_or_error);
24716
24717 if (success_p)
24718 {
24719 cp_finalize_omp_declare_simd (parser, current_function_decl);
24720 parser->omp_declare_simd = NULL;
24721 cp_finalize_oacc_routine (parser, current_function_decl, true);
24722 parser->oacc_routine = NULL;
24723 }
24724
24725 if (!success_p)
24726 {
24727 /* Skip the entire function. */
24728 cp_parser_skip_to_end_of_block_or_statement (parser);
24729 fn = error_mark_node;
24730 }
24731 else if (DECL_INITIAL (current_function_decl) != error_mark_node)
24732 {
24733 /* Seen already, skip it. An error message has already been output. */
24734 cp_parser_skip_to_end_of_block_or_statement (parser);
24735 fn = current_function_decl;
24736 current_function_decl = NULL_TREE;
24737 /* If this is a function from a class, pop the nested class. */
24738 if (current_class_name)
24739 pop_nested_class ();
24740 }
24741 else
24742 {
24743 timevar_id_t tv;
24744 if (DECL_DECLARED_INLINE_P (current_function_decl))
24745 tv = TV_PARSE_INLINE;
24746 else
24747 tv = TV_PARSE_FUNC;
24748 timevar_push (tv);
24749 fn = cp_parser_function_definition_after_declarator (parser,
24750 /*inline_p=*/false);
24751 timevar_pop (tv);
24752 }
24753
24754 return fn;
24755 }
24756
24757 /* Parse the part of a function-definition that follows the
24758 declarator. INLINE_P is TRUE iff this function is an inline
24759 function defined within a class-specifier.
24760
24761 Returns the function defined. */
24762
24763 static tree
24764 cp_parser_function_definition_after_declarator (cp_parser* parser,
24765 bool inline_p)
24766 {
24767 tree fn;
24768 bool ctor_initializer_p = false;
24769 bool saved_in_unbraced_linkage_specification_p;
24770 bool saved_in_function_body;
24771 unsigned saved_num_template_parameter_lists;
24772 cp_token *token;
24773 bool fully_implicit_function_template_p
24774 = parser->fully_implicit_function_template_p;
24775 parser->fully_implicit_function_template_p = false;
24776 tree implicit_template_parms
24777 = parser->implicit_template_parms;
24778 parser->implicit_template_parms = 0;
24779 cp_binding_level* implicit_template_scope
24780 = parser->implicit_template_scope;
24781 parser->implicit_template_scope = 0;
24782
24783 saved_in_function_body = parser->in_function_body;
24784 parser->in_function_body = true;
24785 /* If the next token is `return', then the code may be trying to
24786 make use of the "named return value" extension that G++ used to
24787 support. */
24788 token = cp_lexer_peek_token (parser->lexer);
24789 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
24790 {
24791 /* Consume the `return' keyword. */
24792 cp_lexer_consume_token (parser->lexer);
24793 /* Look for the identifier that indicates what value is to be
24794 returned. */
24795 cp_parser_identifier (parser);
24796 /* Issue an error message. */
24797 error_at (token->location,
24798 "named return values are no longer supported");
24799 /* Skip tokens until we reach the start of the function body. */
24800 while (true)
24801 {
24802 cp_token *token = cp_lexer_peek_token (parser->lexer);
24803 if (token->type == CPP_OPEN_BRACE
24804 || token->type == CPP_EOF
24805 || token->type == CPP_PRAGMA_EOL)
24806 break;
24807 cp_lexer_consume_token (parser->lexer);
24808 }
24809 }
24810 /* The `extern' in `extern "C" void f () { ... }' does not apply to
24811 anything declared inside `f'. */
24812 saved_in_unbraced_linkage_specification_p
24813 = parser->in_unbraced_linkage_specification_p;
24814 parser->in_unbraced_linkage_specification_p = false;
24815 /* Inside the function, surrounding template-parameter-lists do not
24816 apply. */
24817 saved_num_template_parameter_lists
24818 = parser->num_template_parameter_lists;
24819 parser->num_template_parameter_lists = 0;
24820
24821 start_lambda_scope (current_function_decl);
24822
24823 /* If the next token is `try', `__transaction_atomic', or
24824 `__transaction_relaxed`, then we are looking at either function-try-block
24825 or function-transaction-block. Note that all of these include the
24826 function-body. */
24827 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRANSACTION_ATOMIC))
24828 ctor_initializer_p = cp_parser_function_transaction (parser,
24829 RID_TRANSACTION_ATOMIC);
24830 else if (cp_lexer_next_token_is_keyword (parser->lexer,
24831 RID_TRANSACTION_RELAXED))
24832 ctor_initializer_p = cp_parser_function_transaction (parser,
24833 RID_TRANSACTION_RELAXED);
24834 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
24835 ctor_initializer_p = cp_parser_function_try_block (parser);
24836 else
24837 ctor_initializer_p = cp_parser_ctor_initializer_opt_and_function_body
24838 (parser, /*in_function_try_block=*/false);
24839
24840 finish_lambda_scope ();
24841
24842 /* Finish the function. */
24843 fn = finish_function ((ctor_initializer_p ? 1 : 0) |
24844 (inline_p ? 2 : 0));
24845 /* Generate code for it, if necessary. */
24846 expand_or_defer_fn (fn);
24847 /* Restore the saved values. */
24848 parser->in_unbraced_linkage_specification_p
24849 = saved_in_unbraced_linkage_specification_p;
24850 parser->num_template_parameter_lists
24851 = saved_num_template_parameter_lists;
24852 parser->in_function_body = saved_in_function_body;
24853
24854 parser->fully_implicit_function_template_p
24855 = fully_implicit_function_template_p;
24856 parser->implicit_template_parms
24857 = implicit_template_parms;
24858 parser->implicit_template_scope
24859 = implicit_template_scope;
24860
24861 if (parser->fully_implicit_function_template_p)
24862 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
24863
24864 return fn;
24865 }
24866
24867 /* Parse a template-declaration body (following argument list). */
24868
24869 static void
24870 cp_parser_template_declaration_after_parameters (cp_parser* parser,
24871 tree parameter_list,
24872 bool member_p)
24873 {
24874 tree decl = NULL_TREE;
24875 bool friend_p = false;
24876
24877 /* We just processed one more parameter list. */
24878 ++parser->num_template_parameter_lists;
24879
24880 /* Get the deferred access checks from the parameter list. These
24881 will be checked once we know what is being declared, as for a
24882 member template the checks must be performed in the scope of the
24883 class containing the member. */
24884 vec<deferred_access_check, va_gc> *checks = get_deferred_access_checks ();
24885
24886 /* Tentatively parse for a new template parameter list, which can either be
24887 the template keyword or a template introduction. */
24888 if (cp_parser_template_declaration_after_export (parser, member_p))
24889 /* OK */;
24890 else if (cxx_dialect >= cxx11
24891 && cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
24892 decl = cp_parser_alias_declaration (parser);
24893 else
24894 {
24895 /* There are no access checks when parsing a template, as we do not
24896 know if a specialization will be a friend. */
24897 push_deferring_access_checks (dk_no_check);
24898 cp_token *token = cp_lexer_peek_token (parser->lexer);
24899 decl = cp_parser_single_declaration (parser,
24900 checks,
24901 member_p,
24902 /*explicit_specialization_p=*/false,
24903 &friend_p);
24904 pop_deferring_access_checks ();
24905
24906 /* If this is a member template declaration, let the front
24907 end know. */
24908 if (member_p && !friend_p && decl)
24909 {
24910 if (TREE_CODE (decl) == TYPE_DECL)
24911 cp_parser_check_access_in_redeclaration (decl, token->location);
24912
24913 decl = finish_member_template_decl (decl);
24914 }
24915 else if (friend_p && decl
24916 && DECL_DECLARES_TYPE_P (decl))
24917 make_friend_class (current_class_type, TREE_TYPE (decl),
24918 /*complain=*/true);
24919 }
24920 /* We are done with the current parameter list. */
24921 --parser->num_template_parameter_lists;
24922
24923 pop_deferring_access_checks ();
24924
24925 /* Finish up. */
24926 finish_template_decl (parameter_list);
24927
24928 /* Check the template arguments for a literal operator template. */
24929 if (decl
24930 && DECL_DECLARES_FUNCTION_P (decl)
24931 && UDLIT_OPER_P (DECL_NAME (decl)))
24932 {
24933 bool ok = true;
24934 if (parameter_list == NULL_TREE)
24935 ok = false;
24936 else
24937 {
24938 int num_parms = TREE_VEC_LENGTH (parameter_list);
24939 if (num_parms == 1)
24940 {
24941 tree parm_list = TREE_VEC_ELT (parameter_list, 0);
24942 tree parm = INNERMOST_TEMPLATE_PARMS (parm_list);
24943 if (TREE_TYPE (parm) != char_type_node
24944 || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
24945 ok = false;
24946 }
24947 else if (num_parms == 2 && cxx_dialect >= cxx14)
24948 {
24949 tree parm_type = TREE_VEC_ELT (parameter_list, 0);
24950 tree type = INNERMOST_TEMPLATE_PARMS (parm_type);
24951 tree parm_list = TREE_VEC_ELT (parameter_list, 1);
24952 tree parm = INNERMOST_TEMPLATE_PARMS (parm_list);
24953 if (TREE_TYPE (parm) != TREE_TYPE (type)
24954 || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
24955 ok = false;
24956 }
24957 else
24958 ok = false;
24959 }
24960 if (!ok)
24961 {
24962 if (cxx_dialect >= cxx14)
24963 error ("literal operator template %qD has invalid parameter list."
24964 " Expected non-type template argument pack <char...>"
24965 " or <typename CharT, CharT...>",
24966 decl);
24967 else
24968 error ("literal operator template %qD has invalid parameter list."
24969 " Expected non-type template argument pack <char...>",
24970 decl);
24971 }
24972 }
24973
24974 /* Register member declarations. */
24975 if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
24976 finish_member_declaration (decl);
24977 /* If DECL is a function template, we must return to parse it later.
24978 (Even though there is no definition, there might be default
24979 arguments that need handling.) */
24980 if (member_p && decl
24981 && DECL_DECLARES_FUNCTION_P (decl))
24982 vec_safe_push (unparsed_funs_with_definitions, decl);
24983 }
24984
24985 /* Parse a template introduction header for a template-declaration. Returns
24986 false if tentative parse fails. */
24987
24988 static bool
24989 cp_parser_template_introduction (cp_parser* parser, bool member_p)
24990 {
24991 cp_parser_parse_tentatively (parser);
24992
24993 tree saved_scope = parser->scope;
24994 tree saved_object_scope = parser->object_scope;
24995 tree saved_qualifying_scope = parser->qualifying_scope;
24996
24997 /* Look for the optional `::' operator. */
24998 cp_parser_global_scope_opt (parser,
24999 /*current_scope_valid_p=*/false);
25000 /* Look for the nested-name-specifier. */
25001 cp_parser_nested_name_specifier_opt (parser,
25002 /*typename_keyword_p=*/false,
25003 /*check_dependency_p=*/true,
25004 /*type_p=*/false,
25005 /*is_declaration=*/false);
25006
25007 cp_token *token = cp_lexer_peek_token (parser->lexer);
25008 tree concept_name = cp_parser_identifier (parser);
25009
25010 /* Look up the concept for which we will be matching
25011 template parameters. */
25012 tree tmpl_decl = cp_parser_lookup_name_simple (parser, concept_name,
25013 token->location);
25014 parser->scope = saved_scope;
25015 parser->object_scope = saved_object_scope;
25016 parser->qualifying_scope = saved_qualifying_scope;
25017
25018 if (concept_name == error_mark_node)
25019 cp_parser_simulate_error (parser);
25020
25021 /* Look for opening brace for introduction. */
25022 cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
25023
25024 if (!cp_parser_parse_definitely (parser))
25025 return false;
25026
25027 push_deferring_access_checks (dk_deferred);
25028
25029 /* Build vector of placeholder parameters and grab
25030 matching identifiers. */
25031 tree introduction_list = cp_parser_introduction_list (parser);
25032
25033 /* The introduction-list shall not be empty. */
25034 int nargs = TREE_VEC_LENGTH (introduction_list);
25035 if (nargs == 0)
25036 {
25037 error ("empty introduction-list");
25038 return true;
25039 }
25040
25041 /* Look for closing brace for introduction. */
25042 if (!cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE))
25043 return true;
25044
25045 if (tmpl_decl == error_mark_node)
25046 {
25047 cp_parser_name_lookup_error (parser, concept_name, tmpl_decl, NLE_NULL,
25048 token->location);
25049 return true;
25050 }
25051
25052 /* Build and associate the constraint. */
25053 tree parms = finish_template_introduction (tmpl_decl, introduction_list);
25054 if (parms && parms != error_mark_node)
25055 {
25056 cp_parser_template_declaration_after_parameters (parser, parms,
25057 member_p);
25058 return true;
25059 }
25060
25061 error_at (token->location, "no matching concept for template-introduction");
25062 return true;
25063 }
25064
25065 /* Parse a normal template-declaration following the template keyword. */
25066
25067 static void
25068 cp_parser_explicit_template_declaration (cp_parser* parser, bool member_p)
25069 {
25070 tree parameter_list;
25071 bool need_lang_pop;
25072 location_t location = input_location;
25073
25074 /* Look for the `<' token. */
25075 if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
25076 return;
25077 if (at_class_scope_p () && current_function_decl)
25078 {
25079 /* 14.5.2.2 [temp.mem]
25080
25081 A local class shall not have member templates. */
25082 error_at (location,
25083 "invalid declaration of member template in local class");
25084 cp_parser_skip_to_end_of_block_or_statement (parser);
25085 return;
25086 }
25087 /* [temp]
25088
25089 A template ... shall not have C linkage. */
25090 if (current_lang_name == lang_name_c)
25091 {
25092 error_at (location, "template with C linkage");
25093 /* Give it C++ linkage to avoid confusing other parts of the
25094 front end. */
25095 push_lang_context (lang_name_cplusplus);
25096 need_lang_pop = true;
25097 }
25098 else
25099 need_lang_pop = false;
25100
25101 /* We cannot perform access checks on the template parameter
25102 declarations until we know what is being declared, just as we
25103 cannot check the decl-specifier list. */
25104 push_deferring_access_checks (dk_deferred);
25105
25106 /* If the next token is `>', then we have an invalid
25107 specialization. Rather than complain about an invalid template
25108 parameter, issue an error message here. */
25109 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
25110 {
25111 cp_parser_error (parser, "invalid explicit specialization");
25112 begin_specialization ();
25113 parameter_list = NULL_TREE;
25114 }
25115 else
25116 {
25117 /* Parse the template parameters. */
25118 parameter_list = cp_parser_template_parameter_list (parser);
25119 }
25120
25121 /* Look for the `>'. */
25122 cp_parser_skip_to_end_of_template_parameter_list (parser);
25123
25124 /* Manage template requirements */
25125 if (flag_concepts)
25126 {
25127 tree reqs = get_shorthand_constraints (current_template_parms);
25128 if (tree r = cp_parser_requires_clause_opt (parser))
25129 reqs = conjoin_constraints (reqs, make_predicate_constraint (r));
25130 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) = reqs;
25131 }
25132
25133 cp_parser_template_declaration_after_parameters (parser, parameter_list,
25134 member_p);
25135
25136 /* For the erroneous case of a template with C linkage, we pushed an
25137 implicit C++ linkage scope; exit that scope now. */
25138 if (need_lang_pop)
25139 pop_lang_context ();
25140 }
25141
25142 /* Parse a template-declaration, assuming that the `export' (and
25143 `extern') keywords, if present, has already been scanned. MEMBER_P
25144 is as for cp_parser_template_declaration. */
25145
25146 static bool
25147 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
25148 {
25149 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
25150 {
25151 cp_lexer_consume_token (parser->lexer);
25152 cp_parser_explicit_template_declaration (parser, member_p);
25153 return true;
25154 }
25155 else if (flag_concepts)
25156 return cp_parser_template_introduction (parser, member_p);
25157
25158 return false;
25159 }
25160
25161 /* Perform the deferred access checks from a template-parameter-list.
25162 CHECKS is a TREE_LIST of access checks, as returned by
25163 get_deferred_access_checks. */
25164
25165 static void
25166 cp_parser_perform_template_parameter_access_checks (vec<deferred_access_check, va_gc> *checks)
25167 {
25168 ++processing_template_parmlist;
25169 perform_access_checks (checks, tf_warning_or_error);
25170 --processing_template_parmlist;
25171 }
25172
25173 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
25174 `function-definition' sequence that follows a template header.
25175 If MEMBER_P is true, this declaration appears in a class scope.
25176
25177 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
25178 *FRIEND_P is set to TRUE iff the declaration is a friend. */
25179
25180 static tree
25181 cp_parser_single_declaration (cp_parser* parser,
25182 vec<deferred_access_check, va_gc> *checks,
25183 bool member_p,
25184 bool explicit_specialization_p,
25185 bool* friend_p)
25186 {
25187 int declares_class_or_enum;
25188 tree decl = NULL_TREE;
25189 cp_decl_specifier_seq decl_specifiers;
25190 bool function_definition_p = false;
25191 cp_token *decl_spec_token_start;
25192
25193 /* This function is only used when processing a template
25194 declaration. */
25195 gcc_assert (innermost_scope_kind () == sk_template_parms
25196 || innermost_scope_kind () == sk_template_spec);
25197
25198 /* Defer access checks until we know what is being declared. */
25199 push_deferring_access_checks (dk_deferred);
25200
25201 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
25202 alternative. */
25203 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
25204 cp_parser_decl_specifier_seq (parser,
25205 CP_PARSER_FLAGS_OPTIONAL,
25206 &decl_specifiers,
25207 &declares_class_or_enum);
25208 if (friend_p)
25209 *friend_p = cp_parser_friend_p (&decl_specifiers);
25210
25211 /* There are no template typedefs. */
25212 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_typedef))
25213 {
25214 error_at (decl_spec_token_start->location,
25215 "template declaration of %<typedef%>");
25216 decl = error_mark_node;
25217 }
25218
25219 /* Gather up the access checks that occurred the
25220 decl-specifier-seq. */
25221 stop_deferring_access_checks ();
25222
25223 /* Check for the declaration of a template class. */
25224 if (declares_class_or_enum)
25225 {
25226 if (cp_parser_declares_only_class_p (parser))
25227 {
25228 // If this is a declaration, but not a definition, associate
25229 // any constraints with the type declaration. Constraints
25230 // are associated with definitions in cp_parser_class_specifier.
25231 if (declares_class_or_enum == 1)
25232 associate_classtype_constraints (decl_specifiers.type);
25233
25234 decl = shadow_tag (&decl_specifiers);
25235
25236 /* In this case:
25237
25238 struct C {
25239 friend template <typename T> struct A<T>::B;
25240 };
25241
25242 A<T>::B will be represented by a TYPENAME_TYPE, and
25243 therefore not recognized by shadow_tag. */
25244 if (friend_p && *friend_p
25245 && !decl
25246 && decl_specifiers.type
25247 && TYPE_P (decl_specifiers.type))
25248 decl = decl_specifiers.type;
25249
25250 if (decl && decl != error_mark_node)
25251 decl = TYPE_NAME (decl);
25252 else
25253 decl = error_mark_node;
25254
25255 /* Perform access checks for template parameters. */
25256 cp_parser_perform_template_parameter_access_checks (checks);
25257 }
25258 }
25259
25260 /* Complain about missing 'typename' or other invalid type names. */
25261 if (!decl_specifiers.any_type_specifiers_p
25262 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
25263 {
25264 /* cp_parser_parse_and_diagnose_invalid_type_name calls
25265 cp_parser_skip_to_end_of_block_or_statement, so don't try to parse
25266 the rest of this declaration. */
25267 decl = error_mark_node;
25268 goto out;
25269 }
25270
25271 /* If it's not a template class, try for a template function. If
25272 the next token is a `;', then this declaration does not declare
25273 anything. But, if there were errors in the decl-specifiers, then
25274 the error might well have come from an attempted class-specifier.
25275 In that case, there's no need to warn about a missing declarator. */
25276 if (!decl
25277 && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
25278 || decl_specifiers.type != error_mark_node))
25279 {
25280 decl = cp_parser_init_declarator (parser,
25281 &decl_specifiers,
25282 checks,
25283 /*function_definition_allowed_p=*/true,
25284 member_p,
25285 declares_class_or_enum,
25286 &function_definition_p,
25287 NULL, NULL);
25288
25289 /* 7.1.1-1 [dcl.stc]
25290
25291 A storage-class-specifier shall not be specified in an explicit
25292 specialization... */
25293 if (decl
25294 && explicit_specialization_p
25295 && decl_specifiers.storage_class != sc_none)
25296 {
25297 error_at (decl_spec_token_start->location,
25298 "explicit template specialization cannot have a storage class");
25299 decl = error_mark_node;
25300 }
25301
25302 if (decl && VAR_P (decl))
25303 check_template_variable (decl);
25304 }
25305
25306 /* Look for a trailing `;' after the declaration. */
25307 if (!function_definition_p
25308 && (decl == error_mark_node
25309 || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON)))
25310 cp_parser_skip_to_end_of_block_or_statement (parser);
25311
25312 out:
25313 pop_deferring_access_checks ();
25314
25315 /* Clear any current qualification; whatever comes next is the start
25316 of something new. */
25317 parser->scope = NULL_TREE;
25318 parser->qualifying_scope = NULL_TREE;
25319 parser->object_scope = NULL_TREE;
25320
25321 return decl;
25322 }
25323
25324 /* Parse a cast-expression that is not the operand of a unary "&". */
25325
25326 static tree
25327 cp_parser_simple_cast_expression (cp_parser *parser)
25328 {
25329 return cp_parser_cast_expression (parser, /*address_p=*/false,
25330 /*cast_p=*/false, /*decltype*/false, NULL);
25331 }
25332
25333 /* Parse a functional cast to TYPE. Returns an expression
25334 representing the cast. */
25335
25336 static tree
25337 cp_parser_functional_cast (cp_parser* parser, tree type)
25338 {
25339 vec<tree, va_gc> *vec;
25340 tree expression_list;
25341 tree cast;
25342 bool nonconst_p;
25343
25344 if (!type)
25345 type = error_mark_node;
25346
25347 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
25348 {
25349 cp_lexer_set_source_position (parser->lexer);
25350 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
25351 expression_list = cp_parser_braced_list (parser, &nonconst_p);
25352 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
25353 if (TREE_CODE (type) == TYPE_DECL)
25354 type = TREE_TYPE (type);
25355 return finish_compound_literal (type, expression_list,
25356 tf_warning_or_error);
25357 }
25358
25359
25360 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
25361 /*cast_p=*/true,
25362 /*allow_expansion_p=*/true,
25363 /*non_constant_p=*/NULL);
25364 if (vec == NULL)
25365 expression_list = error_mark_node;
25366 else
25367 {
25368 expression_list = build_tree_list_vec (vec);
25369 release_tree_vector (vec);
25370 }
25371
25372 cast = build_functional_cast (type, expression_list,
25373 tf_warning_or_error);
25374 /* [expr.const]/1: In an integral constant expression "only type
25375 conversions to integral or enumeration type can be used". */
25376 if (TREE_CODE (type) == TYPE_DECL)
25377 type = TREE_TYPE (type);
25378 if (cast != error_mark_node
25379 && !cast_valid_in_integral_constant_expression_p (type)
25380 && cp_parser_non_integral_constant_expression (parser,
25381 NIC_CONSTRUCTOR))
25382 return error_mark_node;
25383 return cast;
25384 }
25385
25386 /* Save the tokens that make up the body of a member function defined
25387 in a class-specifier. The DECL_SPECIFIERS and DECLARATOR have
25388 already been parsed. The ATTRIBUTES are any GNU "__attribute__"
25389 specifiers applied to the declaration. Returns the FUNCTION_DECL
25390 for the member function. */
25391
25392 static tree
25393 cp_parser_save_member_function_body (cp_parser* parser,
25394 cp_decl_specifier_seq *decl_specifiers,
25395 cp_declarator *declarator,
25396 tree attributes)
25397 {
25398 cp_token *first;
25399 cp_token *last;
25400 tree fn;
25401
25402 /* Create the FUNCTION_DECL. */
25403 fn = grokmethod (decl_specifiers, declarator, attributes);
25404 cp_finalize_omp_declare_simd (parser, fn);
25405 cp_finalize_oacc_routine (parser, fn, true);
25406 /* If something went badly wrong, bail out now. */
25407 if (fn == error_mark_node)
25408 {
25409 /* If there's a function-body, skip it. */
25410 if (cp_parser_token_starts_function_definition_p
25411 (cp_lexer_peek_token (parser->lexer)))
25412 cp_parser_skip_to_end_of_block_or_statement (parser);
25413 return error_mark_node;
25414 }
25415
25416 /* Remember it, if there default args to post process. */
25417 cp_parser_save_default_args (parser, fn);
25418
25419 /* Save away the tokens that make up the body of the
25420 function. */
25421 first = parser->lexer->next_token;
25422 /* Handle function try blocks. */
25423 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
25424 cp_lexer_consume_token (parser->lexer);
25425 /* We can have braced-init-list mem-initializers before the fn body. */
25426 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
25427 {
25428 cp_lexer_consume_token (parser->lexer);
25429 while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
25430 {
25431 /* cache_group will stop after an un-nested { } pair, too. */
25432 if (cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0))
25433 break;
25434
25435 /* variadic mem-inits have ... after the ')'. */
25436 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
25437 cp_lexer_consume_token (parser->lexer);
25438 }
25439 }
25440 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
25441 /* Handle function try blocks. */
25442 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
25443 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
25444 last = parser->lexer->next_token;
25445
25446 /* Save away the inline definition; we will process it when the
25447 class is complete. */
25448 DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
25449 DECL_PENDING_INLINE_P (fn) = 1;
25450
25451 /* We need to know that this was defined in the class, so that
25452 friend templates are handled correctly. */
25453 DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
25454
25455 /* Add FN to the queue of functions to be parsed later. */
25456 vec_safe_push (unparsed_funs_with_definitions, fn);
25457
25458 return fn;
25459 }
25460
25461 /* Save the tokens that make up the in-class initializer for a non-static
25462 data member. Returns a DEFAULT_ARG. */
25463
25464 static tree
25465 cp_parser_save_nsdmi (cp_parser* parser)
25466 {
25467 return cp_parser_cache_defarg (parser, /*nsdmi=*/true);
25468 }
25469
25470 /* Parse a template-argument-list, as well as the trailing ">" (but
25471 not the opening "<"). See cp_parser_template_argument_list for the
25472 return value. */
25473
25474 static tree
25475 cp_parser_enclosed_template_argument_list (cp_parser* parser)
25476 {
25477 tree arguments;
25478 tree saved_scope;
25479 tree saved_qualifying_scope;
25480 tree saved_object_scope;
25481 bool saved_greater_than_is_operator_p;
25482 int saved_unevaluated_operand;
25483 int saved_inhibit_evaluation_warnings;
25484
25485 /* [temp.names]
25486
25487 When parsing a template-id, the first non-nested `>' is taken as
25488 the end of the template-argument-list rather than a greater-than
25489 operator. */
25490 saved_greater_than_is_operator_p
25491 = parser->greater_than_is_operator_p;
25492 parser->greater_than_is_operator_p = false;
25493 /* Parsing the argument list may modify SCOPE, so we save it
25494 here. */
25495 saved_scope = parser->scope;
25496 saved_qualifying_scope = parser->qualifying_scope;
25497 saved_object_scope = parser->object_scope;
25498 /* We need to evaluate the template arguments, even though this
25499 template-id may be nested within a "sizeof". */
25500 saved_unevaluated_operand = cp_unevaluated_operand;
25501 cp_unevaluated_operand = 0;
25502 saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
25503 c_inhibit_evaluation_warnings = 0;
25504 /* Parse the template-argument-list itself. */
25505 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER)
25506 || cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
25507 arguments = NULL_TREE;
25508 else
25509 arguments = cp_parser_template_argument_list (parser);
25510 /* Look for the `>' that ends the template-argument-list. If we find
25511 a '>>' instead, it's probably just a typo. */
25512 if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
25513 {
25514 if (cxx_dialect != cxx98)
25515 {
25516 /* In C++0x, a `>>' in a template argument list or cast
25517 expression is considered to be two separate `>'
25518 tokens. So, change the current token to a `>', but don't
25519 consume it: it will be consumed later when the outer
25520 template argument list (or cast expression) is parsed.
25521 Note that this replacement of `>' for `>>' is necessary
25522 even if we are parsing tentatively: in the tentative
25523 case, after calling
25524 cp_parser_enclosed_template_argument_list we will always
25525 throw away all of the template arguments and the first
25526 closing `>', either because the template argument list
25527 was erroneous or because we are replacing those tokens
25528 with a CPP_TEMPLATE_ID token. The second `>' (which will
25529 not have been thrown away) is needed either to close an
25530 outer template argument list or to complete a new-style
25531 cast. */
25532 cp_token *token = cp_lexer_peek_token (parser->lexer);
25533 token->type = CPP_GREATER;
25534 }
25535 else if (!saved_greater_than_is_operator_p)
25536 {
25537 /* If we're in a nested template argument list, the '>>' has
25538 to be a typo for '> >'. We emit the error message, but we
25539 continue parsing and we push a '>' as next token, so that
25540 the argument list will be parsed correctly. Note that the
25541 global source location is still on the token before the
25542 '>>', so we need to say explicitly where we want it. */
25543 cp_token *token = cp_lexer_peek_token (parser->lexer);
25544 error_at (token->location, "%<>>%> should be %<> >%> "
25545 "within a nested template argument list");
25546
25547 token->type = CPP_GREATER;
25548 }
25549 else
25550 {
25551 /* If this is not a nested template argument list, the '>>'
25552 is a typo for '>'. Emit an error message and continue.
25553 Same deal about the token location, but here we can get it
25554 right by consuming the '>>' before issuing the diagnostic. */
25555 cp_token *token = cp_lexer_consume_token (parser->lexer);
25556 error_at (token->location,
25557 "spurious %<>>%>, use %<>%> to terminate "
25558 "a template argument list");
25559 }
25560 }
25561 else
25562 cp_parser_skip_to_end_of_template_parameter_list (parser);
25563 /* The `>' token might be a greater-than operator again now. */
25564 parser->greater_than_is_operator_p
25565 = saved_greater_than_is_operator_p;
25566 /* Restore the SAVED_SCOPE. */
25567 parser->scope = saved_scope;
25568 parser->qualifying_scope = saved_qualifying_scope;
25569 parser->object_scope = saved_object_scope;
25570 cp_unevaluated_operand = saved_unevaluated_operand;
25571 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
25572
25573 return arguments;
25574 }
25575
25576 /* MEMBER_FUNCTION is a member function, or a friend. If default
25577 arguments, or the body of the function have not yet been parsed,
25578 parse them now. */
25579
25580 static void
25581 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
25582 {
25583 timevar_push (TV_PARSE_INMETH);
25584 /* If this member is a template, get the underlying
25585 FUNCTION_DECL. */
25586 if (DECL_FUNCTION_TEMPLATE_P (member_function))
25587 member_function = DECL_TEMPLATE_RESULT (member_function);
25588
25589 /* There should not be any class definitions in progress at this
25590 point; the bodies of members are only parsed outside of all class
25591 definitions. */
25592 gcc_assert (parser->num_classes_being_defined == 0);
25593 /* While we're parsing the member functions we might encounter more
25594 classes. We want to handle them right away, but we don't want
25595 them getting mixed up with functions that are currently in the
25596 queue. */
25597 push_unparsed_function_queues (parser);
25598
25599 /* Make sure that any template parameters are in scope. */
25600 maybe_begin_member_template_processing (member_function);
25601
25602 /* If the body of the function has not yet been parsed, parse it
25603 now. */
25604 if (DECL_PENDING_INLINE_P (member_function))
25605 {
25606 tree function_scope;
25607 cp_token_cache *tokens;
25608
25609 /* The function is no longer pending; we are processing it. */
25610 tokens = DECL_PENDING_INLINE_INFO (member_function);
25611 DECL_PENDING_INLINE_INFO (member_function) = NULL;
25612 DECL_PENDING_INLINE_P (member_function) = 0;
25613
25614 /* If this is a local class, enter the scope of the containing
25615 function. */
25616 function_scope = current_function_decl;
25617 if (function_scope)
25618 push_function_context ();
25619
25620 /* Push the body of the function onto the lexer stack. */
25621 cp_parser_push_lexer_for_tokens (parser, tokens);
25622
25623 /* Let the front end know that we going to be defining this
25624 function. */
25625 start_preparsed_function (member_function, NULL_TREE,
25626 SF_PRE_PARSED | SF_INCLASS_INLINE);
25627
25628 /* Don't do access checking if it is a templated function. */
25629 if (processing_template_decl)
25630 push_deferring_access_checks (dk_no_check);
25631
25632 /* #pragma omp declare reduction needs special parsing. */
25633 if (DECL_OMP_DECLARE_REDUCTION_P (member_function))
25634 {
25635 parser->lexer->in_pragma = true;
25636 cp_parser_omp_declare_reduction_exprs (member_function, parser);
25637 finish_function (/*inline*/2);
25638 cp_check_omp_declare_reduction (member_function);
25639 }
25640 else
25641 /* Now, parse the body of the function. */
25642 cp_parser_function_definition_after_declarator (parser,
25643 /*inline_p=*/true);
25644
25645 if (processing_template_decl)
25646 pop_deferring_access_checks ();
25647
25648 /* Leave the scope of the containing function. */
25649 if (function_scope)
25650 pop_function_context ();
25651 cp_parser_pop_lexer (parser);
25652 }
25653
25654 /* Remove any template parameters from the symbol table. */
25655 maybe_end_member_template_processing ();
25656
25657 /* Restore the queue. */
25658 pop_unparsed_function_queues (parser);
25659 timevar_pop (TV_PARSE_INMETH);
25660 }
25661
25662 /* If DECL contains any default args, remember it on the unparsed
25663 functions queue. */
25664
25665 static void
25666 cp_parser_save_default_args (cp_parser* parser, tree decl)
25667 {
25668 tree probe;
25669
25670 for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
25671 probe;
25672 probe = TREE_CHAIN (probe))
25673 if (TREE_PURPOSE (probe))
25674 {
25675 cp_default_arg_entry entry = {current_class_type, decl};
25676 vec_safe_push (unparsed_funs_with_default_args, entry);
25677 break;
25678 }
25679 }
25680
25681 /* DEFAULT_ARG contains the saved tokens for the initializer of DECL,
25682 which is either a FIELD_DECL or PARM_DECL. Parse it and return
25683 the result. For a PARM_DECL, PARMTYPE is the corresponding type
25684 from the parameter-type-list. */
25685
25686 static tree
25687 cp_parser_late_parse_one_default_arg (cp_parser *parser, tree decl,
25688 tree default_arg, tree parmtype)
25689 {
25690 cp_token_cache *tokens;
25691 tree parsed_arg;
25692 bool dummy;
25693
25694 if (default_arg == error_mark_node)
25695 return error_mark_node;
25696
25697 /* Push the saved tokens for the default argument onto the parser's
25698 lexer stack. */
25699 tokens = DEFARG_TOKENS (default_arg);
25700 cp_parser_push_lexer_for_tokens (parser, tokens);
25701
25702 start_lambda_scope (decl);
25703
25704 /* Parse the default argument. */
25705 parsed_arg = cp_parser_initializer (parser, &dummy, &dummy);
25706 if (BRACE_ENCLOSED_INITIALIZER_P (parsed_arg))
25707 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
25708
25709 finish_lambda_scope ();
25710
25711 if (parsed_arg == error_mark_node)
25712 cp_parser_skip_to_end_of_statement (parser);
25713
25714 if (!processing_template_decl)
25715 {
25716 /* In a non-template class, check conversions now. In a template,
25717 we'll wait and instantiate these as needed. */
25718 if (TREE_CODE (decl) == PARM_DECL)
25719 parsed_arg = check_default_argument (parmtype, parsed_arg,
25720 tf_warning_or_error);
25721 else
25722 parsed_arg = digest_nsdmi_init (decl, parsed_arg);
25723 }
25724
25725 /* If the token stream has not been completely used up, then
25726 there was extra junk after the end of the default
25727 argument. */
25728 if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
25729 {
25730 if (TREE_CODE (decl) == PARM_DECL)
25731 cp_parser_error (parser, "expected %<,%>");
25732 else
25733 cp_parser_error (parser, "expected %<;%>");
25734 }
25735
25736 /* Revert to the main lexer. */
25737 cp_parser_pop_lexer (parser);
25738
25739 return parsed_arg;
25740 }
25741
25742 /* FIELD is a non-static data member with an initializer which we saved for
25743 later; parse it now. */
25744
25745 static void
25746 cp_parser_late_parsing_nsdmi (cp_parser *parser, tree field)
25747 {
25748 tree def;
25749
25750 maybe_begin_member_template_processing (field);
25751
25752 push_unparsed_function_queues (parser);
25753 def = cp_parser_late_parse_one_default_arg (parser, field,
25754 DECL_INITIAL (field),
25755 NULL_TREE);
25756 pop_unparsed_function_queues (parser);
25757
25758 maybe_end_member_template_processing ();
25759
25760 DECL_INITIAL (field) = def;
25761 }
25762
25763 /* FN is a FUNCTION_DECL which may contains a parameter with an
25764 unparsed DEFAULT_ARG. Parse the default args now. This function
25765 assumes that the current scope is the scope in which the default
25766 argument should be processed. */
25767
25768 static void
25769 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
25770 {
25771 bool saved_local_variables_forbidden_p;
25772 tree parm, parmdecl;
25773
25774 /* While we're parsing the default args, we might (due to the
25775 statement expression extension) encounter more classes. We want
25776 to handle them right away, but we don't want them getting mixed
25777 up with default args that are currently in the queue. */
25778 push_unparsed_function_queues (parser);
25779
25780 /* Local variable names (and the `this' keyword) may not appear
25781 in a default argument. */
25782 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
25783 parser->local_variables_forbidden_p = true;
25784
25785 push_defarg_context (fn);
25786
25787 for (parm = TYPE_ARG_TYPES (TREE_TYPE (fn)),
25788 parmdecl = DECL_ARGUMENTS (fn);
25789 parm && parm != void_list_node;
25790 parm = TREE_CHAIN (parm),
25791 parmdecl = DECL_CHAIN (parmdecl))
25792 {
25793 tree default_arg = TREE_PURPOSE (parm);
25794 tree parsed_arg;
25795 vec<tree, va_gc> *insts;
25796 tree copy;
25797 unsigned ix;
25798
25799 if (!default_arg)
25800 continue;
25801
25802 if (TREE_CODE (default_arg) != DEFAULT_ARG)
25803 /* This can happen for a friend declaration for a function
25804 already declared with default arguments. */
25805 continue;
25806
25807 parsed_arg
25808 = cp_parser_late_parse_one_default_arg (parser, parmdecl,
25809 default_arg,
25810 TREE_VALUE (parm));
25811 if (parsed_arg == error_mark_node)
25812 {
25813 continue;
25814 }
25815
25816 TREE_PURPOSE (parm) = parsed_arg;
25817
25818 /* Update any instantiations we've already created. */
25819 for (insts = DEFARG_INSTANTIATIONS (default_arg), ix = 0;
25820 vec_safe_iterate (insts, ix, &copy); ix++)
25821 TREE_PURPOSE (copy) = parsed_arg;
25822 }
25823
25824 pop_defarg_context ();
25825
25826 /* Make sure no default arg is missing. */
25827 check_default_args (fn);
25828
25829 /* Restore the state of local_variables_forbidden_p. */
25830 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
25831
25832 /* Restore the queue. */
25833 pop_unparsed_function_queues (parser);
25834 }
25835
25836 /* Subroutine of cp_parser_sizeof_operand, for handling C++11
25837
25838 sizeof ... ( identifier )
25839
25840 where the 'sizeof' token has already been consumed. */
25841
25842 static tree
25843 cp_parser_sizeof_pack (cp_parser *parser)
25844 {
25845 /* Consume the `...'. */
25846 cp_lexer_consume_token (parser->lexer);
25847 maybe_warn_variadic_templates ();
25848
25849 bool paren = cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN);
25850 if (paren)
25851 cp_lexer_consume_token (parser->lexer);
25852 else
25853 permerror (cp_lexer_peek_token (parser->lexer)->location,
25854 "%<sizeof...%> argument must be surrounded by parentheses");
25855
25856 cp_token *token = cp_lexer_peek_token (parser->lexer);
25857 tree name = cp_parser_identifier (parser);
25858 if (name == error_mark_node)
25859 return error_mark_node;
25860 /* The name is not qualified. */
25861 parser->scope = NULL_TREE;
25862 parser->qualifying_scope = NULL_TREE;
25863 parser->object_scope = NULL_TREE;
25864 tree expr = cp_parser_lookup_name_simple (parser, name, token->location);
25865 if (expr == error_mark_node)
25866 cp_parser_name_lookup_error (parser, name, expr, NLE_NULL,
25867 token->location);
25868 if (TREE_CODE (expr) == TYPE_DECL || TREE_CODE (expr) == TEMPLATE_DECL)
25869 expr = TREE_TYPE (expr);
25870 else if (TREE_CODE (expr) == CONST_DECL)
25871 expr = DECL_INITIAL (expr);
25872 expr = make_pack_expansion (expr);
25873 PACK_EXPANSION_SIZEOF_P (expr) = true;
25874
25875 if (paren)
25876 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
25877
25878 return expr;
25879 }
25880
25881 /* Parse the operand of `sizeof' (or a similar operator). Returns
25882 either a TYPE or an expression, depending on the form of the
25883 input. The KEYWORD indicates which kind of expression we have
25884 encountered. */
25885
25886 static tree
25887 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
25888 {
25889 tree expr = NULL_TREE;
25890 const char *saved_message;
25891 char *tmp;
25892 bool saved_integral_constant_expression_p;
25893 bool saved_non_integral_constant_expression_p;
25894
25895 /* If it's a `...', then we are computing the length of a parameter
25896 pack. */
25897 if (keyword == RID_SIZEOF
25898 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
25899 return cp_parser_sizeof_pack (parser);
25900
25901 /* Types cannot be defined in a `sizeof' expression. Save away the
25902 old message. */
25903 saved_message = parser->type_definition_forbidden_message;
25904 /* And create the new one. */
25905 tmp = concat ("types may not be defined in %<",
25906 IDENTIFIER_POINTER (ridpointers[keyword]),
25907 "%> expressions", NULL);
25908 parser->type_definition_forbidden_message = tmp;
25909
25910 /* The restrictions on constant-expressions do not apply inside
25911 sizeof expressions. */
25912 saved_integral_constant_expression_p
25913 = parser->integral_constant_expression_p;
25914 saved_non_integral_constant_expression_p
25915 = parser->non_integral_constant_expression_p;
25916 parser->integral_constant_expression_p = false;
25917
25918 /* Do not actually evaluate the expression. */
25919 ++cp_unevaluated_operand;
25920 ++c_inhibit_evaluation_warnings;
25921 /* If it's a `(', then we might be looking at the type-id
25922 construction. */
25923 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
25924 {
25925 tree type = NULL_TREE;
25926
25927 /* We can't be sure yet whether we're looking at a type-id or an
25928 expression. */
25929 cp_parser_parse_tentatively (parser);
25930 /* Note: as a GNU Extension, compound literals are considered
25931 postfix-expressions as they are in C99, so they are valid
25932 arguments to sizeof. See comment in cp_parser_cast_expression
25933 for details. */
25934 if (cp_parser_compound_literal_p (parser))
25935 cp_parser_simulate_error (parser);
25936 else
25937 {
25938 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
25939 parser->in_type_id_in_expr_p = true;
25940 /* Look for the type-id. */
25941 type = cp_parser_type_id (parser);
25942 /* Look for the closing `)'. */
25943 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
25944 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
25945 }
25946
25947 /* If all went well, then we're done. */
25948 if (cp_parser_parse_definitely (parser))
25949 {
25950 cp_decl_specifier_seq decl_specs;
25951
25952 /* Build a trivial decl-specifier-seq. */
25953 clear_decl_specs (&decl_specs);
25954 decl_specs.type = type;
25955
25956 /* Call grokdeclarator to figure out what type this is. */
25957 expr = grokdeclarator (NULL,
25958 &decl_specs,
25959 TYPENAME,
25960 /*initialized=*/0,
25961 /*attrlist=*/NULL);
25962 }
25963 }
25964
25965 /* If the type-id production did not work out, then we must be
25966 looking at the unary-expression production. */
25967 if (!expr)
25968 expr = cp_parser_unary_expression (parser);
25969
25970 /* Go back to evaluating expressions. */
25971 --cp_unevaluated_operand;
25972 --c_inhibit_evaluation_warnings;
25973
25974 /* Free the message we created. */
25975 free (tmp);
25976 /* And restore the old one. */
25977 parser->type_definition_forbidden_message = saved_message;
25978 parser->integral_constant_expression_p
25979 = saved_integral_constant_expression_p;
25980 parser->non_integral_constant_expression_p
25981 = saved_non_integral_constant_expression_p;
25982
25983 return expr;
25984 }
25985
25986 /* If the current declaration has no declarator, return true. */
25987
25988 static bool
25989 cp_parser_declares_only_class_p (cp_parser *parser)
25990 {
25991 /* If the next token is a `;' or a `,' then there is no
25992 declarator. */
25993 return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
25994 || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
25995 }
25996
25997 /* Update the DECL_SPECS to reflect the storage class indicated by
25998 KEYWORD. */
25999
26000 static void
26001 cp_parser_set_storage_class (cp_parser *parser,
26002 cp_decl_specifier_seq *decl_specs,
26003 enum rid keyword,
26004 cp_token *token)
26005 {
26006 cp_storage_class storage_class;
26007
26008 if (parser->in_unbraced_linkage_specification_p)
26009 {
26010 error_at (token->location, "invalid use of %qD in linkage specification",
26011 ridpointers[keyword]);
26012 return;
26013 }
26014 else if (decl_specs->storage_class != sc_none)
26015 {
26016 decl_specs->conflicting_specifiers_p = true;
26017 return;
26018 }
26019
26020 if ((keyword == RID_EXTERN || keyword == RID_STATIC)
26021 && decl_spec_seq_has_spec_p (decl_specs, ds_thread)
26022 && decl_specs->gnu_thread_keyword_p)
26023 {
26024 pedwarn (decl_specs->locations[ds_thread], 0,
26025 "%<__thread%> before %qD", ridpointers[keyword]);
26026 }
26027
26028 switch (keyword)
26029 {
26030 case RID_AUTO:
26031 storage_class = sc_auto;
26032 break;
26033 case RID_REGISTER:
26034 storage_class = sc_register;
26035 break;
26036 case RID_STATIC:
26037 storage_class = sc_static;
26038 break;
26039 case RID_EXTERN:
26040 storage_class = sc_extern;
26041 break;
26042 case RID_MUTABLE:
26043 storage_class = sc_mutable;
26044 break;
26045 default:
26046 gcc_unreachable ();
26047 }
26048 decl_specs->storage_class = storage_class;
26049 set_and_check_decl_spec_loc (decl_specs, ds_storage_class, token);
26050
26051 /* A storage class specifier cannot be applied alongside a typedef
26052 specifier. If there is a typedef specifier present then set
26053 conflicting_specifiers_p which will trigger an error later
26054 on in grokdeclarator. */
26055 if (decl_spec_seq_has_spec_p (decl_specs, ds_typedef))
26056 decl_specs->conflicting_specifiers_p = true;
26057 }
26058
26059 /* Update the DECL_SPECS to reflect the TYPE_SPEC. If TYPE_DEFINITION_P
26060 is true, the type is a class or enum definition. */
26061
26062 static void
26063 cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
26064 tree type_spec,
26065 cp_token *token,
26066 bool type_definition_p)
26067 {
26068 decl_specs->any_specifiers_p = true;
26069
26070 /* If the user tries to redeclare bool, char16_t, char32_t, or wchar_t
26071 (with, for example, in "typedef int wchar_t;") we remember that
26072 this is what happened. In system headers, we ignore these
26073 declarations so that G++ can work with system headers that are not
26074 C++-safe. */
26075 if (decl_spec_seq_has_spec_p (decl_specs, ds_typedef)
26076 && !type_definition_p
26077 && (type_spec == boolean_type_node
26078 || type_spec == char16_type_node
26079 || type_spec == char32_type_node
26080 || type_spec == wchar_type_node)
26081 && (decl_specs->type
26082 || decl_spec_seq_has_spec_p (decl_specs, ds_long)
26083 || decl_spec_seq_has_spec_p (decl_specs, ds_short)
26084 || decl_spec_seq_has_spec_p (decl_specs, ds_unsigned)
26085 || decl_spec_seq_has_spec_p (decl_specs, ds_signed)))
26086 {
26087 decl_specs->redefined_builtin_type = type_spec;
26088 set_and_check_decl_spec_loc (decl_specs,
26089 ds_redefined_builtin_type_spec,
26090 token);
26091 if (!decl_specs->type)
26092 {
26093 decl_specs->type = type_spec;
26094 decl_specs->type_definition_p = false;
26095 set_and_check_decl_spec_loc (decl_specs,ds_type_spec, token);
26096 }
26097 }
26098 else if (decl_specs->type)
26099 decl_specs->multiple_types_p = true;
26100 else
26101 {
26102 decl_specs->type = type_spec;
26103 decl_specs->type_definition_p = type_definition_p;
26104 decl_specs->redefined_builtin_type = NULL_TREE;
26105 set_and_check_decl_spec_loc (decl_specs, ds_type_spec, token);
26106 }
26107 }
26108
26109 /* True iff TOKEN is the GNU keyword __thread. */
26110
26111 static bool
26112 token_is__thread (cp_token *token)
26113 {
26114 gcc_assert (token->keyword == RID_THREAD);
26115 return !strcmp (IDENTIFIER_POINTER (token->u.value), "__thread");
26116 }
26117
26118 /* Set the location for a declarator specifier and check if it is
26119 duplicated.
26120
26121 DECL_SPECS is the sequence of declarator specifiers onto which to
26122 set the location.
26123
26124 DS is the single declarator specifier to set which location is to
26125 be set onto the existing sequence of declarators.
26126
26127 LOCATION is the location for the declarator specifier to
26128 consider. */
26129
26130 static void
26131 set_and_check_decl_spec_loc (cp_decl_specifier_seq *decl_specs,
26132 cp_decl_spec ds, cp_token *token)
26133 {
26134 gcc_assert (ds < ds_last);
26135
26136 if (decl_specs == NULL)
26137 return;
26138
26139 source_location location = token->location;
26140
26141 if (decl_specs->locations[ds] == 0)
26142 {
26143 decl_specs->locations[ds] = location;
26144 if (ds == ds_thread)
26145 decl_specs->gnu_thread_keyword_p = token_is__thread (token);
26146 }
26147 else
26148 {
26149 if (ds == ds_long)
26150 {
26151 if (decl_specs->locations[ds_long_long] != 0)
26152 error_at (location,
26153 "%<long long long%> is too long for GCC");
26154 else
26155 {
26156 decl_specs->locations[ds_long_long] = location;
26157 pedwarn_cxx98 (location,
26158 OPT_Wlong_long,
26159 "ISO C++ 1998 does not support %<long long%>");
26160 }
26161 }
26162 else if (ds == ds_thread)
26163 {
26164 bool gnu = token_is__thread (token);
26165 if (gnu != decl_specs->gnu_thread_keyword_p)
26166 error_at (location,
26167 "both %<__thread%> and %<thread_local%> specified");
26168 else
26169 error_at (location, "duplicate %qD", token->u.value);
26170 }
26171 else
26172 {
26173 static const char *const decl_spec_names[] = {
26174 "signed",
26175 "unsigned",
26176 "short",
26177 "long",
26178 "const",
26179 "volatile",
26180 "restrict",
26181 "inline",
26182 "virtual",
26183 "explicit",
26184 "friend",
26185 "typedef",
26186 "using",
26187 "constexpr",
26188 "__complex"
26189 };
26190 error_at (location,
26191 "duplicate %qs", decl_spec_names[ds]);
26192 }
26193 }
26194 }
26195
26196 /* Return true iff the declarator specifier DS is present in the
26197 sequence of declarator specifiers DECL_SPECS. */
26198
26199 bool
26200 decl_spec_seq_has_spec_p (const cp_decl_specifier_seq * decl_specs,
26201 cp_decl_spec ds)
26202 {
26203 gcc_assert (ds < ds_last);
26204
26205 if (decl_specs == NULL)
26206 return false;
26207
26208 return decl_specs->locations[ds] != 0;
26209 }
26210
26211 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
26212 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
26213
26214 static bool
26215 cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
26216 {
26217 return decl_spec_seq_has_spec_p (decl_specifiers, ds_friend);
26218 }
26219
26220 /* Issue an error message indicating that TOKEN_DESC was expected.
26221 If KEYWORD is true, it indicated this function is called by
26222 cp_parser_require_keword and the required token can only be
26223 a indicated keyword. */
26224
26225 static void
26226 cp_parser_required_error (cp_parser *parser,
26227 required_token token_desc,
26228 bool keyword)
26229 {
26230 switch (token_desc)
26231 {
26232 case RT_NEW:
26233 cp_parser_error (parser, "expected %<new%>");
26234 return;
26235 case RT_DELETE:
26236 cp_parser_error (parser, "expected %<delete%>");
26237 return;
26238 case RT_RETURN:
26239 cp_parser_error (parser, "expected %<return%>");
26240 return;
26241 case RT_WHILE:
26242 cp_parser_error (parser, "expected %<while%>");
26243 return;
26244 case RT_EXTERN:
26245 cp_parser_error (parser, "expected %<extern%>");
26246 return;
26247 case RT_STATIC_ASSERT:
26248 cp_parser_error (parser, "expected %<static_assert%>");
26249 return;
26250 case RT_DECLTYPE:
26251 cp_parser_error (parser, "expected %<decltype%>");
26252 return;
26253 case RT_OPERATOR:
26254 cp_parser_error (parser, "expected %<operator%>");
26255 return;
26256 case RT_CLASS:
26257 cp_parser_error (parser, "expected %<class%>");
26258 return;
26259 case RT_TEMPLATE:
26260 cp_parser_error (parser, "expected %<template%>");
26261 return;
26262 case RT_NAMESPACE:
26263 cp_parser_error (parser, "expected %<namespace%>");
26264 return;
26265 case RT_USING:
26266 cp_parser_error (parser, "expected %<using%>");
26267 return;
26268 case RT_ASM:
26269 cp_parser_error (parser, "expected %<asm%>");
26270 return;
26271 case RT_TRY:
26272 cp_parser_error (parser, "expected %<try%>");
26273 return;
26274 case RT_CATCH:
26275 cp_parser_error (parser, "expected %<catch%>");
26276 return;
26277 case RT_THROW:
26278 cp_parser_error (parser, "expected %<throw%>");
26279 return;
26280 case RT_LABEL:
26281 cp_parser_error (parser, "expected %<__label__%>");
26282 return;
26283 case RT_AT_TRY:
26284 cp_parser_error (parser, "expected %<@try%>");
26285 return;
26286 case RT_AT_SYNCHRONIZED:
26287 cp_parser_error (parser, "expected %<@synchronized%>");
26288 return;
26289 case RT_AT_THROW:
26290 cp_parser_error (parser, "expected %<@throw%>");
26291 return;
26292 case RT_TRANSACTION_ATOMIC:
26293 cp_parser_error (parser, "expected %<__transaction_atomic%>");
26294 return;
26295 case RT_TRANSACTION_RELAXED:
26296 cp_parser_error (parser, "expected %<__transaction_relaxed%>");
26297 return;
26298 default:
26299 break;
26300 }
26301 if (!keyword)
26302 {
26303 switch (token_desc)
26304 {
26305 case RT_SEMICOLON:
26306 cp_parser_error (parser, "expected %<;%>");
26307 return;
26308 case RT_OPEN_PAREN:
26309 cp_parser_error (parser, "expected %<(%>");
26310 return;
26311 case RT_CLOSE_BRACE:
26312 cp_parser_error (parser, "expected %<}%>");
26313 return;
26314 case RT_OPEN_BRACE:
26315 cp_parser_error (parser, "expected %<{%>");
26316 return;
26317 case RT_CLOSE_SQUARE:
26318 cp_parser_error (parser, "expected %<]%>");
26319 return;
26320 case RT_OPEN_SQUARE:
26321 cp_parser_error (parser, "expected %<[%>");
26322 return;
26323 case RT_COMMA:
26324 cp_parser_error (parser, "expected %<,%>");
26325 return;
26326 case RT_SCOPE:
26327 cp_parser_error (parser, "expected %<::%>");
26328 return;
26329 case RT_LESS:
26330 cp_parser_error (parser, "expected %<<%>");
26331 return;
26332 case RT_GREATER:
26333 cp_parser_error (parser, "expected %<>%>");
26334 return;
26335 case RT_EQ:
26336 cp_parser_error (parser, "expected %<=%>");
26337 return;
26338 case RT_ELLIPSIS:
26339 cp_parser_error (parser, "expected %<...%>");
26340 return;
26341 case RT_MULT:
26342 cp_parser_error (parser, "expected %<*%>");
26343 return;
26344 case RT_COMPL:
26345 cp_parser_error (parser, "expected %<~%>");
26346 return;
26347 case RT_COLON:
26348 cp_parser_error (parser, "expected %<:%>");
26349 return;
26350 case RT_COLON_SCOPE:
26351 cp_parser_error (parser, "expected %<:%> or %<::%>");
26352 return;
26353 case RT_CLOSE_PAREN:
26354 cp_parser_error (parser, "expected %<)%>");
26355 return;
26356 case RT_COMMA_CLOSE_PAREN:
26357 cp_parser_error (parser, "expected %<,%> or %<)%>");
26358 return;
26359 case RT_PRAGMA_EOL:
26360 cp_parser_error (parser, "expected end of line");
26361 return;
26362 case RT_NAME:
26363 cp_parser_error (parser, "expected identifier");
26364 return;
26365 case RT_SELECT:
26366 cp_parser_error (parser, "expected selection-statement");
26367 return;
26368 case RT_INTERATION:
26369 cp_parser_error (parser, "expected iteration-statement");
26370 return;
26371 case RT_JUMP:
26372 cp_parser_error (parser, "expected jump-statement");
26373 return;
26374 case RT_CLASS_KEY:
26375 cp_parser_error (parser, "expected class-key");
26376 return;
26377 case RT_CLASS_TYPENAME_TEMPLATE:
26378 cp_parser_error (parser,
26379 "expected %<class%>, %<typename%>, or %<template%>");
26380 return;
26381 default:
26382 gcc_unreachable ();
26383 }
26384 }
26385 else
26386 gcc_unreachable ();
26387 }
26388
26389
26390
26391 /* If the next token is of the indicated TYPE, consume it. Otherwise,
26392 issue an error message indicating that TOKEN_DESC was expected.
26393
26394 Returns the token consumed, if the token had the appropriate type.
26395 Otherwise, returns NULL. */
26396
26397 static cp_token *
26398 cp_parser_require (cp_parser* parser,
26399 enum cpp_ttype type,
26400 required_token token_desc)
26401 {
26402 if (cp_lexer_next_token_is (parser->lexer, type))
26403 return cp_lexer_consume_token (parser->lexer);
26404 else
26405 {
26406 /* Output the MESSAGE -- unless we're parsing tentatively. */
26407 if (!cp_parser_simulate_error (parser))
26408 cp_parser_required_error (parser, token_desc, /*keyword=*/false);
26409 return NULL;
26410 }
26411 }
26412
26413 /* An error message is produced if the next token is not '>'.
26414 All further tokens are skipped until the desired token is
26415 found or '{', '}', ';' or an unbalanced ')' or ']'. */
26416
26417 static void
26418 cp_parser_skip_to_end_of_template_parameter_list (cp_parser* parser)
26419 {
26420 /* Current level of '< ... >'. */
26421 unsigned level = 0;
26422 /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'. */
26423 unsigned nesting_depth = 0;
26424
26425 /* Are we ready, yet? If not, issue error message. */
26426 if (cp_parser_require (parser, CPP_GREATER, RT_GREATER))
26427 return;
26428
26429 /* Skip tokens until the desired token is found. */
26430 while (true)
26431 {
26432 /* Peek at the next token. */
26433 switch (cp_lexer_peek_token (parser->lexer)->type)
26434 {
26435 case CPP_LESS:
26436 if (!nesting_depth)
26437 ++level;
26438 break;
26439
26440 case CPP_RSHIFT:
26441 if (cxx_dialect == cxx98)
26442 /* C++0x views the `>>' operator as two `>' tokens, but
26443 C++98 does not. */
26444 break;
26445 else if (!nesting_depth && level-- == 0)
26446 {
26447 /* We've hit a `>>' where the first `>' closes the
26448 template argument list, and the second `>' is
26449 spurious. Just consume the `>>' and stop; we've
26450 already produced at least one error. */
26451 cp_lexer_consume_token (parser->lexer);
26452 return;
26453 }
26454 /* Fall through for C++0x, so we handle the second `>' in
26455 the `>>'. */
26456
26457 case CPP_GREATER:
26458 if (!nesting_depth && level-- == 0)
26459 {
26460 /* We've reached the token we want, consume it and stop. */
26461 cp_lexer_consume_token (parser->lexer);
26462 return;
26463 }
26464 break;
26465
26466 case CPP_OPEN_PAREN:
26467 case CPP_OPEN_SQUARE:
26468 ++nesting_depth;
26469 break;
26470
26471 case CPP_CLOSE_PAREN:
26472 case CPP_CLOSE_SQUARE:
26473 if (nesting_depth-- == 0)
26474 return;
26475 break;
26476
26477 case CPP_EOF:
26478 case CPP_PRAGMA_EOL:
26479 case CPP_SEMICOLON:
26480 case CPP_OPEN_BRACE:
26481 case CPP_CLOSE_BRACE:
26482 /* The '>' was probably forgotten, don't look further. */
26483 return;
26484
26485 default:
26486 break;
26487 }
26488
26489 /* Consume this token. */
26490 cp_lexer_consume_token (parser->lexer);
26491 }
26492 }
26493
26494 /* If the next token is the indicated keyword, consume it. Otherwise,
26495 issue an error message indicating that TOKEN_DESC was expected.
26496
26497 Returns the token consumed, if the token had the appropriate type.
26498 Otherwise, returns NULL. */
26499
26500 static cp_token *
26501 cp_parser_require_keyword (cp_parser* parser,
26502 enum rid keyword,
26503 required_token token_desc)
26504 {
26505 cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
26506
26507 if (token && token->keyword != keyword)
26508 {
26509 cp_parser_required_error (parser, token_desc, /*keyword=*/true);
26510 return NULL;
26511 }
26512
26513 return token;
26514 }
26515
26516 /* Returns TRUE iff TOKEN is a token that can begin the body of a
26517 function-definition. */
26518
26519 static bool
26520 cp_parser_token_starts_function_definition_p (cp_token* token)
26521 {
26522 return (/* An ordinary function-body begins with an `{'. */
26523 token->type == CPP_OPEN_BRACE
26524 /* A ctor-initializer begins with a `:'. */
26525 || token->type == CPP_COLON
26526 /* A function-try-block begins with `try'. */
26527 || token->keyword == RID_TRY
26528 /* A function-transaction-block begins with `__transaction_atomic'
26529 or `__transaction_relaxed'. */
26530 || token->keyword == RID_TRANSACTION_ATOMIC
26531 || token->keyword == RID_TRANSACTION_RELAXED
26532 /* The named return value extension begins with `return'. */
26533 || token->keyword == RID_RETURN);
26534 }
26535
26536 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
26537 definition. */
26538
26539 static bool
26540 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
26541 {
26542 cp_token *token;
26543
26544 token = cp_lexer_peek_token (parser->lexer);
26545 return (token->type == CPP_OPEN_BRACE
26546 || (token->type == CPP_COLON
26547 && !parser->colon_doesnt_start_class_def_p));
26548 }
26549
26550 /* Returns TRUE iff the next token is the "," or ">" (or `>>', in
26551 C++0x) ending a template-argument. */
26552
26553 static bool
26554 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
26555 {
26556 cp_token *token;
26557
26558 token = cp_lexer_peek_token (parser->lexer);
26559 return (token->type == CPP_COMMA
26560 || token->type == CPP_GREATER
26561 || token->type == CPP_ELLIPSIS
26562 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT));
26563 }
26564
26565 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
26566 (n+1)-th is a ":" (which is a possible digraph typo for "< ::"). */
26567
26568 static bool
26569 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
26570 size_t n)
26571 {
26572 cp_token *token;
26573
26574 token = cp_lexer_peek_nth_token (parser->lexer, n);
26575 if (token->type == CPP_LESS)
26576 return true;
26577 /* Check for the sequence `<::' in the original code. It would be lexed as
26578 `[:', where `[' is a digraph, and there is no whitespace before
26579 `:'. */
26580 if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
26581 {
26582 cp_token *token2;
26583 token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
26584 if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
26585 return true;
26586 }
26587 return false;
26588 }
26589
26590 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
26591 or none_type otherwise. */
26592
26593 static enum tag_types
26594 cp_parser_token_is_class_key (cp_token* token)
26595 {
26596 switch (token->keyword)
26597 {
26598 case RID_CLASS:
26599 return class_type;
26600 case RID_STRUCT:
26601 return record_type;
26602 case RID_UNION:
26603 return union_type;
26604
26605 default:
26606 return none_type;
26607 }
26608 }
26609
26610 /* Returns the kind of tag indicated by TOKEN, if it is a type-parameter-key,
26611 or none_type otherwise or if the token is null. */
26612
26613 static enum tag_types
26614 cp_parser_token_is_type_parameter_key (cp_token* token)
26615 {
26616 if (!token)
26617 return none_type;
26618
26619 switch (token->keyword)
26620 {
26621 case RID_CLASS:
26622 return class_type;
26623 case RID_TYPENAME:
26624 return typename_type;
26625
26626 default:
26627 return none_type;
26628 }
26629 }
26630
26631 /* Issue an error message if the CLASS_KEY does not match the TYPE. */
26632
26633 static void
26634 cp_parser_check_class_key (enum tag_types class_key, tree type)
26635 {
26636 if (type == error_mark_node)
26637 return;
26638 if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
26639 {
26640 if (permerror (input_location, "%qs tag used in naming %q#T",
26641 class_key == union_type ? "union"
26642 : class_key == record_type ? "struct" : "class",
26643 type))
26644 inform (DECL_SOURCE_LOCATION (TYPE_NAME (type)),
26645 "%q#T was previously declared here", type);
26646 }
26647 }
26648
26649 /* Issue an error message if DECL is redeclared with different
26650 access than its original declaration [class.access.spec/3].
26651 This applies to nested classes and nested class templates.
26652 [class.mem/1]. */
26653
26654 static void
26655 cp_parser_check_access_in_redeclaration (tree decl, location_t location)
26656 {
26657 if (!decl || !CLASS_TYPE_P (TREE_TYPE (decl)))
26658 return;
26659
26660 if ((TREE_PRIVATE (decl)
26661 != (current_access_specifier == access_private_node))
26662 || (TREE_PROTECTED (decl)
26663 != (current_access_specifier == access_protected_node)))
26664 error_at (location, "%qD redeclared with different access", decl);
26665 }
26666
26667 /* Look for the `template' keyword, as a syntactic disambiguator.
26668 Return TRUE iff it is present, in which case it will be
26669 consumed. */
26670
26671 static bool
26672 cp_parser_optional_template_keyword (cp_parser *parser)
26673 {
26674 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
26675 {
26676 /* In C++98 the `template' keyword can only be used within templates;
26677 outside templates the parser can always figure out what is a
26678 template and what is not. In C++11, per the resolution of DR 468,
26679 `template' is allowed in cases where it is not strictly necessary. */
26680 if (!processing_template_decl
26681 && pedantic && cxx_dialect == cxx98)
26682 {
26683 cp_token *token = cp_lexer_peek_token (parser->lexer);
26684 pedwarn (token->location, OPT_Wpedantic,
26685 "in C++98 %<template%> (as a disambiguator) is only "
26686 "allowed within templates");
26687 /* If this part of the token stream is rescanned, the same
26688 error message would be generated. So, we purge the token
26689 from the stream. */
26690 cp_lexer_purge_token (parser->lexer);
26691 return false;
26692 }
26693 else
26694 {
26695 /* Consume the `template' keyword. */
26696 cp_lexer_consume_token (parser->lexer);
26697 return true;
26698 }
26699 }
26700 return false;
26701 }
26702
26703 /* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
26704 set PARSER->SCOPE, and perform other related actions. */
26705
26706 static void
26707 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
26708 {
26709 int i;
26710 struct tree_check *check_value;
26711 deferred_access_check *chk;
26712 vec<deferred_access_check, va_gc> *checks;
26713
26714 /* Get the stored value. */
26715 check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
26716 /* Perform any access checks that were deferred. */
26717 checks = check_value->checks;
26718 if (checks)
26719 {
26720 FOR_EACH_VEC_SAFE_ELT (checks, i, chk)
26721 perform_or_defer_access_check (chk->binfo,
26722 chk->decl,
26723 chk->diag_decl, tf_warning_or_error);
26724 }
26725 /* Set the scope from the stored value. */
26726 parser->scope = check_value->value;
26727 parser->qualifying_scope = check_value->qualifying_scope;
26728 parser->object_scope = NULL_TREE;
26729 }
26730
26731 /* Consume tokens up through a non-nested END token. Returns TRUE if we
26732 encounter the end of a block before what we were looking for. */
26733
26734 static bool
26735 cp_parser_cache_group (cp_parser *parser,
26736 enum cpp_ttype end,
26737 unsigned depth)
26738 {
26739 while (true)
26740 {
26741 cp_token *token = cp_lexer_peek_token (parser->lexer);
26742
26743 /* Abort a parenthesized expression if we encounter a semicolon. */
26744 if ((end == CPP_CLOSE_PAREN || depth == 0)
26745 && token->type == CPP_SEMICOLON)
26746 return true;
26747 /* If we've reached the end of the file, stop. */
26748 if (token->type == CPP_EOF
26749 || (end != CPP_PRAGMA_EOL
26750 && token->type == CPP_PRAGMA_EOL))
26751 return true;
26752 if (token->type == CPP_CLOSE_BRACE && depth == 0)
26753 /* We've hit the end of an enclosing block, so there's been some
26754 kind of syntax error. */
26755 return true;
26756
26757 /* Consume the token. */
26758 cp_lexer_consume_token (parser->lexer);
26759 /* See if it starts a new group. */
26760 if (token->type == CPP_OPEN_BRACE)
26761 {
26762 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
26763 /* In theory this should probably check end == '}', but
26764 cp_parser_save_member_function_body needs it to exit
26765 after either '}' or ')' when called with ')'. */
26766 if (depth == 0)
26767 return false;
26768 }
26769 else if (token->type == CPP_OPEN_PAREN)
26770 {
26771 cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
26772 if (depth == 0 && end == CPP_CLOSE_PAREN)
26773 return false;
26774 }
26775 else if (token->type == CPP_PRAGMA)
26776 cp_parser_cache_group (parser, CPP_PRAGMA_EOL, depth + 1);
26777 else if (token->type == end)
26778 return false;
26779 }
26780 }
26781
26782 /* Like above, for caching a default argument or NSDMI. Both of these are
26783 terminated by a non-nested comma, but it can be unclear whether or not a
26784 comma is nested in a template argument list unless we do more parsing.
26785 In order to handle this ambiguity, when we encounter a ',' after a '<'
26786 we try to parse what follows as a parameter-declaration-list (in the
26787 case of a default argument) or a member-declarator (in the case of an
26788 NSDMI). If that succeeds, then we stop caching. */
26789
26790 static tree
26791 cp_parser_cache_defarg (cp_parser *parser, bool nsdmi)
26792 {
26793 unsigned depth = 0;
26794 int maybe_template_id = 0;
26795 cp_token *first_token;
26796 cp_token *token;
26797 tree default_argument;
26798
26799 /* Add tokens until we have processed the entire default
26800 argument. We add the range [first_token, token). */
26801 first_token = cp_lexer_peek_token (parser->lexer);
26802 if (first_token->type == CPP_OPEN_BRACE)
26803 {
26804 /* For list-initialization, this is straightforward. */
26805 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
26806 token = cp_lexer_peek_token (parser->lexer);
26807 }
26808 else while (true)
26809 {
26810 bool done = false;
26811
26812 /* Peek at the next token. */
26813 token = cp_lexer_peek_token (parser->lexer);
26814 /* What we do depends on what token we have. */
26815 switch (token->type)
26816 {
26817 /* In valid code, a default argument must be
26818 immediately followed by a `,' `)', or `...'. */
26819 case CPP_COMMA:
26820 if (depth == 0 && maybe_template_id)
26821 {
26822 /* If we've seen a '<', we might be in a
26823 template-argument-list. Until Core issue 325 is
26824 resolved, we don't know how this situation ought
26825 to be handled, so try to DTRT. We check whether
26826 what comes after the comma is a valid parameter
26827 declaration list. If it is, then the comma ends
26828 the default argument; otherwise the default
26829 argument continues. */
26830 bool error = false;
26831 cp_token *peek;
26832
26833 /* Set ITALP so cp_parser_parameter_declaration_list
26834 doesn't decide to commit to this parse. */
26835 bool saved_italp = parser->in_template_argument_list_p;
26836 parser->in_template_argument_list_p = true;
26837
26838 cp_parser_parse_tentatively (parser);
26839
26840 if (nsdmi)
26841 {
26842 /* Parse declarators until we reach a non-comma or
26843 somthing that cannot be an initializer.
26844 Just checking whether we're looking at a single
26845 declarator is insufficient. Consider:
26846 int var = tuple<T,U>::x;
26847 The template parameter 'U' looks exactly like a
26848 declarator. */
26849 do
26850 {
26851 int ctor_dtor_or_conv_p;
26852 cp_lexer_consume_token (parser->lexer);
26853 cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
26854 &ctor_dtor_or_conv_p,
26855 /*parenthesized_p=*/NULL,
26856 /*member_p=*/true,
26857 /*friend_p=*/false);
26858 peek = cp_lexer_peek_token (parser->lexer);
26859 if (cp_parser_error_occurred (parser))
26860 break;
26861 }
26862 while (peek->type == CPP_COMMA);
26863 /* If we met an '=' or ';' then the original comma
26864 was the end of the NSDMI. Otherwise assume
26865 we're still in the NSDMI. */
26866 error = (peek->type != CPP_EQ
26867 && peek->type != CPP_SEMICOLON);
26868 }
26869 else
26870 {
26871 cp_lexer_consume_token (parser->lexer);
26872 begin_scope (sk_function_parms, NULL_TREE);
26873 cp_parser_parameter_declaration_list (parser, &error);
26874 pop_bindings_and_leave_scope ();
26875 }
26876 if (!cp_parser_error_occurred (parser) && !error)
26877 done = true;
26878 cp_parser_abort_tentative_parse (parser);
26879
26880 parser->in_template_argument_list_p = saved_italp;
26881 break;
26882 }
26883 case CPP_CLOSE_PAREN:
26884 case CPP_ELLIPSIS:
26885 /* If we run into a non-nested `;', `}', or `]',
26886 then the code is invalid -- but the default
26887 argument is certainly over. */
26888 case CPP_SEMICOLON:
26889 case CPP_CLOSE_BRACE:
26890 case CPP_CLOSE_SQUARE:
26891 if (depth == 0
26892 /* Handle correctly int n = sizeof ... ( p ); */
26893 && token->type != CPP_ELLIPSIS)
26894 done = true;
26895 /* Update DEPTH, if necessary. */
26896 else if (token->type == CPP_CLOSE_PAREN
26897 || token->type == CPP_CLOSE_BRACE
26898 || token->type == CPP_CLOSE_SQUARE)
26899 --depth;
26900 break;
26901
26902 case CPP_OPEN_PAREN:
26903 case CPP_OPEN_SQUARE:
26904 case CPP_OPEN_BRACE:
26905 ++depth;
26906 break;
26907
26908 case CPP_LESS:
26909 if (depth == 0)
26910 /* This might be the comparison operator, or it might
26911 start a template argument list. */
26912 ++maybe_template_id;
26913 break;
26914
26915 case CPP_RSHIFT:
26916 if (cxx_dialect == cxx98)
26917 break;
26918 /* Fall through for C++0x, which treats the `>>'
26919 operator like two `>' tokens in certain
26920 cases. */
26921
26922 case CPP_GREATER:
26923 if (depth == 0)
26924 {
26925 /* This might be an operator, or it might close a
26926 template argument list. But if a previous '<'
26927 started a template argument list, this will have
26928 closed it, so we can't be in one anymore. */
26929 maybe_template_id -= 1 + (token->type == CPP_RSHIFT);
26930 if (maybe_template_id < 0)
26931 maybe_template_id = 0;
26932 }
26933 break;
26934
26935 /* If we run out of tokens, issue an error message. */
26936 case CPP_EOF:
26937 case CPP_PRAGMA_EOL:
26938 error_at (token->location, "file ends in default argument");
26939 done = true;
26940 break;
26941
26942 case CPP_NAME:
26943 case CPP_SCOPE:
26944 /* In these cases, we should look for template-ids.
26945 For example, if the default argument is
26946 `X<int, double>()', we need to do name lookup to
26947 figure out whether or not `X' is a template; if
26948 so, the `,' does not end the default argument.
26949
26950 That is not yet done. */
26951 break;
26952
26953 default:
26954 break;
26955 }
26956
26957 /* If we've reached the end, stop. */
26958 if (done)
26959 break;
26960
26961 /* Add the token to the token block. */
26962 token = cp_lexer_consume_token (parser->lexer);
26963 }
26964
26965 /* Create a DEFAULT_ARG to represent the unparsed default
26966 argument. */
26967 default_argument = make_node (DEFAULT_ARG);
26968 DEFARG_TOKENS (default_argument)
26969 = cp_token_cache_new (first_token, token);
26970 DEFARG_INSTANTIATIONS (default_argument) = NULL;
26971
26972 return default_argument;
26973 }
26974
26975 /* Begin parsing tentatively. We always save tokens while parsing
26976 tentatively so that if the tentative parsing fails we can restore the
26977 tokens. */
26978
26979 static void
26980 cp_parser_parse_tentatively (cp_parser* parser)
26981 {
26982 /* Enter a new parsing context. */
26983 parser->context = cp_parser_context_new (parser->context);
26984 /* Begin saving tokens. */
26985 cp_lexer_save_tokens (parser->lexer);
26986 /* In order to avoid repetitive access control error messages,
26987 access checks are queued up until we are no longer parsing
26988 tentatively. */
26989 push_deferring_access_checks (dk_deferred);
26990 }
26991
26992 /* Commit to the currently active tentative parse. */
26993
26994 static void
26995 cp_parser_commit_to_tentative_parse (cp_parser* parser)
26996 {
26997 cp_parser_context *context;
26998 cp_lexer *lexer;
26999
27000 /* Mark all of the levels as committed. */
27001 lexer = parser->lexer;
27002 for (context = parser->context; context->next; context = context->next)
27003 {
27004 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
27005 break;
27006 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
27007 while (!cp_lexer_saving_tokens (lexer))
27008 lexer = lexer->next;
27009 cp_lexer_commit_tokens (lexer);
27010 }
27011 }
27012
27013 /* Commit to the topmost currently active tentative parse.
27014
27015 Note that this function shouldn't be called when there are
27016 irreversible side-effects while in a tentative state. For
27017 example, we shouldn't create a permanent entry in the symbol
27018 table, or issue an error message that might not apply if the
27019 tentative parse is aborted. */
27020
27021 static void
27022 cp_parser_commit_to_topmost_tentative_parse (cp_parser* parser)
27023 {
27024 cp_parser_context *context = parser->context;
27025 cp_lexer *lexer = parser->lexer;
27026
27027 if (context)
27028 {
27029 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
27030 return;
27031 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
27032
27033 while (!cp_lexer_saving_tokens (lexer))
27034 lexer = lexer->next;
27035 cp_lexer_commit_tokens (lexer);
27036 }
27037 }
27038
27039 /* Abort the currently active tentative parse. All consumed tokens
27040 will be rolled back, and no diagnostics will be issued. */
27041
27042 static void
27043 cp_parser_abort_tentative_parse (cp_parser* parser)
27044 {
27045 gcc_assert (parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED
27046 || errorcount > 0);
27047 cp_parser_simulate_error (parser);
27048 /* Now, pretend that we want to see if the construct was
27049 successfully parsed. */
27050 cp_parser_parse_definitely (parser);
27051 }
27052
27053 /* Stop parsing tentatively. If a parse error has occurred, restore the
27054 token stream. Otherwise, commit to the tokens we have consumed.
27055 Returns true if no error occurred; false otherwise. */
27056
27057 static bool
27058 cp_parser_parse_definitely (cp_parser* parser)
27059 {
27060 bool error_occurred;
27061 cp_parser_context *context;
27062
27063 /* Remember whether or not an error occurred, since we are about to
27064 destroy that information. */
27065 error_occurred = cp_parser_error_occurred (parser);
27066 /* Remove the topmost context from the stack. */
27067 context = parser->context;
27068 parser->context = context->next;
27069 /* If no parse errors occurred, commit to the tentative parse. */
27070 if (!error_occurred)
27071 {
27072 /* Commit to the tokens read tentatively, unless that was
27073 already done. */
27074 if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
27075 cp_lexer_commit_tokens (parser->lexer);
27076
27077 pop_to_parent_deferring_access_checks ();
27078 }
27079 /* Otherwise, if errors occurred, roll back our state so that things
27080 are just as they were before we began the tentative parse. */
27081 else
27082 {
27083 cp_lexer_rollback_tokens (parser->lexer);
27084 pop_deferring_access_checks ();
27085 }
27086 /* Add the context to the front of the free list. */
27087 context->next = cp_parser_context_free_list;
27088 cp_parser_context_free_list = context;
27089
27090 return !error_occurred;
27091 }
27092
27093 /* Returns true if we are parsing tentatively and are not committed to
27094 this tentative parse. */
27095
27096 static bool
27097 cp_parser_uncommitted_to_tentative_parse_p (cp_parser* parser)
27098 {
27099 return (cp_parser_parsing_tentatively (parser)
27100 && parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED);
27101 }
27102
27103 /* Returns nonzero iff an error has occurred during the most recent
27104 tentative parse. */
27105
27106 static bool
27107 cp_parser_error_occurred (cp_parser* parser)
27108 {
27109 return (cp_parser_parsing_tentatively (parser)
27110 && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
27111 }
27112
27113 /* Returns nonzero if GNU extensions are allowed. */
27114
27115 static bool
27116 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
27117 {
27118 return parser->allow_gnu_extensions_p;
27119 }
27120 \f
27121 /* Objective-C++ Productions */
27122
27123
27124 /* Parse an Objective-C expression, which feeds into a primary-expression
27125 above.
27126
27127 objc-expression:
27128 objc-message-expression
27129 objc-string-literal
27130 objc-encode-expression
27131 objc-protocol-expression
27132 objc-selector-expression
27133
27134 Returns a tree representation of the expression. */
27135
27136 static tree
27137 cp_parser_objc_expression (cp_parser* parser)
27138 {
27139 /* Try to figure out what kind of declaration is present. */
27140 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
27141
27142 switch (kwd->type)
27143 {
27144 case CPP_OPEN_SQUARE:
27145 return cp_parser_objc_message_expression (parser);
27146
27147 case CPP_OBJC_STRING:
27148 kwd = cp_lexer_consume_token (parser->lexer);
27149 return objc_build_string_object (kwd->u.value);
27150
27151 case CPP_KEYWORD:
27152 switch (kwd->keyword)
27153 {
27154 case RID_AT_ENCODE:
27155 return cp_parser_objc_encode_expression (parser);
27156
27157 case RID_AT_PROTOCOL:
27158 return cp_parser_objc_protocol_expression (parser);
27159
27160 case RID_AT_SELECTOR:
27161 return cp_parser_objc_selector_expression (parser);
27162
27163 default:
27164 break;
27165 }
27166 default:
27167 error_at (kwd->location,
27168 "misplaced %<@%D%> Objective-C++ construct",
27169 kwd->u.value);
27170 cp_parser_skip_to_end_of_block_or_statement (parser);
27171 }
27172
27173 return error_mark_node;
27174 }
27175
27176 /* Parse an Objective-C message expression.
27177
27178 objc-message-expression:
27179 [ objc-message-receiver objc-message-args ]
27180
27181 Returns a representation of an Objective-C message. */
27182
27183 static tree
27184 cp_parser_objc_message_expression (cp_parser* parser)
27185 {
27186 tree receiver, messageargs;
27187
27188 cp_lexer_consume_token (parser->lexer); /* Eat '['. */
27189 receiver = cp_parser_objc_message_receiver (parser);
27190 messageargs = cp_parser_objc_message_args (parser);
27191 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
27192
27193 return objc_build_message_expr (receiver, messageargs);
27194 }
27195
27196 /* Parse an objc-message-receiver.
27197
27198 objc-message-receiver:
27199 expression
27200 simple-type-specifier
27201
27202 Returns a representation of the type or expression. */
27203
27204 static tree
27205 cp_parser_objc_message_receiver (cp_parser* parser)
27206 {
27207 tree rcv;
27208
27209 /* An Objective-C message receiver may be either (1) a type
27210 or (2) an expression. */
27211 cp_parser_parse_tentatively (parser);
27212 rcv = cp_parser_expression (parser);
27213
27214 /* If that worked out, fine. */
27215 if (cp_parser_parse_definitely (parser))
27216 return rcv;
27217
27218 cp_parser_parse_tentatively (parser);
27219 rcv = cp_parser_simple_type_specifier (parser,
27220 /*decl_specs=*/NULL,
27221 CP_PARSER_FLAGS_NONE);
27222
27223 if (cp_parser_parse_definitely (parser))
27224 return objc_get_class_reference (rcv);
27225
27226 cp_parser_error (parser, "objective-c++ message receiver expected");
27227 return error_mark_node;
27228 }
27229
27230 /* Parse the arguments and selectors comprising an Objective-C message.
27231
27232 objc-message-args:
27233 objc-selector
27234 objc-selector-args
27235 objc-selector-args , objc-comma-args
27236
27237 objc-selector-args:
27238 objc-selector [opt] : assignment-expression
27239 objc-selector-args objc-selector [opt] : assignment-expression
27240
27241 objc-comma-args:
27242 assignment-expression
27243 objc-comma-args , assignment-expression
27244
27245 Returns a TREE_LIST, with TREE_PURPOSE containing a list of
27246 selector arguments and TREE_VALUE containing a list of comma
27247 arguments. */
27248
27249 static tree
27250 cp_parser_objc_message_args (cp_parser* parser)
27251 {
27252 tree sel_args = NULL_TREE, addl_args = NULL_TREE;
27253 bool maybe_unary_selector_p = true;
27254 cp_token *token = cp_lexer_peek_token (parser->lexer);
27255
27256 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
27257 {
27258 tree selector = NULL_TREE, arg;
27259
27260 if (token->type != CPP_COLON)
27261 selector = cp_parser_objc_selector (parser);
27262
27263 /* Detect if we have a unary selector. */
27264 if (maybe_unary_selector_p
27265 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
27266 return build_tree_list (selector, NULL_TREE);
27267
27268 maybe_unary_selector_p = false;
27269 cp_parser_require (parser, CPP_COLON, RT_COLON);
27270 arg = cp_parser_assignment_expression (parser);
27271
27272 sel_args
27273 = chainon (sel_args,
27274 build_tree_list (selector, arg));
27275
27276 token = cp_lexer_peek_token (parser->lexer);
27277 }
27278
27279 /* Handle non-selector arguments, if any. */
27280 while (token->type == CPP_COMMA)
27281 {
27282 tree arg;
27283
27284 cp_lexer_consume_token (parser->lexer);
27285 arg = cp_parser_assignment_expression (parser);
27286
27287 addl_args
27288 = chainon (addl_args,
27289 build_tree_list (NULL_TREE, arg));
27290
27291 token = cp_lexer_peek_token (parser->lexer);
27292 }
27293
27294 if (sel_args == NULL_TREE && addl_args == NULL_TREE)
27295 {
27296 cp_parser_error (parser, "objective-c++ message argument(s) are expected");
27297 return build_tree_list (error_mark_node, error_mark_node);
27298 }
27299
27300 return build_tree_list (sel_args, addl_args);
27301 }
27302
27303 /* Parse an Objective-C encode expression.
27304
27305 objc-encode-expression:
27306 @encode objc-typename
27307
27308 Returns an encoded representation of the type argument. */
27309
27310 static tree
27311 cp_parser_objc_encode_expression (cp_parser* parser)
27312 {
27313 tree type;
27314 cp_token *token;
27315
27316 cp_lexer_consume_token (parser->lexer); /* Eat '@encode'. */
27317 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
27318 token = cp_lexer_peek_token (parser->lexer);
27319 type = complete_type (cp_parser_type_id (parser));
27320 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
27321
27322 if (!type)
27323 {
27324 error_at (token->location,
27325 "%<@encode%> must specify a type as an argument");
27326 return error_mark_node;
27327 }
27328
27329 /* This happens if we find @encode(T) (where T is a template
27330 typename or something dependent on a template typename) when
27331 parsing a template. In that case, we can't compile it
27332 immediately, but we rather create an AT_ENCODE_EXPR which will
27333 need to be instantiated when the template is used.
27334 */
27335 if (dependent_type_p (type))
27336 {
27337 tree value = build_min (AT_ENCODE_EXPR, size_type_node, type);
27338 TREE_READONLY (value) = 1;
27339 return value;
27340 }
27341
27342 return objc_build_encode_expr (type);
27343 }
27344
27345 /* Parse an Objective-C @defs expression. */
27346
27347 static tree
27348 cp_parser_objc_defs_expression (cp_parser *parser)
27349 {
27350 tree name;
27351
27352 cp_lexer_consume_token (parser->lexer); /* Eat '@defs'. */
27353 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
27354 name = cp_parser_identifier (parser);
27355 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
27356
27357 return objc_get_class_ivars (name);
27358 }
27359
27360 /* Parse an Objective-C protocol expression.
27361
27362 objc-protocol-expression:
27363 @protocol ( identifier )
27364
27365 Returns a representation of the protocol expression. */
27366
27367 static tree
27368 cp_parser_objc_protocol_expression (cp_parser* parser)
27369 {
27370 tree proto;
27371
27372 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
27373 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
27374 proto = cp_parser_identifier (parser);
27375 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
27376
27377 return objc_build_protocol_expr (proto);
27378 }
27379
27380 /* Parse an Objective-C selector expression.
27381
27382 objc-selector-expression:
27383 @selector ( objc-method-signature )
27384
27385 objc-method-signature:
27386 objc-selector
27387 objc-selector-seq
27388
27389 objc-selector-seq:
27390 objc-selector :
27391 objc-selector-seq objc-selector :
27392
27393 Returns a representation of the method selector. */
27394
27395 static tree
27396 cp_parser_objc_selector_expression (cp_parser* parser)
27397 {
27398 tree sel_seq = NULL_TREE;
27399 bool maybe_unary_selector_p = true;
27400 cp_token *token;
27401 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
27402
27403 cp_lexer_consume_token (parser->lexer); /* Eat '@selector'. */
27404 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
27405 token = cp_lexer_peek_token (parser->lexer);
27406
27407 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON
27408 || token->type == CPP_SCOPE)
27409 {
27410 tree selector = NULL_TREE;
27411
27412 if (token->type != CPP_COLON
27413 || token->type == CPP_SCOPE)
27414 selector = cp_parser_objc_selector (parser);
27415
27416 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON)
27417 && cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
27418 {
27419 /* Detect if we have a unary selector. */
27420 if (maybe_unary_selector_p)
27421 {
27422 sel_seq = selector;
27423 goto finish_selector;
27424 }
27425 else
27426 {
27427 cp_parser_error (parser, "expected %<:%>");
27428 }
27429 }
27430 maybe_unary_selector_p = false;
27431 token = cp_lexer_consume_token (parser->lexer);
27432
27433 if (token->type == CPP_SCOPE)
27434 {
27435 sel_seq
27436 = chainon (sel_seq,
27437 build_tree_list (selector, NULL_TREE));
27438 sel_seq
27439 = chainon (sel_seq,
27440 build_tree_list (NULL_TREE, NULL_TREE));
27441 }
27442 else
27443 sel_seq
27444 = chainon (sel_seq,
27445 build_tree_list (selector, NULL_TREE));
27446
27447 token = cp_lexer_peek_token (parser->lexer);
27448 }
27449
27450 finish_selector:
27451 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
27452
27453 return objc_build_selector_expr (loc, sel_seq);
27454 }
27455
27456 /* Parse a list of identifiers.
27457
27458 objc-identifier-list:
27459 identifier
27460 objc-identifier-list , identifier
27461
27462 Returns a TREE_LIST of identifier nodes. */
27463
27464 static tree
27465 cp_parser_objc_identifier_list (cp_parser* parser)
27466 {
27467 tree identifier;
27468 tree list;
27469 cp_token *sep;
27470
27471 identifier = cp_parser_identifier (parser);
27472 if (identifier == error_mark_node)
27473 return error_mark_node;
27474
27475 list = build_tree_list (NULL_TREE, identifier);
27476 sep = cp_lexer_peek_token (parser->lexer);
27477
27478 while (sep->type == CPP_COMMA)
27479 {
27480 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
27481 identifier = cp_parser_identifier (parser);
27482 if (identifier == error_mark_node)
27483 return list;
27484
27485 list = chainon (list, build_tree_list (NULL_TREE,
27486 identifier));
27487 sep = cp_lexer_peek_token (parser->lexer);
27488 }
27489
27490 return list;
27491 }
27492
27493 /* Parse an Objective-C alias declaration.
27494
27495 objc-alias-declaration:
27496 @compatibility_alias identifier identifier ;
27497
27498 This function registers the alias mapping with the Objective-C front end.
27499 It returns nothing. */
27500
27501 static void
27502 cp_parser_objc_alias_declaration (cp_parser* parser)
27503 {
27504 tree alias, orig;
27505
27506 cp_lexer_consume_token (parser->lexer); /* Eat '@compatibility_alias'. */
27507 alias = cp_parser_identifier (parser);
27508 orig = cp_parser_identifier (parser);
27509 objc_declare_alias (alias, orig);
27510 cp_parser_consume_semicolon_at_end_of_statement (parser);
27511 }
27512
27513 /* Parse an Objective-C class forward-declaration.
27514
27515 objc-class-declaration:
27516 @class objc-identifier-list ;
27517
27518 The function registers the forward declarations with the Objective-C
27519 front end. It returns nothing. */
27520
27521 static void
27522 cp_parser_objc_class_declaration (cp_parser* parser)
27523 {
27524 cp_lexer_consume_token (parser->lexer); /* Eat '@class'. */
27525 while (true)
27526 {
27527 tree id;
27528
27529 id = cp_parser_identifier (parser);
27530 if (id == error_mark_node)
27531 break;
27532
27533 objc_declare_class (id);
27534
27535 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
27536 cp_lexer_consume_token (parser->lexer);
27537 else
27538 break;
27539 }
27540 cp_parser_consume_semicolon_at_end_of_statement (parser);
27541 }
27542
27543 /* Parse a list of Objective-C protocol references.
27544
27545 objc-protocol-refs-opt:
27546 objc-protocol-refs [opt]
27547
27548 objc-protocol-refs:
27549 < objc-identifier-list >
27550
27551 Returns a TREE_LIST of identifiers, if any. */
27552
27553 static tree
27554 cp_parser_objc_protocol_refs_opt (cp_parser* parser)
27555 {
27556 tree protorefs = NULL_TREE;
27557
27558 if(cp_lexer_next_token_is (parser->lexer, CPP_LESS))
27559 {
27560 cp_lexer_consume_token (parser->lexer); /* Eat '<'. */
27561 protorefs = cp_parser_objc_identifier_list (parser);
27562 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
27563 }
27564
27565 return protorefs;
27566 }
27567
27568 /* Parse a Objective-C visibility specification. */
27569
27570 static void
27571 cp_parser_objc_visibility_spec (cp_parser* parser)
27572 {
27573 cp_token *vis = cp_lexer_peek_token (parser->lexer);
27574
27575 switch (vis->keyword)
27576 {
27577 case RID_AT_PRIVATE:
27578 objc_set_visibility (OBJC_IVAR_VIS_PRIVATE);
27579 break;
27580 case RID_AT_PROTECTED:
27581 objc_set_visibility (OBJC_IVAR_VIS_PROTECTED);
27582 break;
27583 case RID_AT_PUBLIC:
27584 objc_set_visibility (OBJC_IVAR_VIS_PUBLIC);
27585 break;
27586 case RID_AT_PACKAGE:
27587 objc_set_visibility (OBJC_IVAR_VIS_PACKAGE);
27588 break;
27589 default:
27590 return;
27591 }
27592
27593 /* Eat '@private'/'@protected'/'@public'. */
27594 cp_lexer_consume_token (parser->lexer);
27595 }
27596
27597 /* Parse an Objective-C method type. Return 'true' if it is a class
27598 (+) method, and 'false' if it is an instance (-) method. */
27599
27600 static inline bool
27601 cp_parser_objc_method_type (cp_parser* parser)
27602 {
27603 if (cp_lexer_consume_token (parser->lexer)->type == CPP_PLUS)
27604 return true;
27605 else
27606 return false;
27607 }
27608
27609 /* Parse an Objective-C protocol qualifier. */
27610
27611 static tree
27612 cp_parser_objc_protocol_qualifiers (cp_parser* parser)
27613 {
27614 tree quals = NULL_TREE, node;
27615 cp_token *token = cp_lexer_peek_token (parser->lexer);
27616
27617 node = token->u.value;
27618
27619 while (node && identifier_p (node)
27620 && (node == ridpointers [(int) RID_IN]
27621 || node == ridpointers [(int) RID_OUT]
27622 || node == ridpointers [(int) RID_INOUT]
27623 || node == ridpointers [(int) RID_BYCOPY]
27624 || node == ridpointers [(int) RID_BYREF]
27625 || node == ridpointers [(int) RID_ONEWAY]))
27626 {
27627 quals = tree_cons (NULL_TREE, node, quals);
27628 cp_lexer_consume_token (parser->lexer);
27629 token = cp_lexer_peek_token (parser->lexer);
27630 node = token->u.value;
27631 }
27632
27633 return quals;
27634 }
27635
27636 /* Parse an Objective-C typename. */
27637
27638 static tree
27639 cp_parser_objc_typename (cp_parser* parser)
27640 {
27641 tree type_name = NULL_TREE;
27642
27643 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
27644 {
27645 tree proto_quals, cp_type = NULL_TREE;
27646
27647 cp_lexer_consume_token (parser->lexer); /* Eat '('. */
27648 proto_quals = cp_parser_objc_protocol_qualifiers (parser);
27649
27650 /* An ObjC type name may consist of just protocol qualifiers, in which
27651 case the type shall default to 'id'. */
27652 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
27653 {
27654 cp_type = cp_parser_type_id (parser);
27655
27656 /* If the type could not be parsed, an error has already
27657 been produced. For error recovery, behave as if it had
27658 not been specified, which will use the default type
27659 'id'. */
27660 if (cp_type == error_mark_node)
27661 {
27662 cp_type = NULL_TREE;
27663 /* We need to skip to the closing parenthesis as
27664 cp_parser_type_id() does not seem to do it for
27665 us. */
27666 cp_parser_skip_to_closing_parenthesis (parser,
27667 /*recovering=*/true,
27668 /*or_comma=*/false,
27669 /*consume_paren=*/false);
27670 }
27671 }
27672
27673 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
27674 type_name = build_tree_list (proto_quals, cp_type);
27675 }
27676
27677 return type_name;
27678 }
27679
27680 /* Check to see if TYPE refers to an Objective-C selector name. */
27681
27682 static bool
27683 cp_parser_objc_selector_p (enum cpp_ttype type)
27684 {
27685 return (type == CPP_NAME || type == CPP_KEYWORD
27686 || type == CPP_AND_AND || type == CPP_AND_EQ || type == CPP_AND
27687 || type == CPP_OR || type == CPP_COMPL || type == CPP_NOT
27688 || type == CPP_NOT_EQ || type == CPP_OR_OR || type == CPP_OR_EQ
27689 || type == CPP_XOR || type == CPP_XOR_EQ);
27690 }
27691
27692 /* Parse an Objective-C selector. */
27693
27694 static tree
27695 cp_parser_objc_selector (cp_parser* parser)
27696 {
27697 cp_token *token = cp_lexer_consume_token (parser->lexer);
27698
27699 if (!cp_parser_objc_selector_p (token->type))
27700 {
27701 error_at (token->location, "invalid Objective-C++ selector name");
27702 return error_mark_node;
27703 }
27704
27705 /* C++ operator names are allowed to appear in ObjC selectors. */
27706 switch (token->type)
27707 {
27708 case CPP_AND_AND: return get_identifier ("and");
27709 case CPP_AND_EQ: return get_identifier ("and_eq");
27710 case CPP_AND: return get_identifier ("bitand");
27711 case CPP_OR: return get_identifier ("bitor");
27712 case CPP_COMPL: return get_identifier ("compl");
27713 case CPP_NOT: return get_identifier ("not");
27714 case CPP_NOT_EQ: return get_identifier ("not_eq");
27715 case CPP_OR_OR: return get_identifier ("or");
27716 case CPP_OR_EQ: return get_identifier ("or_eq");
27717 case CPP_XOR: return get_identifier ("xor");
27718 case CPP_XOR_EQ: return get_identifier ("xor_eq");
27719 default: return token->u.value;
27720 }
27721 }
27722
27723 /* Parse an Objective-C params list. */
27724
27725 static tree
27726 cp_parser_objc_method_keyword_params (cp_parser* parser, tree* attributes)
27727 {
27728 tree params = NULL_TREE;
27729 bool maybe_unary_selector_p = true;
27730 cp_token *token = cp_lexer_peek_token (parser->lexer);
27731
27732 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
27733 {
27734 tree selector = NULL_TREE, type_name, identifier;
27735 tree parm_attr = NULL_TREE;
27736
27737 if (token->keyword == RID_ATTRIBUTE)
27738 break;
27739
27740 if (token->type != CPP_COLON)
27741 selector = cp_parser_objc_selector (parser);
27742
27743 /* Detect if we have a unary selector. */
27744 if (maybe_unary_selector_p
27745 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
27746 {
27747 params = selector; /* Might be followed by attributes. */
27748 break;
27749 }
27750
27751 maybe_unary_selector_p = false;
27752 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
27753 {
27754 /* Something went quite wrong. There should be a colon
27755 here, but there is not. Stop parsing parameters. */
27756 break;
27757 }
27758 type_name = cp_parser_objc_typename (parser);
27759 /* New ObjC allows attributes on parameters too. */
27760 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
27761 parm_attr = cp_parser_attributes_opt (parser);
27762 identifier = cp_parser_identifier (parser);
27763
27764 params
27765 = chainon (params,
27766 objc_build_keyword_decl (selector,
27767 type_name,
27768 identifier,
27769 parm_attr));
27770
27771 token = cp_lexer_peek_token (parser->lexer);
27772 }
27773
27774 if (params == NULL_TREE)
27775 {
27776 cp_parser_error (parser, "objective-c++ method declaration is expected");
27777 return error_mark_node;
27778 }
27779
27780 /* We allow tail attributes for the method. */
27781 if (token->keyword == RID_ATTRIBUTE)
27782 {
27783 *attributes = cp_parser_attributes_opt (parser);
27784 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
27785 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
27786 return params;
27787 cp_parser_error (parser,
27788 "method attributes must be specified at the end");
27789 return error_mark_node;
27790 }
27791
27792 if (params == NULL_TREE)
27793 {
27794 cp_parser_error (parser, "objective-c++ method declaration is expected");
27795 return error_mark_node;
27796 }
27797 return params;
27798 }
27799
27800 /* Parse the non-keyword Objective-C params. */
27801
27802 static tree
27803 cp_parser_objc_method_tail_params_opt (cp_parser* parser, bool *ellipsisp,
27804 tree* attributes)
27805 {
27806 tree params = make_node (TREE_LIST);
27807 cp_token *token = cp_lexer_peek_token (parser->lexer);
27808 *ellipsisp = false; /* Initially, assume no ellipsis. */
27809
27810 while (token->type == CPP_COMMA)
27811 {
27812 cp_parameter_declarator *parmdecl;
27813 tree parm;
27814
27815 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
27816 token = cp_lexer_peek_token (parser->lexer);
27817
27818 if (token->type == CPP_ELLIPSIS)
27819 {
27820 cp_lexer_consume_token (parser->lexer); /* Eat '...'. */
27821 *ellipsisp = true;
27822 token = cp_lexer_peek_token (parser->lexer);
27823 break;
27824 }
27825
27826 /* TODO: parse attributes for tail parameters. */
27827 parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
27828 parm = grokdeclarator (parmdecl->declarator,
27829 &parmdecl->decl_specifiers,
27830 PARM, /*initialized=*/0,
27831 /*attrlist=*/NULL);
27832
27833 chainon (params, build_tree_list (NULL_TREE, parm));
27834 token = cp_lexer_peek_token (parser->lexer);
27835 }
27836
27837 /* We allow tail attributes for the method. */
27838 if (token->keyword == RID_ATTRIBUTE)
27839 {
27840 if (*attributes == NULL_TREE)
27841 {
27842 *attributes = cp_parser_attributes_opt (parser);
27843 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
27844 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
27845 return params;
27846 }
27847 else
27848 /* We have an error, but parse the attributes, so that we can
27849 carry on. */
27850 *attributes = cp_parser_attributes_opt (parser);
27851
27852 cp_parser_error (parser,
27853 "method attributes must be specified at the end");
27854 return error_mark_node;
27855 }
27856
27857 return params;
27858 }
27859
27860 /* Parse a linkage specification, a pragma, an extra semicolon or a block. */
27861
27862 static void
27863 cp_parser_objc_interstitial_code (cp_parser* parser)
27864 {
27865 cp_token *token = cp_lexer_peek_token (parser->lexer);
27866
27867 /* If the next token is `extern' and the following token is a string
27868 literal, then we have a linkage specification. */
27869 if (token->keyword == RID_EXTERN
27870 && cp_parser_is_pure_string_literal
27871 (cp_lexer_peek_nth_token (parser->lexer, 2)))
27872 cp_parser_linkage_specification (parser);
27873 /* Handle #pragma, if any. */
27874 else if (token->type == CPP_PRAGMA)
27875 cp_parser_pragma (parser, pragma_objc_icode);
27876 /* Allow stray semicolons. */
27877 else if (token->type == CPP_SEMICOLON)
27878 cp_lexer_consume_token (parser->lexer);
27879 /* Mark methods as optional or required, when building protocols. */
27880 else if (token->keyword == RID_AT_OPTIONAL)
27881 {
27882 cp_lexer_consume_token (parser->lexer);
27883 objc_set_method_opt (true);
27884 }
27885 else if (token->keyword == RID_AT_REQUIRED)
27886 {
27887 cp_lexer_consume_token (parser->lexer);
27888 objc_set_method_opt (false);
27889 }
27890 else if (token->keyword == RID_NAMESPACE)
27891 cp_parser_namespace_definition (parser);
27892 /* Other stray characters must generate errors. */
27893 else if (token->type == CPP_OPEN_BRACE || token->type == CPP_CLOSE_BRACE)
27894 {
27895 cp_lexer_consume_token (parser->lexer);
27896 error ("stray %qs between Objective-C++ methods",
27897 token->type == CPP_OPEN_BRACE ? "{" : "}");
27898 }
27899 /* Finally, try to parse a block-declaration, or a function-definition. */
27900 else
27901 cp_parser_block_declaration (parser, /*statement_p=*/false);
27902 }
27903
27904 /* Parse a method signature. */
27905
27906 static tree
27907 cp_parser_objc_method_signature (cp_parser* parser, tree* attributes)
27908 {
27909 tree rettype, kwdparms, optparms;
27910 bool ellipsis = false;
27911 bool is_class_method;
27912
27913 is_class_method = cp_parser_objc_method_type (parser);
27914 rettype = cp_parser_objc_typename (parser);
27915 *attributes = NULL_TREE;
27916 kwdparms = cp_parser_objc_method_keyword_params (parser, attributes);
27917 if (kwdparms == error_mark_node)
27918 return error_mark_node;
27919 optparms = cp_parser_objc_method_tail_params_opt (parser, &ellipsis, attributes);
27920 if (optparms == error_mark_node)
27921 return error_mark_node;
27922
27923 return objc_build_method_signature (is_class_method, rettype, kwdparms, optparms, ellipsis);
27924 }
27925
27926 static bool
27927 cp_parser_objc_method_maybe_bad_prefix_attributes (cp_parser* parser)
27928 {
27929 tree tattr;
27930 cp_lexer_save_tokens (parser->lexer);
27931 tattr = cp_parser_attributes_opt (parser);
27932 gcc_assert (tattr) ;
27933
27934 /* If the attributes are followed by a method introducer, this is not allowed.
27935 Dump the attributes and flag the situation. */
27936 if (cp_lexer_next_token_is (parser->lexer, CPP_PLUS)
27937 || cp_lexer_next_token_is (parser->lexer, CPP_MINUS))
27938 return true;
27939
27940 /* Otherwise, the attributes introduce some interstitial code, possibly so
27941 rewind to allow that check. */
27942 cp_lexer_rollback_tokens (parser->lexer);
27943 return false;
27944 }
27945
27946 /* Parse an Objective-C method prototype list. */
27947
27948 static void
27949 cp_parser_objc_method_prototype_list (cp_parser* parser)
27950 {
27951 cp_token *token = cp_lexer_peek_token (parser->lexer);
27952
27953 while (token->keyword != RID_AT_END && token->type != CPP_EOF)
27954 {
27955 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
27956 {
27957 tree attributes, sig;
27958 bool is_class_method;
27959 if (token->type == CPP_PLUS)
27960 is_class_method = true;
27961 else
27962 is_class_method = false;
27963 sig = cp_parser_objc_method_signature (parser, &attributes);
27964 if (sig == error_mark_node)
27965 {
27966 cp_parser_skip_to_end_of_block_or_statement (parser);
27967 token = cp_lexer_peek_token (parser->lexer);
27968 continue;
27969 }
27970 objc_add_method_declaration (is_class_method, sig, attributes);
27971 cp_parser_consume_semicolon_at_end_of_statement (parser);
27972 }
27973 else if (token->keyword == RID_AT_PROPERTY)
27974 cp_parser_objc_at_property_declaration (parser);
27975 else if (token->keyword == RID_ATTRIBUTE
27976 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
27977 warning_at (cp_lexer_peek_token (parser->lexer)->location,
27978 OPT_Wattributes,
27979 "prefix attributes are ignored for methods");
27980 else
27981 /* Allow for interspersed non-ObjC++ code. */
27982 cp_parser_objc_interstitial_code (parser);
27983
27984 token = cp_lexer_peek_token (parser->lexer);
27985 }
27986
27987 if (token->type != CPP_EOF)
27988 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
27989 else
27990 cp_parser_error (parser, "expected %<@end%>");
27991
27992 objc_finish_interface ();
27993 }
27994
27995 /* Parse an Objective-C method definition list. */
27996
27997 static void
27998 cp_parser_objc_method_definition_list (cp_parser* parser)
27999 {
28000 cp_token *token = cp_lexer_peek_token (parser->lexer);
28001
28002 while (token->keyword != RID_AT_END && token->type != CPP_EOF)
28003 {
28004 tree meth;
28005
28006 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
28007 {
28008 cp_token *ptk;
28009 tree sig, attribute;
28010 bool is_class_method;
28011 if (token->type == CPP_PLUS)
28012 is_class_method = true;
28013 else
28014 is_class_method = false;
28015 push_deferring_access_checks (dk_deferred);
28016 sig = cp_parser_objc_method_signature (parser, &attribute);
28017 if (sig == error_mark_node)
28018 {
28019 cp_parser_skip_to_end_of_block_or_statement (parser);
28020 token = cp_lexer_peek_token (parser->lexer);
28021 continue;
28022 }
28023 objc_start_method_definition (is_class_method, sig, attribute,
28024 NULL_TREE);
28025
28026 /* For historical reasons, we accept an optional semicolon. */
28027 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
28028 cp_lexer_consume_token (parser->lexer);
28029
28030 ptk = cp_lexer_peek_token (parser->lexer);
28031 if (!(ptk->type == CPP_PLUS || ptk->type == CPP_MINUS
28032 || ptk->type == CPP_EOF || ptk->keyword == RID_AT_END))
28033 {
28034 perform_deferred_access_checks (tf_warning_or_error);
28035 stop_deferring_access_checks ();
28036 meth = cp_parser_function_definition_after_declarator (parser,
28037 false);
28038 pop_deferring_access_checks ();
28039 objc_finish_method_definition (meth);
28040 }
28041 }
28042 /* The following case will be removed once @synthesize is
28043 completely implemented. */
28044 else if (token->keyword == RID_AT_PROPERTY)
28045 cp_parser_objc_at_property_declaration (parser);
28046 else if (token->keyword == RID_AT_SYNTHESIZE)
28047 cp_parser_objc_at_synthesize_declaration (parser);
28048 else if (token->keyword == RID_AT_DYNAMIC)
28049 cp_parser_objc_at_dynamic_declaration (parser);
28050 else if (token->keyword == RID_ATTRIBUTE
28051 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
28052 warning_at (token->location, OPT_Wattributes,
28053 "prefix attributes are ignored for methods");
28054 else
28055 /* Allow for interspersed non-ObjC++ code. */
28056 cp_parser_objc_interstitial_code (parser);
28057
28058 token = cp_lexer_peek_token (parser->lexer);
28059 }
28060
28061 if (token->type != CPP_EOF)
28062 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
28063 else
28064 cp_parser_error (parser, "expected %<@end%>");
28065
28066 objc_finish_implementation ();
28067 }
28068
28069 /* Parse Objective-C ivars. */
28070
28071 static void
28072 cp_parser_objc_class_ivars (cp_parser* parser)
28073 {
28074 cp_token *token = cp_lexer_peek_token (parser->lexer);
28075
28076 if (token->type != CPP_OPEN_BRACE)
28077 return; /* No ivars specified. */
28078
28079 cp_lexer_consume_token (parser->lexer); /* Eat '{'. */
28080 token = cp_lexer_peek_token (parser->lexer);
28081
28082 while (token->type != CPP_CLOSE_BRACE
28083 && token->keyword != RID_AT_END && token->type != CPP_EOF)
28084 {
28085 cp_decl_specifier_seq declspecs;
28086 int decl_class_or_enum_p;
28087 tree prefix_attributes;
28088
28089 cp_parser_objc_visibility_spec (parser);
28090
28091 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
28092 break;
28093
28094 cp_parser_decl_specifier_seq (parser,
28095 CP_PARSER_FLAGS_OPTIONAL,
28096 &declspecs,
28097 &decl_class_or_enum_p);
28098
28099 /* auto, register, static, extern, mutable. */
28100 if (declspecs.storage_class != sc_none)
28101 {
28102 cp_parser_error (parser, "invalid type for instance variable");
28103 declspecs.storage_class = sc_none;
28104 }
28105
28106 /* thread_local. */
28107 if (decl_spec_seq_has_spec_p (&declspecs, ds_thread))
28108 {
28109 cp_parser_error (parser, "invalid type for instance variable");
28110 declspecs.locations[ds_thread] = 0;
28111 }
28112
28113 /* typedef. */
28114 if (decl_spec_seq_has_spec_p (&declspecs, ds_typedef))
28115 {
28116 cp_parser_error (parser, "invalid type for instance variable");
28117 declspecs.locations[ds_typedef] = 0;
28118 }
28119
28120 prefix_attributes = declspecs.attributes;
28121 declspecs.attributes = NULL_TREE;
28122
28123 /* Keep going until we hit the `;' at the end of the
28124 declaration. */
28125 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
28126 {
28127 tree width = NULL_TREE, attributes, first_attribute, decl;
28128 cp_declarator *declarator = NULL;
28129 int ctor_dtor_or_conv_p;
28130
28131 /* Check for a (possibly unnamed) bitfield declaration. */
28132 token = cp_lexer_peek_token (parser->lexer);
28133 if (token->type == CPP_COLON)
28134 goto eat_colon;
28135
28136 if (token->type == CPP_NAME
28137 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
28138 == CPP_COLON))
28139 {
28140 /* Get the name of the bitfield. */
28141 declarator = make_id_declarator (NULL_TREE,
28142 cp_parser_identifier (parser),
28143 sfk_none);
28144
28145 eat_colon:
28146 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
28147 /* Get the width of the bitfield. */
28148 width
28149 = cp_parser_constant_expression (parser);
28150 }
28151 else
28152 {
28153 /* Parse the declarator. */
28154 declarator
28155 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
28156 &ctor_dtor_or_conv_p,
28157 /*parenthesized_p=*/NULL,
28158 /*member_p=*/false,
28159 /*friend_p=*/false);
28160 }
28161
28162 /* Look for attributes that apply to the ivar. */
28163 attributes = cp_parser_attributes_opt (parser);
28164 /* Remember which attributes are prefix attributes and
28165 which are not. */
28166 first_attribute = attributes;
28167 /* Combine the attributes. */
28168 attributes = chainon (prefix_attributes, attributes);
28169
28170 if (width)
28171 /* Create the bitfield declaration. */
28172 decl = grokbitfield (declarator, &declspecs,
28173 width,
28174 attributes);
28175 else
28176 decl = grokfield (declarator, &declspecs,
28177 NULL_TREE, /*init_const_expr_p=*/false,
28178 NULL_TREE, attributes);
28179
28180 /* Add the instance variable. */
28181 if (decl != error_mark_node && decl != NULL_TREE)
28182 objc_add_instance_variable (decl);
28183
28184 /* Reset PREFIX_ATTRIBUTES. */
28185 while (attributes && TREE_CHAIN (attributes) != first_attribute)
28186 attributes = TREE_CHAIN (attributes);
28187 if (attributes)
28188 TREE_CHAIN (attributes) = NULL_TREE;
28189
28190 token = cp_lexer_peek_token (parser->lexer);
28191
28192 if (token->type == CPP_COMMA)
28193 {
28194 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
28195 continue;
28196 }
28197 break;
28198 }
28199
28200 cp_parser_consume_semicolon_at_end_of_statement (parser);
28201 token = cp_lexer_peek_token (parser->lexer);
28202 }
28203
28204 if (token->keyword == RID_AT_END)
28205 cp_parser_error (parser, "expected %<}%>");
28206
28207 /* Do not consume the RID_AT_END, so it will be read again as terminating
28208 the @interface of @implementation. */
28209 if (token->keyword != RID_AT_END && token->type != CPP_EOF)
28210 cp_lexer_consume_token (parser->lexer); /* Eat '}'. */
28211
28212 /* For historical reasons, we accept an optional semicolon. */
28213 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
28214 cp_lexer_consume_token (parser->lexer);
28215 }
28216
28217 /* Parse an Objective-C protocol declaration. */
28218
28219 static void
28220 cp_parser_objc_protocol_declaration (cp_parser* parser, tree attributes)
28221 {
28222 tree proto, protorefs;
28223 cp_token *tok;
28224
28225 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
28226 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
28227 {
28228 tok = cp_lexer_peek_token (parser->lexer);
28229 error_at (tok->location, "identifier expected after %<@protocol%>");
28230 cp_parser_consume_semicolon_at_end_of_statement (parser);
28231 return;
28232 }
28233
28234 /* See if we have a forward declaration or a definition. */
28235 tok = cp_lexer_peek_nth_token (parser->lexer, 2);
28236
28237 /* Try a forward declaration first. */
28238 if (tok->type == CPP_COMMA || tok->type == CPP_SEMICOLON)
28239 {
28240 while (true)
28241 {
28242 tree id;
28243
28244 id = cp_parser_identifier (parser);
28245 if (id == error_mark_node)
28246 break;
28247
28248 objc_declare_protocol (id, attributes);
28249
28250 if(cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
28251 cp_lexer_consume_token (parser->lexer);
28252 else
28253 break;
28254 }
28255 cp_parser_consume_semicolon_at_end_of_statement (parser);
28256 }
28257
28258 /* Ok, we got a full-fledged definition (or at least should). */
28259 else
28260 {
28261 proto = cp_parser_identifier (parser);
28262 protorefs = cp_parser_objc_protocol_refs_opt (parser);
28263 objc_start_protocol (proto, protorefs, attributes);
28264 cp_parser_objc_method_prototype_list (parser);
28265 }
28266 }
28267
28268 /* Parse an Objective-C superclass or category. */
28269
28270 static void
28271 cp_parser_objc_superclass_or_category (cp_parser *parser,
28272 bool iface_p,
28273 tree *super,
28274 tree *categ, bool *is_class_extension)
28275 {
28276 cp_token *next = cp_lexer_peek_token (parser->lexer);
28277
28278 *super = *categ = NULL_TREE;
28279 *is_class_extension = false;
28280 if (next->type == CPP_COLON)
28281 {
28282 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
28283 *super = cp_parser_identifier (parser);
28284 }
28285 else if (next->type == CPP_OPEN_PAREN)
28286 {
28287 cp_lexer_consume_token (parser->lexer); /* Eat '('. */
28288
28289 /* If there is no category name, and this is an @interface, we
28290 have a class extension. */
28291 if (iface_p && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
28292 {
28293 *categ = NULL_TREE;
28294 *is_class_extension = true;
28295 }
28296 else
28297 *categ = cp_parser_identifier (parser);
28298
28299 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
28300 }
28301 }
28302
28303 /* Parse an Objective-C class interface. */
28304
28305 static void
28306 cp_parser_objc_class_interface (cp_parser* parser, tree attributes)
28307 {
28308 tree name, super, categ, protos;
28309 bool is_class_extension;
28310
28311 cp_lexer_consume_token (parser->lexer); /* Eat '@interface'. */
28312 name = cp_parser_identifier (parser);
28313 if (name == error_mark_node)
28314 {
28315 /* It's hard to recover because even if valid @interface stuff
28316 is to follow, we can't compile it (or validate it) if we
28317 don't even know which class it refers to. Let's assume this
28318 was a stray '@interface' token in the stream and skip it.
28319 */
28320 return;
28321 }
28322 cp_parser_objc_superclass_or_category (parser, true, &super, &categ,
28323 &is_class_extension);
28324 protos = cp_parser_objc_protocol_refs_opt (parser);
28325
28326 /* We have either a class or a category on our hands. */
28327 if (categ || is_class_extension)
28328 objc_start_category_interface (name, categ, protos, attributes);
28329 else
28330 {
28331 objc_start_class_interface (name, super, protos, attributes);
28332 /* Handle instance variable declarations, if any. */
28333 cp_parser_objc_class_ivars (parser);
28334 objc_continue_interface ();
28335 }
28336
28337 cp_parser_objc_method_prototype_list (parser);
28338 }
28339
28340 /* Parse an Objective-C class implementation. */
28341
28342 static void
28343 cp_parser_objc_class_implementation (cp_parser* parser)
28344 {
28345 tree name, super, categ;
28346 bool is_class_extension;
28347
28348 cp_lexer_consume_token (parser->lexer); /* Eat '@implementation'. */
28349 name = cp_parser_identifier (parser);
28350 if (name == error_mark_node)
28351 {
28352 /* It's hard to recover because even if valid @implementation
28353 stuff is to follow, we can't compile it (or validate it) if
28354 we don't even know which class it refers to. Let's assume
28355 this was a stray '@implementation' token in the stream and
28356 skip it.
28357 */
28358 return;
28359 }
28360 cp_parser_objc_superclass_or_category (parser, false, &super, &categ,
28361 &is_class_extension);
28362
28363 /* We have either a class or a category on our hands. */
28364 if (categ)
28365 objc_start_category_implementation (name, categ);
28366 else
28367 {
28368 objc_start_class_implementation (name, super);
28369 /* Handle instance variable declarations, if any. */
28370 cp_parser_objc_class_ivars (parser);
28371 objc_continue_implementation ();
28372 }
28373
28374 cp_parser_objc_method_definition_list (parser);
28375 }
28376
28377 /* Consume the @end token and finish off the implementation. */
28378
28379 static void
28380 cp_parser_objc_end_implementation (cp_parser* parser)
28381 {
28382 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
28383 objc_finish_implementation ();
28384 }
28385
28386 /* Parse an Objective-C declaration. */
28387
28388 static void
28389 cp_parser_objc_declaration (cp_parser* parser, tree attributes)
28390 {
28391 /* Try to figure out what kind of declaration is present. */
28392 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
28393
28394 if (attributes)
28395 switch (kwd->keyword)
28396 {
28397 case RID_AT_ALIAS:
28398 case RID_AT_CLASS:
28399 case RID_AT_END:
28400 error_at (kwd->location, "attributes may not be specified before"
28401 " the %<@%D%> Objective-C++ keyword",
28402 kwd->u.value);
28403 attributes = NULL;
28404 break;
28405 case RID_AT_IMPLEMENTATION:
28406 warning_at (kwd->location, OPT_Wattributes,
28407 "prefix attributes are ignored before %<@%D%>",
28408 kwd->u.value);
28409 attributes = NULL;
28410 default:
28411 break;
28412 }
28413
28414 switch (kwd->keyword)
28415 {
28416 case RID_AT_ALIAS:
28417 cp_parser_objc_alias_declaration (parser);
28418 break;
28419 case RID_AT_CLASS:
28420 cp_parser_objc_class_declaration (parser);
28421 break;
28422 case RID_AT_PROTOCOL:
28423 cp_parser_objc_protocol_declaration (parser, attributes);
28424 break;
28425 case RID_AT_INTERFACE:
28426 cp_parser_objc_class_interface (parser, attributes);
28427 break;
28428 case RID_AT_IMPLEMENTATION:
28429 cp_parser_objc_class_implementation (parser);
28430 break;
28431 case RID_AT_END:
28432 cp_parser_objc_end_implementation (parser);
28433 break;
28434 default:
28435 error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
28436 kwd->u.value);
28437 cp_parser_skip_to_end_of_block_or_statement (parser);
28438 }
28439 }
28440
28441 /* Parse an Objective-C try-catch-finally statement.
28442
28443 objc-try-catch-finally-stmt:
28444 @try compound-statement objc-catch-clause-seq [opt]
28445 objc-finally-clause [opt]
28446
28447 objc-catch-clause-seq:
28448 objc-catch-clause objc-catch-clause-seq [opt]
28449
28450 objc-catch-clause:
28451 @catch ( objc-exception-declaration ) compound-statement
28452
28453 objc-finally-clause:
28454 @finally compound-statement
28455
28456 objc-exception-declaration:
28457 parameter-declaration
28458 '...'
28459
28460 where '...' is to be interpreted literally, that is, it means CPP_ELLIPSIS.
28461
28462 Returns NULL_TREE.
28463
28464 PS: This function is identical to c_parser_objc_try_catch_finally_statement
28465 for C. Keep them in sync. */
28466
28467 static tree
28468 cp_parser_objc_try_catch_finally_statement (cp_parser *parser)
28469 {
28470 location_t location;
28471 tree stmt;
28472
28473 cp_parser_require_keyword (parser, RID_AT_TRY, RT_AT_TRY);
28474 location = cp_lexer_peek_token (parser->lexer)->location;
28475 objc_maybe_warn_exceptions (location);
28476 /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
28477 node, lest it get absorbed into the surrounding block. */
28478 stmt = push_stmt_list ();
28479 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
28480 objc_begin_try_stmt (location, pop_stmt_list (stmt));
28481
28482 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_CATCH))
28483 {
28484 cp_parameter_declarator *parm;
28485 tree parameter_declaration = error_mark_node;
28486 bool seen_open_paren = false;
28487
28488 cp_lexer_consume_token (parser->lexer);
28489 if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28490 seen_open_paren = true;
28491 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
28492 {
28493 /* We have "@catch (...)" (where the '...' are literally
28494 what is in the code). Skip the '...'.
28495 parameter_declaration is set to NULL_TREE, and
28496 objc_being_catch_clauses() knows that that means
28497 '...'. */
28498 cp_lexer_consume_token (parser->lexer);
28499 parameter_declaration = NULL_TREE;
28500 }
28501 else
28502 {
28503 /* We have "@catch (NSException *exception)" or something
28504 like that. Parse the parameter declaration. */
28505 parm = cp_parser_parameter_declaration (parser, false, NULL);
28506 if (parm == NULL)
28507 parameter_declaration = error_mark_node;
28508 else
28509 parameter_declaration = grokdeclarator (parm->declarator,
28510 &parm->decl_specifiers,
28511 PARM, /*initialized=*/0,
28512 /*attrlist=*/NULL);
28513 }
28514 if (seen_open_paren)
28515 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
28516 else
28517 {
28518 /* If there was no open parenthesis, we are recovering from
28519 an error, and we are trying to figure out what mistake
28520 the user has made. */
28521
28522 /* If there is an immediate closing parenthesis, the user
28523 probably forgot the opening one (ie, they typed "@catch
28524 NSException *e)". Parse the closing parenthesis and keep
28525 going. */
28526 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
28527 cp_lexer_consume_token (parser->lexer);
28528
28529 /* If these is no immediate closing parenthesis, the user
28530 probably doesn't know that parenthesis are required at
28531 all (ie, they typed "@catch NSException *e"). So, just
28532 forget about the closing parenthesis and keep going. */
28533 }
28534 objc_begin_catch_clause (parameter_declaration);
28535 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
28536 objc_finish_catch_clause ();
28537 }
28538 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_FINALLY))
28539 {
28540 cp_lexer_consume_token (parser->lexer);
28541 location = cp_lexer_peek_token (parser->lexer)->location;
28542 /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
28543 node, lest it get absorbed into the surrounding block. */
28544 stmt = push_stmt_list ();
28545 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
28546 objc_build_finally_clause (location, pop_stmt_list (stmt));
28547 }
28548
28549 return objc_finish_try_stmt ();
28550 }
28551
28552 /* Parse an Objective-C synchronized statement.
28553
28554 objc-synchronized-stmt:
28555 @synchronized ( expression ) compound-statement
28556
28557 Returns NULL_TREE. */
28558
28559 static tree
28560 cp_parser_objc_synchronized_statement (cp_parser *parser)
28561 {
28562 location_t location;
28563 tree lock, stmt;
28564
28565 cp_parser_require_keyword (parser, RID_AT_SYNCHRONIZED, RT_AT_SYNCHRONIZED);
28566
28567 location = cp_lexer_peek_token (parser->lexer)->location;
28568 objc_maybe_warn_exceptions (location);
28569 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
28570 lock = cp_parser_expression (parser);
28571 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
28572
28573 /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
28574 node, lest it get absorbed into the surrounding block. */
28575 stmt = push_stmt_list ();
28576 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
28577
28578 return objc_build_synchronized (location, lock, pop_stmt_list (stmt));
28579 }
28580
28581 /* Parse an Objective-C throw statement.
28582
28583 objc-throw-stmt:
28584 @throw assignment-expression [opt] ;
28585
28586 Returns a constructed '@throw' statement. */
28587
28588 static tree
28589 cp_parser_objc_throw_statement (cp_parser *parser)
28590 {
28591 tree expr = NULL_TREE;
28592 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
28593
28594 cp_parser_require_keyword (parser, RID_AT_THROW, RT_AT_THROW);
28595
28596 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
28597 expr = cp_parser_expression (parser);
28598
28599 cp_parser_consume_semicolon_at_end_of_statement (parser);
28600
28601 return objc_build_throw_stmt (loc, expr);
28602 }
28603
28604 /* Parse an Objective-C statement. */
28605
28606 static tree
28607 cp_parser_objc_statement (cp_parser * parser)
28608 {
28609 /* Try to figure out what kind of declaration is present. */
28610 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
28611
28612 switch (kwd->keyword)
28613 {
28614 case RID_AT_TRY:
28615 return cp_parser_objc_try_catch_finally_statement (parser);
28616 case RID_AT_SYNCHRONIZED:
28617 return cp_parser_objc_synchronized_statement (parser);
28618 case RID_AT_THROW:
28619 return cp_parser_objc_throw_statement (parser);
28620 default:
28621 error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
28622 kwd->u.value);
28623 cp_parser_skip_to_end_of_block_or_statement (parser);
28624 }
28625
28626 return error_mark_node;
28627 }
28628
28629 /* If we are compiling ObjC++ and we see an __attribute__ we neeed to
28630 look ahead to see if an objc keyword follows the attributes. This
28631 is to detect the use of prefix attributes on ObjC @interface and
28632 @protocol. */
28633
28634 static bool
28635 cp_parser_objc_valid_prefix_attributes (cp_parser* parser, tree *attrib)
28636 {
28637 cp_lexer_save_tokens (parser->lexer);
28638 *attrib = cp_parser_attributes_opt (parser);
28639 gcc_assert (*attrib);
28640 if (OBJC_IS_AT_KEYWORD (cp_lexer_peek_token (parser->lexer)->keyword))
28641 {
28642 cp_lexer_commit_tokens (parser->lexer);
28643 return true;
28644 }
28645 cp_lexer_rollback_tokens (parser->lexer);
28646 return false;
28647 }
28648
28649 /* This routine is a minimal replacement for
28650 c_parser_struct_declaration () used when parsing the list of
28651 types/names or ObjC++ properties. For example, when parsing the
28652 code
28653
28654 @property (readonly) int a, b, c;
28655
28656 this function is responsible for parsing "int a, int b, int c" and
28657 returning the declarations as CHAIN of DECLs.
28658
28659 TODO: Share this code with cp_parser_objc_class_ivars. It's very
28660 similar parsing. */
28661 static tree
28662 cp_parser_objc_struct_declaration (cp_parser *parser)
28663 {
28664 tree decls = NULL_TREE;
28665 cp_decl_specifier_seq declspecs;
28666 int decl_class_or_enum_p;
28667 tree prefix_attributes;
28668
28669 cp_parser_decl_specifier_seq (parser,
28670 CP_PARSER_FLAGS_NONE,
28671 &declspecs,
28672 &decl_class_or_enum_p);
28673
28674 if (declspecs.type == error_mark_node)
28675 return error_mark_node;
28676
28677 /* auto, register, static, extern, mutable. */
28678 if (declspecs.storage_class != sc_none)
28679 {
28680 cp_parser_error (parser, "invalid type for property");
28681 declspecs.storage_class = sc_none;
28682 }
28683
28684 /* thread_local. */
28685 if (decl_spec_seq_has_spec_p (&declspecs, ds_thread))
28686 {
28687 cp_parser_error (parser, "invalid type for property");
28688 declspecs.locations[ds_thread] = 0;
28689 }
28690
28691 /* typedef. */
28692 if (decl_spec_seq_has_spec_p (&declspecs, ds_typedef))
28693 {
28694 cp_parser_error (parser, "invalid type for property");
28695 declspecs.locations[ds_typedef] = 0;
28696 }
28697
28698 prefix_attributes = declspecs.attributes;
28699 declspecs.attributes = NULL_TREE;
28700
28701 /* Keep going until we hit the `;' at the end of the declaration. */
28702 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
28703 {
28704 tree attributes, first_attribute, decl;
28705 cp_declarator *declarator;
28706 cp_token *token;
28707
28708 /* Parse the declarator. */
28709 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
28710 NULL, NULL, false, false);
28711
28712 /* Look for attributes that apply to the ivar. */
28713 attributes = cp_parser_attributes_opt (parser);
28714 /* Remember which attributes are prefix attributes and
28715 which are not. */
28716 first_attribute = attributes;
28717 /* Combine the attributes. */
28718 attributes = chainon (prefix_attributes, attributes);
28719
28720 decl = grokfield (declarator, &declspecs,
28721 NULL_TREE, /*init_const_expr_p=*/false,
28722 NULL_TREE, attributes);
28723
28724 if (decl == error_mark_node || decl == NULL_TREE)
28725 return error_mark_node;
28726
28727 /* Reset PREFIX_ATTRIBUTES. */
28728 while (attributes && TREE_CHAIN (attributes) != first_attribute)
28729 attributes = TREE_CHAIN (attributes);
28730 if (attributes)
28731 TREE_CHAIN (attributes) = NULL_TREE;
28732
28733 DECL_CHAIN (decl) = decls;
28734 decls = decl;
28735
28736 token = cp_lexer_peek_token (parser->lexer);
28737 if (token->type == CPP_COMMA)
28738 {
28739 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
28740 continue;
28741 }
28742 else
28743 break;
28744 }
28745 return decls;
28746 }
28747
28748 /* Parse an Objective-C @property declaration. The syntax is:
28749
28750 objc-property-declaration:
28751 '@property' objc-property-attributes[opt] struct-declaration ;
28752
28753 objc-property-attributes:
28754 '(' objc-property-attribute-list ')'
28755
28756 objc-property-attribute-list:
28757 objc-property-attribute
28758 objc-property-attribute-list, objc-property-attribute
28759
28760 objc-property-attribute
28761 'getter' = identifier
28762 'setter' = identifier
28763 'readonly'
28764 'readwrite'
28765 'assign'
28766 'retain'
28767 'copy'
28768 'nonatomic'
28769
28770 For example:
28771 @property NSString *name;
28772 @property (readonly) id object;
28773 @property (retain, nonatomic, getter=getTheName) id name;
28774 @property int a, b, c;
28775
28776 PS: This function is identical to
28777 c_parser_objc_at_property_declaration for C. Keep them in sync. */
28778 static void
28779 cp_parser_objc_at_property_declaration (cp_parser *parser)
28780 {
28781 /* The following variables hold the attributes of the properties as
28782 parsed. They are 'false' or 'NULL_TREE' if the attribute was not
28783 seen. When we see an attribute, we set them to 'true' (if they
28784 are boolean properties) or to the identifier (if they have an
28785 argument, ie, for getter and setter). Note that here we only
28786 parse the list of attributes, check the syntax and accumulate the
28787 attributes that we find. objc_add_property_declaration() will
28788 then process the information. */
28789 bool property_assign = false;
28790 bool property_copy = false;
28791 tree property_getter_ident = NULL_TREE;
28792 bool property_nonatomic = false;
28793 bool property_readonly = false;
28794 bool property_readwrite = false;
28795 bool property_retain = false;
28796 tree property_setter_ident = NULL_TREE;
28797
28798 /* 'properties' is the list of properties that we read. Usually a
28799 single one, but maybe more (eg, in "@property int a, b, c;" there
28800 are three). */
28801 tree properties;
28802 location_t loc;
28803
28804 loc = cp_lexer_peek_token (parser->lexer)->location;
28805
28806 cp_lexer_consume_token (parser->lexer); /* Eat '@property'. */
28807
28808 /* Parse the optional attribute list... */
28809 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
28810 {
28811 /* Eat the '('. */
28812 cp_lexer_consume_token (parser->lexer);
28813
28814 while (true)
28815 {
28816 bool syntax_error = false;
28817 cp_token *token = cp_lexer_peek_token (parser->lexer);
28818 enum rid keyword;
28819
28820 if (token->type != CPP_NAME)
28821 {
28822 cp_parser_error (parser, "expected identifier");
28823 break;
28824 }
28825 keyword = C_RID_CODE (token->u.value);
28826 cp_lexer_consume_token (parser->lexer);
28827 switch (keyword)
28828 {
28829 case RID_ASSIGN: property_assign = true; break;
28830 case RID_COPY: property_copy = true; break;
28831 case RID_NONATOMIC: property_nonatomic = true; break;
28832 case RID_READONLY: property_readonly = true; break;
28833 case RID_READWRITE: property_readwrite = true; break;
28834 case RID_RETAIN: property_retain = true; break;
28835
28836 case RID_GETTER:
28837 case RID_SETTER:
28838 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
28839 {
28840 if (keyword == RID_GETTER)
28841 cp_parser_error (parser,
28842 "missing %<=%> (after %<getter%> attribute)");
28843 else
28844 cp_parser_error (parser,
28845 "missing %<=%> (after %<setter%> attribute)");
28846 syntax_error = true;
28847 break;
28848 }
28849 cp_lexer_consume_token (parser->lexer); /* eat the = */
28850 if (!cp_parser_objc_selector_p (cp_lexer_peek_token (parser->lexer)->type))
28851 {
28852 cp_parser_error (parser, "expected identifier");
28853 syntax_error = true;
28854 break;
28855 }
28856 if (keyword == RID_SETTER)
28857 {
28858 if (property_setter_ident != NULL_TREE)
28859 {
28860 cp_parser_error (parser, "the %<setter%> attribute may only be specified once");
28861 cp_lexer_consume_token (parser->lexer);
28862 }
28863 else
28864 property_setter_ident = cp_parser_objc_selector (parser);
28865 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
28866 cp_parser_error (parser, "setter name must terminate with %<:%>");
28867 else
28868 cp_lexer_consume_token (parser->lexer);
28869 }
28870 else
28871 {
28872 if (property_getter_ident != NULL_TREE)
28873 {
28874 cp_parser_error (parser, "the %<getter%> attribute may only be specified once");
28875 cp_lexer_consume_token (parser->lexer);
28876 }
28877 else
28878 property_getter_ident = cp_parser_objc_selector (parser);
28879 }
28880 break;
28881 default:
28882 cp_parser_error (parser, "unknown property attribute");
28883 syntax_error = true;
28884 break;
28885 }
28886
28887 if (syntax_error)
28888 break;
28889
28890 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
28891 cp_lexer_consume_token (parser->lexer);
28892 else
28893 break;
28894 }
28895
28896 /* FIXME: "@property (setter, assign);" will generate a spurious
28897 "error: expected ‘)’ before ‘,’ token". This is because
28898 cp_parser_require, unlike the C counterpart, will produce an
28899 error even if we are in error recovery. */
28900 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28901 {
28902 cp_parser_skip_to_closing_parenthesis (parser,
28903 /*recovering=*/true,
28904 /*or_comma=*/false,
28905 /*consume_paren=*/true);
28906 }
28907 }
28908
28909 /* ... and the property declaration(s). */
28910 properties = cp_parser_objc_struct_declaration (parser);
28911
28912 if (properties == error_mark_node)
28913 {
28914 cp_parser_skip_to_end_of_statement (parser);
28915 /* If the next token is now a `;', consume it. */
28916 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
28917 cp_lexer_consume_token (parser->lexer);
28918 return;
28919 }
28920
28921 if (properties == NULL_TREE)
28922 cp_parser_error (parser, "expected identifier");
28923 else
28924 {
28925 /* Comma-separated properties are chained together in
28926 reverse order; add them one by one. */
28927 properties = nreverse (properties);
28928
28929 for (; properties; properties = TREE_CHAIN (properties))
28930 objc_add_property_declaration (loc, copy_node (properties),
28931 property_readonly, property_readwrite,
28932 property_assign, property_retain,
28933 property_copy, property_nonatomic,
28934 property_getter_ident, property_setter_ident);
28935 }
28936
28937 cp_parser_consume_semicolon_at_end_of_statement (parser);
28938 }
28939
28940 /* Parse an Objective-C++ @synthesize declaration. The syntax is:
28941
28942 objc-synthesize-declaration:
28943 @synthesize objc-synthesize-identifier-list ;
28944
28945 objc-synthesize-identifier-list:
28946 objc-synthesize-identifier
28947 objc-synthesize-identifier-list, objc-synthesize-identifier
28948
28949 objc-synthesize-identifier
28950 identifier
28951 identifier = identifier
28952
28953 For example:
28954 @synthesize MyProperty;
28955 @synthesize OneProperty, AnotherProperty=MyIvar, YetAnotherProperty;
28956
28957 PS: This function is identical to c_parser_objc_at_synthesize_declaration
28958 for C. Keep them in sync.
28959 */
28960 static void
28961 cp_parser_objc_at_synthesize_declaration (cp_parser *parser)
28962 {
28963 tree list = NULL_TREE;
28964 location_t loc;
28965 loc = cp_lexer_peek_token (parser->lexer)->location;
28966
28967 cp_lexer_consume_token (parser->lexer); /* Eat '@synthesize'. */
28968 while (true)
28969 {
28970 tree property, ivar;
28971 property = cp_parser_identifier (parser);
28972 if (property == error_mark_node)
28973 {
28974 cp_parser_consume_semicolon_at_end_of_statement (parser);
28975 return;
28976 }
28977 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
28978 {
28979 cp_lexer_consume_token (parser->lexer);
28980 ivar = cp_parser_identifier (parser);
28981 if (ivar == error_mark_node)
28982 {
28983 cp_parser_consume_semicolon_at_end_of_statement (parser);
28984 return;
28985 }
28986 }
28987 else
28988 ivar = NULL_TREE;
28989 list = chainon (list, build_tree_list (ivar, property));
28990 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
28991 cp_lexer_consume_token (parser->lexer);
28992 else
28993 break;
28994 }
28995 cp_parser_consume_semicolon_at_end_of_statement (parser);
28996 objc_add_synthesize_declaration (loc, list);
28997 }
28998
28999 /* Parse an Objective-C++ @dynamic declaration. The syntax is:
29000
29001 objc-dynamic-declaration:
29002 @dynamic identifier-list ;
29003
29004 For example:
29005 @dynamic MyProperty;
29006 @dynamic MyProperty, AnotherProperty;
29007
29008 PS: This function is identical to c_parser_objc_at_dynamic_declaration
29009 for C. Keep them in sync.
29010 */
29011 static void
29012 cp_parser_objc_at_dynamic_declaration (cp_parser *parser)
29013 {
29014 tree list = NULL_TREE;
29015 location_t loc;
29016 loc = cp_lexer_peek_token (parser->lexer)->location;
29017
29018 cp_lexer_consume_token (parser->lexer); /* Eat '@dynamic'. */
29019 while (true)
29020 {
29021 tree property;
29022 property = cp_parser_identifier (parser);
29023 if (property == error_mark_node)
29024 {
29025 cp_parser_consume_semicolon_at_end_of_statement (parser);
29026 return;
29027 }
29028 list = chainon (list, build_tree_list (NULL, property));
29029 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
29030 cp_lexer_consume_token (parser->lexer);
29031 else
29032 break;
29033 }
29034 cp_parser_consume_semicolon_at_end_of_statement (parser);
29035 objc_add_dynamic_declaration (loc, list);
29036 }
29037
29038 \f
29039 /* OpenMP 2.5 / 3.0 / 3.1 / 4.0 parsing routines. */
29040
29041 /* Returns name of the next clause.
29042 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
29043 the token is not consumed. Otherwise appropriate pragma_omp_clause is
29044 returned and the token is consumed. */
29045
29046 static pragma_omp_clause
29047 cp_parser_omp_clause_name (cp_parser *parser)
29048 {
29049 pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
29050
29051 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
29052 result = PRAGMA_OACC_CLAUSE_AUTO;
29053 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
29054 result = PRAGMA_OMP_CLAUSE_IF;
29055 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT))
29056 result = PRAGMA_OMP_CLAUSE_DEFAULT;
29057 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DELETE))
29058 result = PRAGMA_OACC_CLAUSE_DELETE;
29059 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_PRIVATE))
29060 result = PRAGMA_OMP_CLAUSE_PRIVATE;
29061 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
29062 result = PRAGMA_OMP_CLAUSE_FOR;
29063 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
29064 {
29065 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
29066 const char *p = IDENTIFIER_POINTER (id);
29067
29068 switch (p[0])
29069 {
29070 case 'a':
29071 if (!strcmp ("aligned", p))
29072 result = PRAGMA_OMP_CLAUSE_ALIGNED;
29073 else if (!strcmp ("async", p))
29074 result = PRAGMA_OACC_CLAUSE_ASYNC;
29075 break;
29076 case 'c':
29077 if (!strcmp ("collapse", p))
29078 result = PRAGMA_OMP_CLAUSE_COLLAPSE;
29079 else if (!strcmp ("copy", p))
29080 result = PRAGMA_OACC_CLAUSE_COPY;
29081 else if (!strcmp ("copyin", p))
29082 result = PRAGMA_OMP_CLAUSE_COPYIN;
29083 else if (!strcmp ("copyout", p))
29084 result = PRAGMA_OACC_CLAUSE_COPYOUT;
29085 else if (!strcmp ("copyprivate", p))
29086 result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
29087 else if (!strcmp ("create", p))
29088 result = PRAGMA_OACC_CLAUSE_CREATE;
29089 break;
29090 case 'd':
29091 if (!strcmp ("defaultmap", p))
29092 result = PRAGMA_OMP_CLAUSE_DEFAULTMAP;
29093 else if (!strcmp ("depend", p))
29094 result = PRAGMA_OMP_CLAUSE_DEPEND;
29095 else if (!strcmp ("device", p))
29096 result = PRAGMA_OMP_CLAUSE_DEVICE;
29097 else if (!strcmp ("deviceptr", p))
29098 result = PRAGMA_OACC_CLAUSE_DEVICEPTR;
29099 else if (!strcmp ("device_resident", p))
29100 result = PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT;
29101 else if (!strcmp ("dist_schedule", p))
29102 result = PRAGMA_OMP_CLAUSE_DIST_SCHEDULE;
29103 break;
29104 case 'f':
29105 if (!strcmp ("final", p))
29106 result = PRAGMA_OMP_CLAUSE_FINAL;
29107 else if (!strcmp ("firstprivate", p))
29108 result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
29109 else if (!strcmp ("from", p))
29110 result = PRAGMA_OMP_CLAUSE_FROM;
29111 break;
29112 case 'g':
29113 if (!strcmp ("gang", p))
29114 result = PRAGMA_OACC_CLAUSE_GANG;
29115 else if (!strcmp ("grainsize", p))
29116 result = PRAGMA_OMP_CLAUSE_GRAINSIZE;
29117 break;
29118 case 'h':
29119 if (!strcmp ("hint", p))
29120 result = PRAGMA_OMP_CLAUSE_HINT;
29121 else if (!strcmp ("host", p))
29122 result = PRAGMA_OACC_CLAUSE_HOST;
29123 break;
29124 case 'i':
29125 if (!strcmp ("inbranch", p))
29126 result = PRAGMA_OMP_CLAUSE_INBRANCH;
29127 else if (!strcmp ("independent", p))
29128 result = PRAGMA_OACC_CLAUSE_INDEPENDENT;
29129 else if (!strcmp ("is_device_ptr", p))
29130 result = PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR;
29131 break;
29132 case 'l':
29133 if (!strcmp ("lastprivate", p))
29134 result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
29135 else if (!strcmp ("linear", p))
29136 result = PRAGMA_OMP_CLAUSE_LINEAR;
29137 else if (!strcmp ("link", p))
29138 result = PRAGMA_OMP_CLAUSE_LINK;
29139 break;
29140 case 'm':
29141 if (!strcmp ("map", p))
29142 result = PRAGMA_OMP_CLAUSE_MAP;
29143 else if (!strcmp ("mergeable", p))
29144 result = PRAGMA_OMP_CLAUSE_MERGEABLE;
29145 else if (flag_cilkplus && !strcmp ("mask", p))
29146 result = PRAGMA_CILK_CLAUSE_MASK;
29147 break;
29148 case 'n':
29149 if (!strcmp ("nogroup", p))
29150 result = PRAGMA_OMP_CLAUSE_NOGROUP;
29151 else if (!strcmp ("notinbranch", p))
29152 result = PRAGMA_OMP_CLAUSE_NOTINBRANCH;
29153 else if (!strcmp ("nowait", p))
29154 result = PRAGMA_OMP_CLAUSE_NOWAIT;
29155 else if (flag_cilkplus && !strcmp ("nomask", p))
29156 result = PRAGMA_CILK_CLAUSE_NOMASK;
29157 else if (!strcmp ("num_gangs", p))
29158 result = PRAGMA_OACC_CLAUSE_NUM_GANGS;
29159 else if (!strcmp ("num_tasks", p))
29160 result = PRAGMA_OMP_CLAUSE_NUM_TASKS;
29161 else if (!strcmp ("num_teams", p))
29162 result = PRAGMA_OMP_CLAUSE_NUM_TEAMS;
29163 else if (!strcmp ("num_threads", p))
29164 result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
29165 else if (!strcmp ("num_workers", p))
29166 result = PRAGMA_OACC_CLAUSE_NUM_WORKERS;
29167 break;
29168 case 'o':
29169 if (!strcmp ("ordered", p))
29170 result = PRAGMA_OMP_CLAUSE_ORDERED;
29171 break;
29172 case 'p':
29173 if (!strcmp ("parallel", p))
29174 result = PRAGMA_OMP_CLAUSE_PARALLEL;
29175 else if (!strcmp ("present", p))
29176 result = PRAGMA_OACC_CLAUSE_PRESENT;
29177 else if (!strcmp ("present_or_copy", p)
29178 || !strcmp ("pcopy", p))
29179 result = PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY;
29180 else if (!strcmp ("present_or_copyin", p)
29181 || !strcmp ("pcopyin", p))
29182 result = PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN;
29183 else if (!strcmp ("present_or_copyout", p)
29184 || !strcmp ("pcopyout", p))
29185 result = PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT;
29186 else if (!strcmp ("present_or_create", p)
29187 || !strcmp ("pcreate", p))
29188 result = PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE;
29189 else if (!strcmp ("priority", p))
29190 result = PRAGMA_OMP_CLAUSE_PRIORITY;
29191 else if (!strcmp ("proc_bind", p))
29192 result = PRAGMA_OMP_CLAUSE_PROC_BIND;
29193 break;
29194 case 'r':
29195 if (!strcmp ("reduction", p))
29196 result = PRAGMA_OMP_CLAUSE_REDUCTION;
29197 break;
29198 case 's':
29199 if (!strcmp ("safelen", p))
29200 result = PRAGMA_OMP_CLAUSE_SAFELEN;
29201 else if (!strcmp ("schedule", p))
29202 result = PRAGMA_OMP_CLAUSE_SCHEDULE;
29203 else if (!strcmp ("sections", p))
29204 result = PRAGMA_OMP_CLAUSE_SECTIONS;
29205 else if (!strcmp ("self", p))
29206 result = PRAGMA_OACC_CLAUSE_SELF;
29207 else if (!strcmp ("seq", p))
29208 result = PRAGMA_OACC_CLAUSE_SEQ;
29209 else if (!strcmp ("shared", p))
29210 result = PRAGMA_OMP_CLAUSE_SHARED;
29211 else if (!strcmp ("simd", p))
29212 result = PRAGMA_OMP_CLAUSE_SIMD;
29213 else if (!strcmp ("simdlen", p))
29214 result = PRAGMA_OMP_CLAUSE_SIMDLEN;
29215 break;
29216 case 't':
29217 if (!strcmp ("taskgroup", p))
29218 result = PRAGMA_OMP_CLAUSE_TASKGROUP;
29219 else if (!strcmp ("thread_limit", p))
29220 result = PRAGMA_OMP_CLAUSE_THREAD_LIMIT;
29221 else if (!strcmp ("threads", p))
29222 result = PRAGMA_OMP_CLAUSE_THREADS;
29223 else if (!strcmp ("tile", p))
29224 result = PRAGMA_OACC_CLAUSE_TILE;
29225 else if (!strcmp ("to", p))
29226 result = PRAGMA_OMP_CLAUSE_TO;
29227 break;
29228 case 'u':
29229 if (!strcmp ("uniform", p))
29230 result = PRAGMA_OMP_CLAUSE_UNIFORM;
29231 else if (!strcmp ("untied", p))
29232 result = PRAGMA_OMP_CLAUSE_UNTIED;
29233 else if (!strcmp ("use_device", p))
29234 result = PRAGMA_OACC_CLAUSE_USE_DEVICE;
29235 else if (!strcmp ("use_device_ptr", p))
29236 result = PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR;
29237 break;
29238 case 'v':
29239 if (!strcmp ("vector", p))
29240 result = PRAGMA_OACC_CLAUSE_VECTOR;
29241 else if (!strcmp ("vector_length", p))
29242 result = PRAGMA_OACC_CLAUSE_VECTOR_LENGTH;
29243 else if (flag_cilkplus && !strcmp ("vectorlength", p))
29244 result = PRAGMA_CILK_CLAUSE_VECTORLENGTH;
29245 break;
29246 case 'w':
29247 if (!strcmp ("wait", p))
29248 result = PRAGMA_OACC_CLAUSE_WAIT;
29249 else if (!strcmp ("worker", p))
29250 result = PRAGMA_OACC_CLAUSE_WORKER;
29251 break;
29252 }
29253 }
29254
29255 if (result != PRAGMA_OMP_CLAUSE_NONE)
29256 cp_lexer_consume_token (parser->lexer);
29257
29258 return result;
29259 }
29260
29261 /* Validate that a clause of the given type does not already exist. */
29262
29263 static void
29264 check_no_duplicate_clause (tree clauses, enum omp_clause_code code,
29265 const char *name, location_t location)
29266 {
29267 tree c;
29268
29269 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
29270 if (OMP_CLAUSE_CODE (c) == code)
29271 {
29272 error_at (location, "too many %qs clauses", name);
29273 break;
29274 }
29275 }
29276
29277 /* OpenMP 2.5:
29278 variable-list:
29279 identifier
29280 variable-list , identifier
29281
29282 In addition, we match a closing parenthesis (or, if COLON is non-NULL,
29283 colon). An opening parenthesis will have been consumed by the caller.
29284
29285 If KIND is nonzero, create the appropriate node and install the decl
29286 in OMP_CLAUSE_DECL and add the node to the head of the list.
29287
29288 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
29289 return the list created.
29290
29291 COLON can be NULL if only closing parenthesis should end the list,
29292 or pointer to bool which will receive false if the list is terminated
29293 by closing parenthesis or true if the list is terminated by colon. */
29294
29295 static tree
29296 cp_parser_omp_var_list_no_open (cp_parser *parser, enum omp_clause_code kind,
29297 tree list, bool *colon)
29298 {
29299 cp_token *token;
29300 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
29301 if (colon)
29302 {
29303 parser->colon_corrects_to_scope_p = false;
29304 *colon = false;
29305 }
29306 while (1)
29307 {
29308 tree name, decl;
29309
29310 token = cp_lexer_peek_token (parser->lexer);
29311 if (kind != 0
29312 && current_class_ptr
29313 && cp_parser_is_keyword (token, RID_THIS))
29314 {
29315 decl = finish_this_expr ();
29316 if (TREE_CODE (decl) == NON_LVALUE_EXPR
29317 || CONVERT_EXPR_P (decl))
29318 decl = TREE_OPERAND (decl, 0);
29319 cp_lexer_consume_token (parser->lexer);
29320 }
29321 else
29322 {
29323 name = cp_parser_id_expression (parser, /*template_p=*/false,
29324 /*check_dependency_p=*/true,
29325 /*template_p=*/NULL,
29326 /*declarator_p=*/false,
29327 /*optional_p=*/false);
29328 if (name == error_mark_node)
29329 goto skip_comma;
29330
29331 decl = cp_parser_lookup_name_simple (parser, name, token->location);
29332 if (decl == error_mark_node)
29333 cp_parser_name_lookup_error (parser, name, decl, NLE_NULL,
29334 token->location);
29335 }
29336 if (decl == error_mark_node)
29337 ;
29338 else if (kind != 0)
29339 {
29340 switch (kind)
29341 {
29342 case OMP_CLAUSE__CACHE_:
29343 if (cp_lexer_peek_token (parser->lexer)->type != CPP_OPEN_SQUARE)
29344 {
29345 error_at (token->location, "expected %<[%>");
29346 decl = error_mark_node;
29347 break;
29348 }
29349 /* FALLTHROUGH. */
29350 case OMP_CLAUSE_MAP:
29351 case OMP_CLAUSE_FROM:
29352 case OMP_CLAUSE_TO:
29353 while (cp_lexer_next_token_is (parser->lexer, CPP_DOT))
29354 {
29355 location_t loc
29356 = cp_lexer_peek_token (parser->lexer)->location;
29357 cp_id_kind idk = CP_ID_KIND_NONE;
29358 cp_lexer_consume_token (parser->lexer);
29359 decl
29360 = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT,
29361 decl, false,
29362 &idk, loc);
29363 }
29364 /* FALLTHROUGH. */
29365 case OMP_CLAUSE_DEPEND:
29366 case OMP_CLAUSE_REDUCTION:
29367 while (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
29368 {
29369 tree low_bound = NULL_TREE, length = NULL_TREE;
29370
29371 parser->colon_corrects_to_scope_p = false;
29372 cp_lexer_consume_token (parser->lexer);
29373 if (!cp_lexer_next_token_is (parser->lexer, CPP_COLON))
29374 low_bound = cp_parser_expression (parser);
29375 if (!colon)
29376 parser->colon_corrects_to_scope_p
29377 = saved_colon_corrects_to_scope_p;
29378 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_SQUARE))
29379 length = integer_one_node;
29380 else
29381 {
29382 /* Look for `:'. */
29383 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
29384 goto skip_comma;
29385 if (!cp_lexer_next_token_is (parser->lexer,
29386 CPP_CLOSE_SQUARE))
29387 length = cp_parser_expression (parser);
29388 }
29389 /* Look for the closing `]'. */
29390 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE,
29391 RT_CLOSE_SQUARE))
29392 goto skip_comma;
29393
29394 if (kind == OMP_CLAUSE__CACHE_)
29395 {
29396 if (TREE_CODE (low_bound) != INTEGER_CST
29397 && !TREE_READONLY (low_bound))
29398 {
29399 error_at (token->location,
29400 "%qD is not a constant", low_bound);
29401 decl = error_mark_node;
29402 }
29403
29404 if (TREE_CODE (length) != INTEGER_CST
29405 && !TREE_READONLY (length))
29406 {
29407 error_at (token->location,
29408 "%qD is not a constant", length);
29409 decl = error_mark_node;
29410 }
29411 }
29412
29413 decl = tree_cons (low_bound, length, decl);
29414 }
29415 break;
29416 default:
29417 break;
29418 }
29419
29420 tree u = build_omp_clause (token->location, kind);
29421 OMP_CLAUSE_DECL (u) = decl;
29422 OMP_CLAUSE_CHAIN (u) = list;
29423 list = u;
29424 }
29425 else
29426 list = tree_cons (decl, NULL_TREE, list);
29427
29428 get_comma:
29429 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
29430 break;
29431 cp_lexer_consume_token (parser->lexer);
29432 }
29433
29434 if (colon)
29435 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
29436
29437 if (colon != NULL && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
29438 {
29439 *colon = true;
29440 cp_parser_require (parser, CPP_COLON, RT_COLON);
29441 return list;
29442 }
29443
29444 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
29445 {
29446 int ending;
29447
29448 /* Try to resync to an unnested comma. Copied from
29449 cp_parser_parenthesized_expression_list. */
29450 skip_comma:
29451 if (colon)
29452 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
29453 ending = cp_parser_skip_to_closing_parenthesis (parser,
29454 /*recovering=*/true,
29455 /*or_comma=*/true,
29456 /*consume_paren=*/true);
29457 if (ending < 0)
29458 goto get_comma;
29459 }
29460
29461 return list;
29462 }
29463
29464 /* Similarly, but expect leading and trailing parenthesis. This is a very
29465 common case for omp clauses. */
29466
29467 static tree
29468 cp_parser_omp_var_list (cp_parser *parser, enum omp_clause_code kind, tree list)
29469 {
29470 if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
29471 return cp_parser_omp_var_list_no_open (parser, kind, list, NULL);
29472 return list;
29473 }
29474
29475 /* OpenACC 2.0:
29476 copy ( variable-list )
29477 copyin ( variable-list )
29478 copyout ( variable-list )
29479 create ( variable-list )
29480 delete ( variable-list )
29481 present ( variable-list )
29482 present_or_copy ( variable-list )
29483 pcopy ( variable-list )
29484 present_or_copyin ( variable-list )
29485 pcopyin ( variable-list )
29486 present_or_copyout ( variable-list )
29487 pcopyout ( variable-list )
29488 present_or_create ( variable-list )
29489 pcreate ( variable-list ) */
29490
29491 static tree
29492 cp_parser_oacc_data_clause (cp_parser *parser, pragma_omp_clause c_kind,
29493 tree list)
29494 {
29495 enum gomp_map_kind kind;
29496 switch (c_kind)
29497 {
29498 case PRAGMA_OACC_CLAUSE_COPY:
29499 kind = GOMP_MAP_FORCE_TOFROM;
29500 break;
29501 case PRAGMA_OACC_CLAUSE_COPYIN:
29502 kind = GOMP_MAP_FORCE_TO;
29503 break;
29504 case PRAGMA_OACC_CLAUSE_COPYOUT:
29505 kind = GOMP_MAP_FORCE_FROM;
29506 break;
29507 case PRAGMA_OACC_CLAUSE_CREATE:
29508 kind = GOMP_MAP_FORCE_ALLOC;
29509 break;
29510 case PRAGMA_OACC_CLAUSE_DELETE:
29511 kind = GOMP_MAP_FORCE_DEALLOC;
29512 break;
29513 case PRAGMA_OACC_CLAUSE_DEVICE:
29514 kind = GOMP_MAP_FORCE_TO;
29515 break;
29516 case PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT:
29517 kind = GOMP_MAP_DEVICE_RESIDENT;
29518 break;
29519 case PRAGMA_OACC_CLAUSE_HOST:
29520 case PRAGMA_OACC_CLAUSE_SELF:
29521 kind = GOMP_MAP_FORCE_FROM;
29522 break;
29523 case PRAGMA_OACC_CLAUSE_LINK:
29524 kind = GOMP_MAP_LINK;
29525 break;
29526 case PRAGMA_OACC_CLAUSE_PRESENT:
29527 kind = GOMP_MAP_FORCE_PRESENT;
29528 break;
29529 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY:
29530 kind = GOMP_MAP_TOFROM;
29531 break;
29532 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN:
29533 kind = GOMP_MAP_TO;
29534 break;
29535 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT:
29536 kind = GOMP_MAP_FROM;
29537 break;
29538 case PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE:
29539 kind = GOMP_MAP_ALLOC;
29540 break;
29541 default:
29542 gcc_unreachable ();
29543 }
29544 tree nl, c;
29545 nl = cp_parser_omp_var_list (parser, OMP_CLAUSE_MAP, list);
29546
29547 for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
29548 OMP_CLAUSE_SET_MAP_KIND (c, kind);
29549
29550 return nl;
29551 }
29552
29553 /* OpenACC 2.0:
29554 deviceptr ( variable-list ) */
29555
29556 static tree
29557 cp_parser_oacc_data_clause_deviceptr (cp_parser *parser, tree list)
29558 {
29559 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
29560 tree vars, t;
29561
29562 /* Can't use OMP_CLAUSE_MAP here (that is, can't use the generic
29563 cp_parser_oacc_data_clause), as for PRAGMA_OACC_CLAUSE_DEVICEPTR,
29564 variable-list must only allow for pointer variables. */
29565 vars = cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
29566 for (t = vars; t; t = TREE_CHAIN (t))
29567 {
29568 tree v = TREE_PURPOSE (t);
29569
29570 /* FIXME diagnostics: Ideally we should keep individual
29571 locations for all the variables in the var list to make the
29572 following errors more precise. Perhaps
29573 c_parser_omp_var_list_parens should construct a list of
29574 locations to go along with the var list. */
29575
29576 if (!VAR_P (v))
29577 error_at (loc, "%qD is not a variable", v);
29578 else if (TREE_TYPE (v) == error_mark_node)
29579 ;
29580 else if (!POINTER_TYPE_P (TREE_TYPE (v)))
29581 error_at (loc, "%qD is not a pointer variable", v);
29582
29583 tree u = build_omp_clause (loc, OMP_CLAUSE_MAP);
29584 OMP_CLAUSE_SET_MAP_KIND (u, GOMP_MAP_FORCE_DEVICEPTR);
29585 OMP_CLAUSE_DECL (u) = v;
29586 OMP_CLAUSE_CHAIN (u) = list;
29587 list = u;
29588 }
29589
29590 return list;
29591 }
29592
29593 /* OpenACC 2.0:
29594 auto
29595 independent
29596 nohost
29597 seq */
29598
29599 static tree
29600 cp_parser_oacc_simple_clause (cp_parser * /* parser */,
29601 enum omp_clause_code code,
29602 tree list, location_t location)
29603 {
29604 check_no_duplicate_clause (list, code, omp_clause_code_name[code], location);
29605 tree c = build_omp_clause (location, code);
29606 OMP_CLAUSE_CHAIN (c) = list;
29607 return c;
29608 }
29609
29610 /* OpenACC:
29611 num_gangs ( expression )
29612 num_workers ( expression )
29613 vector_length ( expression ) */
29614
29615 static tree
29616 cp_parser_oacc_single_int_clause (cp_parser *parser, omp_clause_code code,
29617 const char *str, tree list)
29618 {
29619 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
29620
29621 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
29622 return list;
29623
29624 tree t = cp_parser_assignment_expression (parser, NULL, false, false);
29625
29626 if (t == error_mark_node
29627 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
29628 {
29629 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
29630 /*or_comma=*/false,
29631 /*consume_paren=*/true);
29632 return list;
29633 }
29634
29635 check_no_duplicate_clause (list, code, str, loc);
29636
29637 tree c = build_omp_clause (loc, code);
29638 OMP_CLAUSE_OPERAND (c, 0) = t;
29639 OMP_CLAUSE_CHAIN (c) = list;
29640 return c;
29641 }
29642
29643 /* OpenACC:
29644
29645 gang [( gang-arg-list )]
29646 worker [( [num:] int-expr )]
29647 vector [( [length:] int-expr )]
29648
29649 where gang-arg is one of:
29650
29651 [num:] int-expr
29652 static: size-expr
29653
29654 and size-expr may be:
29655
29656 *
29657 int-expr
29658 */
29659
29660 static tree
29661 cp_parser_oacc_shape_clause (cp_parser *parser, omp_clause_code kind,
29662 const char *str, tree list)
29663 {
29664 const char *id = "num";
29665 cp_lexer *lexer = parser->lexer;
29666 tree ops[2] = { NULL_TREE, NULL_TREE }, c;
29667 location_t loc = cp_lexer_peek_token (lexer)->location;
29668
29669 if (kind == OMP_CLAUSE_VECTOR)
29670 id = "length";
29671
29672 if (cp_lexer_next_token_is (lexer, CPP_OPEN_PAREN))
29673 {
29674 cp_lexer_consume_token (lexer);
29675
29676 do
29677 {
29678 cp_token *next = cp_lexer_peek_token (lexer);
29679 int idx = 0;
29680
29681 /* Gang static argument. */
29682 if (kind == OMP_CLAUSE_GANG
29683 && cp_lexer_next_token_is_keyword (lexer, RID_STATIC))
29684 {
29685 cp_lexer_consume_token (lexer);
29686
29687 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
29688 goto cleanup_error;
29689
29690 idx = 1;
29691 if (ops[idx] != NULL)
29692 {
29693 cp_parser_error (parser, "too many %<static%> arguments");
29694 goto cleanup_error;
29695 }
29696
29697 /* Check for the '*' argument. */
29698 if (cp_lexer_next_token_is (lexer, CPP_MULT)
29699 && (cp_lexer_nth_token_is (parser->lexer, 2, CPP_COMMA)
29700 || cp_lexer_nth_token_is (parser->lexer, 2,
29701 CPP_CLOSE_PAREN)))
29702 {
29703 cp_lexer_consume_token (lexer);
29704 ops[idx] = integer_minus_one_node;
29705
29706 if (cp_lexer_next_token_is (lexer, CPP_COMMA))
29707 {
29708 cp_lexer_consume_token (lexer);
29709 continue;
29710 }
29711 else break;
29712 }
29713 }
29714 /* Worker num: argument and vector length: arguments. */
29715 else if (cp_lexer_next_token_is (lexer, CPP_NAME)
29716 && strcmp (id, IDENTIFIER_POINTER (next->u.value)) == 0
29717 && cp_lexer_nth_token_is (lexer, 2, CPP_COLON))
29718 {
29719 cp_lexer_consume_token (lexer); /* id */
29720 cp_lexer_consume_token (lexer); /* ':' */
29721 }
29722
29723 /* Now collect the actual argument. */
29724 if (ops[idx] != NULL_TREE)
29725 {
29726 cp_parser_error (parser, "unexpected argument");
29727 goto cleanup_error;
29728 }
29729
29730 tree expr = cp_parser_assignment_expression (parser, NULL, false,
29731 false);
29732 if (expr == error_mark_node)
29733 goto cleanup_error;
29734
29735 mark_exp_read (expr);
29736 ops[idx] = expr;
29737
29738 if (kind == OMP_CLAUSE_GANG
29739 && cp_lexer_next_token_is (lexer, CPP_COMMA))
29740 {
29741 cp_lexer_consume_token (lexer);
29742 continue;
29743 }
29744 break;
29745 }
29746 while (1);
29747
29748 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
29749 goto cleanup_error;
29750 }
29751
29752 check_no_duplicate_clause (list, kind, str, loc);
29753
29754 c = build_omp_clause (loc, kind);
29755
29756 if (ops[1])
29757 OMP_CLAUSE_OPERAND (c, 1) = ops[1];
29758
29759 OMP_CLAUSE_OPERAND (c, 0) = ops[0];
29760 OMP_CLAUSE_CHAIN (c) = list;
29761
29762 return c;
29763
29764 cleanup_error:
29765 cp_parser_skip_to_closing_parenthesis (parser, false, false, true);
29766 return list;
29767 }
29768
29769 /* OpenACC 2.0:
29770 tile ( size-expr-list ) */
29771
29772 static tree
29773 cp_parser_oacc_clause_tile (cp_parser *parser, location_t clause_loc, tree list)
29774 {
29775 tree c, expr = error_mark_node;
29776 tree tile = NULL_TREE;
29777
29778 check_no_duplicate_clause (list, OMP_CLAUSE_TILE, "tile", clause_loc);
29779
29780 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
29781 return list;
29782
29783 do
29784 {
29785 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT)
29786 && (cp_lexer_nth_token_is (parser->lexer, 2, CPP_COMMA)
29787 || cp_lexer_nth_token_is (parser->lexer, 2, CPP_CLOSE_PAREN)))
29788 {
29789 cp_lexer_consume_token (parser->lexer);
29790 expr = integer_minus_one_node;
29791 }
29792 else
29793 expr = cp_parser_assignment_expression (parser, NULL, false, false);
29794
29795 if (expr == error_mark_node)
29796 return list;
29797
29798 tile = tree_cons (NULL_TREE, expr, tile);
29799
29800 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
29801 cp_lexer_consume_token (parser->lexer);
29802 }
29803 while (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN));
29804
29805 /* Consume the trailing ')'. */
29806 cp_lexer_consume_token (parser->lexer);
29807
29808 c = build_omp_clause (clause_loc, OMP_CLAUSE_TILE);
29809 tile = nreverse (tile);
29810 OMP_CLAUSE_TILE_LIST (c) = tile;
29811 OMP_CLAUSE_CHAIN (c) = list;
29812 return c;
29813 }
29814
29815 /* OpenACC 2.0
29816 Parse wait clause or directive parameters. */
29817
29818 static tree
29819 cp_parser_oacc_wait_list (cp_parser *parser, location_t clause_loc, tree list)
29820 {
29821 vec<tree, va_gc> *args;
29822 tree t, args_tree;
29823
29824 args = cp_parser_parenthesized_expression_list (parser, non_attr,
29825 /*cast_p=*/false,
29826 /*allow_expansion_p=*/true,
29827 /*non_constant_p=*/NULL);
29828
29829 if (args == NULL || args->length () == 0)
29830 {
29831 cp_parser_error (parser, "expected integer expression before ')'");
29832 if (args != NULL)
29833 release_tree_vector (args);
29834 return list;
29835 }
29836
29837 args_tree = build_tree_list_vec (args);
29838
29839 release_tree_vector (args);
29840
29841 for (t = args_tree; t; t = TREE_CHAIN (t))
29842 {
29843 tree targ = TREE_VALUE (t);
29844
29845 if (targ != error_mark_node)
29846 {
29847 if (!INTEGRAL_TYPE_P (TREE_TYPE (targ)))
29848 error ("%<wait%> expression must be integral");
29849 else
29850 {
29851 tree c = build_omp_clause (clause_loc, OMP_CLAUSE_WAIT);
29852
29853 mark_rvalue_use (targ);
29854 OMP_CLAUSE_DECL (c) = targ;
29855 OMP_CLAUSE_CHAIN (c) = list;
29856 list = c;
29857 }
29858 }
29859 }
29860
29861 return list;
29862 }
29863
29864 /* OpenACC:
29865 wait ( int-expr-list ) */
29866
29867 static tree
29868 cp_parser_oacc_clause_wait (cp_parser *parser, tree list)
29869 {
29870 location_t location = cp_lexer_peek_token (parser->lexer)->location;
29871
29872 if (cp_lexer_peek_token (parser->lexer)->type != CPP_OPEN_PAREN)
29873 return list;
29874
29875 list = cp_parser_oacc_wait_list (parser, location, list);
29876
29877 return list;
29878 }
29879
29880 /* OpenMP 3.0:
29881 collapse ( constant-expression ) */
29882
29883 static tree
29884 cp_parser_omp_clause_collapse (cp_parser *parser, tree list, location_t location)
29885 {
29886 tree c, num;
29887 location_t loc;
29888 HOST_WIDE_INT n;
29889
29890 loc = cp_lexer_peek_token (parser->lexer)->location;
29891 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
29892 return list;
29893
29894 num = cp_parser_constant_expression (parser);
29895
29896 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
29897 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
29898 /*or_comma=*/false,
29899 /*consume_paren=*/true);
29900
29901 if (num == error_mark_node)
29902 return list;
29903 num = fold_non_dependent_expr (num);
29904 if (!tree_fits_shwi_p (num)
29905 || !INTEGRAL_TYPE_P (TREE_TYPE (num))
29906 || (n = tree_to_shwi (num)) <= 0
29907 || (int) n != n)
29908 {
29909 error_at (loc, "collapse argument needs positive constant integer expression");
29910 return list;
29911 }
29912
29913 check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse", location);
29914 c = build_omp_clause (loc, OMP_CLAUSE_COLLAPSE);
29915 OMP_CLAUSE_CHAIN (c) = list;
29916 OMP_CLAUSE_COLLAPSE_EXPR (c) = num;
29917
29918 return c;
29919 }
29920
29921 /* OpenMP 2.5:
29922 default ( shared | none )
29923
29924 OpenACC 2.0
29925 default (none) */
29926
29927 static tree
29928 cp_parser_omp_clause_default (cp_parser *parser, tree list,
29929 location_t location, bool is_oacc)
29930 {
29931 enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
29932 tree c;
29933
29934 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
29935 return list;
29936 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
29937 {
29938 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
29939 const char *p = IDENTIFIER_POINTER (id);
29940
29941 switch (p[0])
29942 {
29943 case 'n':
29944 if (strcmp ("none", p) != 0)
29945 goto invalid_kind;
29946 kind = OMP_CLAUSE_DEFAULT_NONE;
29947 break;
29948
29949 case 's':
29950 if (strcmp ("shared", p) != 0 || is_oacc)
29951 goto invalid_kind;
29952 kind = OMP_CLAUSE_DEFAULT_SHARED;
29953 break;
29954
29955 default:
29956 goto invalid_kind;
29957 }
29958
29959 cp_lexer_consume_token (parser->lexer);
29960 }
29961 else
29962 {
29963 invalid_kind:
29964 if (is_oacc)
29965 cp_parser_error (parser, "expected %<none%>");
29966 else
29967 cp_parser_error (parser, "expected %<none%> or %<shared%>");
29968 }
29969
29970 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
29971 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
29972 /*or_comma=*/false,
29973 /*consume_paren=*/true);
29974
29975 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
29976 return list;
29977
29978 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default", location);
29979 c = build_omp_clause (location, OMP_CLAUSE_DEFAULT);
29980 OMP_CLAUSE_CHAIN (c) = list;
29981 OMP_CLAUSE_DEFAULT_KIND (c) = kind;
29982
29983 return c;
29984 }
29985
29986 /* OpenMP 3.1:
29987 final ( expression ) */
29988
29989 static tree
29990 cp_parser_omp_clause_final (cp_parser *parser, tree list, location_t location)
29991 {
29992 tree t, c;
29993
29994 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
29995 return list;
29996
29997 t = cp_parser_condition (parser);
29998
29999 if (t == error_mark_node
30000 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
30001 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
30002 /*or_comma=*/false,
30003 /*consume_paren=*/true);
30004
30005 check_no_duplicate_clause (list, OMP_CLAUSE_FINAL, "final", location);
30006
30007 c = build_omp_clause (location, OMP_CLAUSE_FINAL);
30008 OMP_CLAUSE_FINAL_EXPR (c) = t;
30009 OMP_CLAUSE_CHAIN (c) = list;
30010
30011 return c;
30012 }
30013
30014 /* OpenMP 2.5:
30015 if ( expression )
30016
30017 OpenMP 4.5:
30018 if ( directive-name-modifier : expression )
30019
30020 directive-name-modifier:
30021 parallel | task | taskloop | target data | target | target update
30022 | target enter data | target exit data */
30023
30024 static tree
30025 cp_parser_omp_clause_if (cp_parser *parser, tree list, location_t location,
30026 bool is_omp)
30027 {
30028 tree t, c;
30029 enum tree_code if_modifier = ERROR_MARK;
30030
30031 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
30032 return list;
30033
30034 if (is_omp && cp_lexer_next_token_is (parser->lexer, CPP_NAME))
30035 {
30036 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
30037 const char *p = IDENTIFIER_POINTER (id);
30038 int n = 2;
30039
30040 if (strcmp ("parallel", p) == 0)
30041 if_modifier = OMP_PARALLEL;
30042 else if (strcmp ("task", p) == 0)
30043 if_modifier = OMP_TASK;
30044 else if (strcmp ("taskloop", p) == 0)
30045 if_modifier = OMP_TASKLOOP;
30046 else if (strcmp ("target", p) == 0)
30047 {
30048 if_modifier = OMP_TARGET;
30049 if (cp_lexer_nth_token_is (parser->lexer, 2, CPP_NAME))
30050 {
30051 id = cp_lexer_peek_nth_token (parser->lexer, 2)->u.value;
30052 p = IDENTIFIER_POINTER (id);
30053 if (strcmp ("data", p) == 0)
30054 if_modifier = OMP_TARGET_DATA;
30055 else if (strcmp ("update", p) == 0)
30056 if_modifier = OMP_TARGET_UPDATE;
30057 else if (strcmp ("enter", p) == 0)
30058 if_modifier = OMP_TARGET_ENTER_DATA;
30059 else if (strcmp ("exit", p) == 0)
30060 if_modifier = OMP_TARGET_EXIT_DATA;
30061 if (if_modifier != OMP_TARGET)
30062 n = 3;
30063 else
30064 {
30065 location_t loc
30066 = cp_lexer_peek_nth_token (parser->lexer, 2)->location;
30067 error_at (loc, "expected %<data%>, %<update%>, %<enter%> "
30068 "or %<exit%>");
30069 if_modifier = ERROR_MARK;
30070 }
30071 if (if_modifier == OMP_TARGET_ENTER_DATA
30072 || if_modifier == OMP_TARGET_EXIT_DATA)
30073 {
30074 if (cp_lexer_nth_token_is (parser->lexer, 3, CPP_NAME))
30075 {
30076 id = cp_lexer_peek_nth_token (parser->lexer, 3)->u.value;
30077 p = IDENTIFIER_POINTER (id);
30078 if (strcmp ("data", p) == 0)
30079 n = 4;
30080 }
30081 if (n != 4)
30082 {
30083 location_t loc
30084 = cp_lexer_peek_nth_token (parser->lexer, 3)->location;
30085 error_at (loc, "expected %<data%>");
30086 if_modifier = ERROR_MARK;
30087 }
30088 }
30089 }
30090 }
30091 if (if_modifier != ERROR_MARK)
30092 {
30093 if (cp_lexer_nth_token_is (parser->lexer, n, CPP_COLON))
30094 {
30095 while (n-- > 0)
30096 cp_lexer_consume_token (parser->lexer);
30097 }
30098 else
30099 {
30100 if (n > 2)
30101 {
30102 location_t loc
30103 = cp_lexer_peek_nth_token (parser->lexer, n)->location;
30104 error_at (loc, "expected %<:%>");
30105 }
30106 if_modifier = ERROR_MARK;
30107 }
30108 }
30109 }
30110
30111 t = cp_parser_condition (parser);
30112
30113 if (t == error_mark_node
30114 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
30115 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
30116 /*or_comma=*/false,
30117 /*consume_paren=*/true);
30118
30119 for (c = list; c ; c = OMP_CLAUSE_CHAIN (c))
30120 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IF)
30121 {
30122 if (if_modifier != ERROR_MARK
30123 && OMP_CLAUSE_IF_MODIFIER (c) == if_modifier)
30124 {
30125 const char *p = NULL;
30126 switch (if_modifier)
30127 {
30128 case OMP_PARALLEL: p = "parallel"; break;
30129 case OMP_TASK: p = "task"; break;
30130 case OMP_TASKLOOP: p = "taskloop"; break;
30131 case OMP_TARGET_DATA: p = "target data"; break;
30132 case OMP_TARGET: p = "target"; break;
30133 case OMP_TARGET_UPDATE: p = "target update"; break;
30134 case OMP_TARGET_ENTER_DATA: p = "enter data"; break;
30135 case OMP_TARGET_EXIT_DATA: p = "exit data"; break;
30136 default: gcc_unreachable ();
30137 }
30138 error_at (location, "too many %<if%> clauses with %qs modifier",
30139 p);
30140 return list;
30141 }
30142 else if (OMP_CLAUSE_IF_MODIFIER (c) == if_modifier)
30143 {
30144 if (!is_omp)
30145 error_at (location, "too many %<if%> clauses");
30146 else
30147 error_at (location, "too many %<if%> clauses without modifier");
30148 return list;
30149 }
30150 else if (if_modifier == ERROR_MARK
30151 || OMP_CLAUSE_IF_MODIFIER (c) == ERROR_MARK)
30152 {
30153 error_at (location, "if any %<if%> clause has modifier, then all "
30154 "%<if%> clauses have to use modifier");
30155 return list;
30156 }
30157 }
30158
30159 c = build_omp_clause (location, OMP_CLAUSE_IF);
30160 OMP_CLAUSE_IF_MODIFIER (c) = if_modifier;
30161 OMP_CLAUSE_IF_EXPR (c) = t;
30162 OMP_CLAUSE_CHAIN (c) = list;
30163
30164 return c;
30165 }
30166
30167 /* OpenMP 3.1:
30168 mergeable */
30169
30170 static tree
30171 cp_parser_omp_clause_mergeable (cp_parser * /*parser*/,
30172 tree list, location_t location)
30173 {
30174 tree c;
30175
30176 check_no_duplicate_clause (list, OMP_CLAUSE_MERGEABLE, "mergeable",
30177 location);
30178
30179 c = build_omp_clause (location, OMP_CLAUSE_MERGEABLE);
30180 OMP_CLAUSE_CHAIN (c) = list;
30181 return c;
30182 }
30183
30184 /* OpenMP 2.5:
30185 nowait */
30186
30187 static tree
30188 cp_parser_omp_clause_nowait (cp_parser * /*parser*/,
30189 tree list, location_t location)
30190 {
30191 tree c;
30192
30193 check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait", location);
30194
30195 c = build_omp_clause (location, OMP_CLAUSE_NOWAIT);
30196 OMP_CLAUSE_CHAIN (c) = list;
30197 return c;
30198 }
30199
30200 /* OpenMP 2.5:
30201 num_threads ( expression ) */
30202
30203 static tree
30204 cp_parser_omp_clause_num_threads (cp_parser *parser, tree list,
30205 location_t location)
30206 {
30207 tree t, c;
30208
30209 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
30210 return list;
30211
30212 t = cp_parser_expression (parser);
30213
30214 if (t == error_mark_node
30215 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
30216 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
30217 /*or_comma=*/false,
30218 /*consume_paren=*/true);
30219
30220 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS,
30221 "num_threads", location);
30222
30223 c = build_omp_clause (location, OMP_CLAUSE_NUM_THREADS);
30224 OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
30225 OMP_CLAUSE_CHAIN (c) = list;
30226
30227 return c;
30228 }
30229
30230 /* OpenMP 4.5:
30231 num_tasks ( expression ) */
30232
30233 static tree
30234 cp_parser_omp_clause_num_tasks (cp_parser *parser, tree list,
30235 location_t location)
30236 {
30237 tree t, c;
30238
30239 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
30240 return list;
30241
30242 t = cp_parser_expression (parser);
30243
30244 if (t == error_mark_node
30245 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
30246 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
30247 /*or_comma=*/false,
30248 /*consume_paren=*/true);
30249
30250 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_TASKS,
30251 "num_tasks", location);
30252
30253 c = build_omp_clause (location, OMP_CLAUSE_NUM_TASKS);
30254 OMP_CLAUSE_NUM_TASKS_EXPR (c) = t;
30255 OMP_CLAUSE_CHAIN (c) = list;
30256
30257 return c;
30258 }
30259
30260 /* OpenMP 4.5:
30261 grainsize ( expression ) */
30262
30263 static tree
30264 cp_parser_omp_clause_grainsize (cp_parser *parser, tree list,
30265 location_t location)
30266 {
30267 tree t, c;
30268
30269 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
30270 return list;
30271
30272 t = cp_parser_expression (parser);
30273
30274 if (t == error_mark_node
30275 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
30276 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
30277 /*or_comma=*/false,
30278 /*consume_paren=*/true);
30279
30280 check_no_duplicate_clause (list, OMP_CLAUSE_GRAINSIZE,
30281 "grainsize", location);
30282
30283 c = build_omp_clause (location, OMP_CLAUSE_GRAINSIZE);
30284 OMP_CLAUSE_GRAINSIZE_EXPR (c) = t;
30285 OMP_CLAUSE_CHAIN (c) = list;
30286
30287 return c;
30288 }
30289
30290 /* OpenMP 4.5:
30291 priority ( expression ) */
30292
30293 static tree
30294 cp_parser_omp_clause_priority (cp_parser *parser, tree list,
30295 location_t location)
30296 {
30297 tree t, c;
30298
30299 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
30300 return list;
30301
30302 t = cp_parser_expression (parser);
30303
30304 if (t == error_mark_node
30305 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
30306 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
30307 /*or_comma=*/false,
30308 /*consume_paren=*/true);
30309
30310 check_no_duplicate_clause (list, OMP_CLAUSE_PRIORITY,
30311 "priority", location);
30312
30313 c = build_omp_clause (location, OMP_CLAUSE_PRIORITY);
30314 OMP_CLAUSE_PRIORITY_EXPR (c) = t;
30315 OMP_CLAUSE_CHAIN (c) = list;
30316
30317 return c;
30318 }
30319
30320 /* OpenMP 4.5:
30321 hint ( expression ) */
30322
30323 static tree
30324 cp_parser_omp_clause_hint (cp_parser *parser, tree list,
30325 location_t location)
30326 {
30327 tree t, c;
30328
30329 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
30330 return list;
30331
30332 t = cp_parser_expression (parser);
30333
30334 if (t == error_mark_node
30335 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
30336 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
30337 /*or_comma=*/false,
30338 /*consume_paren=*/true);
30339
30340 check_no_duplicate_clause (list, OMP_CLAUSE_HINT, "hint", location);
30341
30342 c = build_omp_clause (location, OMP_CLAUSE_HINT);
30343 OMP_CLAUSE_HINT_EXPR (c) = t;
30344 OMP_CLAUSE_CHAIN (c) = list;
30345
30346 return c;
30347 }
30348
30349 /* OpenMP 4.5:
30350 defaultmap ( tofrom : scalar ) */
30351
30352 static tree
30353 cp_parser_omp_clause_defaultmap (cp_parser *parser, tree list,
30354 location_t location)
30355 {
30356 tree c, id;
30357 const char *p;
30358
30359 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
30360 return list;
30361
30362 if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME))
30363 {
30364 cp_parser_error (parser, "expected %<tofrom%>");
30365 goto out_err;
30366 }
30367 id = cp_lexer_peek_token (parser->lexer)->u.value;
30368 p = IDENTIFIER_POINTER (id);
30369 if (strcmp (p, "tofrom") != 0)
30370 {
30371 cp_parser_error (parser, "expected %<tofrom%>");
30372 goto out_err;
30373 }
30374 cp_lexer_consume_token (parser->lexer);
30375 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
30376 goto out_err;
30377
30378 if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME))
30379 {
30380 cp_parser_error (parser, "expected %<scalar%>");
30381 goto out_err;
30382 }
30383 id = cp_lexer_peek_token (parser->lexer)->u.value;
30384 p = IDENTIFIER_POINTER (id);
30385 if (strcmp (p, "scalar") != 0)
30386 {
30387 cp_parser_error (parser, "expected %<scalar%>");
30388 goto out_err;
30389 }
30390 cp_lexer_consume_token (parser->lexer);
30391 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
30392 goto out_err;
30393
30394 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULTMAP, "defaultmap",
30395 location);
30396
30397 c = build_omp_clause (location, OMP_CLAUSE_DEFAULTMAP);
30398 OMP_CLAUSE_CHAIN (c) = list;
30399 return c;
30400
30401 out_err:
30402 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
30403 /*or_comma=*/false,
30404 /*consume_paren=*/true);
30405 return list;
30406 }
30407
30408 /* OpenMP 2.5:
30409 ordered
30410
30411 OpenMP 4.5:
30412 ordered ( constant-expression ) */
30413
30414 static tree
30415 cp_parser_omp_clause_ordered (cp_parser *parser,
30416 tree list, location_t location)
30417 {
30418 tree c, num = NULL_TREE;
30419 HOST_WIDE_INT n;
30420
30421 check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED,
30422 "ordered", location);
30423
30424 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
30425 {
30426 cp_lexer_consume_token (parser->lexer);
30427
30428 num = cp_parser_constant_expression (parser);
30429
30430 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
30431 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
30432 /*or_comma=*/false,
30433 /*consume_paren=*/true);
30434
30435 if (num == error_mark_node)
30436 return list;
30437 num = fold_non_dependent_expr (num);
30438 if (!tree_fits_shwi_p (num)
30439 || !INTEGRAL_TYPE_P (TREE_TYPE (num))
30440 || (n = tree_to_shwi (num)) <= 0
30441 || (int) n != n)
30442 {
30443 error_at (location,
30444 "ordered argument needs positive constant integer "
30445 "expression");
30446 return list;
30447 }
30448 }
30449
30450 c = build_omp_clause (location, OMP_CLAUSE_ORDERED);
30451 OMP_CLAUSE_ORDERED_EXPR (c) = num;
30452 OMP_CLAUSE_CHAIN (c) = list;
30453 return c;
30454 }
30455
30456 /* OpenMP 2.5:
30457 reduction ( reduction-operator : variable-list )
30458
30459 reduction-operator:
30460 One of: + * - & ^ | && ||
30461
30462 OpenMP 3.1:
30463
30464 reduction-operator:
30465 One of: + * - & ^ | && || min max
30466
30467 OpenMP 4.0:
30468
30469 reduction-operator:
30470 One of: + * - & ^ | && ||
30471 id-expression */
30472
30473 static tree
30474 cp_parser_omp_clause_reduction (cp_parser *parser, tree list)
30475 {
30476 enum tree_code code = ERROR_MARK;
30477 tree nlist, c, id = NULL_TREE;
30478
30479 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
30480 return list;
30481
30482 switch (cp_lexer_peek_token (parser->lexer)->type)
30483 {
30484 case CPP_PLUS: code = PLUS_EXPR; break;
30485 case CPP_MULT: code = MULT_EXPR; break;
30486 case CPP_MINUS: code = MINUS_EXPR; break;
30487 case CPP_AND: code = BIT_AND_EXPR; break;
30488 case CPP_XOR: code = BIT_XOR_EXPR; break;
30489 case CPP_OR: code = BIT_IOR_EXPR; break;
30490 case CPP_AND_AND: code = TRUTH_ANDIF_EXPR; break;
30491 case CPP_OR_OR: code = TRUTH_ORIF_EXPR; break;
30492 default: break;
30493 }
30494
30495 if (code != ERROR_MARK)
30496 cp_lexer_consume_token (parser->lexer);
30497 else
30498 {
30499 bool saved_colon_corrects_to_scope_p;
30500 saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
30501 parser->colon_corrects_to_scope_p = false;
30502 id = cp_parser_id_expression (parser, /*template_p=*/false,
30503 /*check_dependency_p=*/true,
30504 /*template_p=*/NULL,
30505 /*declarator_p=*/false,
30506 /*optional_p=*/false);
30507 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
30508 if (identifier_p (id))
30509 {
30510 const char *p = IDENTIFIER_POINTER (id);
30511
30512 if (strcmp (p, "min") == 0)
30513 code = MIN_EXPR;
30514 else if (strcmp (p, "max") == 0)
30515 code = MAX_EXPR;
30516 else if (id == ansi_opname (PLUS_EXPR))
30517 code = PLUS_EXPR;
30518 else if (id == ansi_opname (MULT_EXPR))
30519 code = MULT_EXPR;
30520 else if (id == ansi_opname (MINUS_EXPR))
30521 code = MINUS_EXPR;
30522 else if (id == ansi_opname (BIT_AND_EXPR))
30523 code = BIT_AND_EXPR;
30524 else if (id == ansi_opname (BIT_IOR_EXPR))
30525 code = BIT_IOR_EXPR;
30526 else if (id == ansi_opname (BIT_XOR_EXPR))
30527 code = BIT_XOR_EXPR;
30528 else if (id == ansi_opname (TRUTH_ANDIF_EXPR))
30529 code = TRUTH_ANDIF_EXPR;
30530 else if (id == ansi_opname (TRUTH_ORIF_EXPR))
30531 code = TRUTH_ORIF_EXPR;
30532 id = omp_reduction_id (code, id, NULL_TREE);
30533 tree scope = parser->scope;
30534 if (scope)
30535 id = build_qualified_name (NULL_TREE, scope, id, false);
30536 parser->scope = NULL_TREE;
30537 parser->qualifying_scope = NULL_TREE;
30538 parser->object_scope = NULL_TREE;
30539 }
30540 else
30541 {
30542 error ("invalid reduction-identifier");
30543 resync_fail:
30544 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
30545 /*or_comma=*/false,
30546 /*consume_paren=*/true);
30547 return list;
30548 }
30549 }
30550
30551 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
30552 goto resync_fail;
30553
30554 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_REDUCTION, list,
30555 NULL);
30556 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
30557 {
30558 OMP_CLAUSE_REDUCTION_CODE (c) = code;
30559 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = id;
30560 }
30561
30562 return nlist;
30563 }
30564
30565 /* OpenMP 2.5:
30566 schedule ( schedule-kind )
30567 schedule ( schedule-kind , expression )
30568
30569 schedule-kind:
30570 static | dynamic | guided | runtime | auto
30571
30572 OpenMP 4.5:
30573 schedule ( schedule-modifier : schedule-kind )
30574 schedule ( schedule-modifier [ , schedule-modifier ] : schedule-kind , expression )
30575
30576 schedule-modifier:
30577 simd
30578 monotonic
30579 nonmonotonic */
30580
30581 static tree
30582 cp_parser_omp_clause_schedule (cp_parser *parser, tree list, location_t location)
30583 {
30584 tree c, t;
30585 int modifiers = 0, nmodifiers = 0;
30586
30587 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
30588 return list;
30589
30590 c = build_omp_clause (location, OMP_CLAUSE_SCHEDULE);
30591
30592 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
30593 {
30594 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
30595 const char *p = IDENTIFIER_POINTER (id);
30596 if (strcmp ("simd", p) == 0)
30597 OMP_CLAUSE_SCHEDULE_SIMD (c) = 1;
30598 else if (strcmp ("monotonic", p) == 0)
30599 modifiers |= OMP_CLAUSE_SCHEDULE_MONOTONIC;
30600 else if (strcmp ("nonmonotonic", p) == 0)
30601 modifiers |= OMP_CLAUSE_SCHEDULE_NONMONOTONIC;
30602 else
30603 break;
30604 cp_lexer_consume_token (parser->lexer);
30605 if (nmodifiers++ == 0
30606 && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
30607 cp_lexer_consume_token (parser->lexer);
30608 else
30609 {
30610 cp_parser_require (parser, CPP_COLON, RT_COLON);
30611 break;
30612 }
30613 }
30614
30615 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
30616 {
30617 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
30618 const char *p = IDENTIFIER_POINTER (id);
30619
30620 switch (p[0])
30621 {
30622 case 'd':
30623 if (strcmp ("dynamic", p) != 0)
30624 goto invalid_kind;
30625 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
30626 break;
30627
30628 case 'g':
30629 if (strcmp ("guided", p) != 0)
30630 goto invalid_kind;
30631 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
30632 break;
30633
30634 case 'r':
30635 if (strcmp ("runtime", p) != 0)
30636 goto invalid_kind;
30637 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
30638 break;
30639
30640 default:
30641 goto invalid_kind;
30642 }
30643 }
30644 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
30645 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
30646 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
30647 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_AUTO;
30648 else
30649 goto invalid_kind;
30650 cp_lexer_consume_token (parser->lexer);
30651
30652 if ((modifiers & (OMP_CLAUSE_SCHEDULE_MONOTONIC
30653 | OMP_CLAUSE_SCHEDULE_NONMONOTONIC))
30654 == (OMP_CLAUSE_SCHEDULE_MONOTONIC
30655 | OMP_CLAUSE_SCHEDULE_NONMONOTONIC))
30656 {
30657 error_at (location, "both %<monotonic%> and %<nonmonotonic%> modifiers "
30658 "specified");
30659 modifiers = 0;
30660 }
30661
30662 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
30663 {
30664 cp_token *token;
30665 cp_lexer_consume_token (parser->lexer);
30666
30667 token = cp_lexer_peek_token (parser->lexer);
30668 t = cp_parser_assignment_expression (parser);
30669
30670 if (t == error_mark_node)
30671 goto resync_fail;
30672 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
30673 error_at (token->location, "schedule %<runtime%> does not take "
30674 "a %<chunk_size%> parameter");
30675 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_AUTO)
30676 error_at (token->location, "schedule %<auto%> does not take "
30677 "a %<chunk_size%> parameter");
30678 else
30679 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
30680
30681 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
30682 goto resync_fail;
30683 }
30684 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
30685 goto resync_fail;
30686
30687 OMP_CLAUSE_SCHEDULE_KIND (c)
30688 = (enum omp_clause_schedule_kind)
30689 (OMP_CLAUSE_SCHEDULE_KIND (c) | modifiers);
30690
30691 check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule", location);
30692 OMP_CLAUSE_CHAIN (c) = list;
30693 return c;
30694
30695 invalid_kind:
30696 cp_parser_error (parser, "invalid schedule kind");
30697 resync_fail:
30698 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
30699 /*or_comma=*/false,
30700 /*consume_paren=*/true);
30701 return list;
30702 }
30703
30704 /* OpenMP 3.0:
30705 untied */
30706
30707 static tree
30708 cp_parser_omp_clause_untied (cp_parser * /*parser*/,
30709 tree list, location_t location)
30710 {
30711 tree c;
30712
30713 check_no_duplicate_clause (list, OMP_CLAUSE_UNTIED, "untied", location);
30714
30715 c = build_omp_clause (location, OMP_CLAUSE_UNTIED);
30716 OMP_CLAUSE_CHAIN (c) = list;
30717 return c;
30718 }
30719
30720 /* OpenMP 4.0:
30721 inbranch
30722 notinbranch */
30723
30724 static tree
30725 cp_parser_omp_clause_branch (cp_parser * /*parser*/, enum omp_clause_code code,
30726 tree list, location_t location)
30727 {
30728 check_no_duplicate_clause (list, code, omp_clause_code_name[code], location);
30729 tree c = build_omp_clause (location, code);
30730 OMP_CLAUSE_CHAIN (c) = list;
30731 return c;
30732 }
30733
30734 /* OpenMP 4.0:
30735 parallel
30736 for
30737 sections
30738 taskgroup */
30739
30740 static tree
30741 cp_parser_omp_clause_cancelkind (cp_parser * /*parser*/,
30742 enum omp_clause_code code,
30743 tree list, location_t location)
30744 {
30745 tree c = build_omp_clause (location, code);
30746 OMP_CLAUSE_CHAIN (c) = list;
30747 return c;
30748 }
30749
30750 /* OpenMP 4.5:
30751 nogroup */
30752
30753 static tree
30754 cp_parser_omp_clause_nogroup (cp_parser * /*parser*/,
30755 tree list, location_t location)
30756 {
30757 check_no_duplicate_clause (list, OMP_CLAUSE_NOGROUP, "nogroup", location);
30758 tree c = build_omp_clause (location, OMP_CLAUSE_NOGROUP);
30759 OMP_CLAUSE_CHAIN (c) = list;
30760 return c;
30761 }
30762
30763 /* OpenMP 4.5:
30764 simd
30765 threads */
30766
30767 static tree
30768 cp_parser_omp_clause_orderedkind (cp_parser * /*parser*/,
30769 enum omp_clause_code code,
30770 tree list, location_t location)
30771 {
30772 check_no_duplicate_clause (list, code, omp_clause_code_name[code], location);
30773 tree c = build_omp_clause (location, code);
30774 OMP_CLAUSE_CHAIN (c) = list;
30775 return c;
30776 }
30777
30778 /* OpenMP 4.0:
30779 num_teams ( expression ) */
30780
30781 static tree
30782 cp_parser_omp_clause_num_teams (cp_parser *parser, tree list,
30783 location_t location)
30784 {
30785 tree t, c;
30786
30787 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
30788 return list;
30789
30790 t = cp_parser_expression (parser);
30791
30792 if (t == error_mark_node
30793 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
30794 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
30795 /*or_comma=*/false,
30796 /*consume_paren=*/true);
30797
30798 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_TEAMS,
30799 "num_teams", location);
30800
30801 c = build_omp_clause (location, OMP_CLAUSE_NUM_TEAMS);
30802 OMP_CLAUSE_NUM_TEAMS_EXPR (c) = t;
30803 OMP_CLAUSE_CHAIN (c) = list;
30804
30805 return c;
30806 }
30807
30808 /* OpenMP 4.0:
30809 thread_limit ( expression ) */
30810
30811 static tree
30812 cp_parser_omp_clause_thread_limit (cp_parser *parser, tree list,
30813 location_t location)
30814 {
30815 tree t, c;
30816
30817 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
30818 return list;
30819
30820 t = cp_parser_expression (parser);
30821
30822 if (t == error_mark_node
30823 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
30824 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
30825 /*or_comma=*/false,
30826 /*consume_paren=*/true);
30827
30828 check_no_duplicate_clause (list, OMP_CLAUSE_THREAD_LIMIT,
30829 "thread_limit", location);
30830
30831 c = build_omp_clause (location, OMP_CLAUSE_THREAD_LIMIT);
30832 OMP_CLAUSE_THREAD_LIMIT_EXPR (c) = t;
30833 OMP_CLAUSE_CHAIN (c) = list;
30834
30835 return c;
30836 }
30837
30838 /* OpenMP 4.0:
30839 aligned ( variable-list )
30840 aligned ( variable-list : constant-expression ) */
30841
30842 static tree
30843 cp_parser_omp_clause_aligned (cp_parser *parser, tree list)
30844 {
30845 tree nlist, c, alignment = NULL_TREE;
30846 bool colon;
30847
30848 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
30849 return list;
30850
30851 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_ALIGNED, list,
30852 &colon);
30853
30854 if (colon)
30855 {
30856 alignment = cp_parser_constant_expression (parser);
30857
30858 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
30859 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
30860 /*or_comma=*/false,
30861 /*consume_paren=*/true);
30862
30863 if (alignment == error_mark_node)
30864 alignment = NULL_TREE;
30865 }
30866
30867 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
30868 OMP_CLAUSE_ALIGNED_ALIGNMENT (c) = alignment;
30869
30870 return nlist;
30871 }
30872
30873 /* OpenMP 4.0:
30874 linear ( variable-list )
30875 linear ( variable-list : expression )
30876
30877 OpenMP 4.5:
30878 linear ( modifier ( variable-list ) )
30879 linear ( modifier ( variable-list ) : expression ) */
30880
30881 static tree
30882 cp_parser_omp_clause_linear (cp_parser *parser, tree list,
30883 bool is_cilk_simd_fn, bool declare_simd)
30884 {
30885 tree nlist, c, step = integer_one_node;
30886 bool colon;
30887 enum omp_clause_linear_kind kind = OMP_CLAUSE_LINEAR_DEFAULT;
30888
30889 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
30890 return list;
30891
30892 if (!is_cilk_simd_fn
30893 && cp_lexer_next_token_is (parser->lexer, CPP_NAME))
30894 {
30895 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
30896 const char *p = IDENTIFIER_POINTER (id);
30897
30898 if (strcmp ("ref", p) == 0)
30899 kind = OMP_CLAUSE_LINEAR_REF;
30900 else if (strcmp ("val", p) == 0)
30901 kind = OMP_CLAUSE_LINEAR_VAL;
30902 else if (strcmp ("uval", p) == 0)
30903 kind = OMP_CLAUSE_LINEAR_UVAL;
30904 if (cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_PAREN))
30905 cp_lexer_consume_token (parser->lexer);
30906 else
30907 kind = OMP_CLAUSE_LINEAR_DEFAULT;
30908 }
30909
30910 if (kind == OMP_CLAUSE_LINEAR_DEFAULT)
30911 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_LINEAR, list,
30912 &colon);
30913 else
30914 {
30915 nlist = cp_parser_omp_var_list (parser, OMP_CLAUSE_LINEAR, list);
30916 colon = cp_lexer_next_token_is (parser->lexer, CPP_COLON);
30917 if (colon)
30918 cp_parser_require (parser, CPP_COLON, RT_COLON);
30919 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
30920 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
30921 /*or_comma=*/false,
30922 /*consume_paren=*/true);
30923 }
30924
30925 if (colon)
30926 {
30927 step = NULL_TREE;
30928 if (declare_simd
30929 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
30930 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_CLOSE_PAREN))
30931 {
30932 cp_token *token = cp_lexer_peek_token (parser->lexer);
30933 cp_parser_parse_tentatively (parser);
30934 step = cp_parser_id_expression (parser, /*template_p=*/false,
30935 /*check_dependency_p=*/true,
30936 /*template_p=*/NULL,
30937 /*declarator_p=*/false,
30938 /*optional_p=*/false);
30939 if (step != error_mark_node)
30940 step = cp_parser_lookup_name_simple (parser, step, token->location);
30941 if (step == error_mark_node)
30942 {
30943 step = NULL_TREE;
30944 cp_parser_abort_tentative_parse (parser);
30945 }
30946 else if (!cp_parser_parse_definitely (parser))
30947 step = NULL_TREE;
30948 }
30949 if (!step)
30950 step = cp_parser_expression (parser);
30951
30952 if (is_cilk_simd_fn && TREE_CODE (step) == PARM_DECL)
30953 {
30954 sorry ("using parameters for %<linear%> step is not supported yet");
30955 step = integer_one_node;
30956 }
30957 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
30958 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
30959 /*or_comma=*/false,
30960 /*consume_paren=*/true);
30961
30962 if (step == error_mark_node)
30963 return list;
30964 }
30965
30966 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
30967 {
30968 OMP_CLAUSE_LINEAR_STEP (c) = step;
30969 OMP_CLAUSE_LINEAR_KIND (c) = kind;
30970 }
30971
30972 return nlist;
30973 }
30974
30975 /* OpenMP 4.0:
30976 safelen ( constant-expression ) */
30977
30978 static tree
30979 cp_parser_omp_clause_safelen (cp_parser *parser, tree list,
30980 location_t location)
30981 {
30982 tree t, c;
30983
30984 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
30985 return list;
30986
30987 t = cp_parser_constant_expression (parser);
30988
30989 if (t == error_mark_node
30990 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
30991 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
30992 /*or_comma=*/false,
30993 /*consume_paren=*/true);
30994
30995 check_no_duplicate_clause (list, OMP_CLAUSE_SAFELEN, "safelen", location);
30996
30997 c = build_omp_clause (location, OMP_CLAUSE_SAFELEN);
30998 OMP_CLAUSE_SAFELEN_EXPR (c) = t;
30999 OMP_CLAUSE_CHAIN (c) = list;
31000
31001 return c;
31002 }
31003
31004 /* OpenMP 4.0:
31005 simdlen ( constant-expression ) */
31006
31007 static tree
31008 cp_parser_omp_clause_simdlen (cp_parser *parser, tree list,
31009 location_t location)
31010 {
31011 tree t, c;
31012
31013 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31014 return list;
31015
31016 t = cp_parser_constant_expression (parser);
31017
31018 if (t == error_mark_node
31019 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31020 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31021 /*or_comma=*/false,
31022 /*consume_paren=*/true);
31023
31024 check_no_duplicate_clause (list, OMP_CLAUSE_SIMDLEN, "simdlen", location);
31025
31026 c = build_omp_clause (location, OMP_CLAUSE_SIMDLEN);
31027 OMP_CLAUSE_SIMDLEN_EXPR (c) = t;
31028 OMP_CLAUSE_CHAIN (c) = list;
31029
31030 return c;
31031 }
31032
31033 /* OpenMP 4.5:
31034 vec:
31035 identifier [+/- integer]
31036 vec , identifier [+/- integer]
31037 */
31038
31039 static tree
31040 cp_parser_omp_clause_depend_sink (cp_parser *parser, location_t clause_loc,
31041 tree list)
31042 {
31043 tree vec = NULL;
31044
31045 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
31046 {
31047 cp_parser_error (parser, "expected identifier");
31048 return list;
31049 }
31050
31051 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
31052 {
31053 location_t id_loc = cp_lexer_peek_token (parser->lexer)->location;
31054 tree t, identifier = cp_parser_identifier (parser);
31055 tree addend = NULL;
31056
31057 if (identifier == error_mark_node)
31058 t = error_mark_node;
31059 else
31060 {
31061 t = cp_parser_lookup_name_simple
31062 (parser, identifier,
31063 cp_lexer_peek_token (parser->lexer)->location);
31064 if (t == error_mark_node)
31065 cp_parser_name_lookup_error (parser, identifier, t, NLE_NULL,
31066 id_loc);
31067 }
31068
31069 bool neg = false;
31070 if (cp_lexer_next_token_is (parser->lexer, CPP_MINUS))
31071 neg = true;
31072 else if (!cp_lexer_next_token_is (parser->lexer, CPP_PLUS))
31073 {
31074 addend = integer_zero_node;
31075 goto add_to_vector;
31076 }
31077 cp_lexer_consume_token (parser->lexer);
31078
31079 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NUMBER))
31080 {
31081 cp_parser_error (parser, "expected integer");
31082 return list;
31083 }
31084
31085 addend = cp_lexer_peek_token (parser->lexer)->u.value;
31086 if (TREE_CODE (addend) != INTEGER_CST)
31087 {
31088 cp_parser_error (parser, "expected integer");
31089 return list;
31090 }
31091 cp_lexer_consume_token (parser->lexer);
31092
31093 add_to_vector:
31094 if (t != error_mark_node)
31095 {
31096 vec = tree_cons (addend, t, vec);
31097 if (neg)
31098 OMP_CLAUSE_DEPEND_SINK_NEGATIVE (vec) = 1;
31099 }
31100
31101 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
31102 break;
31103
31104 cp_lexer_consume_token (parser->lexer);
31105 }
31106
31107 if (cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN) && vec)
31108 {
31109 tree u = build_omp_clause (clause_loc, OMP_CLAUSE_DEPEND);
31110 OMP_CLAUSE_DEPEND_KIND (u) = OMP_CLAUSE_DEPEND_SINK;
31111 OMP_CLAUSE_DECL (u) = nreverse (vec);
31112 OMP_CLAUSE_CHAIN (u) = list;
31113 return u;
31114 }
31115 return list;
31116 }
31117
31118 /* OpenMP 4.0:
31119 depend ( depend-kind : variable-list )
31120
31121 depend-kind:
31122 in | out | inout
31123
31124 OpenMP 4.5:
31125 depend ( source )
31126
31127 depend ( sink : vec ) */
31128
31129 static tree
31130 cp_parser_omp_clause_depend (cp_parser *parser, tree list, location_t loc)
31131 {
31132 tree nlist, c;
31133 enum omp_clause_depend_kind kind = OMP_CLAUSE_DEPEND_INOUT;
31134
31135 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31136 return list;
31137
31138 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
31139 {
31140 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
31141 const char *p = IDENTIFIER_POINTER (id);
31142
31143 if (strcmp ("in", p) == 0)
31144 kind = OMP_CLAUSE_DEPEND_IN;
31145 else if (strcmp ("inout", p) == 0)
31146 kind = OMP_CLAUSE_DEPEND_INOUT;
31147 else if (strcmp ("out", p) == 0)
31148 kind = OMP_CLAUSE_DEPEND_OUT;
31149 else if (strcmp ("source", p) == 0)
31150 kind = OMP_CLAUSE_DEPEND_SOURCE;
31151 else if (strcmp ("sink", p) == 0)
31152 kind = OMP_CLAUSE_DEPEND_SINK;
31153 else
31154 goto invalid_kind;
31155 }
31156 else
31157 goto invalid_kind;
31158
31159 cp_lexer_consume_token (parser->lexer);
31160
31161 if (kind == OMP_CLAUSE_DEPEND_SOURCE)
31162 {
31163 c = build_omp_clause (loc, OMP_CLAUSE_DEPEND);
31164 OMP_CLAUSE_DEPEND_KIND (c) = kind;
31165 OMP_CLAUSE_DECL (c) = NULL_TREE;
31166 OMP_CLAUSE_CHAIN (c) = list;
31167 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31168 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31169 /*or_comma=*/false,
31170 /*consume_paren=*/true);
31171 return c;
31172 }
31173
31174 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
31175 goto resync_fail;
31176
31177 if (kind == OMP_CLAUSE_DEPEND_SINK)
31178 nlist = cp_parser_omp_clause_depend_sink (parser, loc, list);
31179 else
31180 {
31181 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_DEPEND,
31182 list, NULL);
31183
31184 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
31185 OMP_CLAUSE_DEPEND_KIND (c) = kind;
31186 }
31187 return nlist;
31188
31189 invalid_kind:
31190 cp_parser_error (parser, "invalid depend kind");
31191 resync_fail:
31192 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31193 /*or_comma=*/false,
31194 /*consume_paren=*/true);
31195 return list;
31196 }
31197
31198 /* OpenMP 4.0:
31199 map ( map-kind : variable-list )
31200 map ( variable-list )
31201
31202 map-kind:
31203 alloc | to | from | tofrom
31204
31205 OpenMP 4.5:
31206 map-kind:
31207 alloc | to | from | tofrom | release | delete
31208
31209 map ( always [,] map-kind: variable-list ) */
31210
31211 static tree
31212 cp_parser_omp_clause_map (cp_parser *parser, tree list)
31213 {
31214 tree nlist, c;
31215 enum gomp_map_kind kind = GOMP_MAP_TOFROM;
31216 bool always = false;
31217
31218 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31219 return list;
31220
31221 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
31222 {
31223 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
31224 const char *p = IDENTIFIER_POINTER (id);
31225
31226 if (strcmp ("always", p) == 0)
31227 {
31228 int nth = 2;
31229 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COMMA)
31230 nth++;
31231 if ((cp_lexer_peek_nth_token (parser->lexer, nth)->type == CPP_NAME
31232 || (cp_lexer_peek_nth_token (parser->lexer, nth)->keyword
31233 == RID_DELETE))
31234 && (cp_lexer_peek_nth_token (parser->lexer, nth + 1)->type
31235 == CPP_COLON))
31236 {
31237 always = true;
31238 cp_lexer_consume_token (parser->lexer);
31239 if (nth == 3)
31240 cp_lexer_consume_token (parser->lexer);
31241 }
31242 }
31243 }
31244
31245 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
31246 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
31247 {
31248 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
31249 const char *p = IDENTIFIER_POINTER (id);
31250
31251 if (strcmp ("alloc", p) == 0)
31252 kind = GOMP_MAP_ALLOC;
31253 else if (strcmp ("to", p) == 0)
31254 kind = always ? GOMP_MAP_ALWAYS_TO : GOMP_MAP_TO;
31255 else if (strcmp ("from", p) == 0)
31256 kind = always ? GOMP_MAP_ALWAYS_FROM : GOMP_MAP_FROM;
31257 else if (strcmp ("tofrom", p) == 0)
31258 kind = always ? GOMP_MAP_ALWAYS_TOFROM : GOMP_MAP_TOFROM;
31259 else if (strcmp ("release", p) == 0)
31260 kind = GOMP_MAP_RELEASE;
31261 else
31262 {
31263 cp_parser_error (parser, "invalid map kind");
31264 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31265 /*or_comma=*/false,
31266 /*consume_paren=*/true);
31267 return list;
31268 }
31269 cp_lexer_consume_token (parser->lexer);
31270 cp_lexer_consume_token (parser->lexer);
31271 }
31272 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DELETE)
31273 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
31274 {
31275 kind = GOMP_MAP_DELETE;
31276 cp_lexer_consume_token (parser->lexer);
31277 cp_lexer_consume_token (parser->lexer);
31278 }
31279
31280 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_MAP, list,
31281 NULL);
31282
31283 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
31284 OMP_CLAUSE_SET_MAP_KIND (c, kind);
31285
31286 return nlist;
31287 }
31288
31289 /* OpenMP 4.0:
31290 device ( expression ) */
31291
31292 static tree
31293 cp_parser_omp_clause_device (cp_parser *parser, tree list,
31294 location_t location)
31295 {
31296 tree t, c;
31297
31298 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31299 return list;
31300
31301 t = cp_parser_expression (parser);
31302
31303 if (t == error_mark_node
31304 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31305 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31306 /*or_comma=*/false,
31307 /*consume_paren=*/true);
31308
31309 check_no_duplicate_clause (list, OMP_CLAUSE_DEVICE,
31310 "device", location);
31311
31312 c = build_omp_clause (location, OMP_CLAUSE_DEVICE);
31313 OMP_CLAUSE_DEVICE_ID (c) = t;
31314 OMP_CLAUSE_CHAIN (c) = list;
31315
31316 return c;
31317 }
31318
31319 /* OpenMP 4.0:
31320 dist_schedule ( static )
31321 dist_schedule ( static , expression ) */
31322
31323 static tree
31324 cp_parser_omp_clause_dist_schedule (cp_parser *parser, tree list,
31325 location_t location)
31326 {
31327 tree c, t;
31328
31329 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31330 return list;
31331
31332 c = build_omp_clause (location, OMP_CLAUSE_DIST_SCHEDULE);
31333
31334 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
31335 goto invalid_kind;
31336 cp_lexer_consume_token (parser->lexer);
31337
31338 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
31339 {
31340 cp_lexer_consume_token (parser->lexer);
31341
31342 t = cp_parser_assignment_expression (parser);
31343
31344 if (t == error_mark_node)
31345 goto resync_fail;
31346 OMP_CLAUSE_DIST_SCHEDULE_CHUNK_EXPR (c) = t;
31347
31348 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31349 goto resync_fail;
31350 }
31351 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
31352 goto resync_fail;
31353
31354 check_no_duplicate_clause (list, OMP_CLAUSE_DIST_SCHEDULE, "dist_schedule",
31355 location);
31356 OMP_CLAUSE_CHAIN (c) = list;
31357 return c;
31358
31359 invalid_kind:
31360 cp_parser_error (parser, "invalid dist_schedule kind");
31361 resync_fail:
31362 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31363 /*or_comma=*/false,
31364 /*consume_paren=*/true);
31365 return list;
31366 }
31367
31368 /* OpenMP 4.0:
31369 proc_bind ( proc-bind-kind )
31370
31371 proc-bind-kind:
31372 master | close | spread */
31373
31374 static tree
31375 cp_parser_omp_clause_proc_bind (cp_parser *parser, tree list,
31376 location_t location)
31377 {
31378 tree c;
31379 enum omp_clause_proc_bind_kind kind;
31380
31381 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31382 return list;
31383
31384 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
31385 {
31386 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
31387 const char *p = IDENTIFIER_POINTER (id);
31388
31389 if (strcmp ("master", p) == 0)
31390 kind = OMP_CLAUSE_PROC_BIND_MASTER;
31391 else if (strcmp ("close", p) == 0)
31392 kind = OMP_CLAUSE_PROC_BIND_CLOSE;
31393 else if (strcmp ("spread", p) == 0)
31394 kind = OMP_CLAUSE_PROC_BIND_SPREAD;
31395 else
31396 goto invalid_kind;
31397 }
31398 else
31399 goto invalid_kind;
31400
31401 cp_lexer_consume_token (parser->lexer);
31402 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
31403 goto resync_fail;
31404
31405 c = build_omp_clause (location, OMP_CLAUSE_PROC_BIND);
31406 check_no_duplicate_clause (list, OMP_CLAUSE_PROC_BIND, "proc_bind",
31407 location);
31408 OMP_CLAUSE_PROC_BIND_KIND (c) = kind;
31409 OMP_CLAUSE_CHAIN (c) = list;
31410 return c;
31411
31412 invalid_kind:
31413 cp_parser_error (parser, "invalid depend kind");
31414 resync_fail:
31415 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31416 /*or_comma=*/false,
31417 /*consume_paren=*/true);
31418 return list;
31419 }
31420
31421 /* OpenACC:
31422 async [( int-expr )] */
31423
31424 static tree
31425 cp_parser_oacc_clause_async (cp_parser *parser, tree list)
31426 {
31427 tree c, t;
31428 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
31429
31430 t = build_int_cst (integer_type_node, GOMP_ASYNC_NOVAL);
31431
31432 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
31433 {
31434 cp_lexer_consume_token (parser->lexer);
31435
31436 t = cp_parser_expression (parser);
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
31444 check_no_duplicate_clause (list, OMP_CLAUSE_ASYNC, "async", loc);
31445
31446 c = build_omp_clause (loc, OMP_CLAUSE_ASYNC);
31447 OMP_CLAUSE_ASYNC_EXPR (c) = t;
31448 OMP_CLAUSE_CHAIN (c) = list;
31449 list = c;
31450
31451 return list;
31452 }
31453
31454 /* Parse all OpenACC clauses. The set clauses allowed by the directive
31455 is a bitmask in MASK. Return the list of clauses found. */
31456
31457 static tree
31458 cp_parser_oacc_all_clauses (cp_parser *parser, omp_clause_mask mask,
31459 const char *where, cp_token *pragma_tok,
31460 bool finish_p = true)
31461 {
31462 tree clauses = NULL;
31463 bool first = true;
31464
31465 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
31466 {
31467 location_t here;
31468 pragma_omp_clause c_kind;
31469 omp_clause_code code;
31470 const char *c_name;
31471 tree prev = clauses;
31472
31473 if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
31474 cp_lexer_consume_token (parser->lexer);
31475
31476 here = cp_lexer_peek_token (parser->lexer)->location;
31477 c_kind = cp_parser_omp_clause_name (parser);
31478
31479 switch (c_kind)
31480 {
31481 case PRAGMA_OACC_CLAUSE_ASYNC:
31482 clauses = cp_parser_oacc_clause_async (parser, clauses);
31483 c_name = "async";
31484 break;
31485 case PRAGMA_OACC_CLAUSE_AUTO:
31486 clauses = cp_parser_oacc_simple_clause (parser, OMP_CLAUSE_AUTO,
31487 clauses, here);
31488 c_name = "auto";
31489 break;
31490 case PRAGMA_OACC_CLAUSE_COLLAPSE:
31491 clauses = cp_parser_omp_clause_collapse (parser, clauses, here);
31492 c_name = "collapse";
31493 break;
31494 case PRAGMA_OACC_CLAUSE_COPY:
31495 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
31496 c_name = "copy";
31497 break;
31498 case PRAGMA_OACC_CLAUSE_COPYIN:
31499 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
31500 c_name = "copyin";
31501 break;
31502 case PRAGMA_OACC_CLAUSE_COPYOUT:
31503 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
31504 c_name = "copyout";
31505 break;
31506 case PRAGMA_OACC_CLAUSE_CREATE:
31507 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
31508 c_name = "create";
31509 break;
31510 case PRAGMA_OACC_CLAUSE_DELETE:
31511 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
31512 c_name = "delete";
31513 break;
31514 case PRAGMA_OMP_CLAUSE_DEFAULT:
31515 clauses = cp_parser_omp_clause_default (parser, clauses, here, true);
31516 c_name = "default";
31517 break;
31518 case PRAGMA_OACC_CLAUSE_DEVICE:
31519 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
31520 c_name = "device";
31521 break;
31522 case PRAGMA_OACC_CLAUSE_DEVICEPTR:
31523 clauses = cp_parser_oacc_data_clause_deviceptr (parser, clauses);
31524 c_name = "deviceptr";
31525 break;
31526 case PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT:
31527 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
31528 c_name = "device_resident";
31529 break;
31530 case PRAGMA_OACC_CLAUSE_FIRSTPRIVATE:
31531 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
31532 clauses);
31533 c_name = "firstprivate";
31534 break;
31535 case PRAGMA_OACC_CLAUSE_GANG:
31536 c_name = "gang";
31537 clauses = cp_parser_oacc_shape_clause (parser, OMP_CLAUSE_GANG,
31538 c_name, clauses);
31539 break;
31540 case PRAGMA_OACC_CLAUSE_HOST:
31541 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
31542 c_name = "host";
31543 break;
31544 case PRAGMA_OACC_CLAUSE_IF:
31545 clauses = cp_parser_omp_clause_if (parser, clauses, here, false);
31546 c_name = "if";
31547 break;
31548 case PRAGMA_OACC_CLAUSE_INDEPENDENT:
31549 clauses = cp_parser_oacc_simple_clause (parser,
31550 OMP_CLAUSE_INDEPENDENT,
31551 clauses, here);
31552 c_name = "independent";
31553 break;
31554 case PRAGMA_OACC_CLAUSE_LINK:
31555 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
31556 c_name = "link";
31557 break;
31558 case PRAGMA_OACC_CLAUSE_NUM_GANGS:
31559 code = OMP_CLAUSE_NUM_GANGS;
31560 c_name = "num_gangs";
31561 clauses = cp_parser_oacc_single_int_clause (parser, code, c_name,
31562 clauses);
31563 break;
31564 case PRAGMA_OACC_CLAUSE_NUM_WORKERS:
31565 c_name = "num_workers";
31566 code = OMP_CLAUSE_NUM_WORKERS;
31567 clauses = cp_parser_oacc_single_int_clause (parser, code, c_name,
31568 clauses);
31569 break;
31570 case PRAGMA_OACC_CLAUSE_PRESENT:
31571 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
31572 c_name = "present";
31573 break;
31574 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY:
31575 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
31576 c_name = "present_or_copy";
31577 break;
31578 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN:
31579 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
31580 c_name = "present_or_copyin";
31581 break;
31582 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT:
31583 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
31584 c_name = "present_or_copyout";
31585 break;
31586 case PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE:
31587 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
31588 c_name = "present_or_create";
31589 break;
31590 case PRAGMA_OACC_CLAUSE_PRIVATE:
31591 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
31592 clauses);
31593 c_name = "private";
31594 break;
31595 case PRAGMA_OACC_CLAUSE_REDUCTION:
31596 clauses = cp_parser_omp_clause_reduction (parser, clauses);
31597 c_name = "reduction";
31598 break;
31599 case PRAGMA_OACC_CLAUSE_SELF:
31600 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
31601 c_name = "self";
31602 break;
31603 case PRAGMA_OACC_CLAUSE_SEQ:
31604 clauses = cp_parser_oacc_simple_clause (parser, OMP_CLAUSE_SEQ,
31605 clauses, here);
31606 c_name = "seq";
31607 break;
31608 case PRAGMA_OACC_CLAUSE_TILE:
31609 clauses = cp_parser_oacc_clause_tile (parser, here, clauses);
31610 c_name = "tile";
31611 break;
31612 case PRAGMA_OACC_CLAUSE_USE_DEVICE:
31613 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_USE_DEVICE,
31614 clauses);
31615 c_name = "use_device";
31616 break;
31617 case PRAGMA_OACC_CLAUSE_VECTOR:
31618 c_name = "vector";
31619 clauses = cp_parser_oacc_shape_clause (parser, OMP_CLAUSE_VECTOR,
31620 c_name, clauses);
31621 break;
31622 case PRAGMA_OACC_CLAUSE_VECTOR_LENGTH:
31623 c_name = "vector_length";
31624 code = OMP_CLAUSE_VECTOR_LENGTH;
31625 clauses = cp_parser_oacc_single_int_clause (parser, code, c_name,
31626 clauses);
31627 break;
31628 case PRAGMA_OACC_CLAUSE_WAIT:
31629 clauses = cp_parser_oacc_clause_wait (parser, clauses);
31630 c_name = "wait";
31631 break;
31632 case PRAGMA_OACC_CLAUSE_WORKER:
31633 c_name = "worker";
31634 clauses = cp_parser_oacc_shape_clause (parser, OMP_CLAUSE_WORKER,
31635 c_name, clauses);
31636 break;
31637 default:
31638 cp_parser_error (parser, "expected %<#pragma acc%> clause");
31639 goto saw_error;
31640 }
31641
31642 first = false;
31643
31644 if (((mask >> c_kind) & 1) == 0)
31645 {
31646 /* Remove the invalid clause(s) from the list to avoid
31647 confusing the rest of the compiler. */
31648 clauses = prev;
31649 error_at (here, "%qs is not valid for %qs", c_name, where);
31650 }
31651 }
31652
31653 saw_error:
31654 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
31655
31656 if (finish_p)
31657 return finish_omp_clauses (clauses, false);
31658
31659 return clauses;
31660 }
31661
31662 /* Parse all OpenMP clauses. The set clauses allowed by the directive
31663 is a bitmask in MASK. Return the list of clauses found; the result
31664 of clause default goes in *pdefault. */
31665
31666 static tree
31667 cp_parser_omp_all_clauses (cp_parser *parser, omp_clause_mask mask,
31668 const char *where, cp_token *pragma_tok,
31669 bool finish_p = true)
31670 {
31671 tree clauses = NULL;
31672 bool first = true;
31673 cp_token *token = NULL;
31674
31675 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
31676 {
31677 pragma_omp_clause c_kind;
31678 const char *c_name;
31679 tree prev = clauses;
31680
31681 if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
31682 cp_lexer_consume_token (parser->lexer);
31683
31684 token = cp_lexer_peek_token (parser->lexer);
31685 c_kind = cp_parser_omp_clause_name (parser);
31686
31687 switch (c_kind)
31688 {
31689 case PRAGMA_OMP_CLAUSE_COLLAPSE:
31690 clauses = cp_parser_omp_clause_collapse (parser, clauses,
31691 token->location);
31692 c_name = "collapse";
31693 break;
31694 case PRAGMA_OMP_CLAUSE_COPYIN:
31695 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYIN, clauses);
31696 c_name = "copyin";
31697 break;
31698 case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
31699 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYPRIVATE,
31700 clauses);
31701 c_name = "copyprivate";
31702 break;
31703 case PRAGMA_OMP_CLAUSE_DEFAULT:
31704 clauses = cp_parser_omp_clause_default (parser, clauses,
31705 token->location, false);
31706 c_name = "default";
31707 break;
31708 case PRAGMA_OMP_CLAUSE_FINAL:
31709 clauses = cp_parser_omp_clause_final (parser, clauses, token->location);
31710 c_name = "final";
31711 break;
31712 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
31713 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
31714 clauses);
31715 c_name = "firstprivate";
31716 break;
31717 case PRAGMA_OMP_CLAUSE_GRAINSIZE:
31718 clauses = cp_parser_omp_clause_grainsize (parser, clauses,
31719 token->location);
31720 c_name = "grainsize";
31721 break;
31722 case PRAGMA_OMP_CLAUSE_HINT:
31723 clauses = cp_parser_omp_clause_hint (parser, clauses,
31724 token->location);
31725 c_name = "hint";
31726 break;
31727 case PRAGMA_OMP_CLAUSE_DEFAULTMAP:
31728 clauses = cp_parser_omp_clause_defaultmap (parser, clauses,
31729 token->location);
31730 c_name = "defaultmap";
31731 break;
31732 case PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR:
31733 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_USE_DEVICE_PTR,
31734 clauses);
31735 c_name = "use_device_ptr";
31736 break;
31737 case PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR:
31738 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_IS_DEVICE_PTR,
31739 clauses);
31740 c_name = "is_device_ptr";
31741 break;
31742 case PRAGMA_OMP_CLAUSE_IF:
31743 clauses = cp_parser_omp_clause_if (parser, clauses, token->location,
31744 true);
31745 c_name = "if";
31746 break;
31747 case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
31748 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LASTPRIVATE,
31749 clauses);
31750 c_name = "lastprivate";
31751 break;
31752 case PRAGMA_OMP_CLAUSE_MERGEABLE:
31753 clauses = cp_parser_omp_clause_mergeable (parser, clauses,
31754 token->location);
31755 c_name = "mergeable";
31756 break;
31757 case PRAGMA_OMP_CLAUSE_NOWAIT:
31758 clauses = cp_parser_omp_clause_nowait (parser, clauses, token->location);
31759 c_name = "nowait";
31760 break;
31761 case PRAGMA_OMP_CLAUSE_NUM_TASKS:
31762 clauses = cp_parser_omp_clause_num_tasks (parser, clauses,
31763 token->location);
31764 c_name = "num_tasks";
31765 break;
31766 case PRAGMA_OMP_CLAUSE_NUM_THREADS:
31767 clauses = cp_parser_omp_clause_num_threads (parser, clauses,
31768 token->location);
31769 c_name = "num_threads";
31770 break;
31771 case PRAGMA_OMP_CLAUSE_ORDERED:
31772 clauses = cp_parser_omp_clause_ordered (parser, clauses,
31773 token->location);
31774 c_name = "ordered";
31775 break;
31776 case PRAGMA_OMP_CLAUSE_PRIORITY:
31777 clauses = cp_parser_omp_clause_priority (parser, clauses,
31778 token->location);
31779 c_name = "priority";
31780 break;
31781 case PRAGMA_OMP_CLAUSE_PRIVATE:
31782 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
31783 clauses);
31784 c_name = "private";
31785 break;
31786 case PRAGMA_OMP_CLAUSE_REDUCTION:
31787 clauses = cp_parser_omp_clause_reduction (parser, clauses);
31788 c_name = "reduction";
31789 break;
31790 case PRAGMA_OMP_CLAUSE_SCHEDULE:
31791 clauses = cp_parser_omp_clause_schedule (parser, clauses,
31792 token->location);
31793 c_name = "schedule";
31794 break;
31795 case PRAGMA_OMP_CLAUSE_SHARED:
31796 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_SHARED,
31797 clauses);
31798 c_name = "shared";
31799 break;
31800 case PRAGMA_OMP_CLAUSE_UNTIED:
31801 clauses = cp_parser_omp_clause_untied (parser, clauses,
31802 token->location);
31803 c_name = "untied";
31804 break;
31805 case PRAGMA_OMP_CLAUSE_INBRANCH:
31806 case PRAGMA_CILK_CLAUSE_MASK:
31807 clauses = cp_parser_omp_clause_branch (parser, OMP_CLAUSE_INBRANCH,
31808 clauses, token->location);
31809 c_name = "inbranch";
31810 break;
31811 case PRAGMA_OMP_CLAUSE_NOTINBRANCH:
31812 case PRAGMA_CILK_CLAUSE_NOMASK:
31813 clauses = cp_parser_omp_clause_branch (parser,
31814 OMP_CLAUSE_NOTINBRANCH,
31815 clauses, token->location);
31816 c_name = "notinbranch";
31817 break;
31818 case PRAGMA_OMP_CLAUSE_PARALLEL:
31819 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_PARALLEL,
31820 clauses, token->location);
31821 c_name = "parallel";
31822 if (!first)
31823 {
31824 clause_not_first:
31825 error_at (token->location, "%qs must be the first clause of %qs",
31826 c_name, where);
31827 clauses = prev;
31828 }
31829 break;
31830 case PRAGMA_OMP_CLAUSE_FOR:
31831 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_FOR,
31832 clauses, token->location);
31833 c_name = "for";
31834 if (!first)
31835 goto clause_not_first;
31836 break;
31837 case PRAGMA_OMP_CLAUSE_SECTIONS:
31838 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_SECTIONS,
31839 clauses, token->location);
31840 c_name = "sections";
31841 if (!first)
31842 goto clause_not_first;
31843 break;
31844 case PRAGMA_OMP_CLAUSE_TASKGROUP:
31845 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_TASKGROUP,
31846 clauses, token->location);
31847 c_name = "taskgroup";
31848 if (!first)
31849 goto clause_not_first;
31850 break;
31851 case PRAGMA_OMP_CLAUSE_LINK:
31852 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LINK, clauses);
31853 c_name = "to";
31854 break;
31855 case PRAGMA_OMP_CLAUSE_TO:
31856 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINK)) != 0)
31857 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_TO_DECLARE,
31858 clauses);
31859 else
31860 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_TO, clauses);
31861 c_name = "to";
31862 break;
31863 case PRAGMA_OMP_CLAUSE_FROM:
31864 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FROM, clauses);
31865 c_name = "from";
31866 break;
31867 case PRAGMA_OMP_CLAUSE_UNIFORM:
31868 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_UNIFORM,
31869 clauses);
31870 c_name = "uniform";
31871 break;
31872 case PRAGMA_OMP_CLAUSE_NUM_TEAMS:
31873 clauses = cp_parser_omp_clause_num_teams (parser, clauses,
31874 token->location);
31875 c_name = "num_teams";
31876 break;
31877 case PRAGMA_OMP_CLAUSE_THREAD_LIMIT:
31878 clauses = cp_parser_omp_clause_thread_limit (parser, clauses,
31879 token->location);
31880 c_name = "thread_limit";
31881 break;
31882 case PRAGMA_OMP_CLAUSE_ALIGNED:
31883 clauses = cp_parser_omp_clause_aligned (parser, clauses);
31884 c_name = "aligned";
31885 break;
31886 case PRAGMA_OMP_CLAUSE_LINEAR:
31887 {
31888 bool cilk_simd_fn = false, declare_simd = false;
31889 if (((mask >> PRAGMA_CILK_CLAUSE_VECTORLENGTH) & 1) != 0)
31890 cilk_simd_fn = true;
31891 else if (((mask >> PRAGMA_OMP_CLAUSE_UNIFORM) & 1) != 0)
31892 declare_simd = true;
31893 clauses = cp_parser_omp_clause_linear (parser, clauses,
31894 cilk_simd_fn, declare_simd);
31895 }
31896 c_name = "linear";
31897 break;
31898 case PRAGMA_OMP_CLAUSE_DEPEND:
31899 clauses = cp_parser_omp_clause_depend (parser, clauses,
31900 token->location);
31901 c_name = "depend";
31902 break;
31903 case PRAGMA_OMP_CLAUSE_MAP:
31904 clauses = cp_parser_omp_clause_map (parser, clauses);
31905 c_name = "map";
31906 break;
31907 case PRAGMA_OMP_CLAUSE_DEVICE:
31908 clauses = cp_parser_omp_clause_device (parser, clauses,
31909 token->location);
31910 c_name = "device";
31911 break;
31912 case PRAGMA_OMP_CLAUSE_DIST_SCHEDULE:
31913 clauses = cp_parser_omp_clause_dist_schedule (parser, clauses,
31914 token->location);
31915 c_name = "dist_schedule";
31916 break;
31917 case PRAGMA_OMP_CLAUSE_PROC_BIND:
31918 clauses = cp_parser_omp_clause_proc_bind (parser, clauses,
31919 token->location);
31920 c_name = "proc_bind";
31921 break;
31922 case PRAGMA_OMP_CLAUSE_SAFELEN:
31923 clauses = cp_parser_omp_clause_safelen (parser, clauses,
31924 token->location);
31925 c_name = "safelen";
31926 break;
31927 case PRAGMA_OMP_CLAUSE_SIMDLEN:
31928 clauses = cp_parser_omp_clause_simdlen (parser, clauses,
31929 token->location);
31930 c_name = "simdlen";
31931 break;
31932 case PRAGMA_OMP_CLAUSE_NOGROUP:
31933 clauses = cp_parser_omp_clause_nogroup (parser, clauses,
31934 token->location);
31935 c_name = "nogroup";
31936 break;
31937 case PRAGMA_OMP_CLAUSE_THREADS:
31938 clauses
31939 = cp_parser_omp_clause_orderedkind (parser, OMP_CLAUSE_THREADS,
31940 clauses, token->location);
31941 c_name = "threads";
31942 break;
31943 case PRAGMA_OMP_CLAUSE_SIMD:
31944 clauses
31945 = cp_parser_omp_clause_orderedkind (parser, OMP_CLAUSE_SIMD,
31946 clauses, token->location);
31947 c_name = "simd";
31948 break;
31949 case PRAGMA_CILK_CLAUSE_VECTORLENGTH:
31950 clauses = cp_parser_cilk_simd_vectorlength (parser, clauses, true);
31951 c_name = "simdlen";
31952 break;
31953 default:
31954 cp_parser_error (parser, "expected %<#pragma omp%> clause");
31955 goto saw_error;
31956 }
31957
31958 first = false;
31959
31960 if (((mask >> c_kind) & 1) == 0)
31961 {
31962 /* Remove the invalid clause(s) from the list to avoid
31963 confusing the rest of the compiler. */
31964 clauses = prev;
31965 error_at (token->location, "%qs is not valid for %qs", c_name, where);
31966 }
31967 }
31968 saw_error:
31969 /* In Cilk Plus SIMD enabled functions there is no pragma_token, so
31970 no reason to skip to the end. */
31971 if (!(flag_cilkplus && pragma_tok == NULL))
31972 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
31973 if (finish_p)
31974 {
31975 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNIFORM)) != 0)
31976 return finish_omp_clauses (clauses, false, true);
31977 else
31978 return finish_omp_clauses (clauses, true);
31979 }
31980 return clauses;
31981 }
31982
31983 /* OpenMP 2.5:
31984 structured-block:
31985 statement
31986
31987 In practice, we're also interested in adding the statement to an
31988 outer node. So it is convenient if we work around the fact that
31989 cp_parser_statement calls add_stmt. */
31990
31991 static unsigned
31992 cp_parser_begin_omp_structured_block (cp_parser *parser)
31993 {
31994 unsigned save = parser->in_statement;
31995
31996 /* Only move the values to IN_OMP_BLOCK if they weren't false.
31997 This preserves the "not within loop or switch" style error messages
31998 for nonsense cases like
31999 void foo() {
32000 #pragma omp single
32001 break;
32002 }
32003 */
32004 if (parser->in_statement)
32005 parser->in_statement = IN_OMP_BLOCK;
32006
32007 return save;
32008 }
32009
32010 static void
32011 cp_parser_end_omp_structured_block (cp_parser *parser, unsigned save)
32012 {
32013 parser->in_statement = save;
32014 }
32015
32016 static tree
32017 cp_parser_omp_structured_block (cp_parser *parser)
32018 {
32019 tree stmt = begin_omp_structured_block ();
32020 unsigned int save = cp_parser_begin_omp_structured_block (parser);
32021
32022 cp_parser_statement (parser, NULL_TREE, false, NULL);
32023
32024 cp_parser_end_omp_structured_block (parser, save);
32025 return finish_omp_structured_block (stmt);
32026 }
32027
32028 /* OpenMP 2.5:
32029 # pragma omp atomic new-line
32030 expression-stmt
32031
32032 expression-stmt:
32033 x binop= expr | x++ | ++x | x-- | --x
32034 binop:
32035 +, *, -, /, &, ^, |, <<, >>
32036
32037 where x is an lvalue expression with scalar type.
32038
32039 OpenMP 3.1:
32040 # pragma omp atomic new-line
32041 update-stmt
32042
32043 # pragma omp atomic read new-line
32044 read-stmt
32045
32046 # pragma omp atomic write new-line
32047 write-stmt
32048
32049 # pragma omp atomic update new-line
32050 update-stmt
32051
32052 # pragma omp atomic capture new-line
32053 capture-stmt
32054
32055 # pragma omp atomic capture new-line
32056 capture-block
32057
32058 read-stmt:
32059 v = x
32060 write-stmt:
32061 x = expr
32062 update-stmt:
32063 expression-stmt | x = x binop expr
32064 capture-stmt:
32065 v = expression-stmt
32066 capture-block:
32067 { v = x; update-stmt; } | { update-stmt; v = x; }
32068
32069 OpenMP 4.0:
32070 update-stmt:
32071 expression-stmt | x = x binop expr | x = expr binop x
32072 capture-stmt:
32073 v = update-stmt
32074 capture-block:
32075 { v = x; update-stmt; } | { update-stmt; v = x; } | { v = x; x = expr; }
32076
32077 where x and v are lvalue expressions with scalar type. */
32078
32079 static void
32080 cp_parser_omp_atomic (cp_parser *parser, cp_token *pragma_tok)
32081 {
32082 tree lhs = NULL_TREE, rhs = NULL_TREE, v = NULL_TREE, lhs1 = NULL_TREE;
32083 tree rhs1 = NULL_TREE, orig_lhs;
32084 enum tree_code code = OMP_ATOMIC, opcode = NOP_EXPR;
32085 bool structured_block = false;
32086 bool seq_cst = false;
32087
32088 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32089 {
32090 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
32091 const char *p = IDENTIFIER_POINTER (id);
32092
32093 if (!strcmp (p, "seq_cst"))
32094 {
32095 seq_cst = true;
32096 cp_lexer_consume_token (parser->lexer);
32097 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)
32098 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME)
32099 cp_lexer_consume_token (parser->lexer);
32100 }
32101 }
32102 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32103 {
32104 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
32105 const char *p = IDENTIFIER_POINTER (id);
32106
32107 if (!strcmp (p, "read"))
32108 code = OMP_ATOMIC_READ;
32109 else if (!strcmp (p, "write"))
32110 code = NOP_EXPR;
32111 else if (!strcmp (p, "update"))
32112 code = OMP_ATOMIC;
32113 else if (!strcmp (p, "capture"))
32114 code = OMP_ATOMIC_CAPTURE_NEW;
32115 else
32116 p = NULL;
32117 if (p)
32118 cp_lexer_consume_token (parser->lexer);
32119 }
32120 if (!seq_cst)
32121 {
32122 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)
32123 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME)
32124 cp_lexer_consume_token (parser->lexer);
32125
32126 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32127 {
32128 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
32129 const char *p = IDENTIFIER_POINTER (id);
32130
32131 if (!strcmp (p, "seq_cst"))
32132 {
32133 seq_cst = true;
32134 cp_lexer_consume_token (parser->lexer);
32135 }
32136 }
32137 }
32138 cp_parser_require_pragma_eol (parser, pragma_tok);
32139
32140 switch (code)
32141 {
32142 case OMP_ATOMIC_READ:
32143 case NOP_EXPR: /* atomic write */
32144 v = cp_parser_unary_expression (parser);
32145 if (v == error_mark_node)
32146 goto saw_error;
32147 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
32148 goto saw_error;
32149 if (code == NOP_EXPR)
32150 lhs = cp_parser_expression (parser);
32151 else
32152 lhs = cp_parser_unary_expression (parser);
32153 if (lhs == error_mark_node)
32154 goto saw_error;
32155 if (code == NOP_EXPR)
32156 {
32157 /* atomic write is represented by OMP_ATOMIC with NOP_EXPR
32158 opcode. */
32159 code = OMP_ATOMIC;
32160 rhs = lhs;
32161 lhs = v;
32162 v = NULL_TREE;
32163 }
32164 goto done;
32165 case OMP_ATOMIC_CAPTURE_NEW:
32166 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
32167 {
32168 cp_lexer_consume_token (parser->lexer);
32169 structured_block = true;
32170 }
32171 else
32172 {
32173 v = cp_parser_unary_expression (parser);
32174 if (v == error_mark_node)
32175 goto saw_error;
32176 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
32177 goto saw_error;
32178 }
32179 default:
32180 break;
32181 }
32182
32183 restart:
32184 lhs = cp_parser_unary_expression (parser);
32185 orig_lhs = lhs;
32186 switch (TREE_CODE (lhs))
32187 {
32188 case ERROR_MARK:
32189 goto saw_error;
32190
32191 case POSTINCREMENT_EXPR:
32192 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
32193 code = OMP_ATOMIC_CAPTURE_OLD;
32194 /* FALLTHROUGH */
32195 case PREINCREMENT_EXPR:
32196 lhs = TREE_OPERAND (lhs, 0);
32197 opcode = PLUS_EXPR;
32198 rhs = integer_one_node;
32199 break;
32200
32201 case POSTDECREMENT_EXPR:
32202 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
32203 code = OMP_ATOMIC_CAPTURE_OLD;
32204 /* FALLTHROUGH */
32205 case PREDECREMENT_EXPR:
32206 lhs = TREE_OPERAND (lhs, 0);
32207 opcode = MINUS_EXPR;
32208 rhs = integer_one_node;
32209 break;
32210
32211 case COMPOUND_EXPR:
32212 if (TREE_CODE (TREE_OPERAND (lhs, 0)) == SAVE_EXPR
32213 && TREE_CODE (TREE_OPERAND (lhs, 1)) == COMPOUND_EXPR
32214 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (lhs, 1), 0)) == MODIFY_EXPR
32215 && TREE_OPERAND (TREE_OPERAND (lhs, 1), 1) == TREE_OPERAND (lhs, 0)
32216 && TREE_CODE (TREE_TYPE (TREE_OPERAND (TREE_OPERAND
32217 (TREE_OPERAND (lhs, 1), 0), 0)))
32218 == BOOLEAN_TYPE)
32219 /* Undo effects of boolean_increment for post {in,de}crement. */
32220 lhs = TREE_OPERAND (TREE_OPERAND (lhs, 1), 0);
32221 /* FALLTHRU */
32222 case MODIFY_EXPR:
32223 if (TREE_CODE (lhs) == MODIFY_EXPR
32224 && TREE_CODE (TREE_TYPE (TREE_OPERAND (lhs, 0))) == BOOLEAN_TYPE)
32225 {
32226 /* Undo effects of boolean_increment. */
32227 if (integer_onep (TREE_OPERAND (lhs, 1)))
32228 {
32229 /* This is pre or post increment. */
32230 rhs = TREE_OPERAND (lhs, 1);
32231 lhs = TREE_OPERAND (lhs, 0);
32232 opcode = NOP_EXPR;
32233 if (code == OMP_ATOMIC_CAPTURE_NEW
32234 && !structured_block
32235 && TREE_CODE (orig_lhs) == COMPOUND_EXPR)
32236 code = OMP_ATOMIC_CAPTURE_OLD;
32237 break;
32238 }
32239 }
32240 /* FALLTHRU */
32241 default:
32242 switch (cp_lexer_peek_token (parser->lexer)->type)
32243 {
32244 case CPP_MULT_EQ:
32245 opcode = MULT_EXPR;
32246 break;
32247 case CPP_DIV_EQ:
32248 opcode = TRUNC_DIV_EXPR;
32249 break;
32250 case CPP_PLUS_EQ:
32251 opcode = PLUS_EXPR;
32252 break;
32253 case CPP_MINUS_EQ:
32254 opcode = MINUS_EXPR;
32255 break;
32256 case CPP_LSHIFT_EQ:
32257 opcode = LSHIFT_EXPR;
32258 break;
32259 case CPP_RSHIFT_EQ:
32260 opcode = RSHIFT_EXPR;
32261 break;
32262 case CPP_AND_EQ:
32263 opcode = BIT_AND_EXPR;
32264 break;
32265 case CPP_OR_EQ:
32266 opcode = BIT_IOR_EXPR;
32267 break;
32268 case CPP_XOR_EQ:
32269 opcode = BIT_XOR_EXPR;
32270 break;
32271 case CPP_EQ:
32272 enum cp_parser_prec oprec;
32273 cp_token *token;
32274 cp_lexer_consume_token (parser->lexer);
32275 cp_parser_parse_tentatively (parser);
32276 rhs1 = cp_parser_simple_cast_expression (parser);
32277 if (rhs1 == error_mark_node)
32278 {
32279 cp_parser_abort_tentative_parse (parser);
32280 cp_parser_simple_cast_expression (parser);
32281 goto saw_error;
32282 }
32283 token = cp_lexer_peek_token (parser->lexer);
32284 if (token->type != CPP_SEMICOLON && !cp_tree_equal (lhs, rhs1))
32285 {
32286 cp_parser_abort_tentative_parse (parser);
32287 cp_parser_parse_tentatively (parser);
32288 rhs = cp_parser_binary_expression (parser, false, true,
32289 PREC_NOT_OPERATOR, NULL);
32290 if (rhs == error_mark_node)
32291 {
32292 cp_parser_abort_tentative_parse (parser);
32293 cp_parser_binary_expression (parser, false, true,
32294 PREC_NOT_OPERATOR, NULL);
32295 goto saw_error;
32296 }
32297 switch (TREE_CODE (rhs))
32298 {
32299 case MULT_EXPR:
32300 case TRUNC_DIV_EXPR:
32301 case RDIV_EXPR:
32302 case PLUS_EXPR:
32303 case MINUS_EXPR:
32304 case LSHIFT_EXPR:
32305 case RSHIFT_EXPR:
32306 case BIT_AND_EXPR:
32307 case BIT_IOR_EXPR:
32308 case BIT_XOR_EXPR:
32309 if (cp_tree_equal (lhs, TREE_OPERAND (rhs, 1)))
32310 {
32311 if (cp_parser_parse_definitely (parser))
32312 {
32313 opcode = TREE_CODE (rhs);
32314 rhs1 = TREE_OPERAND (rhs, 0);
32315 rhs = TREE_OPERAND (rhs, 1);
32316 goto stmt_done;
32317 }
32318 else
32319 goto saw_error;
32320 }
32321 break;
32322 default:
32323 break;
32324 }
32325 cp_parser_abort_tentative_parse (parser);
32326 if (structured_block && code == OMP_ATOMIC_CAPTURE_OLD)
32327 {
32328 rhs = cp_parser_expression (parser);
32329 if (rhs == error_mark_node)
32330 goto saw_error;
32331 opcode = NOP_EXPR;
32332 rhs1 = NULL_TREE;
32333 goto stmt_done;
32334 }
32335 cp_parser_error (parser,
32336 "invalid form of %<#pragma omp atomic%>");
32337 goto saw_error;
32338 }
32339 if (!cp_parser_parse_definitely (parser))
32340 goto saw_error;
32341 switch (token->type)
32342 {
32343 case CPP_SEMICOLON:
32344 if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
32345 {
32346 code = OMP_ATOMIC_CAPTURE_OLD;
32347 v = lhs;
32348 lhs = NULL_TREE;
32349 lhs1 = rhs1;
32350 rhs1 = NULL_TREE;
32351 cp_lexer_consume_token (parser->lexer);
32352 goto restart;
32353 }
32354 else if (structured_block)
32355 {
32356 opcode = NOP_EXPR;
32357 rhs = rhs1;
32358 rhs1 = NULL_TREE;
32359 goto stmt_done;
32360 }
32361 cp_parser_error (parser,
32362 "invalid form of %<#pragma omp atomic%>");
32363 goto saw_error;
32364 case CPP_MULT:
32365 opcode = MULT_EXPR;
32366 break;
32367 case CPP_DIV:
32368 opcode = TRUNC_DIV_EXPR;
32369 break;
32370 case CPP_PLUS:
32371 opcode = PLUS_EXPR;
32372 break;
32373 case CPP_MINUS:
32374 opcode = MINUS_EXPR;
32375 break;
32376 case CPP_LSHIFT:
32377 opcode = LSHIFT_EXPR;
32378 break;
32379 case CPP_RSHIFT:
32380 opcode = RSHIFT_EXPR;
32381 break;
32382 case CPP_AND:
32383 opcode = BIT_AND_EXPR;
32384 break;
32385 case CPP_OR:
32386 opcode = BIT_IOR_EXPR;
32387 break;
32388 case CPP_XOR:
32389 opcode = BIT_XOR_EXPR;
32390 break;
32391 default:
32392 cp_parser_error (parser,
32393 "invalid operator for %<#pragma omp atomic%>");
32394 goto saw_error;
32395 }
32396 oprec = TOKEN_PRECEDENCE (token);
32397 gcc_assert (oprec != PREC_NOT_OPERATOR);
32398 if (commutative_tree_code (opcode))
32399 oprec = (enum cp_parser_prec) (oprec - 1);
32400 cp_lexer_consume_token (parser->lexer);
32401 rhs = cp_parser_binary_expression (parser, false, false,
32402 oprec, NULL);
32403 if (rhs == error_mark_node)
32404 goto saw_error;
32405 goto stmt_done;
32406 /* FALLTHROUGH */
32407 default:
32408 cp_parser_error (parser,
32409 "invalid operator for %<#pragma omp atomic%>");
32410 goto saw_error;
32411 }
32412 cp_lexer_consume_token (parser->lexer);
32413
32414 rhs = cp_parser_expression (parser);
32415 if (rhs == error_mark_node)
32416 goto saw_error;
32417 break;
32418 }
32419 stmt_done:
32420 if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
32421 {
32422 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
32423 goto saw_error;
32424 v = cp_parser_unary_expression (parser);
32425 if (v == error_mark_node)
32426 goto saw_error;
32427 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
32428 goto saw_error;
32429 lhs1 = cp_parser_unary_expression (parser);
32430 if (lhs1 == error_mark_node)
32431 goto saw_error;
32432 }
32433 if (structured_block)
32434 {
32435 cp_parser_consume_semicolon_at_end_of_statement (parser);
32436 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
32437 }
32438 done:
32439 finish_omp_atomic (code, opcode, lhs, rhs, v, lhs1, rhs1, seq_cst);
32440 if (!structured_block)
32441 cp_parser_consume_semicolon_at_end_of_statement (parser);
32442 return;
32443
32444 saw_error:
32445 cp_parser_skip_to_end_of_block_or_statement (parser);
32446 if (structured_block)
32447 {
32448 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
32449 cp_lexer_consume_token (parser->lexer);
32450 else if (code == OMP_ATOMIC_CAPTURE_NEW)
32451 {
32452 cp_parser_skip_to_end_of_block_or_statement (parser);
32453 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
32454 cp_lexer_consume_token (parser->lexer);
32455 }
32456 }
32457 }
32458
32459
32460 /* OpenMP 2.5:
32461 # pragma omp barrier new-line */
32462
32463 static void
32464 cp_parser_omp_barrier (cp_parser *parser, cp_token *pragma_tok)
32465 {
32466 cp_parser_require_pragma_eol (parser, pragma_tok);
32467 finish_omp_barrier ();
32468 }
32469
32470 /* OpenMP 2.5:
32471 # pragma omp critical [(name)] new-line
32472 structured-block
32473
32474 OpenMP 4.5:
32475 # pragma omp critical [(name) [hint(expression)]] new-line
32476 structured-block */
32477
32478 #define OMP_CRITICAL_CLAUSE_MASK \
32479 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_HINT) )
32480
32481 static tree
32482 cp_parser_omp_critical (cp_parser *parser, cp_token *pragma_tok)
32483 {
32484 tree stmt, name = NULL_TREE, clauses = NULL_TREE;
32485
32486 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
32487 {
32488 cp_lexer_consume_token (parser->lexer);
32489
32490 name = cp_parser_identifier (parser);
32491
32492 if (name == error_mark_node
32493 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
32494 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32495 /*or_comma=*/false,
32496 /*consume_paren=*/true);
32497 if (name == error_mark_node)
32498 name = NULL;
32499
32500 clauses = cp_parser_omp_all_clauses (parser,
32501 OMP_CRITICAL_CLAUSE_MASK,
32502 "#pragma omp critical", pragma_tok);
32503 }
32504 else
32505 cp_parser_require_pragma_eol (parser, pragma_tok);
32506
32507 stmt = cp_parser_omp_structured_block (parser);
32508 return c_finish_omp_critical (input_location, stmt, name, clauses);
32509 }
32510
32511 /* OpenMP 2.5:
32512 # pragma omp flush flush-vars[opt] new-line
32513
32514 flush-vars:
32515 ( variable-list ) */
32516
32517 static void
32518 cp_parser_omp_flush (cp_parser *parser, cp_token *pragma_tok)
32519 {
32520 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
32521 (void) cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
32522 cp_parser_require_pragma_eol (parser, pragma_tok);
32523
32524 finish_omp_flush ();
32525 }
32526
32527 /* Helper function, to parse omp for increment expression. */
32528
32529 static tree
32530 cp_parser_omp_for_cond (cp_parser *parser, tree decl, enum tree_code code)
32531 {
32532 tree cond = cp_parser_binary_expression (parser, false, true,
32533 PREC_NOT_OPERATOR, NULL);
32534 if (cond == error_mark_node
32535 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
32536 {
32537 cp_parser_skip_to_end_of_statement (parser);
32538 return error_mark_node;
32539 }
32540
32541 switch (TREE_CODE (cond))
32542 {
32543 case GT_EXPR:
32544 case GE_EXPR:
32545 case LT_EXPR:
32546 case LE_EXPR:
32547 break;
32548 case NE_EXPR:
32549 if (code == CILK_SIMD || code == CILK_FOR)
32550 break;
32551 /* Fall through: OpenMP disallows NE_EXPR. */
32552 default:
32553 return error_mark_node;
32554 }
32555
32556 /* If decl is an iterator, preserve LHS and RHS of the relational
32557 expr until finish_omp_for. */
32558 if (decl
32559 && (type_dependent_expression_p (decl)
32560 || CLASS_TYPE_P (TREE_TYPE (decl))))
32561 return cond;
32562
32563 return build_x_binary_op (input_location, TREE_CODE (cond),
32564 TREE_OPERAND (cond, 0), ERROR_MARK,
32565 TREE_OPERAND (cond, 1), ERROR_MARK,
32566 /*overload=*/NULL, tf_warning_or_error);
32567 }
32568
32569 /* Helper function, to parse omp for increment expression. */
32570
32571 static tree
32572 cp_parser_omp_for_incr (cp_parser *parser, tree decl)
32573 {
32574 cp_token *token = cp_lexer_peek_token (parser->lexer);
32575 enum tree_code op;
32576 tree lhs, rhs;
32577 cp_id_kind idk;
32578 bool decl_first;
32579
32580 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
32581 {
32582 op = (token->type == CPP_PLUS_PLUS
32583 ? PREINCREMENT_EXPR : PREDECREMENT_EXPR);
32584 cp_lexer_consume_token (parser->lexer);
32585 lhs = cp_parser_simple_cast_expression (parser);
32586 if (lhs != decl
32587 && (!processing_template_decl || !cp_tree_equal (lhs, decl)))
32588 return error_mark_node;
32589 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
32590 }
32591
32592 lhs = cp_parser_primary_expression (parser, false, false, false, &idk);
32593 if (lhs != decl
32594 && (!processing_template_decl || !cp_tree_equal (lhs, decl)))
32595 return error_mark_node;
32596
32597 token = cp_lexer_peek_token (parser->lexer);
32598 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
32599 {
32600 op = (token->type == CPP_PLUS_PLUS
32601 ? POSTINCREMENT_EXPR : POSTDECREMENT_EXPR);
32602 cp_lexer_consume_token (parser->lexer);
32603 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
32604 }
32605
32606 op = cp_parser_assignment_operator_opt (parser);
32607 if (op == ERROR_MARK)
32608 return error_mark_node;
32609
32610 if (op != NOP_EXPR)
32611 {
32612 rhs = cp_parser_assignment_expression (parser);
32613 rhs = build2 (op, TREE_TYPE (decl), decl, rhs);
32614 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
32615 }
32616
32617 lhs = cp_parser_binary_expression (parser, false, false,
32618 PREC_ADDITIVE_EXPRESSION, NULL);
32619 token = cp_lexer_peek_token (parser->lexer);
32620 decl_first = (lhs == decl
32621 || (processing_template_decl && cp_tree_equal (lhs, decl)));
32622 if (decl_first)
32623 lhs = NULL_TREE;
32624 if (token->type != CPP_PLUS
32625 && token->type != CPP_MINUS)
32626 return error_mark_node;
32627
32628 do
32629 {
32630 op = token->type == CPP_PLUS ? PLUS_EXPR : MINUS_EXPR;
32631 cp_lexer_consume_token (parser->lexer);
32632 rhs = cp_parser_binary_expression (parser, false, false,
32633 PREC_ADDITIVE_EXPRESSION, NULL);
32634 token = cp_lexer_peek_token (parser->lexer);
32635 if (token->type == CPP_PLUS || token->type == CPP_MINUS || decl_first)
32636 {
32637 if (lhs == NULL_TREE)
32638 {
32639 if (op == PLUS_EXPR)
32640 lhs = rhs;
32641 else
32642 lhs = build_x_unary_op (input_location, NEGATE_EXPR, rhs,
32643 tf_warning_or_error);
32644 }
32645 else
32646 lhs = build_x_binary_op (input_location, op, lhs, ERROR_MARK, rhs,
32647 ERROR_MARK, NULL, tf_warning_or_error);
32648 }
32649 }
32650 while (token->type == CPP_PLUS || token->type == CPP_MINUS);
32651
32652 if (!decl_first)
32653 {
32654 if ((rhs != decl
32655 && (!processing_template_decl || !cp_tree_equal (rhs, decl)))
32656 || op == MINUS_EXPR)
32657 return error_mark_node;
32658 rhs = build2 (op, TREE_TYPE (decl), lhs, decl);
32659 }
32660 else
32661 rhs = build2 (PLUS_EXPR, TREE_TYPE (decl), decl, lhs);
32662
32663 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
32664 }
32665
32666 /* Parse the initialization statement of either an OpenMP for loop or
32667 a Cilk Plus for loop.
32668
32669 Return true if the resulting construct should have an
32670 OMP_CLAUSE_PRIVATE added to it. */
32671
32672 static tree
32673 cp_parser_omp_for_loop_init (cp_parser *parser,
32674 enum tree_code code,
32675 tree &this_pre_body,
32676 vec<tree, va_gc> *for_block,
32677 tree &init,
32678 tree &orig_init,
32679 tree &decl,
32680 tree &real_decl)
32681 {
32682 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
32683 return NULL_TREE;
32684
32685 tree add_private_clause = NULL_TREE;
32686
32687 /* See 2.5.1 (in OpenMP 3.0, similar wording is in 2.5 standard too):
32688
32689 init-expr:
32690 var = lb
32691 integer-type var = lb
32692 random-access-iterator-type var = lb
32693 pointer-type var = lb
32694 */
32695 cp_decl_specifier_seq type_specifiers;
32696
32697 /* First, try to parse as an initialized declaration. See
32698 cp_parser_condition, from whence the bulk of this is copied. */
32699
32700 cp_parser_parse_tentatively (parser);
32701 cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
32702 /*is_trailing_return=*/false,
32703 &type_specifiers);
32704 if (cp_parser_parse_definitely (parser))
32705 {
32706 /* If parsing a type specifier seq succeeded, then this
32707 MUST be a initialized declaration. */
32708 tree asm_specification, attributes;
32709 cp_declarator *declarator;
32710
32711 declarator = cp_parser_declarator (parser,
32712 CP_PARSER_DECLARATOR_NAMED,
32713 /*ctor_dtor_or_conv_p=*/NULL,
32714 /*parenthesized_p=*/NULL,
32715 /*member_p=*/false,
32716 /*friend_p=*/false);
32717 attributes = cp_parser_attributes_opt (parser);
32718 asm_specification = cp_parser_asm_specification_opt (parser);
32719
32720 if (declarator == cp_error_declarator)
32721 cp_parser_skip_to_end_of_statement (parser);
32722
32723 else
32724 {
32725 tree pushed_scope, auto_node;
32726
32727 decl = start_decl (declarator, &type_specifiers,
32728 SD_INITIALIZED, attributes,
32729 /*prefix_attributes=*/NULL_TREE,
32730 &pushed_scope);
32731
32732 auto_node = type_uses_auto (TREE_TYPE (decl));
32733 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
32734 {
32735 if (cp_lexer_next_token_is (parser->lexer,
32736 CPP_OPEN_PAREN))
32737 {
32738 if (code != CILK_SIMD && code != CILK_FOR)
32739 error ("parenthesized initialization is not allowed in "
32740 "OpenMP %<for%> loop");
32741 else
32742 error ("parenthesized initialization is "
32743 "not allowed in for-loop");
32744 }
32745 else
32746 /* Trigger an error. */
32747 cp_parser_require (parser, CPP_EQ, RT_EQ);
32748
32749 init = error_mark_node;
32750 cp_parser_skip_to_end_of_statement (parser);
32751 }
32752 else if (CLASS_TYPE_P (TREE_TYPE (decl))
32753 || type_dependent_expression_p (decl)
32754 || auto_node)
32755 {
32756 bool is_direct_init, is_non_constant_init;
32757
32758 init = cp_parser_initializer (parser,
32759 &is_direct_init,
32760 &is_non_constant_init);
32761
32762 if (auto_node)
32763 {
32764 TREE_TYPE (decl)
32765 = do_auto_deduction (TREE_TYPE (decl), init,
32766 auto_node);
32767
32768 if (!CLASS_TYPE_P (TREE_TYPE (decl))
32769 && !type_dependent_expression_p (decl))
32770 goto non_class;
32771 }
32772
32773 cp_finish_decl (decl, init, !is_non_constant_init,
32774 asm_specification,
32775 LOOKUP_ONLYCONVERTING);
32776 orig_init = init;
32777 if (CLASS_TYPE_P (TREE_TYPE (decl)))
32778 {
32779 vec_safe_push (for_block, this_pre_body);
32780 init = NULL_TREE;
32781 }
32782 else
32783 init = pop_stmt_list (this_pre_body);
32784 this_pre_body = NULL_TREE;
32785 }
32786 else
32787 {
32788 /* Consume '='. */
32789 cp_lexer_consume_token (parser->lexer);
32790 init = cp_parser_assignment_expression (parser);
32791
32792 non_class:
32793 if (TREE_CODE (TREE_TYPE (decl)) == REFERENCE_TYPE)
32794 init = error_mark_node;
32795 else
32796 cp_finish_decl (decl, NULL_TREE,
32797 /*init_const_expr_p=*/false,
32798 asm_specification,
32799 LOOKUP_ONLYCONVERTING);
32800 }
32801
32802 if (pushed_scope)
32803 pop_scope (pushed_scope);
32804 }
32805 }
32806 else
32807 {
32808 cp_id_kind idk;
32809 /* If parsing a type specifier sequence failed, then
32810 this MUST be a simple expression. */
32811 if (code == CILK_FOR)
32812 error ("%<_Cilk_for%> allows expression instead of declaration only "
32813 "in C, not in C++");
32814 cp_parser_parse_tentatively (parser);
32815 decl = cp_parser_primary_expression (parser, false, false,
32816 false, &idk);
32817 cp_token *last_tok = cp_lexer_peek_token (parser->lexer);
32818 if (!cp_parser_error_occurred (parser)
32819 && decl
32820 && (TREE_CODE (decl) == COMPONENT_REF
32821 || (TREE_CODE (decl) == SCOPE_REF && TREE_TYPE (decl))))
32822 {
32823 cp_parser_abort_tentative_parse (parser);
32824 cp_parser_parse_tentatively (parser);
32825 cp_token *token = cp_lexer_peek_token (parser->lexer);
32826 tree name = cp_parser_id_expression (parser, /*template_p=*/false,
32827 /*check_dependency_p=*/true,
32828 /*template_p=*/NULL,
32829 /*declarator_p=*/false,
32830 /*optional_p=*/false);
32831 if (name != error_mark_node
32832 && last_tok == cp_lexer_peek_token (parser->lexer))
32833 {
32834 decl = cp_parser_lookup_name_simple (parser, name,
32835 token->location);
32836 if (TREE_CODE (decl) == FIELD_DECL)
32837 add_private_clause = omp_privatize_field (decl, false);
32838 }
32839 cp_parser_abort_tentative_parse (parser);
32840 cp_parser_parse_tentatively (parser);
32841 decl = cp_parser_primary_expression (parser, false, false,
32842 false, &idk);
32843 }
32844 if (!cp_parser_error_occurred (parser)
32845 && decl
32846 && DECL_P (decl)
32847 && CLASS_TYPE_P (TREE_TYPE (decl)))
32848 {
32849 tree rhs;
32850
32851 cp_parser_parse_definitely (parser);
32852 cp_parser_require (parser, CPP_EQ, RT_EQ);
32853 rhs = cp_parser_assignment_expression (parser);
32854 orig_init = rhs;
32855 finish_expr_stmt (build_x_modify_expr (EXPR_LOCATION (rhs),
32856 decl, NOP_EXPR,
32857 rhs,
32858 tf_warning_or_error));
32859 if (!add_private_clause)
32860 add_private_clause = decl;
32861 }
32862 else
32863 {
32864 decl = NULL;
32865 cp_parser_abort_tentative_parse (parser);
32866 init = cp_parser_expression (parser);
32867 if (init)
32868 {
32869 if (TREE_CODE (init) == MODIFY_EXPR
32870 || TREE_CODE (init) == MODOP_EXPR)
32871 real_decl = TREE_OPERAND (init, 0);
32872 }
32873 }
32874 }
32875 return add_private_clause;
32876 }
32877
32878 /* Parse the restricted form of the for statement allowed by OpenMP. */
32879
32880 static tree
32881 cp_parser_omp_for_loop (cp_parser *parser, enum tree_code code, tree clauses,
32882 tree *cclauses)
32883 {
32884 tree init, orig_init, cond, incr, body, decl, pre_body = NULL_TREE, ret;
32885 tree real_decl, initv, condv, incrv, declv;
32886 tree this_pre_body, cl, ordered_cl = NULL_TREE;
32887 location_t loc_first;
32888 bool collapse_err = false;
32889 int i, collapse = 1, ordered = 0, count, nbraces = 0;
32890 vec<tree, va_gc> *for_block = make_tree_vector ();
32891 auto_vec<tree, 4> orig_inits;
32892
32893 for (cl = clauses; cl; cl = OMP_CLAUSE_CHAIN (cl))
32894 if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_COLLAPSE)
32895 collapse = tree_to_shwi (OMP_CLAUSE_COLLAPSE_EXPR (cl));
32896 else if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_ORDERED
32897 && OMP_CLAUSE_ORDERED_EXPR (cl))
32898 {
32899 ordered_cl = cl;
32900 ordered = tree_to_shwi (OMP_CLAUSE_ORDERED_EXPR (cl));
32901 }
32902
32903 if (ordered && ordered < collapse)
32904 {
32905 error_at (OMP_CLAUSE_LOCATION (ordered_cl),
32906 "%<ordered%> clause parameter is less than %<collapse%>");
32907 OMP_CLAUSE_ORDERED_EXPR (ordered_cl)
32908 = build_int_cst (NULL_TREE, collapse);
32909 ordered = collapse;
32910 }
32911 if (ordered)
32912 {
32913 for (tree *pc = &clauses; *pc; )
32914 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_LINEAR)
32915 {
32916 error_at (OMP_CLAUSE_LOCATION (*pc),
32917 "%<linear%> clause may not be specified together "
32918 "with %<ordered%> clause with a parameter");
32919 *pc = OMP_CLAUSE_CHAIN (*pc);
32920 }
32921 else
32922 pc = &OMP_CLAUSE_CHAIN (*pc);
32923 }
32924
32925 gcc_assert (collapse >= 1 && ordered >= 0);
32926 count = ordered ? ordered : collapse;
32927
32928 declv = make_tree_vec (count);
32929 initv = make_tree_vec (count);
32930 condv = make_tree_vec (count);
32931 incrv = make_tree_vec (count);
32932
32933 loc_first = cp_lexer_peek_token (parser->lexer)->location;
32934
32935 for (i = 0; i < count; i++)
32936 {
32937 int bracecount = 0;
32938 tree add_private_clause = NULL_TREE;
32939 location_t loc;
32940
32941 if (code != CILK_FOR
32942 && !cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
32943 {
32944 cp_parser_error (parser, "for statement expected");
32945 return NULL;
32946 }
32947 if (code == CILK_FOR
32948 && !cp_lexer_next_token_is_keyword (parser->lexer, RID_CILK_FOR))
32949 {
32950 cp_parser_error (parser, "_Cilk_for statement expected");
32951 return NULL;
32952 }
32953 loc = cp_lexer_consume_token (parser->lexer)->location;
32954
32955 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
32956 return NULL;
32957
32958 init = orig_init = decl = real_decl = NULL;
32959 this_pre_body = push_stmt_list ();
32960
32961 add_private_clause
32962 = cp_parser_omp_for_loop_init (parser, code,
32963 this_pre_body, for_block,
32964 init, orig_init, decl, real_decl);
32965
32966 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
32967 if (this_pre_body)
32968 {
32969 this_pre_body = pop_stmt_list (this_pre_body);
32970 if (pre_body)
32971 {
32972 tree t = pre_body;
32973 pre_body = push_stmt_list ();
32974 add_stmt (t);
32975 add_stmt (this_pre_body);
32976 pre_body = pop_stmt_list (pre_body);
32977 }
32978 else
32979 pre_body = this_pre_body;
32980 }
32981
32982 if (decl)
32983 real_decl = decl;
32984 if (cclauses != NULL
32985 && cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL] != NULL
32986 && real_decl != NULL_TREE)
32987 {
32988 tree *c;
32989 for (c = &cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL]; *c ; )
32990 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_FIRSTPRIVATE
32991 && OMP_CLAUSE_DECL (*c) == real_decl)
32992 {
32993 error_at (loc, "iteration variable %qD"
32994 " should not be firstprivate", real_decl);
32995 *c = OMP_CLAUSE_CHAIN (*c);
32996 }
32997 else if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_LASTPRIVATE
32998 && OMP_CLAUSE_DECL (*c) == real_decl)
32999 {
33000 /* Move lastprivate (decl) clause to OMP_FOR_CLAUSES. */
33001 tree l = *c;
33002 *c = OMP_CLAUSE_CHAIN (*c);
33003 if (code == OMP_SIMD)
33004 {
33005 OMP_CLAUSE_CHAIN (l) = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
33006 cclauses[C_OMP_CLAUSE_SPLIT_FOR] = l;
33007 }
33008 else
33009 {
33010 OMP_CLAUSE_CHAIN (l) = clauses;
33011 clauses = l;
33012 }
33013 add_private_clause = NULL_TREE;
33014 }
33015 else
33016 {
33017 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_PRIVATE
33018 && OMP_CLAUSE_DECL (*c) == real_decl)
33019 add_private_clause = NULL_TREE;
33020 c = &OMP_CLAUSE_CHAIN (*c);
33021 }
33022 }
33023
33024 if (add_private_clause)
33025 {
33026 tree c;
33027 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
33028 {
33029 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
33030 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
33031 && OMP_CLAUSE_DECL (c) == decl)
33032 break;
33033 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
33034 && OMP_CLAUSE_DECL (c) == decl)
33035 error_at (loc, "iteration variable %qD "
33036 "should not be firstprivate",
33037 decl);
33038 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
33039 && OMP_CLAUSE_DECL (c) == decl)
33040 error_at (loc, "iteration variable %qD should not be reduction",
33041 decl);
33042 }
33043 if (c == NULL)
33044 {
33045 if (code != OMP_SIMD)
33046 c = build_omp_clause (loc, OMP_CLAUSE_PRIVATE);
33047 else if (collapse == 1)
33048 c = build_omp_clause (loc, OMP_CLAUSE_LINEAR);
33049 else
33050 c = build_omp_clause (loc, OMP_CLAUSE_LASTPRIVATE);
33051 OMP_CLAUSE_DECL (c) = add_private_clause;
33052 c = finish_omp_clauses (c, true);
33053 if (c)
33054 {
33055 OMP_CLAUSE_CHAIN (c) = clauses;
33056 clauses = c;
33057 /* For linear, signal that we need to fill up
33058 the so far unknown linear step. */
33059 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINEAR)
33060 OMP_CLAUSE_LINEAR_STEP (c) = NULL_TREE;
33061 }
33062 }
33063 }
33064
33065 cond = NULL;
33066 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
33067 cond = cp_parser_omp_for_cond (parser, decl, code);
33068 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
33069
33070 incr = NULL;
33071 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
33072 {
33073 /* If decl is an iterator, preserve the operator on decl
33074 until finish_omp_for. */
33075 if (real_decl
33076 && ((processing_template_decl
33077 && (TREE_TYPE (real_decl) == NULL_TREE
33078 || !POINTER_TYPE_P (TREE_TYPE (real_decl))))
33079 || CLASS_TYPE_P (TREE_TYPE (real_decl))))
33080 incr = cp_parser_omp_for_incr (parser, real_decl);
33081 else
33082 incr = cp_parser_expression (parser);
33083 if (!EXPR_HAS_LOCATION (incr))
33084 protected_set_expr_location (incr, input_location);
33085 }
33086
33087 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
33088 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33089 /*or_comma=*/false,
33090 /*consume_paren=*/true);
33091
33092 TREE_VEC_ELT (declv, i) = decl;
33093 TREE_VEC_ELT (initv, i) = init;
33094 TREE_VEC_ELT (condv, i) = cond;
33095 TREE_VEC_ELT (incrv, i) = incr;
33096 if (orig_init)
33097 {
33098 orig_inits.safe_grow_cleared (i + 1);
33099 orig_inits[i] = orig_init;
33100 }
33101
33102 if (i == count - 1)
33103 break;
33104
33105 /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
33106 in between the collapsed for loops to be still considered perfectly
33107 nested. Hopefully the final version clarifies this.
33108 For now handle (multiple) {'s and empty statements. */
33109 cp_parser_parse_tentatively (parser);
33110 do
33111 {
33112 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
33113 break;
33114 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
33115 {
33116 cp_lexer_consume_token (parser->lexer);
33117 bracecount++;
33118 }
33119 else if (bracecount
33120 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
33121 cp_lexer_consume_token (parser->lexer);
33122 else
33123 {
33124 loc = cp_lexer_peek_token (parser->lexer)->location;
33125 error_at (loc, "not enough collapsed for loops");
33126 collapse_err = true;
33127 cp_parser_abort_tentative_parse (parser);
33128 declv = NULL_TREE;
33129 break;
33130 }
33131 }
33132 while (1);
33133
33134 if (declv)
33135 {
33136 cp_parser_parse_definitely (parser);
33137 nbraces += bracecount;
33138 }
33139 }
33140
33141 /* Note that we saved the original contents of this flag when we entered
33142 the structured block, and so we don't need to re-save it here. */
33143 if (code == CILK_SIMD || code == CILK_FOR)
33144 parser->in_statement = IN_CILK_SIMD_FOR;
33145 else
33146 parser->in_statement = IN_OMP_FOR;
33147
33148 /* Note that the grammar doesn't call for a structured block here,
33149 though the loop as a whole is a structured block. */
33150 body = push_stmt_list ();
33151 cp_parser_statement (parser, NULL_TREE, false, NULL);
33152 body = pop_stmt_list (body);
33153
33154 if (declv == NULL_TREE)
33155 ret = NULL_TREE;
33156 else
33157 ret = finish_omp_for (loc_first, code, declv, NULL, initv, condv, incrv,
33158 body, pre_body, &orig_inits, clauses);
33159
33160 while (nbraces)
33161 {
33162 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
33163 {
33164 cp_lexer_consume_token (parser->lexer);
33165 nbraces--;
33166 }
33167 else if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
33168 cp_lexer_consume_token (parser->lexer);
33169 else
33170 {
33171 if (!collapse_err)
33172 {
33173 error_at (cp_lexer_peek_token (parser->lexer)->location,
33174 "collapsed loops not perfectly nested");
33175 }
33176 collapse_err = true;
33177 cp_parser_statement_seq_opt (parser, NULL);
33178 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
33179 break;
33180 }
33181 }
33182
33183 while (!for_block->is_empty ())
33184 add_stmt (pop_stmt_list (for_block->pop ()));
33185 release_tree_vector (for_block);
33186
33187 return ret;
33188 }
33189
33190 /* Helper function for OpenMP parsing, split clauses and call
33191 finish_omp_clauses on each of the set of clauses afterwards. */
33192
33193 static void
33194 cp_omp_split_clauses (location_t loc, enum tree_code code,
33195 omp_clause_mask mask, tree clauses, tree *cclauses)
33196 {
33197 int i;
33198 c_omp_split_clauses (loc, code, mask, clauses, cclauses);
33199 for (i = 0; i < C_OMP_CLAUSE_SPLIT_COUNT; i++)
33200 if (cclauses[i])
33201 cclauses[i] = finish_omp_clauses (cclauses[i], true);
33202 }
33203
33204 /* OpenMP 4.0:
33205 #pragma omp simd simd-clause[optseq] new-line
33206 for-loop */
33207
33208 #define OMP_SIMD_CLAUSE_MASK \
33209 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SAFELEN) \
33210 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN) \
33211 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
33212 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
33213 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
33214 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
33215 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
33216 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
33217
33218 static tree
33219 cp_parser_omp_simd (cp_parser *parser, cp_token *pragma_tok,
33220 char *p_name, omp_clause_mask mask, tree *cclauses)
33221 {
33222 tree clauses, sb, ret;
33223 unsigned int save;
33224 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
33225
33226 strcat (p_name, " simd");
33227 mask |= OMP_SIMD_CLAUSE_MASK;
33228
33229 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
33230 cclauses == NULL);
33231 if (cclauses)
33232 {
33233 cp_omp_split_clauses (loc, OMP_SIMD, mask, clauses, cclauses);
33234 clauses = cclauses[C_OMP_CLAUSE_SPLIT_SIMD];
33235 tree c = find_omp_clause (cclauses[C_OMP_CLAUSE_SPLIT_FOR],
33236 OMP_CLAUSE_ORDERED);
33237 if (c && OMP_CLAUSE_ORDERED_EXPR (c))
33238 {
33239 error_at (OMP_CLAUSE_LOCATION (c),
33240 "%<ordered%> clause with parameter may not be specified "
33241 "on %qs construct", p_name);
33242 OMP_CLAUSE_ORDERED_EXPR (c) = NULL_TREE;
33243 }
33244 }
33245
33246 sb = begin_omp_structured_block ();
33247 save = cp_parser_begin_omp_structured_block (parser);
33248
33249 ret = cp_parser_omp_for_loop (parser, OMP_SIMD, clauses, cclauses);
33250
33251 cp_parser_end_omp_structured_block (parser, save);
33252 add_stmt (finish_omp_structured_block (sb));
33253
33254 return ret;
33255 }
33256
33257 /* OpenMP 2.5:
33258 #pragma omp for for-clause[optseq] new-line
33259 for-loop
33260
33261 OpenMP 4.0:
33262 #pragma omp for simd for-simd-clause[optseq] new-line
33263 for-loop */
33264
33265 #define OMP_FOR_CLAUSE_MASK \
33266 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
33267 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
33268 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
33269 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
33270 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
33271 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED) \
33272 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SCHEDULE) \
33273 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT) \
33274 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
33275
33276 static tree
33277 cp_parser_omp_for (cp_parser *parser, cp_token *pragma_tok,
33278 char *p_name, omp_clause_mask mask, tree *cclauses)
33279 {
33280 tree clauses, sb, ret;
33281 unsigned int save;
33282 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
33283
33284 strcat (p_name, " for");
33285 mask |= OMP_FOR_CLAUSE_MASK;
33286 if (cclauses)
33287 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT);
33288 /* Composite distribute parallel for{, simd} disallows ordered clause. */
33289 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
33290 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED);
33291
33292 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
33293 {
33294 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
33295 const char *p = IDENTIFIER_POINTER (id);
33296
33297 if (strcmp (p, "simd") == 0)
33298 {
33299 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
33300 if (cclauses == NULL)
33301 cclauses = cclauses_buf;
33302
33303 cp_lexer_consume_token (parser->lexer);
33304 if (!flag_openmp) /* flag_openmp_simd */
33305 return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
33306 cclauses);
33307 sb = begin_omp_structured_block ();
33308 save = cp_parser_begin_omp_structured_block (parser);
33309 ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
33310 cclauses);
33311 cp_parser_end_omp_structured_block (parser, save);
33312 tree body = finish_omp_structured_block (sb);
33313 if (ret == NULL)
33314 return ret;
33315 ret = make_node (OMP_FOR);
33316 TREE_TYPE (ret) = void_type_node;
33317 OMP_FOR_BODY (ret) = body;
33318 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
33319 SET_EXPR_LOCATION (ret, loc);
33320 add_stmt (ret);
33321 return ret;
33322 }
33323 }
33324 if (!flag_openmp) /* flag_openmp_simd */
33325 {
33326 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
33327 return NULL_TREE;
33328 }
33329
33330 /* Composite distribute parallel for disallows linear clause. */
33331 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
33332 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR);
33333
33334 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
33335 cclauses == NULL);
33336 if (cclauses)
33337 {
33338 cp_omp_split_clauses (loc, OMP_FOR, mask, clauses, cclauses);
33339 clauses = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
33340 }
33341
33342 sb = begin_omp_structured_block ();
33343 save = cp_parser_begin_omp_structured_block (parser);
33344
33345 ret = cp_parser_omp_for_loop (parser, OMP_FOR, clauses, cclauses);
33346
33347 cp_parser_end_omp_structured_block (parser, save);
33348 add_stmt (finish_omp_structured_block (sb));
33349
33350 return ret;
33351 }
33352
33353 /* OpenMP 2.5:
33354 # pragma omp master new-line
33355 structured-block */
33356
33357 static tree
33358 cp_parser_omp_master (cp_parser *parser, cp_token *pragma_tok)
33359 {
33360 cp_parser_require_pragma_eol (parser, pragma_tok);
33361 return c_finish_omp_master (input_location,
33362 cp_parser_omp_structured_block (parser));
33363 }
33364
33365 /* OpenMP 2.5:
33366 # pragma omp ordered new-line
33367 structured-block
33368
33369 OpenMP 4.5:
33370 # pragma omp ordered ordered-clauses new-line
33371 structured-block */
33372
33373 #define OMP_ORDERED_CLAUSE_MASK \
33374 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREADS) \
33375 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMD))
33376
33377 #define OMP_ORDERED_DEPEND_CLAUSE_MASK \
33378 (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND)
33379
33380 static bool
33381 cp_parser_omp_ordered (cp_parser *parser, cp_token *pragma_tok,
33382 enum pragma_context context)
33383 {
33384 location_t loc = pragma_tok->location;
33385
33386 if (context != pragma_stmt && context != pragma_compound)
33387 {
33388 cp_parser_error (parser, "expected declaration specifiers");
33389 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
33390 return false;
33391 }
33392
33393 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
33394 {
33395 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
33396 const char *p = IDENTIFIER_POINTER (id);
33397
33398 if (strcmp (p, "depend") == 0)
33399 {
33400 if (context == pragma_stmt)
33401 {
33402 error_at (pragma_tok->location, "%<#pragma omp ordered%> with "
33403 "%<depend%> clause may only be used in compound "
33404 "statements");
33405 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
33406 return false;
33407 }
33408 tree clauses
33409 = cp_parser_omp_all_clauses (parser,
33410 OMP_ORDERED_DEPEND_CLAUSE_MASK,
33411 "#pragma omp ordered", pragma_tok);
33412 c_finish_omp_ordered (loc, clauses, NULL_TREE);
33413 return false;
33414 }
33415 }
33416
33417 tree clauses
33418 = cp_parser_omp_all_clauses (parser, OMP_ORDERED_CLAUSE_MASK,
33419 "#pragma omp ordered", pragma_tok);
33420 c_finish_omp_ordered (loc, clauses,
33421 cp_parser_omp_structured_block (parser));
33422 return true;
33423 }
33424
33425 /* OpenMP 2.5:
33426
33427 section-scope:
33428 { section-sequence }
33429
33430 section-sequence:
33431 section-directive[opt] structured-block
33432 section-sequence section-directive structured-block */
33433
33434 static tree
33435 cp_parser_omp_sections_scope (cp_parser *parser)
33436 {
33437 tree stmt, substmt;
33438 bool error_suppress = false;
33439 cp_token *tok;
33440
33441 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
33442 return NULL_TREE;
33443
33444 stmt = push_stmt_list ();
33445
33446 if (cp_lexer_peek_token (parser->lexer)->pragma_kind != PRAGMA_OMP_SECTION)
33447 {
33448 substmt = cp_parser_omp_structured_block (parser);
33449 substmt = build1 (OMP_SECTION, void_type_node, substmt);
33450 add_stmt (substmt);
33451 }
33452
33453 while (1)
33454 {
33455 tok = cp_lexer_peek_token (parser->lexer);
33456 if (tok->type == CPP_CLOSE_BRACE)
33457 break;
33458 if (tok->type == CPP_EOF)
33459 break;
33460
33461 if (tok->pragma_kind == PRAGMA_OMP_SECTION)
33462 {
33463 cp_lexer_consume_token (parser->lexer);
33464 cp_parser_require_pragma_eol (parser, tok);
33465 error_suppress = false;
33466 }
33467 else if (!error_suppress)
33468 {
33469 cp_parser_error (parser, "expected %<#pragma omp section%> or %<}%>");
33470 error_suppress = true;
33471 }
33472
33473 substmt = cp_parser_omp_structured_block (parser);
33474 substmt = build1 (OMP_SECTION, void_type_node, substmt);
33475 add_stmt (substmt);
33476 }
33477 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
33478
33479 substmt = pop_stmt_list (stmt);
33480
33481 stmt = make_node (OMP_SECTIONS);
33482 TREE_TYPE (stmt) = void_type_node;
33483 OMP_SECTIONS_BODY (stmt) = substmt;
33484
33485 add_stmt (stmt);
33486 return stmt;
33487 }
33488
33489 /* OpenMP 2.5:
33490 # pragma omp sections sections-clause[optseq] newline
33491 sections-scope */
33492
33493 #define OMP_SECTIONS_CLAUSE_MASK \
33494 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
33495 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
33496 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
33497 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
33498 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
33499
33500 static tree
33501 cp_parser_omp_sections (cp_parser *parser, cp_token *pragma_tok,
33502 char *p_name, omp_clause_mask mask, tree *cclauses)
33503 {
33504 tree clauses, ret;
33505 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
33506
33507 strcat (p_name, " sections");
33508 mask |= OMP_SECTIONS_CLAUSE_MASK;
33509 if (cclauses)
33510 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT);
33511
33512 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
33513 cclauses == NULL);
33514 if (cclauses)
33515 {
33516 cp_omp_split_clauses (loc, OMP_SECTIONS, mask, clauses, cclauses);
33517 clauses = cclauses[C_OMP_CLAUSE_SPLIT_SECTIONS];
33518 }
33519
33520 ret = cp_parser_omp_sections_scope (parser);
33521 if (ret)
33522 OMP_SECTIONS_CLAUSES (ret) = clauses;
33523
33524 return ret;
33525 }
33526
33527 /* OpenMP 2.5:
33528 # pragma omp parallel parallel-clause[optseq] new-line
33529 structured-block
33530 # pragma omp parallel for parallel-for-clause[optseq] new-line
33531 structured-block
33532 # pragma omp parallel sections parallel-sections-clause[optseq] new-line
33533 structured-block
33534
33535 OpenMP 4.0:
33536 # pragma omp parallel for simd parallel-for-simd-clause[optseq] new-line
33537 structured-block */
33538
33539 #define OMP_PARALLEL_CLAUSE_MASK \
33540 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
33541 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
33542 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
33543 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
33544 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
33545 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYIN) \
33546 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
33547 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_THREADS) \
33548 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PROC_BIND))
33549
33550 static tree
33551 cp_parser_omp_parallel (cp_parser *parser, cp_token *pragma_tok,
33552 char *p_name, omp_clause_mask mask, tree *cclauses)
33553 {
33554 tree stmt, clauses, block;
33555 unsigned int save;
33556 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
33557
33558 strcat (p_name, " parallel");
33559 mask |= OMP_PARALLEL_CLAUSE_MASK;
33560 /* #pragma omp target parallel{, for, for simd} disallow copyin clause. */
33561 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP)) != 0
33562 && (mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) == 0)
33563 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYIN);
33564
33565 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
33566 {
33567 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
33568 if (cclauses == NULL)
33569 cclauses = cclauses_buf;
33570
33571 cp_lexer_consume_token (parser->lexer);
33572 if (!flag_openmp) /* flag_openmp_simd */
33573 return cp_parser_omp_for (parser, pragma_tok, p_name, mask, cclauses);
33574 block = begin_omp_parallel ();
33575 save = cp_parser_begin_omp_structured_block (parser);
33576 tree ret = cp_parser_omp_for (parser, pragma_tok, p_name, mask, cclauses);
33577 cp_parser_end_omp_structured_block (parser, save);
33578 stmt = finish_omp_parallel (cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
33579 block);
33580 if (ret == NULL_TREE)
33581 return ret;
33582 OMP_PARALLEL_COMBINED (stmt) = 1;
33583 return stmt;
33584 }
33585 /* When combined with distribute, parallel has to be followed by for.
33586 #pragma omp target parallel is allowed though. */
33587 else if (cclauses
33588 && (mask & (OMP_CLAUSE_MASK_1
33589 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
33590 {
33591 error_at (loc, "expected %<for%> after %qs", p_name);
33592 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
33593 return NULL_TREE;
33594 }
33595 else if (!flag_openmp) /* flag_openmp_simd */
33596 {
33597 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
33598 return NULL_TREE;
33599 }
33600 else if (cclauses == NULL && cp_lexer_next_token_is (parser->lexer, CPP_NAME))
33601 {
33602 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
33603 const char *p = IDENTIFIER_POINTER (id);
33604 if (strcmp (p, "sections") == 0)
33605 {
33606 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
33607 cclauses = cclauses_buf;
33608
33609 cp_lexer_consume_token (parser->lexer);
33610 block = begin_omp_parallel ();
33611 save = cp_parser_begin_omp_structured_block (parser);
33612 cp_parser_omp_sections (parser, pragma_tok, p_name, mask, cclauses);
33613 cp_parser_end_omp_structured_block (parser, save);
33614 stmt = finish_omp_parallel (cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
33615 block);
33616 OMP_PARALLEL_COMBINED (stmt) = 1;
33617 return stmt;
33618 }
33619 }
33620
33621 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok);
33622 if (cclauses)
33623 {
33624 cp_omp_split_clauses (loc, OMP_PARALLEL, mask, clauses, cclauses);
33625 clauses = cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL];
33626 }
33627
33628 block = begin_omp_parallel ();
33629 save = cp_parser_begin_omp_structured_block (parser);
33630 cp_parser_statement (parser, NULL_TREE, false, NULL);
33631 cp_parser_end_omp_structured_block (parser, save);
33632 stmt = finish_omp_parallel (clauses, block);
33633 return stmt;
33634 }
33635
33636 /* OpenMP 2.5:
33637 # pragma omp single single-clause[optseq] new-line
33638 structured-block */
33639
33640 #define OMP_SINGLE_CLAUSE_MASK \
33641 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
33642 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
33643 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \
33644 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
33645
33646 static tree
33647 cp_parser_omp_single (cp_parser *parser, cp_token *pragma_tok)
33648 {
33649 tree stmt = make_node (OMP_SINGLE);
33650 TREE_TYPE (stmt) = void_type_node;
33651
33652 OMP_SINGLE_CLAUSES (stmt)
33653 = cp_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
33654 "#pragma omp single", pragma_tok);
33655 OMP_SINGLE_BODY (stmt) = cp_parser_omp_structured_block (parser);
33656
33657 return add_stmt (stmt);
33658 }
33659
33660 /* OpenMP 3.0:
33661 # pragma omp task task-clause[optseq] new-line
33662 structured-block */
33663
33664 #define OMP_TASK_CLAUSE_MASK \
33665 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
33666 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED) \
33667 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
33668 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
33669 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
33670 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
33671 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL) \
33672 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE) \
33673 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
33674 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIORITY))
33675
33676 static tree
33677 cp_parser_omp_task (cp_parser *parser, cp_token *pragma_tok)
33678 {
33679 tree clauses, block;
33680 unsigned int save;
33681
33682 clauses = cp_parser_omp_all_clauses (parser, OMP_TASK_CLAUSE_MASK,
33683 "#pragma omp task", pragma_tok);
33684 block = begin_omp_task ();
33685 save = cp_parser_begin_omp_structured_block (parser);
33686 cp_parser_statement (parser, NULL_TREE, false, NULL);
33687 cp_parser_end_omp_structured_block (parser, save);
33688 return finish_omp_task (clauses, block);
33689 }
33690
33691 /* OpenMP 3.0:
33692 # pragma omp taskwait new-line */
33693
33694 static void
33695 cp_parser_omp_taskwait (cp_parser *parser, cp_token *pragma_tok)
33696 {
33697 cp_parser_require_pragma_eol (parser, pragma_tok);
33698 finish_omp_taskwait ();
33699 }
33700
33701 /* OpenMP 3.1:
33702 # pragma omp taskyield new-line */
33703
33704 static void
33705 cp_parser_omp_taskyield (cp_parser *parser, cp_token *pragma_tok)
33706 {
33707 cp_parser_require_pragma_eol (parser, pragma_tok);
33708 finish_omp_taskyield ();
33709 }
33710
33711 /* OpenMP 4.0:
33712 # pragma omp taskgroup new-line
33713 structured-block */
33714
33715 static tree
33716 cp_parser_omp_taskgroup (cp_parser *parser, cp_token *pragma_tok)
33717 {
33718 cp_parser_require_pragma_eol (parser, pragma_tok);
33719 return c_finish_omp_taskgroup (input_location,
33720 cp_parser_omp_structured_block (parser));
33721 }
33722
33723
33724 /* OpenMP 2.5:
33725 # pragma omp threadprivate (variable-list) */
33726
33727 static void
33728 cp_parser_omp_threadprivate (cp_parser *parser, cp_token *pragma_tok)
33729 {
33730 tree vars;
33731
33732 vars = cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
33733 cp_parser_require_pragma_eol (parser, pragma_tok);
33734
33735 finish_omp_threadprivate (vars);
33736 }
33737
33738 /* OpenMP 4.0:
33739 # pragma omp cancel cancel-clause[optseq] new-line */
33740
33741 #define OMP_CANCEL_CLAUSE_MASK \
33742 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
33743 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
33744 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
33745 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP) \
33746 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
33747
33748 static void
33749 cp_parser_omp_cancel (cp_parser *parser, cp_token *pragma_tok)
33750 {
33751 tree clauses = cp_parser_omp_all_clauses (parser, OMP_CANCEL_CLAUSE_MASK,
33752 "#pragma omp cancel", pragma_tok);
33753 finish_omp_cancel (clauses);
33754 }
33755
33756 /* OpenMP 4.0:
33757 # pragma omp cancellation point cancelpt-clause[optseq] new-line */
33758
33759 #define OMP_CANCELLATION_POINT_CLAUSE_MASK \
33760 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
33761 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
33762 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
33763 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP))
33764
33765 static void
33766 cp_parser_omp_cancellation_point (cp_parser *parser, cp_token *pragma_tok)
33767 {
33768 tree clauses;
33769 bool point_seen = false;
33770
33771 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
33772 {
33773 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
33774 const char *p = IDENTIFIER_POINTER (id);
33775
33776 if (strcmp (p, "point") == 0)
33777 {
33778 cp_lexer_consume_token (parser->lexer);
33779 point_seen = true;
33780 }
33781 }
33782 if (!point_seen)
33783 {
33784 cp_parser_error (parser, "expected %<point%>");
33785 cp_parser_require_pragma_eol (parser, pragma_tok);
33786 return;
33787 }
33788
33789 clauses = cp_parser_omp_all_clauses (parser,
33790 OMP_CANCELLATION_POINT_CLAUSE_MASK,
33791 "#pragma omp cancellation point",
33792 pragma_tok);
33793 finish_omp_cancellation_point (clauses);
33794 }
33795
33796 /* OpenMP 4.0:
33797 #pragma omp distribute distribute-clause[optseq] new-line
33798 for-loop */
33799
33800 #define OMP_DISTRIBUTE_CLAUSE_MASK \
33801 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
33802 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
33803 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
33804 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)\
33805 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
33806
33807 static tree
33808 cp_parser_omp_distribute (cp_parser *parser, cp_token *pragma_tok,
33809 char *p_name, omp_clause_mask mask, tree *cclauses)
33810 {
33811 tree clauses, sb, ret;
33812 unsigned int save;
33813 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
33814
33815 strcat (p_name, " distribute");
33816 mask |= OMP_DISTRIBUTE_CLAUSE_MASK;
33817
33818 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
33819 {
33820 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
33821 const char *p = IDENTIFIER_POINTER (id);
33822 bool simd = false;
33823 bool parallel = false;
33824
33825 if (strcmp (p, "simd") == 0)
33826 simd = true;
33827 else
33828 parallel = strcmp (p, "parallel") == 0;
33829 if (parallel || simd)
33830 {
33831 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
33832 if (cclauses == NULL)
33833 cclauses = cclauses_buf;
33834 cp_lexer_consume_token (parser->lexer);
33835 if (!flag_openmp) /* flag_openmp_simd */
33836 {
33837 if (simd)
33838 return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
33839 cclauses);
33840 else
33841 return cp_parser_omp_parallel (parser, pragma_tok, p_name, mask,
33842 cclauses);
33843 }
33844 sb = begin_omp_structured_block ();
33845 save = cp_parser_begin_omp_structured_block (parser);
33846 if (simd)
33847 ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
33848 cclauses);
33849 else
33850 ret = cp_parser_omp_parallel (parser, pragma_tok, p_name, mask,
33851 cclauses);
33852 cp_parser_end_omp_structured_block (parser, save);
33853 tree body = finish_omp_structured_block (sb);
33854 if (ret == NULL)
33855 return ret;
33856 ret = make_node (OMP_DISTRIBUTE);
33857 TREE_TYPE (ret) = void_type_node;
33858 OMP_FOR_BODY (ret) = body;
33859 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_DISTRIBUTE];
33860 SET_EXPR_LOCATION (ret, loc);
33861 add_stmt (ret);
33862 return ret;
33863 }
33864 }
33865 if (!flag_openmp) /* flag_openmp_simd */
33866 {
33867 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
33868 return NULL_TREE;
33869 }
33870
33871 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
33872 cclauses == NULL);
33873 if (cclauses)
33874 {
33875 cp_omp_split_clauses (loc, OMP_DISTRIBUTE, mask, clauses, cclauses);
33876 clauses = cclauses[C_OMP_CLAUSE_SPLIT_DISTRIBUTE];
33877 }
33878
33879 sb = begin_omp_structured_block ();
33880 save = cp_parser_begin_omp_structured_block (parser);
33881
33882 ret = cp_parser_omp_for_loop (parser, OMP_DISTRIBUTE, clauses, NULL);
33883
33884 cp_parser_end_omp_structured_block (parser, save);
33885 add_stmt (finish_omp_structured_block (sb));
33886
33887 return ret;
33888 }
33889
33890 /* OpenMP 4.0:
33891 # pragma omp teams teams-clause[optseq] new-line
33892 structured-block */
33893
33894 #define OMP_TEAMS_CLAUSE_MASK \
33895 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
33896 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
33897 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
33898 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
33899 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TEAMS) \
33900 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREAD_LIMIT) \
33901 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT))
33902
33903 static tree
33904 cp_parser_omp_teams (cp_parser *parser, cp_token *pragma_tok,
33905 char *p_name, omp_clause_mask mask, tree *cclauses)
33906 {
33907 tree clauses, sb, ret;
33908 unsigned int save;
33909 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
33910
33911 strcat (p_name, " teams");
33912 mask |= OMP_TEAMS_CLAUSE_MASK;
33913
33914 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
33915 {
33916 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
33917 const char *p = IDENTIFIER_POINTER (id);
33918 if (strcmp (p, "distribute") == 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_distribute (parser, pragma_tok, p_name, mask,
33927 cclauses);
33928 sb = begin_omp_structured_block ();
33929 save = cp_parser_begin_omp_structured_block (parser);
33930 ret = cp_parser_omp_distribute (parser, pragma_tok, p_name, mask,
33931 cclauses);
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 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
33937 ret = make_node (OMP_TEAMS);
33938 TREE_TYPE (ret) = void_type_node;
33939 OMP_TEAMS_CLAUSES (ret) = clauses;
33940 OMP_TEAMS_BODY (ret) = body;
33941 OMP_TEAMS_COMBINED (ret) = 1;
33942 return add_stmt (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 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
33952 cclauses == NULL);
33953 if (cclauses)
33954 {
33955 cp_omp_split_clauses (loc, OMP_TEAMS, mask, clauses, cclauses);
33956 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
33957 }
33958
33959 tree stmt = make_node (OMP_TEAMS);
33960 TREE_TYPE (stmt) = void_type_node;
33961 OMP_TEAMS_CLAUSES (stmt) = clauses;
33962 OMP_TEAMS_BODY (stmt) = cp_parser_omp_structured_block (parser);
33963
33964 return add_stmt (stmt);
33965 }
33966
33967 /* OpenMP 4.0:
33968 # pragma omp target data target-data-clause[optseq] new-line
33969 structured-block */
33970
33971 #define OMP_TARGET_DATA_CLAUSE_MASK \
33972 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
33973 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
33974 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
33975 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR))
33976
33977 static tree
33978 cp_parser_omp_target_data (cp_parser *parser, cp_token *pragma_tok)
33979 {
33980 tree clauses
33981 = cp_parser_omp_all_clauses (parser, OMP_TARGET_DATA_CLAUSE_MASK,
33982 "#pragma omp target data", pragma_tok);
33983 int map_seen = 0;
33984 for (tree *pc = &clauses; *pc;)
33985 {
33986 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
33987 switch (OMP_CLAUSE_MAP_KIND (*pc))
33988 {
33989 case GOMP_MAP_TO:
33990 case GOMP_MAP_ALWAYS_TO:
33991 case GOMP_MAP_FROM:
33992 case GOMP_MAP_ALWAYS_FROM:
33993 case GOMP_MAP_TOFROM:
33994 case GOMP_MAP_ALWAYS_TOFROM:
33995 case GOMP_MAP_ALLOC:
33996 map_seen = 3;
33997 break;
33998 case GOMP_MAP_FIRSTPRIVATE_POINTER:
33999 case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
34000 case GOMP_MAP_ALWAYS_POINTER:
34001 break;
34002 default:
34003 map_seen |= 1;
34004 error_at (OMP_CLAUSE_LOCATION (*pc),
34005 "%<#pragma omp target data%> with map-type other "
34006 "than %<to%>, %<from%>, %<tofrom%> or %<alloc%> "
34007 "on %<map%> clause");
34008 *pc = OMP_CLAUSE_CHAIN (*pc);
34009 continue;
34010 }
34011 pc = &OMP_CLAUSE_CHAIN (*pc);
34012 }
34013
34014 if (map_seen != 3)
34015 {
34016 if (map_seen == 0)
34017 error_at (pragma_tok->location,
34018 "%<#pragma omp target data%> must contain at least "
34019 "one %<map%> clause");
34020 return NULL_TREE;
34021 }
34022
34023 tree stmt = make_node (OMP_TARGET_DATA);
34024 TREE_TYPE (stmt) = void_type_node;
34025 OMP_TARGET_DATA_CLAUSES (stmt) = clauses;
34026
34027 keep_next_level (true);
34028 OMP_TARGET_DATA_BODY (stmt) = cp_parser_omp_structured_block (parser);
34029
34030 SET_EXPR_LOCATION (stmt, pragma_tok->location);
34031 return add_stmt (stmt);
34032 }
34033
34034 /* OpenMP 4.5:
34035 # pragma omp target enter data target-enter-data-clause[optseq] new-line
34036 structured-block */
34037
34038 #define OMP_TARGET_ENTER_DATA_CLAUSE_MASK \
34039 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
34040 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
34041 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
34042 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
34043 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
34044
34045 static tree
34046 cp_parser_omp_target_enter_data (cp_parser *parser, cp_token *pragma_tok,
34047 enum pragma_context context)
34048 {
34049 bool data_seen = false;
34050 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
34051 {
34052 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
34053 const char *p = IDENTIFIER_POINTER (id);
34054
34055 if (strcmp (p, "data") == 0)
34056 {
34057 cp_lexer_consume_token (parser->lexer);
34058 data_seen = true;
34059 }
34060 }
34061 if (!data_seen)
34062 {
34063 cp_parser_error (parser, "expected %<data%>");
34064 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
34065 return NULL_TREE;
34066 }
34067
34068 if (context == pragma_stmt)
34069 {
34070 error_at (pragma_tok->location,
34071 "%<#pragma omp target enter data%> may only be "
34072 "used in compound statements");
34073 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
34074 return NULL_TREE;
34075 }
34076
34077 tree clauses
34078 = cp_parser_omp_all_clauses (parser, OMP_TARGET_ENTER_DATA_CLAUSE_MASK,
34079 "#pragma omp target enter data", pragma_tok);
34080 int map_seen = 0;
34081 for (tree *pc = &clauses; *pc;)
34082 {
34083 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
34084 switch (OMP_CLAUSE_MAP_KIND (*pc))
34085 {
34086 case GOMP_MAP_TO:
34087 case GOMP_MAP_ALWAYS_TO:
34088 case GOMP_MAP_ALLOC:
34089 map_seen = 3;
34090 break;
34091 case GOMP_MAP_FIRSTPRIVATE_POINTER:
34092 case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
34093 case GOMP_MAP_ALWAYS_POINTER:
34094 break;
34095 default:
34096 map_seen |= 1;
34097 error_at (OMP_CLAUSE_LOCATION (*pc),
34098 "%<#pragma omp target enter data%> with map-type other "
34099 "than %<to%> or %<alloc%> on %<map%> clause");
34100 *pc = OMP_CLAUSE_CHAIN (*pc);
34101 continue;
34102 }
34103 pc = &OMP_CLAUSE_CHAIN (*pc);
34104 }
34105
34106 if (map_seen != 3)
34107 {
34108 if (map_seen == 0)
34109 error_at (pragma_tok->location,
34110 "%<#pragma omp target enter data%> must contain at least "
34111 "one %<map%> clause");
34112 return NULL_TREE;
34113 }
34114
34115 tree stmt = make_node (OMP_TARGET_ENTER_DATA);
34116 TREE_TYPE (stmt) = void_type_node;
34117 OMP_TARGET_ENTER_DATA_CLAUSES (stmt) = clauses;
34118 SET_EXPR_LOCATION (stmt, pragma_tok->location);
34119 return add_stmt (stmt);
34120 }
34121
34122 /* OpenMP 4.5:
34123 # pragma omp target exit data target-enter-data-clause[optseq] new-line
34124 structured-block */
34125
34126 #define OMP_TARGET_EXIT_DATA_CLAUSE_MASK \
34127 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
34128 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
34129 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
34130 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
34131 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
34132
34133 static tree
34134 cp_parser_omp_target_exit_data (cp_parser *parser, cp_token *pragma_tok,
34135 enum pragma_context context)
34136 {
34137 bool data_seen = false;
34138 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
34139 {
34140 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
34141 const char *p = IDENTIFIER_POINTER (id);
34142
34143 if (strcmp (p, "data") == 0)
34144 {
34145 cp_lexer_consume_token (parser->lexer);
34146 data_seen = true;
34147 }
34148 }
34149 if (!data_seen)
34150 {
34151 cp_parser_error (parser, "expected %<data%>");
34152 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
34153 return NULL_TREE;
34154 }
34155
34156 if (context == pragma_stmt)
34157 {
34158 error_at (pragma_tok->location,
34159 "%<#pragma omp target exit data%> may only be "
34160 "used in compound statements");
34161 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
34162 return NULL_TREE;
34163 }
34164
34165 tree clauses
34166 = cp_parser_omp_all_clauses (parser, OMP_TARGET_EXIT_DATA_CLAUSE_MASK,
34167 "#pragma omp target exit data", pragma_tok);
34168 int map_seen = 0;
34169 for (tree *pc = &clauses; *pc;)
34170 {
34171 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
34172 switch (OMP_CLAUSE_MAP_KIND (*pc))
34173 {
34174 case GOMP_MAP_FROM:
34175 case GOMP_MAP_ALWAYS_FROM:
34176 case GOMP_MAP_RELEASE:
34177 case GOMP_MAP_DELETE:
34178 map_seen = 3;
34179 break;
34180 case GOMP_MAP_FIRSTPRIVATE_POINTER:
34181 case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
34182 case GOMP_MAP_ALWAYS_POINTER:
34183 break;
34184 default:
34185 map_seen |= 1;
34186 error_at (OMP_CLAUSE_LOCATION (*pc),
34187 "%<#pragma omp target exit data%> with map-type other "
34188 "than %<from%>, %<release%> or %<delete%> on %<map%>"
34189 " clause");
34190 *pc = OMP_CLAUSE_CHAIN (*pc);
34191 continue;
34192 }
34193 pc = &OMP_CLAUSE_CHAIN (*pc);
34194 }
34195
34196 if (map_seen != 3)
34197 {
34198 if (map_seen == 0)
34199 error_at (pragma_tok->location,
34200 "%<#pragma omp target exit data%> must contain at least "
34201 "one %<map%> clause");
34202 return NULL_TREE;
34203 }
34204
34205 tree stmt = make_node (OMP_TARGET_EXIT_DATA);
34206 TREE_TYPE (stmt) = void_type_node;
34207 OMP_TARGET_EXIT_DATA_CLAUSES (stmt) = clauses;
34208 SET_EXPR_LOCATION (stmt, pragma_tok->location);
34209 return add_stmt (stmt);
34210 }
34211
34212 /* OpenMP 4.0:
34213 # pragma omp target update target-update-clause[optseq] new-line */
34214
34215 #define OMP_TARGET_UPDATE_CLAUSE_MASK \
34216 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FROM) \
34217 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO) \
34218 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
34219 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
34220 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
34221 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
34222
34223 static bool
34224 cp_parser_omp_target_update (cp_parser *parser, cp_token *pragma_tok,
34225 enum pragma_context context)
34226 {
34227 if (context == pragma_stmt)
34228 {
34229 error_at (pragma_tok->location,
34230 "%<#pragma omp target update%> may only be "
34231 "used in compound statements");
34232 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
34233 return false;
34234 }
34235
34236 tree clauses
34237 = cp_parser_omp_all_clauses (parser, OMP_TARGET_UPDATE_CLAUSE_MASK,
34238 "#pragma omp target update", pragma_tok);
34239 if (find_omp_clause (clauses, OMP_CLAUSE_TO) == NULL_TREE
34240 && find_omp_clause (clauses, OMP_CLAUSE_FROM) == NULL_TREE)
34241 {
34242 error_at (pragma_tok->location,
34243 "%<#pragma omp target update%> must contain at least one "
34244 "%<from%> or %<to%> clauses");
34245 return false;
34246 }
34247
34248 tree stmt = make_node (OMP_TARGET_UPDATE);
34249 TREE_TYPE (stmt) = void_type_node;
34250 OMP_TARGET_UPDATE_CLAUSES (stmt) = clauses;
34251 SET_EXPR_LOCATION (stmt, pragma_tok->location);
34252 add_stmt (stmt);
34253 return false;
34254 }
34255
34256 /* OpenMP 4.0:
34257 # pragma omp target target-clause[optseq] new-line
34258 structured-block */
34259
34260 #define OMP_TARGET_CLAUSE_MASK \
34261 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
34262 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
34263 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
34264 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
34265 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT) \
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_DEFAULTMAP) \
34269 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR))
34270
34271 static bool
34272 cp_parser_omp_target (cp_parser *parser, cp_token *pragma_tok,
34273 enum pragma_context context)
34274 {
34275 tree *pc = NULL, stmt;
34276
34277 if (context != pragma_stmt && context != pragma_compound)
34278 {
34279 cp_parser_error (parser, "expected declaration specifiers");
34280 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
34281 return false;
34282 }
34283
34284 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
34285 {
34286 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
34287 const char *p = IDENTIFIER_POINTER (id);
34288 enum tree_code ccode = ERROR_MARK;
34289
34290 if (strcmp (p, "teams") == 0)
34291 ccode = OMP_TEAMS;
34292 else if (strcmp (p, "parallel") == 0)
34293 ccode = OMP_PARALLEL;
34294 else if (strcmp (p, "simd") == 0)
34295 ccode = OMP_SIMD;
34296 if (ccode != ERROR_MARK)
34297 {
34298 tree cclauses[C_OMP_CLAUSE_SPLIT_COUNT];
34299 char p_name[sizeof ("#pragma omp target teams distribute "
34300 "parallel for simd")];
34301
34302 cp_lexer_consume_token (parser->lexer);
34303 strcpy (p_name, "#pragma omp target");
34304 if (!flag_openmp) /* flag_openmp_simd */
34305 {
34306 tree stmt;
34307 switch (ccode)
34308 {
34309 case OMP_TEAMS:
34310 stmt = cp_parser_omp_teams (parser, pragma_tok, p_name,
34311 OMP_TARGET_CLAUSE_MASK,
34312 cclauses);
34313 break;
34314 case OMP_PARALLEL:
34315 stmt = cp_parser_omp_parallel (parser, pragma_tok, p_name,
34316 OMP_TARGET_CLAUSE_MASK,
34317 cclauses);
34318 break;
34319 case OMP_SIMD:
34320 stmt = cp_parser_omp_simd (parser, pragma_tok, p_name,
34321 OMP_TARGET_CLAUSE_MASK,
34322 cclauses);
34323 break;
34324 default:
34325 gcc_unreachable ();
34326 }
34327 return stmt != NULL_TREE;
34328 }
34329 keep_next_level (true);
34330 tree sb = begin_omp_structured_block (), ret;
34331 unsigned save = cp_parser_begin_omp_structured_block (parser);
34332 switch (ccode)
34333 {
34334 case OMP_TEAMS:
34335 ret = cp_parser_omp_teams (parser, pragma_tok, p_name,
34336 OMP_TARGET_CLAUSE_MASK, cclauses);
34337 break;
34338 case OMP_PARALLEL:
34339 ret = cp_parser_omp_parallel (parser, pragma_tok, p_name,
34340 OMP_TARGET_CLAUSE_MASK, cclauses);
34341 break;
34342 case OMP_SIMD:
34343 ret = cp_parser_omp_simd (parser, pragma_tok, p_name,
34344 OMP_TARGET_CLAUSE_MASK, cclauses);
34345 break;
34346 default:
34347 gcc_unreachable ();
34348 }
34349 cp_parser_end_omp_structured_block (parser, save);
34350 tree body = finish_omp_structured_block (sb);
34351 if (ret == NULL_TREE)
34352 return false;
34353 if (ccode == OMP_TEAMS && !processing_template_decl)
34354 {
34355 /* For combined target teams, ensure the num_teams and
34356 thread_limit clause expressions are evaluated on the host,
34357 before entering the target construct. */
34358 tree c;
34359 for (c = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
34360 c; c = OMP_CLAUSE_CHAIN (c))
34361 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_NUM_TEAMS
34362 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_THREAD_LIMIT)
34363 && TREE_CODE (OMP_CLAUSE_OPERAND (c, 0)) != INTEGER_CST)
34364 {
34365 tree expr = OMP_CLAUSE_OPERAND (c, 0);
34366 expr = force_target_expr (TREE_TYPE (expr), expr, tf_none);
34367 if (expr == error_mark_node)
34368 continue;
34369 tree tmp = TARGET_EXPR_SLOT (expr);
34370 add_stmt (expr);
34371 OMP_CLAUSE_OPERAND (c, 0) = expr;
34372 tree tc = build_omp_clause (OMP_CLAUSE_LOCATION (c),
34373 OMP_CLAUSE_FIRSTPRIVATE);
34374 OMP_CLAUSE_DECL (tc) = tmp;
34375 OMP_CLAUSE_CHAIN (tc)
34376 = cclauses[C_OMP_CLAUSE_SPLIT_TARGET];
34377 cclauses[C_OMP_CLAUSE_SPLIT_TARGET] = tc;
34378 }
34379 }
34380 tree stmt = make_node (OMP_TARGET);
34381 TREE_TYPE (stmt) = void_type_node;
34382 OMP_TARGET_CLAUSES (stmt) = cclauses[C_OMP_CLAUSE_SPLIT_TARGET];
34383 OMP_TARGET_BODY (stmt) = body;
34384 OMP_TARGET_COMBINED (stmt) = 1;
34385 add_stmt (stmt);
34386 pc = &OMP_TARGET_CLAUSES (stmt);
34387 goto check_clauses;
34388 }
34389 else if (!flag_openmp) /* flag_openmp_simd */
34390 {
34391 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
34392 return false;
34393 }
34394 else if (strcmp (p, "data") == 0)
34395 {
34396 cp_lexer_consume_token (parser->lexer);
34397 cp_parser_omp_target_data (parser, pragma_tok);
34398 return true;
34399 }
34400 else if (strcmp (p, "enter") == 0)
34401 {
34402 cp_lexer_consume_token (parser->lexer);
34403 cp_parser_omp_target_enter_data (parser, pragma_tok, context);
34404 return false;
34405 }
34406 else if (strcmp (p, "exit") == 0)
34407 {
34408 cp_lexer_consume_token (parser->lexer);
34409 cp_parser_omp_target_exit_data (parser, pragma_tok, context);
34410 return false;
34411 }
34412 else if (strcmp (p, "update") == 0)
34413 {
34414 cp_lexer_consume_token (parser->lexer);
34415 return cp_parser_omp_target_update (parser, pragma_tok, context);
34416 }
34417 }
34418
34419 stmt = make_node (OMP_TARGET);
34420 TREE_TYPE (stmt) = void_type_node;
34421
34422 OMP_TARGET_CLAUSES (stmt)
34423 = cp_parser_omp_all_clauses (parser, OMP_TARGET_CLAUSE_MASK,
34424 "#pragma omp target", pragma_tok);
34425 pc = &OMP_TARGET_CLAUSES (stmt);
34426 keep_next_level (true);
34427 OMP_TARGET_BODY (stmt) = cp_parser_omp_structured_block (parser);
34428
34429 SET_EXPR_LOCATION (stmt, pragma_tok->location);
34430 add_stmt (stmt);
34431
34432 check_clauses:
34433 while (*pc)
34434 {
34435 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
34436 switch (OMP_CLAUSE_MAP_KIND (*pc))
34437 {
34438 case GOMP_MAP_TO:
34439 case GOMP_MAP_ALWAYS_TO:
34440 case GOMP_MAP_FROM:
34441 case GOMP_MAP_ALWAYS_FROM:
34442 case GOMP_MAP_TOFROM:
34443 case GOMP_MAP_ALWAYS_TOFROM:
34444 case GOMP_MAP_ALLOC:
34445 case GOMP_MAP_FIRSTPRIVATE_POINTER:
34446 case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
34447 case GOMP_MAP_ALWAYS_POINTER:
34448 break;
34449 default:
34450 error_at (OMP_CLAUSE_LOCATION (*pc),
34451 "%<#pragma omp target%> with map-type other "
34452 "than %<to%>, %<from%>, %<tofrom%> or %<alloc%> "
34453 "on %<map%> clause");
34454 *pc = OMP_CLAUSE_CHAIN (*pc);
34455 continue;
34456 }
34457 pc = &OMP_CLAUSE_CHAIN (*pc);
34458 }
34459 return true;
34460 }
34461
34462 /* OpenACC 2.0:
34463 # pragma acc cache (variable-list) new-line
34464 */
34465
34466 static tree
34467 cp_parser_oacc_cache (cp_parser *parser, cp_token *pragma_tok)
34468 {
34469 tree stmt, clauses;
34470
34471 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE__CACHE_, NULL_TREE);
34472 clauses = finish_omp_clauses (clauses, false);
34473
34474 cp_parser_require_pragma_eol (parser, cp_lexer_peek_token (parser->lexer));
34475
34476 stmt = make_node (OACC_CACHE);
34477 TREE_TYPE (stmt) = void_type_node;
34478 OACC_CACHE_CLAUSES (stmt) = clauses;
34479 SET_EXPR_LOCATION (stmt, pragma_tok->location);
34480 add_stmt (stmt);
34481
34482 return stmt;
34483 }
34484
34485 /* OpenACC 2.0:
34486 # pragma acc data oacc-data-clause[optseq] new-line
34487 structured-block */
34488
34489 #define OACC_DATA_CLAUSE_MASK \
34490 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
34491 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
34492 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
34493 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
34494 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
34495 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
34496 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
34497 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
34498 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
34499 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
34500 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE))
34501
34502 static tree
34503 cp_parser_oacc_data (cp_parser *parser, cp_token *pragma_tok)
34504 {
34505 tree stmt, clauses, block;
34506 unsigned int save;
34507
34508 clauses = cp_parser_oacc_all_clauses (parser, OACC_DATA_CLAUSE_MASK,
34509 "#pragma acc data", pragma_tok);
34510
34511 block = begin_omp_parallel ();
34512 save = cp_parser_begin_omp_structured_block (parser);
34513 cp_parser_statement (parser, NULL_TREE, false, NULL);
34514 cp_parser_end_omp_structured_block (parser, save);
34515 stmt = finish_oacc_data (clauses, block);
34516 return stmt;
34517 }
34518
34519 /* OpenACC 2.0:
34520 # pragma acc host_data <clauses> new-line
34521 structured-block */
34522
34523 #define OACC_HOST_DATA_CLAUSE_MASK \
34524 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_USE_DEVICE) )
34525
34526 static tree
34527 cp_parser_oacc_host_data (cp_parser *parser, cp_token *pragma_tok)
34528 {
34529 tree stmt, clauses, block;
34530 unsigned int save;
34531
34532 clauses = cp_parser_oacc_all_clauses (parser, OACC_HOST_DATA_CLAUSE_MASK,
34533 "#pragma acc host_data", pragma_tok);
34534
34535 block = begin_omp_parallel ();
34536 save = cp_parser_begin_omp_structured_block (parser);
34537 cp_parser_statement (parser, NULL_TREE, false, NULL);
34538 cp_parser_end_omp_structured_block (parser, save);
34539 stmt = finish_oacc_host_data (clauses, block);
34540 return stmt;
34541 }
34542
34543 /* OpenACC 2.0:
34544 # pragma acc declare oacc-data-clause[optseq] new-line
34545 */
34546
34547 #define OACC_DECLARE_CLAUSE_MASK \
34548 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
34549 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
34550 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
34551 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
34552 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
34553 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT) \
34554 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_LINK) \
34555 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
34556 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
34557 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
34558 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
34559 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE))
34560
34561 static tree
34562 cp_parser_oacc_declare (cp_parser *parser, cp_token *pragma_tok)
34563 {
34564 tree clauses, stmt;
34565 bool error = false;
34566
34567 clauses = cp_parser_oacc_all_clauses (parser, OACC_DECLARE_CLAUSE_MASK,
34568 "#pragma acc declare", pragma_tok, true);
34569
34570
34571 if (find_omp_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
34572 {
34573 error_at (pragma_tok->location,
34574 "no valid clauses specified in %<#pragma acc declare%>");
34575 return NULL_TREE;
34576 }
34577
34578 for (tree t = clauses; t; t = OMP_CLAUSE_CHAIN (t))
34579 {
34580 location_t loc = OMP_CLAUSE_LOCATION (t);
34581 tree decl = OMP_CLAUSE_DECL (t);
34582 if (!DECL_P (decl))
34583 {
34584 error_at (loc, "array section in %<#pragma acc declare%>");
34585 error = true;
34586 continue;
34587 }
34588 gcc_assert (OMP_CLAUSE_CODE (t) == OMP_CLAUSE_MAP);
34589 switch (OMP_CLAUSE_MAP_KIND (t))
34590 {
34591 case GOMP_MAP_FORCE_ALLOC:
34592 case GOMP_MAP_FORCE_TO:
34593 case GOMP_MAP_FORCE_DEVICEPTR:
34594 case GOMP_MAP_DEVICE_RESIDENT:
34595 break;
34596
34597 case GOMP_MAP_POINTER:
34598 /* Generated by c_finish_omp_clauses from array sections;
34599 avoid spurious diagnostics. */
34600 break;
34601
34602 case GOMP_MAP_LINK:
34603 if (!global_bindings_p ()
34604 && (TREE_STATIC (decl)
34605 || !DECL_EXTERNAL (decl)))
34606 {
34607 error_at (loc,
34608 "%qD must be a global variable in"
34609 "%<#pragma acc declare link%>",
34610 decl);
34611 error = true;
34612 continue;
34613 }
34614 break;
34615
34616 default:
34617 if (global_bindings_p ())
34618 {
34619 error_at (loc, "invalid OpenACC clause at file scope");
34620 error = true;
34621 continue;
34622 }
34623 if (DECL_EXTERNAL (decl))
34624 {
34625 error_at (loc,
34626 "invalid use of %<extern%> variable %qD "
34627 "in %<#pragma acc declare%>", decl);
34628 error = true;
34629 continue;
34630 }
34631 else if (TREE_PUBLIC (decl))
34632 {
34633 error_at (loc,
34634 "invalid use of %<global%> variable %qD "
34635 "in %<#pragma acc declare%>", decl);
34636 error = true;
34637 continue;
34638 }
34639 break;
34640 }
34641
34642 if (lookup_attribute ("omp declare target", DECL_ATTRIBUTES (decl))
34643 || lookup_attribute ("omp declare target link",
34644 DECL_ATTRIBUTES (decl)))
34645 {
34646 error_at (loc, "variable %qD used more than once with "
34647 "%<#pragma acc declare%>", decl);
34648 error = true;
34649 continue;
34650 }
34651
34652 if (!error)
34653 {
34654 tree id;
34655
34656 if (OMP_CLAUSE_MAP_KIND (t) == GOMP_MAP_LINK)
34657 id = get_identifier ("omp declare target link");
34658 else
34659 id = get_identifier ("omp declare target");
34660
34661 DECL_ATTRIBUTES (decl)
34662 = tree_cons (id, NULL_TREE, DECL_ATTRIBUTES (decl));
34663 if (global_bindings_p ())
34664 {
34665 symtab_node *node = symtab_node::get (decl);
34666 if (node != NULL)
34667 {
34668 node->offloadable = 1;
34669 if (ENABLE_OFFLOADING)
34670 {
34671 g->have_offload = true;
34672 if (is_a <varpool_node *> (node))
34673 {
34674 vec_safe_push (offload_vars, decl);
34675 node->force_output = 1;
34676 }
34677 }
34678 }
34679 }
34680 }
34681 }
34682
34683 if (error || global_bindings_p ())
34684 return NULL_TREE;
34685
34686 stmt = make_node (OACC_DECLARE);
34687 TREE_TYPE (stmt) = void_type_node;
34688 OACC_DECLARE_CLAUSES (stmt) = clauses;
34689 SET_EXPR_LOCATION (stmt, pragma_tok->location);
34690
34691 add_stmt (stmt);
34692
34693 return NULL_TREE;
34694 }
34695
34696 /* OpenACC 2.0:
34697 # pragma acc enter data oacc-enter-data-clause[optseq] new-line
34698
34699 or
34700
34701 # pragma acc exit data oacc-exit-data-clause[optseq] new-line
34702
34703 LOC is the location of the #pragma token.
34704 */
34705
34706 #define OACC_ENTER_DATA_CLAUSE_MASK \
34707 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
34708 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
34709 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
34710 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
34711 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
34712 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE) \
34713 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
34714
34715 #define OACC_EXIT_DATA_CLAUSE_MASK \
34716 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
34717 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
34718 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
34719 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DELETE) \
34720 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
34721
34722 static tree
34723 cp_parser_oacc_enter_exit_data (cp_parser *parser, cp_token *pragma_tok,
34724 bool enter)
34725 {
34726 tree stmt, clauses;
34727
34728 if (cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA_EOL)
34729 || cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
34730 {
34731 cp_parser_error (parser, enter
34732 ? "expected %<data%> in %<#pragma acc enter data%>"
34733 : "expected %<data%> in %<#pragma acc exit data%>");
34734 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
34735 return NULL_TREE;
34736 }
34737
34738 const char *p =
34739 IDENTIFIER_POINTER (cp_lexer_peek_token (parser->lexer)->u.value);
34740 if (strcmp (p, "data") != 0)
34741 {
34742 cp_parser_error (parser, "invalid pragma");
34743 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
34744 return NULL_TREE;
34745 }
34746
34747 cp_lexer_consume_token (parser->lexer);
34748
34749 if (enter)
34750 clauses = cp_parser_oacc_all_clauses (parser, OACC_ENTER_DATA_CLAUSE_MASK,
34751 "#pragma acc enter data", pragma_tok);
34752 else
34753 clauses = cp_parser_oacc_all_clauses (parser, OACC_EXIT_DATA_CLAUSE_MASK,
34754 "#pragma acc exit data", pragma_tok);
34755
34756 if (find_omp_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
34757 {
34758 error_at (pragma_tok->location,
34759 "%<#pragma acc enter data%> has no data movement clause");
34760 return NULL_TREE;
34761 }
34762
34763 stmt = enter ? make_node (OACC_ENTER_DATA) : make_node (OACC_EXIT_DATA);
34764 TREE_TYPE (stmt) = void_type_node;
34765 OMP_STANDALONE_CLAUSES (stmt) = clauses;
34766 SET_EXPR_LOCATION (stmt, pragma_tok->location);
34767 add_stmt (stmt);
34768 return stmt;
34769 }
34770
34771 /* OpenACC 2.0:
34772 # pragma acc loop oacc-loop-clause[optseq] new-line
34773 structured-block */
34774
34775 #define OACC_LOOP_CLAUSE_MASK \
34776 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COLLAPSE) \
34777 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRIVATE) \
34778 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION) \
34779 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_GANG) \
34780 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR) \
34781 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WORKER) \
34782 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_AUTO) \
34783 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_INDEPENDENT) \
34784 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SEQ) \
34785 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_TILE))
34786
34787 static tree
34788 cp_parser_oacc_loop (cp_parser *parser, cp_token *pragma_tok, char *p_name,
34789 omp_clause_mask mask, tree *cclauses)
34790 {
34791 strcat (p_name, " loop");
34792 mask |= OACC_LOOP_CLAUSE_MASK;
34793
34794 tree clauses = cp_parser_oacc_all_clauses (parser, mask, p_name, pragma_tok,
34795 cclauses == NULL);
34796 if (cclauses)
34797 {
34798 clauses = c_oacc_split_loop_clauses (clauses, cclauses);
34799 if (*cclauses)
34800 finish_omp_clauses (*cclauses, false);
34801 if (clauses)
34802 finish_omp_clauses (clauses, false);
34803 }
34804
34805 tree block = begin_omp_structured_block ();
34806 int save = cp_parser_begin_omp_structured_block (parser);
34807 tree stmt = cp_parser_omp_for_loop (parser, OACC_LOOP, clauses, NULL);
34808 cp_parser_end_omp_structured_block (parser, save);
34809 add_stmt (finish_omp_structured_block (block));
34810
34811 return stmt;
34812 }
34813
34814 /* OpenACC 2.0:
34815 # pragma acc kernels oacc-kernels-clause[optseq] new-line
34816 structured-block
34817
34818 or
34819
34820 # pragma acc parallel oacc-parallel-clause[optseq] new-line
34821 structured-block
34822 */
34823
34824 #define OACC_KERNELS_CLAUSE_MASK \
34825 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
34826 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
34827 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
34828 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
34829 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
34830 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEFAULT) \
34831 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
34832 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
34833 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
34834 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
34835 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
34836 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
34837 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE) \
34838 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
34839
34840 #define OACC_PARALLEL_CLAUSE_MASK \
34841 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
34842 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
34843 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
34844 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
34845 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
34846 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEFAULT) \
34847 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
34848 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_FIRSTPRIVATE) \
34849 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
34850 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_GANGS) \
34851 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_WORKERS) \
34852 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
34853 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
34854 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
34855 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
34856 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE) \
34857 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRIVATE) \
34858 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION) \
34859 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR_LENGTH) \
34860 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
34861
34862 static tree
34863 cp_parser_oacc_kernels_parallel (cp_parser *parser, cp_token *pragma_tok,
34864 char *p_name)
34865 {
34866 omp_clause_mask mask;
34867 enum tree_code code;
34868 switch (pragma_tok->pragma_kind)
34869 {
34870 case PRAGMA_OACC_KERNELS:
34871 strcat (p_name, " kernels");
34872 mask = OACC_KERNELS_CLAUSE_MASK;
34873 code = OACC_KERNELS;
34874 break;
34875 case PRAGMA_OACC_PARALLEL:
34876 strcat (p_name, " parallel");
34877 mask = OACC_PARALLEL_CLAUSE_MASK;
34878 code = OACC_PARALLEL;
34879 break;
34880 default:
34881 gcc_unreachable ();
34882 }
34883
34884 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
34885 {
34886 const char *p
34887 = IDENTIFIER_POINTER (cp_lexer_peek_token (parser->lexer)->u.value);
34888 if (strcmp (p, "loop") == 0)
34889 {
34890 cp_lexer_consume_token (parser->lexer);
34891 mask |= OACC_LOOP_CLAUSE_MASK;
34892
34893 tree block = begin_omp_parallel ();
34894 tree clauses;
34895 cp_parser_oacc_loop (parser, pragma_tok, p_name, mask, &clauses);
34896 return finish_omp_construct (code, block, clauses);
34897 }
34898 }
34899
34900 tree clauses = cp_parser_oacc_all_clauses (parser, mask, p_name, pragma_tok);
34901
34902 tree block = begin_omp_parallel ();
34903 unsigned int save = cp_parser_begin_omp_structured_block (parser);
34904 cp_parser_statement (parser, NULL_TREE, false, NULL);
34905 cp_parser_end_omp_structured_block (parser, save);
34906 return finish_omp_construct (code, block, clauses);
34907 }
34908
34909 /* OpenACC 2.0:
34910 # pragma acc update oacc-update-clause[optseq] new-line
34911 */
34912
34913 #define OACC_UPDATE_CLAUSE_MASK \
34914 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
34915 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICE) \
34916 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_HOST) \
34917 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
34918 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SELF) \
34919 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT))
34920
34921 static tree
34922 cp_parser_oacc_update (cp_parser *parser, cp_token *pragma_tok)
34923 {
34924 tree stmt, clauses;
34925
34926 clauses = cp_parser_oacc_all_clauses (parser, OACC_UPDATE_CLAUSE_MASK,
34927 "#pragma acc update", pragma_tok);
34928
34929 if (find_omp_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
34930 {
34931 error_at (pragma_tok->location,
34932 "%<#pragma acc update%> must contain at least one "
34933 "%<device%> or %<host%> or %<self%> clause");
34934 return NULL_TREE;
34935 }
34936
34937 stmt = make_node (OACC_UPDATE);
34938 TREE_TYPE (stmt) = void_type_node;
34939 OACC_UPDATE_CLAUSES (stmt) = clauses;
34940 SET_EXPR_LOCATION (stmt, pragma_tok->location);
34941 add_stmt (stmt);
34942 return stmt;
34943 }
34944
34945 /* OpenACC 2.0:
34946 # pragma acc wait [(intseq)] oacc-wait-clause[optseq] new-line
34947
34948 LOC is the location of the #pragma token.
34949 */
34950
34951 #define OACC_WAIT_CLAUSE_MASK \
34952 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC))
34953
34954 static tree
34955 cp_parser_oacc_wait (cp_parser *parser, cp_token *pragma_tok)
34956 {
34957 tree clauses, list = NULL_TREE, stmt = NULL_TREE;
34958 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
34959
34960 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
34961 list = cp_parser_oacc_wait_list (parser, loc, list);
34962
34963 clauses = cp_parser_oacc_all_clauses (parser, OACC_WAIT_CLAUSE_MASK,
34964 "#pragma acc wait", pragma_tok);
34965
34966 stmt = c_finish_oacc_wait (loc, list, clauses);
34967
34968 return stmt;
34969 }
34970
34971 /* OpenMP 4.0:
34972 # pragma omp declare simd declare-simd-clauses[optseq] new-line */
34973
34974 #define OMP_DECLARE_SIMD_CLAUSE_MASK \
34975 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN) \
34976 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
34977 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
34978 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNIFORM) \
34979 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_INBRANCH) \
34980 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOTINBRANCH))
34981
34982 static void
34983 cp_parser_omp_declare_simd (cp_parser *parser, cp_token *pragma_tok,
34984 enum pragma_context context)
34985 {
34986 bool first_p = parser->omp_declare_simd == NULL;
34987 cp_omp_declare_simd_data data;
34988 if (first_p)
34989 {
34990 data.error_seen = false;
34991 data.fndecl_seen = false;
34992 data.tokens = vNULL;
34993 parser->omp_declare_simd = &data;
34994 }
34995 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
34996 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
34997 cp_lexer_consume_token (parser->lexer);
34998 if (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
34999 parser->omp_declare_simd->error_seen = true;
35000 cp_parser_require_pragma_eol (parser, pragma_tok);
35001 struct cp_token_cache *cp
35002 = cp_token_cache_new (pragma_tok, cp_lexer_peek_token (parser->lexer));
35003 parser->omp_declare_simd->tokens.safe_push (cp);
35004 if (first_p)
35005 {
35006 while (cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA))
35007 cp_parser_pragma (parser, context);
35008 switch (context)
35009 {
35010 case pragma_external:
35011 cp_parser_declaration (parser);
35012 break;
35013 case pragma_member:
35014 cp_parser_member_declaration (parser);
35015 break;
35016 case pragma_objc_icode:
35017 cp_parser_block_declaration (parser, /*statement_p=*/false);
35018 break;
35019 default:
35020 cp_parser_declaration_statement (parser);
35021 break;
35022 }
35023 if (parser->omp_declare_simd
35024 && !parser->omp_declare_simd->error_seen
35025 && !parser->omp_declare_simd->fndecl_seen)
35026 error_at (pragma_tok->location,
35027 "%<#pragma omp declare simd%> not immediately followed by "
35028 "function declaration or definition");
35029 data.tokens.release ();
35030 parser->omp_declare_simd = NULL;
35031 }
35032 }
35033
35034 /* Handles the delayed parsing of the Cilk Plus SIMD-enabled function.
35035 This function is modelled similar to the late parsing of omp declare
35036 simd. */
35037
35038 static tree
35039 cp_parser_late_parsing_cilk_simd_fn_info (cp_parser *parser, tree attrs)
35040 {
35041 struct cp_token_cache *ce;
35042 cp_omp_declare_simd_data *info = parser->cilk_simd_fn_info;
35043 int ii = 0;
35044
35045 if (parser->omp_declare_simd != NULL
35046 || lookup_attribute ("simd", attrs))
35047 {
35048 error ("%<#pragma omp declare simd%> of %<simd%> attribute cannot be "
35049 "used in the same function marked as a Cilk Plus SIMD-enabled "
35050 " function");
35051 parser->cilk_simd_fn_info->tokens.release ();
35052 XDELETE (parser->cilk_simd_fn_info);
35053 parser->cilk_simd_fn_info = NULL;
35054 return attrs;
35055 }
35056 if (!info->error_seen && info->fndecl_seen)
35057 {
35058 error ("vector attribute not immediately followed by a single function"
35059 " declaration or definition");
35060 info->error_seen = true;
35061 }
35062 if (info->error_seen)
35063 return attrs;
35064
35065 FOR_EACH_VEC_ELT (info->tokens, ii, ce)
35066 {
35067 tree c, cl;
35068
35069 cp_parser_push_lexer_for_tokens (parser, ce);
35070 parser->lexer->in_pragma = true;
35071 cl = cp_parser_omp_all_clauses (parser, CILK_SIMD_FN_CLAUSE_MASK,
35072 "SIMD-enabled functions attribute",
35073 NULL);
35074 cp_parser_pop_lexer (parser);
35075 if (cl)
35076 cl = tree_cons (NULL_TREE, cl, NULL_TREE);
35077
35078 c = build_tree_list (get_identifier ("cilk simd function"), NULL_TREE);
35079 TREE_CHAIN (c) = attrs;
35080 attrs = c;
35081
35082 c = build_tree_list (get_identifier ("omp declare simd"), cl);
35083 TREE_CHAIN (c) = attrs;
35084 if (processing_template_decl)
35085 ATTR_IS_DEPENDENT (c) = 1;
35086 attrs = c;
35087 }
35088 info->fndecl_seen = true;
35089 parser->cilk_simd_fn_info->tokens.release ();
35090 XDELETE (parser->cilk_simd_fn_info);
35091 parser->cilk_simd_fn_info = NULL;
35092 return attrs;
35093 }
35094
35095 /* Finalize #pragma omp declare simd clauses after direct declarator has
35096 been parsed, and put that into "omp declare simd" attribute. */
35097
35098 static tree
35099 cp_parser_late_parsing_omp_declare_simd (cp_parser *parser, tree attrs)
35100 {
35101 struct cp_token_cache *ce;
35102 cp_omp_declare_simd_data *data = parser->omp_declare_simd;
35103 int i;
35104
35105 if (!data->error_seen && data->fndecl_seen)
35106 {
35107 error ("%<#pragma omp declare simd%> not immediately followed by "
35108 "a single function declaration or definition");
35109 data->error_seen = true;
35110 return attrs;
35111 }
35112 if (data->error_seen)
35113 return attrs;
35114
35115 FOR_EACH_VEC_ELT (data->tokens, i, ce)
35116 {
35117 tree c, cl;
35118
35119 cp_parser_push_lexer_for_tokens (parser, ce);
35120 parser->lexer->in_pragma = true;
35121 gcc_assert (cp_lexer_peek_token (parser->lexer)->type == CPP_PRAGMA);
35122 cp_token *pragma_tok = cp_lexer_consume_token (parser->lexer);
35123 cp_lexer_consume_token (parser->lexer);
35124 cl = cp_parser_omp_all_clauses (parser, OMP_DECLARE_SIMD_CLAUSE_MASK,
35125 "#pragma omp declare simd", pragma_tok);
35126 cp_parser_pop_lexer (parser);
35127 if (cl)
35128 cl = tree_cons (NULL_TREE, cl, NULL_TREE);
35129 c = build_tree_list (get_identifier ("omp declare simd"), cl);
35130 TREE_CHAIN (c) = attrs;
35131 if (processing_template_decl)
35132 ATTR_IS_DEPENDENT (c) = 1;
35133 attrs = c;
35134 }
35135
35136 data->fndecl_seen = true;
35137 return attrs;
35138 }
35139
35140
35141 /* OpenMP 4.0:
35142 # pragma omp declare target new-line
35143 declarations and definitions
35144 # pragma omp end declare target new-line
35145
35146 OpenMP 4.5:
35147 # pragma omp declare target ( extended-list ) new-line
35148
35149 # pragma omp declare target declare-target-clauses[seq] new-line */
35150
35151 #define OMP_DECLARE_TARGET_CLAUSE_MASK \
35152 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO) \
35153 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINK))
35154
35155 static void
35156 cp_parser_omp_declare_target (cp_parser *parser, cp_token *pragma_tok)
35157 {
35158 tree clauses = NULL_TREE;
35159 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35160 clauses
35161 = cp_parser_omp_all_clauses (parser, OMP_DECLARE_TARGET_CLAUSE_MASK,
35162 "#pragma omp declare target", pragma_tok);
35163 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
35164 {
35165 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_TO_DECLARE,
35166 clauses);
35167 clauses = finish_omp_clauses (clauses, true);
35168 cp_parser_require_pragma_eol (parser, pragma_tok);
35169 }
35170 else
35171 {
35172 cp_parser_require_pragma_eol (parser, pragma_tok);
35173 scope_chain->omp_declare_target_attribute++;
35174 return;
35175 }
35176 if (scope_chain->omp_declare_target_attribute)
35177 error_at (pragma_tok->location,
35178 "%<#pragma omp declare target%> with clauses in between "
35179 "%<#pragma omp declare target%> without clauses and "
35180 "%<#pragma omp end declare target%>");
35181 for (tree c = clauses; c; c = OMP_CLAUSE_CHAIN (c))
35182 {
35183 tree t = OMP_CLAUSE_DECL (c), id;
35184 tree at1 = lookup_attribute ("omp declare target", DECL_ATTRIBUTES (t));
35185 tree at2 = lookup_attribute ("omp declare target link",
35186 DECL_ATTRIBUTES (t));
35187 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINK)
35188 {
35189 id = get_identifier ("omp declare target link");
35190 std::swap (at1, at2);
35191 }
35192 else
35193 id = get_identifier ("omp declare target");
35194 if (at2)
35195 {
35196 error_at (OMP_CLAUSE_LOCATION (c),
35197 "%qD specified both in declare target %<link%> and %<to%>"
35198 " clauses", t);
35199 continue;
35200 }
35201 if (!at1)
35202 {
35203 symtab_node *node = symtab_node::get (t);
35204 DECL_ATTRIBUTES (t) = tree_cons (id, NULL_TREE, DECL_ATTRIBUTES (t));
35205 if (node != NULL)
35206 {
35207 node->offloadable = 1;
35208 if (ENABLE_OFFLOADING)
35209 {
35210 g->have_offload = true;
35211 if (is_a <varpool_node *> (node))
35212 {
35213 vec_safe_push (offload_vars, t);
35214 node->force_output = 1;
35215 }
35216 }
35217 }
35218 }
35219 }
35220 }
35221
35222 static void
35223 cp_parser_omp_end_declare_target (cp_parser *parser, cp_token *pragma_tok)
35224 {
35225 const char *p = "";
35226 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35227 {
35228 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
35229 p = IDENTIFIER_POINTER (id);
35230 }
35231 if (strcmp (p, "declare") == 0)
35232 {
35233 cp_lexer_consume_token (parser->lexer);
35234 p = "";
35235 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35236 {
35237 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
35238 p = IDENTIFIER_POINTER (id);
35239 }
35240 if (strcmp (p, "target") == 0)
35241 cp_lexer_consume_token (parser->lexer);
35242 else
35243 {
35244 cp_parser_error (parser, "expected %<target%>");
35245 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35246 return;
35247 }
35248 }
35249 else
35250 {
35251 cp_parser_error (parser, "expected %<declare%>");
35252 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35253 return;
35254 }
35255 cp_parser_require_pragma_eol (parser, pragma_tok);
35256 if (!scope_chain->omp_declare_target_attribute)
35257 error_at (pragma_tok->location,
35258 "%<#pragma omp end declare target%> without corresponding "
35259 "%<#pragma omp declare target%>");
35260 else
35261 scope_chain->omp_declare_target_attribute--;
35262 }
35263
35264 /* Helper function of cp_parser_omp_declare_reduction. Parse the combiner
35265 expression and optional initializer clause of
35266 #pragma omp declare reduction. We store the expression(s) as
35267 either 3, 6 or 7 special statements inside of the artificial function's
35268 body. The first two statements are DECL_EXPRs for the artificial
35269 OMP_OUT resp. OMP_IN variables, followed by a statement with the combiner
35270 expression that uses those variables.
35271 If there was any INITIALIZER clause, this is followed by further statements,
35272 the fourth and fifth statements are DECL_EXPRs for the artificial
35273 OMP_PRIV resp. OMP_ORIG variables. If the INITIALIZER clause wasn't the
35274 constructor variant (first token after open paren is not omp_priv),
35275 then the sixth statement is a statement with the function call expression
35276 that uses the OMP_PRIV and optionally OMP_ORIG variable.
35277 Otherwise, the sixth statement is whatever statement cp_finish_decl emits
35278 to initialize the OMP_PRIV artificial variable and there is seventh
35279 statement, a DECL_EXPR of the OMP_PRIV statement again. */
35280
35281 static bool
35282 cp_parser_omp_declare_reduction_exprs (tree fndecl, cp_parser *parser)
35283 {
35284 tree type = TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (fndecl)));
35285 gcc_assert (TREE_CODE (type) == REFERENCE_TYPE);
35286 type = TREE_TYPE (type);
35287 tree omp_out = build_lang_decl (VAR_DECL, get_identifier ("omp_out"), type);
35288 DECL_ARTIFICIAL (omp_out) = 1;
35289 pushdecl (omp_out);
35290 add_decl_expr (omp_out);
35291 tree omp_in = build_lang_decl (VAR_DECL, get_identifier ("omp_in"), type);
35292 DECL_ARTIFICIAL (omp_in) = 1;
35293 pushdecl (omp_in);
35294 add_decl_expr (omp_in);
35295 tree combiner;
35296 tree omp_priv = NULL_TREE, omp_orig = NULL_TREE, initializer = NULL_TREE;
35297
35298 keep_next_level (true);
35299 tree block = begin_omp_structured_block ();
35300 combiner = cp_parser_expression (parser);
35301 finish_expr_stmt (combiner);
35302 block = finish_omp_structured_block (block);
35303 add_stmt (block);
35304
35305 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
35306 return false;
35307
35308 const char *p = "";
35309 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35310 {
35311 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
35312 p = IDENTIFIER_POINTER (id);
35313 }
35314
35315 if (strcmp (p, "initializer") == 0)
35316 {
35317 cp_lexer_consume_token (parser->lexer);
35318 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
35319 return false;
35320
35321 p = "";
35322 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35323 {
35324 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
35325 p = IDENTIFIER_POINTER (id);
35326 }
35327
35328 omp_priv = build_lang_decl (VAR_DECL, get_identifier ("omp_priv"), type);
35329 DECL_ARTIFICIAL (omp_priv) = 1;
35330 pushdecl (omp_priv);
35331 add_decl_expr (omp_priv);
35332 omp_orig = build_lang_decl (VAR_DECL, get_identifier ("omp_orig"), type);
35333 DECL_ARTIFICIAL (omp_orig) = 1;
35334 pushdecl (omp_orig);
35335 add_decl_expr (omp_orig);
35336
35337 keep_next_level (true);
35338 block = begin_omp_structured_block ();
35339
35340 bool ctor = false;
35341 if (strcmp (p, "omp_priv") == 0)
35342 {
35343 bool is_direct_init, is_non_constant_init;
35344 ctor = true;
35345 cp_lexer_consume_token (parser->lexer);
35346 /* Reject initializer (omp_priv) and initializer (omp_priv ()). */
35347 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
35348 || (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
35349 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
35350 == CPP_CLOSE_PAREN
35351 && cp_lexer_peek_nth_token (parser->lexer, 3)->type
35352 == CPP_CLOSE_PAREN))
35353 {
35354 finish_omp_structured_block (block);
35355 error ("invalid initializer clause");
35356 return false;
35357 }
35358 initializer = cp_parser_initializer (parser, &is_direct_init,
35359 &is_non_constant_init);
35360 cp_finish_decl (omp_priv, initializer, !is_non_constant_init,
35361 NULL_TREE, LOOKUP_ONLYCONVERTING);
35362 }
35363 else
35364 {
35365 cp_parser_parse_tentatively (parser);
35366 tree fn_name = cp_parser_id_expression (parser, /*template_p=*/false,
35367 /*check_dependency_p=*/true,
35368 /*template_p=*/NULL,
35369 /*declarator_p=*/false,
35370 /*optional_p=*/false);
35371 vec<tree, va_gc> *args;
35372 if (fn_name == error_mark_node
35373 || cp_parser_error_occurred (parser)
35374 || !cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
35375 || ((args = cp_parser_parenthesized_expression_list
35376 (parser, non_attr, /*cast_p=*/false,
35377 /*allow_expansion_p=*/true,
35378 /*non_constant_p=*/NULL)),
35379 cp_parser_error_occurred (parser)))
35380 {
35381 finish_omp_structured_block (block);
35382 cp_parser_abort_tentative_parse (parser);
35383 cp_parser_error (parser, "expected id-expression (arguments)");
35384 return false;
35385 }
35386 unsigned int i;
35387 tree arg;
35388 FOR_EACH_VEC_SAFE_ELT (args, i, arg)
35389 if (arg == omp_priv
35390 || (TREE_CODE (arg) == ADDR_EXPR
35391 && TREE_OPERAND (arg, 0) == omp_priv))
35392 break;
35393 cp_parser_abort_tentative_parse (parser);
35394 if (arg == NULL_TREE)
35395 error ("one of the initializer call arguments should be %<omp_priv%>"
35396 " or %<&omp_priv%>");
35397 initializer = cp_parser_postfix_expression (parser, false, false, false,
35398 false, NULL);
35399 finish_expr_stmt (initializer);
35400 }
35401
35402 block = finish_omp_structured_block (block);
35403 cp_walk_tree (&block, cp_remove_omp_priv_cleanup_stmt, omp_priv, NULL);
35404 add_stmt (block);
35405
35406 if (ctor)
35407 add_decl_expr (omp_orig);
35408
35409 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
35410 return false;
35411 }
35412
35413 if (!cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA_EOL))
35414 cp_parser_required_error (parser, RT_PRAGMA_EOL, /*keyword=*/false);
35415
35416 return true;
35417 }
35418
35419 /* OpenMP 4.0
35420 #pragma omp declare reduction (reduction-id : typename-list : expression) \
35421 initializer-clause[opt] new-line
35422
35423 initializer-clause:
35424 initializer (omp_priv initializer)
35425 initializer (function-name (argument-list)) */
35426
35427 static void
35428 cp_parser_omp_declare_reduction (cp_parser *parser, cp_token *pragma_tok,
35429 enum pragma_context)
35430 {
35431 auto_vec<tree> types;
35432 enum tree_code reduc_code = ERROR_MARK;
35433 tree reduc_id = NULL_TREE, orig_reduc_id = NULL_TREE, type;
35434 unsigned int i;
35435 cp_token *first_token;
35436 cp_token_cache *cp;
35437 int errs;
35438 void *p;
35439
35440 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
35441 p = obstack_alloc (&declarator_obstack, 0);
35442
35443 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
35444 goto fail;
35445
35446 switch (cp_lexer_peek_token (parser->lexer)->type)
35447 {
35448 case CPP_PLUS:
35449 reduc_code = PLUS_EXPR;
35450 break;
35451 case CPP_MULT:
35452 reduc_code = MULT_EXPR;
35453 break;
35454 case CPP_MINUS:
35455 reduc_code = MINUS_EXPR;
35456 break;
35457 case CPP_AND:
35458 reduc_code = BIT_AND_EXPR;
35459 break;
35460 case CPP_XOR:
35461 reduc_code = BIT_XOR_EXPR;
35462 break;
35463 case CPP_OR:
35464 reduc_code = BIT_IOR_EXPR;
35465 break;
35466 case CPP_AND_AND:
35467 reduc_code = TRUTH_ANDIF_EXPR;
35468 break;
35469 case CPP_OR_OR:
35470 reduc_code = TRUTH_ORIF_EXPR;
35471 break;
35472 case CPP_NAME:
35473 reduc_id = orig_reduc_id = cp_parser_identifier (parser);
35474 break;
35475 default:
35476 cp_parser_error (parser, "expected %<+%>, %<*%>, %<-%>, %<&%>, %<^%>, "
35477 "%<|%>, %<&&%>, %<||%> or identifier");
35478 goto fail;
35479 }
35480
35481 if (reduc_code != ERROR_MARK)
35482 cp_lexer_consume_token (parser->lexer);
35483
35484 reduc_id = omp_reduction_id (reduc_code, reduc_id, NULL_TREE);
35485 if (reduc_id == error_mark_node)
35486 goto fail;
35487
35488 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
35489 goto fail;
35490
35491 /* Types may not be defined in declare reduction type list. */
35492 const char *saved_message;
35493 saved_message = parser->type_definition_forbidden_message;
35494 parser->type_definition_forbidden_message
35495 = G_("types may not be defined in declare reduction type list");
35496 bool saved_colon_corrects_to_scope_p;
35497 saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
35498 parser->colon_corrects_to_scope_p = false;
35499 bool saved_colon_doesnt_start_class_def_p;
35500 saved_colon_doesnt_start_class_def_p
35501 = parser->colon_doesnt_start_class_def_p;
35502 parser->colon_doesnt_start_class_def_p = true;
35503
35504 while (true)
35505 {
35506 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
35507 type = cp_parser_type_id (parser);
35508 if (type == error_mark_node)
35509 ;
35510 else if (ARITHMETIC_TYPE_P (type)
35511 && (orig_reduc_id == NULL_TREE
35512 || (TREE_CODE (type) != COMPLEX_TYPE
35513 && (strcmp (IDENTIFIER_POINTER (orig_reduc_id),
35514 "min") == 0
35515 || strcmp (IDENTIFIER_POINTER (orig_reduc_id),
35516 "max") == 0))))
35517 error_at (loc, "predeclared arithmetic type %qT in "
35518 "%<#pragma omp declare reduction%>", type);
35519 else if (TREE_CODE (type) == FUNCTION_TYPE
35520 || TREE_CODE (type) == METHOD_TYPE
35521 || TREE_CODE (type) == ARRAY_TYPE)
35522 error_at (loc, "function or array type %qT in "
35523 "%<#pragma omp declare reduction%>", type);
35524 else if (TREE_CODE (type) == REFERENCE_TYPE)
35525 error_at (loc, "reference type %qT in "
35526 "%<#pragma omp declare reduction%>", type);
35527 else if (TYPE_QUALS_NO_ADDR_SPACE (type))
35528 error_at (loc, "const, volatile or __restrict qualified type %qT in "
35529 "%<#pragma omp declare reduction%>", type);
35530 else
35531 types.safe_push (type);
35532
35533 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
35534 cp_lexer_consume_token (parser->lexer);
35535 else
35536 break;
35537 }
35538
35539 /* Restore the saved message. */
35540 parser->type_definition_forbidden_message = saved_message;
35541 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
35542 parser->colon_doesnt_start_class_def_p
35543 = saved_colon_doesnt_start_class_def_p;
35544
35545 if (!cp_parser_require (parser, CPP_COLON, RT_COLON)
35546 || types.is_empty ())
35547 {
35548 fail:
35549 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35550 goto done;
35551 }
35552
35553 first_token = cp_lexer_peek_token (parser->lexer);
35554 cp = NULL;
35555 errs = errorcount;
35556 FOR_EACH_VEC_ELT (types, i, type)
35557 {
35558 tree fntype
35559 = build_function_type_list (void_type_node,
35560 cp_build_reference_type (type, false),
35561 NULL_TREE);
35562 tree this_reduc_id = reduc_id;
35563 if (!dependent_type_p (type))
35564 this_reduc_id = omp_reduction_id (ERROR_MARK, reduc_id, type);
35565 tree fndecl = build_lang_decl (FUNCTION_DECL, this_reduc_id, fntype);
35566 DECL_SOURCE_LOCATION (fndecl) = pragma_tok->location;
35567 DECL_ARTIFICIAL (fndecl) = 1;
35568 DECL_EXTERNAL (fndecl) = 1;
35569 DECL_DECLARED_INLINE_P (fndecl) = 1;
35570 DECL_IGNORED_P (fndecl) = 1;
35571 DECL_OMP_DECLARE_REDUCTION_P (fndecl) = 1;
35572 DECL_ATTRIBUTES (fndecl)
35573 = tree_cons (get_identifier ("gnu_inline"), NULL_TREE,
35574 DECL_ATTRIBUTES (fndecl));
35575 if (processing_template_decl)
35576 fndecl = push_template_decl (fndecl);
35577 bool block_scope = false;
35578 tree block = NULL_TREE;
35579 if (current_function_decl)
35580 {
35581 block_scope = true;
35582 DECL_CONTEXT (fndecl) = global_namespace;
35583 if (!processing_template_decl)
35584 pushdecl (fndecl);
35585 }
35586 else if (current_class_type)
35587 {
35588 if (cp == NULL)
35589 {
35590 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
35591 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
35592 cp_lexer_consume_token (parser->lexer);
35593 if (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
35594 goto fail;
35595 cp = cp_token_cache_new (first_token,
35596 cp_lexer_peek_nth_token (parser->lexer,
35597 2));
35598 }
35599 DECL_STATIC_FUNCTION_P (fndecl) = 1;
35600 finish_member_declaration (fndecl);
35601 DECL_PENDING_INLINE_INFO (fndecl) = cp;
35602 DECL_PENDING_INLINE_P (fndecl) = 1;
35603 vec_safe_push (unparsed_funs_with_definitions, fndecl);
35604 continue;
35605 }
35606 else
35607 {
35608 DECL_CONTEXT (fndecl) = current_namespace;
35609 pushdecl (fndecl);
35610 }
35611 if (!block_scope)
35612 start_preparsed_function (fndecl, NULL_TREE, SF_PRE_PARSED);
35613 else
35614 block = begin_omp_structured_block ();
35615 if (cp)
35616 {
35617 cp_parser_push_lexer_for_tokens (parser, cp);
35618 parser->lexer->in_pragma = true;
35619 }
35620 if (!cp_parser_omp_declare_reduction_exprs (fndecl, parser))
35621 {
35622 if (!block_scope)
35623 finish_function (0);
35624 else
35625 DECL_CONTEXT (fndecl) = current_function_decl;
35626 if (cp)
35627 cp_parser_pop_lexer (parser);
35628 goto fail;
35629 }
35630 if (cp)
35631 cp_parser_pop_lexer (parser);
35632 if (!block_scope)
35633 finish_function (0);
35634 else
35635 {
35636 DECL_CONTEXT (fndecl) = current_function_decl;
35637 block = finish_omp_structured_block (block);
35638 if (TREE_CODE (block) == BIND_EXPR)
35639 DECL_SAVED_TREE (fndecl) = BIND_EXPR_BODY (block);
35640 else if (TREE_CODE (block) == STATEMENT_LIST)
35641 DECL_SAVED_TREE (fndecl) = block;
35642 if (processing_template_decl)
35643 add_decl_expr (fndecl);
35644 }
35645 cp_check_omp_declare_reduction (fndecl);
35646 if (cp == NULL && types.length () > 1)
35647 cp = cp_token_cache_new (first_token,
35648 cp_lexer_peek_nth_token (parser->lexer, 2));
35649 if (errs != errorcount)
35650 break;
35651 }
35652
35653 cp_parser_require_pragma_eol (parser, pragma_tok);
35654
35655 done:
35656 /* Free any declarators allocated. */
35657 obstack_free (&declarator_obstack, p);
35658 }
35659
35660 /* OpenMP 4.0
35661 #pragma omp declare simd declare-simd-clauses[optseq] new-line
35662 #pragma omp declare reduction (reduction-id : typename-list : expression) \
35663 initializer-clause[opt] new-line
35664 #pragma omp declare target new-line */
35665
35666 static void
35667 cp_parser_omp_declare (cp_parser *parser, cp_token *pragma_tok,
35668 enum pragma_context context)
35669 {
35670 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35671 {
35672 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
35673 const char *p = IDENTIFIER_POINTER (id);
35674
35675 if (strcmp (p, "simd") == 0)
35676 {
35677 cp_lexer_consume_token (parser->lexer);
35678 cp_parser_omp_declare_simd (parser, pragma_tok,
35679 context);
35680 return;
35681 }
35682 cp_ensure_no_omp_declare_simd (parser);
35683 if (strcmp (p, "reduction") == 0)
35684 {
35685 cp_lexer_consume_token (parser->lexer);
35686 cp_parser_omp_declare_reduction (parser, pragma_tok,
35687 context);
35688 return;
35689 }
35690 if (!flag_openmp) /* flag_openmp_simd */
35691 {
35692 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35693 return;
35694 }
35695 if (strcmp (p, "target") == 0)
35696 {
35697 cp_lexer_consume_token (parser->lexer);
35698 cp_parser_omp_declare_target (parser, pragma_tok);
35699 return;
35700 }
35701 }
35702 cp_parser_error (parser, "expected %<simd%> or %<reduction%> "
35703 "or %<target%>");
35704 cp_parser_require_pragma_eol (parser, pragma_tok);
35705 }
35706
35707 /* OpenMP 4.5:
35708 #pragma omp taskloop taskloop-clause[optseq] new-line
35709 for-loop
35710
35711 #pragma omp taskloop simd taskloop-simd-clause[optseq] new-line
35712 for-loop */
35713
35714 #define OMP_TASKLOOP_CLAUSE_MASK \
35715 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
35716 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35717 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35718 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
35719 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
35720 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_GRAINSIZE) \
35721 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TASKS) \
35722 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE) \
35723 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED) \
35724 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
35725 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL) \
35726 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE) \
35727 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOGROUP) \
35728 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIORITY))
35729
35730 static tree
35731 cp_parser_omp_taskloop (cp_parser *parser, cp_token *pragma_tok,
35732 char *p_name, omp_clause_mask mask, tree *cclauses)
35733 {
35734 tree clauses, sb, ret;
35735 unsigned int save;
35736 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
35737
35738 strcat (p_name, " taskloop");
35739 mask |= OMP_TASKLOOP_CLAUSE_MASK;
35740
35741 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35742 {
35743 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
35744 const char *p = IDENTIFIER_POINTER (id);
35745
35746 if (strcmp (p, "simd") == 0)
35747 {
35748 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
35749 if (cclauses == NULL)
35750 cclauses = cclauses_buf;
35751
35752 cp_lexer_consume_token (parser->lexer);
35753 if (!flag_openmp) /* flag_openmp_simd */
35754 return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
35755 cclauses);
35756 sb = begin_omp_structured_block ();
35757 save = cp_parser_begin_omp_structured_block (parser);
35758 ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
35759 cclauses);
35760 cp_parser_end_omp_structured_block (parser, save);
35761 tree body = finish_omp_structured_block (sb);
35762 if (ret == NULL)
35763 return ret;
35764 ret = make_node (OMP_TASKLOOP);
35765 TREE_TYPE (ret) = void_type_node;
35766 OMP_FOR_BODY (ret) = body;
35767 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_TASKLOOP];
35768 SET_EXPR_LOCATION (ret, loc);
35769 add_stmt (ret);
35770 return ret;
35771 }
35772 }
35773 if (!flag_openmp) /* flag_openmp_simd */
35774 {
35775 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35776 return NULL_TREE;
35777 }
35778
35779 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
35780 cclauses == NULL);
35781 if (cclauses)
35782 {
35783 cp_omp_split_clauses (loc, OMP_TASKLOOP, mask, clauses, cclauses);
35784 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TASKLOOP];
35785 }
35786
35787 sb = begin_omp_structured_block ();
35788 save = cp_parser_begin_omp_structured_block (parser);
35789
35790 ret = cp_parser_omp_for_loop (parser, OMP_TASKLOOP, clauses, cclauses);
35791
35792 cp_parser_end_omp_structured_block (parser, save);
35793 add_stmt (finish_omp_structured_block (sb));
35794
35795 return ret;
35796 }
35797
35798
35799 /* OpenACC 2.0:
35800 # pragma acc routine oacc-routine-clause[optseq] new-line
35801 function-definition
35802
35803 # pragma acc routine ( name ) oacc-routine-clause[optseq] new-line
35804 */
35805
35806 #define OACC_ROUTINE_CLAUSE_MASK \
35807 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_GANG) \
35808 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WORKER) \
35809 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR) \
35810 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SEQ))
35811
35812
35813 /* Parse the OpenACC routine pragma. This has an optional '( name )'
35814 component, which must resolve to a declared namespace-scope
35815 function. The clauses are either processed directly (for a named
35816 function), or defered until the immediatley following declaration
35817 is parsed. */
35818
35819 static void
35820 cp_parser_oacc_routine (cp_parser *parser, cp_token *pragma_tok,
35821 enum pragma_context context)
35822 {
35823 bool first_p = parser->oacc_routine == NULL;
35824 location_t loc = pragma_tok->location;
35825 cp_omp_declare_simd_data data;
35826 if (first_p)
35827 {
35828 data.error_seen = false;
35829 data.fndecl_seen = false;
35830 data.tokens = vNULL;
35831 data.clauses = NULL_TREE;
35832 parser->oacc_routine = &data;
35833 }
35834
35835 tree decl = NULL_TREE;
35836 /* Create a dummy claue, to record location. */
35837 tree c_head = build_omp_clause (pragma_tok->location, OMP_CLAUSE_SEQ);
35838
35839 if (context != pragma_external)
35840 {
35841 cp_parser_error (parser, "%<#pragma acc routine%> not at file scope");
35842 parser->oacc_routine->error_seen = true;
35843 parser->oacc_routine = NULL;
35844 return;
35845 }
35846
35847 /* Look for optional '( name )'. */
35848 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
35849 {
35850 if (!first_p)
35851 {
35852 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
35853 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
35854 cp_lexer_consume_token (parser->lexer);
35855 if (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
35856 parser->oacc_routine->error_seen = true;
35857 cp_parser_require_pragma_eol (parser, pragma_tok);
35858
35859 error_at (OMP_CLAUSE_LOCATION (parser->oacc_routine->clauses),
35860 "%<#pragma oacc routine%> not followed by a single "
35861 "function declaration or definition");
35862
35863 parser->oacc_routine->error_seen = true;
35864 return;
35865 }
35866
35867 cp_lexer_consume_token (parser->lexer);
35868 cp_token *token = cp_lexer_peek_token (parser->lexer);
35869
35870 /* We parse the name as an id-expression. If it resolves to
35871 anything other than a non-overloaded function at namespace
35872 scope, it's an error. */
35873 tree id = cp_parser_id_expression (parser,
35874 /*template_keyword_p=*/false,
35875 /*check_dependency_p=*/false,
35876 /*template_p=*/NULL,
35877 /*declarator_p=*/false,
35878 /*optional_p=*/false);
35879 decl = cp_parser_lookup_name_simple (parser, id, token->location);
35880 if (id != error_mark_node && decl == error_mark_node)
35881 cp_parser_name_lookup_error (parser, id, decl, NLE_NULL,
35882 token->location);
35883
35884 if (decl == error_mark_node
35885 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
35886 {
35887 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35888 parser->oacc_routine = NULL;
35889 return;
35890 }
35891
35892 /* Build a chain of clauses. */
35893 parser->lexer->in_pragma = true;
35894 tree clauses = NULL_TREE;
35895 clauses = cp_parser_oacc_all_clauses (parser, OACC_ROUTINE_CLAUSE_MASK,
35896 "#pragma acc routine",
35897 cp_lexer_peek_token
35898 (parser->lexer));
35899
35900 /* Force clauses to be non-null, by attaching context to it. */
35901 clauses = tree_cons (c_head, clauses, NULL_TREE);
35902
35903 if (decl && is_overloaded_fn (decl)
35904 && (TREE_CODE (decl) != FUNCTION_DECL
35905 || DECL_FUNCTION_TEMPLATE_P (decl)))
35906 {
35907 error_at (loc, "%<#pragma acc routine%> names a set of overloads");
35908 parser->oacc_routine = NULL;
35909 return;
35910 }
35911
35912 /* Perhaps we should use the same rule as declarations in different
35913 namespaces? */
35914 if (!DECL_NAMESPACE_SCOPE_P (decl))
35915 {
35916 error_at (loc, "%<#pragma acc routine%> does not refer to a "
35917 "namespace scope function");
35918 parser->oacc_routine = NULL;
35919 return;
35920 }
35921
35922 if (!decl || TREE_CODE (decl) != FUNCTION_DECL)
35923 {
35924 error_at (loc,
35925 "%<#pragma acc routine%> does not refer to a function");
35926 parser->oacc_routine = NULL;
35927 return;
35928 }
35929
35930 data.clauses = clauses;
35931
35932 cp_finalize_oacc_routine (parser, decl, false);
35933 data.tokens.release ();
35934 parser->oacc_routine = NULL;
35935 }
35936 else
35937 {
35938 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
35939 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
35940 cp_lexer_consume_token (parser->lexer);
35941 if (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
35942 parser->oacc_routine->error_seen = true;
35943 cp_parser_require_pragma_eol (parser, pragma_tok);
35944
35945 struct cp_token_cache *cp
35946 = cp_token_cache_new (pragma_tok, cp_lexer_peek_token (parser->lexer));
35947 parser->oacc_routine->tokens.safe_push (cp);
35948
35949 if (first_p)
35950 parser->oacc_routine->clauses = c_head;
35951
35952 while (cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA))
35953 cp_parser_pragma (parser, context);
35954
35955 if (first_p)
35956 {
35957 /* Create an empty list of clauses. */
35958 parser->oacc_routine->clauses = tree_cons (c_head, NULL_TREE,
35959 NULL_TREE);
35960 cp_parser_declaration (parser);
35961
35962 if (parser->oacc_routine
35963 && !parser->oacc_routine->error_seen
35964 && !parser->oacc_routine->fndecl_seen)
35965 error_at (loc, "%<#pragma acc routine%> not followed by "
35966 "function declaration or definition");
35967
35968 data.tokens.release ();
35969 parser->oacc_routine = NULL;
35970 }
35971 }
35972 }
35973
35974 /* Finalize #pragma acc routine clauses after direct declarator has
35975 been parsed, and put that into "oacc routine" attribute. */
35976
35977 static tree
35978 cp_parser_late_parsing_oacc_routine (cp_parser *parser, tree attrs)
35979 {
35980 struct cp_token_cache *ce;
35981 cp_omp_declare_simd_data *data = parser->oacc_routine;
35982 tree cl, clauses = parser->oacc_routine->clauses;
35983 location_t loc;
35984
35985 loc = OMP_CLAUSE_LOCATION (TREE_PURPOSE(clauses));
35986
35987 if ((!data->error_seen && data->fndecl_seen)
35988 || data->tokens.length () != 1)
35989 {
35990 error_at (loc, "%<#pragma oacc routine%> not followed by a single "
35991 "function declaration or definition");
35992 data->error_seen = true;
35993 return attrs;
35994 }
35995 if (data->error_seen)
35996 return attrs;
35997
35998 ce = data->tokens[0];
35999
36000 cp_parser_push_lexer_for_tokens (parser, ce);
36001 parser->lexer->in_pragma = true;
36002 gcc_assert (cp_lexer_peek_token (parser->lexer)->type == CPP_PRAGMA);
36003
36004 cp_token *pragma_tok = cp_lexer_consume_token (parser->lexer);
36005 cl = cp_parser_oacc_all_clauses (parser, OACC_ROUTINE_CLAUSE_MASK,
36006 "#pragma oacc routine", pragma_tok);
36007 cp_parser_pop_lexer (parser);
36008
36009 tree c_head = build_omp_clause (loc, OMP_CLAUSE_SEQ);
36010
36011 /* Force clauses to be non-null, by attaching context to it. */
36012 parser->oacc_routine->clauses = tree_cons (c_head, cl, NULL_TREE);
36013
36014 data->fndecl_seen = true;
36015 return attrs;
36016 }
36017
36018 /* Apply any saved OpenACC routine clauses to a just-parsed
36019 declaration. */
36020
36021 static void
36022 cp_finalize_oacc_routine (cp_parser *parser, tree fndecl, bool is_defn)
36023 {
36024 if (__builtin_expect (parser->oacc_routine != NULL, 0))
36025 {
36026 tree clauses = parser->oacc_routine->clauses;
36027 location_t loc = OMP_CLAUSE_LOCATION (TREE_PURPOSE(clauses));
36028
36029 if (parser->oacc_routine->error_seen)
36030 return;
36031
36032 if (fndecl == error_mark_node)
36033 {
36034 parser->oacc_routine = NULL;
36035 return;
36036 }
36037
36038 if (TREE_CODE (fndecl) != FUNCTION_DECL)
36039 {
36040 cp_ensure_no_oacc_routine (parser);
36041 return;
36042 }
36043
36044 if (!fndecl || TREE_CODE (fndecl) != FUNCTION_DECL)
36045 {
36046 error_at (loc,
36047 "%<#pragma acc routine%> not followed by single function");
36048 parser->oacc_routine = NULL;
36049 }
36050
36051 if (get_oacc_fn_attrib (fndecl))
36052 {
36053 error_at (loc, "%<#pragma acc routine%> already applied to %D",
36054 fndecl);
36055 parser->oacc_routine = NULL;
36056 }
36057
36058 if (TREE_USED (fndecl) || (!is_defn && DECL_SAVED_TREE (fndecl)))
36059 {
36060 error_at (loc, "%<#pragma acc routine%> must be applied before %s",
36061 TREE_USED (fndecl) ? "use" : "definition");
36062 parser->oacc_routine = NULL;
36063 }
36064
36065 /* Process for function attrib */
36066 tree dims = build_oacc_routine_dims (TREE_VALUE (clauses));
36067 replace_oacc_fn_attrib (fndecl, dims);
36068
36069 /* Add an "omp target" attribute. */
36070 DECL_ATTRIBUTES (fndecl)
36071 = tree_cons (get_identifier ("omp declare target"),
36072 NULL_TREE, DECL_ATTRIBUTES (fndecl));
36073 }
36074 }
36075
36076 /* Main entry point to OpenMP statement pragmas. */
36077
36078 static void
36079 cp_parser_omp_construct (cp_parser *parser, cp_token *pragma_tok)
36080 {
36081 tree stmt;
36082 char p_name[sizeof "#pragma omp teams distribute parallel for simd"];
36083 omp_clause_mask mask (0);
36084
36085 switch (pragma_tok->pragma_kind)
36086 {
36087 case PRAGMA_OACC_ATOMIC:
36088 cp_parser_omp_atomic (parser, pragma_tok);
36089 return;
36090 case PRAGMA_OACC_CACHE:
36091 stmt = cp_parser_oacc_cache (parser, pragma_tok);
36092 break;
36093 case PRAGMA_OACC_DATA:
36094 stmt = cp_parser_oacc_data (parser, pragma_tok);
36095 break;
36096 case PRAGMA_OACC_ENTER_DATA:
36097 stmt = cp_parser_oacc_enter_exit_data (parser, pragma_tok, true);
36098 break;
36099 case PRAGMA_OACC_EXIT_DATA:
36100 stmt = cp_parser_oacc_enter_exit_data (parser, pragma_tok, false);
36101 break;
36102 case PRAGMA_OACC_HOST_DATA:
36103 stmt = cp_parser_oacc_host_data (parser, pragma_tok);
36104 break;
36105 case PRAGMA_OACC_KERNELS:
36106 case PRAGMA_OACC_PARALLEL:
36107 strcpy (p_name, "#pragma acc");
36108 stmt = cp_parser_oacc_kernels_parallel (parser, pragma_tok, p_name);
36109 break;
36110 case PRAGMA_OACC_LOOP:
36111 strcpy (p_name, "#pragma acc");
36112 stmt = cp_parser_oacc_loop (parser, pragma_tok, p_name, mask, NULL);
36113 break;
36114 case PRAGMA_OACC_UPDATE:
36115 stmt = cp_parser_oacc_update (parser, pragma_tok);
36116 break;
36117 case PRAGMA_OACC_WAIT:
36118 stmt = cp_parser_oacc_wait (parser, pragma_tok);
36119 break;
36120 case PRAGMA_OMP_ATOMIC:
36121 cp_parser_omp_atomic (parser, pragma_tok);
36122 return;
36123 case PRAGMA_OMP_CRITICAL:
36124 stmt = cp_parser_omp_critical (parser, pragma_tok);
36125 break;
36126 case PRAGMA_OMP_DISTRIBUTE:
36127 strcpy (p_name, "#pragma omp");
36128 stmt = cp_parser_omp_distribute (parser, pragma_tok, p_name, mask, NULL);
36129 break;
36130 case PRAGMA_OMP_FOR:
36131 strcpy (p_name, "#pragma omp");
36132 stmt = cp_parser_omp_for (parser, pragma_tok, p_name, mask, NULL);
36133 break;
36134 case PRAGMA_OMP_MASTER:
36135 stmt = cp_parser_omp_master (parser, pragma_tok);
36136 break;
36137 case PRAGMA_OMP_PARALLEL:
36138 strcpy (p_name, "#pragma omp");
36139 stmt = cp_parser_omp_parallel (parser, pragma_tok, p_name, mask, NULL);
36140 break;
36141 case PRAGMA_OMP_SECTIONS:
36142 strcpy (p_name, "#pragma omp");
36143 stmt = cp_parser_omp_sections (parser, pragma_tok, p_name, mask, NULL);
36144 break;
36145 case PRAGMA_OMP_SIMD:
36146 strcpy (p_name, "#pragma omp");
36147 stmt = cp_parser_omp_simd (parser, pragma_tok, p_name, mask, NULL);
36148 break;
36149 case PRAGMA_OMP_SINGLE:
36150 stmt = cp_parser_omp_single (parser, pragma_tok);
36151 break;
36152 case PRAGMA_OMP_TASK:
36153 stmt = cp_parser_omp_task (parser, pragma_tok);
36154 break;
36155 case PRAGMA_OMP_TASKGROUP:
36156 stmt = cp_parser_omp_taskgroup (parser, pragma_tok);
36157 break;
36158 case PRAGMA_OMP_TASKLOOP:
36159 strcpy (p_name, "#pragma omp");
36160 stmt = cp_parser_omp_taskloop (parser, pragma_tok, p_name, mask, NULL);
36161 break;
36162 case PRAGMA_OMP_TEAMS:
36163 strcpy (p_name, "#pragma omp");
36164 stmt = cp_parser_omp_teams (parser, pragma_tok, p_name, mask, NULL);
36165 break;
36166 default:
36167 gcc_unreachable ();
36168 }
36169
36170 protected_set_expr_location (stmt, pragma_tok->location);
36171 }
36172 \f
36173 /* Transactional Memory parsing routines. */
36174
36175 /* Parse a transaction attribute.
36176
36177 txn-attribute:
36178 attribute
36179 [ [ identifier ] ]
36180
36181 We use this instead of cp_parser_attributes_opt for transactions to avoid
36182 the pedwarn in C++98 mode. */
36183
36184 static tree
36185 cp_parser_txn_attribute_opt (cp_parser *parser)
36186 {
36187 cp_token *token;
36188 tree attr_name, attr = NULL;
36189
36190 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
36191 return cp_parser_attributes_opt (parser);
36192
36193 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
36194 return NULL_TREE;
36195 cp_lexer_consume_token (parser->lexer);
36196 if (!cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE))
36197 goto error1;
36198
36199 token = cp_lexer_peek_token (parser->lexer);
36200 if (token->type == CPP_NAME || token->type == CPP_KEYWORD)
36201 {
36202 token = cp_lexer_consume_token (parser->lexer);
36203
36204 attr_name = (token->type == CPP_KEYWORD
36205 /* For keywords, use the canonical spelling,
36206 not the parsed identifier. */
36207 ? ridpointers[(int) token->keyword]
36208 : token->u.value);
36209 attr = build_tree_list (attr_name, NULL_TREE);
36210 }
36211 else
36212 cp_parser_error (parser, "expected identifier");
36213
36214 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
36215 error1:
36216 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
36217 return attr;
36218 }
36219
36220 /* Parse a __transaction_atomic or __transaction_relaxed statement.
36221
36222 transaction-statement:
36223 __transaction_atomic txn-attribute[opt] txn-noexcept-spec[opt]
36224 compound-statement
36225 __transaction_relaxed txn-noexcept-spec[opt] compound-statement
36226 */
36227
36228 static tree
36229 cp_parser_transaction (cp_parser *parser, cp_token *token)
36230 {
36231 unsigned char old_in = parser->in_transaction;
36232 unsigned char this_in = 1, new_in;
36233 enum rid keyword = token->keyword;
36234 tree stmt, attrs, noex;
36235
36236 cp_lexer_consume_token (parser->lexer);
36237
36238 if (keyword == RID_TRANSACTION_RELAXED
36239 || keyword == RID_SYNCHRONIZED)
36240 this_in |= TM_STMT_ATTR_RELAXED;
36241 else
36242 {
36243 attrs = cp_parser_txn_attribute_opt (parser);
36244 if (attrs)
36245 this_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
36246 }
36247
36248 /* Parse a noexcept specification. */
36249 if (keyword == RID_ATOMIC_NOEXCEPT)
36250 noex = boolean_true_node;
36251 else if (keyword == RID_ATOMIC_CANCEL)
36252 {
36253 /* cancel-and-throw is unimplemented. */
36254 sorry ("atomic_cancel");
36255 noex = NULL_TREE;
36256 }
36257 else
36258 noex = cp_parser_noexcept_specification_opt (parser, true, NULL, true);
36259
36260 /* Keep track if we're in the lexical scope of an outer transaction. */
36261 new_in = this_in | (old_in & TM_STMT_ATTR_OUTER);
36262
36263 stmt = begin_transaction_stmt (token->location, NULL, this_in);
36264
36265 parser->in_transaction = new_in;
36266 cp_parser_compound_statement (parser, NULL, BCS_TRANSACTION, false);
36267 parser->in_transaction = old_in;
36268
36269 finish_transaction_stmt (stmt, NULL, this_in, noex);
36270
36271 return stmt;
36272 }
36273
36274 /* Parse a __transaction_atomic or __transaction_relaxed expression.
36275
36276 transaction-expression:
36277 __transaction_atomic txn-noexcept-spec[opt] ( expression )
36278 __transaction_relaxed txn-noexcept-spec[opt] ( expression )
36279 */
36280
36281 static tree
36282 cp_parser_transaction_expression (cp_parser *parser, enum rid keyword)
36283 {
36284 unsigned char old_in = parser->in_transaction;
36285 unsigned char this_in = 1;
36286 cp_token *token;
36287 tree expr, noex;
36288 bool noex_expr;
36289
36290 gcc_assert (keyword == RID_TRANSACTION_ATOMIC
36291 || keyword == RID_TRANSACTION_RELAXED);
36292
36293 if (!flag_tm)
36294 error (keyword == RID_TRANSACTION_RELAXED
36295 ? G_("%<__transaction_relaxed%> without transactional memory "
36296 "support enabled")
36297 : G_("%<__transaction_atomic%> without transactional memory "
36298 "support enabled"));
36299
36300 token = cp_parser_require_keyword (parser, keyword,
36301 (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
36302 : RT_TRANSACTION_RELAXED));
36303 gcc_assert (token != NULL);
36304
36305 if (keyword == RID_TRANSACTION_RELAXED)
36306 this_in |= TM_STMT_ATTR_RELAXED;
36307
36308 /* Set this early. This might mean that we allow transaction_cancel in
36309 an expression that we find out later actually has to be a constexpr.
36310 However, we expect that cxx_constant_value will be able to deal with
36311 this; also, if the noexcept has no constexpr, then what we parse next
36312 really is a transaction's body. */
36313 parser->in_transaction = this_in;
36314
36315 /* Parse a noexcept specification. */
36316 noex = cp_parser_noexcept_specification_opt (parser, false, &noex_expr,
36317 true);
36318
36319 if (!noex || !noex_expr
36320 || cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
36321 {
36322 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
36323
36324 expr = cp_parser_expression (parser);
36325 expr = finish_parenthesized_expr (expr);
36326
36327 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
36328 }
36329 else
36330 {
36331 /* The only expression that is available got parsed for the noexcept
36332 already. noexcept is true then. */
36333 expr = noex;
36334 noex = boolean_true_node;
36335 }
36336
36337 expr = build_transaction_expr (token->location, expr, this_in, noex);
36338 parser->in_transaction = old_in;
36339
36340 if (cp_parser_non_integral_constant_expression (parser, NIC_TRANSACTION))
36341 return error_mark_node;
36342
36343 return (flag_tm ? expr : error_mark_node);
36344 }
36345
36346 /* Parse a function-transaction-block.
36347
36348 function-transaction-block:
36349 __transaction_atomic txn-attribute[opt] ctor-initializer[opt]
36350 function-body
36351 __transaction_atomic txn-attribute[opt] function-try-block
36352 __transaction_relaxed ctor-initializer[opt] function-body
36353 __transaction_relaxed function-try-block
36354 */
36355
36356 static bool
36357 cp_parser_function_transaction (cp_parser *parser, enum rid keyword)
36358 {
36359 unsigned char old_in = parser->in_transaction;
36360 unsigned char new_in = 1;
36361 tree compound_stmt, stmt, attrs;
36362 bool ctor_initializer_p;
36363 cp_token *token;
36364
36365 gcc_assert (keyword == RID_TRANSACTION_ATOMIC
36366 || keyword == RID_TRANSACTION_RELAXED);
36367 token = cp_parser_require_keyword (parser, keyword,
36368 (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
36369 : RT_TRANSACTION_RELAXED));
36370 gcc_assert (token != NULL);
36371
36372 if (keyword == RID_TRANSACTION_RELAXED)
36373 new_in |= TM_STMT_ATTR_RELAXED;
36374 else
36375 {
36376 attrs = cp_parser_txn_attribute_opt (parser);
36377 if (attrs)
36378 new_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
36379 }
36380
36381 stmt = begin_transaction_stmt (token->location, &compound_stmt, new_in);
36382
36383 parser->in_transaction = new_in;
36384
36385 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
36386 ctor_initializer_p = cp_parser_function_try_block (parser);
36387 else
36388 ctor_initializer_p = cp_parser_ctor_initializer_opt_and_function_body
36389 (parser, /*in_function_try_block=*/false);
36390
36391 parser->in_transaction = old_in;
36392
36393 finish_transaction_stmt (stmt, compound_stmt, new_in, NULL_TREE);
36394
36395 return ctor_initializer_p;
36396 }
36397
36398 /* Parse a __transaction_cancel statement.
36399
36400 cancel-statement:
36401 __transaction_cancel txn-attribute[opt] ;
36402 __transaction_cancel txn-attribute[opt] throw-expression ;
36403
36404 ??? Cancel and throw is not yet implemented. */
36405
36406 static tree
36407 cp_parser_transaction_cancel (cp_parser *parser)
36408 {
36409 cp_token *token;
36410 bool is_outer = false;
36411 tree stmt, attrs;
36412
36413 token = cp_parser_require_keyword (parser, RID_TRANSACTION_CANCEL,
36414 RT_TRANSACTION_CANCEL);
36415 gcc_assert (token != NULL);
36416
36417 attrs = cp_parser_txn_attribute_opt (parser);
36418 if (attrs)
36419 is_outer = (parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER) != 0);
36420
36421 /* ??? Parse cancel-and-throw here. */
36422
36423 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
36424
36425 if (!flag_tm)
36426 {
36427 error_at (token->location, "%<__transaction_cancel%> without "
36428 "transactional memory support enabled");
36429 return error_mark_node;
36430 }
36431 else if (parser->in_transaction & TM_STMT_ATTR_RELAXED)
36432 {
36433 error_at (token->location, "%<__transaction_cancel%> within a "
36434 "%<__transaction_relaxed%>");
36435 return error_mark_node;
36436 }
36437 else if (is_outer)
36438 {
36439 if ((parser->in_transaction & TM_STMT_ATTR_OUTER) == 0
36440 && !is_tm_may_cancel_outer (current_function_decl))
36441 {
36442 error_at (token->location, "outer %<__transaction_cancel%> not "
36443 "within outer %<__transaction_atomic%>");
36444 error_at (token->location,
36445 " or a %<transaction_may_cancel_outer%> function");
36446 return error_mark_node;
36447 }
36448 }
36449 else if (parser->in_transaction == 0)
36450 {
36451 error_at (token->location, "%<__transaction_cancel%> not within "
36452 "%<__transaction_atomic%>");
36453 return error_mark_node;
36454 }
36455
36456 stmt = build_tm_abort_call (token->location, is_outer);
36457 add_stmt (stmt);
36458
36459 return stmt;
36460 }
36461 \f
36462 /* The parser. */
36463
36464 static GTY (()) cp_parser *the_parser;
36465
36466 \f
36467 /* Special handling for the first token or line in the file. The first
36468 thing in the file might be #pragma GCC pch_preprocess, which loads a
36469 PCH file, which is a GC collection point. So we need to handle this
36470 first pragma without benefit of an existing lexer structure.
36471
36472 Always returns one token to the caller in *FIRST_TOKEN. This is
36473 either the true first token of the file, or the first token after
36474 the initial pragma. */
36475
36476 static void
36477 cp_parser_initial_pragma (cp_token *first_token)
36478 {
36479 tree name = NULL;
36480
36481 cp_lexer_get_preprocessor_token (NULL, first_token);
36482 if (first_token->pragma_kind != PRAGMA_GCC_PCH_PREPROCESS)
36483 return;
36484
36485 cp_lexer_get_preprocessor_token (NULL, first_token);
36486 if (first_token->type == CPP_STRING)
36487 {
36488 name = first_token->u.value;
36489
36490 cp_lexer_get_preprocessor_token (NULL, first_token);
36491 if (first_token->type != CPP_PRAGMA_EOL)
36492 error_at (first_token->location,
36493 "junk at end of %<#pragma GCC pch_preprocess%>");
36494 }
36495 else
36496 error_at (first_token->location, "expected string literal");
36497
36498 /* Skip to the end of the pragma. */
36499 while (first_token->type != CPP_PRAGMA_EOL && first_token->type != CPP_EOF)
36500 cp_lexer_get_preprocessor_token (NULL, first_token);
36501
36502 /* Now actually load the PCH file. */
36503 if (name)
36504 c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
36505
36506 /* Read one more token to return to our caller. We have to do this
36507 after reading the PCH file in, since its pointers have to be
36508 live. */
36509 cp_lexer_get_preprocessor_token (NULL, first_token);
36510 }
36511
36512 /* Parses the grainsize pragma for the _Cilk_for statement.
36513 Syntax:
36514 #pragma cilk grainsize = <VALUE>. */
36515
36516 static void
36517 cp_parser_cilk_grainsize (cp_parser *parser, cp_token *pragma_tok)
36518 {
36519 if (cp_parser_require (parser, CPP_EQ, RT_EQ))
36520 {
36521 tree exp = cp_parser_binary_expression (parser, false, false,
36522 PREC_NOT_OPERATOR, NULL);
36523 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36524 if (!exp || exp == error_mark_node)
36525 {
36526 error_at (pragma_tok->location, "invalid grainsize for _Cilk_for");
36527 return;
36528 }
36529
36530 /* Make sure the next token is _Cilk_for, it is invalid otherwise. */
36531 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CILK_FOR))
36532 cp_parser_cilk_for (parser, exp);
36533 else
36534 warning_at (cp_lexer_peek_token (parser->lexer)->location, 0,
36535 "%<#pragma cilk grainsize%> is not followed by "
36536 "%<_Cilk_for%>");
36537 return;
36538 }
36539 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36540 }
36541
36542 /* Normal parsing of a pragma token. Here we can (and must) use the
36543 regular lexer. */
36544
36545 static bool
36546 cp_parser_pragma (cp_parser *parser, enum pragma_context context)
36547 {
36548 cp_token *pragma_tok;
36549 unsigned int id;
36550 tree stmt;
36551 bool ret;
36552
36553 pragma_tok = cp_lexer_consume_token (parser->lexer);
36554 gcc_assert (pragma_tok->type == CPP_PRAGMA);
36555 parser->lexer->in_pragma = true;
36556
36557 id = pragma_tok->pragma_kind;
36558 if (id != PRAGMA_OMP_DECLARE_REDUCTION && id != PRAGMA_OACC_ROUTINE)
36559 cp_ensure_no_omp_declare_simd (parser);
36560 switch (id)
36561 {
36562 case PRAGMA_GCC_PCH_PREPROCESS:
36563 error_at (pragma_tok->location,
36564 "%<#pragma GCC pch_preprocess%> must be first");
36565 break;
36566
36567 case PRAGMA_OMP_BARRIER:
36568 switch (context)
36569 {
36570 case pragma_compound:
36571 cp_parser_omp_barrier (parser, pragma_tok);
36572 return false;
36573 case pragma_stmt:
36574 error_at (pragma_tok->location, "%<#pragma omp barrier%> may only be "
36575 "used in compound statements");
36576 break;
36577 default:
36578 goto bad_stmt;
36579 }
36580 break;
36581
36582 case PRAGMA_OMP_FLUSH:
36583 switch (context)
36584 {
36585 case pragma_compound:
36586 cp_parser_omp_flush (parser, pragma_tok);
36587 return false;
36588 case pragma_stmt:
36589 error_at (pragma_tok->location, "%<#pragma omp flush%> may only be "
36590 "used in compound statements");
36591 break;
36592 default:
36593 goto bad_stmt;
36594 }
36595 break;
36596
36597 case PRAGMA_OMP_TASKWAIT:
36598 switch (context)
36599 {
36600 case pragma_compound:
36601 cp_parser_omp_taskwait (parser, pragma_tok);
36602 return false;
36603 case pragma_stmt:
36604 error_at (pragma_tok->location,
36605 "%<#pragma omp taskwait%> may only be "
36606 "used in compound statements");
36607 break;
36608 default:
36609 goto bad_stmt;
36610 }
36611 break;
36612
36613 case PRAGMA_OMP_TASKYIELD:
36614 switch (context)
36615 {
36616 case pragma_compound:
36617 cp_parser_omp_taskyield (parser, pragma_tok);
36618 return false;
36619 case pragma_stmt:
36620 error_at (pragma_tok->location,
36621 "%<#pragma omp taskyield%> may only be "
36622 "used in compound statements");
36623 break;
36624 default:
36625 goto bad_stmt;
36626 }
36627 break;
36628
36629 case PRAGMA_OMP_CANCEL:
36630 switch (context)
36631 {
36632 case pragma_compound:
36633 cp_parser_omp_cancel (parser, pragma_tok);
36634 return false;
36635 case pragma_stmt:
36636 error_at (pragma_tok->location,
36637 "%<#pragma omp cancel%> may only be "
36638 "used in compound statements");
36639 break;
36640 default:
36641 goto bad_stmt;
36642 }
36643 break;
36644
36645 case PRAGMA_OMP_CANCELLATION_POINT:
36646 switch (context)
36647 {
36648 case pragma_compound:
36649 cp_parser_omp_cancellation_point (parser, pragma_tok);
36650 return false;
36651 case pragma_stmt:
36652 error_at (pragma_tok->location,
36653 "%<#pragma omp cancellation point%> may only be "
36654 "used in compound statements");
36655 break;
36656 default:
36657 goto bad_stmt;
36658 }
36659 break;
36660
36661 case PRAGMA_OMP_THREADPRIVATE:
36662 cp_parser_omp_threadprivate (parser, pragma_tok);
36663 return false;
36664
36665 case PRAGMA_OMP_DECLARE_REDUCTION:
36666 cp_parser_omp_declare (parser, pragma_tok, context);
36667 return false;
36668
36669 case PRAGMA_OACC_DECLARE:
36670 cp_parser_oacc_declare (parser, pragma_tok);
36671 return false;
36672
36673 case PRAGMA_OACC_ROUTINE:
36674 cp_parser_oacc_routine (parser, pragma_tok, context);
36675 return false;
36676
36677 case PRAGMA_OACC_ATOMIC:
36678 case PRAGMA_OACC_CACHE:
36679 case PRAGMA_OACC_DATA:
36680 case PRAGMA_OACC_ENTER_DATA:
36681 case PRAGMA_OACC_EXIT_DATA:
36682 case PRAGMA_OACC_HOST_DATA:
36683 case PRAGMA_OACC_KERNELS:
36684 case PRAGMA_OACC_PARALLEL:
36685 case PRAGMA_OACC_LOOP:
36686 case PRAGMA_OACC_UPDATE:
36687 case PRAGMA_OACC_WAIT:
36688 case PRAGMA_OMP_ATOMIC:
36689 case PRAGMA_OMP_CRITICAL:
36690 case PRAGMA_OMP_DISTRIBUTE:
36691 case PRAGMA_OMP_FOR:
36692 case PRAGMA_OMP_MASTER:
36693 case PRAGMA_OMP_PARALLEL:
36694 case PRAGMA_OMP_SECTIONS:
36695 case PRAGMA_OMP_SIMD:
36696 case PRAGMA_OMP_SINGLE:
36697 case PRAGMA_OMP_TASK:
36698 case PRAGMA_OMP_TASKGROUP:
36699 case PRAGMA_OMP_TASKLOOP:
36700 case PRAGMA_OMP_TEAMS:
36701 if (context != pragma_stmt && context != pragma_compound)
36702 goto bad_stmt;
36703 stmt = push_omp_privatization_clauses (false);
36704 cp_parser_omp_construct (parser, pragma_tok);
36705 pop_omp_privatization_clauses (stmt);
36706 return true;
36707
36708 case PRAGMA_OMP_ORDERED:
36709 stmt = push_omp_privatization_clauses (false);
36710 ret = cp_parser_omp_ordered (parser, pragma_tok, context);
36711 pop_omp_privatization_clauses (stmt);
36712 return ret;
36713
36714 case PRAGMA_OMP_TARGET:
36715 stmt = push_omp_privatization_clauses (false);
36716 ret = cp_parser_omp_target (parser, pragma_tok, context);
36717 pop_omp_privatization_clauses (stmt);
36718 return ret;
36719
36720 case PRAGMA_OMP_END_DECLARE_TARGET:
36721 cp_parser_omp_end_declare_target (parser, pragma_tok);
36722 return false;
36723
36724 case PRAGMA_OMP_SECTION:
36725 error_at (pragma_tok->location,
36726 "%<#pragma omp section%> may only be used in "
36727 "%<#pragma omp sections%> construct");
36728 break;
36729
36730 case PRAGMA_IVDEP:
36731 {
36732 if (context == pragma_external)
36733 {
36734 error_at (pragma_tok->location,
36735 "%<#pragma GCC ivdep%> must be inside a function");
36736 break;
36737 }
36738 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36739 cp_token *tok;
36740 tok = cp_lexer_peek_token (the_parser->lexer);
36741 if (tok->type != CPP_KEYWORD
36742 || (tok->keyword != RID_FOR && tok->keyword != RID_WHILE
36743 && tok->keyword != RID_DO))
36744 {
36745 cp_parser_error (parser, "for, while or do statement expected");
36746 return false;
36747 }
36748 cp_parser_iteration_statement (parser, true);
36749 return true;
36750 }
36751
36752 case PRAGMA_CILK_SIMD:
36753 if (context == pragma_external)
36754 {
36755 error_at (pragma_tok->location,
36756 "%<#pragma simd%> must be inside a function");
36757 break;
36758 }
36759 stmt = push_omp_privatization_clauses (false);
36760 cp_parser_cilk_simd (parser, pragma_tok);
36761 pop_omp_privatization_clauses (stmt);
36762 return true;
36763
36764 case PRAGMA_CILK_GRAINSIZE:
36765 if (context == pragma_external)
36766 {
36767 error_at (pragma_tok->location,
36768 "%<#pragma cilk grainsize%> must be inside a function");
36769 break;
36770 }
36771
36772 /* Ignore the pragma if Cilk Plus is not enabled. */
36773 if (flag_cilkplus)
36774 {
36775 cp_parser_cilk_grainsize (parser, pragma_tok);
36776 return true;
36777 }
36778 else
36779 {
36780 error_at (pragma_tok->location, "-fcilkplus must be enabled to use "
36781 "%<#pragma cilk grainsize%>");
36782 break;
36783 }
36784
36785 default:
36786 gcc_assert (id >= PRAGMA_FIRST_EXTERNAL);
36787 c_invoke_pragma_handler (id);
36788 break;
36789
36790 bad_stmt:
36791 cp_parser_error (parser, "expected declaration specifiers");
36792 break;
36793 }
36794
36795 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36796 return false;
36797 }
36798
36799 /* The interface the pragma parsers have to the lexer. */
36800
36801 enum cpp_ttype
36802 pragma_lex (tree *value, location_t *loc)
36803 {
36804 cp_token *tok = cp_lexer_peek_token (the_parser->lexer);
36805 enum cpp_ttype ret = tok->type;
36806
36807 *value = tok->u.value;
36808 if (loc)
36809 *loc = tok->location;
36810
36811 if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
36812 ret = CPP_EOF;
36813 else if (ret == CPP_STRING)
36814 *value = cp_parser_string_literal (the_parser, false, false);
36815 else
36816 {
36817 if (ret == CPP_KEYWORD)
36818 ret = CPP_NAME;
36819 cp_lexer_consume_token (the_parser->lexer);
36820 }
36821
36822 return ret;
36823 }
36824
36825 \f
36826 /* External interface. */
36827
36828 /* Parse one entire translation unit. */
36829
36830 void
36831 c_parse_file (void)
36832 {
36833 static bool already_called = false;
36834
36835 if (already_called)
36836 fatal_error (input_location,
36837 "inter-module optimizations not implemented for C++");
36838 already_called = true;
36839
36840 the_parser = cp_parser_new ();
36841 push_deferring_access_checks (flag_access_control
36842 ? dk_no_deferred : dk_no_check);
36843 cp_parser_translation_unit (the_parser);
36844 the_parser = NULL;
36845 }
36846
36847 /* Parses the Cilk Plus #pragma simd and SIMD-enabled function attribute's
36848 vectorlength clause:
36849 Syntax:
36850 vectorlength ( constant-expression ) */
36851
36852 static tree
36853 cp_parser_cilk_simd_vectorlength (cp_parser *parser, tree clauses,
36854 bool is_simd_fn)
36855 {
36856 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
36857 tree expr;
36858 /* The vectorlength clause in #pragma simd behaves exactly like OpenMP's
36859 safelen clause. Thus, vectorlength is represented as OMP 4.0
36860 safelen. For SIMD-enabled function it is represented by OMP 4.0
36861 simdlen. */
36862 if (!is_simd_fn)
36863 check_no_duplicate_clause (clauses, OMP_CLAUSE_SAFELEN, "vectorlength",
36864 loc);
36865 else
36866 check_no_duplicate_clause (clauses, OMP_CLAUSE_SIMDLEN, "vectorlength",
36867 loc);
36868
36869 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
36870 return error_mark_node;
36871
36872 expr = cp_parser_constant_expression (parser);
36873 expr = maybe_constant_value (expr);
36874
36875 /* If expr == error_mark_node, then don't emit any errors nor
36876 create a clause. if any of the above functions returns
36877 error mark node then they would have emitted an error message. */
36878 if (expr == error_mark_node)
36879 ;
36880 else if (!TREE_TYPE (expr)
36881 || !TREE_CONSTANT (expr)
36882 || !INTEGRAL_TYPE_P (TREE_TYPE (expr)))
36883 error_at (loc, "vectorlength must be an integer constant");
36884 else if (TREE_CONSTANT (expr)
36885 && exact_log2 (TREE_INT_CST_LOW (expr)) == -1)
36886 error_at (loc, "vectorlength must be a power of 2");
36887 else
36888 {
36889 tree c;
36890 if (!is_simd_fn)
36891 {
36892 c = build_omp_clause (loc, OMP_CLAUSE_SAFELEN);
36893 OMP_CLAUSE_SAFELEN_EXPR (c) = expr;
36894 OMP_CLAUSE_CHAIN (c) = clauses;
36895 clauses = c;
36896 }
36897 else
36898 {
36899 c = build_omp_clause (loc, OMP_CLAUSE_SIMDLEN);
36900 OMP_CLAUSE_SIMDLEN_EXPR (c) = expr;
36901 OMP_CLAUSE_CHAIN (c) = clauses;
36902 clauses = c;
36903 }
36904 }
36905
36906 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
36907 return error_mark_node;
36908 return clauses;
36909 }
36910
36911 /* Handles the Cilk Plus #pragma simd linear clause.
36912 Syntax:
36913 linear ( simd-linear-variable-list )
36914
36915 simd-linear-variable-list:
36916 simd-linear-variable
36917 simd-linear-variable-list , simd-linear-variable
36918
36919 simd-linear-variable:
36920 id-expression
36921 id-expression : simd-linear-step
36922
36923 simd-linear-step:
36924 conditional-expression */
36925
36926 static tree
36927 cp_parser_cilk_simd_linear (cp_parser *parser, tree clauses)
36928 {
36929 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
36930
36931 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
36932 return clauses;
36933 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
36934 {
36935 cp_parser_error (parser, "expected identifier");
36936 cp_parser_skip_to_closing_parenthesis (parser, false, false, true);
36937 return error_mark_node;
36938 }
36939
36940 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
36941 parser->colon_corrects_to_scope_p = false;
36942 while (1)
36943 {
36944 cp_token *token = cp_lexer_peek_token (parser->lexer);
36945 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
36946 {
36947 cp_parser_error (parser, "expected variable-name");
36948 clauses = error_mark_node;
36949 break;
36950 }
36951
36952 tree var_name = cp_parser_id_expression (parser, false, true, NULL,
36953 false, false);
36954 tree decl = cp_parser_lookup_name_simple (parser, var_name,
36955 token->location);
36956 if (decl == error_mark_node)
36957 {
36958 cp_parser_name_lookup_error (parser, var_name, decl, NLE_NULL,
36959 token->location);
36960 clauses = error_mark_node;
36961 }
36962 else
36963 {
36964 tree e = NULL_TREE;
36965 tree step_size = integer_one_node;
36966
36967 /* If present, parse the linear step. Otherwise, assume the default
36968 value of 1. */
36969 if (cp_lexer_peek_token (parser->lexer)->type == CPP_COLON)
36970 {
36971 cp_lexer_consume_token (parser->lexer);
36972
36973 e = cp_parser_assignment_expression (parser);
36974 e = maybe_constant_value (e);
36975
36976 if (e == error_mark_node)
36977 {
36978 /* If an error has occurred, then the whole pragma is
36979 considered ill-formed. Thus, no reason to keep
36980 parsing. */
36981 clauses = error_mark_node;
36982 break;
36983 }
36984 else if (type_dependent_expression_p (e)
36985 || value_dependent_expression_p (e)
36986 || (TREE_TYPE (e)
36987 && INTEGRAL_TYPE_P (TREE_TYPE (e))
36988 && (TREE_CONSTANT (e)
36989 || DECL_P (e))))
36990 step_size = e;
36991 else
36992 cp_parser_error (parser,
36993 "step size must be an integer constant "
36994 "expression or an integer variable");
36995 }
36996
36997 /* Use the OMP_CLAUSE_LINEAR, which has the same semantics. */
36998 tree l = build_omp_clause (loc, OMP_CLAUSE_LINEAR);
36999 OMP_CLAUSE_DECL (l) = decl;
37000 OMP_CLAUSE_LINEAR_STEP (l) = step_size;
37001 OMP_CLAUSE_CHAIN (l) = clauses;
37002 clauses = l;
37003 }
37004 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
37005 cp_lexer_consume_token (parser->lexer);
37006 else if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
37007 break;
37008 else
37009 {
37010 error_at (cp_lexer_peek_token (parser->lexer)->location,
37011 "expected %<,%> or %<)%> after %qE", decl);
37012 clauses = error_mark_node;
37013 break;
37014 }
37015 }
37016 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
37017 cp_parser_skip_to_closing_parenthesis (parser, false, false, true);
37018 return clauses;
37019 }
37020
37021 /* Returns the name of the next clause. If the clause is not
37022 recognized, then PRAGMA_CILK_CLAUSE_NONE is returned and the next
37023 token is not consumed. Otherwise, the appropriate enum from the
37024 pragma_simd_clause is returned and the token is consumed. */
37025
37026 static pragma_omp_clause
37027 cp_parser_cilk_simd_clause_name (cp_parser *parser)
37028 {
37029 pragma_omp_clause clause_type;
37030 cp_token *token = cp_lexer_peek_token (parser->lexer);
37031
37032 if (token->keyword == RID_PRIVATE)
37033 clause_type = PRAGMA_CILK_CLAUSE_PRIVATE;
37034 else if (!token->u.value || token->type != CPP_NAME)
37035 return PRAGMA_CILK_CLAUSE_NONE;
37036 else if (!strcmp (IDENTIFIER_POINTER (token->u.value), "vectorlength"))
37037 clause_type = PRAGMA_CILK_CLAUSE_VECTORLENGTH;
37038 else if (!strcmp (IDENTIFIER_POINTER (token->u.value), "linear"))
37039 clause_type = PRAGMA_CILK_CLAUSE_LINEAR;
37040 else if (!strcmp (IDENTIFIER_POINTER (token->u.value), "firstprivate"))
37041 clause_type = PRAGMA_CILK_CLAUSE_FIRSTPRIVATE;
37042 else if (!strcmp (IDENTIFIER_POINTER (token->u.value), "lastprivate"))
37043 clause_type = PRAGMA_CILK_CLAUSE_LASTPRIVATE;
37044 else if (!strcmp (IDENTIFIER_POINTER (token->u.value), "reduction"))
37045 clause_type = PRAGMA_CILK_CLAUSE_REDUCTION;
37046 else
37047 return PRAGMA_CILK_CLAUSE_NONE;
37048
37049 cp_lexer_consume_token (parser->lexer);
37050 return clause_type;
37051 }
37052
37053 /* Parses all the #pragma simd clauses. Returns a list of clauses found. */
37054
37055 static tree
37056 cp_parser_cilk_simd_all_clauses (cp_parser *parser, cp_token *pragma_token)
37057 {
37058 tree clauses = NULL_TREE;
37059
37060 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
37061 && clauses != error_mark_node)
37062 {
37063 pragma_omp_clause c_kind;
37064 c_kind = cp_parser_cilk_simd_clause_name (parser);
37065 if (c_kind == PRAGMA_CILK_CLAUSE_VECTORLENGTH)
37066 clauses = cp_parser_cilk_simd_vectorlength (parser, clauses, false);
37067 else if (c_kind == PRAGMA_CILK_CLAUSE_LINEAR)
37068 clauses = cp_parser_cilk_simd_linear (parser, clauses);
37069 else if (c_kind == PRAGMA_CILK_CLAUSE_PRIVATE)
37070 /* Use the OpenMP 4.0 equivalent function. */
37071 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE, clauses);
37072 else if (c_kind == PRAGMA_CILK_CLAUSE_FIRSTPRIVATE)
37073 /* Use the OpenMP 4.0 equivalent function. */
37074 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
37075 clauses);
37076 else if (c_kind == PRAGMA_CILK_CLAUSE_LASTPRIVATE)
37077 /* Use the OMP 4.0 equivalent function. */
37078 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LASTPRIVATE,
37079 clauses);
37080 else if (c_kind == PRAGMA_CILK_CLAUSE_REDUCTION)
37081 /* Use the OMP 4.0 equivalent function. */
37082 clauses = cp_parser_omp_clause_reduction (parser, clauses);
37083 else
37084 {
37085 clauses = error_mark_node;
37086 cp_parser_error (parser, "expected %<#pragma simd%> clause");
37087 break;
37088 }
37089 }
37090
37091 cp_parser_skip_to_pragma_eol (parser, pragma_token);
37092
37093 if (clauses == error_mark_node)
37094 return error_mark_node;
37095 else
37096 return c_finish_cilk_clauses (clauses);
37097 }
37098
37099 /* Main entry-point for parsing Cilk Plus <#pragma simd> for loops. */
37100
37101 static void
37102 cp_parser_cilk_simd (cp_parser *parser, cp_token *pragma_token)
37103 {
37104 tree clauses = cp_parser_cilk_simd_all_clauses (parser, pragma_token);
37105
37106 if (clauses == error_mark_node)
37107 return;
37108
37109 if (cp_lexer_next_token_is_not_keyword (parser->lexer, RID_FOR))
37110 {
37111 error_at (cp_lexer_peek_token (parser->lexer)->location,
37112 "for statement expected");
37113 return;
37114 }
37115
37116 tree sb = begin_omp_structured_block ();
37117 int save = cp_parser_begin_omp_structured_block (parser);
37118 tree ret = cp_parser_omp_for_loop (parser, CILK_SIMD, clauses, NULL);
37119 if (ret)
37120 cpp_validate_cilk_plus_loop (OMP_FOR_BODY (ret));
37121 cp_parser_end_omp_structured_block (parser, save);
37122 add_stmt (finish_omp_structured_block (sb));
37123 }
37124
37125 /* Main entry-point for parsing Cilk Plus _Cilk_for
37126 loops. The return value is error_mark_node
37127 when errors happen and CILK_FOR tree on success. */
37128
37129 static tree
37130 cp_parser_cilk_for (cp_parser *parser, tree grain)
37131 {
37132 if (cp_lexer_next_token_is_not_keyword (parser->lexer, RID_CILK_FOR))
37133 gcc_unreachable ();
37134
37135 tree sb = begin_omp_structured_block ();
37136 int save = cp_parser_begin_omp_structured_block (parser);
37137
37138 tree clauses = build_omp_clause (EXPR_LOCATION (grain), OMP_CLAUSE_SCHEDULE);
37139 OMP_CLAUSE_SCHEDULE_KIND (clauses) = OMP_CLAUSE_SCHEDULE_CILKFOR;
37140 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (clauses) = grain;
37141 clauses = finish_omp_clauses (clauses, false);
37142
37143 tree ret = cp_parser_omp_for_loop (parser, CILK_FOR, clauses, NULL);
37144 if (ret)
37145 cpp_validate_cilk_plus_loop (ret);
37146 else
37147 ret = error_mark_node;
37148
37149 cp_parser_end_omp_structured_block (parser, save);
37150 add_stmt (finish_omp_structured_block (sb));
37151 return ret;
37152 }
37153
37154 /* Create an identifier for a generic parameter type (a synthesized
37155 template parameter implied by `auto' or a concept identifier). */
37156
37157 static GTY(()) int generic_parm_count;
37158 static tree
37159 make_generic_type_name ()
37160 {
37161 char buf[32];
37162 sprintf (buf, "auto:%d", ++generic_parm_count);
37163 return get_identifier (buf);
37164 }
37165
37166 /* Predicate that behaves as is_auto_or_concept but matches the parent
37167 node of the generic type rather than the generic type itself. This
37168 allows for type transformation in add_implicit_template_parms. */
37169
37170 static inline bool
37171 tree_type_is_auto_or_concept (const_tree t)
37172 {
37173 return TREE_TYPE (t) && is_auto_or_concept (TREE_TYPE (t));
37174 }
37175
37176 /* Add an implicit template type parameter to the CURRENT_TEMPLATE_PARMS
37177 (creating a new template parameter list if necessary). Returns the newly
37178 created template type parm. */
37179
37180 static tree
37181 synthesize_implicit_template_parm (cp_parser *parser, tree constr)
37182 {
37183 gcc_assert (current_binding_level->kind == sk_function_parms);
37184
37185 /* Before committing to modifying any scope, if we're in an
37186 implicit template scope, and we're trying to synthesize a
37187 constrained parameter, try to find a previous parameter with
37188 the same name. This is the same-type rule for abbreviated
37189 function templates. */
37190 if (parser->implicit_template_scope && constr)
37191 {
37192 tree t = parser->implicit_template_parms;
37193 while (t)
37194 {
37195 if (equivalent_placeholder_constraints (TREE_TYPE (t), constr))
37196 {
37197 tree d = TREE_VALUE (t);
37198 if (TREE_CODE (d) == PARM_DECL)
37199 /* Return the TEMPLATE_PARM_INDEX. */
37200 d = DECL_INITIAL (d);
37201 return d;
37202 }
37203 t = TREE_CHAIN (t);
37204 }
37205 }
37206
37207 /* We are either continuing a function template that already contains implicit
37208 template parameters, creating a new fully-implicit function template, or
37209 extending an existing explicit function template with implicit template
37210 parameters. */
37211
37212 cp_binding_level *const entry_scope = current_binding_level;
37213
37214 bool become_template = false;
37215 cp_binding_level *parent_scope = 0;
37216
37217 if (parser->implicit_template_scope)
37218 {
37219 gcc_assert (parser->implicit_template_parms);
37220
37221 current_binding_level = parser->implicit_template_scope;
37222 }
37223 else
37224 {
37225 /* Roll back to the existing template parameter scope (in the case of
37226 extending an explicit function template) or introduce a new template
37227 parameter scope ahead of the function parameter scope (or class scope
37228 in the case of out-of-line member definitions). The function scope is
37229 added back after template parameter synthesis below. */
37230
37231 cp_binding_level *scope = entry_scope;
37232
37233 while (scope->kind == sk_function_parms)
37234 {
37235 parent_scope = scope;
37236 scope = scope->level_chain;
37237 }
37238 if (current_class_type && !LAMBDA_TYPE_P (current_class_type))
37239 {
37240 /* If not defining a class, then any class scope is a scope level in
37241 an out-of-line member definition. In this case simply wind back
37242 beyond the first such scope to inject the template parameter list.
37243 Otherwise wind back to the class being defined. The latter can
37244 occur in class member friend declarations such as:
37245
37246 class A {
37247 void foo (auto);
37248 };
37249 class B {
37250 friend void A::foo (auto);
37251 };
37252
37253 The template parameter list synthesized for the friend declaration
37254 must be injected in the scope of 'B'. This can also occur in
37255 erroneous cases such as:
37256
37257 struct A {
37258 struct B {
37259 void foo (auto);
37260 };
37261 void B::foo (auto) {}
37262 };
37263
37264 Here the attempted definition of 'B::foo' within 'A' is ill-formed
37265 but, nevertheless, the template parameter list synthesized for the
37266 declarator should be injected into the scope of 'A' as if the
37267 ill-formed template was specified explicitly. */
37268
37269 while (scope->kind == sk_class && !scope->defining_class_p)
37270 {
37271 parent_scope = scope;
37272 scope = scope->level_chain;
37273 }
37274 }
37275
37276 current_binding_level = scope;
37277
37278 if (scope->kind != sk_template_parms
37279 || !function_being_declared_is_template_p (parser))
37280 {
37281 /* Introduce a new template parameter list for implicit template
37282 parameters. */
37283
37284 become_template = true;
37285
37286 parser->implicit_template_scope
37287 = begin_scope (sk_template_parms, NULL);
37288
37289 ++processing_template_decl;
37290
37291 parser->fully_implicit_function_template_p = true;
37292 ++parser->num_template_parameter_lists;
37293 }
37294 else
37295 {
37296 /* Synthesize implicit template parameters at the end of the explicit
37297 template parameter list. */
37298
37299 gcc_assert (current_template_parms);
37300
37301 parser->implicit_template_scope = scope;
37302
37303 tree v = INNERMOST_TEMPLATE_PARMS (current_template_parms);
37304 parser->implicit_template_parms
37305 = TREE_VEC_ELT (v, TREE_VEC_LENGTH (v) - 1);
37306 }
37307 }
37308
37309 /* Synthesize a new template parameter and track the current template
37310 parameter chain with implicit_template_parms. */
37311
37312 tree proto = constr ? DECL_INITIAL (constr) : NULL_TREE;
37313 tree synth_id = make_generic_type_name ();
37314 tree synth_tmpl_parm;
37315 bool non_type = false;
37316
37317 if (proto == NULL_TREE || TREE_CODE (proto) == TYPE_DECL)
37318 synth_tmpl_parm
37319 = finish_template_type_parm (class_type_node, synth_id);
37320 else if (TREE_CODE (proto) == TEMPLATE_DECL)
37321 synth_tmpl_parm
37322 = finish_constrained_template_template_parm (proto, synth_id);
37323 else
37324 {
37325 synth_tmpl_parm = copy_decl (proto);
37326 DECL_NAME (synth_tmpl_parm) = synth_id;
37327 non_type = true;
37328 }
37329
37330 // Attach the constraint to the parm before processing.
37331 tree node = build_tree_list (NULL_TREE, synth_tmpl_parm);
37332 TREE_TYPE (node) = constr;
37333 tree new_parm
37334 = process_template_parm (parser->implicit_template_parms,
37335 input_location,
37336 node,
37337 /*non_type=*/non_type,
37338 /*param_pack=*/false);
37339
37340 // Chain the new parameter to the list of implicit parameters.
37341 if (parser->implicit_template_parms)
37342 parser->implicit_template_parms
37343 = TREE_CHAIN (parser->implicit_template_parms);
37344 else
37345 parser->implicit_template_parms = new_parm;
37346
37347 tree new_decl = getdecls ();
37348 if (non_type)
37349 /* Return the TEMPLATE_PARM_INDEX, not the PARM_DECL. */
37350 new_decl = DECL_INITIAL (new_decl);
37351
37352 /* If creating a fully implicit function template, start the new implicit
37353 template parameter list with this synthesized type, otherwise grow the
37354 current template parameter list. */
37355
37356 if (become_template)
37357 {
37358 parent_scope->level_chain = current_binding_level;
37359
37360 tree new_parms = make_tree_vec (1);
37361 TREE_VEC_ELT (new_parms, 0) = parser->implicit_template_parms;
37362 current_template_parms = tree_cons (size_int (processing_template_decl),
37363 new_parms, current_template_parms);
37364 }
37365 else
37366 {
37367 tree& new_parms = INNERMOST_TEMPLATE_PARMS (current_template_parms);
37368 int new_parm_idx = TREE_VEC_LENGTH (new_parms);
37369 new_parms = grow_tree_vec (new_parms, new_parm_idx + 1);
37370 TREE_VEC_ELT (new_parms, new_parm_idx) = parser->implicit_template_parms;
37371 }
37372
37373 // If the new parameter was constrained, we need to add that to the
37374 // constraints in the template parameter list.
37375 if (tree req = TEMPLATE_PARM_CONSTRAINTS (tree_last (new_parm)))
37376 {
37377 tree reqs = TEMPLATE_PARMS_CONSTRAINTS (current_template_parms);
37378 reqs = conjoin_constraints (reqs, req);
37379 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) = reqs;
37380 }
37381
37382 current_binding_level = entry_scope;
37383
37384 return new_decl;
37385 }
37386
37387 /* Finish the declaration of a fully implicit function template. Such a
37388 template has no explicit template parameter list so has not been through the
37389 normal template head and tail processing. synthesize_implicit_template_parm
37390 tries to do the head; this tries to do the tail. MEMBER_DECL_OPT should be
37391 provided if the declaration is a class member such that its template
37392 declaration can be completed. If MEMBER_DECL_OPT is provided the finished
37393 form is returned. Otherwise NULL_TREE is returned. */
37394
37395 static tree
37396 finish_fully_implicit_template (cp_parser *parser, tree member_decl_opt)
37397 {
37398 gcc_assert (parser->fully_implicit_function_template_p);
37399
37400 if (member_decl_opt && member_decl_opt != error_mark_node
37401 && DECL_VIRTUAL_P (member_decl_opt))
37402 {
37403 error_at (DECL_SOURCE_LOCATION (member_decl_opt),
37404 "implicit templates may not be %<virtual%>");
37405 DECL_VIRTUAL_P (member_decl_opt) = false;
37406 }
37407
37408 if (member_decl_opt)
37409 member_decl_opt = finish_member_template_decl (member_decl_opt);
37410 end_template_decl ();
37411
37412 parser->fully_implicit_function_template_p = false;
37413 --parser->num_template_parameter_lists;
37414
37415 return member_decl_opt;
37416 }
37417
37418 #include "gt-cp-parser.h"