2017-03-06 Vladimir Makarov <vmakarov@redhat.com>
[official-gcc.git] / gcc / cp / parser.c
blobe6848701150020f02cd683b271a8d32ba01540ea
1 /* -*- C++ -*- Parser.
2 Copyright (C) 2000-2017 Free Software Foundation, Inc.
3 Written by Mark Mitchell <mark@codesourcery.com>.
5 This file is part of GCC.
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.
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.
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/>. */
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 "gomp-constants.h"
39 #include "omp-general.h"
40 #include "omp-offload.h"
41 #include "c-family/c-indentation.h"
42 #include "context.h"
43 #include "cp-cilkplus.h"
44 #include "gcc-rich-location.h"
45 #include "tree-iterator.h"
48 /* The lexer. */
50 /* The cp_lexer_* routines mediate between the lexer proper (in libcpp
51 and c-lex.c) and the C++ parser. */
53 static cp_token eof_token =
55 CPP_EOF, RID_MAX, 0, false, false, false, 0, { NULL }
58 /* The various kinds of non integral constant we encounter. */
59 enum non_integral_constant {
60 NIC_NONE,
61 /* floating-point literal */
62 NIC_FLOAT,
63 /* %<this%> */
64 NIC_THIS,
65 /* %<__FUNCTION__%> */
66 NIC_FUNC_NAME,
67 /* %<__PRETTY_FUNCTION__%> */
68 NIC_PRETTY_FUNC,
69 /* %<__func__%> */
70 NIC_C99_FUNC,
71 /* "%<va_arg%> */
72 NIC_VA_ARG,
73 /* a cast */
74 NIC_CAST,
75 /* %<typeid%> operator */
76 NIC_TYPEID,
77 /* non-constant compound literals */
78 NIC_NCC,
79 /* a function call */
80 NIC_FUNC_CALL,
81 /* an increment */
82 NIC_INC,
83 /* an decrement */
84 NIC_DEC,
85 /* an array reference */
86 NIC_ARRAY_REF,
87 /* %<->%> */
88 NIC_ARROW,
89 /* %<.%> */
90 NIC_POINT,
91 /* the address of a label */
92 NIC_ADDR_LABEL,
93 /* %<*%> */
94 NIC_STAR,
95 /* %<&%> */
96 NIC_ADDR,
97 /* %<++%> */
98 NIC_PREINCREMENT,
99 /* %<--%> */
100 NIC_PREDECREMENT,
101 /* %<new%> */
102 NIC_NEW,
103 /* %<delete%> */
104 NIC_DEL,
105 /* calls to overloaded operators */
106 NIC_OVERLOADED,
107 /* an assignment */
108 NIC_ASSIGNMENT,
109 /* a comma operator */
110 NIC_COMMA,
111 /* a call to a constructor */
112 NIC_CONSTRUCTOR,
113 /* a transaction expression */
114 NIC_TRANSACTION
117 /* The various kinds of errors about name-lookup failing. */
118 enum name_lookup_error {
119 /* NULL */
120 NLE_NULL,
121 /* is not a type */
122 NLE_TYPE,
123 /* is not a class or namespace */
124 NLE_CXX98,
125 /* is not a class, namespace, or enumeration */
126 NLE_NOT_CXX98
129 /* The various kinds of required token */
130 enum required_token {
131 RT_NONE,
132 RT_SEMICOLON, /* ';' */
133 RT_OPEN_PAREN, /* '(' */
134 RT_CLOSE_BRACE, /* '}' */
135 RT_OPEN_BRACE, /* '{' */
136 RT_CLOSE_SQUARE, /* ']' */
137 RT_OPEN_SQUARE, /* '[' */
138 RT_COMMA, /* ',' */
139 RT_SCOPE, /* '::' */
140 RT_LESS, /* '<' */
141 RT_GREATER, /* '>' */
142 RT_EQ, /* '=' */
143 RT_ELLIPSIS, /* '...' */
144 RT_MULT, /* '*' */
145 RT_COMPL, /* '~' */
146 RT_COLON, /* ':' */
147 RT_COLON_SCOPE, /* ':' or '::' */
148 RT_CLOSE_PAREN, /* ')' */
149 RT_COMMA_CLOSE_PAREN, /* ',' or ')' */
150 RT_PRAGMA_EOL, /* end of line */
151 RT_NAME, /* identifier */
153 /* The type is CPP_KEYWORD */
154 RT_NEW, /* new */
155 RT_DELETE, /* delete */
156 RT_RETURN, /* return */
157 RT_WHILE, /* while */
158 RT_EXTERN, /* extern */
159 RT_STATIC_ASSERT, /* static_assert */
160 RT_DECLTYPE, /* decltype */
161 RT_OPERATOR, /* operator */
162 RT_CLASS, /* class */
163 RT_TEMPLATE, /* template */
164 RT_NAMESPACE, /* namespace */
165 RT_USING, /* using */
166 RT_ASM, /* asm */
167 RT_TRY, /* try */
168 RT_CATCH, /* catch */
169 RT_THROW, /* throw */
170 RT_LABEL, /* __label__ */
171 RT_AT_TRY, /* @try */
172 RT_AT_SYNCHRONIZED, /* @synchronized */
173 RT_AT_THROW, /* @throw */
175 RT_SELECT, /* selection-statement */
176 RT_INTERATION, /* iteration-statement */
177 RT_JUMP, /* jump-statement */
178 RT_CLASS_KEY, /* class-key */
179 RT_CLASS_TYPENAME_TEMPLATE, /* class, typename, or template */
180 RT_TRANSACTION_ATOMIC, /* __transaction_atomic */
181 RT_TRANSACTION_RELAXED, /* __transaction_relaxed */
182 RT_TRANSACTION_CANCEL /* __transaction_cancel */
185 /* RAII wrapper for parser->in_type_id_in_expr_p, setting it on creation and
186 reverting it on destruction. */
188 class type_id_in_expr_sentinel
190 cp_parser *parser;
191 bool saved;
192 public:
193 type_id_in_expr_sentinel (cp_parser *parser, bool set = true)
194 : parser (parser),
195 saved (parser->in_type_id_in_expr_p)
196 { parser->in_type_id_in_expr_p = set; }
197 ~type_id_in_expr_sentinel ()
198 { parser->in_type_id_in_expr_p = saved; }
201 /* Prototypes. */
203 static cp_lexer *cp_lexer_new_main
204 (void);
205 static cp_lexer *cp_lexer_new_from_tokens
206 (cp_token_cache *tokens);
207 static void cp_lexer_destroy
208 (cp_lexer *);
209 static int cp_lexer_saving_tokens
210 (const cp_lexer *);
211 static cp_token *cp_lexer_token_at
212 (cp_lexer *, cp_token_position);
213 static void cp_lexer_get_preprocessor_token
214 (cp_lexer *, cp_token *);
215 static inline cp_token *cp_lexer_peek_token
216 (cp_lexer *);
217 static cp_token *cp_lexer_peek_nth_token
218 (cp_lexer *, size_t);
219 static inline bool cp_lexer_next_token_is
220 (cp_lexer *, enum cpp_ttype);
221 static bool cp_lexer_next_token_is_not
222 (cp_lexer *, enum cpp_ttype);
223 static bool cp_lexer_next_token_is_keyword
224 (cp_lexer *, enum rid);
225 static cp_token *cp_lexer_consume_token
226 (cp_lexer *);
227 static void cp_lexer_purge_token
228 (cp_lexer *);
229 static void cp_lexer_purge_tokens_after
230 (cp_lexer *, cp_token_position);
231 static void cp_lexer_save_tokens
232 (cp_lexer *);
233 static void cp_lexer_commit_tokens
234 (cp_lexer *);
235 static void cp_lexer_rollback_tokens
236 (cp_lexer *);
237 static void cp_lexer_print_token
238 (FILE *, cp_token *);
239 static inline bool cp_lexer_debugging_p
240 (cp_lexer *);
241 static void cp_lexer_start_debugging
242 (cp_lexer *) ATTRIBUTE_UNUSED;
243 static void cp_lexer_stop_debugging
244 (cp_lexer *) ATTRIBUTE_UNUSED;
246 static cp_token_cache *cp_token_cache_new
247 (cp_token *, cp_token *);
249 static void cp_parser_initial_pragma
250 (cp_token *);
252 static void cp_parser_cilk_simd
253 (cp_parser *, cp_token *, bool *);
254 static tree cp_parser_cilk_for
255 (cp_parser *, tree, bool *);
256 static bool cp_parser_omp_declare_reduction_exprs
257 (tree, cp_parser *);
258 static tree cp_parser_cilk_simd_vectorlength
259 (cp_parser *, tree, bool);
260 static void cp_finalize_oacc_routine
261 (cp_parser *, tree, bool);
263 /* Manifest constants. */
264 #define CP_LEXER_BUFFER_SIZE ((256 * 1024) / sizeof (cp_token))
265 #define CP_SAVED_TOKEN_STACK 5
267 /* Variables. */
269 /* The stream to which debugging output should be written. */
270 static FILE *cp_lexer_debug_stream;
272 /* Nonzero if we are parsing an unevaluated operand: an operand to
273 sizeof, typeof, or alignof. */
274 int cp_unevaluated_operand;
276 /* Dump up to NUM tokens in BUFFER to FILE starting with token
277 START_TOKEN. If START_TOKEN is NULL, the dump starts with the
278 first token in BUFFER. If NUM is 0, dump all the tokens. If
279 CURR_TOKEN is set and it is one of the tokens in BUFFER, it will be
280 highlighted by surrounding it in [[ ]]. */
282 static void
283 cp_lexer_dump_tokens (FILE *file, vec<cp_token, va_gc> *buffer,
284 cp_token *start_token, unsigned num,
285 cp_token *curr_token)
287 unsigned i, nprinted;
288 cp_token *token;
289 bool do_print;
291 fprintf (file, "%u tokens\n", vec_safe_length (buffer));
293 if (buffer == NULL)
294 return;
296 if (num == 0)
297 num = buffer->length ();
299 if (start_token == NULL)
300 start_token = buffer->address ();
302 if (start_token > buffer->address ())
304 cp_lexer_print_token (file, &(*buffer)[0]);
305 fprintf (file, " ... ");
308 do_print = false;
309 nprinted = 0;
310 for (i = 0; buffer->iterate (i, &token) && nprinted < num; i++)
312 if (token == start_token)
313 do_print = true;
315 if (!do_print)
316 continue;
318 nprinted++;
319 if (token == curr_token)
320 fprintf (file, "[[");
322 cp_lexer_print_token (file, token);
324 if (token == curr_token)
325 fprintf (file, "]]");
327 switch (token->type)
329 case CPP_SEMICOLON:
330 case CPP_OPEN_BRACE:
331 case CPP_CLOSE_BRACE:
332 case CPP_EOF:
333 fputc ('\n', file);
334 break;
336 default:
337 fputc (' ', file);
341 if (i == num && i < buffer->length ())
343 fprintf (file, " ... ");
344 cp_lexer_print_token (file, &buffer->last ());
347 fprintf (file, "\n");
351 /* Dump all tokens in BUFFER to stderr. */
353 void
354 cp_lexer_debug_tokens (vec<cp_token, va_gc> *buffer)
356 cp_lexer_dump_tokens (stderr, buffer, NULL, 0, NULL);
359 DEBUG_FUNCTION void
360 debug (vec<cp_token, va_gc> &ref)
362 cp_lexer_dump_tokens (stderr, &ref, NULL, 0, NULL);
365 DEBUG_FUNCTION void
366 debug (vec<cp_token, va_gc> *ptr)
368 if (ptr)
369 debug (*ptr);
370 else
371 fprintf (stderr, "<nil>\n");
375 /* Dump the cp_parser tree field T to FILE if T is non-NULL. DESC is the
376 description for T. */
378 static void
379 cp_debug_print_tree_if_set (FILE *file, const char *desc, tree t)
381 if (t)
383 fprintf (file, "%s: ", desc);
384 print_node_brief (file, "", t, 0);
389 /* Dump parser context C to FILE. */
391 static void
392 cp_debug_print_context (FILE *file, cp_parser_context *c)
394 const char *status_s[] = { "OK", "ERROR", "COMMITTED" };
395 fprintf (file, "{ status = %s, scope = ", status_s[c->status]);
396 print_node_brief (file, "", c->object_type, 0);
397 fprintf (file, "}\n");
401 /* Print the stack of parsing contexts to FILE starting with FIRST. */
403 static void
404 cp_debug_print_context_stack (FILE *file, cp_parser_context *first)
406 unsigned i;
407 cp_parser_context *c;
409 fprintf (file, "Parsing context stack:\n");
410 for (i = 0, c = first; c; c = c->next, i++)
412 fprintf (file, "\t#%u: ", i);
413 cp_debug_print_context (file, c);
418 /* Print the value of FLAG to FILE. DESC is a string describing the flag. */
420 static void
421 cp_debug_print_flag (FILE *file, const char *desc, bool flag)
423 if (flag)
424 fprintf (file, "%s: true\n", desc);
428 /* Print an unparsed function entry UF to FILE. */
430 static void
431 cp_debug_print_unparsed_function (FILE *file, cp_unparsed_functions_entry *uf)
433 unsigned i;
434 cp_default_arg_entry *default_arg_fn;
435 tree fn;
437 fprintf (file, "\tFunctions with default args:\n");
438 for (i = 0;
439 vec_safe_iterate (uf->funs_with_default_args, i, &default_arg_fn);
440 i++)
442 fprintf (file, "\t\tClass type: ");
443 print_node_brief (file, "", default_arg_fn->class_type, 0);
444 fprintf (file, "\t\tDeclaration: ");
445 print_node_brief (file, "", default_arg_fn->decl, 0);
446 fprintf (file, "\n");
449 fprintf (file, "\n\tFunctions with definitions that require "
450 "post-processing\n\t\t");
451 for (i = 0; vec_safe_iterate (uf->funs_with_definitions, i, &fn); i++)
453 print_node_brief (file, "", fn, 0);
454 fprintf (file, " ");
456 fprintf (file, "\n");
458 fprintf (file, "\n\tNon-static data members with initializers that require "
459 "post-processing\n\t\t");
460 for (i = 0; vec_safe_iterate (uf->nsdmis, i, &fn); i++)
462 print_node_brief (file, "", fn, 0);
463 fprintf (file, " ");
465 fprintf (file, "\n");
469 /* Print the stack of unparsed member functions S to FILE. */
471 static void
472 cp_debug_print_unparsed_queues (FILE *file,
473 vec<cp_unparsed_functions_entry, va_gc> *s)
475 unsigned i;
476 cp_unparsed_functions_entry *uf;
478 fprintf (file, "Unparsed functions\n");
479 for (i = 0; vec_safe_iterate (s, i, &uf); i++)
481 fprintf (file, "#%u:\n", i);
482 cp_debug_print_unparsed_function (file, uf);
487 /* Dump the tokens in a window of size WINDOW_SIZE around the next_token for
488 the given PARSER. If FILE is NULL, the output is printed on stderr. */
490 static void
491 cp_debug_parser_tokens (FILE *file, cp_parser *parser, int window_size)
493 cp_token *next_token, *first_token, *start_token;
495 if (file == NULL)
496 file = stderr;
498 next_token = parser->lexer->next_token;
499 first_token = parser->lexer->buffer->address ();
500 start_token = (next_token > first_token + window_size / 2)
501 ? next_token - window_size / 2
502 : first_token;
503 cp_lexer_dump_tokens (file, parser->lexer->buffer, start_token, window_size,
504 next_token);
508 /* Dump debugging information for the given PARSER. If FILE is NULL,
509 the output is printed on stderr. */
511 void
512 cp_debug_parser (FILE *file, cp_parser *parser)
514 const size_t window_size = 20;
515 cp_token *token;
516 expanded_location eloc;
518 if (file == NULL)
519 file = stderr;
521 fprintf (file, "Parser state\n\n");
522 fprintf (file, "Number of tokens: %u\n",
523 vec_safe_length (parser->lexer->buffer));
524 cp_debug_print_tree_if_set (file, "Lookup scope", parser->scope);
525 cp_debug_print_tree_if_set (file, "Object scope",
526 parser->object_scope);
527 cp_debug_print_tree_if_set (file, "Qualifying scope",
528 parser->qualifying_scope);
529 cp_debug_print_context_stack (file, parser->context);
530 cp_debug_print_flag (file, "Allow GNU extensions",
531 parser->allow_gnu_extensions_p);
532 cp_debug_print_flag (file, "'>' token is greater-than",
533 parser->greater_than_is_operator_p);
534 cp_debug_print_flag (file, "Default args allowed in current "
535 "parameter list", parser->default_arg_ok_p);
536 cp_debug_print_flag (file, "Parsing integral constant-expression",
537 parser->integral_constant_expression_p);
538 cp_debug_print_flag (file, "Allow non-constant expression in current "
539 "constant-expression",
540 parser->allow_non_integral_constant_expression_p);
541 cp_debug_print_flag (file, "Seen non-constant expression",
542 parser->non_integral_constant_expression_p);
543 cp_debug_print_flag (file, "Local names and 'this' forbidden in "
544 "current context",
545 parser->local_variables_forbidden_p);
546 cp_debug_print_flag (file, "In unbraced linkage specification",
547 parser->in_unbraced_linkage_specification_p);
548 cp_debug_print_flag (file, "Parsing a declarator",
549 parser->in_declarator_p);
550 cp_debug_print_flag (file, "In template argument list",
551 parser->in_template_argument_list_p);
552 cp_debug_print_flag (file, "Parsing an iteration statement",
553 parser->in_statement & IN_ITERATION_STMT);
554 cp_debug_print_flag (file, "Parsing a switch statement",
555 parser->in_statement & IN_SWITCH_STMT);
556 cp_debug_print_flag (file, "Parsing a structured OpenMP block",
557 parser->in_statement & IN_OMP_BLOCK);
558 cp_debug_print_flag (file, "Parsing a Cilk Plus for loop",
559 parser->in_statement & IN_CILK_SIMD_FOR);
560 cp_debug_print_flag (file, "Parsing a an OpenMP loop",
561 parser->in_statement & IN_OMP_FOR);
562 cp_debug_print_flag (file, "Parsing an if statement",
563 parser->in_statement & IN_IF_STMT);
564 cp_debug_print_flag (file, "Parsing a type-id in an expression "
565 "context", parser->in_type_id_in_expr_p);
566 cp_debug_print_flag (file, "Declarations are implicitly extern \"C\"",
567 parser->implicit_extern_c);
568 cp_debug_print_flag (file, "String expressions should be translated "
569 "to execution character set",
570 parser->translate_strings_p);
571 cp_debug_print_flag (file, "Parsing function body outside of a "
572 "local class", parser->in_function_body);
573 cp_debug_print_flag (file, "Auto correct a colon to a scope operator",
574 parser->colon_corrects_to_scope_p);
575 cp_debug_print_flag (file, "Colon doesn't start a class definition",
576 parser->colon_doesnt_start_class_def_p);
577 if (parser->type_definition_forbidden_message)
578 fprintf (file, "Error message for forbidden type definitions: %s\n",
579 parser->type_definition_forbidden_message);
580 cp_debug_print_unparsed_queues (file, parser->unparsed_queues);
581 fprintf (file, "Number of class definitions in progress: %u\n",
582 parser->num_classes_being_defined);
583 fprintf (file, "Number of template parameter lists for the current "
584 "declaration: %u\n", parser->num_template_parameter_lists);
585 cp_debug_parser_tokens (file, parser, window_size);
586 token = parser->lexer->next_token;
587 fprintf (file, "Next token to parse:\n");
588 fprintf (file, "\tToken: ");
589 cp_lexer_print_token (file, token);
590 eloc = expand_location (token->location);
591 fprintf (file, "\n\tFile: %s\n", eloc.file);
592 fprintf (file, "\tLine: %d\n", eloc.line);
593 fprintf (file, "\tColumn: %d\n", eloc.column);
596 DEBUG_FUNCTION void
597 debug (cp_parser &ref)
599 cp_debug_parser (stderr, &ref);
602 DEBUG_FUNCTION void
603 debug (cp_parser *ptr)
605 if (ptr)
606 debug (*ptr);
607 else
608 fprintf (stderr, "<nil>\n");
611 /* Allocate memory for a new lexer object and return it. */
613 static cp_lexer *
614 cp_lexer_alloc (void)
616 cp_lexer *lexer;
618 c_common_no_more_pch ();
620 /* Allocate the memory. */
621 lexer = ggc_cleared_alloc<cp_lexer> ();
623 /* Initially we are not debugging. */
624 lexer->debugging_p = false;
626 lexer->saved_tokens.create (CP_SAVED_TOKEN_STACK);
628 /* Create the buffer. */
629 vec_alloc (lexer->buffer, CP_LEXER_BUFFER_SIZE);
631 return lexer;
635 /* Create a new main C++ lexer, the lexer that gets tokens from the
636 preprocessor. */
638 static cp_lexer *
639 cp_lexer_new_main (void)
641 cp_lexer *lexer;
642 cp_token token;
644 /* It's possible that parsing the first pragma will load a PCH file,
645 which is a GC collection point. So we have to do that before
646 allocating any memory. */
647 cp_parser_initial_pragma (&token);
649 lexer = cp_lexer_alloc ();
651 /* Put the first token in the buffer. */
652 lexer->buffer->quick_push (token);
654 /* Get the remaining tokens from the preprocessor. */
655 while (token.type != CPP_EOF)
657 cp_lexer_get_preprocessor_token (lexer, &token);
658 vec_safe_push (lexer->buffer, token);
661 lexer->last_token = lexer->buffer->address ()
662 + lexer->buffer->length ()
663 - 1;
664 lexer->next_token = lexer->buffer->length ()
665 ? lexer->buffer->address ()
666 : &eof_token;
668 /* Subsequent preprocessor diagnostics should use compiler
669 diagnostic functions to get the compiler source location. */
670 done_lexing = true;
672 gcc_assert (!lexer->next_token->purged_p);
673 return lexer;
676 /* Create a new lexer whose token stream is primed with the tokens in
677 CACHE. When these tokens are exhausted, no new tokens will be read. */
679 static cp_lexer *
680 cp_lexer_new_from_tokens (cp_token_cache *cache)
682 cp_token *first = cache->first;
683 cp_token *last = cache->last;
684 cp_lexer *lexer = ggc_cleared_alloc<cp_lexer> ();
686 /* We do not own the buffer. */
687 lexer->buffer = NULL;
688 lexer->next_token = first == last ? &eof_token : first;
689 lexer->last_token = last;
691 lexer->saved_tokens.create (CP_SAVED_TOKEN_STACK);
693 /* Initially we are not debugging. */
694 lexer->debugging_p = false;
696 gcc_assert (!lexer->next_token->purged_p);
697 return lexer;
700 /* Frees all resources associated with LEXER. */
702 static void
703 cp_lexer_destroy (cp_lexer *lexer)
705 vec_free (lexer->buffer);
706 lexer->saved_tokens.release ();
707 ggc_free (lexer);
710 /* This needs to be set to TRUE before the lexer-debugging infrastructure can
711 be used. The point of this flag is to help the compiler to fold away calls
712 to cp_lexer_debugging_p within this source file at compile time, when the
713 lexer is not being debugged. */
715 #define LEXER_DEBUGGING_ENABLED_P false
717 /* Returns nonzero if debugging information should be output. */
719 static inline bool
720 cp_lexer_debugging_p (cp_lexer *lexer)
722 if (!LEXER_DEBUGGING_ENABLED_P)
723 return false;
725 return lexer->debugging_p;
729 static inline cp_token_position
730 cp_lexer_token_position (cp_lexer *lexer, bool previous_p)
732 gcc_assert (!previous_p || lexer->next_token != &eof_token);
734 return lexer->next_token - previous_p;
737 static inline cp_token *
738 cp_lexer_token_at (cp_lexer * /*lexer*/, cp_token_position pos)
740 return pos;
743 static inline void
744 cp_lexer_set_token_position (cp_lexer *lexer, cp_token_position pos)
746 lexer->next_token = cp_lexer_token_at (lexer, pos);
749 static inline cp_token_position
750 cp_lexer_previous_token_position (cp_lexer *lexer)
752 if (lexer->next_token == &eof_token)
753 return lexer->last_token - 1;
754 else
755 return cp_lexer_token_position (lexer, true);
758 static inline cp_token *
759 cp_lexer_previous_token (cp_lexer *lexer)
761 cp_token_position tp = cp_lexer_previous_token_position (lexer);
763 /* Skip past purged tokens. */
764 while (tp->purged_p)
766 gcc_assert (tp != vec_safe_address (lexer->buffer));
767 tp--;
770 return cp_lexer_token_at (lexer, tp);
773 /* nonzero if we are presently saving tokens. */
775 static inline int
776 cp_lexer_saving_tokens (const cp_lexer* lexer)
778 return lexer->saved_tokens.length () != 0;
781 /* Store the next token from the preprocessor in *TOKEN. Return true
782 if we reach EOF. If LEXER is NULL, assume we are handling an
783 initial #pragma pch_preprocess, and thus want the lexer to return
784 processed strings. */
786 static void
787 cp_lexer_get_preprocessor_token (cp_lexer *lexer, cp_token *token)
789 static int is_extern_c = 0;
791 /* Get a new token from the preprocessor. */
792 token->type
793 = c_lex_with_flags (&token->u.value, &token->location, &token->flags,
794 lexer == NULL ? 0 : C_LEX_STRING_NO_JOIN);
795 token->keyword = RID_MAX;
796 token->purged_p = false;
797 token->error_reported = false;
799 /* On some systems, some header files are surrounded by an
800 implicit extern "C" block. Set a flag in the token if it
801 comes from such a header. */
802 is_extern_c += pending_lang_change;
803 pending_lang_change = 0;
804 token->implicit_extern_c = is_extern_c > 0;
806 /* Check to see if this token is a keyword. */
807 if (token->type == CPP_NAME)
809 if (C_IS_RESERVED_WORD (token->u.value))
811 /* Mark this token as a keyword. */
812 token->type = CPP_KEYWORD;
813 /* Record which keyword. */
814 token->keyword = C_RID_CODE (token->u.value);
816 else
818 if (warn_cxx11_compat
819 && C_RID_CODE (token->u.value) >= RID_FIRST_CXX11
820 && C_RID_CODE (token->u.value) <= RID_LAST_CXX11)
822 /* Warn about the C++0x keyword (but still treat it as
823 an identifier). */
824 warning (OPT_Wc__11_compat,
825 "identifier %qE is a keyword in C++11",
826 token->u.value);
828 /* Clear out the C_RID_CODE so we don't warn about this
829 particular identifier-turned-keyword again. */
830 C_SET_RID_CODE (token->u.value, RID_MAX);
833 token->keyword = RID_MAX;
836 else if (token->type == CPP_AT_NAME)
838 /* This only happens in Objective-C++; it must be a keyword. */
839 token->type = CPP_KEYWORD;
840 switch (C_RID_CODE (token->u.value))
842 /* Replace 'class' with '@class', 'private' with '@private',
843 etc. This prevents confusion with the C++ keyword
844 'class', and makes the tokens consistent with other
845 Objective-C 'AT' keywords. For example '@class' is
846 reported as RID_AT_CLASS which is consistent with
847 '@synchronized', which is reported as
848 RID_AT_SYNCHRONIZED.
850 case RID_CLASS: token->keyword = RID_AT_CLASS; break;
851 case RID_PRIVATE: token->keyword = RID_AT_PRIVATE; break;
852 case RID_PROTECTED: token->keyword = RID_AT_PROTECTED; break;
853 case RID_PUBLIC: token->keyword = RID_AT_PUBLIC; break;
854 case RID_THROW: token->keyword = RID_AT_THROW; break;
855 case RID_TRY: token->keyword = RID_AT_TRY; break;
856 case RID_CATCH: token->keyword = RID_AT_CATCH; break;
857 case RID_SYNCHRONIZED: token->keyword = RID_AT_SYNCHRONIZED; break;
858 default: token->keyword = C_RID_CODE (token->u.value);
863 /* Update the globals input_location and the input file stack from TOKEN. */
864 static inline void
865 cp_lexer_set_source_position_from_token (cp_token *token)
867 if (token->type != CPP_EOF)
869 input_location = token->location;
873 /* Update the globals input_location and the input file stack from LEXER. */
874 static inline void
875 cp_lexer_set_source_position (cp_lexer *lexer)
877 cp_token *token = cp_lexer_peek_token (lexer);
878 cp_lexer_set_source_position_from_token (token);
881 /* Return a pointer to the next token in the token stream, but do not
882 consume it. */
884 static inline cp_token *
885 cp_lexer_peek_token (cp_lexer *lexer)
887 if (cp_lexer_debugging_p (lexer))
889 fputs ("cp_lexer: peeking at token: ", cp_lexer_debug_stream);
890 cp_lexer_print_token (cp_lexer_debug_stream, lexer->next_token);
891 putc ('\n', cp_lexer_debug_stream);
893 return lexer->next_token;
896 /* Return true if the next token has the indicated TYPE. */
898 static inline bool
899 cp_lexer_next_token_is (cp_lexer* lexer, enum cpp_ttype type)
901 return cp_lexer_peek_token (lexer)->type == type;
904 /* Return true if the next token does not have the indicated TYPE. */
906 static inline bool
907 cp_lexer_next_token_is_not (cp_lexer* lexer, enum cpp_ttype type)
909 return !cp_lexer_next_token_is (lexer, type);
912 /* Return true if the next token is the indicated KEYWORD. */
914 static inline bool
915 cp_lexer_next_token_is_keyword (cp_lexer* lexer, enum rid keyword)
917 return cp_lexer_peek_token (lexer)->keyword == keyword;
920 static inline bool
921 cp_lexer_nth_token_is (cp_lexer* lexer, size_t n, enum cpp_ttype type)
923 return cp_lexer_peek_nth_token (lexer, n)->type == type;
926 static inline bool
927 cp_lexer_nth_token_is_keyword (cp_lexer* lexer, size_t n, enum rid keyword)
929 return cp_lexer_peek_nth_token (lexer, n)->keyword == keyword;
932 /* Return true if the next token is not the indicated KEYWORD. */
934 static inline bool
935 cp_lexer_next_token_is_not_keyword (cp_lexer* lexer, enum rid keyword)
937 return cp_lexer_peek_token (lexer)->keyword != keyword;
940 /* Return true if KEYWORD can start a decl-specifier. */
942 bool
943 cp_keyword_starts_decl_specifier_p (enum rid keyword)
945 switch (keyword)
947 /* auto specifier: storage-class-specifier in C++,
948 simple-type-specifier in C++0x. */
949 case RID_AUTO:
950 /* Storage classes. */
951 case RID_REGISTER:
952 case RID_STATIC:
953 case RID_EXTERN:
954 case RID_MUTABLE:
955 case RID_THREAD:
956 /* Elaborated type specifiers. */
957 case RID_ENUM:
958 case RID_CLASS:
959 case RID_STRUCT:
960 case RID_UNION:
961 case RID_TYPENAME:
962 /* Simple type specifiers. */
963 case RID_CHAR:
964 case RID_CHAR16:
965 case RID_CHAR32:
966 case RID_WCHAR:
967 case RID_BOOL:
968 case RID_SHORT:
969 case RID_INT:
970 case RID_LONG:
971 case RID_SIGNED:
972 case RID_UNSIGNED:
973 case RID_FLOAT:
974 case RID_DOUBLE:
975 case RID_VOID:
976 /* GNU extensions. */
977 case RID_ATTRIBUTE:
978 case RID_TYPEOF:
979 /* C++0x extensions. */
980 case RID_DECLTYPE:
981 case RID_UNDERLYING_TYPE:
982 case RID_CONSTEXPR:
983 return true;
985 default:
986 if (keyword >= RID_FIRST_INT_N
987 && keyword < RID_FIRST_INT_N + NUM_INT_N_ENTS
988 && int_n_enabled_p[keyword - RID_FIRST_INT_N])
989 return true;
990 return false;
994 /* Return true if the next token is a keyword for a decl-specifier. */
996 static bool
997 cp_lexer_next_token_is_decl_specifier_keyword (cp_lexer *lexer)
999 cp_token *token;
1001 token = cp_lexer_peek_token (lexer);
1002 return cp_keyword_starts_decl_specifier_p (token->keyword);
1005 /* Returns TRUE iff the token T begins a decltype type. */
1007 static bool
1008 token_is_decltype (cp_token *t)
1010 return (t->keyword == RID_DECLTYPE
1011 || t->type == CPP_DECLTYPE);
1014 /* Returns TRUE iff the next token begins a decltype type. */
1016 static bool
1017 cp_lexer_next_token_is_decltype (cp_lexer *lexer)
1019 cp_token *t = cp_lexer_peek_token (lexer);
1020 return token_is_decltype (t);
1023 /* Called when processing a token with tree_check_value; perform or defer the
1024 associated checks and return the value. */
1026 static tree
1027 saved_checks_value (struct tree_check *check_value)
1029 /* Perform any access checks that were deferred. */
1030 vec<deferred_access_check, va_gc> *checks;
1031 deferred_access_check *chk;
1032 checks = check_value->checks;
1033 if (checks)
1035 int i;
1036 FOR_EACH_VEC_SAFE_ELT (checks, i, chk)
1037 perform_or_defer_access_check (chk->binfo,
1038 chk->decl,
1039 chk->diag_decl, tf_warning_or_error);
1041 /* Return the stored value. */
1042 return check_value->value;
1045 /* Return a pointer to the Nth token in the token stream. If N is 1,
1046 then this is precisely equivalent to cp_lexer_peek_token (except
1047 that it is not inline). One would like to disallow that case, but
1048 there is one case (cp_parser_nth_token_starts_template_id) where
1049 the caller passes a variable for N and it might be 1. */
1051 static cp_token *
1052 cp_lexer_peek_nth_token (cp_lexer* lexer, size_t n)
1054 cp_token *token;
1056 /* N is 1-based, not zero-based. */
1057 gcc_assert (n > 0);
1059 if (cp_lexer_debugging_p (lexer))
1060 fprintf (cp_lexer_debug_stream,
1061 "cp_lexer: peeking ahead %ld at token: ", (long)n);
1063 --n;
1064 token = lexer->next_token;
1065 gcc_assert (!n || token != &eof_token);
1066 while (n != 0)
1068 ++token;
1069 if (token == lexer->last_token)
1071 token = &eof_token;
1072 break;
1075 if (!token->purged_p)
1076 --n;
1079 if (cp_lexer_debugging_p (lexer))
1081 cp_lexer_print_token (cp_lexer_debug_stream, token);
1082 putc ('\n', cp_lexer_debug_stream);
1085 return token;
1088 /* Return the next token, and advance the lexer's next_token pointer
1089 to point to the next non-purged token. */
1091 static cp_token *
1092 cp_lexer_consume_token (cp_lexer* lexer)
1094 cp_token *token = lexer->next_token;
1096 gcc_assert (token != &eof_token);
1097 gcc_assert (!lexer->in_pragma || token->type != CPP_PRAGMA_EOL);
1101 lexer->next_token++;
1102 if (lexer->next_token == lexer->last_token)
1104 lexer->next_token = &eof_token;
1105 break;
1109 while (lexer->next_token->purged_p);
1111 cp_lexer_set_source_position_from_token (token);
1113 /* Provide debugging output. */
1114 if (cp_lexer_debugging_p (lexer))
1116 fputs ("cp_lexer: consuming token: ", cp_lexer_debug_stream);
1117 cp_lexer_print_token (cp_lexer_debug_stream, token);
1118 putc ('\n', cp_lexer_debug_stream);
1121 return token;
1124 /* Permanently remove the next token from the token stream, and
1125 advance the next_token pointer to refer to the next non-purged
1126 token. */
1128 static void
1129 cp_lexer_purge_token (cp_lexer *lexer)
1131 cp_token *tok = lexer->next_token;
1133 gcc_assert (tok != &eof_token);
1134 tok->purged_p = true;
1135 tok->location = UNKNOWN_LOCATION;
1136 tok->u.value = NULL_TREE;
1137 tok->keyword = RID_MAX;
1141 tok++;
1142 if (tok == lexer->last_token)
1144 tok = &eof_token;
1145 break;
1148 while (tok->purged_p);
1149 lexer->next_token = tok;
1152 /* Permanently remove all tokens after TOK, up to, but not
1153 including, the token that will be returned next by
1154 cp_lexer_peek_token. */
1156 static void
1157 cp_lexer_purge_tokens_after (cp_lexer *lexer, cp_token *tok)
1159 cp_token *peek = lexer->next_token;
1161 if (peek == &eof_token)
1162 peek = lexer->last_token;
1164 gcc_assert (tok < peek);
1166 for ( tok += 1; tok != peek; tok += 1)
1168 tok->purged_p = true;
1169 tok->location = UNKNOWN_LOCATION;
1170 tok->u.value = NULL_TREE;
1171 tok->keyword = RID_MAX;
1175 /* Begin saving tokens. All tokens consumed after this point will be
1176 preserved. */
1178 static void
1179 cp_lexer_save_tokens (cp_lexer* lexer)
1181 /* Provide debugging output. */
1182 if (cp_lexer_debugging_p (lexer))
1183 fprintf (cp_lexer_debug_stream, "cp_lexer: saving tokens\n");
1185 lexer->saved_tokens.safe_push (lexer->next_token);
1188 /* Commit to the portion of the token stream most recently saved. */
1190 static void
1191 cp_lexer_commit_tokens (cp_lexer* lexer)
1193 /* Provide debugging output. */
1194 if (cp_lexer_debugging_p (lexer))
1195 fprintf (cp_lexer_debug_stream, "cp_lexer: committing tokens\n");
1197 lexer->saved_tokens.pop ();
1200 /* Return all tokens saved since the last call to cp_lexer_save_tokens
1201 to the token stream. Stop saving tokens. */
1203 static void
1204 cp_lexer_rollback_tokens (cp_lexer* lexer)
1206 /* Provide debugging output. */
1207 if (cp_lexer_debugging_p (lexer))
1208 fprintf (cp_lexer_debug_stream, "cp_lexer: restoring tokens\n");
1210 lexer->next_token = lexer->saved_tokens.pop ();
1213 /* RAII wrapper around the above functions, with sanity checking. Creating
1214 a variable saves tokens, which are committed when the variable is
1215 destroyed unless they are explicitly rolled back by calling the rollback
1216 member function. */
1218 struct saved_token_sentinel
1220 cp_lexer *lexer;
1221 unsigned len;
1222 bool commit;
1223 saved_token_sentinel(cp_lexer *lexer): lexer(lexer), commit(true)
1225 len = lexer->saved_tokens.length ();
1226 cp_lexer_save_tokens (lexer);
1228 void rollback ()
1230 cp_lexer_rollback_tokens (lexer);
1231 commit = false;
1233 ~saved_token_sentinel()
1235 if (commit)
1236 cp_lexer_commit_tokens (lexer);
1237 gcc_assert (lexer->saved_tokens.length () == len);
1241 /* Print a representation of the TOKEN on the STREAM. */
1243 static void
1244 cp_lexer_print_token (FILE * stream, cp_token *token)
1246 /* We don't use cpp_type2name here because the parser defines
1247 a few tokens of its own. */
1248 static const char *const token_names[] = {
1249 /* cpplib-defined token types */
1250 #define OP(e, s) #e,
1251 #define TK(e, s) #e,
1252 TTYPE_TABLE
1253 #undef OP
1254 #undef TK
1255 /* C++ parser token types - see "Manifest constants", above. */
1256 "KEYWORD",
1257 "TEMPLATE_ID",
1258 "NESTED_NAME_SPECIFIER",
1261 /* For some tokens, print the associated data. */
1262 switch (token->type)
1264 case CPP_KEYWORD:
1265 /* Some keywords have a value that is not an IDENTIFIER_NODE.
1266 For example, `struct' is mapped to an INTEGER_CST. */
1267 if (!identifier_p (token->u.value))
1268 break;
1269 /* fall through */
1270 case CPP_NAME:
1271 fputs (IDENTIFIER_POINTER (token->u.value), stream);
1272 break;
1274 case CPP_STRING:
1275 case CPP_STRING16:
1276 case CPP_STRING32:
1277 case CPP_WSTRING:
1278 case CPP_UTF8STRING:
1279 fprintf (stream, " \"%s\"", TREE_STRING_POINTER (token->u.value));
1280 break;
1282 case CPP_NUMBER:
1283 print_generic_expr (stream, token->u.value, 0);
1284 break;
1286 default:
1287 /* If we have a name for the token, print it out. Otherwise, we
1288 simply give the numeric code. */
1289 if (token->type < ARRAY_SIZE(token_names))
1290 fputs (token_names[token->type], stream);
1291 else
1292 fprintf (stream, "[%d]", token->type);
1293 break;
1297 DEBUG_FUNCTION void
1298 debug (cp_token &ref)
1300 cp_lexer_print_token (stderr, &ref);
1301 fprintf (stderr, "\n");
1304 DEBUG_FUNCTION void
1305 debug (cp_token *ptr)
1307 if (ptr)
1308 debug (*ptr);
1309 else
1310 fprintf (stderr, "<nil>\n");
1314 /* Start emitting debugging information. */
1316 static void
1317 cp_lexer_start_debugging (cp_lexer* lexer)
1319 if (!LEXER_DEBUGGING_ENABLED_P)
1320 fatal_error (input_location,
1321 "LEXER_DEBUGGING_ENABLED_P is not set to true");
1323 lexer->debugging_p = true;
1324 cp_lexer_debug_stream = stderr;
1327 /* Stop emitting debugging information. */
1329 static void
1330 cp_lexer_stop_debugging (cp_lexer* lexer)
1332 if (!LEXER_DEBUGGING_ENABLED_P)
1333 fatal_error (input_location,
1334 "LEXER_DEBUGGING_ENABLED_P is not set to true");
1336 lexer->debugging_p = false;
1337 cp_lexer_debug_stream = NULL;
1340 /* Create a new cp_token_cache, representing a range of tokens. */
1342 static cp_token_cache *
1343 cp_token_cache_new (cp_token *first, cp_token *last)
1345 cp_token_cache *cache = ggc_alloc<cp_token_cache> ();
1346 cache->first = first;
1347 cache->last = last;
1348 return cache;
1351 /* Diagnose if #pragma omp declare simd isn't followed immediately
1352 by function declaration or definition. */
1354 static inline void
1355 cp_ensure_no_omp_declare_simd (cp_parser *parser)
1357 if (parser->omp_declare_simd && !parser->omp_declare_simd->error_seen)
1359 error ("%<#pragma omp declare simd%> not immediately followed by "
1360 "function declaration or definition");
1361 parser->omp_declare_simd = NULL;
1365 /* Finalize #pragma omp declare simd clauses after FNDECL has been parsed,
1366 and put that into "omp declare simd" attribute. */
1368 static inline void
1369 cp_finalize_omp_declare_simd (cp_parser *parser, tree fndecl)
1371 if (__builtin_expect (parser->omp_declare_simd != NULL, 0))
1373 if (fndecl == error_mark_node)
1375 parser->omp_declare_simd = NULL;
1376 return;
1378 if (TREE_CODE (fndecl) != FUNCTION_DECL)
1380 cp_ensure_no_omp_declare_simd (parser);
1381 return;
1386 /* Diagnose if #pragma acc routine isn't followed immediately by function
1387 declaration or definition. */
1389 static inline void
1390 cp_ensure_no_oacc_routine (cp_parser *parser)
1392 if (parser->oacc_routine && !parser->oacc_routine->error_seen)
1394 error_at (parser->oacc_routine->loc,
1395 "%<#pragma acc routine%> not immediately followed by "
1396 "function declaration or definition");
1397 parser->oacc_routine = NULL;
1401 /* Decl-specifiers. */
1403 /* Set *DECL_SPECS to represent an empty decl-specifier-seq. */
1405 static void
1406 clear_decl_specs (cp_decl_specifier_seq *decl_specs)
1408 memset (decl_specs, 0, sizeof (cp_decl_specifier_seq));
1411 /* Declarators. */
1413 /* Nothing other than the parser should be creating declarators;
1414 declarators are a semi-syntactic representation of C++ entities.
1415 Other parts of the front end that need to create entities (like
1416 VAR_DECLs or FUNCTION_DECLs) should do that directly. */
1418 static cp_declarator *make_call_declarator
1419 (cp_declarator *, tree, cp_cv_quals, cp_virt_specifiers, cp_ref_qualifier, tree, tree, tree, tree);
1420 static cp_declarator *make_array_declarator
1421 (cp_declarator *, tree);
1422 static cp_declarator *make_pointer_declarator
1423 (cp_cv_quals, cp_declarator *, tree);
1424 static cp_declarator *make_reference_declarator
1425 (cp_cv_quals, cp_declarator *, bool, tree);
1426 static cp_declarator *make_ptrmem_declarator
1427 (cp_cv_quals, tree, cp_declarator *, tree);
1429 /* An erroneous declarator. */
1430 static cp_declarator *cp_error_declarator;
1432 /* The obstack on which declarators and related data structures are
1433 allocated. */
1434 static struct obstack declarator_obstack;
1436 /* Alloc BYTES from the declarator memory pool. */
1438 static inline void *
1439 alloc_declarator (size_t bytes)
1441 return obstack_alloc (&declarator_obstack, bytes);
1444 /* Allocate a declarator of the indicated KIND. Clear fields that are
1445 common to all declarators. */
1447 static cp_declarator *
1448 make_declarator (cp_declarator_kind kind)
1450 cp_declarator *declarator;
1452 declarator = (cp_declarator *) alloc_declarator (sizeof (cp_declarator));
1453 declarator->kind = kind;
1454 declarator->attributes = NULL_TREE;
1455 declarator->std_attributes = NULL_TREE;
1456 declarator->declarator = NULL;
1457 declarator->parameter_pack_p = false;
1458 declarator->id_loc = UNKNOWN_LOCATION;
1460 return declarator;
1463 /* Make a declarator for a generalized identifier. If
1464 QUALIFYING_SCOPE is non-NULL, the identifier is
1465 QUALIFYING_SCOPE::UNQUALIFIED_NAME; otherwise, it is just
1466 UNQUALIFIED_NAME. SFK indicates the kind of special function this
1467 is, if any. */
1469 static cp_declarator *
1470 make_id_declarator (tree qualifying_scope, tree unqualified_name,
1471 special_function_kind sfk)
1473 cp_declarator *declarator;
1475 /* It is valid to write:
1477 class C { void f(); };
1478 typedef C D;
1479 void D::f();
1481 The standard is not clear about whether `typedef const C D' is
1482 legal; as of 2002-09-15 the committee is considering that
1483 question. EDG 3.0 allows that syntax. Therefore, we do as
1484 well. */
1485 if (qualifying_scope && TYPE_P (qualifying_scope))
1486 qualifying_scope = TYPE_MAIN_VARIANT (qualifying_scope);
1488 gcc_assert (identifier_p (unqualified_name)
1489 || TREE_CODE (unqualified_name) == BIT_NOT_EXPR
1490 || TREE_CODE (unqualified_name) == TEMPLATE_ID_EXPR);
1492 declarator = make_declarator (cdk_id);
1493 declarator->u.id.qualifying_scope = qualifying_scope;
1494 declarator->u.id.unqualified_name = unqualified_name;
1495 declarator->u.id.sfk = sfk;
1497 return declarator;
1500 /* Make a declarator for a pointer to TARGET. CV_QUALIFIERS is a list
1501 of modifiers such as const or volatile to apply to the pointer
1502 type, represented as identifiers. ATTRIBUTES represent the attributes that
1503 appertain to the pointer or reference. */
1505 cp_declarator *
1506 make_pointer_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target,
1507 tree attributes)
1509 cp_declarator *declarator;
1511 declarator = make_declarator (cdk_pointer);
1512 declarator->declarator = target;
1513 declarator->u.pointer.qualifiers = cv_qualifiers;
1514 declarator->u.pointer.class_type = NULL_TREE;
1515 if (target)
1517 declarator->id_loc = target->id_loc;
1518 declarator->parameter_pack_p = target->parameter_pack_p;
1519 target->parameter_pack_p = false;
1521 else
1522 declarator->parameter_pack_p = false;
1524 declarator->std_attributes = attributes;
1526 return declarator;
1529 /* Like make_pointer_declarator -- but for references. ATTRIBUTES
1530 represent the attributes that appertain to the pointer or
1531 reference. */
1533 cp_declarator *
1534 make_reference_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target,
1535 bool rvalue_ref, tree attributes)
1537 cp_declarator *declarator;
1539 declarator = make_declarator (cdk_reference);
1540 declarator->declarator = target;
1541 declarator->u.reference.qualifiers = cv_qualifiers;
1542 declarator->u.reference.rvalue_ref = rvalue_ref;
1543 if (target)
1545 declarator->id_loc = target->id_loc;
1546 declarator->parameter_pack_p = target->parameter_pack_p;
1547 target->parameter_pack_p = false;
1549 else
1550 declarator->parameter_pack_p = false;
1552 declarator->std_attributes = attributes;
1554 return declarator;
1557 /* Like make_pointer_declarator -- but for a pointer to a non-static
1558 member of CLASS_TYPE. ATTRIBUTES represent the attributes that
1559 appertain to the pointer or reference. */
1561 cp_declarator *
1562 make_ptrmem_declarator (cp_cv_quals cv_qualifiers, tree class_type,
1563 cp_declarator *pointee,
1564 tree attributes)
1566 cp_declarator *declarator;
1568 declarator = make_declarator (cdk_ptrmem);
1569 declarator->declarator = pointee;
1570 declarator->u.pointer.qualifiers = cv_qualifiers;
1571 declarator->u.pointer.class_type = class_type;
1573 if (pointee)
1575 declarator->parameter_pack_p = pointee->parameter_pack_p;
1576 pointee->parameter_pack_p = false;
1578 else
1579 declarator->parameter_pack_p = false;
1581 declarator->std_attributes = attributes;
1583 return declarator;
1586 /* Make a declarator for the function given by TARGET, with the
1587 indicated PARMS. The CV_QUALIFIERS aply to the function, as in
1588 "const"-qualified member function. The EXCEPTION_SPECIFICATION
1589 indicates what exceptions can be thrown. */
1591 cp_declarator *
1592 make_call_declarator (cp_declarator *target,
1593 tree parms,
1594 cp_cv_quals cv_qualifiers,
1595 cp_virt_specifiers virt_specifiers,
1596 cp_ref_qualifier ref_qualifier,
1597 tree tx_qualifier,
1598 tree exception_specification,
1599 tree late_return_type,
1600 tree requires_clause)
1602 cp_declarator *declarator;
1604 declarator = make_declarator (cdk_function);
1605 declarator->declarator = target;
1606 declarator->u.function.parameters = parms;
1607 declarator->u.function.qualifiers = cv_qualifiers;
1608 declarator->u.function.virt_specifiers = virt_specifiers;
1609 declarator->u.function.ref_qualifier = ref_qualifier;
1610 declarator->u.function.tx_qualifier = tx_qualifier;
1611 declarator->u.function.exception_specification = exception_specification;
1612 declarator->u.function.late_return_type = late_return_type;
1613 declarator->u.function.requires_clause = requires_clause;
1614 if (target)
1616 declarator->id_loc = target->id_loc;
1617 declarator->parameter_pack_p = target->parameter_pack_p;
1618 target->parameter_pack_p = false;
1620 else
1621 declarator->parameter_pack_p = false;
1623 return declarator;
1626 /* Make a declarator for an array of BOUNDS elements, each of which is
1627 defined by ELEMENT. */
1629 cp_declarator *
1630 make_array_declarator (cp_declarator *element, tree bounds)
1632 cp_declarator *declarator;
1634 declarator = make_declarator (cdk_array);
1635 declarator->declarator = element;
1636 declarator->u.array.bounds = bounds;
1637 if (element)
1639 declarator->id_loc = element->id_loc;
1640 declarator->parameter_pack_p = element->parameter_pack_p;
1641 element->parameter_pack_p = false;
1643 else
1644 declarator->parameter_pack_p = false;
1646 return declarator;
1649 /* Determine whether the declarator we've seen so far can be a
1650 parameter pack, when followed by an ellipsis. */
1651 static bool
1652 declarator_can_be_parameter_pack (cp_declarator *declarator)
1654 if (declarator && declarator->parameter_pack_p)
1655 /* We already saw an ellipsis. */
1656 return false;
1658 /* Search for a declarator name, or any other declarator that goes
1659 after the point where the ellipsis could appear in a parameter
1660 pack. If we find any of these, then this declarator can not be
1661 made into a parameter pack. */
1662 bool found = false;
1663 while (declarator && !found)
1665 switch ((int)declarator->kind)
1667 case cdk_id:
1668 case cdk_array:
1669 case cdk_decomp:
1670 found = true;
1671 break;
1673 case cdk_error:
1674 return true;
1676 default:
1677 declarator = declarator->declarator;
1678 break;
1682 return !found;
1685 cp_parameter_declarator *no_parameters;
1687 /* Create a parameter declarator with the indicated DECL_SPECIFIERS,
1688 DECLARATOR and DEFAULT_ARGUMENT. */
1690 cp_parameter_declarator *
1691 make_parameter_declarator (cp_decl_specifier_seq *decl_specifiers,
1692 cp_declarator *declarator,
1693 tree default_argument,
1694 bool template_parameter_pack_p = false)
1696 cp_parameter_declarator *parameter;
1698 parameter = ((cp_parameter_declarator *)
1699 alloc_declarator (sizeof (cp_parameter_declarator)));
1700 parameter->next = NULL;
1701 if (decl_specifiers)
1702 parameter->decl_specifiers = *decl_specifiers;
1703 else
1704 clear_decl_specs (&parameter->decl_specifiers);
1705 parameter->declarator = declarator;
1706 parameter->default_argument = default_argument;
1707 parameter->template_parameter_pack_p = template_parameter_pack_p;
1709 return parameter;
1712 /* Returns true iff DECLARATOR is a declaration for a function. */
1714 static bool
1715 function_declarator_p (const cp_declarator *declarator)
1717 while (declarator)
1719 if (declarator->kind == cdk_function
1720 && declarator->declarator->kind == cdk_id)
1721 return true;
1722 if (declarator->kind == cdk_id
1723 || declarator->kind == cdk_decomp
1724 || declarator->kind == cdk_error)
1725 return false;
1726 declarator = declarator->declarator;
1728 return false;
1731 /* The parser. */
1733 /* Overview
1734 --------
1736 A cp_parser parses the token stream as specified by the C++
1737 grammar. Its job is purely parsing, not semantic analysis. For
1738 example, the parser breaks the token stream into declarators,
1739 expressions, statements, and other similar syntactic constructs.
1740 It does not check that the types of the expressions on either side
1741 of an assignment-statement are compatible, or that a function is
1742 not declared with a parameter of type `void'.
1744 The parser invokes routines elsewhere in the compiler to perform
1745 semantic analysis and to build up the abstract syntax tree for the
1746 code processed.
1748 The parser (and the template instantiation code, which is, in a
1749 way, a close relative of parsing) are the only parts of the
1750 compiler that should be calling push_scope and pop_scope, or
1751 related functions. The parser (and template instantiation code)
1752 keeps track of what scope is presently active; everything else
1753 should simply honor that. (The code that generates static
1754 initializers may also need to set the scope, in order to check
1755 access control correctly when emitting the initializers.)
1757 Methodology
1758 -----------
1760 The parser is of the standard recursive-descent variety. Upcoming
1761 tokens in the token stream are examined in order to determine which
1762 production to use when parsing a non-terminal. Some C++ constructs
1763 require arbitrary look ahead to disambiguate. For example, it is
1764 impossible, in the general case, to tell whether a statement is an
1765 expression or declaration without scanning the entire statement.
1766 Therefore, the parser is capable of "parsing tentatively." When the
1767 parser is not sure what construct comes next, it enters this mode.
1768 Then, while we attempt to parse the construct, the parser queues up
1769 error messages, rather than issuing them immediately, and saves the
1770 tokens it consumes. If the construct is parsed successfully, the
1771 parser "commits", i.e., it issues any queued error messages and
1772 the tokens that were being preserved are permanently discarded.
1773 If, however, the construct is not parsed successfully, the parser
1774 rolls back its state completely so that it can resume parsing using
1775 a different alternative.
1777 Future Improvements
1778 -------------------
1780 The performance of the parser could probably be improved substantially.
1781 We could often eliminate the need to parse tentatively by looking ahead
1782 a little bit. In some places, this approach might not entirely eliminate
1783 the need to parse tentatively, but it might still speed up the average
1784 case. */
1786 /* Flags that are passed to some parsing functions. These values can
1787 be bitwise-ored together. */
1789 enum
1791 /* No flags. */
1792 CP_PARSER_FLAGS_NONE = 0x0,
1793 /* The construct is optional. If it is not present, then no error
1794 should be issued. */
1795 CP_PARSER_FLAGS_OPTIONAL = 0x1,
1796 /* When parsing a type-specifier, treat user-defined type-names
1797 as non-type identifiers. */
1798 CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES = 0x2,
1799 /* When parsing a type-specifier, do not try to parse a class-specifier
1800 or enum-specifier. */
1801 CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS = 0x4,
1802 /* When parsing a decl-specifier-seq, only allow type-specifier or
1803 constexpr. */
1804 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR = 0x8,
1805 /* When parsing a decl-specifier-seq, only allow mutable or constexpr. */
1806 CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR = 0x10
1809 /* This type is used for parameters and variables which hold
1810 combinations of the above flags. */
1811 typedef int cp_parser_flags;
1813 /* The different kinds of declarators we want to parse. */
1815 enum cp_parser_declarator_kind
1817 /* We want an abstract declarator. */
1818 CP_PARSER_DECLARATOR_ABSTRACT,
1819 /* We want a named declarator. */
1820 CP_PARSER_DECLARATOR_NAMED,
1821 /* We don't mind, but the name must be an unqualified-id. */
1822 CP_PARSER_DECLARATOR_EITHER
1825 /* The precedence values used to parse binary expressions. The minimum value
1826 of PREC must be 1, because zero is reserved to quickly discriminate
1827 binary operators from other tokens. */
1829 enum cp_parser_prec
1831 PREC_NOT_OPERATOR,
1832 PREC_LOGICAL_OR_EXPRESSION,
1833 PREC_LOGICAL_AND_EXPRESSION,
1834 PREC_INCLUSIVE_OR_EXPRESSION,
1835 PREC_EXCLUSIVE_OR_EXPRESSION,
1836 PREC_AND_EXPRESSION,
1837 PREC_EQUALITY_EXPRESSION,
1838 PREC_RELATIONAL_EXPRESSION,
1839 PREC_SHIFT_EXPRESSION,
1840 PREC_ADDITIVE_EXPRESSION,
1841 PREC_MULTIPLICATIVE_EXPRESSION,
1842 PREC_PM_EXPRESSION,
1843 NUM_PREC_VALUES = PREC_PM_EXPRESSION
1846 /* A mapping from a token type to a corresponding tree node type, with a
1847 precedence value. */
1849 struct cp_parser_binary_operations_map_node
1851 /* The token type. */
1852 enum cpp_ttype token_type;
1853 /* The corresponding tree code. */
1854 enum tree_code tree_type;
1855 /* The precedence of this operator. */
1856 enum cp_parser_prec prec;
1859 struct cp_parser_expression_stack_entry
1861 /* Left hand side of the binary operation we are currently
1862 parsing. */
1863 cp_expr lhs;
1864 /* Original tree code for left hand side, if it was a binary
1865 expression itself (used for -Wparentheses). */
1866 enum tree_code lhs_type;
1867 /* Tree code for the binary operation we are parsing. */
1868 enum tree_code tree_type;
1869 /* Precedence of the binary operation we are parsing. */
1870 enum cp_parser_prec prec;
1871 /* Location of the binary operation we are parsing. */
1872 location_t loc;
1875 /* The stack for storing partial expressions. We only need NUM_PREC_VALUES
1876 entries because precedence levels on the stack are monotonically
1877 increasing. */
1878 typedef struct cp_parser_expression_stack_entry
1879 cp_parser_expression_stack[NUM_PREC_VALUES];
1881 /* Prototypes. */
1883 /* Constructors and destructors. */
1885 static cp_parser_context *cp_parser_context_new
1886 (cp_parser_context *);
1888 /* Class variables. */
1890 static GTY((deletable)) cp_parser_context* cp_parser_context_free_list;
1892 /* The operator-precedence table used by cp_parser_binary_expression.
1893 Transformed into an associative array (binops_by_token) by
1894 cp_parser_new. */
1896 static const cp_parser_binary_operations_map_node binops[] = {
1897 { CPP_DEREF_STAR, MEMBER_REF, PREC_PM_EXPRESSION },
1898 { CPP_DOT_STAR, DOTSTAR_EXPR, PREC_PM_EXPRESSION },
1900 { CPP_MULT, MULT_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1901 { CPP_DIV, TRUNC_DIV_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1902 { CPP_MOD, TRUNC_MOD_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1904 { CPP_PLUS, PLUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1905 { CPP_MINUS, MINUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1907 { CPP_LSHIFT, LSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1908 { CPP_RSHIFT, RSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1910 { CPP_LESS, LT_EXPR, PREC_RELATIONAL_EXPRESSION },
1911 { CPP_GREATER, GT_EXPR, PREC_RELATIONAL_EXPRESSION },
1912 { CPP_LESS_EQ, LE_EXPR, PREC_RELATIONAL_EXPRESSION },
1913 { CPP_GREATER_EQ, GE_EXPR, PREC_RELATIONAL_EXPRESSION },
1915 { CPP_EQ_EQ, EQ_EXPR, PREC_EQUALITY_EXPRESSION },
1916 { CPP_NOT_EQ, NE_EXPR, PREC_EQUALITY_EXPRESSION },
1918 { CPP_AND, BIT_AND_EXPR, PREC_AND_EXPRESSION },
1920 { CPP_XOR, BIT_XOR_EXPR, PREC_EXCLUSIVE_OR_EXPRESSION },
1922 { CPP_OR, BIT_IOR_EXPR, PREC_INCLUSIVE_OR_EXPRESSION },
1924 { CPP_AND_AND, TRUTH_ANDIF_EXPR, PREC_LOGICAL_AND_EXPRESSION },
1926 { CPP_OR_OR, TRUTH_ORIF_EXPR, PREC_LOGICAL_OR_EXPRESSION }
1929 /* The same as binops, but initialized by cp_parser_new so that
1930 binops_by_token[N].token_type == N. Used in cp_parser_binary_expression
1931 for speed. */
1932 static cp_parser_binary_operations_map_node binops_by_token[N_CP_TTYPES];
1934 /* Constructors and destructors. */
1936 /* Construct a new context. The context below this one on the stack
1937 is given by NEXT. */
1939 static cp_parser_context *
1940 cp_parser_context_new (cp_parser_context* next)
1942 cp_parser_context *context;
1944 /* Allocate the storage. */
1945 if (cp_parser_context_free_list != NULL)
1947 /* Pull the first entry from the free list. */
1948 context = cp_parser_context_free_list;
1949 cp_parser_context_free_list = context->next;
1950 memset (context, 0, sizeof (*context));
1952 else
1953 context = ggc_cleared_alloc<cp_parser_context> ();
1955 /* No errors have occurred yet in this context. */
1956 context->status = CP_PARSER_STATUS_KIND_NO_ERROR;
1957 /* If this is not the bottommost context, copy information that we
1958 need from the previous context. */
1959 if (next)
1961 /* If, in the NEXT context, we are parsing an `x->' or `x.'
1962 expression, then we are parsing one in this context, too. */
1963 context->object_type = next->object_type;
1964 /* Thread the stack. */
1965 context->next = next;
1968 return context;
1971 /* Managing the unparsed function queues. */
1973 #define unparsed_funs_with_default_args \
1974 parser->unparsed_queues->last ().funs_with_default_args
1975 #define unparsed_funs_with_definitions \
1976 parser->unparsed_queues->last ().funs_with_definitions
1977 #define unparsed_nsdmis \
1978 parser->unparsed_queues->last ().nsdmis
1979 #define unparsed_classes \
1980 parser->unparsed_queues->last ().classes
1982 static void
1983 push_unparsed_function_queues (cp_parser *parser)
1985 cp_unparsed_functions_entry e = {NULL, make_tree_vector (), NULL, NULL};
1986 vec_safe_push (parser->unparsed_queues, e);
1989 static void
1990 pop_unparsed_function_queues (cp_parser *parser)
1992 release_tree_vector (unparsed_funs_with_definitions);
1993 parser->unparsed_queues->pop ();
1996 /* Prototypes. */
1998 /* Constructors and destructors. */
2000 static cp_parser *cp_parser_new
2001 (void);
2003 /* Routines to parse various constructs.
2005 Those that return `tree' will return the error_mark_node (rather
2006 than NULL_TREE) if a parse error occurs, unless otherwise noted.
2007 Sometimes, they will return an ordinary node if error-recovery was
2008 attempted, even though a parse error occurred. So, to check
2009 whether or not a parse error occurred, you should always use
2010 cp_parser_error_occurred. If the construct is optional (indicated
2011 either by an `_opt' in the name of the function that does the
2012 parsing or via a FLAGS parameter), then NULL_TREE is returned if
2013 the construct is not present. */
2015 /* Lexical conventions [gram.lex] */
2017 static cp_expr cp_parser_identifier
2018 (cp_parser *);
2019 static cp_expr cp_parser_string_literal
2020 (cp_parser *, bool, bool, bool);
2021 static cp_expr cp_parser_userdef_char_literal
2022 (cp_parser *);
2023 static tree cp_parser_userdef_string_literal
2024 (tree);
2025 static cp_expr cp_parser_userdef_numeric_literal
2026 (cp_parser *);
2028 /* Basic concepts [gram.basic] */
2030 static bool cp_parser_translation_unit
2031 (cp_parser *);
2033 /* Expressions [gram.expr] */
2035 static cp_expr cp_parser_primary_expression
2036 (cp_parser *, bool, bool, bool, cp_id_kind *);
2037 static cp_expr cp_parser_id_expression
2038 (cp_parser *, bool, bool, bool *, bool, bool);
2039 static cp_expr cp_parser_unqualified_id
2040 (cp_parser *, bool, bool, bool, bool);
2041 static tree cp_parser_nested_name_specifier_opt
2042 (cp_parser *, bool, bool, bool, bool);
2043 static tree cp_parser_nested_name_specifier
2044 (cp_parser *, bool, bool, bool, bool);
2045 static tree cp_parser_qualifying_entity
2046 (cp_parser *, bool, bool, bool, bool, bool);
2047 static cp_expr cp_parser_postfix_expression
2048 (cp_parser *, bool, bool, bool, bool, cp_id_kind *);
2049 static tree cp_parser_postfix_open_square_expression
2050 (cp_parser *, tree, bool, bool);
2051 static tree cp_parser_postfix_dot_deref_expression
2052 (cp_parser *, enum cpp_ttype, cp_expr, bool, cp_id_kind *, location_t);
2053 static vec<tree, va_gc> *cp_parser_parenthesized_expression_list
2054 (cp_parser *, int, bool, bool, bool *, location_t * = NULL);
2055 /* Values for the second parameter of cp_parser_parenthesized_expression_list. */
2056 enum { non_attr = 0, normal_attr = 1, id_attr = 2 };
2057 static void cp_parser_pseudo_destructor_name
2058 (cp_parser *, tree, tree *, tree *);
2059 static cp_expr cp_parser_unary_expression
2060 (cp_parser *, cp_id_kind * = NULL, bool = false, bool = false, bool = false);
2061 static enum tree_code cp_parser_unary_operator
2062 (cp_token *);
2063 static tree cp_parser_new_expression
2064 (cp_parser *);
2065 static vec<tree, va_gc> *cp_parser_new_placement
2066 (cp_parser *);
2067 static tree cp_parser_new_type_id
2068 (cp_parser *, tree *);
2069 static cp_declarator *cp_parser_new_declarator_opt
2070 (cp_parser *);
2071 static cp_declarator *cp_parser_direct_new_declarator
2072 (cp_parser *);
2073 static vec<tree, va_gc> *cp_parser_new_initializer
2074 (cp_parser *);
2075 static tree cp_parser_delete_expression
2076 (cp_parser *);
2077 static cp_expr cp_parser_cast_expression
2078 (cp_parser *, bool, bool, bool, cp_id_kind *);
2079 static cp_expr cp_parser_binary_expression
2080 (cp_parser *, bool, bool, enum cp_parser_prec, cp_id_kind *);
2081 static tree cp_parser_question_colon_clause
2082 (cp_parser *, cp_expr);
2083 static cp_expr cp_parser_assignment_expression
2084 (cp_parser *, cp_id_kind * = NULL, bool = false, bool = false);
2085 static enum tree_code cp_parser_assignment_operator_opt
2086 (cp_parser *);
2087 static cp_expr cp_parser_expression
2088 (cp_parser *, cp_id_kind * = NULL, bool = false, bool = false);
2089 static cp_expr cp_parser_constant_expression
2090 (cp_parser *, bool = false, bool * = NULL);
2091 static cp_expr cp_parser_builtin_offsetof
2092 (cp_parser *);
2093 static cp_expr cp_parser_lambda_expression
2094 (cp_parser *);
2095 static void cp_parser_lambda_introducer
2096 (cp_parser *, tree);
2097 static bool cp_parser_lambda_declarator_opt
2098 (cp_parser *, tree);
2099 static void cp_parser_lambda_body
2100 (cp_parser *, tree);
2102 /* Statements [gram.stmt.stmt] */
2104 static void cp_parser_statement
2105 (cp_parser *, tree, bool, bool *, vec<tree> * = NULL);
2106 static void cp_parser_label_for_labeled_statement
2107 (cp_parser *, tree);
2108 static tree cp_parser_expression_statement
2109 (cp_parser *, tree);
2110 static tree cp_parser_compound_statement
2111 (cp_parser *, tree, int, bool);
2112 static void cp_parser_statement_seq_opt
2113 (cp_parser *, tree);
2114 static tree cp_parser_selection_statement
2115 (cp_parser *, bool *, vec<tree> *);
2116 static tree cp_parser_condition
2117 (cp_parser *);
2118 static tree cp_parser_iteration_statement
2119 (cp_parser *, bool *, bool);
2120 static bool cp_parser_init_statement
2121 (cp_parser *, tree *decl);
2122 static tree cp_parser_for
2123 (cp_parser *, bool);
2124 static tree cp_parser_c_for
2125 (cp_parser *, tree, tree, bool);
2126 static tree cp_parser_range_for
2127 (cp_parser *, tree, tree, tree, bool);
2128 static void do_range_for_auto_deduction
2129 (tree, tree);
2130 static tree cp_parser_perform_range_for_lookup
2131 (tree, tree *, tree *);
2132 static tree cp_parser_range_for_member_function
2133 (tree, tree);
2134 static tree cp_parser_jump_statement
2135 (cp_parser *);
2136 static void cp_parser_declaration_statement
2137 (cp_parser *);
2139 static tree cp_parser_implicitly_scoped_statement
2140 (cp_parser *, bool *, const token_indent_info &, vec<tree> * = NULL);
2141 static void cp_parser_already_scoped_statement
2142 (cp_parser *, bool *, const token_indent_info &);
2144 /* Declarations [gram.dcl.dcl] */
2146 static void cp_parser_declaration_seq_opt
2147 (cp_parser *);
2148 static void cp_parser_declaration
2149 (cp_parser *);
2150 static void cp_parser_block_declaration
2151 (cp_parser *, bool);
2152 static void cp_parser_simple_declaration
2153 (cp_parser *, bool, tree *);
2154 static void cp_parser_decl_specifier_seq
2155 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, int *);
2156 static tree cp_parser_storage_class_specifier_opt
2157 (cp_parser *);
2158 static tree cp_parser_function_specifier_opt
2159 (cp_parser *, cp_decl_specifier_seq *);
2160 static tree cp_parser_type_specifier
2161 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, bool,
2162 int *, bool *);
2163 static tree cp_parser_simple_type_specifier
2164 (cp_parser *, cp_decl_specifier_seq *, cp_parser_flags);
2165 static tree cp_parser_type_name
2166 (cp_parser *, bool);
2167 static tree cp_parser_type_name
2168 (cp_parser *);
2169 static tree cp_parser_nonclass_name
2170 (cp_parser* parser);
2171 static tree cp_parser_elaborated_type_specifier
2172 (cp_parser *, bool, bool);
2173 static tree cp_parser_enum_specifier
2174 (cp_parser *);
2175 static void cp_parser_enumerator_list
2176 (cp_parser *, tree);
2177 static void cp_parser_enumerator_definition
2178 (cp_parser *, tree);
2179 static tree cp_parser_namespace_name
2180 (cp_parser *);
2181 static void cp_parser_namespace_definition
2182 (cp_parser *);
2183 static void cp_parser_namespace_body
2184 (cp_parser *);
2185 static tree cp_parser_qualified_namespace_specifier
2186 (cp_parser *);
2187 static void cp_parser_namespace_alias_definition
2188 (cp_parser *);
2189 static bool cp_parser_using_declaration
2190 (cp_parser *, bool);
2191 static void cp_parser_using_directive
2192 (cp_parser *);
2193 static tree cp_parser_alias_declaration
2194 (cp_parser *);
2195 static void cp_parser_asm_definition
2196 (cp_parser *);
2197 static void cp_parser_linkage_specification
2198 (cp_parser *);
2199 static void cp_parser_static_assert
2200 (cp_parser *, bool);
2201 static tree cp_parser_decltype
2202 (cp_parser *);
2203 static tree cp_parser_decomposition_declaration
2204 (cp_parser *, cp_decl_specifier_seq *, tree *, location_t *);
2206 /* Declarators [gram.dcl.decl] */
2208 static tree cp_parser_init_declarator
2209 (cp_parser *, cp_decl_specifier_seq *, vec<deferred_access_check, va_gc> *,
2210 bool, bool, int, bool *, tree *, location_t *, tree *);
2211 static cp_declarator *cp_parser_declarator
2212 (cp_parser *, cp_parser_declarator_kind, int *, bool *, bool, bool);
2213 static cp_declarator *cp_parser_direct_declarator
2214 (cp_parser *, cp_parser_declarator_kind, int *, bool, bool);
2215 static enum tree_code cp_parser_ptr_operator
2216 (cp_parser *, tree *, cp_cv_quals *, tree *);
2217 static cp_cv_quals cp_parser_cv_qualifier_seq_opt
2218 (cp_parser *);
2219 static cp_virt_specifiers cp_parser_virt_specifier_seq_opt
2220 (cp_parser *);
2221 static cp_ref_qualifier cp_parser_ref_qualifier_opt
2222 (cp_parser *);
2223 static tree cp_parser_tx_qualifier_opt
2224 (cp_parser *);
2225 static tree cp_parser_late_return_type_opt
2226 (cp_parser *, cp_declarator *, tree &, cp_cv_quals);
2227 static tree cp_parser_declarator_id
2228 (cp_parser *, bool);
2229 static tree cp_parser_type_id
2230 (cp_parser *);
2231 static tree cp_parser_template_type_arg
2232 (cp_parser *);
2233 static tree cp_parser_trailing_type_id (cp_parser *);
2234 static tree cp_parser_type_id_1
2235 (cp_parser *, bool, bool);
2236 static void cp_parser_type_specifier_seq
2237 (cp_parser *, bool, bool, cp_decl_specifier_seq *);
2238 static tree cp_parser_parameter_declaration_clause
2239 (cp_parser *);
2240 static tree cp_parser_parameter_declaration_list
2241 (cp_parser *, bool *);
2242 static cp_parameter_declarator *cp_parser_parameter_declaration
2243 (cp_parser *, bool, bool *);
2244 static tree cp_parser_default_argument
2245 (cp_parser *, bool);
2246 static void cp_parser_function_body
2247 (cp_parser *, bool);
2248 static tree cp_parser_initializer
2249 (cp_parser *, bool *, bool *);
2250 static cp_expr cp_parser_initializer_clause
2251 (cp_parser *, bool *);
2252 static cp_expr cp_parser_braced_list
2253 (cp_parser*, bool*);
2254 static vec<constructor_elt, va_gc> *cp_parser_initializer_list
2255 (cp_parser *, bool *);
2257 static bool cp_parser_ctor_initializer_opt_and_function_body
2258 (cp_parser *, bool);
2260 static tree cp_parser_late_parsing_omp_declare_simd
2261 (cp_parser *, tree);
2263 static tree cp_parser_late_parsing_cilk_simd_fn_info
2264 (cp_parser *, tree);
2266 static tree cp_parser_late_parsing_oacc_routine
2267 (cp_parser *, tree);
2269 static tree synthesize_implicit_template_parm
2270 (cp_parser *, tree);
2271 static tree finish_fully_implicit_template
2272 (cp_parser *, tree);
2274 /* Classes [gram.class] */
2276 static tree cp_parser_class_name
2277 (cp_parser *, bool, bool, enum tag_types, bool, bool, bool, bool = false);
2278 static tree cp_parser_class_specifier
2279 (cp_parser *);
2280 static tree cp_parser_class_head
2281 (cp_parser *, bool *);
2282 static enum tag_types cp_parser_class_key
2283 (cp_parser *);
2284 static void cp_parser_type_parameter_key
2285 (cp_parser* parser);
2286 static void cp_parser_member_specification_opt
2287 (cp_parser *);
2288 static void cp_parser_member_declaration
2289 (cp_parser *);
2290 static tree cp_parser_pure_specifier
2291 (cp_parser *);
2292 static tree cp_parser_constant_initializer
2293 (cp_parser *);
2295 /* Derived classes [gram.class.derived] */
2297 static tree cp_parser_base_clause
2298 (cp_parser *);
2299 static tree cp_parser_base_specifier
2300 (cp_parser *);
2302 /* Special member functions [gram.special] */
2304 static tree cp_parser_conversion_function_id
2305 (cp_parser *);
2306 static tree cp_parser_conversion_type_id
2307 (cp_parser *);
2308 static cp_declarator *cp_parser_conversion_declarator_opt
2309 (cp_parser *);
2310 static bool cp_parser_ctor_initializer_opt
2311 (cp_parser *);
2312 static void cp_parser_mem_initializer_list
2313 (cp_parser *);
2314 static tree cp_parser_mem_initializer
2315 (cp_parser *);
2316 static tree cp_parser_mem_initializer_id
2317 (cp_parser *);
2319 /* Overloading [gram.over] */
2321 static cp_expr cp_parser_operator_function_id
2322 (cp_parser *);
2323 static cp_expr cp_parser_operator
2324 (cp_parser *);
2326 /* Templates [gram.temp] */
2328 static void cp_parser_template_declaration
2329 (cp_parser *, bool);
2330 static tree cp_parser_template_parameter_list
2331 (cp_parser *);
2332 static tree cp_parser_template_parameter
2333 (cp_parser *, bool *, bool *);
2334 static tree cp_parser_type_parameter
2335 (cp_parser *, bool *);
2336 static tree cp_parser_template_id
2337 (cp_parser *, bool, bool, enum tag_types, bool);
2338 static tree cp_parser_template_name
2339 (cp_parser *, bool, bool, bool, enum tag_types, bool *);
2340 static tree cp_parser_template_argument_list
2341 (cp_parser *);
2342 static tree cp_parser_template_argument
2343 (cp_parser *);
2344 static void cp_parser_explicit_instantiation
2345 (cp_parser *);
2346 static void cp_parser_explicit_specialization
2347 (cp_parser *);
2349 /* Exception handling [gram.exception] */
2351 static tree cp_parser_try_block
2352 (cp_parser *);
2353 static bool cp_parser_function_try_block
2354 (cp_parser *);
2355 static void cp_parser_handler_seq
2356 (cp_parser *);
2357 static void cp_parser_handler
2358 (cp_parser *);
2359 static tree cp_parser_exception_declaration
2360 (cp_parser *);
2361 static tree cp_parser_throw_expression
2362 (cp_parser *);
2363 static tree cp_parser_exception_specification_opt
2364 (cp_parser *);
2365 static tree cp_parser_type_id_list
2366 (cp_parser *);
2368 /* GNU Extensions */
2370 static tree cp_parser_asm_specification_opt
2371 (cp_parser *);
2372 static tree cp_parser_asm_operand_list
2373 (cp_parser *);
2374 static tree cp_parser_asm_clobber_list
2375 (cp_parser *);
2376 static tree cp_parser_asm_label_list
2377 (cp_parser *);
2378 static bool cp_next_tokens_can_be_attribute_p
2379 (cp_parser *);
2380 static bool cp_next_tokens_can_be_gnu_attribute_p
2381 (cp_parser *);
2382 static bool cp_next_tokens_can_be_std_attribute_p
2383 (cp_parser *);
2384 static bool cp_nth_tokens_can_be_std_attribute_p
2385 (cp_parser *, size_t);
2386 static bool cp_nth_tokens_can_be_gnu_attribute_p
2387 (cp_parser *, size_t);
2388 static bool cp_nth_tokens_can_be_attribute_p
2389 (cp_parser *, size_t);
2390 static tree cp_parser_attributes_opt
2391 (cp_parser *);
2392 static tree cp_parser_gnu_attributes_opt
2393 (cp_parser *);
2394 static tree cp_parser_gnu_attribute_list
2395 (cp_parser *);
2396 static tree cp_parser_std_attribute
2397 (cp_parser *, tree);
2398 static tree cp_parser_std_attribute_spec
2399 (cp_parser *);
2400 static tree cp_parser_std_attribute_spec_seq
2401 (cp_parser *);
2402 static bool cp_parser_extension_opt
2403 (cp_parser *, int *);
2404 static void cp_parser_label_declaration
2405 (cp_parser *);
2407 /* Concept Extensions */
2409 static tree cp_parser_requires_clause
2410 (cp_parser *);
2411 static tree cp_parser_requires_clause_opt
2412 (cp_parser *);
2413 static tree cp_parser_requires_expression
2414 (cp_parser *);
2415 static tree cp_parser_requirement_parameter_list
2416 (cp_parser *);
2417 static tree cp_parser_requirement_body
2418 (cp_parser *);
2419 static tree cp_parser_requirement_list
2420 (cp_parser *);
2421 static tree cp_parser_requirement
2422 (cp_parser *);
2423 static tree cp_parser_simple_requirement
2424 (cp_parser *);
2425 static tree cp_parser_compound_requirement
2426 (cp_parser *);
2427 static tree cp_parser_type_requirement
2428 (cp_parser *);
2429 static tree cp_parser_nested_requirement
2430 (cp_parser *);
2432 /* Transactional Memory Extensions */
2434 static tree cp_parser_transaction
2435 (cp_parser *, cp_token *);
2436 static tree cp_parser_transaction_expression
2437 (cp_parser *, enum rid);
2438 static bool cp_parser_function_transaction
2439 (cp_parser *, enum rid);
2440 static tree cp_parser_transaction_cancel
2441 (cp_parser *);
2443 enum pragma_context {
2444 pragma_external,
2445 pragma_member,
2446 pragma_objc_icode,
2447 pragma_stmt,
2448 pragma_compound
2450 static bool cp_parser_pragma
2451 (cp_parser *, enum pragma_context, bool *);
2453 /* Objective-C++ Productions */
2455 static tree cp_parser_objc_message_receiver
2456 (cp_parser *);
2457 static tree cp_parser_objc_message_args
2458 (cp_parser *);
2459 static tree cp_parser_objc_message_expression
2460 (cp_parser *);
2461 static cp_expr cp_parser_objc_encode_expression
2462 (cp_parser *);
2463 static tree cp_parser_objc_defs_expression
2464 (cp_parser *);
2465 static tree cp_parser_objc_protocol_expression
2466 (cp_parser *);
2467 static tree cp_parser_objc_selector_expression
2468 (cp_parser *);
2469 static cp_expr cp_parser_objc_expression
2470 (cp_parser *);
2471 static bool cp_parser_objc_selector_p
2472 (enum cpp_ttype);
2473 static tree cp_parser_objc_selector
2474 (cp_parser *);
2475 static tree cp_parser_objc_protocol_refs_opt
2476 (cp_parser *);
2477 static void cp_parser_objc_declaration
2478 (cp_parser *, tree);
2479 static tree cp_parser_objc_statement
2480 (cp_parser *);
2481 static bool cp_parser_objc_valid_prefix_attributes
2482 (cp_parser *, tree *);
2483 static void cp_parser_objc_at_property_declaration
2484 (cp_parser *) ;
2485 static void cp_parser_objc_at_synthesize_declaration
2486 (cp_parser *) ;
2487 static void cp_parser_objc_at_dynamic_declaration
2488 (cp_parser *) ;
2489 static tree cp_parser_objc_struct_declaration
2490 (cp_parser *) ;
2492 /* Utility Routines */
2494 static cp_expr cp_parser_lookup_name
2495 (cp_parser *, tree, enum tag_types, bool, bool, bool, tree *, location_t);
2496 static tree cp_parser_lookup_name_simple
2497 (cp_parser *, tree, location_t);
2498 static tree cp_parser_maybe_treat_template_as_class
2499 (tree, bool);
2500 static bool cp_parser_check_declarator_template_parameters
2501 (cp_parser *, cp_declarator *, location_t);
2502 static bool cp_parser_check_template_parameters
2503 (cp_parser *, unsigned, location_t, cp_declarator *);
2504 static cp_expr cp_parser_simple_cast_expression
2505 (cp_parser *);
2506 static tree cp_parser_global_scope_opt
2507 (cp_parser *, bool);
2508 static bool cp_parser_constructor_declarator_p
2509 (cp_parser *, bool);
2510 static tree cp_parser_function_definition_from_specifiers_and_declarator
2511 (cp_parser *, cp_decl_specifier_seq *, tree, const cp_declarator *);
2512 static tree cp_parser_function_definition_after_declarator
2513 (cp_parser *, bool);
2514 static bool cp_parser_template_declaration_after_export
2515 (cp_parser *, bool);
2516 static void cp_parser_perform_template_parameter_access_checks
2517 (vec<deferred_access_check, va_gc> *);
2518 static tree cp_parser_single_declaration
2519 (cp_parser *, vec<deferred_access_check, va_gc> *, bool, bool, bool *);
2520 static cp_expr cp_parser_functional_cast
2521 (cp_parser *, tree);
2522 static tree cp_parser_save_member_function_body
2523 (cp_parser *, cp_decl_specifier_seq *, cp_declarator *, tree);
2524 static tree cp_parser_save_nsdmi
2525 (cp_parser *);
2526 static tree cp_parser_enclosed_template_argument_list
2527 (cp_parser *);
2528 static void cp_parser_save_default_args
2529 (cp_parser *, tree);
2530 static void cp_parser_late_parsing_for_member
2531 (cp_parser *, tree);
2532 static tree cp_parser_late_parse_one_default_arg
2533 (cp_parser *, tree, tree, tree);
2534 static void cp_parser_late_parsing_nsdmi
2535 (cp_parser *, tree);
2536 static void cp_parser_late_parsing_default_args
2537 (cp_parser *, tree);
2538 static tree cp_parser_sizeof_operand
2539 (cp_parser *, enum rid);
2540 static tree cp_parser_trait_expr
2541 (cp_parser *, enum rid);
2542 static bool cp_parser_declares_only_class_p
2543 (cp_parser *);
2544 static void cp_parser_set_storage_class
2545 (cp_parser *, cp_decl_specifier_seq *, enum rid, cp_token *);
2546 static void cp_parser_set_decl_spec_type
2547 (cp_decl_specifier_seq *, tree, cp_token *, bool);
2548 static void set_and_check_decl_spec_loc
2549 (cp_decl_specifier_seq *decl_specs,
2550 cp_decl_spec ds, cp_token *);
2551 static bool cp_parser_friend_p
2552 (const cp_decl_specifier_seq *);
2553 static void cp_parser_required_error
2554 (cp_parser *, required_token, bool);
2555 static cp_token *cp_parser_require
2556 (cp_parser *, enum cpp_ttype, required_token);
2557 static cp_token *cp_parser_require_keyword
2558 (cp_parser *, enum rid, required_token);
2559 static bool cp_parser_token_starts_function_definition_p
2560 (cp_token *);
2561 static bool cp_parser_next_token_starts_class_definition_p
2562 (cp_parser *);
2563 static bool cp_parser_next_token_ends_template_argument_p
2564 (cp_parser *);
2565 static bool cp_parser_nth_token_starts_template_argument_list_p
2566 (cp_parser *, size_t);
2567 static enum tag_types cp_parser_token_is_class_key
2568 (cp_token *);
2569 static enum tag_types cp_parser_token_is_type_parameter_key
2570 (cp_token *);
2571 static void cp_parser_check_class_key
2572 (enum tag_types, tree type);
2573 static void cp_parser_check_access_in_redeclaration
2574 (tree type, location_t location);
2575 static bool cp_parser_optional_template_keyword
2576 (cp_parser *);
2577 static void cp_parser_pre_parsed_nested_name_specifier
2578 (cp_parser *);
2579 static bool cp_parser_cache_group
2580 (cp_parser *, enum cpp_ttype, unsigned);
2581 static tree cp_parser_cache_defarg
2582 (cp_parser *parser, bool nsdmi);
2583 static void cp_parser_parse_tentatively
2584 (cp_parser *);
2585 static void cp_parser_commit_to_tentative_parse
2586 (cp_parser *);
2587 static void cp_parser_commit_to_topmost_tentative_parse
2588 (cp_parser *);
2589 static void cp_parser_abort_tentative_parse
2590 (cp_parser *);
2591 static bool cp_parser_parse_definitely
2592 (cp_parser *);
2593 static inline bool cp_parser_parsing_tentatively
2594 (cp_parser *);
2595 static bool cp_parser_uncommitted_to_tentative_parse_p
2596 (cp_parser *);
2597 static void cp_parser_error
2598 (cp_parser *, const char *);
2599 static void cp_parser_name_lookup_error
2600 (cp_parser *, tree, tree, name_lookup_error, location_t);
2601 static bool cp_parser_simulate_error
2602 (cp_parser *);
2603 static bool cp_parser_check_type_definition
2604 (cp_parser *);
2605 static void cp_parser_check_for_definition_in_return_type
2606 (cp_declarator *, tree, location_t type_location);
2607 static void cp_parser_check_for_invalid_template_id
2608 (cp_parser *, tree, enum tag_types, location_t location);
2609 static bool cp_parser_non_integral_constant_expression
2610 (cp_parser *, non_integral_constant);
2611 static void cp_parser_diagnose_invalid_type_name
2612 (cp_parser *, tree, location_t);
2613 static bool cp_parser_parse_and_diagnose_invalid_type_name
2614 (cp_parser *);
2615 static int cp_parser_skip_to_closing_parenthesis
2616 (cp_parser *, bool, bool, bool);
2617 static void cp_parser_skip_to_end_of_statement
2618 (cp_parser *);
2619 static void cp_parser_consume_semicolon_at_end_of_statement
2620 (cp_parser *);
2621 static void cp_parser_skip_to_end_of_block_or_statement
2622 (cp_parser *);
2623 static bool cp_parser_skip_to_closing_brace
2624 (cp_parser *);
2625 static void cp_parser_skip_to_end_of_template_parameter_list
2626 (cp_parser *);
2627 static void cp_parser_skip_to_pragma_eol
2628 (cp_parser*, cp_token *);
2629 static bool cp_parser_error_occurred
2630 (cp_parser *);
2631 static bool cp_parser_allow_gnu_extensions_p
2632 (cp_parser *);
2633 static bool cp_parser_is_pure_string_literal
2634 (cp_token *);
2635 static bool cp_parser_is_string_literal
2636 (cp_token *);
2637 static bool cp_parser_is_keyword
2638 (cp_token *, enum rid);
2639 static tree cp_parser_make_typename_type
2640 (cp_parser *, tree, location_t location);
2641 static cp_declarator * cp_parser_make_indirect_declarator
2642 (enum tree_code, tree, cp_cv_quals, cp_declarator *, tree);
2643 static bool cp_parser_compound_literal_p
2644 (cp_parser *);
2645 static bool cp_parser_array_designator_p
2646 (cp_parser *);
2647 static bool cp_parser_init_statement_p
2648 (cp_parser *);
2649 static bool cp_parser_skip_to_closing_square_bracket
2650 (cp_parser *);
2652 /* Concept-related syntactic transformations */
2654 static tree cp_parser_maybe_concept_name (cp_parser *, tree);
2655 static tree cp_parser_maybe_partial_concept_id (cp_parser *, tree, tree);
2657 // -------------------------------------------------------------------------- //
2658 // Unevaluated Operand Guard
2660 // Implementation of an RAII helper for unevaluated operand parsing.
2661 cp_unevaluated::cp_unevaluated ()
2663 ++cp_unevaluated_operand;
2664 ++c_inhibit_evaluation_warnings;
2667 cp_unevaluated::~cp_unevaluated ()
2669 --c_inhibit_evaluation_warnings;
2670 --cp_unevaluated_operand;
2673 // -------------------------------------------------------------------------- //
2674 // Tentative Parsing
2676 /* Returns nonzero if we are parsing tentatively. */
2678 static inline bool
2679 cp_parser_parsing_tentatively (cp_parser* parser)
2681 return parser->context->next != NULL;
2684 /* Returns nonzero if TOKEN is a string literal. */
2686 static bool
2687 cp_parser_is_pure_string_literal (cp_token* token)
2689 return (token->type == CPP_STRING ||
2690 token->type == CPP_STRING16 ||
2691 token->type == CPP_STRING32 ||
2692 token->type == CPP_WSTRING ||
2693 token->type == CPP_UTF8STRING);
2696 /* Returns nonzero if TOKEN is a string literal
2697 of a user-defined string literal. */
2699 static bool
2700 cp_parser_is_string_literal (cp_token* token)
2702 return (cp_parser_is_pure_string_literal (token) ||
2703 token->type == CPP_STRING_USERDEF ||
2704 token->type == CPP_STRING16_USERDEF ||
2705 token->type == CPP_STRING32_USERDEF ||
2706 token->type == CPP_WSTRING_USERDEF ||
2707 token->type == CPP_UTF8STRING_USERDEF);
2710 /* Returns nonzero if TOKEN is the indicated KEYWORD. */
2712 static bool
2713 cp_parser_is_keyword (cp_token* token, enum rid keyword)
2715 return token->keyword == keyword;
2718 /* Return TOKEN's pragma_kind if it is CPP_PRAGMA, otherwise
2719 PRAGMA_NONE. */
2721 static enum pragma_kind
2722 cp_parser_pragma_kind (cp_token *token)
2724 if (token->type != CPP_PRAGMA)
2725 return PRAGMA_NONE;
2726 /* We smuggled the cpp_token->u.pragma value in an INTEGER_CST. */
2727 return (enum pragma_kind) TREE_INT_CST_LOW (token->u.value);
2730 /* Helper function for cp_parser_error.
2731 Having peeked a token of kind TOK1_KIND that might signify
2732 a conflict marker, peek successor tokens to determine
2733 if we actually do have a conflict marker.
2734 Specifically, we consider a run of 7 '<', '=' or '>' characters
2735 at the start of a line as a conflict marker.
2736 These come through the lexer as three pairs and a single,
2737 e.g. three CPP_LSHIFT tokens ("<<") and a CPP_LESS token ('<').
2738 If it returns true, *OUT_LOC is written to with the location/range
2739 of the marker. */
2741 static bool
2742 cp_lexer_peek_conflict_marker (cp_lexer *lexer, enum cpp_ttype tok1_kind,
2743 location_t *out_loc)
2745 cp_token *token2 = cp_lexer_peek_nth_token (lexer, 2);
2746 if (token2->type != tok1_kind)
2747 return false;
2748 cp_token *token3 = cp_lexer_peek_nth_token (lexer, 3);
2749 if (token3->type != tok1_kind)
2750 return false;
2751 cp_token *token4 = cp_lexer_peek_nth_token (lexer, 4);
2752 if (token4->type != conflict_marker_get_final_tok_kind (tok1_kind))
2753 return false;
2755 /* It must be at the start of the line. */
2756 location_t start_loc = cp_lexer_peek_token (lexer)->location;
2757 if (LOCATION_COLUMN (start_loc) != 1)
2758 return false;
2760 /* We have a conflict marker. Construct a location of the form:
2761 <<<<<<<
2762 ^~~~~~~
2763 with start == caret, finishing at the end of the marker. */
2764 location_t finish_loc = get_finish (token4->location);
2765 *out_loc = make_location (start_loc, start_loc, finish_loc);
2767 return true;
2770 /* If not parsing tentatively, issue a diagnostic of the form
2771 FILE:LINE: MESSAGE before TOKEN
2772 where TOKEN is the next token in the input stream. MESSAGE
2773 (specified by the caller) is usually of the form "expected
2774 OTHER-TOKEN". */
2776 static void
2777 cp_parser_error (cp_parser* parser, const char* gmsgid)
2779 if (!cp_parser_simulate_error (parser))
2781 cp_token *token = cp_lexer_peek_token (parser->lexer);
2782 /* This diagnostic makes more sense if it is tagged to the line
2783 of the token we just peeked at. */
2784 cp_lexer_set_source_position_from_token (token);
2786 if (token->type == CPP_PRAGMA)
2788 error_at (token->location,
2789 "%<#pragma%> is not allowed here");
2790 cp_parser_skip_to_pragma_eol (parser, token);
2791 return;
2794 /* If this is actually a conflict marker, report it as such. */
2795 if (token->type == CPP_LSHIFT
2796 || token->type == CPP_RSHIFT
2797 || token->type == CPP_EQ_EQ)
2799 location_t loc;
2800 if (cp_lexer_peek_conflict_marker (parser->lexer, token->type, &loc))
2802 error_at (loc, "version control conflict marker in file");
2803 return;
2807 c_parse_error (gmsgid,
2808 /* Because c_parser_error does not understand
2809 CPP_KEYWORD, keywords are treated like
2810 identifiers. */
2811 (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
2812 token->u.value, token->flags);
2816 /* Issue an error about name-lookup failing. NAME is the
2817 IDENTIFIER_NODE DECL is the result of
2818 the lookup (as returned from cp_parser_lookup_name). DESIRED is
2819 the thing that we hoped to find. */
2821 static void
2822 cp_parser_name_lookup_error (cp_parser* parser,
2823 tree name,
2824 tree decl,
2825 name_lookup_error desired,
2826 location_t location)
2828 /* If name lookup completely failed, tell the user that NAME was not
2829 declared. */
2830 if (decl == error_mark_node)
2832 if (parser->scope && parser->scope != global_namespace)
2833 error_at (location, "%<%E::%E%> has not been declared",
2834 parser->scope, name);
2835 else if (parser->scope == global_namespace)
2836 error_at (location, "%<::%E%> has not been declared", name);
2837 else if (parser->object_scope
2838 && !CLASS_TYPE_P (parser->object_scope))
2839 error_at (location, "request for member %qE in non-class type %qT",
2840 name, parser->object_scope);
2841 else if (parser->object_scope)
2842 error_at (location, "%<%T::%E%> has not been declared",
2843 parser->object_scope, name);
2844 else
2845 error_at (location, "%qE has not been declared", name);
2847 else if (parser->scope && parser->scope != global_namespace)
2849 switch (desired)
2851 case NLE_TYPE:
2852 error_at (location, "%<%E::%E%> is not a type",
2853 parser->scope, name);
2854 break;
2855 case NLE_CXX98:
2856 error_at (location, "%<%E::%E%> is not a class or namespace",
2857 parser->scope, name);
2858 break;
2859 case NLE_NOT_CXX98:
2860 error_at (location,
2861 "%<%E::%E%> is not a class, namespace, or enumeration",
2862 parser->scope, name);
2863 break;
2864 default:
2865 gcc_unreachable ();
2869 else if (parser->scope == global_namespace)
2871 switch (desired)
2873 case NLE_TYPE:
2874 error_at (location, "%<::%E%> is not a type", name);
2875 break;
2876 case NLE_CXX98:
2877 error_at (location, "%<::%E%> is not a class or namespace", name);
2878 break;
2879 case NLE_NOT_CXX98:
2880 error_at (location,
2881 "%<::%E%> is not a class, namespace, or enumeration",
2882 name);
2883 break;
2884 default:
2885 gcc_unreachable ();
2888 else
2890 switch (desired)
2892 case NLE_TYPE:
2893 error_at (location, "%qE is not a type", name);
2894 break;
2895 case NLE_CXX98:
2896 error_at (location, "%qE is not a class or namespace", name);
2897 break;
2898 case NLE_NOT_CXX98:
2899 error_at (location,
2900 "%qE is not a class, namespace, or enumeration", name);
2901 break;
2902 default:
2903 gcc_unreachable ();
2908 /* If we are parsing tentatively, remember that an error has occurred
2909 during this tentative parse. Returns true if the error was
2910 simulated; false if a message should be issued by the caller. */
2912 static bool
2913 cp_parser_simulate_error (cp_parser* parser)
2915 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2917 parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
2918 return true;
2920 return false;
2923 /* This function is called when a type is defined. If type
2924 definitions are forbidden at this point, an error message is
2925 issued. */
2927 static bool
2928 cp_parser_check_type_definition (cp_parser* parser)
2930 /* If types are forbidden here, issue a message. */
2931 if (parser->type_definition_forbidden_message)
2933 /* Don't use `%s' to print the string, because quotations (`%<', `%>')
2934 in the message need to be interpreted. */
2935 error (parser->type_definition_forbidden_message);
2936 return false;
2938 return true;
2941 /* This function is called when the DECLARATOR is processed. The TYPE
2942 was a type defined in the decl-specifiers. If it is invalid to
2943 define a type in the decl-specifiers for DECLARATOR, an error is
2944 issued. TYPE_LOCATION is the location of TYPE and is used
2945 for error reporting. */
2947 static void
2948 cp_parser_check_for_definition_in_return_type (cp_declarator *declarator,
2949 tree type, location_t type_location)
2951 /* [dcl.fct] forbids type definitions in return types.
2952 Unfortunately, it's not easy to know whether or not we are
2953 processing a return type until after the fact. */
2954 while (declarator
2955 && (declarator->kind == cdk_pointer
2956 || declarator->kind == cdk_reference
2957 || declarator->kind == cdk_ptrmem))
2958 declarator = declarator->declarator;
2959 if (declarator
2960 && declarator->kind == cdk_function)
2962 error_at (type_location,
2963 "new types may not be defined in a return type");
2964 inform (type_location,
2965 "(perhaps a semicolon is missing after the definition of %qT)",
2966 type);
2970 /* A type-specifier (TYPE) has been parsed which cannot be followed by
2971 "<" in any valid C++ program. If the next token is indeed "<",
2972 issue a message warning the user about what appears to be an
2973 invalid attempt to form a template-id. LOCATION is the location
2974 of the type-specifier (TYPE) */
2976 static void
2977 cp_parser_check_for_invalid_template_id (cp_parser* parser,
2978 tree type,
2979 enum tag_types tag_type,
2980 location_t location)
2982 cp_token_position start = 0;
2984 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
2986 if (TYPE_P (type))
2987 error_at (location, "%qT is not a template", type);
2988 else if (identifier_p (type))
2990 if (tag_type != none_type)
2991 error_at (location, "%qE is not a class template", type);
2992 else
2993 error_at (location, "%qE is not a template", type);
2995 else
2996 error_at (location, "invalid template-id");
2997 /* Remember the location of the invalid "<". */
2998 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2999 start = cp_lexer_token_position (parser->lexer, true);
3000 /* Consume the "<". */
3001 cp_lexer_consume_token (parser->lexer);
3002 /* Parse the template arguments. */
3003 cp_parser_enclosed_template_argument_list (parser);
3004 /* Permanently remove the invalid template arguments so that
3005 this error message is not issued again. */
3006 if (start)
3007 cp_lexer_purge_tokens_after (parser->lexer, start);
3011 /* If parsing an integral constant-expression, issue an error message
3012 about the fact that THING appeared and return true. Otherwise,
3013 return false. In either case, set
3014 PARSER->NON_INTEGRAL_CONSTANT_EXPRESSION_P. */
3016 static bool
3017 cp_parser_non_integral_constant_expression (cp_parser *parser,
3018 non_integral_constant thing)
3020 parser->non_integral_constant_expression_p = true;
3021 if (parser->integral_constant_expression_p)
3023 if (!parser->allow_non_integral_constant_expression_p)
3025 const char *msg = NULL;
3026 switch (thing)
3028 case NIC_FLOAT:
3029 pedwarn (input_location, OPT_Wpedantic,
3030 "ISO C++ forbids using a floating-point literal "
3031 "in a constant-expression");
3032 return true;
3033 case NIC_CAST:
3034 error ("a cast to a type other than an integral or "
3035 "enumeration type cannot appear in a "
3036 "constant-expression");
3037 return true;
3038 case NIC_TYPEID:
3039 error ("%<typeid%> operator "
3040 "cannot appear in a constant-expression");
3041 return true;
3042 case NIC_NCC:
3043 error ("non-constant compound literals "
3044 "cannot appear in a constant-expression");
3045 return true;
3046 case NIC_FUNC_CALL:
3047 error ("a function call "
3048 "cannot appear in a constant-expression");
3049 return true;
3050 case NIC_INC:
3051 error ("an increment "
3052 "cannot appear in a constant-expression");
3053 return true;
3054 case NIC_DEC:
3055 error ("an decrement "
3056 "cannot appear in a constant-expression");
3057 return true;
3058 case NIC_ARRAY_REF:
3059 error ("an array reference "
3060 "cannot appear in a constant-expression");
3061 return true;
3062 case NIC_ADDR_LABEL:
3063 error ("the address of a label "
3064 "cannot appear in a constant-expression");
3065 return true;
3066 case NIC_OVERLOADED:
3067 error ("calls to overloaded operators "
3068 "cannot appear in a constant-expression");
3069 return true;
3070 case NIC_ASSIGNMENT:
3071 error ("an assignment cannot appear in a constant-expression");
3072 return true;
3073 case NIC_COMMA:
3074 error ("a comma operator "
3075 "cannot appear in a constant-expression");
3076 return true;
3077 case NIC_CONSTRUCTOR:
3078 error ("a call to a constructor "
3079 "cannot appear in a constant-expression");
3080 return true;
3081 case NIC_TRANSACTION:
3082 error ("a transaction expression "
3083 "cannot appear in a constant-expression");
3084 return true;
3085 case NIC_THIS:
3086 msg = "this";
3087 break;
3088 case NIC_FUNC_NAME:
3089 msg = "__FUNCTION__";
3090 break;
3091 case NIC_PRETTY_FUNC:
3092 msg = "__PRETTY_FUNCTION__";
3093 break;
3094 case NIC_C99_FUNC:
3095 msg = "__func__";
3096 break;
3097 case NIC_VA_ARG:
3098 msg = "va_arg";
3099 break;
3100 case NIC_ARROW:
3101 msg = "->";
3102 break;
3103 case NIC_POINT:
3104 msg = ".";
3105 break;
3106 case NIC_STAR:
3107 msg = "*";
3108 break;
3109 case NIC_ADDR:
3110 msg = "&";
3111 break;
3112 case NIC_PREINCREMENT:
3113 msg = "++";
3114 break;
3115 case NIC_PREDECREMENT:
3116 msg = "--";
3117 break;
3118 case NIC_NEW:
3119 msg = "new";
3120 break;
3121 case NIC_DEL:
3122 msg = "delete";
3123 break;
3124 default:
3125 gcc_unreachable ();
3127 if (msg)
3128 error ("%qs cannot appear in a constant-expression", msg);
3129 return true;
3132 return false;
3135 /* Emit a diagnostic for an invalid type name. This function commits
3136 to the current active tentative parse, if any. (Otherwise, the
3137 problematic construct might be encountered again later, resulting
3138 in duplicate error messages.) LOCATION is the location of ID. */
3140 static void
3141 cp_parser_diagnose_invalid_type_name (cp_parser *parser, tree id,
3142 location_t location)
3144 tree decl, ambiguous_decls;
3145 cp_parser_commit_to_tentative_parse (parser);
3146 /* Try to lookup the identifier. */
3147 decl = cp_parser_lookup_name (parser, id, none_type,
3148 /*is_template=*/false,
3149 /*is_namespace=*/false,
3150 /*check_dependency=*/true,
3151 &ambiguous_decls, location);
3152 if (ambiguous_decls)
3153 /* If the lookup was ambiguous, an error will already have
3154 been issued. */
3155 return;
3156 /* If the lookup found a template-name, it means that the user forgot
3157 to specify an argument list. Emit a useful error message. */
3158 if (DECL_TYPE_TEMPLATE_P (decl))
3160 error_at (location,
3161 "invalid use of template-name %qE without an argument list",
3162 decl);
3163 if (DECL_CLASS_TEMPLATE_P (decl) && cxx_dialect < cxx1z)
3164 inform (location, "class template argument deduction is only available "
3165 "with -std=c++1z or -std=gnu++1z");
3166 inform (DECL_SOURCE_LOCATION (decl), "%qD declared here", decl);
3168 else if (TREE_CODE (id) == BIT_NOT_EXPR)
3169 error_at (location, "invalid use of destructor %qD as a type", id);
3170 else if (TREE_CODE (decl) == TYPE_DECL)
3171 /* Something like 'unsigned A a;' */
3172 error_at (location, "invalid combination of multiple type-specifiers");
3173 else if (!parser->scope)
3175 /* Issue an error message. */
3176 const char *suggestion = NULL;
3177 if (TREE_CODE (id) == IDENTIFIER_NODE)
3178 suggestion = lookup_name_fuzzy (id, FUZZY_LOOKUP_TYPENAME);
3179 if (suggestion)
3181 gcc_rich_location richloc (location);
3182 richloc.add_fixit_replace (suggestion);
3183 error_at_rich_loc (&richloc,
3184 "%qE does not name a type; did you mean %qs?",
3185 id, suggestion);
3187 else
3188 error_at (location, "%qE does not name a type", id);
3189 /* If we're in a template class, it's possible that the user was
3190 referring to a type from a base class. For example:
3192 template <typename T> struct A { typedef T X; };
3193 template <typename T> struct B : public A<T> { X x; };
3195 The user should have said "typename A<T>::X". */
3196 if (cxx_dialect < cxx11 && id == ridpointers[(int)RID_CONSTEXPR])
3197 inform (location, "C++11 %<constexpr%> only available with "
3198 "-std=c++11 or -std=gnu++11");
3199 else if (cxx_dialect < cxx11 && id == ridpointers[(int)RID_NOEXCEPT])
3200 inform (location, "C++11 %<noexcept%> only available with "
3201 "-std=c++11 or -std=gnu++11");
3202 else if (cxx_dialect < cxx11
3203 && TREE_CODE (id) == IDENTIFIER_NODE
3204 && !strcmp (IDENTIFIER_POINTER (id), "thread_local"))
3205 inform (location, "C++11 %<thread_local%> only available with "
3206 "-std=c++11 or -std=gnu++11");
3207 else if (!flag_concepts && id == ridpointers[(int)RID_CONCEPT])
3208 inform (location, "%<concept%> only available with -fconcepts");
3209 else if (processing_template_decl && current_class_type
3210 && TYPE_BINFO (current_class_type))
3212 tree b;
3214 for (b = TREE_CHAIN (TYPE_BINFO (current_class_type));
3216 b = TREE_CHAIN (b))
3218 tree base_type = BINFO_TYPE (b);
3219 if (CLASS_TYPE_P (base_type)
3220 && dependent_type_p (base_type))
3222 tree field;
3223 /* Go from a particular instantiation of the
3224 template (which will have an empty TYPE_FIELDs),
3225 to the main version. */
3226 base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type);
3227 for (field = TYPE_FIELDS (base_type);
3228 field;
3229 field = DECL_CHAIN (field))
3230 if (TREE_CODE (field) == TYPE_DECL
3231 && DECL_NAME (field) == id)
3233 inform (location,
3234 "(perhaps %<typename %T::%E%> was intended)",
3235 BINFO_TYPE (b), id);
3236 break;
3238 if (field)
3239 break;
3244 /* Here we diagnose qualified-ids where the scope is actually correct,
3245 but the identifier does not resolve to a valid type name. */
3246 else if (parser->scope != error_mark_node)
3248 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
3250 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3251 error_at (location_of (id),
3252 "%qE in namespace %qE does not name a template type",
3253 id, parser->scope);
3254 else
3255 error_at (location_of (id),
3256 "%qE in namespace %qE does not name a type",
3257 id, parser->scope);
3258 if (DECL_P (decl))
3259 inform (DECL_SOURCE_LOCATION (decl), "%qD declared here", decl);
3261 else if (CLASS_TYPE_P (parser->scope)
3262 && constructor_name_p (id, parser->scope))
3264 /* A<T>::A<T>() */
3265 error_at (location, "%<%T::%E%> names the constructor, not"
3266 " the type", parser->scope, id);
3267 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3268 error_at (location, "and %qT has no template constructors",
3269 parser->scope);
3271 else if (TYPE_P (parser->scope)
3272 && dependent_scope_p (parser->scope))
3273 error_at (location, "need %<typename%> before %<%T::%E%> because "
3274 "%qT is a dependent scope",
3275 parser->scope, id, parser->scope);
3276 else if (TYPE_P (parser->scope))
3278 if (!COMPLETE_TYPE_P (parser->scope))
3279 cxx_incomplete_type_error (location_of (id), NULL_TREE,
3280 parser->scope);
3281 else if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3282 error_at (location_of (id),
3283 "%qE in %q#T does not name a template type",
3284 id, parser->scope);
3285 else
3286 error_at (location_of (id),
3287 "%qE in %q#T does not name a type",
3288 id, parser->scope);
3289 if (DECL_P (decl))
3290 inform (DECL_SOURCE_LOCATION (decl), "%qD declared here", decl);
3292 else
3293 gcc_unreachable ();
3297 /* Check for a common situation where a type-name should be present,
3298 but is not, and issue a sensible error message. Returns true if an
3299 invalid type-name was detected.
3301 The situation handled by this function are variable declarations of the
3302 form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
3303 Usually, `ID' should name a type, but if we got here it means that it
3304 does not. We try to emit the best possible error message depending on
3305 how exactly the id-expression looks like. */
3307 static bool
3308 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser *parser)
3310 tree id;
3311 cp_token *token = cp_lexer_peek_token (parser->lexer);
3313 /* Avoid duplicate error about ambiguous lookup. */
3314 if (token->type == CPP_NESTED_NAME_SPECIFIER)
3316 cp_token *next = cp_lexer_peek_nth_token (parser->lexer, 2);
3317 if (next->type == CPP_NAME && next->error_reported)
3318 goto out;
3321 cp_parser_parse_tentatively (parser);
3322 id = cp_parser_id_expression (parser,
3323 /*template_keyword_p=*/false,
3324 /*check_dependency_p=*/true,
3325 /*template_p=*/NULL,
3326 /*declarator_p=*/true,
3327 /*optional_p=*/false);
3328 /* If the next token is a (, this is a function with no explicit return
3329 type, i.e. constructor, destructor or conversion op. */
3330 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
3331 || TREE_CODE (id) == TYPE_DECL)
3333 cp_parser_abort_tentative_parse (parser);
3334 return false;
3336 if (!cp_parser_parse_definitely (parser))
3337 return false;
3339 /* Emit a diagnostic for the invalid type. */
3340 cp_parser_diagnose_invalid_type_name (parser, id, token->location);
3341 out:
3342 /* If we aren't in the middle of a declarator (i.e. in a
3343 parameter-declaration-clause), skip to the end of the declaration;
3344 there's no point in trying to process it. */
3345 if (!parser->in_declarator_p)
3346 cp_parser_skip_to_end_of_block_or_statement (parser);
3347 return true;
3350 /* Consume tokens up to, and including, the next non-nested closing `)'.
3351 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
3352 are doing error recovery. Returns -1 if OR_TTYPE is not CPP_EOF and we
3353 found an unnested token of that type. */
3355 static int
3356 cp_parser_skip_to_closing_parenthesis_1 (cp_parser *parser,
3357 bool recovering,
3358 cpp_ttype or_ttype,
3359 bool consume_paren)
3361 unsigned paren_depth = 0;
3362 unsigned brace_depth = 0;
3363 unsigned square_depth = 0;
3365 if (recovering && or_ttype == CPP_EOF
3366 && cp_parser_uncommitted_to_tentative_parse_p (parser))
3367 return 0;
3369 while (true)
3371 cp_token * token = cp_lexer_peek_token (parser->lexer);
3373 /* Have we found what we're looking for before the closing paren? */
3374 if (token->type == or_ttype && or_ttype != CPP_EOF
3375 && !brace_depth && !paren_depth && !square_depth)
3376 return -1;
3378 switch (token->type)
3380 case CPP_EOF:
3381 case CPP_PRAGMA_EOL:
3382 /* If we've run out of tokens, then there is no closing `)'. */
3383 return 0;
3385 /* This is good for lambda expression capture-lists. */
3386 case CPP_OPEN_SQUARE:
3387 ++square_depth;
3388 break;
3389 case CPP_CLOSE_SQUARE:
3390 if (!square_depth--)
3391 return 0;
3392 break;
3394 case CPP_SEMICOLON:
3395 /* This matches the processing in skip_to_end_of_statement. */
3396 if (!brace_depth)
3397 return 0;
3398 break;
3400 case CPP_OPEN_BRACE:
3401 ++brace_depth;
3402 break;
3403 case CPP_CLOSE_BRACE:
3404 if (!brace_depth--)
3405 return 0;
3406 break;
3408 case CPP_OPEN_PAREN:
3409 if (!brace_depth)
3410 ++paren_depth;
3411 break;
3413 case CPP_CLOSE_PAREN:
3414 if (!brace_depth && !paren_depth--)
3416 if (consume_paren)
3417 cp_lexer_consume_token (parser->lexer);
3418 return 1;
3420 break;
3422 default:
3423 break;
3426 /* Consume the token. */
3427 cp_lexer_consume_token (parser->lexer);
3431 /* Consume tokens up to, and including, the next non-nested closing `)'.
3432 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
3433 are doing error recovery. Returns -1 if OR_COMMA is true and we
3434 found an unnested token of that type. */
3436 static int
3437 cp_parser_skip_to_closing_parenthesis (cp_parser *parser,
3438 bool recovering,
3439 bool or_comma,
3440 bool consume_paren)
3442 cpp_ttype ttype = or_comma ? CPP_COMMA : CPP_EOF;
3443 return cp_parser_skip_to_closing_parenthesis_1 (parser, recovering,
3444 ttype, consume_paren);
3447 /* Consume tokens until we reach the end of the current statement.
3448 Normally, that will be just before consuming a `;'. However, if a
3449 non-nested `}' comes first, then we stop before consuming that. */
3451 static void
3452 cp_parser_skip_to_end_of_statement (cp_parser* parser)
3454 unsigned nesting_depth = 0;
3456 /* Unwind generic function template scope if necessary. */
3457 if (parser->fully_implicit_function_template_p)
3458 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
3460 while (true)
3462 cp_token *token = cp_lexer_peek_token (parser->lexer);
3464 switch (token->type)
3466 case CPP_EOF:
3467 case CPP_PRAGMA_EOL:
3468 /* If we've run out of tokens, stop. */
3469 return;
3471 case CPP_SEMICOLON:
3472 /* If the next token is a `;', we have reached the end of the
3473 statement. */
3474 if (!nesting_depth)
3475 return;
3476 break;
3478 case CPP_CLOSE_BRACE:
3479 /* If this is a non-nested '}', stop before consuming it.
3480 That way, when confronted with something like:
3482 { 3 + }
3484 we stop before consuming the closing '}', even though we
3485 have not yet reached a `;'. */
3486 if (nesting_depth == 0)
3487 return;
3489 /* If it is the closing '}' for a block that we have
3490 scanned, stop -- but only after consuming the token.
3491 That way given:
3493 void f g () { ... }
3494 typedef int I;
3496 we will stop after the body of the erroneously declared
3497 function, but before consuming the following `typedef'
3498 declaration. */
3499 if (--nesting_depth == 0)
3501 cp_lexer_consume_token (parser->lexer);
3502 return;
3504 break;
3506 case CPP_OPEN_BRACE:
3507 ++nesting_depth;
3508 break;
3510 default:
3511 break;
3514 /* Consume the token. */
3515 cp_lexer_consume_token (parser->lexer);
3519 /* This function is called at the end of a statement or declaration.
3520 If the next token is a semicolon, it is consumed; otherwise, error
3521 recovery is attempted. */
3523 static void
3524 cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
3526 /* Look for the trailing `;'. */
3527 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
3529 /* If there is additional (erroneous) input, skip to the end of
3530 the statement. */
3531 cp_parser_skip_to_end_of_statement (parser);
3532 /* If the next token is now a `;', consume it. */
3533 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
3534 cp_lexer_consume_token (parser->lexer);
3538 /* Skip tokens until we have consumed an entire block, or until we
3539 have consumed a non-nested `;'. */
3541 static void
3542 cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
3544 int nesting_depth = 0;
3546 /* Unwind generic function template scope if necessary. */
3547 if (parser->fully_implicit_function_template_p)
3548 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
3550 while (nesting_depth >= 0)
3552 cp_token *token = cp_lexer_peek_token (parser->lexer);
3554 switch (token->type)
3556 case CPP_EOF:
3557 case CPP_PRAGMA_EOL:
3558 /* If we've run out of tokens, stop. */
3559 return;
3561 case CPP_SEMICOLON:
3562 /* Stop if this is an unnested ';'. */
3563 if (!nesting_depth)
3564 nesting_depth = -1;
3565 break;
3567 case CPP_CLOSE_BRACE:
3568 /* Stop if this is an unnested '}', or closes the outermost
3569 nesting level. */
3570 nesting_depth--;
3571 if (nesting_depth < 0)
3572 return;
3573 if (!nesting_depth)
3574 nesting_depth = -1;
3575 break;
3577 case CPP_OPEN_BRACE:
3578 /* Nest. */
3579 nesting_depth++;
3580 break;
3582 default:
3583 break;
3586 /* Consume the token. */
3587 cp_lexer_consume_token (parser->lexer);
3591 /* Skip tokens until a non-nested closing curly brace is the next
3592 token, or there are no more tokens. Return true in the first case,
3593 false otherwise. */
3595 static bool
3596 cp_parser_skip_to_closing_brace (cp_parser *parser)
3598 unsigned nesting_depth = 0;
3600 while (true)
3602 cp_token *token = cp_lexer_peek_token (parser->lexer);
3604 switch (token->type)
3606 case CPP_EOF:
3607 case CPP_PRAGMA_EOL:
3608 /* If we've run out of tokens, stop. */
3609 return false;
3611 case CPP_CLOSE_BRACE:
3612 /* If the next token is a non-nested `}', then we have reached
3613 the end of the current block. */
3614 if (nesting_depth-- == 0)
3615 return true;
3616 break;
3618 case CPP_OPEN_BRACE:
3619 /* If it the next token is a `{', then we are entering a new
3620 block. Consume the entire block. */
3621 ++nesting_depth;
3622 break;
3624 default:
3625 break;
3628 /* Consume the token. */
3629 cp_lexer_consume_token (parser->lexer);
3633 /* Consume tokens until we reach the end of the pragma. The PRAGMA_TOK
3634 parameter is the PRAGMA token, allowing us to purge the entire pragma
3635 sequence. */
3637 static void
3638 cp_parser_skip_to_pragma_eol (cp_parser* parser, cp_token *pragma_tok)
3640 cp_token *token;
3642 parser->lexer->in_pragma = false;
3645 token = cp_lexer_consume_token (parser->lexer);
3646 while (token->type != CPP_PRAGMA_EOL && token->type != CPP_EOF);
3648 /* Ensure that the pragma is not parsed again. */
3649 cp_lexer_purge_tokens_after (parser->lexer, pragma_tok);
3652 /* Require pragma end of line, resyncing with it as necessary. The
3653 arguments are as for cp_parser_skip_to_pragma_eol. */
3655 static void
3656 cp_parser_require_pragma_eol (cp_parser *parser, cp_token *pragma_tok)
3658 parser->lexer->in_pragma = false;
3659 if (!cp_parser_require (parser, CPP_PRAGMA_EOL, RT_PRAGMA_EOL))
3660 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
3663 /* This is a simple wrapper around make_typename_type. When the id is
3664 an unresolved identifier node, we can provide a superior diagnostic
3665 using cp_parser_diagnose_invalid_type_name. */
3667 static tree
3668 cp_parser_make_typename_type (cp_parser *parser, tree id,
3669 location_t id_location)
3671 tree result;
3672 if (identifier_p (id))
3674 result = make_typename_type (parser->scope, id, typename_type,
3675 /*complain=*/tf_none);
3676 if (result == error_mark_node)
3677 cp_parser_diagnose_invalid_type_name (parser, id, id_location);
3678 return result;
3680 return make_typename_type (parser->scope, id, typename_type, tf_error);
3683 /* This is a wrapper around the
3684 make_{pointer,ptrmem,reference}_declarator functions that decides
3685 which one to call based on the CODE and CLASS_TYPE arguments. The
3686 CODE argument should be one of the values returned by
3687 cp_parser_ptr_operator. ATTRIBUTES represent the attributes that
3688 appertain to the pointer or reference. */
3690 static cp_declarator *
3691 cp_parser_make_indirect_declarator (enum tree_code code, tree class_type,
3692 cp_cv_quals cv_qualifiers,
3693 cp_declarator *target,
3694 tree attributes)
3696 if (code == ERROR_MARK)
3697 return cp_error_declarator;
3699 if (code == INDIRECT_REF)
3700 if (class_type == NULL_TREE)
3701 return make_pointer_declarator (cv_qualifiers, target, attributes);
3702 else
3703 return make_ptrmem_declarator (cv_qualifiers, class_type,
3704 target, attributes);
3705 else if (code == ADDR_EXPR && class_type == NULL_TREE)
3706 return make_reference_declarator (cv_qualifiers, target,
3707 false, attributes);
3708 else if (code == NON_LVALUE_EXPR && class_type == NULL_TREE)
3709 return make_reference_declarator (cv_qualifiers, target,
3710 true, attributes);
3711 gcc_unreachable ();
3714 /* Create a new C++ parser. */
3716 static cp_parser *
3717 cp_parser_new (void)
3719 cp_parser *parser;
3720 cp_lexer *lexer;
3721 unsigned i;
3723 /* cp_lexer_new_main is called before doing GC allocation because
3724 cp_lexer_new_main might load a PCH file. */
3725 lexer = cp_lexer_new_main ();
3727 /* Initialize the binops_by_token so that we can get the tree
3728 directly from the token. */
3729 for (i = 0; i < sizeof (binops) / sizeof (binops[0]); i++)
3730 binops_by_token[binops[i].token_type] = binops[i];
3732 parser = ggc_cleared_alloc<cp_parser> ();
3733 parser->lexer = lexer;
3734 parser->context = cp_parser_context_new (NULL);
3736 /* For now, we always accept GNU extensions. */
3737 parser->allow_gnu_extensions_p = 1;
3739 /* The `>' token is a greater-than operator, not the end of a
3740 template-id. */
3741 parser->greater_than_is_operator_p = true;
3743 parser->default_arg_ok_p = true;
3745 /* We are not parsing a constant-expression. */
3746 parser->integral_constant_expression_p = false;
3747 parser->allow_non_integral_constant_expression_p = false;
3748 parser->non_integral_constant_expression_p = false;
3750 /* Local variable names are not forbidden. */
3751 parser->local_variables_forbidden_p = false;
3753 /* We are not processing an `extern "C"' declaration. */
3754 parser->in_unbraced_linkage_specification_p = false;
3756 /* We are not processing a declarator. */
3757 parser->in_declarator_p = false;
3759 /* We are not processing a template-argument-list. */
3760 parser->in_template_argument_list_p = false;
3762 /* We are not in an iteration statement. */
3763 parser->in_statement = 0;
3765 /* We are not in a switch statement. */
3766 parser->in_switch_statement_p = false;
3768 /* We are not parsing a type-id inside an expression. */
3769 parser->in_type_id_in_expr_p = false;
3771 /* Declarations aren't implicitly extern "C". */
3772 parser->implicit_extern_c = false;
3774 /* String literals should be translated to the execution character set. */
3775 parser->translate_strings_p = true;
3777 /* We are not parsing a function body. */
3778 parser->in_function_body = false;
3780 /* We can correct until told otherwise. */
3781 parser->colon_corrects_to_scope_p = true;
3783 /* The unparsed function queue is empty. */
3784 push_unparsed_function_queues (parser);
3786 /* There are no classes being defined. */
3787 parser->num_classes_being_defined = 0;
3789 /* No template parameters apply. */
3790 parser->num_template_parameter_lists = 0;
3792 /* Special parsing data structures. */
3793 parser->omp_declare_simd = NULL;
3794 parser->cilk_simd_fn_info = NULL;
3795 parser->oacc_routine = NULL;
3797 /* Not declaring an implicit function template. */
3798 parser->auto_is_implicit_function_template_parm_p = false;
3799 parser->fully_implicit_function_template_p = false;
3800 parser->implicit_template_parms = 0;
3801 parser->implicit_template_scope = 0;
3803 /* Allow constrained-type-specifiers. */
3804 parser->prevent_constrained_type_specifiers = 0;
3806 return parser;
3809 /* Create a cp_lexer structure which will emit the tokens in CACHE
3810 and push it onto the parser's lexer stack. This is used for delayed
3811 parsing of in-class method bodies and default arguments, and should
3812 not be confused with tentative parsing. */
3813 static void
3814 cp_parser_push_lexer_for_tokens (cp_parser *parser, cp_token_cache *cache)
3816 cp_lexer *lexer = cp_lexer_new_from_tokens (cache);
3817 lexer->next = parser->lexer;
3818 parser->lexer = lexer;
3820 /* Move the current source position to that of the first token in the
3821 new lexer. */
3822 cp_lexer_set_source_position_from_token (lexer->next_token);
3825 /* Pop the top lexer off the parser stack. This is never used for the
3826 "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens. */
3827 static void
3828 cp_parser_pop_lexer (cp_parser *parser)
3830 cp_lexer *lexer = parser->lexer;
3831 parser->lexer = lexer->next;
3832 cp_lexer_destroy (lexer);
3834 /* Put the current source position back where it was before this
3835 lexer was pushed. */
3836 cp_lexer_set_source_position_from_token (parser->lexer->next_token);
3839 /* Lexical conventions [gram.lex] */
3841 /* Parse an identifier. Returns an IDENTIFIER_NODE representing the
3842 identifier. */
3844 static cp_expr
3845 cp_parser_identifier (cp_parser* parser)
3847 cp_token *token;
3849 /* Look for the identifier. */
3850 token = cp_parser_require (parser, CPP_NAME, RT_NAME);
3851 /* Return the value. */
3852 if (token)
3853 return cp_expr (token->u.value, token->location);
3854 else
3855 return error_mark_node;
3858 /* Parse a sequence of adjacent string constants. Returns a
3859 TREE_STRING representing the combined, nul-terminated string
3860 constant. If TRANSLATE is true, translate the string to the
3861 execution character set. If WIDE_OK is true, a wide string is
3862 invalid here.
3864 C++98 [lex.string] says that if a narrow string literal token is
3865 adjacent to a wide string literal token, the behavior is undefined.
3866 However, C99 6.4.5p4 says that this results in a wide string literal.
3867 We follow C99 here, for consistency with the C front end.
3869 This code is largely lifted from lex_string() in c-lex.c.
3871 FUTURE: ObjC++ will need to handle @-strings here. */
3872 static cp_expr
3873 cp_parser_string_literal (cp_parser *parser, bool translate, bool wide_ok,
3874 bool lookup_udlit = true)
3876 tree value;
3877 size_t count;
3878 struct obstack str_ob;
3879 cpp_string str, istr, *strs;
3880 cp_token *tok;
3881 enum cpp_ttype type, curr_type;
3882 int have_suffix_p = 0;
3883 tree string_tree;
3884 tree suffix_id = NULL_TREE;
3885 bool curr_tok_is_userdef_p = false;
3887 tok = cp_lexer_peek_token (parser->lexer);
3888 if (!cp_parser_is_string_literal (tok))
3890 cp_parser_error (parser, "expected string-literal");
3891 return error_mark_node;
3894 location_t loc = tok->location;
3896 if (cpp_userdef_string_p (tok->type))
3898 string_tree = USERDEF_LITERAL_VALUE (tok->u.value);
3899 curr_type = cpp_userdef_string_remove_type (tok->type);
3900 curr_tok_is_userdef_p = true;
3902 else
3904 string_tree = tok->u.value;
3905 curr_type = tok->type;
3907 type = curr_type;
3909 /* Try to avoid the overhead of creating and destroying an obstack
3910 for the common case of just one string. */
3911 if (!cp_parser_is_string_literal
3912 (cp_lexer_peek_nth_token (parser->lexer, 2)))
3914 cp_lexer_consume_token (parser->lexer);
3916 str.text = (const unsigned char *)TREE_STRING_POINTER (string_tree);
3917 str.len = TREE_STRING_LENGTH (string_tree);
3918 count = 1;
3920 if (curr_tok_is_userdef_p)
3922 suffix_id = USERDEF_LITERAL_SUFFIX_ID (tok->u.value);
3923 have_suffix_p = 1;
3924 curr_type = cpp_userdef_string_remove_type (tok->type);
3926 else
3927 curr_type = tok->type;
3929 strs = &str;
3931 else
3933 location_t last_tok_loc = tok->location;
3934 gcc_obstack_init (&str_ob);
3935 count = 0;
3939 cp_lexer_consume_token (parser->lexer);
3940 count++;
3941 str.text = (const unsigned char *)TREE_STRING_POINTER (string_tree);
3942 str.len = TREE_STRING_LENGTH (string_tree);
3944 if (curr_tok_is_userdef_p)
3946 tree curr_suffix_id = USERDEF_LITERAL_SUFFIX_ID (tok->u.value);
3947 if (have_suffix_p == 0)
3949 suffix_id = curr_suffix_id;
3950 have_suffix_p = 1;
3952 else if (have_suffix_p == 1
3953 && curr_suffix_id != suffix_id)
3955 error ("inconsistent user-defined literal suffixes"
3956 " %qD and %qD in string literal",
3957 suffix_id, curr_suffix_id);
3958 have_suffix_p = -1;
3960 curr_type = cpp_userdef_string_remove_type (tok->type);
3962 else
3963 curr_type = tok->type;
3965 if (type != curr_type)
3967 if (type == CPP_STRING)
3968 type = curr_type;
3969 else if (curr_type != CPP_STRING)
3971 rich_location rich_loc (line_table, tok->location);
3972 rich_loc.add_range (last_tok_loc, false);
3973 error_at_rich_loc (&rich_loc,
3974 "unsupported non-standard concatenation "
3975 "of string literals");
3979 obstack_grow (&str_ob, &str, sizeof (cpp_string));
3981 last_tok_loc = tok->location;
3983 tok = cp_lexer_peek_token (parser->lexer);
3984 if (cpp_userdef_string_p (tok->type))
3986 string_tree = USERDEF_LITERAL_VALUE (tok->u.value);
3987 curr_type = cpp_userdef_string_remove_type (tok->type);
3988 curr_tok_is_userdef_p = true;
3990 else
3992 string_tree = tok->u.value;
3993 curr_type = tok->type;
3994 curr_tok_is_userdef_p = false;
3997 while (cp_parser_is_string_literal (tok));
3999 /* A string literal built by concatenation has its caret=start at
4000 the start of the initial string, and its finish at the finish of
4001 the final string literal. */
4002 loc = make_location (loc, loc, get_finish (last_tok_loc));
4004 strs = (cpp_string *) obstack_finish (&str_ob);
4007 if (type != CPP_STRING && !wide_ok)
4009 cp_parser_error (parser, "a wide string is invalid in this context");
4010 type = CPP_STRING;
4013 if ((translate ? cpp_interpret_string : cpp_interpret_string_notranslate)
4014 (parse_in, strs, count, &istr, type))
4016 value = build_string (istr.len, (const char *)istr.text);
4017 free (CONST_CAST (unsigned char *, istr.text));
4019 switch (type)
4021 default:
4022 case CPP_STRING:
4023 case CPP_UTF8STRING:
4024 TREE_TYPE (value) = char_array_type_node;
4025 break;
4026 case CPP_STRING16:
4027 TREE_TYPE (value) = char16_array_type_node;
4028 break;
4029 case CPP_STRING32:
4030 TREE_TYPE (value) = char32_array_type_node;
4031 break;
4032 case CPP_WSTRING:
4033 TREE_TYPE (value) = wchar_array_type_node;
4034 break;
4037 value = fix_string_type (value);
4039 if (have_suffix_p)
4041 tree literal = build_userdef_literal (suffix_id, value,
4042 OT_NONE, NULL_TREE);
4043 if (lookup_udlit)
4044 value = cp_parser_userdef_string_literal (literal);
4045 else
4046 value = literal;
4049 else
4050 /* cpp_interpret_string has issued an error. */
4051 value = error_mark_node;
4053 if (count > 1)
4054 obstack_free (&str_ob, 0);
4056 return cp_expr (value, loc);
4059 /* Look up a literal operator with the name and the exact arguments. */
4061 static tree
4062 lookup_literal_operator (tree name, vec<tree, va_gc> *args)
4064 tree decl, fns;
4065 decl = lookup_name (name);
4066 if (!decl || !is_overloaded_fn (decl))
4067 return error_mark_node;
4069 for (fns = decl; fns; fns = OVL_NEXT (fns))
4071 unsigned int ix;
4072 bool found = true;
4073 tree fn = OVL_CURRENT (fns);
4074 tree parmtypes = TYPE_ARG_TYPES (TREE_TYPE (fn));
4075 if (parmtypes != NULL_TREE)
4077 for (ix = 0; ix < vec_safe_length (args) && parmtypes != NULL_TREE;
4078 ++ix, parmtypes = TREE_CHAIN (parmtypes))
4080 tree tparm = TREE_VALUE (parmtypes);
4081 tree targ = TREE_TYPE ((*args)[ix]);
4082 bool ptr = TYPE_PTR_P (tparm);
4083 bool arr = TREE_CODE (targ) == ARRAY_TYPE;
4084 if ((ptr || arr || !same_type_p (tparm, targ))
4085 && (!ptr || !arr
4086 || !same_type_p (TREE_TYPE (tparm),
4087 TREE_TYPE (targ))))
4088 found = false;
4090 if (found
4091 && ix == vec_safe_length (args)
4092 /* May be this should be sufficient_parms_p instead,
4093 depending on how exactly should user-defined literals
4094 work in presence of default arguments on the literal
4095 operator parameters. */
4096 && parmtypes == void_list_node)
4097 return decl;
4101 return error_mark_node;
4104 /* Parse a user-defined char constant. Returns a call to a user-defined
4105 literal operator taking the character as an argument. */
4107 static cp_expr
4108 cp_parser_userdef_char_literal (cp_parser *parser)
4110 cp_token *token = cp_lexer_consume_token (parser->lexer);
4111 tree literal = token->u.value;
4112 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
4113 tree value = USERDEF_LITERAL_VALUE (literal);
4114 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
4115 tree decl, result;
4117 /* Build up a call to the user-defined operator */
4118 /* Lookup the name we got back from the id-expression. */
4119 vec<tree, va_gc> *args = make_tree_vector ();
4120 vec_safe_push (args, value);
4121 decl = lookup_literal_operator (name, args);
4122 if (!decl || decl == error_mark_node)
4124 error ("unable to find character literal operator %qD with %qT argument",
4125 name, TREE_TYPE (value));
4126 release_tree_vector (args);
4127 return error_mark_node;
4129 result = finish_call_expr (decl, &args, false, true, tf_warning_or_error);
4130 release_tree_vector (args);
4131 return result;
4134 /* A subroutine of cp_parser_userdef_numeric_literal to
4135 create a char... template parameter pack from a string node. */
4137 static tree
4138 make_char_string_pack (tree value)
4140 tree charvec;
4141 tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
4142 const char *str = TREE_STRING_POINTER (value);
4143 int i, len = TREE_STRING_LENGTH (value) - 1;
4144 tree argvec = make_tree_vec (1);
4146 /* Fill in CHARVEC with all of the parameters. */
4147 charvec = make_tree_vec (len);
4148 for (i = 0; i < len; ++i)
4149 TREE_VEC_ELT (charvec, i) = build_int_cst (char_type_node, str[i]);
4151 /* Build the argument packs. */
4152 SET_ARGUMENT_PACK_ARGS (argpack, charvec);
4153 TREE_TYPE (argpack) = char_type_node;
4155 TREE_VEC_ELT (argvec, 0) = argpack;
4157 return argvec;
4160 /* A subroutine of cp_parser_userdef_numeric_literal to
4161 create a char... template parameter pack from a string node. */
4163 static tree
4164 make_string_pack (tree value)
4166 tree charvec;
4167 tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
4168 const unsigned char *str
4169 = (const unsigned char *) TREE_STRING_POINTER (value);
4170 int sz = TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (value))));
4171 int len = TREE_STRING_LENGTH (value) / sz - 1;
4172 tree argvec = make_tree_vec (2);
4174 tree str_char_type_node = TREE_TYPE (TREE_TYPE (value));
4175 str_char_type_node = TYPE_MAIN_VARIANT (str_char_type_node);
4177 /* First template parm is character type. */
4178 TREE_VEC_ELT (argvec, 0) = str_char_type_node;
4180 /* Fill in CHARVEC with all of the parameters. */
4181 charvec = make_tree_vec (len);
4182 for (int i = 0; i < len; ++i)
4183 TREE_VEC_ELT (charvec, i)
4184 = double_int_to_tree (str_char_type_node,
4185 double_int::from_buffer (str + i * sz, sz));
4187 /* Build the argument packs. */
4188 SET_ARGUMENT_PACK_ARGS (argpack, charvec);
4189 TREE_TYPE (argpack) = str_char_type_node;
4191 TREE_VEC_ELT (argvec, 1) = argpack;
4193 return argvec;
4196 /* Parse a user-defined numeric constant. returns a call to a user-defined
4197 literal operator. */
4199 static cp_expr
4200 cp_parser_userdef_numeric_literal (cp_parser *parser)
4202 cp_token *token = cp_lexer_consume_token (parser->lexer);
4203 tree literal = token->u.value;
4204 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
4205 tree value = USERDEF_LITERAL_VALUE (literal);
4206 int overflow = USERDEF_LITERAL_OVERFLOW (literal);
4207 tree num_string = USERDEF_LITERAL_NUM_STRING (literal);
4208 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
4209 tree decl, result;
4210 vec<tree, va_gc> *args;
4212 /* Look for a literal operator taking the exact type of numeric argument
4213 as the literal value. */
4214 args = make_tree_vector ();
4215 vec_safe_push (args, value);
4216 decl = lookup_literal_operator (name, args);
4217 if (decl && decl != error_mark_node)
4219 result = finish_call_expr (decl, &args, false, true,
4220 tf_warning_or_error);
4222 if (TREE_CODE (TREE_TYPE (value)) == INTEGER_TYPE && overflow > 0)
4224 warning_at (token->location, OPT_Woverflow,
4225 "integer literal exceeds range of %qT type",
4226 long_long_unsigned_type_node);
4228 else
4230 if (overflow > 0)
4231 warning_at (token->location, OPT_Woverflow,
4232 "floating literal exceeds range of %qT type",
4233 long_double_type_node);
4234 else if (overflow < 0)
4235 warning_at (token->location, OPT_Woverflow,
4236 "floating literal truncated to zero");
4239 release_tree_vector (args);
4240 return result;
4242 release_tree_vector (args);
4244 /* If the numeric argument didn't work, look for a raw literal
4245 operator taking a const char* argument consisting of the number
4246 in string format. */
4247 args = make_tree_vector ();
4248 vec_safe_push (args, num_string);
4249 decl = lookup_literal_operator (name, args);
4250 if (decl && decl != error_mark_node)
4252 result = finish_call_expr (decl, &args, false, true,
4253 tf_warning_or_error);
4254 release_tree_vector (args);
4255 return result;
4257 release_tree_vector (args);
4259 /* If the raw literal didn't work, look for a non-type template
4260 function with parameter pack char.... Call the function with
4261 template parameter characters representing the number. */
4262 args = make_tree_vector ();
4263 decl = lookup_literal_operator (name, args);
4264 if (decl && decl != error_mark_node)
4266 tree tmpl_args = make_char_string_pack (num_string);
4267 decl = lookup_template_function (decl, tmpl_args);
4268 result = finish_call_expr (decl, &args, false, true,
4269 tf_warning_or_error);
4270 release_tree_vector (args);
4271 return result;
4274 release_tree_vector (args);
4276 error ("unable to find numeric literal operator %qD", name);
4277 if (!cpp_get_options (parse_in)->ext_numeric_literals)
4278 inform (token->location, "use -std=gnu++11 or -fext-numeric-literals "
4279 "to enable more built-in suffixes");
4280 return error_mark_node;
4283 /* Parse a user-defined string constant. Returns a call to a user-defined
4284 literal operator taking a character pointer and the length of the string
4285 as arguments. */
4287 static tree
4288 cp_parser_userdef_string_literal (tree literal)
4290 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
4291 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
4292 tree value = USERDEF_LITERAL_VALUE (literal);
4293 int len = TREE_STRING_LENGTH (value)
4294 / TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (value)))) - 1;
4295 tree decl, result;
4296 vec<tree, va_gc> *args;
4298 /* Build up a call to the user-defined operator. */
4299 /* Lookup the name we got back from the id-expression. */
4300 args = make_tree_vector ();
4301 vec_safe_push (args, value);
4302 vec_safe_push (args, build_int_cst (size_type_node, len));
4303 decl = lookup_literal_operator (name, args);
4305 if (decl && decl != error_mark_node)
4307 result = finish_call_expr (decl, &args, false, true,
4308 tf_warning_or_error);
4309 release_tree_vector (args);
4310 return result;
4312 release_tree_vector (args);
4314 /* Look for a template function with typename parameter CharT
4315 and parameter pack CharT... Call the function with
4316 template parameter characters representing the string. */
4317 args = make_tree_vector ();
4318 decl = lookup_literal_operator (name, args);
4319 if (decl && decl != error_mark_node)
4321 tree tmpl_args = make_string_pack (value);
4322 decl = lookup_template_function (decl, tmpl_args);
4323 result = finish_call_expr (decl, &args, false, true,
4324 tf_warning_or_error);
4325 release_tree_vector (args);
4326 return result;
4328 release_tree_vector (args);
4330 error ("unable to find string literal operator %qD with %qT, %qT arguments",
4331 name, TREE_TYPE (value), size_type_node);
4332 return error_mark_node;
4336 /* Basic concepts [gram.basic] */
4338 /* Parse a translation-unit.
4340 translation-unit:
4341 declaration-seq [opt]
4343 Returns TRUE if all went well. */
4345 static bool
4346 cp_parser_translation_unit (cp_parser* parser)
4348 /* The address of the first non-permanent object on the declarator
4349 obstack. */
4350 static void *declarator_obstack_base;
4352 bool success;
4354 /* Create the declarator obstack, if necessary. */
4355 if (!cp_error_declarator)
4357 gcc_obstack_init (&declarator_obstack);
4358 /* Create the error declarator. */
4359 cp_error_declarator = make_declarator (cdk_error);
4360 /* Create the empty parameter list. */
4361 no_parameters = make_parameter_declarator (NULL, NULL, NULL_TREE);
4362 /* Remember where the base of the declarator obstack lies. */
4363 declarator_obstack_base = obstack_next_free (&declarator_obstack);
4366 cp_parser_declaration_seq_opt (parser);
4368 /* If there are no tokens left then all went well. */
4369 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
4371 /* Get rid of the token array; we don't need it any more. */
4372 cp_lexer_destroy (parser->lexer);
4373 parser->lexer = NULL;
4375 /* This file might have been a context that's implicitly extern
4376 "C". If so, pop the lang context. (Only relevant for PCH.) */
4377 if (parser->implicit_extern_c)
4379 pop_lang_context ();
4380 parser->implicit_extern_c = false;
4383 /* Finish up. */
4384 finish_translation_unit ();
4386 success = true;
4388 else
4390 cp_parser_error (parser, "expected declaration");
4391 success = false;
4394 /* Make sure the declarator obstack was fully cleaned up. */
4395 gcc_assert (obstack_next_free (&declarator_obstack)
4396 == declarator_obstack_base);
4398 /* All went well. */
4399 return success;
4402 /* Return the appropriate tsubst flags for parsing, possibly in N3276
4403 decltype context. */
4405 static inline tsubst_flags_t
4406 complain_flags (bool decltype_p)
4408 tsubst_flags_t complain = tf_warning_or_error;
4409 if (decltype_p)
4410 complain |= tf_decltype;
4411 return complain;
4414 /* We're about to parse a collection of statements. If we're currently
4415 parsing tentatively, set up a firewall so that any nested
4416 cp_parser_commit_to_tentative_parse won't affect the current context. */
4418 static cp_token_position
4419 cp_parser_start_tentative_firewall (cp_parser *parser)
4421 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
4422 return 0;
4424 cp_parser_parse_tentatively (parser);
4425 cp_parser_commit_to_topmost_tentative_parse (parser);
4426 return cp_lexer_token_position (parser->lexer, false);
4429 /* We've finished parsing the collection of statements. Wrap up the
4430 firewall and replace the relevant tokens with the parsed form. */
4432 static void
4433 cp_parser_end_tentative_firewall (cp_parser *parser, cp_token_position start,
4434 tree expr)
4436 if (!start)
4437 return;
4439 /* Finish the firewall level. */
4440 cp_parser_parse_definitely (parser);
4441 /* And remember the result of the parse for when we try again. */
4442 cp_token *token = cp_lexer_token_at (parser->lexer, start);
4443 token->type = CPP_PREPARSED_EXPR;
4444 token->u.value = expr;
4445 token->keyword = RID_MAX;
4446 cp_lexer_purge_tokens_after (parser->lexer, start);
4449 /* Like the above functions, but let the user modify the tokens. Used by
4450 CPP_DECLTYPE and CPP_TEMPLATE_ID, where we are saving the side-effects for
4451 later parses, so it makes sense to localize the effects of
4452 cp_parser_commit_to_tentative_parse. */
4454 struct tentative_firewall
4456 cp_parser *parser;
4457 bool set;
4459 tentative_firewall (cp_parser *p): parser(p)
4461 /* If we're currently parsing tentatively, start a committed level as a
4462 firewall and then an inner tentative parse. */
4463 if ((set = cp_parser_uncommitted_to_tentative_parse_p (parser)))
4465 cp_parser_parse_tentatively (parser);
4466 cp_parser_commit_to_topmost_tentative_parse (parser);
4467 cp_parser_parse_tentatively (parser);
4471 ~tentative_firewall()
4473 if (set)
4475 /* Finish the inner tentative parse and the firewall, propagating any
4476 uncommitted error state to the outer tentative parse. */
4477 bool err = cp_parser_error_occurred (parser);
4478 cp_parser_parse_definitely (parser);
4479 cp_parser_parse_definitely (parser);
4480 if (err)
4481 cp_parser_simulate_error (parser);
4486 /* Parse a GNU statement-expression, i.e. ({ stmts }), except for the
4487 enclosing parentheses. */
4489 static cp_expr
4490 cp_parser_statement_expr (cp_parser *parser)
4492 cp_token_position start = cp_parser_start_tentative_firewall (parser);
4494 /* Consume the '('. */
4495 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
4496 cp_lexer_consume_token (parser->lexer);
4497 /* Start the statement-expression. */
4498 tree expr = begin_stmt_expr ();
4499 /* Parse the compound-statement. */
4500 cp_parser_compound_statement (parser, expr, BCS_NORMAL, false);
4501 /* Finish up. */
4502 expr = finish_stmt_expr (expr, false);
4503 /* Consume the ')'. */
4504 location_t finish_loc = cp_lexer_peek_token (parser->lexer)->location;
4505 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
4506 cp_parser_skip_to_end_of_statement (parser);
4508 cp_parser_end_tentative_firewall (parser, start, expr);
4509 location_t combined_loc = make_location (start_loc, start_loc, finish_loc);
4510 return cp_expr (expr, combined_loc);
4513 /* Expressions [gram.expr] */
4515 /* Parse a fold-operator.
4517 fold-operator:
4518 - * / % ^ & | = < > << >>
4519 = -= *= /= %= ^= &= |= <<= >>=
4520 == != <= >= && || , .* ->*
4522 This returns the tree code corresponding to the matched operator
4523 as an int. When the current token matches a compound assignment
4524 opertor, the resulting tree code is the negative value of the
4525 non-assignment operator. */
4527 static int
4528 cp_parser_fold_operator (cp_token *token)
4530 switch (token->type)
4532 case CPP_PLUS: return PLUS_EXPR;
4533 case CPP_MINUS: return MINUS_EXPR;
4534 case CPP_MULT: return MULT_EXPR;
4535 case CPP_DIV: return TRUNC_DIV_EXPR;
4536 case CPP_MOD: return TRUNC_MOD_EXPR;
4537 case CPP_XOR: return BIT_XOR_EXPR;
4538 case CPP_AND: return BIT_AND_EXPR;
4539 case CPP_OR: return BIT_IOR_EXPR;
4540 case CPP_LSHIFT: return LSHIFT_EXPR;
4541 case CPP_RSHIFT: return RSHIFT_EXPR;
4543 case CPP_EQ: return -NOP_EXPR;
4544 case CPP_PLUS_EQ: return -PLUS_EXPR;
4545 case CPP_MINUS_EQ: return -MINUS_EXPR;
4546 case CPP_MULT_EQ: return -MULT_EXPR;
4547 case CPP_DIV_EQ: return -TRUNC_DIV_EXPR;
4548 case CPP_MOD_EQ: return -TRUNC_MOD_EXPR;
4549 case CPP_XOR_EQ: return -BIT_XOR_EXPR;
4550 case CPP_AND_EQ: return -BIT_AND_EXPR;
4551 case CPP_OR_EQ: return -BIT_IOR_EXPR;
4552 case CPP_LSHIFT_EQ: return -LSHIFT_EXPR;
4553 case CPP_RSHIFT_EQ: return -RSHIFT_EXPR;
4555 case CPP_EQ_EQ: return EQ_EXPR;
4556 case CPP_NOT_EQ: return NE_EXPR;
4557 case CPP_LESS: return LT_EXPR;
4558 case CPP_GREATER: return GT_EXPR;
4559 case CPP_LESS_EQ: return LE_EXPR;
4560 case CPP_GREATER_EQ: return GE_EXPR;
4562 case CPP_AND_AND: return TRUTH_ANDIF_EXPR;
4563 case CPP_OR_OR: return TRUTH_ORIF_EXPR;
4565 case CPP_COMMA: return COMPOUND_EXPR;
4567 case CPP_DOT_STAR: return DOTSTAR_EXPR;
4568 case CPP_DEREF_STAR: return MEMBER_REF;
4570 default: return ERROR_MARK;
4574 /* Returns true if CODE indicates a binary expression, which is not allowed in
4575 the LHS of a fold-expression. More codes will need to be added to use this
4576 function in other contexts. */
4578 static bool
4579 is_binary_op (tree_code code)
4581 switch (code)
4583 case PLUS_EXPR:
4584 case POINTER_PLUS_EXPR:
4585 case MINUS_EXPR:
4586 case MULT_EXPR:
4587 case TRUNC_DIV_EXPR:
4588 case TRUNC_MOD_EXPR:
4589 case BIT_XOR_EXPR:
4590 case BIT_AND_EXPR:
4591 case BIT_IOR_EXPR:
4592 case LSHIFT_EXPR:
4593 case RSHIFT_EXPR:
4595 case MODOP_EXPR:
4597 case EQ_EXPR:
4598 case NE_EXPR:
4599 case LE_EXPR:
4600 case GE_EXPR:
4601 case LT_EXPR:
4602 case GT_EXPR:
4604 case TRUTH_ANDIF_EXPR:
4605 case TRUTH_ORIF_EXPR:
4607 case COMPOUND_EXPR:
4609 case DOTSTAR_EXPR:
4610 case MEMBER_REF:
4611 return true;
4613 default:
4614 return false;
4618 /* If the next token is a suitable fold operator, consume it and return as
4619 the function above. */
4621 static int
4622 cp_parser_fold_operator (cp_parser *parser)
4624 cp_token* token = cp_lexer_peek_token (parser->lexer);
4625 int code = cp_parser_fold_operator (token);
4626 if (code != ERROR_MARK)
4627 cp_lexer_consume_token (parser->lexer);
4628 return code;
4631 /* Parse a fold-expression.
4633 fold-expression:
4634 ( ... folding-operator cast-expression)
4635 ( cast-expression folding-operator ... )
4636 ( cast-expression folding operator ... folding-operator cast-expression)
4638 Note that the '(' and ')' are matched in primary expression. */
4640 static cp_expr
4641 cp_parser_fold_expression (cp_parser *parser, tree expr1)
4643 cp_id_kind pidk;
4645 // Left fold.
4646 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
4648 cp_lexer_consume_token (parser->lexer);
4649 int op = cp_parser_fold_operator (parser);
4650 if (op == ERROR_MARK)
4652 cp_parser_error (parser, "expected binary operator");
4653 return error_mark_node;
4656 tree expr = cp_parser_cast_expression (parser, false, false,
4657 false, &pidk);
4658 if (expr == error_mark_node)
4659 return error_mark_node;
4660 return finish_left_unary_fold_expr (expr, op);
4663 const cp_token* token = cp_lexer_peek_token (parser->lexer);
4664 int op = cp_parser_fold_operator (parser);
4665 if (op == ERROR_MARK)
4667 cp_parser_error (parser, "expected binary operator");
4668 return error_mark_node;
4671 if (cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS))
4673 cp_parser_error (parser, "expected ...");
4674 return error_mark_node;
4676 cp_lexer_consume_token (parser->lexer);
4678 /* The operands of a fold-expression are cast-expressions, so binary or
4679 conditional expressions are not allowed. We check this here to avoid
4680 tentative parsing. */
4681 if (EXPR_P (expr1) && TREE_NO_WARNING (expr1))
4682 /* OK, the expression was parenthesized. */;
4683 else if (is_binary_op (TREE_CODE (expr1)))
4684 error_at (location_of (expr1),
4685 "binary expression in operand of fold-expression");
4686 else if (TREE_CODE (expr1) == COND_EXPR)
4687 error_at (location_of (expr1),
4688 "conditional expression in operand of fold-expression");
4690 // Right fold.
4691 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
4692 return finish_right_unary_fold_expr (expr1, op);
4694 if (cp_lexer_next_token_is_not (parser->lexer, token->type))
4696 cp_parser_error (parser, "mismatched operator in fold-expression");
4697 return error_mark_node;
4699 cp_lexer_consume_token (parser->lexer);
4701 // Binary left or right fold.
4702 tree expr2 = cp_parser_cast_expression (parser, false, false, false, &pidk);
4703 if (expr2 == error_mark_node)
4704 return error_mark_node;
4705 return finish_binary_fold_expr (expr1, expr2, op);
4708 /* Parse a primary-expression.
4710 primary-expression:
4711 literal
4712 this
4713 ( expression )
4714 id-expression
4715 lambda-expression (C++11)
4717 GNU Extensions:
4719 primary-expression:
4720 ( compound-statement )
4721 __builtin_va_arg ( assignment-expression , type-id )
4722 __builtin_offsetof ( type-id , offsetof-expression )
4724 C++ Extensions:
4725 __has_nothrow_assign ( type-id )
4726 __has_nothrow_constructor ( type-id )
4727 __has_nothrow_copy ( type-id )
4728 __has_trivial_assign ( type-id )
4729 __has_trivial_constructor ( type-id )
4730 __has_trivial_copy ( type-id )
4731 __has_trivial_destructor ( type-id )
4732 __has_virtual_destructor ( type-id )
4733 __is_abstract ( type-id )
4734 __is_base_of ( type-id , type-id )
4735 __is_class ( type-id )
4736 __is_empty ( type-id )
4737 __is_enum ( type-id )
4738 __is_final ( type-id )
4739 __is_literal_type ( type-id )
4740 __is_pod ( type-id )
4741 __is_polymorphic ( type-id )
4742 __is_std_layout ( type-id )
4743 __is_trivial ( type-id )
4744 __is_union ( type-id )
4746 Objective-C++ Extension:
4748 primary-expression:
4749 objc-expression
4751 literal:
4752 __null
4754 ADDRESS_P is true iff this expression was immediately preceded by
4755 "&" and therefore might denote a pointer-to-member. CAST_P is true
4756 iff this expression is the target of a cast. TEMPLATE_ARG_P is
4757 true iff this expression is a template argument.
4759 Returns a representation of the expression. Upon return, *IDK
4760 indicates what kind of id-expression (if any) was present. */
4762 static cp_expr
4763 cp_parser_primary_expression (cp_parser *parser,
4764 bool address_p,
4765 bool cast_p,
4766 bool template_arg_p,
4767 bool decltype_p,
4768 cp_id_kind *idk)
4770 cp_token *token = NULL;
4772 /* Assume the primary expression is not an id-expression. */
4773 *idk = CP_ID_KIND_NONE;
4775 /* Peek at the next token. */
4776 token = cp_lexer_peek_token (parser->lexer);
4777 switch ((int) token->type)
4779 /* literal:
4780 integer-literal
4781 character-literal
4782 floating-literal
4783 string-literal
4784 boolean-literal
4785 pointer-literal
4786 user-defined-literal */
4787 case CPP_CHAR:
4788 case CPP_CHAR16:
4789 case CPP_CHAR32:
4790 case CPP_WCHAR:
4791 case CPP_UTF8CHAR:
4792 case CPP_NUMBER:
4793 case CPP_PREPARSED_EXPR:
4794 if (TREE_CODE (token->u.value) == USERDEF_LITERAL)
4795 return cp_parser_userdef_numeric_literal (parser);
4796 token = cp_lexer_consume_token (parser->lexer);
4797 if (TREE_CODE (token->u.value) == FIXED_CST)
4799 error_at (token->location,
4800 "fixed-point types not supported in C++");
4801 return error_mark_node;
4803 /* Floating-point literals are only allowed in an integral
4804 constant expression if they are cast to an integral or
4805 enumeration type. */
4806 if (TREE_CODE (token->u.value) == REAL_CST
4807 && parser->integral_constant_expression_p
4808 && pedantic)
4810 /* CAST_P will be set even in invalid code like "int(2.7 +
4811 ...)". Therefore, we have to check that the next token
4812 is sure to end the cast. */
4813 if (cast_p)
4815 cp_token *next_token;
4817 next_token = cp_lexer_peek_token (parser->lexer);
4818 if (/* The comma at the end of an
4819 enumerator-definition. */
4820 next_token->type != CPP_COMMA
4821 /* The curly brace at the end of an enum-specifier. */
4822 && next_token->type != CPP_CLOSE_BRACE
4823 /* The end of a statement. */
4824 && next_token->type != CPP_SEMICOLON
4825 /* The end of the cast-expression. */
4826 && next_token->type != CPP_CLOSE_PAREN
4827 /* The end of an array bound. */
4828 && next_token->type != CPP_CLOSE_SQUARE
4829 /* The closing ">" in a template-argument-list. */
4830 && (next_token->type != CPP_GREATER
4831 || parser->greater_than_is_operator_p)
4832 /* C++0x only: A ">>" treated like two ">" tokens,
4833 in a template-argument-list. */
4834 && (next_token->type != CPP_RSHIFT
4835 || (cxx_dialect == cxx98)
4836 || parser->greater_than_is_operator_p))
4837 cast_p = false;
4840 /* If we are within a cast, then the constraint that the
4841 cast is to an integral or enumeration type will be
4842 checked at that point. If we are not within a cast, then
4843 this code is invalid. */
4844 if (!cast_p)
4845 cp_parser_non_integral_constant_expression (parser, NIC_FLOAT);
4847 return cp_expr (token->u.value, token->location);
4849 case CPP_CHAR_USERDEF:
4850 case CPP_CHAR16_USERDEF:
4851 case CPP_CHAR32_USERDEF:
4852 case CPP_WCHAR_USERDEF:
4853 case CPP_UTF8CHAR_USERDEF:
4854 return cp_parser_userdef_char_literal (parser);
4856 case CPP_STRING:
4857 case CPP_STRING16:
4858 case CPP_STRING32:
4859 case CPP_WSTRING:
4860 case CPP_UTF8STRING:
4861 case CPP_STRING_USERDEF:
4862 case CPP_STRING16_USERDEF:
4863 case CPP_STRING32_USERDEF:
4864 case CPP_WSTRING_USERDEF:
4865 case CPP_UTF8STRING_USERDEF:
4866 /* ??? Should wide strings be allowed when parser->translate_strings_p
4867 is false (i.e. in attributes)? If not, we can kill the third
4868 argument to cp_parser_string_literal. */
4869 return cp_parser_string_literal (parser,
4870 parser->translate_strings_p,
4871 true);
4873 case CPP_OPEN_PAREN:
4874 /* If we see `( { ' then we are looking at the beginning of
4875 a GNU statement-expression. */
4876 if (cp_parser_allow_gnu_extensions_p (parser)
4877 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_BRACE))
4879 /* Statement-expressions are not allowed by the standard. */
4880 pedwarn (token->location, OPT_Wpedantic,
4881 "ISO C++ forbids braced-groups within expressions");
4883 /* And they're not allowed outside of a function-body; you
4884 cannot, for example, write:
4886 int i = ({ int j = 3; j + 1; });
4888 at class or namespace scope. */
4889 if (!parser->in_function_body
4890 || parser->in_template_argument_list_p)
4892 error_at (token->location,
4893 "statement-expressions are not allowed outside "
4894 "functions nor in template-argument lists");
4895 cp_parser_skip_to_end_of_block_or_statement (parser);
4896 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
4897 cp_lexer_consume_token (parser->lexer);
4898 return error_mark_node;
4900 else
4901 return cp_parser_statement_expr (parser);
4903 /* Otherwise it's a normal parenthesized expression. */
4905 cp_expr expr;
4906 bool saved_greater_than_is_operator_p;
4908 location_t open_paren_loc = token->location;
4910 /* Consume the `('. */
4911 cp_lexer_consume_token (parser->lexer);
4912 /* Within a parenthesized expression, a `>' token is always
4913 the greater-than operator. */
4914 saved_greater_than_is_operator_p
4915 = parser->greater_than_is_operator_p;
4916 parser->greater_than_is_operator_p = true;
4918 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
4919 /* Left fold expression. */
4920 expr = NULL_TREE;
4921 else
4922 /* Parse the parenthesized expression. */
4923 expr = cp_parser_expression (parser, idk, cast_p, decltype_p);
4925 token = cp_lexer_peek_token (parser->lexer);
4926 if (token->type == CPP_ELLIPSIS || cp_parser_fold_operator (token))
4928 expr = cp_parser_fold_expression (parser, expr);
4929 if (expr != error_mark_node
4930 && cxx_dialect < cxx1z
4931 && !in_system_header_at (input_location))
4932 pedwarn (input_location, 0, "fold-expressions only available "
4933 "with -std=c++1z or -std=gnu++1z");
4935 else
4936 /* Let the front end know that this expression was
4937 enclosed in parentheses. This matters in case, for
4938 example, the expression is of the form `A::B', since
4939 `&A::B' might be a pointer-to-member, but `&(A::B)' is
4940 not. */
4941 expr = finish_parenthesized_expr (expr);
4943 /* DR 705: Wrapping an unqualified name in parentheses
4944 suppresses arg-dependent lookup. We want to pass back
4945 CP_ID_KIND_QUALIFIED for suppressing vtable lookup
4946 (c++/37862), but none of the others. */
4947 if (*idk != CP_ID_KIND_QUALIFIED)
4948 *idk = CP_ID_KIND_NONE;
4950 /* The `>' token might be the end of a template-id or
4951 template-parameter-list now. */
4952 parser->greater_than_is_operator_p
4953 = saved_greater_than_is_operator_p;
4955 /* Consume the `)'. */
4956 token = cp_lexer_peek_token (parser->lexer);
4957 location_t close_paren_loc = token->location;
4958 expr.set_range (open_paren_loc, close_paren_loc);
4959 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN)
4960 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
4961 cp_parser_skip_to_end_of_statement (parser);
4963 return expr;
4966 case CPP_OPEN_SQUARE:
4968 if (c_dialect_objc ())
4970 /* We might have an Objective-C++ message. */
4971 cp_parser_parse_tentatively (parser);
4972 tree msg = cp_parser_objc_message_expression (parser);
4973 /* If that works out, we're done ... */
4974 if (cp_parser_parse_definitely (parser))
4975 return msg;
4976 /* ... else, fall though to see if it's a lambda. */
4978 cp_expr lam = cp_parser_lambda_expression (parser);
4979 /* Don't warn about a failed tentative parse. */
4980 if (cp_parser_error_occurred (parser))
4981 return error_mark_node;
4982 maybe_warn_cpp0x (CPP0X_LAMBDA_EXPR);
4983 return lam;
4986 case CPP_OBJC_STRING:
4987 if (c_dialect_objc ())
4988 /* We have an Objective-C++ string literal. */
4989 return cp_parser_objc_expression (parser);
4990 cp_parser_error (parser, "expected primary-expression");
4991 return error_mark_node;
4993 case CPP_KEYWORD:
4994 switch (token->keyword)
4996 /* These two are the boolean literals. */
4997 case RID_TRUE:
4998 cp_lexer_consume_token (parser->lexer);
4999 return cp_expr (boolean_true_node, token->location);
5000 case RID_FALSE:
5001 cp_lexer_consume_token (parser->lexer);
5002 return cp_expr (boolean_false_node, token->location);
5004 /* The `__null' literal. */
5005 case RID_NULL:
5006 cp_lexer_consume_token (parser->lexer);
5007 return cp_expr (null_node, token->location);
5009 /* The `nullptr' literal. */
5010 case RID_NULLPTR:
5011 cp_lexer_consume_token (parser->lexer);
5012 return cp_expr (nullptr_node, token->location);
5014 /* Recognize the `this' keyword. */
5015 case RID_THIS:
5016 cp_lexer_consume_token (parser->lexer);
5017 if (parser->local_variables_forbidden_p)
5019 error_at (token->location,
5020 "%<this%> may not be used in this context");
5021 return error_mark_node;
5023 /* Pointers cannot appear in constant-expressions. */
5024 if (cp_parser_non_integral_constant_expression (parser, NIC_THIS))
5025 return error_mark_node;
5026 return cp_expr (finish_this_expr (), token->location);
5028 /* The `operator' keyword can be the beginning of an
5029 id-expression. */
5030 case RID_OPERATOR:
5031 goto id_expression;
5033 case RID_FUNCTION_NAME:
5034 case RID_PRETTY_FUNCTION_NAME:
5035 case RID_C99_FUNCTION_NAME:
5037 non_integral_constant name;
5039 /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
5040 __func__ are the names of variables -- but they are
5041 treated specially. Therefore, they are handled here,
5042 rather than relying on the generic id-expression logic
5043 below. Grammatically, these names are id-expressions.
5045 Consume the token. */
5046 token = cp_lexer_consume_token (parser->lexer);
5048 switch (token->keyword)
5050 case RID_FUNCTION_NAME:
5051 name = NIC_FUNC_NAME;
5052 break;
5053 case RID_PRETTY_FUNCTION_NAME:
5054 name = NIC_PRETTY_FUNC;
5055 break;
5056 case RID_C99_FUNCTION_NAME:
5057 name = NIC_C99_FUNC;
5058 break;
5059 default:
5060 gcc_unreachable ();
5063 if (cp_parser_non_integral_constant_expression (parser, name))
5064 return error_mark_node;
5066 /* Look up the name. */
5067 return finish_fname (token->u.value);
5070 case RID_VA_ARG:
5072 tree expression;
5073 tree type;
5074 source_location type_location;
5075 location_t start_loc
5076 = cp_lexer_peek_token (parser->lexer)->location;
5077 /* The `__builtin_va_arg' construct is used to handle
5078 `va_arg'. Consume the `__builtin_va_arg' token. */
5079 cp_lexer_consume_token (parser->lexer);
5080 /* Look for the opening `('. */
5081 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
5082 /* Now, parse the assignment-expression. */
5083 expression = cp_parser_assignment_expression (parser);
5084 /* Look for the `,'. */
5085 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
5086 type_location = cp_lexer_peek_token (parser->lexer)->location;
5087 /* Parse the type-id. */
5089 type_id_in_expr_sentinel s (parser);
5090 type = cp_parser_type_id (parser);
5092 /* Look for the closing `)'. */
5093 location_t finish_loc
5094 = cp_lexer_peek_token (parser->lexer)->location;
5095 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
5096 /* Using `va_arg' in a constant-expression is not
5097 allowed. */
5098 if (cp_parser_non_integral_constant_expression (parser,
5099 NIC_VA_ARG))
5100 return error_mark_node;
5101 /* Construct a location of the form:
5102 __builtin_va_arg (v, int)
5103 ~~~~~~~~~~~~~~~~~~~~~^~~~
5104 with the caret at the type, ranging from the start of the
5105 "__builtin_va_arg" token to the close paren. */
5106 location_t combined_loc
5107 = make_location (type_location, start_loc, finish_loc);
5108 return build_x_va_arg (combined_loc, expression, type);
5111 case RID_OFFSETOF:
5112 return cp_parser_builtin_offsetof (parser);
5114 case RID_HAS_NOTHROW_ASSIGN:
5115 case RID_HAS_NOTHROW_CONSTRUCTOR:
5116 case RID_HAS_NOTHROW_COPY:
5117 case RID_HAS_TRIVIAL_ASSIGN:
5118 case RID_HAS_TRIVIAL_CONSTRUCTOR:
5119 case RID_HAS_TRIVIAL_COPY:
5120 case RID_HAS_TRIVIAL_DESTRUCTOR:
5121 case RID_HAS_UNIQUE_OBJ_REPRESENTATIONS:
5122 case RID_HAS_VIRTUAL_DESTRUCTOR:
5123 case RID_IS_ABSTRACT:
5124 case RID_IS_BASE_OF:
5125 case RID_IS_CLASS:
5126 case RID_IS_EMPTY:
5127 case RID_IS_ENUM:
5128 case RID_IS_FINAL:
5129 case RID_IS_LITERAL_TYPE:
5130 case RID_IS_POD:
5131 case RID_IS_POLYMORPHIC:
5132 case RID_IS_SAME_AS:
5133 case RID_IS_STD_LAYOUT:
5134 case RID_IS_TRIVIAL:
5135 case RID_IS_TRIVIALLY_ASSIGNABLE:
5136 case RID_IS_TRIVIALLY_CONSTRUCTIBLE:
5137 case RID_IS_TRIVIALLY_COPYABLE:
5138 case RID_IS_UNION:
5139 return cp_parser_trait_expr (parser, token->keyword);
5141 // C++ concepts
5142 case RID_REQUIRES:
5143 return cp_parser_requires_expression (parser);
5145 /* Objective-C++ expressions. */
5146 case RID_AT_ENCODE:
5147 case RID_AT_PROTOCOL:
5148 case RID_AT_SELECTOR:
5149 return cp_parser_objc_expression (parser);
5151 case RID_TEMPLATE:
5152 if (parser->in_function_body
5153 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
5154 == CPP_LESS))
5156 error_at (token->location,
5157 "a template declaration cannot appear at block scope");
5158 cp_parser_skip_to_end_of_block_or_statement (parser);
5159 return error_mark_node;
5161 /* FALLTHRU */
5162 default:
5163 cp_parser_error (parser, "expected primary-expression");
5164 return error_mark_node;
5167 /* An id-expression can start with either an identifier, a
5168 `::' as the beginning of a qualified-id, or the "operator"
5169 keyword. */
5170 case CPP_NAME:
5171 case CPP_SCOPE:
5172 case CPP_TEMPLATE_ID:
5173 case CPP_NESTED_NAME_SPECIFIER:
5175 id_expression:
5176 cp_expr id_expression;
5177 cp_expr decl;
5178 const char *error_msg;
5179 bool template_p;
5180 bool done;
5181 cp_token *id_expr_token;
5183 /* Parse the id-expression. */
5184 id_expression
5185 = cp_parser_id_expression (parser,
5186 /*template_keyword_p=*/false,
5187 /*check_dependency_p=*/true,
5188 &template_p,
5189 /*declarator_p=*/false,
5190 /*optional_p=*/false);
5191 if (id_expression == error_mark_node)
5192 return error_mark_node;
5193 id_expr_token = token;
5194 token = cp_lexer_peek_token (parser->lexer);
5195 done = (token->type != CPP_OPEN_SQUARE
5196 && token->type != CPP_OPEN_PAREN
5197 && token->type != CPP_DOT
5198 && token->type != CPP_DEREF
5199 && token->type != CPP_PLUS_PLUS
5200 && token->type != CPP_MINUS_MINUS);
5201 /* If we have a template-id, then no further lookup is
5202 required. If the template-id was for a template-class, we
5203 will sometimes have a TYPE_DECL at this point. */
5204 if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
5205 || TREE_CODE (id_expression) == TYPE_DECL)
5206 decl = id_expression;
5207 /* Look up the name. */
5208 else
5210 tree ambiguous_decls;
5212 /* If we already know that this lookup is ambiguous, then
5213 we've already issued an error message; there's no reason
5214 to check again. */
5215 if (id_expr_token->type == CPP_NAME
5216 && id_expr_token->error_reported)
5218 cp_parser_simulate_error (parser);
5219 return error_mark_node;
5222 decl = cp_parser_lookup_name (parser, id_expression,
5223 none_type,
5224 template_p,
5225 /*is_namespace=*/false,
5226 /*check_dependency=*/true,
5227 &ambiguous_decls,
5228 id_expr_token->location);
5229 /* If the lookup was ambiguous, an error will already have
5230 been issued. */
5231 if (ambiguous_decls)
5232 return error_mark_node;
5234 /* In Objective-C++, we may have an Objective-C 2.0
5235 dot-syntax for classes here. */
5236 if (c_dialect_objc ()
5237 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT
5238 && TREE_CODE (decl) == TYPE_DECL
5239 && objc_is_class_name (decl))
5241 tree component;
5242 cp_lexer_consume_token (parser->lexer);
5243 component = cp_parser_identifier (parser);
5244 if (component == error_mark_node)
5245 return error_mark_node;
5247 tree result = objc_build_class_component_ref (id_expression,
5248 component);
5249 /* Build a location of the form:
5250 expr.component
5251 ~~~~~^~~~~~~~~
5252 with caret at the start of the component name (at
5253 input_location), ranging from the start of the id_expression
5254 to the end of the component name. */
5255 location_t combined_loc
5256 = make_location (input_location, id_expression.get_start (),
5257 get_finish (input_location));
5258 protected_set_expr_location (result, combined_loc);
5259 return result;
5262 /* In Objective-C++, an instance variable (ivar) may be preferred
5263 to whatever cp_parser_lookup_name() found.
5264 Call objc_lookup_ivar. To avoid exposing cp_expr to the
5265 rest of c-family, we have to do a little extra work to preserve
5266 any location information in cp_expr "decl". Given that
5267 objc_lookup_ivar is implemented in "c-family" and "objc", we
5268 have a trip through the pure "tree" type, rather than cp_expr.
5269 Naively copying it back to "decl" would implicitly give the
5270 new cp_expr value an UNKNOWN_LOCATION for nodes that don't
5271 store an EXPR_LOCATION. Hence we only update "decl" (and
5272 hence its location_t) if we get back a different tree node. */
5273 tree decl_tree = objc_lookup_ivar (decl.get_value (),
5274 id_expression);
5275 if (decl_tree != decl.get_value ())
5276 decl = cp_expr (decl_tree);
5278 /* If name lookup gives us a SCOPE_REF, then the
5279 qualifying scope was dependent. */
5280 if (TREE_CODE (decl) == SCOPE_REF)
5282 /* At this point, we do not know if DECL is a valid
5283 integral constant expression. We assume that it is
5284 in fact such an expression, so that code like:
5286 template <int N> struct A {
5287 int a[B<N>::i];
5290 is accepted. At template-instantiation time, we
5291 will check that B<N>::i is actually a constant. */
5292 return decl;
5294 /* Check to see if DECL is a local variable in a context
5295 where that is forbidden. */
5296 if (parser->local_variables_forbidden_p
5297 && local_variable_p (decl))
5299 /* It might be that we only found DECL because we are
5300 trying to be generous with pre-ISO scoping rules.
5301 For example, consider:
5303 int i;
5304 void g() {
5305 for (int i = 0; i < 10; ++i) {}
5306 extern void f(int j = i);
5309 Here, name look up will originally find the out
5310 of scope `i'. We need to issue a warning message,
5311 but then use the global `i'. */
5312 decl = check_for_out_of_scope_variable (decl);
5313 if (local_variable_p (decl))
5315 error_at (id_expr_token->location,
5316 "local variable %qD may not appear in this context",
5317 decl.get_value ());
5318 return error_mark_node;
5323 decl = (finish_id_expression
5324 (id_expression, decl, parser->scope,
5325 idk,
5326 parser->integral_constant_expression_p,
5327 parser->allow_non_integral_constant_expression_p,
5328 &parser->non_integral_constant_expression_p,
5329 template_p, done, address_p,
5330 template_arg_p,
5331 &error_msg,
5332 id_expression.get_location ()));
5333 if (error_msg)
5334 cp_parser_error (parser, error_msg);
5335 decl.set_location (id_expr_token->location);
5336 return decl;
5339 /* Anything else is an error. */
5340 default:
5341 cp_parser_error (parser, "expected primary-expression");
5342 return error_mark_node;
5346 static inline cp_expr
5347 cp_parser_primary_expression (cp_parser *parser,
5348 bool address_p,
5349 bool cast_p,
5350 bool template_arg_p,
5351 cp_id_kind *idk)
5353 return cp_parser_primary_expression (parser, address_p, cast_p, template_arg_p,
5354 /*decltype*/false, idk);
5357 /* Parse an id-expression.
5359 id-expression:
5360 unqualified-id
5361 qualified-id
5363 qualified-id:
5364 :: [opt] nested-name-specifier template [opt] unqualified-id
5365 :: identifier
5366 :: operator-function-id
5367 :: template-id
5369 Return a representation of the unqualified portion of the
5370 identifier. Sets PARSER->SCOPE to the qualifying scope if there is
5371 a `::' or nested-name-specifier.
5373 Often, if the id-expression was a qualified-id, the caller will
5374 want to make a SCOPE_REF to represent the qualified-id. This
5375 function does not do this in order to avoid wastefully creating
5376 SCOPE_REFs when they are not required.
5378 If TEMPLATE_KEYWORD_P is true, then we have just seen the
5379 `template' keyword.
5381 If CHECK_DEPENDENCY_P is false, then names are looked up inside
5382 uninstantiated templates.
5384 If *TEMPLATE_P is non-NULL, it is set to true iff the
5385 `template' keyword is used to explicitly indicate that the entity
5386 named is a template.
5388 If DECLARATOR_P is true, the id-expression is appearing as part of
5389 a declarator, rather than as part of an expression. */
5391 static cp_expr
5392 cp_parser_id_expression (cp_parser *parser,
5393 bool template_keyword_p,
5394 bool check_dependency_p,
5395 bool *template_p,
5396 bool declarator_p,
5397 bool optional_p)
5399 bool global_scope_p;
5400 bool nested_name_specifier_p;
5402 /* Assume the `template' keyword was not used. */
5403 if (template_p)
5404 *template_p = template_keyword_p;
5406 /* Look for the optional `::' operator. */
5407 global_scope_p
5408 = (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false)
5409 != NULL_TREE);
5410 /* Look for the optional nested-name-specifier. */
5411 nested_name_specifier_p
5412 = (cp_parser_nested_name_specifier_opt (parser,
5413 /*typename_keyword_p=*/false,
5414 check_dependency_p,
5415 /*type_p=*/false,
5416 declarator_p)
5417 != NULL_TREE);
5418 /* If there is a nested-name-specifier, then we are looking at
5419 the first qualified-id production. */
5420 if (nested_name_specifier_p)
5422 tree saved_scope;
5423 tree saved_object_scope;
5424 tree saved_qualifying_scope;
5425 cp_expr unqualified_id;
5426 bool is_template;
5428 /* See if the next token is the `template' keyword. */
5429 if (!template_p)
5430 template_p = &is_template;
5431 *template_p = cp_parser_optional_template_keyword (parser);
5432 /* Name lookup we do during the processing of the
5433 unqualified-id might obliterate SCOPE. */
5434 saved_scope = parser->scope;
5435 saved_object_scope = parser->object_scope;
5436 saved_qualifying_scope = parser->qualifying_scope;
5437 /* Process the final unqualified-id. */
5438 unqualified_id = cp_parser_unqualified_id (parser, *template_p,
5439 check_dependency_p,
5440 declarator_p,
5441 /*optional_p=*/false);
5442 /* Restore the SAVED_SCOPE for our caller. */
5443 parser->scope = saved_scope;
5444 parser->object_scope = saved_object_scope;
5445 parser->qualifying_scope = saved_qualifying_scope;
5447 return unqualified_id;
5449 /* Otherwise, if we are in global scope, then we are looking at one
5450 of the other qualified-id productions. */
5451 else if (global_scope_p)
5453 cp_token *token;
5454 tree id;
5456 /* Peek at the next token. */
5457 token = cp_lexer_peek_token (parser->lexer);
5459 /* If it's an identifier, and the next token is not a "<", then
5460 we can avoid the template-id case. This is an optimization
5461 for this common case. */
5462 if (token->type == CPP_NAME
5463 && !cp_parser_nth_token_starts_template_argument_list_p
5464 (parser, 2))
5465 return cp_parser_identifier (parser);
5467 cp_parser_parse_tentatively (parser);
5468 /* Try a template-id. */
5469 id = cp_parser_template_id (parser,
5470 /*template_keyword_p=*/false,
5471 /*check_dependency_p=*/true,
5472 none_type,
5473 declarator_p);
5474 /* If that worked, we're done. */
5475 if (cp_parser_parse_definitely (parser))
5476 return id;
5478 /* Peek at the next token. (Changes in the token buffer may
5479 have invalidated the pointer obtained above.) */
5480 token = cp_lexer_peek_token (parser->lexer);
5482 switch (token->type)
5484 case CPP_NAME:
5485 return cp_parser_identifier (parser);
5487 case CPP_KEYWORD:
5488 if (token->keyword == RID_OPERATOR)
5489 return cp_parser_operator_function_id (parser);
5490 /* Fall through. */
5492 default:
5493 cp_parser_error (parser, "expected id-expression");
5494 return error_mark_node;
5497 else
5498 return cp_parser_unqualified_id (parser, template_keyword_p,
5499 /*check_dependency_p=*/true,
5500 declarator_p,
5501 optional_p);
5504 /* Parse an unqualified-id.
5506 unqualified-id:
5507 identifier
5508 operator-function-id
5509 conversion-function-id
5510 ~ class-name
5511 template-id
5513 If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
5514 keyword, in a construct like `A::template ...'.
5516 Returns a representation of unqualified-id. For the `identifier'
5517 production, an IDENTIFIER_NODE is returned. For the `~ class-name'
5518 production a BIT_NOT_EXPR is returned; the operand of the
5519 BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name. For the
5520 other productions, see the documentation accompanying the
5521 corresponding parsing functions. If CHECK_DEPENDENCY_P is false,
5522 names are looked up in uninstantiated templates. If DECLARATOR_P
5523 is true, the unqualified-id is appearing as part of a declarator,
5524 rather than as part of an expression. */
5526 static cp_expr
5527 cp_parser_unqualified_id (cp_parser* parser,
5528 bool template_keyword_p,
5529 bool check_dependency_p,
5530 bool declarator_p,
5531 bool optional_p)
5533 cp_token *token;
5535 /* Peek at the next token. */
5536 token = cp_lexer_peek_token (parser->lexer);
5538 switch ((int) token->type)
5540 case CPP_NAME:
5542 tree id;
5544 /* We don't know yet whether or not this will be a
5545 template-id. */
5546 cp_parser_parse_tentatively (parser);
5547 /* Try a template-id. */
5548 id = cp_parser_template_id (parser, template_keyword_p,
5549 check_dependency_p,
5550 none_type,
5551 declarator_p);
5552 /* If it worked, we're done. */
5553 if (cp_parser_parse_definitely (parser))
5554 return id;
5555 /* Otherwise, it's an ordinary identifier. */
5556 return cp_parser_identifier (parser);
5559 case CPP_TEMPLATE_ID:
5560 return cp_parser_template_id (parser, template_keyword_p,
5561 check_dependency_p,
5562 none_type,
5563 declarator_p);
5565 case CPP_COMPL:
5567 tree type_decl;
5568 tree qualifying_scope;
5569 tree object_scope;
5570 tree scope;
5571 bool done;
5573 /* Consume the `~' token. */
5574 cp_lexer_consume_token (parser->lexer);
5575 /* Parse the class-name. The standard, as written, seems to
5576 say that:
5578 template <typename T> struct S { ~S (); };
5579 template <typename T> S<T>::~S() {}
5581 is invalid, since `~' must be followed by a class-name, but
5582 `S<T>' is dependent, and so not known to be a class.
5583 That's not right; we need to look in uninstantiated
5584 templates. A further complication arises from:
5586 template <typename T> void f(T t) {
5587 t.T::~T();
5590 Here, it is not possible to look up `T' in the scope of `T'
5591 itself. We must look in both the current scope, and the
5592 scope of the containing complete expression.
5594 Yet another issue is:
5596 struct S {
5597 int S;
5598 ~S();
5601 S::~S() {}
5603 The standard does not seem to say that the `S' in `~S'
5604 should refer to the type `S' and not the data member
5605 `S::S'. */
5607 /* DR 244 says that we look up the name after the "~" in the
5608 same scope as we looked up the qualifying name. That idea
5609 isn't fully worked out; it's more complicated than that. */
5610 scope = parser->scope;
5611 object_scope = parser->object_scope;
5612 qualifying_scope = parser->qualifying_scope;
5614 /* Check for invalid scopes. */
5615 if (scope == error_mark_node)
5617 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
5618 cp_lexer_consume_token (parser->lexer);
5619 return error_mark_node;
5621 if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
5623 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
5624 error_at (token->location,
5625 "scope %qT before %<~%> is not a class-name",
5626 scope);
5627 cp_parser_simulate_error (parser);
5628 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
5629 cp_lexer_consume_token (parser->lexer);
5630 return error_mark_node;
5632 gcc_assert (!scope || TYPE_P (scope));
5634 /* If the name is of the form "X::~X" it's OK even if X is a
5635 typedef. */
5636 token = cp_lexer_peek_token (parser->lexer);
5637 if (scope
5638 && token->type == CPP_NAME
5639 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
5640 != CPP_LESS)
5641 && (token->u.value == TYPE_IDENTIFIER (scope)
5642 || (CLASS_TYPE_P (scope)
5643 && constructor_name_p (token->u.value, scope))))
5645 cp_lexer_consume_token (parser->lexer);
5646 return build_nt (BIT_NOT_EXPR, scope);
5649 /* ~auto means the destructor of whatever the object is. */
5650 if (cp_parser_is_keyword (token, RID_AUTO))
5652 if (cxx_dialect < cxx14)
5653 pedwarn (input_location, 0,
5654 "%<~auto%> only available with "
5655 "-std=c++14 or -std=gnu++14");
5656 cp_lexer_consume_token (parser->lexer);
5657 return build_nt (BIT_NOT_EXPR, make_auto ());
5660 /* If there was an explicit qualification (S::~T), first look
5661 in the scope given by the qualification (i.e., S).
5663 Note: in the calls to cp_parser_class_name below we pass
5664 typename_type so that lookup finds the injected-class-name
5665 rather than the constructor. */
5666 done = false;
5667 type_decl = NULL_TREE;
5668 if (scope)
5670 cp_parser_parse_tentatively (parser);
5671 type_decl = cp_parser_class_name (parser,
5672 /*typename_keyword_p=*/false,
5673 /*template_keyword_p=*/false,
5674 typename_type,
5675 /*check_dependency=*/false,
5676 /*class_head_p=*/false,
5677 declarator_p);
5678 if (cp_parser_parse_definitely (parser))
5679 done = true;
5681 /* In "N::S::~S", look in "N" as well. */
5682 if (!done && scope && qualifying_scope)
5684 cp_parser_parse_tentatively (parser);
5685 parser->scope = qualifying_scope;
5686 parser->object_scope = NULL_TREE;
5687 parser->qualifying_scope = NULL_TREE;
5688 type_decl
5689 = cp_parser_class_name (parser,
5690 /*typename_keyword_p=*/false,
5691 /*template_keyword_p=*/false,
5692 typename_type,
5693 /*check_dependency=*/false,
5694 /*class_head_p=*/false,
5695 declarator_p);
5696 if (cp_parser_parse_definitely (parser))
5697 done = true;
5699 /* In "p->S::~T", look in the scope given by "*p" as well. */
5700 else if (!done && object_scope)
5702 cp_parser_parse_tentatively (parser);
5703 parser->scope = object_scope;
5704 parser->object_scope = NULL_TREE;
5705 parser->qualifying_scope = NULL_TREE;
5706 type_decl
5707 = cp_parser_class_name (parser,
5708 /*typename_keyword_p=*/false,
5709 /*template_keyword_p=*/false,
5710 typename_type,
5711 /*check_dependency=*/false,
5712 /*class_head_p=*/false,
5713 declarator_p);
5714 if (cp_parser_parse_definitely (parser))
5715 done = true;
5717 /* Look in the surrounding context. */
5718 if (!done)
5720 parser->scope = NULL_TREE;
5721 parser->object_scope = NULL_TREE;
5722 parser->qualifying_scope = NULL_TREE;
5723 if (processing_template_decl)
5724 cp_parser_parse_tentatively (parser);
5725 type_decl
5726 = cp_parser_class_name (parser,
5727 /*typename_keyword_p=*/false,
5728 /*template_keyword_p=*/false,
5729 typename_type,
5730 /*check_dependency=*/false,
5731 /*class_head_p=*/false,
5732 declarator_p);
5733 if (processing_template_decl
5734 && ! cp_parser_parse_definitely (parser))
5736 /* We couldn't find a type with this name. If we're parsing
5737 tentatively, fail and try something else. */
5738 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
5740 cp_parser_simulate_error (parser);
5741 return error_mark_node;
5743 /* Otherwise, accept it and check for a match at instantiation
5744 time. */
5745 type_decl = cp_parser_identifier (parser);
5746 if (type_decl != error_mark_node)
5747 type_decl = build_nt (BIT_NOT_EXPR, type_decl);
5748 return type_decl;
5751 /* If an error occurred, assume that the name of the
5752 destructor is the same as the name of the qualifying
5753 class. That allows us to keep parsing after running
5754 into ill-formed destructor names. */
5755 if (type_decl == error_mark_node && scope)
5756 return build_nt (BIT_NOT_EXPR, scope);
5757 else if (type_decl == error_mark_node)
5758 return error_mark_node;
5760 /* Check that destructor name and scope match. */
5761 if (declarator_p && scope && !check_dtor_name (scope, type_decl))
5763 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
5764 error_at (token->location,
5765 "declaration of %<~%T%> as member of %qT",
5766 type_decl, scope);
5767 cp_parser_simulate_error (parser);
5768 return error_mark_node;
5771 /* [class.dtor]
5773 A typedef-name that names a class shall not be used as the
5774 identifier in the declarator for a destructor declaration. */
5775 if (declarator_p
5776 && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
5777 && !DECL_SELF_REFERENCE_P (type_decl)
5778 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
5779 error_at (token->location,
5780 "typedef-name %qD used as destructor declarator",
5781 type_decl);
5783 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
5786 case CPP_KEYWORD:
5787 if (token->keyword == RID_OPERATOR)
5789 cp_expr id;
5791 /* This could be a template-id, so we try that first. */
5792 cp_parser_parse_tentatively (parser);
5793 /* Try a template-id. */
5794 id = cp_parser_template_id (parser, template_keyword_p,
5795 /*check_dependency_p=*/true,
5796 none_type,
5797 declarator_p);
5798 /* If that worked, we're done. */
5799 if (cp_parser_parse_definitely (parser))
5800 return id;
5801 /* We still don't know whether we're looking at an
5802 operator-function-id or a conversion-function-id. */
5803 cp_parser_parse_tentatively (parser);
5804 /* Try an operator-function-id. */
5805 id = cp_parser_operator_function_id (parser);
5806 /* If that didn't work, try a conversion-function-id. */
5807 if (!cp_parser_parse_definitely (parser))
5808 id = cp_parser_conversion_function_id (parser);
5809 else if (UDLIT_OPER_P (id))
5811 /* 17.6.3.3.5 */
5812 const char *name = UDLIT_OP_SUFFIX (id);
5813 if (name[0] != '_' && !in_system_header_at (input_location)
5814 && declarator_p)
5815 warning (OPT_Wliteral_suffix,
5816 "literal operator suffixes not preceded by %<_%>"
5817 " are reserved for future standardization");
5820 return id;
5822 /* Fall through. */
5824 default:
5825 if (optional_p)
5826 return NULL_TREE;
5827 cp_parser_error (parser, "expected unqualified-id");
5828 return error_mark_node;
5832 /* Parse an (optional) nested-name-specifier.
5834 nested-name-specifier: [C++98]
5835 class-or-namespace-name :: nested-name-specifier [opt]
5836 class-or-namespace-name :: template nested-name-specifier [opt]
5838 nested-name-specifier: [C++0x]
5839 type-name ::
5840 namespace-name ::
5841 nested-name-specifier identifier ::
5842 nested-name-specifier template [opt] simple-template-id ::
5844 PARSER->SCOPE should be set appropriately before this function is
5845 called. TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
5846 effect. TYPE_P is TRUE if we non-type bindings should be ignored
5847 in name lookups.
5849 Sets PARSER->SCOPE to the class (TYPE) or namespace
5850 (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
5851 it unchanged if there is no nested-name-specifier. Returns the new
5852 scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
5854 If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
5855 part of a declaration and/or decl-specifier. */
5857 static tree
5858 cp_parser_nested_name_specifier_opt (cp_parser *parser,
5859 bool typename_keyword_p,
5860 bool check_dependency_p,
5861 bool type_p,
5862 bool is_declaration)
5864 bool success = false;
5865 cp_token_position start = 0;
5866 cp_token *token;
5868 /* Remember where the nested-name-specifier starts. */
5869 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
5871 start = cp_lexer_token_position (parser->lexer, false);
5872 push_deferring_access_checks (dk_deferred);
5875 while (true)
5877 tree new_scope;
5878 tree old_scope;
5879 tree saved_qualifying_scope;
5880 bool template_keyword_p;
5882 /* Spot cases that cannot be the beginning of a
5883 nested-name-specifier. */
5884 token = cp_lexer_peek_token (parser->lexer);
5886 /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
5887 the already parsed nested-name-specifier. */
5888 if (token->type == CPP_NESTED_NAME_SPECIFIER)
5890 /* Grab the nested-name-specifier and continue the loop. */
5891 cp_parser_pre_parsed_nested_name_specifier (parser);
5892 /* If we originally encountered this nested-name-specifier
5893 with IS_DECLARATION set to false, we will not have
5894 resolved TYPENAME_TYPEs, so we must do so here. */
5895 if (is_declaration
5896 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
5898 new_scope = resolve_typename_type (parser->scope,
5899 /*only_current_p=*/false);
5900 if (TREE_CODE (new_scope) != TYPENAME_TYPE)
5901 parser->scope = new_scope;
5903 success = true;
5904 continue;
5907 /* Spot cases that cannot be the beginning of a
5908 nested-name-specifier. On the second and subsequent times
5909 through the loop, we look for the `template' keyword. */
5910 if (success && token->keyword == RID_TEMPLATE)
5912 /* A template-id can start a nested-name-specifier. */
5913 else if (token->type == CPP_TEMPLATE_ID)
5915 /* DR 743: decltype can be used in a nested-name-specifier. */
5916 else if (token_is_decltype (token))
5918 else
5920 /* If the next token is not an identifier, then it is
5921 definitely not a type-name or namespace-name. */
5922 if (token->type != CPP_NAME)
5923 break;
5924 /* If the following token is neither a `<' (to begin a
5925 template-id), nor a `::', then we are not looking at a
5926 nested-name-specifier. */
5927 token = cp_lexer_peek_nth_token (parser->lexer, 2);
5929 if (token->type == CPP_COLON
5930 && parser->colon_corrects_to_scope_p
5931 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_NAME)
5933 error_at (token->location,
5934 "found %<:%> in nested-name-specifier, expected %<::%>");
5935 token->type = CPP_SCOPE;
5938 if (token->type != CPP_SCOPE
5939 && !cp_parser_nth_token_starts_template_argument_list_p
5940 (parser, 2))
5941 break;
5944 /* The nested-name-specifier is optional, so we parse
5945 tentatively. */
5946 cp_parser_parse_tentatively (parser);
5948 /* Look for the optional `template' keyword, if this isn't the
5949 first time through the loop. */
5950 if (success)
5951 template_keyword_p = cp_parser_optional_template_keyword (parser);
5952 else
5953 template_keyword_p = false;
5955 /* Save the old scope since the name lookup we are about to do
5956 might destroy it. */
5957 old_scope = parser->scope;
5958 saved_qualifying_scope = parser->qualifying_scope;
5959 /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
5960 look up names in "X<T>::I" in order to determine that "Y" is
5961 a template. So, if we have a typename at this point, we make
5962 an effort to look through it. */
5963 if (is_declaration
5964 && !typename_keyword_p
5965 && parser->scope
5966 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
5967 parser->scope = resolve_typename_type (parser->scope,
5968 /*only_current_p=*/false);
5969 /* Parse the qualifying entity. */
5970 new_scope
5971 = cp_parser_qualifying_entity (parser,
5972 typename_keyword_p,
5973 template_keyword_p,
5974 check_dependency_p,
5975 type_p,
5976 is_declaration);
5977 /* Look for the `::' token. */
5978 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
5980 /* If we found what we wanted, we keep going; otherwise, we're
5981 done. */
5982 if (!cp_parser_parse_definitely (parser))
5984 bool error_p = false;
5986 /* Restore the OLD_SCOPE since it was valid before the
5987 failed attempt at finding the last
5988 class-or-namespace-name. */
5989 parser->scope = old_scope;
5990 parser->qualifying_scope = saved_qualifying_scope;
5992 /* If the next token is a decltype, and the one after that is a
5993 `::', then the decltype has failed to resolve to a class or
5994 enumeration type. Give this error even when parsing
5995 tentatively since it can't possibly be valid--and we're going
5996 to replace it with a CPP_NESTED_NAME_SPECIFIER below, so we
5997 won't get another chance.*/
5998 if (cp_lexer_next_token_is (parser->lexer, CPP_DECLTYPE)
5999 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
6000 == CPP_SCOPE))
6002 token = cp_lexer_consume_token (parser->lexer);
6003 error_at (token->location, "decltype evaluates to %qT, "
6004 "which is not a class or enumeration type",
6005 token->u.tree_check_value->value);
6006 parser->scope = error_mark_node;
6007 error_p = true;
6008 /* As below. */
6009 success = true;
6010 cp_lexer_consume_token (parser->lexer);
6013 if (cp_lexer_next_token_is (parser->lexer, CPP_TEMPLATE_ID)
6014 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_SCOPE))
6016 /* If we have a non-type template-id followed by ::, it can't
6017 possibly be valid. */
6018 token = cp_lexer_peek_token (parser->lexer);
6019 tree tid = token->u.tree_check_value->value;
6020 if (TREE_CODE (tid) == TEMPLATE_ID_EXPR
6021 && TREE_CODE (TREE_OPERAND (tid, 0)) != IDENTIFIER_NODE)
6023 tree tmpl = NULL_TREE;
6024 if (is_overloaded_fn (tid))
6026 tree fns = get_fns (tid);
6027 if (!OVL_CHAIN (fns))
6028 tmpl = OVL_CURRENT (fns);
6029 error_at (token->location, "function template-id %qD "
6030 "in nested-name-specifier", tid);
6032 else
6034 /* Variable template. */
6035 tmpl = TREE_OPERAND (tid, 0);
6036 gcc_assert (variable_template_p (tmpl));
6037 error_at (token->location, "variable template-id %qD "
6038 "in nested-name-specifier", tid);
6040 if (tmpl)
6041 inform (DECL_SOURCE_LOCATION (tmpl),
6042 "%qD declared here", tmpl);
6044 parser->scope = error_mark_node;
6045 error_p = true;
6046 /* As below. */
6047 success = true;
6048 cp_lexer_consume_token (parser->lexer);
6049 cp_lexer_consume_token (parser->lexer);
6053 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
6054 break;
6055 /* If the next token is an identifier, and the one after
6056 that is a `::', then any valid interpretation would have
6057 found a class-or-namespace-name. */
6058 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
6059 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
6060 == CPP_SCOPE)
6061 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
6062 != CPP_COMPL))
6064 token = cp_lexer_consume_token (parser->lexer);
6065 if (!error_p)
6067 if (!token->error_reported)
6069 tree decl;
6070 tree ambiguous_decls;
6072 decl = cp_parser_lookup_name (parser, token->u.value,
6073 none_type,
6074 /*is_template=*/false,
6075 /*is_namespace=*/false,
6076 /*check_dependency=*/true,
6077 &ambiguous_decls,
6078 token->location);
6079 if (TREE_CODE (decl) == TEMPLATE_DECL)
6080 error_at (token->location,
6081 "%qD used without template parameters",
6082 decl);
6083 else if (ambiguous_decls)
6085 // cp_parser_lookup_name has the same diagnostic,
6086 // thus make sure to emit it at most once.
6087 if (cp_parser_uncommitted_to_tentative_parse_p
6088 (parser))
6090 error_at (token->location,
6091 "reference to %qD is ambiguous",
6092 token->u.value);
6093 print_candidates (ambiguous_decls);
6095 decl = error_mark_node;
6097 else
6099 if (cxx_dialect != cxx98)
6100 cp_parser_name_lookup_error
6101 (parser, token->u.value, decl, NLE_NOT_CXX98,
6102 token->location);
6103 else
6104 cp_parser_name_lookup_error
6105 (parser, token->u.value, decl, NLE_CXX98,
6106 token->location);
6109 parser->scope = error_mark_node;
6110 error_p = true;
6111 /* Treat this as a successful nested-name-specifier
6112 due to:
6114 [basic.lookup.qual]
6116 If the name found is not a class-name (clause
6117 _class_) or namespace-name (_namespace.def_), the
6118 program is ill-formed. */
6119 success = true;
6121 cp_lexer_consume_token (parser->lexer);
6123 break;
6125 /* We've found one valid nested-name-specifier. */
6126 success = true;
6127 /* Name lookup always gives us a DECL. */
6128 if (TREE_CODE (new_scope) == TYPE_DECL)
6129 new_scope = TREE_TYPE (new_scope);
6130 /* Uses of "template" must be followed by actual templates. */
6131 if (template_keyword_p
6132 && !(CLASS_TYPE_P (new_scope)
6133 && ((CLASSTYPE_USE_TEMPLATE (new_scope)
6134 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (new_scope)))
6135 || CLASSTYPE_IS_TEMPLATE (new_scope)))
6136 && !(TREE_CODE (new_scope) == TYPENAME_TYPE
6137 && (TREE_CODE (TYPENAME_TYPE_FULLNAME (new_scope))
6138 == TEMPLATE_ID_EXPR)))
6139 permerror (input_location, TYPE_P (new_scope)
6140 ? G_("%qT is not a template")
6141 : G_("%qD is not a template"),
6142 new_scope);
6143 /* If it is a class scope, try to complete it; we are about to
6144 be looking up names inside the class. */
6145 if (TYPE_P (new_scope)
6146 /* Since checking types for dependency can be expensive,
6147 avoid doing it if the type is already complete. */
6148 && !COMPLETE_TYPE_P (new_scope)
6149 /* Do not try to complete dependent types. */
6150 && !dependent_type_p (new_scope))
6152 new_scope = complete_type (new_scope);
6153 /* If it is a typedef to current class, use the current
6154 class instead, as the typedef won't have any names inside
6155 it yet. */
6156 if (!COMPLETE_TYPE_P (new_scope)
6157 && currently_open_class (new_scope))
6158 new_scope = TYPE_MAIN_VARIANT (new_scope);
6160 /* Make sure we look in the right scope the next time through
6161 the loop. */
6162 parser->scope = new_scope;
6165 /* If parsing tentatively, replace the sequence of tokens that makes
6166 up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
6167 token. That way, should we re-parse the token stream, we will
6168 not have to repeat the effort required to do the parse, nor will
6169 we issue duplicate error messages. */
6170 if (success && start)
6172 cp_token *token;
6174 token = cp_lexer_token_at (parser->lexer, start);
6175 /* Reset the contents of the START token. */
6176 token->type = CPP_NESTED_NAME_SPECIFIER;
6177 /* Retrieve any deferred checks. Do not pop this access checks yet
6178 so the memory will not be reclaimed during token replacing below. */
6179 token->u.tree_check_value = ggc_cleared_alloc<struct tree_check> ();
6180 token->u.tree_check_value->value = parser->scope;
6181 token->u.tree_check_value->checks = get_deferred_access_checks ();
6182 token->u.tree_check_value->qualifying_scope =
6183 parser->qualifying_scope;
6184 token->keyword = RID_MAX;
6186 /* Purge all subsequent tokens. */
6187 cp_lexer_purge_tokens_after (parser->lexer, start);
6190 if (start)
6191 pop_to_parent_deferring_access_checks ();
6193 return success ? parser->scope : NULL_TREE;
6196 /* Parse a nested-name-specifier. See
6197 cp_parser_nested_name_specifier_opt for details. This function
6198 behaves identically, except that it will an issue an error if no
6199 nested-name-specifier is present. */
6201 static tree
6202 cp_parser_nested_name_specifier (cp_parser *parser,
6203 bool typename_keyword_p,
6204 bool check_dependency_p,
6205 bool type_p,
6206 bool is_declaration)
6208 tree scope;
6210 /* Look for the nested-name-specifier. */
6211 scope = cp_parser_nested_name_specifier_opt (parser,
6212 typename_keyword_p,
6213 check_dependency_p,
6214 type_p,
6215 is_declaration);
6216 /* If it was not present, issue an error message. */
6217 if (!scope)
6219 cp_parser_error (parser, "expected nested-name-specifier");
6220 parser->scope = NULL_TREE;
6223 return scope;
6226 /* Parse the qualifying entity in a nested-name-specifier. For C++98,
6227 this is either a class-name or a namespace-name (which corresponds
6228 to the class-or-namespace-name production in the grammar). For
6229 C++0x, it can also be a type-name that refers to an enumeration
6230 type or a simple-template-id.
6232 TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
6233 TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
6234 CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
6235 TYPE_P is TRUE iff the next name should be taken as a class-name,
6236 even the same name is declared to be another entity in the same
6237 scope.
6239 Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
6240 specified by the class-or-namespace-name. If neither is found the
6241 ERROR_MARK_NODE is returned. */
6243 static tree
6244 cp_parser_qualifying_entity (cp_parser *parser,
6245 bool typename_keyword_p,
6246 bool template_keyword_p,
6247 bool check_dependency_p,
6248 bool type_p,
6249 bool is_declaration)
6251 tree saved_scope;
6252 tree saved_qualifying_scope;
6253 tree saved_object_scope;
6254 tree scope;
6255 bool only_class_p;
6256 bool successful_parse_p;
6258 /* DR 743: decltype can appear in a nested-name-specifier. */
6259 if (cp_lexer_next_token_is_decltype (parser->lexer))
6261 scope = cp_parser_decltype (parser);
6262 if (TREE_CODE (scope) != ENUMERAL_TYPE
6263 && !MAYBE_CLASS_TYPE_P (scope))
6265 cp_parser_simulate_error (parser);
6266 return error_mark_node;
6268 if (TYPE_NAME (scope))
6269 scope = TYPE_NAME (scope);
6270 return scope;
6273 /* Before we try to parse the class-name, we must save away the
6274 current PARSER->SCOPE since cp_parser_class_name will destroy
6275 it. */
6276 saved_scope = parser->scope;
6277 saved_qualifying_scope = parser->qualifying_scope;
6278 saved_object_scope = parser->object_scope;
6279 /* Try for a class-name first. If the SAVED_SCOPE is a type, then
6280 there is no need to look for a namespace-name. */
6281 only_class_p = template_keyword_p
6282 || (saved_scope && TYPE_P (saved_scope) && cxx_dialect == cxx98);
6283 if (!only_class_p)
6284 cp_parser_parse_tentatively (parser);
6285 scope = cp_parser_class_name (parser,
6286 typename_keyword_p,
6287 template_keyword_p,
6288 type_p ? class_type : none_type,
6289 check_dependency_p,
6290 /*class_head_p=*/false,
6291 is_declaration,
6292 /*enum_ok=*/cxx_dialect > cxx98);
6293 successful_parse_p = only_class_p || cp_parser_parse_definitely (parser);
6294 /* If that didn't work, try for a namespace-name. */
6295 if (!only_class_p && !successful_parse_p)
6297 /* Restore the saved scope. */
6298 parser->scope = saved_scope;
6299 parser->qualifying_scope = saved_qualifying_scope;
6300 parser->object_scope = saved_object_scope;
6301 /* If we are not looking at an identifier followed by the scope
6302 resolution operator, then this is not part of a
6303 nested-name-specifier. (Note that this function is only used
6304 to parse the components of a nested-name-specifier.) */
6305 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
6306 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
6307 return error_mark_node;
6308 scope = cp_parser_namespace_name (parser);
6311 return scope;
6314 /* Return true if we are looking at a compound-literal, false otherwise. */
6316 static bool
6317 cp_parser_compound_literal_p (cp_parser *parser)
6319 /* Consume the `('. */
6320 cp_lexer_consume_token (parser->lexer);
6322 cp_lexer_save_tokens (parser->lexer);
6324 /* Skip tokens until the next token is a closing parenthesis.
6325 If we find the closing `)', and the next token is a `{', then
6326 we are looking at a compound-literal. */
6327 bool compound_literal_p
6328 = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
6329 /*consume_paren=*/true)
6330 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
6332 /* Roll back the tokens we skipped. */
6333 cp_lexer_rollback_tokens (parser->lexer);
6335 return compound_literal_p;
6338 /* Parse a postfix-expression.
6340 postfix-expression:
6341 primary-expression
6342 postfix-expression [ expression ]
6343 postfix-expression ( expression-list [opt] )
6344 simple-type-specifier ( expression-list [opt] )
6345 typename :: [opt] nested-name-specifier identifier
6346 ( expression-list [opt] )
6347 typename :: [opt] nested-name-specifier template [opt] template-id
6348 ( expression-list [opt] )
6349 postfix-expression . template [opt] id-expression
6350 postfix-expression -> template [opt] id-expression
6351 postfix-expression . pseudo-destructor-name
6352 postfix-expression -> pseudo-destructor-name
6353 postfix-expression ++
6354 postfix-expression --
6355 dynamic_cast < type-id > ( expression )
6356 static_cast < type-id > ( expression )
6357 reinterpret_cast < type-id > ( expression )
6358 const_cast < type-id > ( expression )
6359 typeid ( expression )
6360 typeid ( type-id )
6362 GNU Extension:
6364 postfix-expression:
6365 ( type-id ) { initializer-list , [opt] }
6367 This extension is a GNU version of the C99 compound-literal
6368 construct. (The C99 grammar uses `type-name' instead of `type-id',
6369 but they are essentially the same concept.)
6371 If ADDRESS_P is true, the postfix expression is the operand of the
6372 `&' operator. CAST_P is true if this expression is the target of a
6373 cast.
6375 If MEMBER_ACCESS_ONLY_P, we only allow postfix expressions that are
6376 class member access expressions [expr.ref].
6378 Returns a representation of the expression. */
6380 static cp_expr
6381 cp_parser_postfix_expression (cp_parser *parser, bool address_p, bool cast_p,
6382 bool member_access_only_p, bool decltype_p,
6383 cp_id_kind * pidk_return)
6385 cp_token *token;
6386 location_t loc;
6387 enum rid keyword;
6388 cp_id_kind idk = CP_ID_KIND_NONE;
6389 cp_expr postfix_expression = NULL_TREE;
6390 bool is_member_access = false;
6391 int saved_in_statement = -1;
6393 /* Peek at the next token. */
6394 token = cp_lexer_peek_token (parser->lexer);
6395 loc = token->location;
6396 location_t start_loc = get_range_from_loc (line_table, loc).m_start;
6398 /* Some of the productions are determined by keywords. */
6399 keyword = token->keyword;
6400 switch (keyword)
6402 case RID_DYNCAST:
6403 case RID_STATCAST:
6404 case RID_REINTCAST:
6405 case RID_CONSTCAST:
6407 tree type;
6408 cp_expr expression;
6409 const char *saved_message;
6410 bool saved_in_type_id_in_expr_p;
6412 /* All of these can be handled in the same way from the point
6413 of view of parsing. Begin by consuming the token
6414 identifying the cast. */
6415 cp_lexer_consume_token (parser->lexer);
6417 /* New types cannot be defined in the cast. */
6418 saved_message = parser->type_definition_forbidden_message;
6419 parser->type_definition_forbidden_message
6420 = G_("types may not be defined in casts");
6422 /* Look for the opening `<'. */
6423 cp_parser_require (parser, CPP_LESS, RT_LESS);
6424 /* Parse the type to which we are casting. */
6425 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
6426 parser->in_type_id_in_expr_p = true;
6427 type = cp_parser_type_id (parser);
6428 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
6429 /* Look for the closing `>'. */
6430 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
6431 /* Restore the old message. */
6432 parser->type_definition_forbidden_message = saved_message;
6434 bool saved_greater_than_is_operator_p
6435 = parser->greater_than_is_operator_p;
6436 parser->greater_than_is_operator_p = true;
6438 /* And the expression which is being cast. */
6439 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
6440 expression = cp_parser_expression (parser, & idk, /*cast_p=*/true);
6441 cp_token *close_paren = cp_parser_require (parser, CPP_CLOSE_PAREN,
6442 RT_CLOSE_PAREN);
6443 location_t end_loc = close_paren ?
6444 close_paren->location : UNKNOWN_LOCATION;
6446 parser->greater_than_is_operator_p
6447 = saved_greater_than_is_operator_p;
6449 /* Only type conversions to integral or enumeration types
6450 can be used in constant-expressions. */
6451 if (!cast_valid_in_integral_constant_expression_p (type)
6452 && cp_parser_non_integral_constant_expression (parser, NIC_CAST))
6454 postfix_expression = error_mark_node;
6455 break;
6458 switch (keyword)
6460 case RID_DYNCAST:
6461 postfix_expression
6462 = build_dynamic_cast (type, expression, tf_warning_or_error);
6463 break;
6464 case RID_STATCAST:
6465 postfix_expression
6466 = build_static_cast (type, expression, tf_warning_or_error);
6467 break;
6468 case RID_REINTCAST:
6469 postfix_expression
6470 = build_reinterpret_cast (type, expression,
6471 tf_warning_or_error);
6472 break;
6473 case RID_CONSTCAST:
6474 postfix_expression
6475 = build_const_cast (type, expression, tf_warning_or_error);
6476 break;
6477 default:
6478 gcc_unreachable ();
6481 /* Construct a location e.g. :
6482 reinterpret_cast <int *> (expr)
6483 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
6484 ranging from the start of the "*_cast" token to the final closing
6485 paren, with the caret at the start. */
6486 location_t cp_cast_loc = make_location (start_loc, start_loc, end_loc);
6487 postfix_expression.set_location (cp_cast_loc);
6489 break;
6491 case RID_TYPEID:
6493 tree type;
6494 const char *saved_message;
6495 bool saved_in_type_id_in_expr_p;
6497 /* Consume the `typeid' token. */
6498 cp_lexer_consume_token (parser->lexer);
6499 /* Look for the `(' token. */
6500 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
6501 /* Types cannot be defined in a `typeid' expression. */
6502 saved_message = parser->type_definition_forbidden_message;
6503 parser->type_definition_forbidden_message
6504 = G_("types may not be defined in a %<typeid%> expression");
6505 /* We can't be sure yet whether we're looking at a type-id or an
6506 expression. */
6507 cp_parser_parse_tentatively (parser);
6508 /* Try a type-id first. */
6509 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
6510 parser->in_type_id_in_expr_p = true;
6511 type = cp_parser_type_id (parser);
6512 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
6513 /* Look for the `)' token. Otherwise, we can't be sure that
6514 we're not looking at an expression: consider `typeid (int
6515 (3))', for example. */
6516 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
6517 /* If all went well, simply lookup the type-id. */
6518 if (cp_parser_parse_definitely (parser))
6519 postfix_expression = get_typeid (type, tf_warning_or_error);
6520 /* Otherwise, fall back to the expression variant. */
6521 else
6523 tree expression;
6525 /* Look for an expression. */
6526 expression = cp_parser_expression (parser, & idk);
6527 /* Compute its typeid. */
6528 postfix_expression = build_typeid (expression, tf_warning_or_error);
6529 /* Look for the `)' token. */
6530 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
6532 /* Restore the saved message. */
6533 parser->type_definition_forbidden_message = saved_message;
6534 /* `typeid' may not appear in an integral constant expression. */
6535 if (cp_parser_non_integral_constant_expression (parser, NIC_TYPEID))
6536 postfix_expression = error_mark_node;
6538 break;
6540 case RID_TYPENAME:
6542 tree type;
6543 /* The syntax permitted here is the same permitted for an
6544 elaborated-type-specifier. */
6545 ++parser->prevent_constrained_type_specifiers;
6546 type = cp_parser_elaborated_type_specifier (parser,
6547 /*is_friend=*/false,
6548 /*is_declaration=*/false);
6549 --parser->prevent_constrained_type_specifiers;
6550 postfix_expression = cp_parser_functional_cast (parser, type);
6552 break;
6554 case RID_CILK_SPAWN:
6556 location_t cilk_spawn_loc
6557 = cp_lexer_peek_token (parser->lexer)->location;
6558 cp_lexer_consume_token (parser->lexer);
6559 token = cp_lexer_peek_token (parser->lexer);
6560 if (token->type == CPP_SEMICOLON)
6562 error_at (token->location, "%<_Cilk_spawn%> must be followed by "
6563 "an expression");
6564 postfix_expression = error_mark_node;
6565 break;
6567 else if (!current_function_decl)
6569 error_at (token->location, "%<_Cilk_spawn%> may only be used "
6570 "inside a function");
6571 postfix_expression = error_mark_node;
6572 break;
6574 else
6576 /* Consecutive _Cilk_spawns are not allowed in a statement. */
6577 saved_in_statement = parser->in_statement;
6578 parser->in_statement |= IN_CILK_SPAWN;
6580 cfun->calls_cilk_spawn = 1;
6581 postfix_expression =
6582 cp_parser_postfix_expression (parser, false, false,
6583 false, false, &idk);
6584 if (!flag_cilkplus)
6586 error_at (token->location, "-fcilkplus must be enabled to use"
6587 " %<_Cilk_spawn%>");
6588 cfun->calls_cilk_spawn = 0;
6590 else if (saved_in_statement & IN_CILK_SPAWN)
6592 error_at (token->location, "consecutive %<_Cilk_spawn%> keywords "
6593 "are not permitted");
6594 postfix_expression = error_mark_node;
6595 cfun->calls_cilk_spawn = 0;
6597 else
6599 location_t loc = postfix_expression.get_location ();
6600 postfix_expression = build_cilk_spawn (token->location,
6601 postfix_expression);
6602 /* Build a location of the form:
6603 _Cilk_spawn expr
6604 ~~~~~~~~~~~~^~~~
6605 with caret at the expr, ranging from the start of the
6606 _Cilk_spawn token to the end of the expression. */
6607 location_t combined_loc =
6608 make_location (loc, cilk_spawn_loc, get_finish (loc));
6609 postfix_expression.set_location (combined_loc);
6610 if (postfix_expression != error_mark_node)
6611 SET_EXPR_LOCATION (postfix_expression, input_location);
6612 parser->in_statement = parser->in_statement & ~IN_CILK_SPAWN;
6614 break;
6617 case RID_ADDRESSOF:
6618 case RID_BUILTIN_SHUFFLE:
6619 case RID_BUILTIN_LAUNDER:
6621 vec<tree, va_gc> *vec;
6622 unsigned int i;
6623 tree p;
6625 cp_lexer_consume_token (parser->lexer);
6626 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
6627 /*cast_p=*/false, /*allow_expansion_p=*/true,
6628 /*non_constant_p=*/NULL);
6629 if (vec == NULL)
6631 postfix_expression = error_mark_node;
6632 break;
6635 FOR_EACH_VEC_ELT (*vec, i, p)
6636 mark_exp_read (p);
6638 switch (keyword)
6640 case RID_ADDRESSOF:
6641 if (vec->length () == 1)
6642 postfix_expression
6643 = cp_build_addressof (loc, (*vec)[0], tf_warning_or_error);
6644 else
6646 error_at (loc, "wrong number of arguments to "
6647 "%<__builtin_addressof%>");
6648 postfix_expression = error_mark_node;
6650 break;
6652 case RID_BUILTIN_LAUNDER:
6653 if (vec->length () == 1)
6654 postfix_expression = finish_builtin_launder (loc, (*vec)[0],
6655 tf_warning_or_error);
6656 else
6658 error_at (loc, "wrong number of arguments to "
6659 "%<__builtin_launder%>");
6660 postfix_expression = error_mark_node;
6662 break;
6664 case RID_BUILTIN_SHUFFLE:
6665 if (vec->length () == 2)
6666 postfix_expression
6667 = build_x_vec_perm_expr (loc, (*vec)[0], NULL_TREE,
6668 (*vec)[1], tf_warning_or_error);
6669 else if (vec->length () == 3)
6670 postfix_expression
6671 = build_x_vec_perm_expr (loc, (*vec)[0], (*vec)[1],
6672 (*vec)[2], tf_warning_or_error);
6673 else
6675 error_at (loc, "wrong number of arguments to "
6676 "%<__builtin_shuffle%>");
6677 postfix_expression = error_mark_node;
6679 break;
6681 default:
6682 gcc_unreachable ();
6684 break;
6687 default:
6689 tree type;
6691 /* If the next thing is a simple-type-specifier, we may be
6692 looking at a functional cast. We could also be looking at
6693 an id-expression. So, we try the functional cast, and if
6694 that doesn't work we fall back to the primary-expression. */
6695 cp_parser_parse_tentatively (parser);
6696 /* Look for the simple-type-specifier. */
6697 ++parser->prevent_constrained_type_specifiers;
6698 type = cp_parser_simple_type_specifier (parser,
6699 /*decl_specs=*/NULL,
6700 CP_PARSER_FLAGS_NONE);
6701 --parser->prevent_constrained_type_specifiers;
6702 /* Parse the cast itself. */
6703 if (!cp_parser_error_occurred (parser))
6704 postfix_expression
6705 = cp_parser_functional_cast (parser, type);
6706 /* If that worked, we're done. */
6707 if (cp_parser_parse_definitely (parser))
6708 break;
6710 /* If the functional-cast didn't work out, try a
6711 compound-literal. */
6712 if (cp_parser_allow_gnu_extensions_p (parser)
6713 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
6715 cp_expr initializer = NULL_TREE;
6717 cp_parser_parse_tentatively (parser);
6719 /* Avoid calling cp_parser_type_id pointlessly, see comment
6720 in cp_parser_cast_expression about c++/29234. */
6721 if (!cp_parser_compound_literal_p (parser))
6722 cp_parser_simulate_error (parser);
6723 else
6725 /* Parse the type. */
6726 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
6727 parser->in_type_id_in_expr_p = true;
6728 type = cp_parser_type_id (parser);
6729 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
6730 /* Look for the `)'. */
6731 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
6734 /* If things aren't going well, there's no need to
6735 keep going. */
6736 if (!cp_parser_error_occurred (parser))
6738 bool non_constant_p;
6739 /* Parse the brace-enclosed initializer list. */
6740 initializer = cp_parser_braced_list (parser,
6741 &non_constant_p);
6743 /* If that worked, we're definitely looking at a
6744 compound-literal expression. */
6745 if (cp_parser_parse_definitely (parser))
6747 /* Warn the user that a compound literal is not
6748 allowed in standard C++. */
6749 pedwarn (input_location, OPT_Wpedantic,
6750 "ISO C++ forbids compound-literals");
6751 /* For simplicity, we disallow compound literals in
6752 constant-expressions. We could
6753 allow compound literals of integer type, whose
6754 initializer was a constant, in constant
6755 expressions. Permitting that usage, as a further
6756 extension, would not change the meaning of any
6757 currently accepted programs. (Of course, as
6758 compound literals are not part of ISO C++, the
6759 standard has nothing to say.) */
6760 if (cp_parser_non_integral_constant_expression (parser,
6761 NIC_NCC))
6763 postfix_expression = error_mark_node;
6764 break;
6766 /* Form the representation of the compound-literal. */
6767 postfix_expression
6768 = finish_compound_literal (type, initializer,
6769 tf_warning_or_error);
6770 postfix_expression.set_location (initializer.get_location ());
6771 break;
6775 /* It must be a primary-expression. */
6776 postfix_expression
6777 = cp_parser_primary_expression (parser, address_p, cast_p,
6778 /*template_arg_p=*/false,
6779 decltype_p,
6780 &idk);
6782 break;
6785 /* Note that we don't need to worry about calling build_cplus_new on a
6786 class-valued CALL_EXPR in decltype when it isn't the end of the
6787 postfix-expression; unary_complex_lvalue will take care of that for
6788 all these cases. */
6790 /* Keep looping until the postfix-expression is complete. */
6791 while (true)
6793 if (idk == CP_ID_KIND_UNQUALIFIED
6794 && identifier_p (postfix_expression)
6795 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
6796 /* It is not a Koenig lookup function call. */
6797 postfix_expression
6798 = unqualified_name_lookup_error (postfix_expression);
6800 /* Peek at the next token. */
6801 token = cp_lexer_peek_token (parser->lexer);
6803 switch (token->type)
6805 case CPP_OPEN_SQUARE:
6806 if (cp_next_tokens_can_be_std_attribute_p (parser))
6808 cp_parser_error (parser,
6809 "two consecutive %<[%> shall "
6810 "only introduce an attribute");
6811 return error_mark_node;
6813 postfix_expression
6814 = cp_parser_postfix_open_square_expression (parser,
6815 postfix_expression,
6816 false,
6817 decltype_p);
6818 postfix_expression.set_range (start_loc,
6819 postfix_expression.get_location ());
6821 idk = CP_ID_KIND_NONE;
6822 is_member_access = false;
6823 break;
6825 case CPP_OPEN_PAREN:
6826 /* postfix-expression ( expression-list [opt] ) */
6828 bool koenig_p;
6829 bool is_builtin_constant_p;
6830 bool saved_integral_constant_expression_p = false;
6831 bool saved_non_integral_constant_expression_p = false;
6832 tsubst_flags_t complain = complain_flags (decltype_p);
6833 vec<tree, va_gc> *args;
6834 location_t close_paren_loc = UNKNOWN_LOCATION;
6836 is_member_access = false;
6838 is_builtin_constant_p
6839 = DECL_IS_BUILTIN_CONSTANT_P (postfix_expression);
6840 if (is_builtin_constant_p)
6842 /* The whole point of __builtin_constant_p is to allow
6843 non-constant expressions to appear as arguments. */
6844 saved_integral_constant_expression_p
6845 = parser->integral_constant_expression_p;
6846 saved_non_integral_constant_expression_p
6847 = parser->non_integral_constant_expression_p;
6848 parser->integral_constant_expression_p = false;
6850 args = (cp_parser_parenthesized_expression_list
6851 (parser, non_attr,
6852 /*cast_p=*/false, /*allow_expansion_p=*/true,
6853 /*non_constant_p=*/NULL,
6854 /*close_paren_loc=*/&close_paren_loc));
6855 if (is_builtin_constant_p)
6857 parser->integral_constant_expression_p
6858 = saved_integral_constant_expression_p;
6859 parser->non_integral_constant_expression_p
6860 = saved_non_integral_constant_expression_p;
6863 if (args == NULL)
6865 postfix_expression = error_mark_node;
6866 break;
6869 /* Function calls are not permitted in
6870 constant-expressions. */
6871 if (! builtin_valid_in_constant_expr_p (postfix_expression)
6872 && cp_parser_non_integral_constant_expression (parser,
6873 NIC_FUNC_CALL))
6875 postfix_expression = error_mark_node;
6876 release_tree_vector (args);
6877 break;
6880 koenig_p = false;
6881 if (idk == CP_ID_KIND_UNQUALIFIED
6882 || idk == CP_ID_KIND_TEMPLATE_ID)
6884 if (identifier_p (postfix_expression))
6886 if (!args->is_empty ())
6888 koenig_p = true;
6889 if (!any_type_dependent_arguments_p (args))
6890 postfix_expression
6891 = perform_koenig_lookup (postfix_expression, args,
6892 complain);
6894 else
6895 postfix_expression
6896 = unqualified_fn_lookup_error (postfix_expression);
6898 /* We do not perform argument-dependent lookup if
6899 normal lookup finds a non-function, in accordance
6900 with the expected resolution of DR 218. */
6901 else if (!args->is_empty ()
6902 && is_overloaded_fn (postfix_expression))
6904 tree fn = get_first_fn (postfix_expression);
6905 fn = STRIP_TEMPLATE (fn);
6907 /* Do not do argument dependent lookup if regular
6908 lookup finds a member function or a block-scope
6909 function declaration. [basic.lookup.argdep]/3 */
6910 if (!DECL_FUNCTION_MEMBER_P (fn)
6911 && !DECL_LOCAL_FUNCTION_P (fn))
6913 koenig_p = true;
6914 if (!any_type_dependent_arguments_p (args))
6915 postfix_expression
6916 = perform_koenig_lookup (postfix_expression, args,
6917 complain);
6922 if (TREE_CODE (postfix_expression) == FUNCTION_DECL
6923 && DECL_BUILT_IN_CLASS (postfix_expression) == BUILT_IN_NORMAL
6924 && DECL_FUNCTION_CODE (postfix_expression) == BUILT_IN_MEMSET
6925 && vec_safe_length (args) == 3)
6927 tree arg0 = (*args)[0];
6928 tree arg1 = (*args)[1];
6929 tree arg2 = (*args)[2];
6930 int literal_mask = ((!!integer_zerop (arg1) << 1)
6931 | (!!integer_zerop (arg2) << 2));
6932 if (TREE_CODE (arg2) == CONST_DECL)
6933 arg2 = DECL_INITIAL (arg2);
6934 warn_for_memset (input_location, arg0, arg2, literal_mask);
6937 if (TREE_CODE (postfix_expression) == COMPONENT_REF)
6939 tree instance = TREE_OPERAND (postfix_expression, 0);
6940 tree fn = TREE_OPERAND (postfix_expression, 1);
6942 if (processing_template_decl
6943 && (type_dependent_object_expression_p (instance)
6944 || (!BASELINK_P (fn)
6945 && TREE_CODE (fn) != FIELD_DECL)
6946 || type_dependent_expression_p (fn)
6947 || any_type_dependent_arguments_p (args)))
6949 maybe_generic_this_capture (instance, fn);
6950 postfix_expression
6951 = build_nt_call_vec (postfix_expression, args);
6952 release_tree_vector (args);
6953 break;
6956 if (BASELINK_P (fn))
6958 postfix_expression
6959 = (build_new_method_call
6960 (instance, fn, &args, NULL_TREE,
6961 (idk == CP_ID_KIND_QUALIFIED
6962 ? LOOKUP_NORMAL|LOOKUP_NONVIRTUAL
6963 : LOOKUP_NORMAL),
6964 /*fn_p=*/NULL,
6965 complain));
6967 else
6968 postfix_expression
6969 = finish_call_expr (postfix_expression, &args,
6970 /*disallow_virtual=*/false,
6971 /*koenig_p=*/false,
6972 complain);
6974 else if (TREE_CODE (postfix_expression) == OFFSET_REF
6975 || TREE_CODE (postfix_expression) == MEMBER_REF
6976 || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
6977 postfix_expression = (build_offset_ref_call_from_tree
6978 (postfix_expression, &args,
6979 complain));
6980 else if (idk == CP_ID_KIND_QUALIFIED)
6981 /* A call to a static class member, or a namespace-scope
6982 function. */
6983 postfix_expression
6984 = finish_call_expr (postfix_expression, &args,
6985 /*disallow_virtual=*/true,
6986 koenig_p,
6987 complain);
6988 else
6989 /* All other function calls. */
6990 postfix_expression
6991 = finish_call_expr (postfix_expression, &args,
6992 /*disallow_virtual=*/false,
6993 koenig_p,
6994 complain);
6996 if (close_paren_loc != UNKNOWN_LOCATION)
6998 location_t combined_loc = make_location (token->location,
6999 start_loc,
7000 close_paren_loc);
7001 postfix_expression.set_location (combined_loc);
7004 /* The POSTFIX_EXPRESSION is certainly no longer an id. */
7005 idk = CP_ID_KIND_NONE;
7007 release_tree_vector (args);
7009 break;
7011 case CPP_DOT:
7012 case CPP_DEREF:
7013 /* postfix-expression . template [opt] id-expression
7014 postfix-expression . pseudo-destructor-name
7015 postfix-expression -> template [opt] id-expression
7016 postfix-expression -> pseudo-destructor-name */
7018 /* Consume the `.' or `->' operator. */
7019 cp_lexer_consume_token (parser->lexer);
7021 postfix_expression
7022 = cp_parser_postfix_dot_deref_expression (parser, token->type,
7023 postfix_expression,
7024 false, &idk, loc);
7026 is_member_access = true;
7027 break;
7029 case CPP_PLUS_PLUS:
7030 /* postfix-expression ++ */
7031 /* Consume the `++' token. */
7032 cp_lexer_consume_token (parser->lexer);
7033 /* Generate a representation for the complete expression. */
7034 postfix_expression
7035 = finish_increment_expr (postfix_expression,
7036 POSTINCREMENT_EXPR);
7037 /* Increments may not appear in constant-expressions. */
7038 if (cp_parser_non_integral_constant_expression (parser, NIC_INC))
7039 postfix_expression = error_mark_node;
7040 idk = CP_ID_KIND_NONE;
7041 is_member_access = false;
7042 break;
7044 case CPP_MINUS_MINUS:
7045 /* postfix-expression -- */
7046 /* Consume the `--' token. */
7047 cp_lexer_consume_token (parser->lexer);
7048 /* Generate a representation for the complete expression. */
7049 postfix_expression
7050 = finish_increment_expr (postfix_expression,
7051 POSTDECREMENT_EXPR);
7052 /* Decrements may not appear in constant-expressions. */
7053 if (cp_parser_non_integral_constant_expression (parser, NIC_DEC))
7054 postfix_expression = error_mark_node;
7055 idk = CP_ID_KIND_NONE;
7056 is_member_access = false;
7057 break;
7059 default:
7060 if (pidk_return != NULL)
7061 * pidk_return = idk;
7062 if (member_access_only_p)
7063 return is_member_access
7064 ? postfix_expression
7065 : cp_expr (error_mark_node);
7066 else
7067 return postfix_expression;
7071 /* We should never get here. */
7072 gcc_unreachable ();
7073 return error_mark_node;
7076 /* This function parses Cilk Plus array notations. If a normal array expr. is
7077 parsed then the array index is passed back to the caller through *INIT_INDEX
7078 and the function returns a NULL_TREE. If array notation expr. is parsed,
7079 then *INIT_INDEX is ignored by the caller and the function returns
7080 a tree of type ARRAY_NOTATION_REF. If some error occurred it returns
7081 error_mark_node. */
7083 static tree
7084 cp_parser_array_notation (location_t loc, cp_parser *parser, tree *init_index,
7085 tree array_value)
7087 cp_token *token = NULL;
7088 tree length_index, stride = NULL_TREE, value_tree, array_type;
7089 if (!array_value || array_value == error_mark_node)
7091 cp_parser_skip_to_end_of_statement (parser);
7092 return error_mark_node;
7095 array_type = TREE_TYPE (array_value);
7097 bool saved_colon_corrects = parser->colon_corrects_to_scope_p;
7098 parser->colon_corrects_to_scope_p = false;
7099 token = cp_lexer_peek_token (parser->lexer);
7101 if (!token)
7103 cp_parser_error (parser, "expected %<:%> or numeral");
7104 return error_mark_node;
7106 else if (token->type == CPP_COLON)
7108 /* Consume the ':'. */
7109 cp_lexer_consume_token (parser->lexer);
7111 /* If we are here, then we have a case like this A[:]. */
7112 if (cp_lexer_peek_token (parser->lexer)->type != CPP_CLOSE_SQUARE)
7114 cp_parser_error (parser, "expected %<]%>");
7115 cp_parser_skip_to_end_of_statement (parser);
7116 return error_mark_node;
7118 *init_index = NULL_TREE;
7119 stride = NULL_TREE;
7120 length_index = NULL_TREE;
7122 else
7124 /* If we are here, then there are three valid possibilities:
7125 1. ARRAY [ EXP ]
7126 2. ARRAY [ EXP : EXP ]
7127 3. ARRAY [ EXP : EXP : EXP ] */
7129 *init_index = cp_parser_expression (parser);
7130 if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
7132 /* This indicates that we have a normal array expression. */
7133 parser->colon_corrects_to_scope_p = saved_colon_corrects;
7134 return NULL_TREE;
7137 /* Consume the ':'. */
7138 cp_lexer_consume_token (parser->lexer);
7139 length_index = cp_parser_expression (parser);
7140 if (cp_lexer_peek_token (parser->lexer)->type == CPP_COLON)
7142 cp_lexer_consume_token (parser->lexer);
7143 stride = cp_parser_expression (parser);
7146 parser->colon_corrects_to_scope_p = saved_colon_corrects;
7148 if (*init_index == error_mark_node || length_index == error_mark_node
7149 || stride == error_mark_node || array_type == error_mark_node)
7151 if (cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_SQUARE)
7152 cp_lexer_consume_token (parser->lexer);
7153 return error_mark_node;
7155 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
7157 value_tree = build_array_notation_ref (loc, array_value, *init_index,
7158 length_index, stride, array_type);
7159 return value_tree;
7162 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
7163 by cp_parser_builtin_offsetof. We're looking for
7165 postfix-expression [ expression ]
7166 postfix-expression [ braced-init-list ] (C++11)
7168 FOR_OFFSETOF is set if we're being called in that context, which
7169 changes how we deal with integer constant expressions. */
7171 static tree
7172 cp_parser_postfix_open_square_expression (cp_parser *parser,
7173 tree postfix_expression,
7174 bool for_offsetof,
7175 bool decltype_p)
7177 tree index = NULL_TREE;
7178 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
7179 bool saved_greater_than_is_operator_p;
7181 /* Consume the `[' token. */
7182 cp_lexer_consume_token (parser->lexer);
7184 saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
7185 parser->greater_than_is_operator_p = true;
7187 /* Parse the index expression. */
7188 /* ??? For offsetof, there is a question of what to allow here. If
7189 offsetof is not being used in an integral constant expression context,
7190 then we *could* get the right answer by computing the value at runtime.
7191 If we are in an integral constant expression context, then we might
7192 could accept any constant expression; hard to say without analysis.
7193 Rather than open the barn door too wide right away, allow only integer
7194 constant expressions here. */
7195 if (for_offsetof)
7196 index = cp_parser_constant_expression (parser);
7197 else
7199 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7201 bool expr_nonconst_p;
7202 cp_lexer_set_source_position (parser->lexer);
7203 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
7204 index = cp_parser_braced_list (parser, &expr_nonconst_p);
7205 if (flag_cilkplus
7206 && cp_lexer_peek_token (parser->lexer)->type == CPP_COLON)
7208 error_at (cp_lexer_peek_token (parser->lexer)->location,
7209 "braced list index is not allowed with array "
7210 "notation");
7211 cp_parser_skip_to_end_of_statement (parser);
7212 return error_mark_node;
7215 else if (flag_cilkplus)
7217 /* Here are have these two options:
7218 ARRAY[EXP : EXP] - Array notation expr with default
7219 stride of 1.
7220 ARRAY[EXP : EXP : EXP] - Array Notation with user-defined
7221 stride. */
7222 tree an_exp = cp_parser_array_notation (loc, parser, &index,
7223 postfix_expression);
7224 if (an_exp)
7225 return an_exp;
7227 else
7228 index = cp_parser_expression (parser);
7231 parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
7233 /* Look for the closing `]'. */
7234 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
7236 /* Build the ARRAY_REF. */
7237 postfix_expression = grok_array_decl (loc, postfix_expression,
7238 index, decltype_p);
7240 /* When not doing offsetof, array references are not permitted in
7241 constant-expressions. */
7242 if (!for_offsetof
7243 && (cp_parser_non_integral_constant_expression (parser, NIC_ARRAY_REF)))
7244 postfix_expression = error_mark_node;
7246 return postfix_expression;
7249 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
7250 by cp_parser_builtin_offsetof. We're looking for
7252 postfix-expression . template [opt] id-expression
7253 postfix-expression . pseudo-destructor-name
7254 postfix-expression -> template [opt] id-expression
7255 postfix-expression -> pseudo-destructor-name
7257 FOR_OFFSETOF is set if we're being called in that context. That sorta
7258 limits what of the above we'll actually accept, but nevermind.
7259 TOKEN_TYPE is the "." or "->" token, which will already have been
7260 removed from the stream. */
7262 static tree
7263 cp_parser_postfix_dot_deref_expression (cp_parser *parser,
7264 enum cpp_ttype token_type,
7265 cp_expr postfix_expression,
7266 bool for_offsetof, cp_id_kind *idk,
7267 location_t location)
7269 tree name;
7270 bool dependent_p;
7271 bool pseudo_destructor_p;
7272 tree scope = NULL_TREE;
7273 location_t start_loc = postfix_expression.get_start ();
7275 /* If this is a `->' operator, dereference the pointer. */
7276 if (token_type == CPP_DEREF)
7277 postfix_expression = build_x_arrow (location, postfix_expression,
7278 tf_warning_or_error);
7279 /* Check to see whether or not the expression is type-dependent and
7280 not the current instantiation. */
7281 dependent_p = type_dependent_object_expression_p (postfix_expression);
7282 /* The identifier following the `->' or `.' is not qualified. */
7283 parser->scope = NULL_TREE;
7284 parser->qualifying_scope = NULL_TREE;
7285 parser->object_scope = NULL_TREE;
7286 *idk = CP_ID_KIND_NONE;
7288 /* Enter the scope corresponding to the type of the object
7289 given by the POSTFIX_EXPRESSION. */
7290 if (!dependent_p)
7292 scope = TREE_TYPE (postfix_expression);
7293 /* According to the standard, no expression should ever have
7294 reference type. Unfortunately, we do not currently match
7295 the standard in this respect in that our internal representation
7296 of an expression may have reference type even when the standard
7297 says it does not. Therefore, we have to manually obtain the
7298 underlying type here. */
7299 scope = non_reference (scope);
7300 /* The type of the POSTFIX_EXPRESSION must be complete. */
7301 /* Unlike the object expression in other contexts, *this is not
7302 required to be of complete type for purposes of class member
7303 access (5.2.5) outside the member function body. */
7304 if (postfix_expression != current_class_ref
7305 && scope != error_mark_node
7306 && !(processing_template_decl
7307 && current_class_type
7308 && (same_type_ignoring_top_level_qualifiers_p
7309 (scope, current_class_type))))
7311 scope = complete_type (scope);
7312 if (!COMPLETE_TYPE_P (scope)
7313 /* Avoid clobbering e.g. OVERLOADs or DECLs. */
7314 && EXPR_P (postfix_expression))
7316 /* In a template, be permissive by treating an object expression
7317 of incomplete type as dependent (after a pedwarn). */
7318 diagnostic_t kind = (processing_template_decl
7319 ? DK_PEDWARN
7320 : DK_ERROR);
7321 cxx_incomplete_type_diagnostic
7322 (location_of (postfix_expression),
7323 postfix_expression, scope, kind);
7324 if (processing_template_decl)
7326 dependent_p = true;
7327 scope = TREE_TYPE (postfix_expression) = NULL_TREE;
7332 if (!dependent_p)
7334 /* Let the name lookup machinery know that we are processing a
7335 class member access expression. */
7336 parser->context->object_type = scope;
7337 /* If something went wrong, we want to be able to discern that case,
7338 as opposed to the case where there was no SCOPE due to the type
7339 of expression being dependent. */
7340 if (!scope)
7341 scope = error_mark_node;
7342 /* If the SCOPE was erroneous, make the various semantic analysis
7343 functions exit quickly -- and without issuing additional error
7344 messages. */
7345 if (scope == error_mark_node)
7346 postfix_expression = error_mark_node;
7350 if (dependent_p)
7351 /* Tell cp_parser_lookup_name that there was an object, even though it's
7352 type-dependent. */
7353 parser->context->object_type = unknown_type_node;
7355 /* Assume this expression is not a pseudo-destructor access. */
7356 pseudo_destructor_p = false;
7358 /* If the SCOPE is a scalar type, then, if this is a valid program,
7359 we must be looking at a pseudo-destructor-name. If POSTFIX_EXPRESSION
7360 is type dependent, it can be pseudo-destructor-name or something else.
7361 Try to parse it as pseudo-destructor-name first. */
7362 if ((scope && SCALAR_TYPE_P (scope)) || dependent_p)
7364 tree s;
7365 tree type;
7367 cp_parser_parse_tentatively (parser);
7368 /* Parse the pseudo-destructor-name. */
7369 s = NULL_TREE;
7370 cp_parser_pseudo_destructor_name (parser, postfix_expression,
7371 &s, &type);
7372 if (dependent_p
7373 && (cp_parser_error_occurred (parser)
7374 || !SCALAR_TYPE_P (type)))
7375 cp_parser_abort_tentative_parse (parser);
7376 else if (cp_parser_parse_definitely (parser))
7378 pseudo_destructor_p = true;
7379 postfix_expression
7380 = finish_pseudo_destructor_expr (postfix_expression,
7381 s, type, location);
7385 if (!pseudo_destructor_p)
7387 /* If the SCOPE is not a scalar type, we are looking at an
7388 ordinary class member access expression, rather than a
7389 pseudo-destructor-name. */
7390 bool template_p;
7391 cp_token *token = cp_lexer_peek_token (parser->lexer);
7392 /* Parse the id-expression. */
7393 name = (cp_parser_id_expression
7394 (parser,
7395 cp_parser_optional_template_keyword (parser),
7396 /*check_dependency_p=*/true,
7397 &template_p,
7398 /*declarator_p=*/false,
7399 /*optional_p=*/false));
7400 /* In general, build a SCOPE_REF if the member name is qualified.
7401 However, if the name was not dependent and has already been
7402 resolved; there is no need to build the SCOPE_REF. For example;
7404 struct X { void f(); };
7405 template <typename T> void f(T* t) { t->X::f(); }
7407 Even though "t" is dependent, "X::f" is not and has been resolved
7408 to a BASELINK; there is no need to include scope information. */
7410 /* But we do need to remember that there was an explicit scope for
7411 virtual function calls. */
7412 if (parser->scope)
7413 *idk = CP_ID_KIND_QUALIFIED;
7415 /* If the name is a template-id that names a type, we will get a
7416 TYPE_DECL here. That is invalid code. */
7417 if (TREE_CODE (name) == TYPE_DECL)
7419 error_at (token->location, "invalid use of %qD", name);
7420 postfix_expression = error_mark_node;
7422 else
7424 if (name != error_mark_node && !BASELINK_P (name) && parser->scope)
7426 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
7428 error_at (token->location, "%<%D::%D%> is not a class member",
7429 parser->scope, name);
7430 postfix_expression = error_mark_node;
7432 else
7433 name = build_qualified_name (/*type=*/NULL_TREE,
7434 parser->scope,
7435 name,
7436 template_p);
7437 parser->scope = NULL_TREE;
7438 parser->qualifying_scope = NULL_TREE;
7439 parser->object_scope = NULL_TREE;
7441 if (parser->scope && name && BASELINK_P (name))
7442 adjust_result_of_qualified_name_lookup
7443 (name, parser->scope, scope);
7444 postfix_expression
7445 = finish_class_member_access_expr (postfix_expression, name,
7446 template_p,
7447 tf_warning_or_error);
7448 /* Build a location e.g.:
7449 ptr->access_expr
7450 ~~~^~~~~~~~~~~~~
7451 where the caret is at the deref token, ranging from
7452 the start of postfix_expression to the end of the access expr. */
7453 location_t end_loc
7454 = get_finish (cp_lexer_previous_token (parser->lexer)->location);
7455 location_t combined_loc
7456 = make_location (input_location, start_loc, end_loc);
7457 protected_set_expr_location (postfix_expression, combined_loc);
7461 /* We no longer need to look up names in the scope of the object on
7462 the left-hand side of the `.' or `->' operator. */
7463 parser->context->object_type = NULL_TREE;
7465 /* Outside of offsetof, these operators may not appear in
7466 constant-expressions. */
7467 if (!for_offsetof
7468 && (cp_parser_non_integral_constant_expression
7469 (parser, token_type == CPP_DEREF ? NIC_ARROW : NIC_POINT)))
7470 postfix_expression = error_mark_node;
7472 return postfix_expression;
7475 /* Parse a parenthesized expression-list.
7477 expression-list:
7478 assignment-expression
7479 expression-list, assignment-expression
7481 attribute-list:
7482 expression-list
7483 identifier
7484 identifier, expression-list
7486 CAST_P is true if this expression is the target of a cast.
7488 ALLOW_EXPANSION_P is true if this expression allows expansion of an
7489 argument pack.
7491 Returns a vector of trees. Each element is a representation of an
7492 assignment-expression. NULL is returned if the ( and or ) are
7493 missing. An empty, but allocated, vector is returned on no
7494 expressions. The parentheses are eaten. IS_ATTRIBUTE_LIST is id_attr
7495 if we are parsing an attribute list for an attribute that wants a
7496 plain identifier argument, normal_attr for an attribute that wants
7497 an expression, or non_attr if we aren't parsing an attribute list. If
7498 NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P indicates whether or
7499 not all of the expressions in the list were constant.
7500 If CLOSE_PAREN_LOC is non-NULL, and no errors occur, then *CLOSE_PAREN_LOC
7501 will be written to with the location of the closing parenthesis. If
7502 an error occurs, it may or may not be written to. */
7504 static vec<tree, va_gc> *
7505 cp_parser_parenthesized_expression_list (cp_parser* parser,
7506 int is_attribute_list,
7507 bool cast_p,
7508 bool allow_expansion_p,
7509 bool *non_constant_p,
7510 location_t *close_paren_loc)
7512 vec<tree, va_gc> *expression_list;
7513 bool fold_expr_p = is_attribute_list != non_attr;
7514 tree identifier = NULL_TREE;
7515 bool saved_greater_than_is_operator_p;
7517 /* Assume all the expressions will be constant. */
7518 if (non_constant_p)
7519 *non_constant_p = false;
7521 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
7522 return NULL;
7524 expression_list = make_tree_vector ();
7526 /* Within a parenthesized expression, a `>' token is always
7527 the greater-than operator. */
7528 saved_greater_than_is_operator_p
7529 = parser->greater_than_is_operator_p;
7530 parser->greater_than_is_operator_p = true;
7532 /* Consume expressions until there are no more. */
7533 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
7534 while (true)
7536 tree expr;
7538 /* At the beginning of attribute lists, check to see if the
7539 next token is an identifier. */
7540 if (is_attribute_list == id_attr
7541 && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
7543 cp_token *token;
7545 /* Consume the identifier. */
7546 token = cp_lexer_consume_token (parser->lexer);
7547 /* Save the identifier. */
7548 identifier = token->u.value;
7550 else
7552 bool expr_non_constant_p;
7554 /* Parse the next assignment-expression. */
7555 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7557 /* A braced-init-list. */
7558 cp_lexer_set_source_position (parser->lexer);
7559 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
7560 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
7561 if (non_constant_p && expr_non_constant_p)
7562 *non_constant_p = true;
7564 else if (non_constant_p)
7566 expr = (cp_parser_constant_expression
7567 (parser, /*allow_non_constant_p=*/true,
7568 &expr_non_constant_p));
7569 if (expr_non_constant_p)
7570 *non_constant_p = true;
7572 else
7573 expr = cp_parser_assignment_expression (parser, /*pidk=*/NULL,
7574 cast_p);
7576 if (fold_expr_p)
7577 expr = instantiate_non_dependent_expr (expr);
7579 /* If we have an ellipsis, then this is an expression
7580 expansion. */
7581 if (allow_expansion_p
7582 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
7584 /* Consume the `...'. */
7585 cp_lexer_consume_token (parser->lexer);
7587 /* Build the argument pack. */
7588 expr = make_pack_expansion (expr);
7591 /* Add it to the list. We add error_mark_node
7592 expressions to the list, so that we can still tell if
7593 the correct form for a parenthesized expression-list
7594 is found. That gives better errors. */
7595 vec_safe_push (expression_list, expr);
7597 if (expr == error_mark_node)
7598 goto skip_comma;
7601 /* After the first item, attribute lists look the same as
7602 expression lists. */
7603 is_attribute_list = non_attr;
7605 get_comma:;
7606 /* If the next token isn't a `,', then we are done. */
7607 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
7608 break;
7610 /* Otherwise, consume the `,' and keep going. */
7611 cp_lexer_consume_token (parser->lexer);
7614 if (close_paren_loc)
7615 *close_paren_loc = cp_lexer_peek_token (parser->lexer)->location;
7617 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
7619 int ending;
7621 skip_comma:;
7622 /* We try and resync to an unnested comma, as that will give the
7623 user better diagnostics. */
7624 ending = cp_parser_skip_to_closing_parenthesis (parser,
7625 /*recovering=*/true,
7626 /*or_comma=*/true,
7627 /*consume_paren=*/true);
7628 if (ending < 0)
7629 goto get_comma;
7630 if (!ending)
7632 parser->greater_than_is_operator_p
7633 = saved_greater_than_is_operator_p;
7634 return NULL;
7638 parser->greater_than_is_operator_p
7639 = saved_greater_than_is_operator_p;
7641 if (identifier)
7642 vec_safe_insert (expression_list, 0, identifier);
7644 return expression_list;
7647 /* Parse a pseudo-destructor-name.
7649 pseudo-destructor-name:
7650 :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
7651 :: [opt] nested-name-specifier template template-id :: ~ type-name
7652 :: [opt] nested-name-specifier [opt] ~ type-name
7654 If either of the first two productions is used, sets *SCOPE to the
7655 TYPE specified before the final `::'. Otherwise, *SCOPE is set to
7656 NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name,
7657 or ERROR_MARK_NODE if the parse fails. */
7659 static void
7660 cp_parser_pseudo_destructor_name (cp_parser* parser,
7661 tree object,
7662 tree* scope,
7663 tree* type)
7665 bool nested_name_specifier_p;
7667 /* Handle ~auto. */
7668 if (cp_lexer_next_token_is (parser->lexer, CPP_COMPL)
7669 && cp_lexer_nth_token_is_keyword (parser->lexer, 2, RID_AUTO)
7670 && !type_dependent_expression_p (object))
7672 if (cxx_dialect < cxx14)
7673 pedwarn (input_location, 0,
7674 "%<~auto%> only available with "
7675 "-std=c++14 or -std=gnu++14");
7676 cp_lexer_consume_token (parser->lexer);
7677 cp_lexer_consume_token (parser->lexer);
7678 *scope = NULL_TREE;
7679 *type = TREE_TYPE (object);
7680 return;
7683 /* Assume that things will not work out. */
7684 *type = error_mark_node;
7686 /* Look for the optional `::' operator. */
7687 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
7688 /* Look for the optional nested-name-specifier. */
7689 nested_name_specifier_p
7690 = (cp_parser_nested_name_specifier_opt (parser,
7691 /*typename_keyword_p=*/false,
7692 /*check_dependency_p=*/true,
7693 /*type_p=*/false,
7694 /*is_declaration=*/false)
7695 != NULL_TREE);
7696 /* Now, if we saw a nested-name-specifier, we might be doing the
7697 second production. */
7698 if (nested_name_specifier_p
7699 && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
7701 /* Consume the `template' keyword. */
7702 cp_lexer_consume_token (parser->lexer);
7703 /* Parse the template-id. */
7704 cp_parser_template_id (parser,
7705 /*template_keyword_p=*/true,
7706 /*check_dependency_p=*/false,
7707 class_type,
7708 /*is_declaration=*/true);
7709 /* Look for the `::' token. */
7710 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
7712 /* If the next token is not a `~', then there might be some
7713 additional qualification. */
7714 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
7716 /* At this point, we're looking for "type-name :: ~". The type-name
7717 must not be a class-name, since this is a pseudo-destructor. So,
7718 it must be either an enum-name, or a typedef-name -- both of which
7719 are just identifiers. So, we peek ahead to check that the "::"
7720 and "~" tokens are present; if they are not, then we can avoid
7721 calling type_name. */
7722 if (cp_lexer_peek_token (parser->lexer)->type != CPP_NAME
7723 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE
7724 || cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_COMPL)
7726 cp_parser_error (parser, "non-scalar type");
7727 return;
7730 /* Look for the type-name. */
7731 *scope = TREE_TYPE (cp_parser_nonclass_name (parser));
7732 if (*scope == error_mark_node)
7733 return;
7735 /* Look for the `::' token. */
7736 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
7738 else
7739 *scope = NULL_TREE;
7741 /* Look for the `~'. */
7742 cp_parser_require (parser, CPP_COMPL, RT_COMPL);
7744 /* Once we see the ~, this has to be a pseudo-destructor. */
7745 if (!processing_template_decl && !cp_parser_error_occurred (parser))
7746 cp_parser_commit_to_topmost_tentative_parse (parser);
7748 /* Look for the type-name again. We are not responsible for
7749 checking that it matches the first type-name. */
7750 *type = TREE_TYPE (cp_parser_nonclass_name (parser));
7753 /* Parse a unary-expression.
7755 unary-expression:
7756 postfix-expression
7757 ++ cast-expression
7758 -- cast-expression
7759 unary-operator cast-expression
7760 sizeof unary-expression
7761 sizeof ( type-id )
7762 alignof ( type-id ) [C++0x]
7763 new-expression
7764 delete-expression
7766 GNU Extensions:
7768 unary-expression:
7769 __extension__ cast-expression
7770 __alignof__ unary-expression
7771 __alignof__ ( type-id )
7772 alignof unary-expression [C++0x]
7773 __real__ cast-expression
7774 __imag__ cast-expression
7775 && identifier
7776 sizeof ( type-id ) { initializer-list , [opt] }
7777 alignof ( type-id ) { initializer-list , [opt] } [C++0x]
7778 __alignof__ ( type-id ) { initializer-list , [opt] }
7780 ADDRESS_P is true iff the unary-expression is appearing as the
7781 operand of the `&' operator. CAST_P is true if this expression is
7782 the target of a cast.
7784 Returns a representation of the expression. */
7786 static cp_expr
7787 cp_parser_unary_expression (cp_parser *parser, cp_id_kind * pidk,
7788 bool address_p, bool cast_p, bool decltype_p)
7790 cp_token *token;
7791 enum tree_code unary_operator;
7793 /* Peek at the next token. */
7794 token = cp_lexer_peek_token (parser->lexer);
7795 /* Some keywords give away the kind of expression. */
7796 if (token->type == CPP_KEYWORD)
7798 enum rid keyword = token->keyword;
7800 switch (keyword)
7802 case RID_ALIGNOF:
7803 case RID_SIZEOF:
7805 tree operand, ret;
7806 enum tree_code op;
7807 location_t first_loc;
7809 op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
7810 /* Consume the token. */
7811 cp_lexer_consume_token (parser->lexer);
7812 first_loc = cp_lexer_peek_token (parser->lexer)->location;
7813 /* Parse the operand. */
7814 operand = cp_parser_sizeof_operand (parser, keyword);
7816 if (TYPE_P (operand))
7817 ret = cxx_sizeof_or_alignof_type (operand, op, true);
7818 else
7820 /* ISO C++ defines alignof only with types, not with
7821 expressions. So pedwarn if alignof is used with a non-
7822 type expression. However, __alignof__ is ok. */
7823 if (!strcmp (IDENTIFIER_POINTER (token->u.value), "alignof"))
7824 pedwarn (token->location, OPT_Wpedantic,
7825 "ISO C++ does not allow %<alignof%> "
7826 "with a non-type");
7828 ret = cxx_sizeof_or_alignof_expr (operand, op, true);
7830 /* For SIZEOF_EXPR, just issue diagnostics, but keep
7831 SIZEOF_EXPR with the original operand. */
7832 if (op == SIZEOF_EXPR && ret != error_mark_node)
7834 if (TREE_CODE (ret) != SIZEOF_EXPR || TYPE_P (operand))
7836 if (!processing_template_decl && TYPE_P (operand))
7838 ret = build_min (SIZEOF_EXPR, size_type_node,
7839 build1 (NOP_EXPR, operand,
7840 error_mark_node));
7841 SIZEOF_EXPR_TYPE_P (ret) = 1;
7843 else
7844 ret = build_min (SIZEOF_EXPR, size_type_node, operand);
7845 TREE_SIDE_EFFECTS (ret) = 0;
7846 TREE_READONLY (ret) = 1;
7848 SET_EXPR_LOCATION (ret, first_loc);
7850 return ret;
7853 case RID_NEW:
7854 return cp_parser_new_expression (parser);
7856 case RID_DELETE:
7857 return cp_parser_delete_expression (parser);
7859 case RID_EXTENSION:
7861 /* The saved value of the PEDANTIC flag. */
7862 int saved_pedantic;
7863 tree expr;
7865 /* Save away the PEDANTIC flag. */
7866 cp_parser_extension_opt (parser, &saved_pedantic);
7867 /* Parse the cast-expression. */
7868 expr = cp_parser_simple_cast_expression (parser);
7869 /* Restore the PEDANTIC flag. */
7870 pedantic = saved_pedantic;
7872 return expr;
7875 case RID_REALPART:
7876 case RID_IMAGPART:
7878 tree expression;
7880 /* Consume the `__real__' or `__imag__' token. */
7881 cp_lexer_consume_token (parser->lexer);
7882 /* Parse the cast-expression. */
7883 expression = cp_parser_simple_cast_expression (parser);
7884 /* Create the complete representation. */
7885 return build_x_unary_op (token->location,
7886 (keyword == RID_REALPART
7887 ? REALPART_EXPR : IMAGPART_EXPR),
7888 expression,
7889 tf_warning_or_error);
7891 break;
7893 case RID_TRANSACTION_ATOMIC:
7894 case RID_TRANSACTION_RELAXED:
7895 return cp_parser_transaction_expression (parser, keyword);
7897 case RID_NOEXCEPT:
7899 tree expr;
7900 const char *saved_message;
7901 bool saved_integral_constant_expression_p;
7902 bool saved_non_integral_constant_expression_p;
7903 bool saved_greater_than_is_operator_p;
7905 cp_lexer_consume_token (parser->lexer);
7906 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
7908 saved_message = parser->type_definition_forbidden_message;
7909 parser->type_definition_forbidden_message
7910 = G_("types may not be defined in %<noexcept%> expressions");
7912 saved_integral_constant_expression_p
7913 = parser->integral_constant_expression_p;
7914 saved_non_integral_constant_expression_p
7915 = parser->non_integral_constant_expression_p;
7916 parser->integral_constant_expression_p = false;
7918 saved_greater_than_is_operator_p
7919 = parser->greater_than_is_operator_p;
7920 parser->greater_than_is_operator_p = true;
7922 ++cp_unevaluated_operand;
7923 ++c_inhibit_evaluation_warnings;
7924 ++cp_noexcept_operand;
7925 expr = cp_parser_expression (parser);
7926 --cp_noexcept_operand;
7927 --c_inhibit_evaluation_warnings;
7928 --cp_unevaluated_operand;
7930 parser->greater_than_is_operator_p
7931 = saved_greater_than_is_operator_p;
7933 parser->integral_constant_expression_p
7934 = saved_integral_constant_expression_p;
7935 parser->non_integral_constant_expression_p
7936 = saved_non_integral_constant_expression_p;
7938 parser->type_definition_forbidden_message = saved_message;
7940 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
7941 return finish_noexcept_expr (expr, tf_warning_or_error);
7944 default:
7945 break;
7949 /* Look for the `:: new' and `:: delete', which also signal the
7950 beginning of a new-expression, or delete-expression,
7951 respectively. If the next token is `::', then it might be one of
7952 these. */
7953 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
7955 enum rid keyword;
7957 /* See if the token after the `::' is one of the keywords in
7958 which we're interested. */
7959 keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
7960 /* If it's `new', we have a new-expression. */
7961 if (keyword == RID_NEW)
7962 return cp_parser_new_expression (parser);
7963 /* Similarly, for `delete'. */
7964 else if (keyword == RID_DELETE)
7965 return cp_parser_delete_expression (parser);
7968 /* Look for a unary operator. */
7969 unary_operator = cp_parser_unary_operator (token);
7970 /* The `++' and `--' operators can be handled similarly, even though
7971 they are not technically unary-operators in the grammar. */
7972 if (unary_operator == ERROR_MARK)
7974 if (token->type == CPP_PLUS_PLUS)
7975 unary_operator = PREINCREMENT_EXPR;
7976 else if (token->type == CPP_MINUS_MINUS)
7977 unary_operator = PREDECREMENT_EXPR;
7978 /* Handle the GNU address-of-label extension. */
7979 else if (cp_parser_allow_gnu_extensions_p (parser)
7980 && token->type == CPP_AND_AND)
7982 tree identifier;
7983 tree expression;
7984 location_t start_loc = token->location;
7986 /* Consume the '&&' token. */
7987 cp_lexer_consume_token (parser->lexer);
7988 /* Look for the identifier. */
7989 location_t finish_loc
7990 = get_finish (cp_lexer_peek_token (parser->lexer)->location);
7991 identifier = cp_parser_identifier (parser);
7992 /* Construct a location of the form:
7993 &&label
7994 ^~~~~~~
7995 with caret==start at the "&&", finish at the end of the label. */
7996 location_t combined_loc
7997 = make_location (start_loc, start_loc, finish_loc);
7998 /* Create an expression representing the address. */
7999 expression = finish_label_address_expr (identifier, combined_loc);
8000 if (cp_parser_non_integral_constant_expression (parser,
8001 NIC_ADDR_LABEL))
8002 expression = error_mark_node;
8003 return expression;
8006 if (unary_operator != ERROR_MARK)
8008 cp_expr cast_expression;
8009 cp_expr expression = error_mark_node;
8010 non_integral_constant non_constant_p = NIC_NONE;
8011 location_t loc = token->location;
8012 tsubst_flags_t complain = complain_flags (decltype_p);
8014 /* Consume the operator token. */
8015 token = cp_lexer_consume_token (parser->lexer);
8016 enum cpp_ttype op_ttype = cp_lexer_peek_token (parser->lexer)->type;
8018 /* Parse the cast-expression. */
8019 cast_expression
8020 = cp_parser_cast_expression (parser,
8021 unary_operator == ADDR_EXPR,
8022 /*cast_p=*/false,
8023 /*decltype*/false,
8024 pidk);
8026 /* Make a location:
8027 OP_TOKEN CAST_EXPRESSION
8028 ^~~~~~~~~~~~~~~~~~~~~~~~~
8029 with start==caret at the operator token, and
8030 extending to the end of the cast_expression. */
8031 loc = make_location (loc, loc, cast_expression.get_finish ());
8033 /* Now, build an appropriate representation. */
8034 switch (unary_operator)
8036 case INDIRECT_REF:
8037 non_constant_p = NIC_STAR;
8038 expression = build_x_indirect_ref (loc, cast_expression,
8039 RO_UNARY_STAR,
8040 complain);
8041 /* TODO: build_x_indirect_ref does not always honor the
8042 location, so ensure it is set. */
8043 expression.set_location (loc);
8044 break;
8046 case ADDR_EXPR:
8047 non_constant_p = NIC_ADDR;
8048 /* Fall through. */
8049 case BIT_NOT_EXPR:
8050 expression = build_x_unary_op (loc, unary_operator,
8051 cast_expression,
8052 complain);
8053 /* TODO: build_x_unary_op does not always honor the location,
8054 so ensure it is set. */
8055 expression.set_location (loc);
8056 break;
8058 case PREINCREMENT_EXPR:
8059 case PREDECREMENT_EXPR:
8060 non_constant_p = unary_operator == PREINCREMENT_EXPR
8061 ? NIC_PREINCREMENT : NIC_PREDECREMENT;
8062 /* Fall through. */
8063 case NEGATE_EXPR:
8064 /* Immediately fold negation of a constant, unless the constant is 0
8065 (since -0 == 0) or it would overflow. */
8066 if (unary_operator == NEGATE_EXPR && op_ttype == CPP_NUMBER
8067 && CONSTANT_CLASS_P (cast_expression)
8068 && !integer_zerop (cast_expression)
8069 && !TREE_OVERFLOW (cast_expression))
8071 tree folded = fold_build1 (unary_operator,
8072 TREE_TYPE (cast_expression),
8073 cast_expression);
8074 if (CONSTANT_CLASS_P (folded) && !TREE_OVERFLOW (folded))
8076 expression = cp_expr (folded, loc);
8077 break;
8080 /* Fall through. */
8081 case UNARY_PLUS_EXPR:
8082 case TRUTH_NOT_EXPR:
8083 expression = finish_unary_op_expr (loc, unary_operator,
8084 cast_expression, complain);
8085 break;
8087 default:
8088 gcc_unreachable ();
8091 if (non_constant_p != NIC_NONE
8092 && cp_parser_non_integral_constant_expression (parser,
8093 non_constant_p))
8094 expression = error_mark_node;
8096 return expression;
8099 return cp_parser_postfix_expression (parser, address_p, cast_p,
8100 /*member_access_only_p=*/false,
8101 decltype_p,
8102 pidk);
8105 /* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a
8106 unary-operator, the corresponding tree code is returned. */
8108 static enum tree_code
8109 cp_parser_unary_operator (cp_token* token)
8111 switch (token->type)
8113 case CPP_MULT:
8114 return INDIRECT_REF;
8116 case CPP_AND:
8117 return ADDR_EXPR;
8119 case CPP_PLUS:
8120 return UNARY_PLUS_EXPR;
8122 case CPP_MINUS:
8123 return NEGATE_EXPR;
8125 case CPP_NOT:
8126 return TRUTH_NOT_EXPR;
8128 case CPP_COMPL:
8129 return BIT_NOT_EXPR;
8131 default:
8132 return ERROR_MARK;
8136 /* Parse a new-expression.
8138 new-expression:
8139 :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
8140 :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
8142 Returns a representation of the expression. */
8144 static tree
8145 cp_parser_new_expression (cp_parser* parser)
8147 bool global_scope_p;
8148 vec<tree, va_gc> *placement;
8149 tree type;
8150 vec<tree, va_gc> *initializer;
8151 tree nelts = NULL_TREE;
8152 tree ret;
8154 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
8156 /* Look for the optional `::' operator. */
8157 global_scope_p
8158 = (cp_parser_global_scope_opt (parser,
8159 /*current_scope_valid_p=*/false)
8160 != NULL_TREE);
8161 /* Look for the `new' operator. */
8162 cp_parser_require_keyword (parser, RID_NEW, RT_NEW);
8163 /* There's no easy way to tell a new-placement from the
8164 `( type-id )' construct. */
8165 cp_parser_parse_tentatively (parser);
8166 /* Look for a new-placement. */
8167 placement = cp_parser_new_placement (parser);
8168 /* If that didn't work out, there's no new-placement. */
8169 if (!cp_parser_parse_definitely (parser))
8171 if (placement != NULL)
8172 release_tree_vector (placement);
8173 placement = NULL;
8176 /* If the next token is a `(', then we have a parenthesized
8177 type-id. */
8178 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
8180 cp_token *token;
8181 const char *saved_message = parser->type_definition_forbidden_message;
8183 /* Consume the `('. */
8184 cp_lexer_consume_token (parser->lexer);
8186 /* Parse the type-id. */
8187 parser->type_definition_forbidden_message
8188 = G_("types may not be defined in a new-expression");
8190 type_id_in_expr_sentinel s (parser);
8191 type = cp_parser_type_id (parser);
8193 parser->type_definition_forbidden_message = saved_message;
8195 /* Look for the closing `)'. */
8196 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
8197 token = cp_lexer_peek_token (parser->lexer);
8198 /* There should not be a direct-new-declarator in this production,
8199 but GCC used to allowed this, so we check and emit a sensible error
8200 message for this case. */
8201 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
8203 error_at (token->location,
8204 "array bound forbidden after parenthesized type-id");
8205 inform (token->location,
8206 "try removing the parentheses around the type-id");
8207 cp_parser_direct_new_declarator (parser);
8210 /* Otherwise, there must be a new-type-id. */
8211 else
8212 type = cp_parser_new_type_id (parser, &nelts);
8214 /* If the next token is a `(' or '{', then we have a new-initializer. */
8215 cp_token *token = cp_lexer_peek_token (parser->lexer);
8216 if (token->type == CPP_OPEN_PAREN
8217 || token->type == CPP_OPEN_BRACE)
8218 initializer = cp_parser_new_initializer (parser);
8219 else
8220 initializer = NULL;
8222 /* A new-expression may not appear in an integral constant
8223 expression. */
8224 if (cp_parser_non_integral_constant_expression (parser, NIC_NEW))
8225 ret = error_mark_node;
8226 /* 5.3.4/2: "If the auto type-specifier appears in the type-specifier-seq
8227 of a new-type-id or type-id of a new-expression, the new-expression shall
8228 contain a new-initializer of the form ( assignment-expression )".
8229 Additionally, consistently with the spirit of DR 1467, we want to accept
8230 'new auto { 2 }' too. */
8231 else if ((ret = type_uses_auto (type))
8232 && !CLASS_PLACEHOLDER_TEMPLATE (ret)
8233 && (vec_safe_length (initializer) != 1
8234 || (BRACE_ENCLOSED_INITIALIZER_P ((*initializer)[0])
8235 && CONSTRUCTOR_NELTS ((*initializer)[0]) != 1)))
8237 error_at (token->location,
8238 "initialization of new-expression for type %<auto%> "
8239 "requires exactly one element");
8240 ret = error_mark_node;
8242 else
8244 /* Construct a location e.g.:
8245 ptr = new int[100]
8246 ^~~~~~~~~~~~
8247 with caret == start at the start of the "new" token, and the end
8248 at the end of the final token we consumed. */
8249 cp_token *end_tok = cp_lexer_previous_token (parser->lexer);
8250 location_t end_loc = get_finish (end_tok->location);
8251 location_t combined_loc = make_location (start_loc, start_loc, end_loc);
8253 /* Create a representation of the new-expression. */
8254 ret = build_new (&placement, type, nelts, &initializer, global_scope_p,
8255 tf_warning_or_error);
8256 protected_set_expr_location (ret, combined_loc);
8259 if (placement != NULL)
8260 release_tree_vector (placement);
8261 if (initializer != NULL)
8262 release_tree_vector (initializer);
8264 return ret;
8267 /* Parse a new-placement.
8269 new-placement:
8270 ( expression-list )
8272 Returns the same representation as for an expression-list. */
8274 static vec<tree, va_gc> *
8275 cp_parser_new_placement (cp_parser* parser)
8277 vec<tree, va_gc> *expression_list;
8279 /* Parse the expression-list. */
8280 expression_list = (cp_parser_parenthesized_expression_list
8281 (parser, non_attr, /*cast_p=*/false,
8282 /*allow_expansion_p=*/true,
8283 /*non_constant_p=*/NULL));
8285 if (expression_list && expression_list->is_empty ())
8286 error ("expected expression-list or type-id");
8288 return expression_list;
8291 /* Parse a new-type-id.
8293 new-type-id:
8294 type-specifier-seq new-declarator [opt]
8296 Returns the TYPE allocated. If the new-type-id indicates an array
8297 type, *NELTS is set to the number of elements in the last array
8298 bound; the TYPE will not include the last array bound. */
8300 static tree
8301 cp_parser_new_type_id (cp_parser* parser, tree *nelts)
8303 cp_decl_specifier_seq type_specifier_seq;
8304 cp_declarator *new_declarator;
8305 cp_declarator *declarator;
8306 cp_declarator *outer_declarator;
8307 const char *saved_message;
8309 /* The type-specifier sequence must not contain type definitions.
8310 (It cannot contain declarations of new types either, but if they
8311 are not definitions we will catch that because they are not
8312 complete.) */
8313 saved_message = parser->type_definition_forbidden_message;
8314 parser->type_definition_forbidden_message
8315 = G_("types may not be defined in a new-type-id");
8316 /* Parse the type-specifier-seq. */
8317 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
8318 /*is_trailing_return=*/false,
8319 &type_specifier_seq);
8320 /* Restore the old message. */
8321 parser->type_definition_forbidden_message = saved_message;
8323 if (type_specifier_seq.type == error_mark_node)
8324 return error_mark_node;
8326 /* Parse the new-declarator. */
8327 new_declarator = cp_parser_new_declarator_opt (parser);
8329 /* Determine the number of elements in the last array dimension, if
8330 any. */
8331 *nelts = NULL_TREE;
8332 /* Skip down to the last array dimension. */
8333 declarator = new_declarator;
8334 outer_declarator = NULL;
8335 while (declarator && (declarator->kind == cdk_pointer
8336 || declarator->kind == cdk_ptrmem))
8338 outer_declarator = declarator;
8339 declarator = declarator->declarator;
8341 while (declarator
8342 && declarator->kind == cdk_array
8343 && declarator->declarator
8344 && declarator->declarator->kind == cdk_array)
8346 outer_declarator = declarator;
8347 declarator = declarator->declarator;
8350 if (declarator && declarator->kind == cdk_array)
8352 *nelts = declarator->u.array.bounds;
8353 if (*nelts == error_mark_node)
8354 *nelts = integer_one_node;
8356 if (outer_declarator)
8357 outer_declarator->declarator = declarator->declarator;
8358 else
8359 new_declarator = NULL;
8362 return groktypename (&type_specifier_seq, new_declarator, false);
8365 /* Parse an (optional) new-declarator.
8367 new-declarator:
8368 ptr-operator new-declarator [opt]
8369 direct-new-declarator
8371 Returns the declarator. */
8373 static cp_declarator *
8374 cp_parser_new_declarator_opt (cp_parser* parser)
8376 enum tree_code code;
8377 tree type, std_attributes = NULL_TREE;
8378 cp_cv_quals cv_quals;
8380 /* We don't know if there's a ptr-operator next, or not. */
8381 cp_parser_parse_tentatively (parser);
8382 /* Look for a ptr-operator. */
8383 code = cp_parser_ptr_operator (parser, &type, &cv_quals, &std_attributes);
8384 /* If that worked, look for more new-declarators. */
8385 if (cp_parser_parse_definitely (parser))
8387 cp_declarator *declarator;
8389 /* Parse another optional declarator. */
8390 declarator = cp_parser_new_declarator_opt (parser);
8392 declarator = cp_parser_make_indirect_declarator
8393 (code, type, cv_quals, declarator, std_attributes);
8395 return declarator;
8398 /* If the next token is a `[', there is a direct-new-declarator. */
8399 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
8400 return cp_parser_direct_new_declarator (parser);
8402 return NULL;
8405 /* Parse a direct-new-declarator.
8407 direct-new-declarator:
8408 [ expression ]
8409 direct-new-declarator [constant-expression]
8413 static cp_declarator *
8414 cp_parser_direct_new_declarator (cp_parser* parser)
8416 cp_declarator *declarator = NULL;
8418 while (true)
8420 tree expression;
8421 cp_token *token;
8423 /* Look for the opening `['. */
8424 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
8426 token = cp_lexer_peek_token (parser->lexer);
8427 expression = cp_parser_expression (parser);
8428 /* The standard requires that the expression have integral
8429 type. DR 74 adds enumeration types. We believe that the
8430 real intent is that these expressions be handled like the
8431 expression in a `switch' condition, which also allows
8432 classes with a single conversion to integral or
8433 enumeration type. */
8434 if (!processing_template_decl)
8436 expression
8437 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
8438 expression,
8439 /*complain=*/true);
8440 if (!expression)
8442 error_at (token->location,
8443 "expression in new-declarator must have integral "
8444 "or enumeration type");
8445 expression = error_mark_node;
8449 /* Look for the closing `]'. */
8450 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
8452 /* Add this bound to the declarator. */
8453 declarator = make_array_declarator (declarator, expression);
8455 /* If the next token is not a `[', then there are no more
8456 bounds. */
8457 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
8458 break;
8461 return declarator;
8464 /* Parse a new-initializer.
8466 new-initializer:
8467 ( expression-list [opt] )
8468 braced-init-list
8470 Returns a representation of the expression-list. */
8472 static vec<tree, va_gc> *
8473 cp_parser_new_initializer (cp_parser* parser)
8475 vec<tree, va_gc> *expression_list;
8477 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
8479 tree t;
8480 bool expr_non_constant_p;
8481 cp_lexer_set_source_position (parser->lexer);
8482 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
8483 t = cp_parser_braced_list (parser, &expr_non_constant_p);
8484 CONSTRUCTOR_IS_DIRECT_INIT (t) = 1;
8485 expression_list = make_tree_vector_single (t);
8487 else
8488 expression_list = (cp_parser_parenthesized_expression_list
8489 (parser, non_attr, /*cast_p=*/false,
8490 /*allow_expansion_p=*/true,
8491 /*non_constant_p=*/NULL));
8493 return expression_list;
8496 /* Parse a delete-expression.
8498 delete-expression:
8499 :: [opt] delete cast-expression
8500 :: [opt] delete [ ] cast-expression
8502 Returns a representation of the expression. */
8504 static tree
8505 cp_parser_delete_expression (cp_parser* parser)
8507 bool global_scope_p;
8508 bool array_p;
8509 tree expression;
8511 /* Look for the optional `::' operator. */
8512 global_scope_p
8513 = (cp_parser_global_scope_opt (parser,
8514 /*current_scope_valid_p=*/false)
8515 != NULL_TREE);
8516 /* Look for the `delete' keyword. */
8517 cp_parser_require_keyword (parser, RID_DELETE, RT_DELETE);
8518 /* See if the array syntax is in use. */
8519 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
8521 /* Consume the `[' token. */
8522 cp_lexer_consume_token (parser->lexer);
8523 /* Look for the `]' token. */
8524 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
8525 /* Remember that this is the `[]' construct. */
8526 array_p = true;
8528 else
8529 array_p = false;
8531 /* Parse the cast-expression. */
8532 expression = cp_parser_simple_cast_expression (parser);
8534 /* A delete-expression may not appear in an integral constant
8535 expression. */
8536 if (cp_parser_non_integral_constant_expression (parser, NIC_DEL))
8537 return error_mark_node;
8539 return delete_sanity (expression, NULL_TREE, array_p, global_scope_p,
8540 tf_warning_or_error);
8543 /* Returns 1 if TOKEN may start a cast-expression and isn't '++', '--',
8544 neither '[' in C++11; -1 if TOKEN is '++', '--', or '[' in C++11;
8545 0 otherwise. */
8547 static int
8548 cp_parser_tokens_start_cast_expression (cp_parser *parser)
8550 cp_token *token = cp_lexer_peek_token (parser->lexer);
8551 switch (token->type)
8553 case CPP_COMMA:
8554 case CPP_SEMICOLON:
8555 case CPP_QUERY:
8556 case CPP_COLON:
8557 case CPP_CLOSE_SQUARE:
8558 case CPP_CLOSE_PAREN:
8559 case CPP_CLOSE_BRACE:
8560 case CPP_OPEN_BRACE:
8561 case CPP_DOT:
8562 case CPP_DOT_STAR:
8563 case CPP_DEREF:
8564 case CPP_DEREF_STAR:
8565 case CPP_DIV:
8566 case CPP_MOD:
8567 case CPP_LSHIFT:
8568 case CPP_RSHIFT:
8569 case CPP_LESS:
8570 case CPP_GREATER:
8571 case CPP_LESS_EQ:
8572 case CPP_GREATER_EQ:
8573 case CPP_EQ_EQ:
8574 case CPP_NOT_EQ:
8575 case CPP_EQ:
8576 case CPP_MULT_EQ:
8577 case CPP_DIV_EQ:
8578 case CPP_MOD_EQ:
8579 case CPP_PLUS_EQ:
8580 case CPP_MINUS_EQ:
8581 case CPP_RSHIFT_EQ:
8582 case CPP_LSHIFT_EQ:
8583 case CPP_AND_EQ:
8584 case CPP_XOR_EQ:
8585 case CPP_OR_EQ:
8586 case CPP_XOR:
8587 case CPP_OR:
8588 case CPP_OR_OR:
8589 case CPP_EOF:
8590 case CPP_ELLIPSIS:
8591 return 0;
8593 case CPP_OPEN_PAREN:
8594 /* In ((type ()) () the last () isn't a valid cast-expression,
8595 so the whole must be parsed as postfix-expression. */
8596 return cp_lexer_peek_nth_token (parser->lexer, 2)->type
8597 != CPP_CLOSE_PAREN;
8599 case CPP_OPEN_SQUARE:
8600 /* '[' may start a primary-expression in obj-c++ and in C++11,
8601 as a lambda-expression, eg, '(void)[]{}'. */
8602 if (cxx_dialect >= cxx11)
8603 return -1;
8604 return c_dialect_objc ();
8606 case CPP_PLUS_PLUS:
8607 case CPP_MINUS_MINUS:
8608 /* '++' and '--' may or may not start a cast-expression:
8610 struct T { void operator++(int); };
8611 void f() { (T())++; }
8615 int a;
8616 (int)++a; */
8617 return -1;
8619 default:
8620 return 1;
8624 /* Parse a cast-expression.
8626 cast-expression:
8627 unary-expression
8628 ( type-id ) cast-expression
8630 ADDRESS_P is true iff the unary-expression is appearing as the
8631 operand of the `&' operator. CAST_P is true if this expression is
8632 the target of a cast.
8634 Returns a representation of the expression. */
8636 static cp_expr
8637 cp_parser_cast_expression (cp_parser *parser, bool address_p, bool cast_p,
8638 bool decltype_p, cp_id_kind * pidk)
8640 /* If it's a `(', then we might be looking at a cast. */
8641 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
8643 tree type = NULL_TREE;
8644 cp_expr expr (NULL_TREE);
8645 int cast_expression = 0;
8646 const char *saved_message;
8648 /* There's no way to know yet whether or not this is a cast.
8649 For example, `(int (3))' is a unary-expression, while `(int)
8650 3' is a cast. So, we resort to parsing tentatively. */
8651 cp_parser_parse_tentatively (parser);
8652 /* Types may not be defined in a cast. */
8653 saved_message = parser->type_definition_forbidden_message;
8654 parser->type_definition_forbidden_message
8655 = G_("types may not be defined in casts");
8656 /* Consume the `('. */
8657 cp_token *open_paren = cp_lexer_consume_token (parser->lexer);
8658 location_t open_paren_loc = open_paren->location;
8660 /* A very tricky bit is that `(struct S) { 3 }' is a
8661 compound-literal (which we permit in C++ as an extension).
8662 But, that construct is not a cast-expression -- it is a
8663 postfix-expression. (The reason is that `(struct S) { 3 }.i'
8664 is legal; if the compound-literal were a cast-expression,
8665 you'd need an extra set of parentheses.) But, if we parse
8666 the type-id, and it happens to be a class-specifier, then we
8667 will commit to the parse at that point, because we cannot
8668 undo the action that is done when creating a new class. So,
8669 then we cannot back up and do a postfix-expression.
8671 Another tricky case is the following (c++/29234):
8673 struct S { void operator () (); };
8675 void foo ()
8677 ( S()() );
8680 As a type-id we parse the parenthesized S()() as a function
8681 returning a function, groktypename complains and we cannot
8682 back up in this case either.
8684 Therefore, we scan ahead to the closing `)', and check to see
8685 if the tokens after the `)' can start a cast-expression. Otherwise
8686 we are dealing with an unary-expression, a postfix-expression
8687 or something else.
8689 Yet another tricky case, in C++11, is the following (c++/54891):
8691 (void)[]{};
8693 The issue is that usually, besides the case of lambda-expressions,
8694 the parenthesized type-id cannot be followed by '[', and, eg, we
8695 want to parse '(C ())[2];' in parse/pr26997.C as unary-expression.
8696 Thus, if cp_parser_tokens_start_cast_expression returns -1, below
8697 we don't commit, we try a cast-expression, then an unary-expression.
8699 Save tokens so that we can put them back. */
8700 cp_lexer_save_tokens (parser->lexer);
8702 /* We may be looking at a cast-expression. */
8703 if (cp_parser_skip_to_closing_parenthesis (parser, false, false,
8704 /*consume_paren=*/true))
8705 cast_expression
8706 = cp_parser_tokens_start_cast_expression (parser);
8708 /* Roll back the tokens we skipped. */
8709 cp_lexer_rollback_tokens (parser->lexer);
8710 /* If we aren't looking at a cast-expression, simulate an error so
8711 that the call to cp_parser_error_occurred below returns true. */
8712 if (!cast_expression)
8713 cp_parser_simulate_error (parser);
8714 else
8716 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
8717 parser->in_type_id_in_expr_p = true;
8718 /* Look for the type-id. */
8719 type = cp_parser_type_id (parser);
8720 /* Look for the closing `)'. */
8721 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
8722 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
8725 /* Restore the saved message. */
8726 parser->type_definition_forbidden_message = saved_message;
8728 /* At this point this can only be either a cast or a
8729 parenthesized ctor such as `(T ())' that looks like a cast to
8730 function returning T. */
8731 if (!cp_parser_error_occurred (parser))
8733 /* Only commit if the cast-expression doesn't start with
8734 '++', '--', or '[' in C++11. */
8735 if (cast_expression > 0)
8736 cp_parser_commit_to_topmost_tentative_parse (parser);
8738 expr = cp_parser_cast_expression (parser,
8739 /*address_p=*/false,
8740 /*cast_p=*/true,
8741 /*decltype_p=*/false,
8742 pidk);
8744 if (cp_parser_parse_definitely (parser))
8746 /* Warn about old-style casts, if so requested. */
8747 if (warn_old_style_cast
8748 && !in_system_header_at (input_location)
8749 && !VOID_TYPE_P (type)
8750 && current_lang_name != lang_name_c)
8751 warning (OPT_Wold_style_cast, "use of old-style cast");
8753 /* Only type conversions to integral or enumeration types
8754 can be used in constant-expressions. */
8755 if (!cast_valid_in_integral_constant_expression_p (type)
8756 && cp_parser_non_integral_constant_expression (parser,
8757 NIC_CAST))
8758 return error_mark_node;
8760 /* Perform the cast. */
8761 /* Make a location:
8762 (TYPE) EXPR
8763 ^~~~~~~~~~~
8764 with start==caret at the open paren, extending to the
8765 end of "expr". */
8766 location_t cast_loc = make_location (open_paren_loc,
8767 open_paren_loc,
8768 expr.get_finish ());
8769 expr = build_c_cast (cast_loc, type, expr);
8770 return expr;
8773 else
8774 cp_parser_abort_tentative_parse (parser);
8777 /* If we get here, then it's not a cast, so it must be a
8778 unary-expression. */
8779 return cp_parser_unary_expression (parser, pidk, address_p,
8780 cast_p, decltype_p);
8783 /* Parse a binary expression of the general form:
8785 pm-expression:
8786 cast-expression
8787 pm-expression .* cast-expression
8788 pm-expression ->* cast-expression
8790 multiplicative-expression:
8791 pm-expression
8792 multiplicative-expression * pm-expression
8793 multiplicative-expression / pm-expression
8794 multiplicative-expression % pm-expression
8796 additive-expression:
8797 multiplicative-expression
8798 additive-expression + multiplicative-expression
8799 additive-expression - multiplicative-expression
8801 shift-expression:
8802 additive-expression
8803 shift-expression << additive-expression
8804 shift-expression >> additive-expression
8806 relational-expression:
8807 shift-expression
8808 relational-expression < shift-expression
8809 relational-expression > shift-expression
8810 relational-expression <= shift-expression
8811 relational-expression >= shift-expression
8813 GNU Extension:
8815 relational-expression:
8816 relational-expression <? shift-expression
8817 relational-expression >? shift-expression
8819 equality-expression:
8820 relational-expression
8821 equality-expression == relational-expression
8822 equality-expression != relational-expression
8824 and-expression:
8825 equality-expression
8826 and-expression & equality-expression
8828 exclusive-or-expression:
8829 and-expression
8830 exclusive-or-expression ^ and-expression
8832 inclusive-or-expression:
8833 exclusive-or-expression
8834 inclusive-or-expression | exclusive-or-expression
8836 logical-and-expression:
8837 inclusive-or-expression
8838 logical-and-expression && inclusive-or-expression
8840 logical-or-expression:
8841 logical-and-expression
8842 logical-or-expression || logical-and-expression
8844 All these are implemented with a single function like:
8846 binary-expression:
8847 simple-cast-expression
8848 binary-expression <token> binary-expression
8850 CAST_P is true if this expression is the target of a cast.
8852 The binops_by_token map is used to get the tree codes for each <token> type.
8853 binary-expressions are associated according to a precedence table. */
8855 #define TOKEN_PRECEDENCE(token) \
8856 (((token->type == CPP_GREATER \
8857 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT)) \
8858 && !parser->greater_than_is_operator_p) \
8859 ? PREC_NOT_OPERATOR \
8860 : binops_by_token[token->type].prec)
8862 static cp_expr
8863 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
8864 bool no_toplevel_fold_p,
8865 bool decltype_p,
8866 enum cp_parser_prec prec,
8867 cp_id_kind * pidk)
8869 cp_parser_expression_stack stack;
8870 cp_parser_expression_stack_entry *sp = &stack[0];
8871 cp_parser_expression_stack_entry current;
8872 cp_expr rhs;
8873 cp_token *token;
8874 enum tree_code rhs_type;
8875 enum cp_parser_prec new_prec, lookahead_prec;
8876 tree overload;
8878 /* Parse the first expression. */
8879 current.lhs_type = (cp_lexer_next_token_is (parser->lexer, CPP_NOT)
8880 ? TRUTH_NOT_EXPR : ERROR_MARK);
8881 current.lhs = cp_parser_cast_expression (parser, /*address_p=*/false,
8882 cast_p, decltype_p, pidk);
8883 current.prec = prec;
8885 if (cp_parser_error_occurred (parser))
8886 return error_mark_node;
8888 for (;;)
8890 /* Get an operator token. */
8891 token = cp_lexer_peek_token (parser->lexer);
8893 if (warn_cxx11_compat
8894 && token->type == CPP_RSHIFT
8895 && !parser->greater_than_is_operator_p)
8897 if (warning_at (token->location, OPT_Wc__11_compat,
8898 "%<>>%> operator is treated"
8899 " as two right angle brackets in C++11"))
8900 inform (token->location,
8901 "suggest parentheses around %<>>%> expression");
8904 new_prec = TOKEN_PRECEDENCE (token);
8905 if (new_prec != PREC_NOT_OPERATOR
8906 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS))
8907 /* This is a fold-expression; handle it later. */
8908 new_prec = PREC_NOT_OPERATOR;
8910 /* Popping an entry off the stack means we completed a subexpression:
8911 - either we found a token which is not an operator (`>' where it is not
8912 an operator, or prec == PREC_NOT_OPERATOR), in which case popping
8913 will happen repeatedly;
8914 - or, we found an operator which has lower priority. This is the case
8915 where the recursive descent *ascends*, as in `3 * 4 + 5' after
8916 parsing `3 * 4'. */
8917 if (new_prec <= current.prec)
8919 if (sp == stack)
8920 break;
8921 else
8922 goto pop;
8925 get_rhs:
8926 current.tree_type = binops_by_token[token->type].tree_type;
8927 current.loc = token->location;
8929 /* We used the operator token. */
8930 cp_lexer_consume_token (parser->lexer);
8932 /* For "false && x" or "true || x", x will never be executed;
8933 disable warnings while evaluating it. */
8934 if (current.tree_type == TRUTH_ANDIF_EXPR)
8935 c_inhibit_evaluation_warnings +=
8936 cp_fully_fold (current.lhs) == truthvalue_false_node;
8937 else if (current.tree_type == TRUTH_ORIF_EXPR)
8938 c_inhibit_evaluation_warnings +=
8939 cp_fully_fold (current.lhs) == truthvalue_true_node;
8941 /* Extract another operand. It may be the RHS of this expression
8942 or the LHS of a new, higher priority expression. */
8943 rhs_type = (cp_lexer_next_token_is (parser->lexer, CPP_NOT)
8944 ? TRUTH_NOT_EXPR : ERROR_MARK);
8945 rhs = cp_parser_simple_cast_expression (parser);
8947 /* Get another operator token. Look up its precedence to avoid
8948 building a useless (immediately popped) stack entry for common
8949 cases such as 3 + 4 + 5 or 3 * 4 + 5. */
8950 token = cp_lexer_peek_token (parser->lexer);
8951 lookahead_prec = TOKEN_PRECEDENCE (token);
8952 if (lookahead_prec != PREC_NOT_OPERATOR
8953 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS))
8954 lookahead_prec = PREC_NOT_OPERATOR;
8955 if (lookahead_prec > new_prec)
8957 /* ... and prepare to parse the RHS of the new, higher priority
8958 expression. Since precedence levels on the stack are
8959 monotonically increasing, we do not have to care about
8960 stack overflows. */
8961 *sp = current;
8962 ++sp;
8963 current.lhs = rhs;
8964 current.lhs_type = rhs_type;
8965 current.prec = new_prec;
8966 new_prec = lookahead_prec;
8967 goto get_rhs;
8969 pop:
8970 lookahead_prec = new_prec;
8971 /* If the stack is not empty, we have parsed into LHS the right side
8972 (`4' in the example above) of an expression we had suspended.
8973 We can use the information on the stack to recover the LHS (`3')
8974 from the stack together with the tree code (`MULT_EXPR'), and
8975 the precedence of the higher level subexpression
8976 (`PREC_ADDITIVE_EXPRESSION'). TOKEN is the CPP_PLUS token,
8977 which will be used to actually build the additive expression. */
8978 rhs = current.lhs;
8979 rhs_type = current.lhs_type;
8980 --sp;
8981 current = *sp;
8984 /* Undo the disabling of warnings done above. */
8985 if (current.tree_type == TRUTH_ANDIF_EXPR)
8986 c_inhibit_evaluation_warnings -=
8987 cp_fully_fold (current.lhs) == truthvalue_false_node;
8988 else if (current.tree_type == TRUTH_ORIF_EXPR)
8989 c_inhibit_evaluation_warnings -=
8990 cp_fully_fold (current.lhs) == truthvalue_true_node;
8992 if (warn_logical_not_paren
8993 && TREE_CODE_CLASS (current.tree_type) == tcc_comparison
8994 && current.lhs_type == TRUTH_NOT_EXPR
8995 /* Avoid warning for !!x == y. */
8996 && (TREE_CODE (current.lhs) != NE_EXPR
8997 || !integer_zerop (TREE_OPERAND (current.lhs, 1)))
8998 && (TREE_CODE (current.lhs) != TRUTH_NOT_EXPR
8999 || (TREE_CODE (TREE_OPERAND (current.lhs, 0)) != TRUTH_NOT_EXPR
9000 /* Avoid warning for !b == y where b is boolean. */
9001 && (TREE_TYPE (TREE_OPERAND (current.lhs, 0)) == NULL_TREE
9002 || (TREE_CODE (TREE_TYPE (TREE_OPERAND (current.lhs, 0)))
9003 != BOOLEAN_TYPE))))
9004 /* Avoid warning for !!b == y where b is boolean. */
9005 && (!DECL_P (current.lhs)
9006 || TREE_TYPE (current.lhs) == NULL_TREE
9007 || TREE_CODE (TREE_TYPE (current.lhs)) != BOOLEAN_TYPE))
9008 warn_logical_not_parentheses (current.loc, current.tree_type,
9009 current.lhs, maybe_constant_value (rhs));
9011 overload = NULL;
9013 location_t combined_loc = make_location (current.loc,
9014 current.lhs.get_start (),
9015 rhs.get_finish ());
9017 /* ??? Currently we pass lhs_type == ERROR_MARK and rhs_type ==
9018 ERROR_MARK for everything that is not a binary expression.
9019 This makes warn_about_parentheses miss some warnings that
9020 involve unary operators. For unary expressions we should
9021 pass the correct tree_code unless the unary expression was
9022 surrounded by parentheses.
9024 if (no_toplevel_fold_p
9025 && lookahead_prec <= current.prec
9026 && sp == stack)
9027 current.lhs = build2_loc (combined_loc,
9028 current.tree_type,
9029 TREE_CODE_CLASS (current.tree_type)
9030 == tcc_comparison
9031 ? boolean_type_node : TREE_TYPE (current.lhs),
9032 current.lhs, rhs);
9033 else
9035 current.lhs = build_x_binary_op (combined_loc, current.tree_type,
9036 current.lhs, current.lhs_type,
9037 rhs, rhs_type, &overload,
9038 complain_flags (decltype_p));
9039 /* TODO: build_x_binary_op doesn't always honor the location. */
9040 current.lhs.set_location (combined_loc);
9042 current.lhs_type = current.tree_type;
9044 /* If the binary operator required the use of an overloaded operator,
9045 then this expression cannot be an integral constant-expression.
9046 An overloaded operator can be used even if both operands are
9047 otherwise permissible in an integral constant-expression if at
9048 least one of the operands is of enumeration type. */
9050 if (overload
9051 && cp_parser_non_integral_constant_expression (parser,
9052 NIC_OVERLOADED))
9053 return error_mark_node;
9056 return current.lhs;
9059 static cp_expr
9060 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
9061 bool no_toplevel_fold_p,
9062 enum cp_parser_prec prec,
9063 cp_id_kind * pidk)
9065 return cp_parser_binary_expression (parser, cast_p, no_toplevel_fold_p,
9066 /*decltype*/false, prec, pidk);
9069 /* Parse the `? expression : assignment-expression' part of a
9070 conditional-expression. The LOGICAL_OR_EXPR is the
9071 logical-or-expression that started the conditional-expression.
9072 Returns a representation of the entire conditional-expression.
9074 This routine is used by cp_parser_assignment_expression.
9076 ? expression : assignment-expression
9078 GNU Extensions:
9080 ? : assignment-expression */
9082 static tree
9083 cp_parser_question_colon_clause (cp_parser* parser, cp_expr logical_or_expr)
9085 tree expr, folded_logical_or_expr = cp_fully_fold (logical_or_expr);
9086 cp_expr assignment_expr;
9087 struct cp_token *token;
9088 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
9090 /* Consume the `?' token. */
9091 cp_lexer_consume_token (parser->lexer);
9092 token = cp_lexer_peek_token (parser->lexer);
9093 if (cp_parser_allow_gnu_extensions_p (parser)
9094 && token->type == CPP_COLON)
9096 pedwarn (token->location, OPT_Wpedantic,
9097 "ISO C++ does not allow ?: with omitted middle operand");
9098 /* Implicit true clause. */
9099 expr = NULL_TREE;
9100 c_inhibit_evaluation_warnings +=
9101 folded_logical_or_expr == truthvalue_true_node;
9102 warn_for_omitted_condop (token->location, logical_or_expr);
9104 else
9106 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
9107 parser->colon_corrects_to_scope_p = false;
9108 /* Parse the expression. */
9109 c_inhibit_evaluation_warnings +=
9110 folded_logical_or_expr == truthvalue_false_node;
9111 expr = cp_parser_expression (parser);
9112 c_inhibit_evaluation_warnings +=
9113 ((folded_logical_or_expr == truthvalue_true_node)
9114 - (folded_logical_or_expr == truthvalue_false_node));
9115 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
9118 /* The next token should be a `:'. */
9119 cp_parser_require (parser, CPP_COLON, RT_COLON);
9120 /* Parse the assignment-expression. */
9121 assignment_expr = cp_parser_assignment_expression (parser);
9122 c_inhibit_evaluation_warnings -=
9123 folded_logical_or_expr == truthvalue_true_node;
9125 /* Make a location:
9126 LOGICAL_OR_EXPR ? EXPR : ASSIGNMENT_EXPR
9127 ~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
9128 with the caret at the "?", ranging from the start of
9129 the logical_or_expr to the end of the assignment_expr. */
9130 loc = make_location (loc,
9131 logical_or_expr.get_start (),
9132 assignment_expr.get_finish ());
9134 /* Build the conditional-expression. */
9135 return build_x_conditional_expr (loc, logical_or_expr,
9136 expr,
9137 assignment_expr,
9138 tf_warning_or_error);
9141 /* Parse an assignment-expression.
9143 assignment-expression:
9144 conditional-expression
9145 logical-or-expression assignment-operator assignment_expression
9146 throw-expression
9148 CAST_P is true if this expression is the target of a cast.
9149 DECLTYPE_P is true if this expression is the operand of decltype.
9151 Returns a representation for the expression. */
9153 static cp_expr
9154 cp_parser_assignment_expression (cp_parser* parser, cp_id_kind * pidk,
9155 bool cast_p, bool decltype_p)
9157 cp_expr expr;
9159 /* If the next token is the `throw' keyword, then we're looking at
9160 a throw-expression. */
9161 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
9162 expr = cp_parser_throw_expression (parser);
9163 /* Otherwise, it must be that we are looking at a
9164 logical-or-expression. */
9165 else
9167 /* Parse the binary expressions (logical-or-expression). */
9168 expr = cp_parser_binary_expression (parser, cast_p, false,
9169 decltype_p,
9170 PREC_NOT_OPERATOR, pidk);
9171 /* If the next token is a `?' then we're actually looking at a
9172 conditional-expression. */
9173 if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
9174 return cp_parser_question_colon_clause (parser, expr);
9175 else
9177 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
9179 /* If it's an assignment-operator, we're using the second
9180 production. */
9181 enum tree_code assignment_operator
9182 = cp_parser_assignment_operator_opt (parser);
9183 if (assignment_operator != ERROR_MARK)
9185 bool non_constant_p;
9187 /* Parse the right-hand side of the assignment. */
9188 cp_expr rhs = cp_parser_initializer_clause (parser,
9189 &non_constant_p);
9191 if (BRACE_ENCLOSED_INITIALIZER_P (rhs))
9192 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
9194 /* An assignment may not appear in a
9195 constant-expression. */
9196 if (cp_parser_non_integral_constant_expression (parser,
9197 NIC_ASSIGNMENT))
9198 return error_mark_node;
9199 /* Build the assignment expression. Its default
9200 location:
9201 LHS = RHS
9202 ~~~~^~~~~
9203 is the location of the '=' token as the
9204 caret, ranging from the start of the lhs to the
9205 end of the rhs. */
9206 loc = make_location (loc,
9207 expr.get_start (),
9208 rhs.get_finish ());
9209 expr = build_x_modify_expr (loc, expr,
9210 assignment_operator,
9211 rhs,
9212 complain_flags (decltype_p));
9213 /* TODO: build_x_modify_expr doesn't honor the location,
9214 so we must set it here. */
9215 expr.set_location (loc);
9220 return expr;
9223 /* Parse an (optional) assignment-operator.
9225 assignment-operator: one of
9226 = *= /= %= += -= >>= <<= &= ^= |=
9228 GNU Extension:
9230 assignment-operator: one of
9231 <?= >?=
9233 If the next token is an assignment operator, the corresponding tree
9234 code is returned, and the token is consumed. For example, for
9235 `+=', PLUS_EXPR is returned. For `=' itself, the code returned is
9236 NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%',
9237 TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment
9238 operator, ERROR_MARK is returned. */
9240 static enum tree_code
9241 cp_parser_assignment_operator_opt (cp_parser* parser)
9243 enum tree_code op;
9244 cp_token *token;
9246 /* Peek at the next token. */
9247 token = cp_lexer_peek_token (parser->lexer);
9249 switch (token->type)
9251 case CPP_EQ:
9252 op = NOP_EXPR;
9253 break;
9255 case CPP_MULT_EQ:
9256 op = MULT_EXPR;
9257 break;
9259 case CPP_DIV_EQ:
9260 op = TRUNC_DIV_EXPR;
9261 break;
9263 case CPP_MOD_EQ:
9264 op = TRUNC_MOD_EXPR;
9265 break;
9267 case CPP_PLUS_EQ:
9268 op = PLUS_EXPR;
9269 break;
9271 case CPP_MINUS_EQ:
9272 op = MINUS_EXPR;
9273 break;
9275 case CPP_RSHIFT_EQ:
9276 op = RSHIFT_EXPR;
9277 break;
9279 case CPP_LSHIFT_EQ:
9280 op = LSHIFT_EXPR;
9281 break;
9283 case CPP_AND_EQ:
9284 op = BIT_AND_EXPR;
9285 break;
9287 case CPP_XOR_EQ:
9288 op = BIT_XOR_EXPR;
9289 break;
9291 case CPP_OR_EQ:
9292 op = BIT_IOR_EXPR;
9293 break;
9295 default:
9296 /* Nothing else is an assignment operator. */
9297 op = ERROR_MARK;
9300 /* An operator followed by ... is a fold-expression, handled elsewhere. */
9301 if (op != ERROR_MARK
9302 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS))
9303 op = ERROR_MARK;
9305 /* If it was an assignment operator, consume it. */
9306 if (op != ERROR_MARK)
9307 cp_lexer_consume_token (parser->lexer);
9309 return op;
9312 /* Parse an expression.
9314 expression:
9315 assignment-expression
9316 expression , assignment-expression
9318 CAST_P is true if this expression is the target of a cast.
9319 DECLTYPE_P is true if this expression is the immediate operand of decltype,
9320 except possibly parenthesized or on the RHS of a comma (N3276).
9322 Returns a representation of the expression. */
9324 static cp_expr
9325 cp_parser_expression (cp_parser* parser, cp_id_kind * pidk,
9326 bool cast_p, bool decltype_p)
9328 cp_expr expression = NULL_TREE;
9329 location_t loc = UNKNOWN_LOCATION;
9331 while (true)
9333 cp_expr assignment_expression;
9335 /* Parse the next assignment-expression. */
9336 assignment_expression
9337 = cp_parser_assignment_expression (parser, pidk, cast_p, decltype_p);
9339 /* We don't create a temporary for a call that is the immediate operand
9340 of decltype or on the RHS of a comma. But when we see a comma, we
9341 need to create a temporary for a call on the LHS. */
9342 if (decltype_p && !processing_template_decl
9343 && TREE_CODE (assignment_expression) == CALL_EXPR
9344 && CLASS_TYPE_P (TREE_TYPE (assignment_expression))
9345 && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
9346 assignment_expression
9347 = build_cplus_new (TREE_TYPE (assignment_expression),
9348 assignment_expression, tf_warning_or_error);
9350 /* If this is the first assignment-expression, we can just
9351 save it away. */
9352 if (!expression)
9353 expression = assignment_expression;
9354 else
9356 /* Create a location with caret at the comma, ranging
9357 from the start of the LHS to the end of the RHS. */
9358 loc = make_location (loc,
9359 expression.get_start (),
9360 assignment_expression.get_finish ());
9361 expression = build_x_compound_expr (loc, expression,
9362 assignment_expression,
9363 complain_flags (decltype_p));
9364 expression.set_location (loc);
9366 /* If the next token is not a comma, or we're in a fold-expression, then
9367 we are done with the expression. */
9368 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA)
9369 || cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS))
9370 break;
9371 /* Consume the `,'. */
9372 loc = cp_lexer_peek_token (parser->lexer)->location;
9373 cp_lexer_consume_token (parser->lexer);
9374 /* A comma operator cannot appear in a constant-expression. */
9375 if (cp_parser_non_integral_constant_expression (parser, NIC_COMMA))
9376 expression = error_mark_node;
9379 return expression;
9382 /* Parse a constant-expression.
9384 constant-expression:
9385 conditional-expression
9387 If ALLOW_NON_CONSTANT_P a non-constant expression is silently
9388 accepted. If ALLOW_NON_CONSTANT_P is true and the expression is not
9389 constant, *NON_CONSTANT_P is set to TRUE. If ALLOW_NON_CONSTANT_P
9390 is false, NON_CONSTANT_P should be NULL. */
9392 static cp_expr
9393 cp_parser_constant_expression (cp_parser* parser,
9394 bool allow_non_constant_p,
9395 bool *non_constant_p)
9397 bool saved_integral_constant_expression_p;
9398 bool saved_allow_non_integral_constant_expression_p;
9399 bool saved_non_integral_constant_expression_p;
9400 cp_expr expression;
9402 /* It might seem that we could simply parse the
9403 conditional-expression, and then check to see if it were
9404 TREE_CONSTANT. However, an expression that is TREE_CONSTANT is
9405 one that the compiler can figure out is constant, possibly after
9406 doing some simplifications or optimizations. The standard has a
9407 precise definition of constant-expression, and we must honor
9408 that, even though it is somewhat more restrictive.
9410 For example:
9412 int i[(2, 3)];
9414 is not a legal declaration, because `(2, 3)' is not a
9415 constant-expression. The `,' operator is forbidden in a
9416 constant-expression. However, GCC's constant-folding machinery
9417 will fold this operation to an INTEGER_CST for `3'. */
9419 /* Save the old settings. */
9420 saved_integral_constant_expression_p = parser->integral_constant_expression_p;
9421 saved_allow_non_integral_constant_expression_p
9422 = parser->allow_non_integral_constant_expression_p;
9423 saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
9424 /* We are now parsing a constant-expression. */
9425 parser->integral_constant_expression_p = true;
9426 parser->allow_non_integral_constant_expression_p
9427 = (allow_non_constant_p || cxx_dialect >= cxx11);
9428 parser->non_integral_constant_expression_p = false;
9429 /* Although the grammar says "conditional-expression", we parse an
9430 "assignment-expression", which also permits "throw-expression"
9431 and the use of assignment operators. In the case that
9432 ALLOW_NON_CONSTANT_P is false, we get better errors than we would
9433 otherwise. In the case that ALLOW_NON_CONSTANT_P is true, it is
9434 actually essential that we look for an assignment-expression.
9435 For example, cp_parser_initializer_clauses uses this function to
9436 determine whether a particular assignment-expression is in fact
9437 constant. */
9438 expression = cp_parser_assignment_expression (parser);
9439 /* Restore the old settings. */
9440 parser->integral_constant_expression_p
9441 = saved_integral_constant_expression_p;
9442 parser->allow_non_integral_constant_expression_p
9443 = saved_allow_non_integral_constant_expression_p;
9444 if (cxx_dialect >= cxx11)
9446 /* Require an rvalue constant expression here; that's what our
9447 callers expect. Reference constant expressions are handled
9448 separately in e.g. cp_parser_template_argument. */
9449 bool is_const = potential_rvalue_constant_expression (expression);
9450 parser->non_integral_constant_expression_p = !is_const;
9451 if (!is_const && !allow_non_constant_p)
9452 require_potential_rvalue_constant_expression (expression);
9454 if (allow_non_constant_p)
9455 *non_constant_p = parser->non_integral_constant_expression_p;
9456 parser->non_integral_constant_expression_p
9457 = saved_non_integral_constant_expression_p;
9459 return expression;
9462 /* Parse __builtin_offsetof.
9464 offsetof-expression:
9465 "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
9467 offsetof-member-designator:
9468 id-expression
9469 | offsetof-member-designator "." id-expression
9470 | offsetof-member-designator "[" expression "]"
9471 | offsetof-member-designator "->" id-expression */
9473 static cp_expr
9474 cp_parser_builtin_offsetof (cp_parser *parser)
9476 int save_ice_p, save_non_ice_p;
9477 tree type;
9478 cp_expr expr;
9479 cp_id_kind dummy;
9480 cp_token *token;
9481 location_t finish_loc;
9483 /* We're about to accept non-integral-constant things, but will
9484 definitely yield an integral constant expression. Save and
9485 restore these values around our local parsing. */
9486 save_ice_p = parser->integral_constant_expression_p;
9487 save_non_ice_p = parser->non_integral_constant_expression_p;
9489 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
9491 /* Consume the "__builtin_offsetof" token. */
9492 cp_lexer_consume_token (parser->lexer);
9493 /* Consume the opening `('. */
9494 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
9495 /* Parse the type-id. */
9496 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
9497 type = cp_parser_type_id (parser);
9498 /* Look for the `,'. */
9499 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
9500 token = cp_lexer_peek_token (parser->lexer);
9502 /* Build the (type *)null that begins the traditional offsetof macro. */
9503 tree object_ptr
9504 = build_static_cast (build_pointer_type (type), null_pointer_node,
9505 tf_warning_or_error);
9507 /* Parse the offsetof-member-designator. We begin as if we saw "expr->". */
9508 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, object_ptr,
9509 true, &dummy, token->location);
9510 while (true)
9512 token = cp_lexer_peek_token (parser->lexer);
9513 switch (token->type)
9515 case CPP_OPEN_SQUARE:
9516 /* offsetof-member-designator "[" expression "]" */
9517 expr = cp_parser_postfix_open_square_expression (parser, expr,
9518 true, false);
9519 break;
9521 case CPP_DEREF:
9522 /* offsetof-member-designator "->" identifier */
9523 expr = grok_array_decl (token->location, expr,
9524 integer_zero_node, false);
9525 /* FALLTHRU */
9527 case CPP_DOT:
9528 /* offsetof-member-designator "." identifier */
9529 cp_lexer_consume_token (parser->lexer);
9530 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT,
9531 expr, true, &dummy,
9532 token->location);
9533 break;
9535 case CPP_CLOSE_PAREN:
9536 /* Consume the ")" token. */
9537 finish_loc = cp_lexer_peek_token (parser->lexer)->location;
9538 cp_lexer_consume_token (parser->lexer);
9539 goto success;
9541 default:
9542 /* Error. We know the following require will fail, but
9543 that gives the proper error message. */
9544 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
9545 cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
9546 expr = error_mark_node;
9547 goto failure;
9551 success:
9552 /* Make a location of the form:
9553 __builtin_offsetof (struct s, f)
9554 ~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~
9555 with caret at the type-id, ranging from the start of the
9556 "_builtin_offsetof" token to the close paren. */
9557 loc = make_location (loc, start_loc, finish_loc);
9558 /* The result will be an INTEGER_CST, so we need to explicitly
9559 preserve the location. */
9560 expr = cp_expr (finish_offsetof (object_ptr, expr, loc), loc);
9562 failure:
9563 parser->integral_constant_expression_p = save_ice_p;
9564 parser->non_integral_constant_expression_p = save_non_ice_p;
9566 return expr;
9569 /* Parse a trait expression.
9571 Returns a representation of the expression, the underlying type
9572 of the type at issue when KEYWORD is RID_UNDERLYING_TYPE. */
9574 static tree
9575 cp_parser_trait_expr (cp_parser* parser, enum rid keyword)
9577 cp_trait_kind kind;
9578 tree type1, type2 = NULL_TREE;
9579 bool binary = false;
9580 bool variadic = false;
9582 switch (keyword)
9584 case RID_HAS_NOTHROW_ASSIGN:
9585 kind = CPTK_HAS_NOTHROW_ASSIGN;
9586 break;
9587 case RID_HAS_NOTHROW_CONSTRUCTOR:
9588 kind = CPTK_HAS_NOTHROW_CONSTRUCTOR;
9589 break;
9590 case RID_HAS_NOTHROW_COPY:
9591 kind = CPTK_HAS_NOTHROW_COPY;
9592 break;
9593 case RID_HAS_TRIVIAL_ASSIGN:
9594 kind = CPTK_HAS_TRIVIAL_ASSIGN;
9595 break;
9596 case RID_HAS_TRIVIAL_CONSTRUCTOR:
9597 kind = CPTK_HAS_TRIVIAL_CONSTRUCTOR;
9598 break;
9599 case RID_HAS_TRIVIAL_COPY:
9600 kind = CPTK_HAS_TRIVIAL_COPY;
9601 break;
9602 case RID_HAS_TRIVIAL_DESTRUCTOR:
9603 kind = CPTK_HAS_TRIVIAL_DESTRUCTOR;
9604 break;
9605 case RID_HAS_UNIQUE_OBJ_REPRESENTATIONS:
9606 kind = CPTK_HAS_UNIQUE_OBJ_REPRESENTATIONS;
9607 break;
9608 case RID_HAS_VIRTUAL_DESTRUCTOR:
9609 kind = CPTK_HAS_VIRTUAL_DESTRUCTOR;
9610 break;
9611 case RID_IS_ABSTRACT:
9612 kind = CPTK_IS_ABSTRACT;
9613 break;
9614 case RID_IS_BASE_OF:
9615 kind = CPTK_IS_BASE_OF;
9616 binary = true;
9617 break;
9618 case RID_IS_CLASS:
9619 kind = CPTK_IS_CLASS;
9620 break;
9621 case RID_IS_EMPTY:
9622 kind = CPTK_IS_EMPTY;
9623 break;
9624 case RID_IS_ENUM:
9625 kind = CPTK_IS_ENUM;
9626 break;
9627 case RID_IS_FINAL:
9628 kind = CPTK_IS_FINAL;
9629 break;
9630 case RID_IS_LITERAL_TYPE:
9631 kind = CPTK_IS_LITERAL_TYPE;
9632 break;
9633 case RID_IS_POD:
9634 kind = CPTK_IS_POD;
9635 break;
9636 case RID_IS_POLYMORPHIC:
9637 kind = CPTK_IS_POLYMORPHIC;
9638 break;
9639 case RID_IS_SAME_AS:
9640 kind = CPTK_IS_SAME_AS;
9641 binary = true;
9642 break;
9643 case RID_IS_STD_LAYOUT:
9644 kind = CPTK_IS_STD_LAYOUT;
9645 break;
9646 case RID_IS_TRIVIAL:
9647 kind = CPTK_IS_TRIVIAL;
9648 break;
9649 case RID_IS_TRIVIALLY_ASSIGNABLE:
9650 kind = CPTK_IS_TRIVIALLY_ASSIGNABLE;
9651 binary = true;
9652 break;
9653 case RID_IS_TRIVIALLY_CONSTRUCTIBLE:
9654 kind = CPTK_IS_TRIVIALLY_CONSTRUCTIBLE;
9655 variadic = true;
9656 break;
9657 case RID_IS_TRIVIALLY_COPYABLE:
9658 kind = CPTK_IS_TRIVIALLY_COPYABLE;
9659 break;
9660 case RID_IS_UNION:
9661 kind = CPTK_IS_UNION;
9662 break;
9663 case RID_UNDERLYING_TYPE:
9664 kind = CPTK_UNDERLYING_TYPE;
9665 break;
9666 case RID_BASES:
9667 kind = CPTK_BASES;
9668 break;
9669 case RID_DIRECT_BASES:
9670 kind = CPTK_DIRECT_BASES;
9671 break;
9672 default:
9673 gcc_unreachable ();
9676 /* Consume the token. */
9677 cp_lexer_consume_token (parser->lexer);
9679 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
9682 type_id_in_expr_sentinel s (parser);
9683 type1 = cp_parser_type_id (parser);
9686 if (type1 == error_mark_node)
9687 return error_mark_node;
9689 if (binary)
9691 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
9694 type_id_in_expr_sentinel s (parser);
9695 type2 = cp_parser_type_id (parser);
9698 if (type2 == error_mark_node)
9699 return error_mark_node;
9701 else if (variadic)
9703 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
9705 cp_lexer_consume_token (parser->lexer);
9706 tree elt = cp_parser_type_id (parser);
9707 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
9709 cp_lexer_consume_token (parser->lexer);
9710 elt = make_pack_expansion (elt);
9712 if (elt == error_mark_node)
9713 return error_mark_node;
9714 type2 = tree_cons (NULL_TREE, elt, type2);
9718 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
9720 /* Complete the trait expression, which may mean either processing
9721 the trait expr now or saving it for template instantiation. */
9722 switch (kind)
9724 case CPTK_UNDERLYING_TYPE:
9725 return finish_underlying_type (type1);
9726 case CPTK_BASES:
9727 return finish_bases (type1, false);
9728 case CPTK_DIRECT_BASES:
9729 return finish_bases (type1, true);
9730 default:
9731 return finish_trait_expr (kind, type1, type2);
9735 /* Lambdas that appear in variable initializer or default argument scope
9736 get that in their mangling, so we need to record it. We might as well
9737 use the count for function and namespace scopes as well. */
9738 static GTY(()) tree lambda_scope;
9739 static GTY(()) int lambda_count;
9740 struct GTY(()) tree_int
9742 tree t;
9743 int i;
9745 static GTY(()) vec<tree_int, va_gc> *lambda_scope_stack;
9747 static void
9748 start_lambda_scope (tree decl)
9750 tree_int ti;
9751 gcc_assert (decl);
9752 /* Once we're inside a function, we ignore other scopes and just push
9753 the function again so that popping works properly. */
9754 if (current_function_decl && TREE_CODE (decl) != FUNCTION_DECL)
9755 decl = current_function_decl;
9756 ti.t = lambda_scope;
9757 ti.i = lambda_count;
9758 vec_safe_push (lambda_scope_stack, ti);
9759 if (lambda_scope != decl)
9761 /* Don't reset the count if we're still in the same function. */
9762 lambda_scope = decl;
9763 lambda_count = 0;
9767 static void
9768 record_lambda_scope (tree lambda)
9770 LAMBDA_EXPR_EXTRA_SCOPE (lambda) = lambda_scope;
9771 LAMBDA_EXPR_DISCRIMINATOR (lambda) = lambda_count++;
9774 static void
9775 finish_lambda_scope (void)
9777 tree_int *p = &lambda_scope_stack->last ();
9778 if (lambda_scope != p->t)
9780 lambda_scope = p->t;
9781 lambda_count = p->i;
9783 lambda_scope_stack->pop ();
9786 /* Parse a lambda expression.
9788 lambda-expression:
9789 lambda-introducer lambda-declarator [opt] compound-statement
9791 Returns a representation of the expression. */
9793 static cp_expr
9794 cp_parser_lambda_expression (cp_parser* parser)
9796 tree lambda_expr = build_lambda_expr ();
9797 tree type;
9798 bool ok = true;
9799 cp_token *token = cp_lexer_peek_token (parser->lexer);
9800 cp_token_position start = 0;
9802 LAMBDA_EXPR_LOCATION (lambda_expr) = token->location;
9804 if (cp_unevaluated_operand)
9806 if (!token->error_reported)
9808 error_at (LAMBDA_EXPR_LOCATION (lambda_expr),
9809 "lambda-expression in unevaluated context");
9810 token->error_reported = true;
9812 ok = false;
9814 else if (parser->in_template_argument_list_p)
9816 if (!token->error_reported)
9818 error_at (token->location, "lambda-expression in template-argument");
9819 token->error_reported = true;
9821 ok = false;
9824 /* We may be in the middle of deferred access check. Disable
9825 it now. */
9826 push_deferring_access_checks (dk_no_deferred);
9828 cp_parser_lambda_introducer (parser, lambda_expr);
9830 type = begin_lambda_type (lambda_expr);
9831 if (type == error_mark_node)
9832 return error_mark_node;
9834 record_lambda_scope (lambda_expr);
9836 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
9837 determine_visibility (TYPE_NAME (type));
9839 /* Now that we've started the type, add the capture fields for any
9840 explicit captures. */
9841 register_capture_members (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr));
9844 /* Inside the class, surrounding template-parameter-lists do not apply. */
9845 unsigned int saved_num_template_parameter_lists
9846 = parser->num_template_parameter_lists;
9847 unsigned char in_statement = parser->in_statement;
9848 bool in_switch_statement_p = parser->in_switch_statement_p;
9849 bool fully_implicit_function_template_p
9850 = parser->fully_implicit_function_template_p;
9851 tree implicit_template_parms = parser->implicit_template_parms;
9852 cp_binding_level* implicit_template_scope = parser->implicit_template_scope;
9853 bool auto_is_implicit_function_template_parm_p
9854 = parser->auto_is_implicit_function_template_parm_p;
9856 parser->num_template_parameter_lists = 0;
9857 parser->in_statement = 0;
9858 parser->in_switch_statement_p = false;
9859 parser->fully_implicit_function_template_p = false;
9860 parser->implicit_template_parms = 0;
9861 parser->implicit_template_scope = 0;
9862 parser->auto_is_implicit_function_template_parm_p = false;
9864 /* By virtue of defining a local class, a lambda expression has access to
9865 the private variables of enclosing classes. */
9867 ok &= cp_parser_lambda_declarator_opt (parser, lambda_expr);
9869 if (ok && cp_parser_error_occurred (parser))
9870 ok = false;
9872 if (ok)
9874 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
9875 && cp_parser_start_tentative_firewall (parser))
9876 start = token;
9877 cp_parser_lambda_body (parser, lambda_expr);
9879 else if (cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
9881 if (cp_parser_skip_to_closing_brace (parser))
9882 cp_lexer_consume_token (parser->lexer);
9885 /* The capture list was built up in reverse order; fix that now. */
9886 LAMBDA_EXPR_CAPTURE_LIST (lambda_expr)
9887 = nreverse (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr));
9889 if (ok)
9890 maybe_add_lambda_conv_op (type);
9892 type = finish_struct (type, /*attributes=*/NULL_TREE);
9894 parser->num_template_parameter_lists = saved_num_template_parameter_lists;
9895 parser->in_statement = in_statement;
9896 parser->in_switch_statement_p = in_switch_statement_p;
9897 parser->fully_implicit_function_template_p
9898 = fully_implicit_function_template_p;
9899 parser->implicit_template_parms = implicit_template_parms;
9900 parser->implicit_template_scope = implicit_template_scope;
9901 parser->auto_is_implicit_function_template_parm_p
9902 = auto_is_implicit_function_template_parm_p;
9905 /* This field is only used during parsing of the lambda. */
9906 LAMBDA_EXPR_THIS_CAPTURE (lambda_expr) = NULL_TREE;
9908 /* This lambda shouldn't have any proxies left at this point. */
9909 gcc_assert (LAMBDA_EXPR_PENDING_PROXIES (lambda_expr) == NULL);
9910 /* And now that we're done, push proxies for an enclosing lambda. */
9911 insert_pending_capture_proxies ();
9913 if (ok)
9914 lambda_expr = build_lambda_object (lambda_expr);
9915 else
9916 lambda_expr = error_mark_node;
9918 cp_parser_end_tentative_firewall (parser, start, lambda_expr);
9920 pop_deferring_access_checks ();
9922 return lambda_expr;
9925 /* Parse the beginning of a lambda expression.
9927 lambda-introducer:
9928 [ lambda-capture [opt] ]
9930 LAMBDA_EXPR is the current representation of the lambda expression. */
9932 static void
9933 cp_parser_lambda_introducer (cp_parser* parser, tree lambda_expr)
9935 /* Need commas after the first capture. */
9936 bool first = true;
9938 /* Eat the leading `['. */
9939 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
9941 /* Record default capture mode. "[&" "[=" "[&," "[=," */
9942 if (cp_lexer_next_token_is (parser->lexer, CPP_AND)
9943 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_NAME)
9944 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_REFERENCE;
9945 else if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
9946 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_COPY;
9948 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE)
9950 cp_lexer_consume_token (parser->lexer);
9951 first = false;
9954 while (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_SQUARE))
9956 cp_token* capture_token;
9957 tree capture_id;
9958 tree capture_init_expr;
9959 cp_id_kind idk = CP_ID_KIND_NONE;
9960 bool explicit_init_p = false;
9962 enum capture_kind_type
9964 BY_COPY,
9965 BY_REFERENCE
9967 enum capture_kind_type capture_kind = BY_COPY;
9969 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
9971 error ("expected end of capture-list");
9972 return;
9975 if (first)
9976 first = false;
9977 else
9978 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
9980 /* Possibly capture `this'. */
9981 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THIS))
9983 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
9984 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_COPY)
9985 pedwarn (loc, 0, "explicit by-copy capture of %<this%> redundant "
9986 "with by-copy capture default");
9987 cp_lexer_consume_token (parser->lexer);
9988 add_capture (lambda_expr,
9989 /*id=*/this_identifier,
9990 /*initializer=*/finish_this_expr (),
9991 /*by_reference_p=*/true,
9992 explicit_init_p);
9993 continue;
9996 /* Possibly capture `*this'. */
9997 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT)
9998 && cp_lexer_nth_token_is_keyword (parser->lexer, 2, RID_THIS))
10000 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
10001 if (cxx_dialect < cxx1z)
10002 pedwarn (loc, 0, "%<*this%> capture only available with "
10003 "-std=c++1z or -std=gnu++1z");
10004 cp_lexer_consume_token (parser->lexer);
10005 cp_lexer_consume_token (parser->lexer);
10006 add_capture (lambda_expr,
10007 /*id=*/this_identifier,
10008 /*initializer=*/finish_this_expr (),
10009 /*by_reference_p=*/false,
10010 explicit_init_p);
10011 continue;
10014 /* Remember whether we want to capture as a reference or not. */
10015 if (cp_lexer_next_token_is (parser->lexer, CPP_AND))
10017 capture_kind = BY_REFERENCE;
10018 cp_lexer_consume_token (parser->lexer);
10021 /* Get the identifier. */
10022 capture_token = cp_lexer_peek_token (parser->lexer);
10023 capture_id = cp_parser_identifier (parser);
10025 if (capture_id == error_mark_node)
10026 /* Would be nice to have a cp_parser_skip_to_closing_x for general
10027 delimiters, but I modified this to stop on unnested ']' as well. It
10028 was already changed to stop on unnested '}', so the
10029 "closing_parenthesis" name is no more misleading with my change. */
10031 cp_parser_skip_to_closing_parenthesis (parser,
10032 /*recovering=*/true,
10033 /*or_comma=*/true,
10034 /*consume_paren=*/true);
10035 break;
10038 /* Find the initializer for this capture. */
10039 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ)
10040 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
10041 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
10043 bool direct, non_constant;
10044 /* An explicit initializer exists. */
10045 if (cxx_dialect < cxx14)
10046 pedwarn (input_location, 0,
10047 "lambda capture initializers "
10048 "only available with -std=c++14 or -std=gnu++14");
10049 capture_init_expr = cp_parser_initializer (parser, &direct,
10050 &non_constant);
10051 explicit_init_p = true;
10052 if (capture_init_expr == NULL_TREE)
10054 error ("empty initializer for lambda init-capture");
10055 capture_init_expr = error_mark_node;
10058 else
10060 const char* error_msg;
10062 /* Turn the identifier into an id-expression. */
10063 capture_init_expr
10064 = cp_parser_lookup_name_simple (parser, capture_id,
10065 capture_token->location);
10067 if (capture_init_expr == error_mark_node)
10069 unqualified_name_lookup_error (capture_id);
10070 continue;
10072 else if (DECL_P (capture_init_expr)
10073 && (!VAR_P (capture_init_expr)
10074 && TREE_CODE (capture_init_expr) != PARM_DECL))
10076 error_at (capture_token->location,
10077 "capture of non-variable %qD ",
10078 capture_init_expr);
10079 inform (DECL_SOURCE_LOCATION (capture_init_expr),
10080 "%q#D declared here", capture_init_expr);
10081 continue;
10083 if (VAR_P (capture_init_expr)
10084 && decl_storage_duration (capture_init_expr) != dk_auto)
10086 if (pedwarn (capture_token->location, 0, "capture of variable "
10087 "%qD with non-automatic storage duration",
10088 capture_init_expr))
10089 inform (DECL_SOURCE_LOCATION (capture_init_expr),
10090 "%q#D declared here", capture_init_expr);
10091 continue;
10094 capture_init_expr
10095 = finish_id_expression
10096 (capture_id,
10097 capture_init_expr,
10098 parser->scope,
10099 &idk,
10100 /*integral_constant_expression_p=*/false,
10101 /*allow_non_integral_constant_expression_p=*/false,
10102 /*non_integral_constant_expression_p=*/NULL,
10103 /*template_p=*/false,
10104 /*done=*/true,
10105 /*address_p=*/false,
10106 /*template_arg_p=*/false,
10107 &error_msg,
10108 capture_token->location);
10110 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
10112 cp_lexer_consume_token (parser->lexer);
10113 capture_init_expr = make_pack_expansion (capture_init_expr);
10115 else
10116 check_for_bare_parameter_packs (capture_init_expr);
10119 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE
10120 && !explicit_init_p)
10122 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_COPY
10123 && capture_kind == BY_COPY)
10124 pedwarn (capture_token->location, 0, "explicit by-copy capture "
10125 "of %qD redundant with by-copy capture default",
10126 capture_id);
10127 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_REFERENCE
10128 && capture_kind == BY_REFERENCE)
10129 pedwarn (capture_token->location, 0, "explicit by-reference "
10130 "capture of %qD redundant with by-reference capture "
10131 "default", capture_id);
10134 add_capture (lambda_expr,
10135 capture_id,
10136 capture_init_expr,
10137 /*by_reference_p=*/capture_kind == BY_REFERENCE,
10138 explicit_init_p);
10141 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
10144 /* Parse the (optional) middle of a lambda expression.
10146 lambda-declarator:
10147 < template-parameter-list [opt] >
10148 ( parameter-declaration-clause [opt] )
10149 attribute-specifier [opt]
10150 decl-specifier-seq [opt]
10151 exception-specification [opt]
10152 lambda-return-type-clause [opt]
10154 LAMBDA_EXPR is the current representation of the lambda expression. */
10156 static bool
10157 cp_parser_lambda_declarator_opt (cp_parser* parser, tree lambda_expr)
10159 /* 5.1.1.4 of the standard says:
10160 If a lambda-expression does not include a lambda-declarator, it is as if
10161 the lambda-declarator were ().
10162 This means an empty parameter list, no attributes, and no exception
10163 specification. */
10164 tree param_list = void_list_node;
10165 tree attributes = NULL_TREE;
10166 tree exception_spec = NULL_TREE;
10167 tree template_param_list = NULL_TREE;
10168 tree tx_qual = NULL_TREE;
10169 cp_decl_specifier_seq lambda_specs;
10170 clear_decl_specs (&lambda_specs);
10172 /* The template-parameter-list is optional, but must begin with
10173 an opening angle if present. */
10174 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
10176 if (cxx_dialect < cxx14)
10177 pedwarn (parser->lexer->next_token->location, 0,
10178 "lambda templates are only available with "
10179 "-std=c++14 or -std=gnu++14");
10180 else
10181 pedwarn (parser->lexer->next_token->location, OPT_Wpedantic,
10182 "ISO C++ does not support lambda templates");
10184 cp_lexer_consume_token (parser->lexer);
10186 template_param_list = cp_parser_template_parameter_list (parser);
10188 cp_parser_skip_to_end_of_template_parameter_list (parser);
10190 /* We just processed one more parameter list. */
10191 ++parser->num_template_parameter_lists;
10194 /* The parameter-declaration-clause is optional (unless
10195 template-parameter-list was given), but must begin with an
10196 opening parenthesis if present. */
10197 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
10199 cp_lexer_consume_token (parser->lexer);
10201 begin_scope (sk_function_parms, /*entity=*/NULL_TREE);
10203 /* Parse parameters. */
10204 param_list = cp_parser_parameter_declaration_clause (parser);
10206 /* Default arguments shall not be specified in the
10207 parameter-declaration-clause of a lambda-declarator. */
10208 if (cxx_dialect < cxx14)
10209 for (tree t = param_list; t; t = TREE_CHAIN (t))
10210 if (TREE_PURPOSE (t) && DECL_P (TREE_VALUE (t)))
10211 pedwarn (DECL_SOURCE_LOCATION (TREE_VALUE (t)), OPT_Wpedantic,
10212 "default argument specified for lambda parameter");
10214 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
10216 attributes = cp_parser_attributes_opt (parser);
10218 /* In the decl-specifier-seq of the lambda-declarator, each
10219 decl-specifier shall either be mutable or constexpr. */
10220 int declares_class_or_enum;
10221 if (cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer))
10222 cp_parser_decl_specifier_seq (parser,
10223 CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR,
10224 &lambda_specs, &declares_class_or_enum);
10225 if (lambda_specs.storage_class == sc_mutable)
10227 LAMBDA_EXPR_MUTABLE_P (lambda_expr) = 1;
10228 if (lambda_specs.conflicting_specifiers_p)
10229 error_at (lambda_specs.locations[ds_storage_class],
10230 "duplicate %<mutable%>");
10233 tx_qual = cp_parser_tx_qualifier_opt (parser);
10235 /* Parse optional exception specification. */
10236 exception_spec = cp_parser_exception_specification_opt (parser);
10238 /* Parse optional trailing return type. */
10239 if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
10241 cp_lexer_consume_token (parser->lexer);
10242 LAMBDA_EXPR_RETURN_TYPE (lambda_expr)
10243 = cp_parser_trailing_type_id (parser);
10246 /* The function parameters must be in scope all the way until after the
10247 trailing-return-type in case of decltype. */
10248 pop_bindings_and_leave_scope ();
10250 else if (template_param_list != NULL_TREE) // generate diagnostic
10251 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
10253 /* Create the function call operator.
10255 Messing with declarators like this is no uglier than building up the
10256 FUNCTION_DECL by hand, and this is less likely to get out of sync with
10257 other code. */
10259 cp_decl_specifier_seq return_type_specs;
10260 cp_declarator* declarator;
10261 tree fco;
10262 int quals;
10263 void *p;
10265 clear_decl_specs (&return_type_specs);
10266 if (LAMBDA_EXPR_RETURN_TYPE (lambda_expr))
10267 return_type_specs.type = LAMBDA_EXPR_RETURN_TYPE (lambda_expr);
10268 else
10269 /* Maybe we will deduce the return type later. */
10270 return_type_specs.type = make_auto ();
10272 if (lambda_specs.locations[ds_constexpr])
10274 if (cxx_dialect >= cxx1z)
10275 return_type_specs.locations[ds_constexpr]
10276 = lambda_specs.locations[ds_constexpr];
10277 else
10278 error_at (lambda_specs.locations[ds_constexpr], "%<constexpr%> "
10279 "lambda only available with -std=c++1z or -std=gnu++1z");
10282 p = obstack_alloc (&declarator_obstack, 0);
10284 declarator = make_id_declarator (NULL_TREE, cp_operator_id (CALL_EXPR),
10285 sfk_none);
10287 quals = (LAMBDA_EXPR_MUTABLE_P (lambda_expr)
10288 ? TYPE_UNQUALIFIED : TYPE_QUAL_CONST);
10289 declarator = make_call_declarator (declarator, param_list, quals,
10290 VIRT_SPEC_UNSPECIFIED,
10291 REF_QUAL_NONE,
10292 tx_qual,
10293 exception_spec,
10294 /*late_return_type=*/NULL_TREE,
10295 /*requires_clause*/NULL_TREE);
10296 declarator->id_loc = LAMBDA_EXPR_LOCATION (lambda_expr);
10298 fco = grokmethod (&return_type_specs,
10299 declarator,
10300 attributes);
10301 if (fco != error_mark_node)
10303 DECL_INITIALIZED_IN_CLASS_P (fco) = 1;
10304 DECL_ARTIFICIAL (fco) = 1;
10305 /* Give the object parameter a different name. */
10306 DECL_NAME (DECL_ARGUMENTS (fco)) = get_identifier ("__closure");
10307 if (LAMBDA_EXPR_RETURN_TYPE (lambda_expr))
10308 TYPE_HAS_LATE_RETURN_TYPE (TREE_TYPE (fco)) = 1;
10310 if (template_param_list)
10312 fco = finish_member_template_decl (fco);
10313 finish_template_decl (template_param_list);
10314 --parser->num_template_parameter_lists;
10316 else if (parser->fully_implicit_function_template_p)
10317 fco = finish_fully_implicit_template (parser, fco);
10319 finish_member_declaration (fco);
10321 obstack_free (&declarator_obstack, p);
10323 return (fco != error_mark_node);
10327 /* Parse the body of a lambda expression, which is simply
10329 compound-statement
10331 but which requires special handling.
10332 LAMBDA_EXPR is the current representation of the lambda expression. */
10334 static void
10335 cp_parser_lambda_body (cp_parser* parser, tree lambda_expr)
10337 bool nested = (current_function_decl != NULL_TREE);
10338 bool local_variables_forbidden_p = parser->local_variables_forbidden_p;
10339 if (nested)
10340 push_function_context ();
10341 else
10342 /* Still increment function_depth so that we don't GC in the
10343 middle of an expression. */
10344 ++function_depth;
10345 vec<tree> omp_privatization_save;
10346 save_omp_privatization_clauses (omp_privatization_save);
10347 /* Clear this in case we're in the middle of a default argument. */
10348 parser->local_variables_forbidden_p = false;
10350 /* Finish the function call operator
10351 - class_specifier
10352 + late_parsing_for_member
10353 + function_definition_after_declarator
10354 + ctor_initializer_opt_and_function_body */
10356 tree fco = lambda_function (lambda_expr);
10357 tree body;
10358 bool done = false;
10359 tree compound_stmt;
10360 tree cap;
10362 /* Let the front end know that we are going to be defining this
10363 function. */
10364 start_preparsed_function (fco,
10365 NULL_TREE,
10366 SF_PRE_PARSED | SF_INCLASS_INLINE);
10368 start_lambda_scope (fco);
10369 body = begin_function_body ();
10371 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
10372 goto out;
10374 /* Push the proxies for any explicit captures. */
10375 for (cap = LAMBDA_EXPR_CAPTURE_LIST (lambda_expr); cap;
10376 cap = TREE_CHAIN (cap))
10377 build_capture_proxy (TREE_PURPOSE (cap));
10379 compound_stmt = begin_compound_stmt (0);
10381 /* 5.1.1.4 of the standard says:
10382 If a lambda-expression does not include a trailing-return-type, it
10383 is as if the trailing-return-type denotes the following type:
10384 * if the compound-statement is of the form
10385 { return attribute-specifier [opt] expression ; }
10386 the type of the returned expression after lvalue-to-rvalue
10387 conversion (_conv.lval_ 4.1), array-to-pointer conversion
10388 (_conv.array_ 4.2), and function-to-pointer conversion
10389 (_conv.func_ 4.3);
10390 * otherwise, void. */
10392 /* In a lambda that has neither a lambda-return-type-clause
10393 nor a deducible form, errors should be reported for return statements
10394 in the body. Since we used void as the placeholder return type, parsing
10395 the body as usual will give such desired behavior. */
10396 if (!LAMBDA_EXPR_RETURN_TYPE (lambda_expr)
10397 && cp_lexer_peek_nth_token (parser->lexer, 1)->keyword == RID_RETURN
10398 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SEMICOLON)
10400 tree expr = NULL_TREE;
10401 cp_id_kind idk = CP_ID_KIND_NONE;
10403 /* Parse tentatively in case there's more after the initial return
10404 statement. */
10405 cp_parser_parse_tentatively (parser);
10407 cp_parser_require_keyword (parser, RID_RETURN, RT_RETURN);
10409 expr = cp_parser_expression (parser, &idk);
10411 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10412 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
10414 if (cp_parser_parse_definitely (parser))
10416 if (!processing_template_decl)
10418 tree type = lambda_return_type (expr);
10419 apply_deduced_return_type (fco, type);
10420 if (type == error_mark_node)
10421 expr = error_mark_node;
10424 /* Will get error here if type not deduced yet. */
10425 finish_return_stmt (expr);
10427 done = true;
10431 if (!done)
10433 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
10434 cp_parser_label_declaration (parser);
10435 cp_parser_statement_seq_opt (parser, NULL_TREE);
10436 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
10439 finish_compound_stmt (compound_stmt);
10441 out:
10442 finish_function_body (body);
10443 finish_lambda_scope ();
10445 /* Finish the function and generate code for it if necessary. */
10446 tree fn = finish_function (/*inline*/2);
10448 /* Only expand if the call op is not a template. */
10449 if (!DECL_TEMPLATE_INFO (fco))
10450 expand_or_defer_fn (fn);
10453 restore_omp_privatization_clauses (omp_privatization_save);
10454 parser->local_variables_forbidden_p = local_variables_forbidden_p;
10455 if (nested)
10456 pop_function_context();
10457 else
10458 --function_depth;
10461 /* Statements [gram.stmt.stmt] */
10463 /* Parse a statement.
10465 statement:
10466 labeled-statement
10467 expression-statement
10468 compound-statement
10469 selection-statement
10470 iteration-statement
10471 jump-statement
10472 declaration-statement
10473 try-block
10475 C++11:
10477 statement:
10478 labeled-statement
10479 attribute-specifier-seq (opt) expression-statement
10480 attribute-specifier-seq (opt) compound-statement
10481 attribute-specifier-seq (opt) selection-statement
10482 attribute-specifier-seq (opt) iteration-statement
10483 attribute-specifier-seq (opt) jump-statement
10484 declaration-statement
10485 attribute-specifier-seq (opt) try-block
10487 init-statement:
10488 expression-statement
10489 simple-declaration
10491 TM Extension:
10493 statement:
10494 atomic-statement
10496 IN_COMPOUND is true when the statement is nested inside a
10497 cp_parser_compound_statement; this matters for certain pragmas.
10499 If IF_P is not NULL, *IF_P is set to indicate whether the statement
10500 is a (possibly labeled) if statement which is not enclosed in braces
10501 and has an else clause. This is used to implement -Wparentheses.
10503 CHAIN is a vector of if-else-if conditions. */
10505 static void
10506 cp_parser_statement (cp_parser* parser, tree in_statement_expr,
10507 bool in_compound, bool *if_p, vec<tree> *chain)
10509 tree statement, std_attrs = NULL_TREE;
10510 cp_token *token;
10511 location_t statement_location, attrs_location;
10513 restart:
10514 if (if_p != NULL)
10515 *if_p = false;
10516 /* There is no statement yet. */
10517 statement = NULL_TREE;
10519 saved_token_sentinel saved_tokens (parser->lexer);
10520 attrs_location = cp_lexer_peek_token (parser->lexer)->location;
10521 if (c_dialect_objc ())
10522 /* In obj-c++, seeing '[[' might be the either the beginning of
10523 c++11 attributes, or a nested objc-message-expression. So
10524 let's parse the c++11 attributes tentatively. */
10525 cp_parser_parse_tentatively (parser);
10526 std_attrs = cp_parser_std_attribute_spec_seq (parser);
10527 if (c_dialect_objc ())
10529 if (!cp_parser_parse_definitely (parser))
10530 std_attrs = NULL_TREE;
10533 /* Peek at the next token. */
10534 token = cp_lexer_peek_token (parser->lexer);
10535 /* Remember the location of the first token in the statement. */
10536 statement_location = token->location;
10537 /* If this is a keyword, then that will often determine what kind of
10538 statement we have. */
10539 if (token->type == CPP_KEYWORD)
10541 enum rid keyword = token->keyword;
10543 switch (keyword)
10545 case RID_CASE:
10546 case RID_DEFAULT:
10547 /* Looks like a labeled-statement with a case label.
10548 Parse the label, and then use tail recursion to parse
10549 the statement. */
10550 cp_parser_label_for_labeled_statement (parser, std_attrs);
10551 in_compound = false;
10552 goto restart;
10554 case RID_IF:
10555 case RID_SWITCH:
10556 statement = cp_parser_selection_statement (parser, if_p, chain);
10557 break;
10559 case RID_WHILE:
10560 case RID_DO:
10561 case RID_FOR:
10562 statement = cp_parser_iteration_statement (parser, if_p, false);
10563 break;
10565 case RID_CILK_FOR:
10566 if (!flag_cilkplus)
10568 error_at (cp_lexer_peek_token (parser->lexer)->location,
10569 "-fcilkplus must be enabled to use %<_Cilk_for%>");
10570 cp_lexer_consume_token (parser->lexer);
10571 statement = error_mark_node;
10573 else
10574 statement = cp_parser_cilk_for (parser, integer_zero_node, if_p);
10575 break;
10577 case RID_BREAK:
10578 case RID_CONTINUE:
10579 case RID_RETURN:
10580 case RID_GOTO:
10581 statement = cp_parser_jump_statement (parser);
10582 break;
10584 case RID_CILK_SYNC:
10585 cp_lexer_consume_token (parser->lexer);
10586 if (flag_cilkplus)
10588 tree sync_expr = build_cilk_sync ();
10589 SET_EXPR_LOCATION (sync_expr,
10590 token->location);
10591 statement = finish_expr_stmt (sync_expr);
10593 else
10595 error_at (token->location, "-fcilkplus must be enabled to use"
10596 " %<_Cilk_sync%>");
10597 statement = error_mark_node;
10599 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10600 break;
10602 /* Objective-C++ exception-handling constructs. */
10603 case RID_AT_TRY:
10604 case RID_AT_CATCH:
10605 case RID_AT_FINALLY:
10606 case RID_AT_SYNCHRONIZED:
10607 case RID_AT_THROW:
10608 statement = cp_parser_objc_statement (parser);
10609 break;
10611 case RID_TRY:
10612 statement = cp_parser_try_block (parser);
10613 break;
10615 case RID_NAMESPACE:
10616 /* This must be a namespace alias definition. */
10617 cp_parser_declaration_statement (parser);
10618 return;
10620 case RID_TRANSACTION_ATOMIC:
10621 case RID_TRANSACTION_RELAXED:
10622 case RID_SYNCHRONIZED:
10623 case RID_ATOMIC_NOEXCEPT:
10624 case RID_ATOMIC_CANCEL:
10625 statement = cp_parser_transaction (parser, token);
10626 break;
10627 case RID_TRANSACTION_CANCEL:
10628 statement = cp_parser_transaction_cancel (parser);
10629 break;
10631 default:
10632 /* It might be a keyword like `int' that can start a
10633 declaration-statement. */
10634 break;
10637 else if (token->type == CPP_NAME)
10639 /* If the next token is a `:', then we are looking at a
10640 labeled-statement. */
10641 token = cp_lexer_peek_nth_token (parser->lexer, 2);
10642 if (token->type == CPP_COLON)
10644 /* Looks like a labeled-statement with an ordinary label.
10645 Parse the label, and then use tail recursion to parse
10646 the statement. */
10648 cp_parser_label_for_labeled_statement (parser, std_attrs);
10649 in_compound = false;
10650 goto restart;
10653 /* Anything that starts with a `{' must be a compound-statement. */
10654 else if (token->type == CPP_OPEN_BRACE)
10655 statement = cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
10656 /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
10657 a statement all its own. */
10658 else if (token->type == CPP_PRAGMA)
10660 /* Only certain OpenMP pragmas are attached to statements, and thus
10661 are considered statements themselves. All others are not. In
10662 the context of a compound, accept the pragma as a "statement" and
10663 return so that we can check for a close brace. Otherwise we
10664 require a real statement and must go back and read one. */
10665 if (in_compound)
10666 cp_parser_pragma (parser, pragma_compound, if_p);
10667 else if (!cp_parser_pragma (parser, pragma_stmt, if_p))
10668 goto restart;
10669 return;
10671 else if (token->type == CPP_EOF)
10673 cp_parser_error (parser, "expected statement");
10674 return;
10677 /* Everything else must be a declaration-statement or an
10678 expression-statement. Try for the declaration-statement
10679 first, unless we are looking at a `;', in which case we know that
10680 we have an expression-statement. */
10681 if (!statement)
10683 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
10685 if (std_attrs != NULL_TREE)
10687 /* Attributes should be parsed as part of the the
10688 declaration, so let's un-parse them. */
10689 saved_tokens.rollback();
10690 std_attrs = NULL_TREE;
10693 cp_parser_parse_tentatively (parser);
10694 /* Try to parse the declaration-statement. */
10695 cp_parser_declaration_statement (parser);
10696 /* If that worked, we're done. */
10697 if (cp_parser_parse_definitely (parser))
10698 return;
10700 /* Look for an expression-statement instead. */
10701 statement = cp_parser_expression_statement (parser, in_statement_expr);
10703 /* Handle [[fallthrough]];. */
10704 if (attribute_fallthrough_p (std_attrs))
10706 /* The next token after the fallthrough attribute is ';'. */
10707 if (statement == NULL_TREE)
10709 /* Turn [[fallthrough]]; into FALLTHROUGH ();. */
10710 statement = build_call_expr_internal_loc (statement_location,
10711 IFN_FALLTHROUGH,
10712 void_type_node, 0);
10713 finish_expr_stmt (statement);
10715 else
10716 warning_at (statement_location, OPT_Wattributes,
10717 "%<fallthrough%> attribute not followed by %<;%>");
10718 std_attrs = NULL_TREE;
10722 /* Set the line number for the statement. */
10723 if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
10724 SET_EXPR_LOCATION (statement, statement_location);
10726 /* Allow "[[fallthrough]];", but warn otherwise. */
10727 if (std_attrs != NULL_TREE)
10728 warning_at (attrs_location,
10729 OPT_Wattributes,
10730 "attributes at the beginning of statement are ignored");
10733 /* Parse the label for a labeled-statement, i.e.
10735 identifier :
10736 case constant-expression :
10737 default :
10739 GNU Extension:
10740 case constant-expression ... constant-expression : statement
10742 When a label is parsed without errors, the label is added to the
10743 parse tree by the finish_* functions, so this function doesn't
10744 have to return the label. */
10746 static void
10747 cp_parser_label_for_labeled_statement (cp_parser* parser, tree attributes)
10749 cp_token *token;
10750 tree label = NULL_TREE;
10751 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
10753 /* The next token should be an identifier. */
10754 token = cp_lexer_peek_token (parser->lexer);
10755 if (token->type != CPP_NAME
10756 && token->type != CPP_KEYWORD)
10758 cp_parser_error (parser, "expected labeled-statement");
10759 return;
10762 /* Remember whether this case or a user-defined label is allowed to fall
10763 through to. */
10764 bool fallthrough_p = token->flags & PREV_FALLTHROUGH;
10766 parser->colon_corrects_to_scope_p = false;
10767 switch (token->keyword)
10769 case RID_CASE:
10771 tree expr, expr_hi;
10772 cp_token *ellipsis;
10774 /* Consume the `case' token. */
10775 cp_lexer_consume_token (parser->lexer);
10776 /* Parse the constant-expression. */
10777 expr = cp_parser_constant_expression (parser);
10778 if (check_for_bare_parameter_packs (expr))
10779 expr = error_mark_node;
10781 ellipsis = cp_lexer_peek_token (parser->lexer);
10782 if (ellipsis->type == CPP_ELLIPSIS)
10784 /* Consume the `...' token. */
10785 cp_lexer_consume_token (parser->lexer);
10786 expr_hi = cp_parser_constant_expression (parser);
10787 if (check_for_bare_parameter_packs (expr_hi))
10788 expr_hi = error_mark_node;
10790 /* We don't need to emit warnings here, as the common code
10791 will do this for us. */
10793 else
10794 expr_hi = NULL_TREE;
10796 if (parser->in_switch_statement_p)
10798 tree l = finish_case_label (token->location, expr, expr_hi);
10799 if (l && TREE_CODE (l) == CASE_LABEL_EXPR)
10800 FALLTHROUGH_LABEL_P (CASE_LABEL (l)) = fallthrough_p;
10802 else
10803 error_at (token->location,
10804 "case label %qE not within a switch statement",
10805 expr);
10807 break;
10809 case RID_DEFAULT:
10810 /* Consume the `default' token. */
10811 cp_lexer_consume_token (parser->lexer);
10813 if (parser->in_switch_statement_p)
10815 tree l = finish_case_label (token->location, NULL_TREE, NULL_TREE);
10816 if (l && TREE_CODE (l) == CASE_LABEL_EXPR)
10817 FALLTHROUGH_LABEL_P (CASE_LABEL (l)) = fallthrough_p;
10819 else
10820 error_at (token->location, "case label not within a switch statement");
10821 break;
10823 default:
10824 /* Anything else must be an ordinary label. */
10825 label = finish_label_stmt (cp_parser_identifier (parser));
10826 if (label && TREE_CODE (label) == LABEL_DECL)
10827 FALLTHROUGH_LABEL_P (label) = fallthrough_p;
10828 break;
10831 /* Require the `:' token. */
10832 cp_parser_require (parser, CPP_COLON, RT_COLON);
10834 /* An ordinary label may optionally be followed by attributes.
10835 However, this is only permitted if the attributes are then
10836 followed by a semicolon. This is because, for backward
10837 compatibility, when parsing
10838 lab: __attribute__ ((unused)) int i;
10839 we want the attribute to attach to "i", not "lab". */
10840 if (label != NULL_TREE
10841 && cp_next_tokens_can_be_gnu_attribute_p (parser))
10843 tree attrs;
10844 cp_parser_parse_tentatively (parser);
10845 attrs = cp_parser_gnu_attributes_opt (parser);
10846 if (attrs == NULL_TREE
10847 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
10848 cp_parser_abort_tentative_parse (parser);
10849 else if (!cp_parser_parse_definitely (parser))
10851 else
10852 attributes = chainon (attributes, attrs);
10855 if (attributes != NULL_TREE)
10856 cplus_decl_attributes (&label, attributes, 0);
10858 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
10861 /* Parse an expression-statement.
10863 expression-statement:
10864 expression [opt] ;
10866 Returns the new EXPR_STMT -- or NULL_TREE if the expression
10867 statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
10868 indicates whether this expression-statement is part of an
10869 expression statement. */
10871 static tree
10872 cp_parser_expression_statement (cp_parser* parser, tree in_statement_expr)
10874 tree statement = NULL_TREE;
10875 cp_token *token = cp_lexer_peek_token (parser->lexer);
10876 location_t loc = token->location;
10878 /* There might be attribute fallthrough. */
10879 tree attr = cp_parser_gnu_attributes_opt (parser);
10881 /* If the next token is a ';', then there is no expression
10882 statement. */
10883 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
10885 statement = cp_parser_expression (parser);
10886 if (statement == error_mark_node
10887 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
10889 cp_parser_skip_to_end_of_block_or_statement (parser);
10890 return error_mark_node;
10894 /* Handle [[fallthrough]];. */
10895 if (attribute_fallthrough_p (attr))
10897 /* The next token after the fallthrough attribute is ';'. */
10898 if (statement == NULL_TREE)
10899 /* Turn [[fallthrough]]; into FALLTHROUGH ();. */
10900 statement = build_call_expr_internal_loc (loc, IFN_FALLTHROUGH,
10901 void_type_node, 0);
10902 else
10903 warning_at (loc, OPT_Wattributes,
10904 "%<fallthrough%> attribute not followed by %<;%>");
10905 attr = NULL_TREE;
10908 /* Allow "[[fallthrough]];", but warn otherwise. */
10909 if (attr != NULL_TREE)
10910 warning_at (loc, OPT_Wattributes,
10911 "attributes at the beginning of statement are ignored");
10913 /* Give a helpful message for "A<T>::type t;" and the like. */
10914 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
10915 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
10917 if (TREE_CODE (statement) == SCOPE_REF)
10918 error_at (token->location, "need %<typename%> before %qE because "
10919 "%qT is a dependent scope",
10920 statement, TREE_OPERAND (statement, 0));
10921 else if (is_overloaded_fn (statement)
10922 && DECL_CONSTRUCTOR_P (get_first_fn (statement)))
10924 /* A::A a; */
10925 tree fn = get_first_fn (statement);
10926 error_at (token->location,
10927 "%<%T::%D%> names the constructor, not the type",
10928 DECL_CONTEXT (fn), DECL_NAME (fn));
10932 /* Consume the final `;'. */
10933 cp_parser_consume_semicolon_at_end_of_statement (parser);
10935 if (in_statement_expr
10936 && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
10937 /* This is the final expression statement of a statement
10938 expression. */
10939 statement = finish_stmt_expr_expr (statement, in_statement_expr);
10940 else if (statement)
10941 statement = finish_expr_stmt (statement);
10943 return statement;
10946 /* Parse a compound-statement.
10948 compound-statement:
10949 { statement-seq [opt] }
10951 GNU extension:
10953 compound-statement:
10954 { label-declaration-seq [opt] statement-seq [opt] }
10956 label-declaration-seq:
10957 label-declaration
10958 label-declaration-seq label-declaration
10960 Returns a tree representing the statement. */
10962 static tree
10963 cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr,
10964 int bcs_flags, bool function_body)
10966 tree compound_stmt;
10968 /* Consume the `{'. */
10969 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
10970 return error_mark_node;
10971 if (DECL_DECLARED_CONSTEXPR_P (current_function_decl)
10972 && !function_body && cxx_dialect < cxx14)
10973 pedwarn (input_location, OPT_Wpedantic,
10974 "compound-statement in constexpr function");
10975 /* Begin the compound-statement. */
10976 compound_stmt = begin_compound_stmt (bcs_flags);
10977 /* If the next keyword is `__label__' we have a label declaration. */
10978 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
10979 cp_parser_label_declaration (parser);
10980 /* Parse an (optional) statement-seq. */
10981 cp_parser_statement_seq_opt (parser, in_statement_expr);
10982 /* Finish the compound-statement. */
10983 finish_compound_stmt (compound_stmt);
10984 /* Consume the `}'. */
10985 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
10987 return compound_stmt;
10990 /* Parse an (optional) statement-seq.
10992 statement-seq:
10993 statement
10994 statement-seq [opt] statement */
10996 static void
10997 cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr)
10999 /* Scan statements until there aren't any more. */
11000 while (true)
11002 cp_token *token = cp_lexer_peek_token (parser->lexer);
11004 /* If we are looking at a `}', then we have run out of
11005 statements; the same is true if we have reached the end
11006 of file, or have stumbled upon a stray '@end'. */
11007 if (token->type == CPP_CLOSE_BRACE
11008 || token->type == CPP_EOF
11009 || token->type == CPP_PRAGMA_EOL
11010 || (token->type == CPP_KEYWORD && token->keyword == RID_AT_END))
11011 break;
11013 /* If we are in a compound statement and find 'else' then
11014 something went wrong. */
11015 else if (token->type == CPP_KEYWORD && token->keyword == RID_ELSE)
11017 if (parser->in_statement & IN_IF_STMT)
11018 break;
11019 else
11021 token = cp_lexer_consume_token (parser->lexer);
11022 error_at (token->location, "%<else%> without a previous %<if%>");
11026 /* Parse the statement. */
11027 cp_parser_statement (parser, in_statement_expr, true, NULL);
11031 /* Return true if we're looking at (init; cond), false otherwise. */
11033 static bool
11034 cp_parser_init_statement_p (cp_parser *parser)
11036 /* Save tokens so that we can put them back. */
11037 cp_lexer_save_tokens (parser->lexer);
11039 /* Look for ';' that is not nested in () or {}. */
11040 int ret = cp_parser_skip_to_closing_parenthesis_1 (parser,
11041 /*recovering=*/false,
11042 CPP_SEMICOLON,
11043 /*consume_paren=*/false);
11045 /* Roll back the tokens we skipped. */
11046 cp_lexer_rollback_tokens (parser->lexer);
11048 return ret == -1;
11051 /* Parse a selection-statement.
11053 selection-statement:
11054 if ( init-statement [opt] condition ) statement
11055 if ( init-statement [opt] condition ) statement else statement
11056 switch ( init-statement [opt] condition ) statement
11058 Returns the new IF_STMT or SWITCH_STMT.
11060 If IF_P is not NULL, *IF_P is set to indicate whether the statement
11061 is a (possibly labeled) if statement which is not enclosed in
11062 braces and has an else clause. This is used to implement
11063 -Wparentheses.
11065 CHAIN is a vector of if-else-if conditions. This is used to implement
11066 -Wduplicated-cond. */
11068 static tree
11069 cp_parser_selection_statement (cp_parser* parser, bool *if_p,
11070 vec<tree> *chain)
11072 cp_token *token;
11073 enum rid keyword;
11074 token_indent_info guard_tinfo;
11076 if (if_p != NULL)
11077 *if_p = false;
11079 /* Peek at the next token. */
11080 token = cp_parser_require (parser, CPP_KEYWORD, RT_SELECT);
11081 guard_tinfo = get_token_indent_info (token);
11083 /* See what kind of keyword it is. */
11084 keyword = token->keyword;
11085 switch (keyword)
11087 case RID_IF:
11088 case RID_SWITCH:
11090 tree statement;
11091 tree condition;
11093 bool cx = false;
11094 if (keyword == RID_IF
11095 && cp_lexer_next_token_is_keyword (parser->lexer,
11096 RID_CONSTEXPR))
11098 cx = true;
11099 cp_token *tok = cp_lexer_consume_token (parser->lexer);
11100 if (cxx_dialect < cxx1z && !in_system_header_at (tok->location))
11101 pedwarn (tok->location, 0, "%<if constexpr%> only available "
11102 "with -std=c++1z or -std=gnu++1z");
11105 /* Look for the `('. */
11106 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
11108 cp_parser_skip_to_end_of_statement (parser);
11109 return error_mark_node;
11112 /* Begin the selection-statement. */
11113 if (keyword == RID_IF)
11115 statement = begin_if_stmt ();
11116 IF_STMT_CONSTEXPR_P (statement) = cx;
11118 else
11119 statement = begin_switch_stmt ();
11121 /* Parse the optional init-statement. */
11122 if (cp_parser_init_statement_p (parser))
11124 tree decl;
11125 if (cxx_dialect < cxx1z)
11126 pedwarn (cp_lexer_peek_token (parser->lexer)->location, 0,
11127 "init-statement in selection statements only available "
11128 "with -std=c++1z or -std=gnu++1z");
11129 cp_parser_init_statement (parser, &decl);
11132 /* Parse the condition. */
11133 condition = cp_parser_condition (parser);
11134 /* Look for the `)'. */
11135 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
11136 cp_parser_skip_to_closing_parenthesis (parser, true, false,
11137 /*consume_paren=*/true);
11139 if (keyword == RID_IF)
11141 bool nested_if;
11142 unsigned char in_statement;
11144 /* Add the condition. */
11145 condition = finish_if_stmt_cond (condition, statement);
11147 if (warn_duplicated_cond)
11148 warn_duplicated_cond_add_or_warn (token->location, condition,
11149 &chain);
11151 /* Parse the then-clause. */
11152 in_statement = parser->in_statement;
11153 parser->in_statement |= IN_IF_STMT;
11155 /* Outside a template, the non-selected branch of a constexpr
11156 if is a 'discarded statement', i.e. unevaluated. */
11157 bool was_discarded = in_discarded_stmt;
11158 bool discard_then = (cx && !processing_template_decl
11159 && integer_zerop (condition));
11160 if (discard_then)
11162 in_discarded_stmt = true;
11163 ++c_inhibit_evaluation_warnings;
11166 cp_parser_implicitly_scoped_statement (parser, &nested_if,
11167 guard_tinfo);
11169 parser->in_statement = in_statement;
11171 finish_then_clause (statement);
11173 if (discard_then)
11175 THEN_CLAUSE (statement) = NULL_TREE;
11176 in_discarded_stmt = was_discarded;
11177 --c_inhibit_evaluation_warnings;
11180 /* If the next token is `else', parse the else-clause. */
11181 if (cp_lexer_next_token_is_keyword (parser->lexer,
11182 RID_ELSE))
11184 bool discard_else = (cx && !processing_template_decl
11185 && integer_nonzerop (condition));
11186 if (discard_else)
11188 in_discarded_stmt = true;
11189 ++c_inhibit_evaluation_warnings;
11192 guard_tinfo
11193 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
11194 /* Consume the `else' keyword. */
11195 cp_lexer_consume_token (parser->lexer);
11196 if (warn_duplicated_cond)
11198 if (cp_lexer_next_token_is_keyword (parser->lexer,
11199 RID_IF)
11200 && chain == NULL)
11202 /* We've got "if (COND) else if (COND2)". Start
11203 the condition chain and add COND as the first
11204 element. */
11205 chain = new vec<tree> ();
11206 if (!CONSTANT_CLASS_P (condition)
11207 && !TREE_SIDE_EFFECTS (condition))
11209 /* Wrap it in a NOP_EXPR so that we can set the
11210 location of the condition. */
11211 tree e = build1 (NOP_EXPR, TREE_TYPE (condition),
11212 condition);
11213 SET_EXPR_LOCATION (e, token->location);
11214 chain->safe_push (e);
11217 else if (!cp_lexer_next_token_is_keyword (parser->lexer,
11218 RID_IF))
11220 /* This is if-else without subsequent if. Zap the
11221 condition chain; we would have already warned at
11222 this point. */
11223 delete chain;
11224 chain = NULL;
11227 begin_else_clause (statement);
11228 /* Parse the else-clause. */
11229 cp_parser_implicitly_scoped_statement (parser, NULL,
11230 guard_tinfo, chain);
11232 finish_else_clause (statement);
11234 /* If we are currently parsing a then-clause, then
11235 IF_P will not be NULL. We set it to true to
11236 indicate that this if statement has an else clause.
11237 This may trigger the Wparentheses warning below
11238 when we get back up to the parent if statement. */
11239 if (if_p != NULL)
11240 *if_p = true;
11242 if (discard_else)
11244 ELSE_CLAUSE (statement) = NULL_TREE;
11245 in_discarded_stmt = was_discarded;
11246 --c_inhibit_evaluation_warnings;
11249 else
11251 /* This if statement does not have an else clause. If
11252 NESTED_IF is true, then the then-clause has an if
11253 statement which does have an else clause. We warn
11254 about the potential ambiguity. */
11255 if (nested_if)
11256 warning_at (EXPR_LOCATION (statement), OPT_Wdangling_else,
11257 "suggest explicit braces to avoid ambiguous"
11258 " %<else%>");
11259 if (warn_duplicated_cond)
11261 /* We don't need the condition chain anymore. */
11262 delete chain;
11263 chain = NULL;
11267 /* Now we're all done with the if-statement. */
11268 finish_if_stmt (statement);
11270 else
11272 bool in_switch_statement_p;
11273 unsigned char in_statement;
11275 /* Add the condition. */
11276 finish_switch_cond (condition, statement);
11278 /* Parse the body of the switch-statement. */
11279 in_switch_statement_p = parser->in_switch_statement_p;
11280 in_statement = parser->in_statement;
11281 parser->in_switch_statement_p = true;
11282 parser->in_statement |= IN_SWITCH_STMT;
11283 cp_parser_implicitly_scoped_statement (parser, if_p,
11284 guard_tinfo);
11285 parser->in_switch_statement_p = in_switch_statement_p;
11286 parser->in_statement = in_statement;
11288 /* Now we're all done with the switch-statement. */
11289 finish_switch_stmt (statement);
11292 return statement;
11294 break;
11296 default:
11297 cp_parser_error (parser, "expected selection-statement");
11298 return error_mark_node;
11302 /* Parse a condition.
11304 condition:
11305 expression
11306 type-specifier-seq declarator = initializer-clause
11307 type-specifier-seq declarator braced-init-list
11309 GNU Extension:
11311 condition:
11312 type-specifier-seq declarator asm-specification [opt]
11313 attributes [opt] = assignment-expression
11315 Returns the expression that should be tested. */
11317 static tree
11318 cp_parser_condition (cp_parser* parser)
11320 cp_decl_specifier_seq type_specifiers;
11321 const char *saved_message;
11322 int declares_class_or_enum;
11324 /* Try the declaration first. */
11325 cp_parser_parse_tentatively (parser);
11326 /* New types are not allowed in the type-specifier-seq for a
11327 condition. */
11328 saved_message = parser->type_definition_forbidden_message;
11329 parser->type_definition_forbidden_message
11330 = G_("types may not be defined in conditions");
11331 /* Parse the type-specifier-seq. */
11332 cp_parser_decl_specifier_seq (parser,
11333 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR,
11334 &type_specifiers,
11335 &declares_class_or_enum);
11336 /* Restore the saved message. */
11337 parser->type_definition_forbidden_message = saved_message;
11338 /* If all is well, we might be looking at a declaration. */
11339 if (!cp_parser_error_occurred (parser))
11341 tree decl;
11342 tree asm_specification;
11343 tree attributes;
11344 cp_declarator *declarator;
11345 tree initializer = NULL_TREE;
11347 /* Parse the declarator. */
11348 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
11349 /*ctor_dtor_or_conv_p=*/NULL,
11350 /*parenthesized_p=*/NULL,
11351 /*member_p=*/false,
11352 /*friend_p=*/false);
11353 /* Parse the attributes. */
11354 attributes = cp_parser_attributes_opt (parser);
11355 /* Parse the asm-specification. */
11356 asm_specification = cp_parser_asm_specification_opt (parser);
11357 /* If the next token is not an `=' or '{', then we might still be
11358 looking at an expression. For example:
11360 if (A(a).x)
11362 looks like a decl-specifier-seq and a declarator -- but then
11363 there is no `=', so this is an expression. */
11364 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
11365 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
11366 cp_parser_simulate_error (parser);
11368 /* If we did see an `=' or '{', then we are looking at a declaration
11369 for sure. */
11370 if (cp_parser_parse_definitely (parser))
11372 tree pushed_scope;
11373 bool non_constant_p;
11374 int flags = LOOKUP_ONLYCONVERTING;
11376 /* Create the declaration. */
11377 decl = start_decl (declarator, &type_specifiers,
11378 /*initialized_p=*/true,
11379 attributes, /*prefix_attributes=*/NULL_TREE,
11380 &pushed_scope);
11382 /* Parse the initializer. */
11383 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
11385 initializer = cp_parser_braced_list (parser, &non_constant_p);
11386 CONSTRUCTOR_IS_DIRECT_INIT (initializer) = 1;
11387 flags = 0;
11389 else
11391 /* Consume the `='. */
11392 cp_parser_require (parser, CPP_EQ, RT_EQ);
11393 initializer = cp_parser_initializer_clause (parser, &non_constant_p);
11395 if (BRACE_ENCLOSED_INITIALIZER_P (initializer))
11396 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
11398 /* Process the initializer. */
11399 cp_finish_decl (decl,
11400 initializer, !non_constant_p,
11401 asm_specification,
11402 flags);
11404 if (pushed_scope)
11405 pop_scope (pushed_scope);
11407 return convert_from_reference (decl);
11410 /* If we didn't even get past the declarator successfully, we are
11411 definitely not looking at a declaration. */
11412 else
11413 cp_parser_abort_tentative_parse (parser);
11415 /* Otherwise, we are looking at an expression. */
11416 return cp_parser_expression (parser);
11419 /* Parses a for-statement or range-for-statement until the closing ')',
11420 not included. */
11422 static tree
11423 cp_parser_for (cp_parser *parser, bool ivdep)
11425 tree init, scope, decl;
11426 bool is_range_for;
11428 /* Begin the for-statement. */
11429 scope = begin_for_scope (&init);
11431 /* Parse the initialization. */
11432 is_range_for = cp_parser_init_statement (parser, &decl);
11434 if (is_range_for)
11435 return cp_parser_range_for (parser, scope, init, decl, ivdep);
11436 else
11437 return cp_parser_c_for (parser, scope, init, ivdep);
11440 static tree
11441 cp_parser_c_for (cp_parser *parser, tree scope, tree init, bool ivdep)
11443 /* Normal for loop */
11444 tree condition = NULL_TREE;
11445 tree expression = NULL_TREE;
11446 tree stmt;
11448 stmt = begin_for_stmt (scope, init);
11449 /* The init-statement has already been parsed in
11450 cp_parser_init_statement, so no work is needed here. */
11451 finish_init_stmt (stmt);
11453 /* If there's a condition, process it. */
11454 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
11455 condition = cp_parser_condition (parser);
11456 else if (ivdep)
11458 cp_parser_error (parser, "missing loop condition in loop with "
11459 "%<GCC ivdep%> pragma");
11460 condition = error_mark_node;
11462 finish_for_cond (condition, stmt, ivdep);
11463 /* Look for the `;'. */
11464 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
11466 /* If there's an expression, process it. */
11467 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
11468 expression = cp_parser_expression (parser);
11469 finish_for_expr (expression, stmt);
11471 return stmt;
11474 /* Tries to parse a range-based for-statement:
11476 range-based-for:
11477 decl-specifier-seq declarator : expression
11479 The decl-specifier-seq declarator and the `:' are already parsed by
11480 cp_parser_init_statement. If processing_template_decl it returns a
11481 newly created RANGE_FOR_STMT; if not, it is converted to a
11482 regular FOR_STMT. */
11484 static tree
11485 cp_parser_range_for (cp_parser *parser, tree scope, tree init, tree range_decl,
11486 bool ivdep)
11488 tree stmt, range_expr;
11489 auto_vec <cxx_binding *, 16> bindings;
11490 auto_vec <tree, 16> names;
11491 tree decomp_first_name = NULL_TREE;
11492 unsigned int decomp_cnt = 0;
11494 /* Get the range declaration momentarily out of the way so that
11495 the range expression doesn't clash with it. */
11496 if (range_decl != error_mark_node)
11498 if (DECL_HAS_VALUE_EXPR_P (range_decl))
11500 tree v = DECL_VALUE_EXPR (range_decl);
11501 /* For decomposition declaration get all of the corresponding
11502 declarations out of the way. */
11503 if (TREE_CODE (v) == ARRAY_REF
11504 && VAR_P (TREE_OPERAND (v, 0))
11505 && DECL_DECOMPOSITION_P (TREE_OPERAND (v, 0)))
11507 tree d = range_decl;
11508 range_decl = TREE_OPERAND (v, 0);
11509 decomp_cnt = tree_to_uhwi (TREE_OPERAND (v, 1)) + 1;
11510 decomp_first_name = d;
11511 for (unsigned int i = 0; i < decomp_cnt; i++, d = DECL_CHAIN (d))
11513 tree name = DECL_NAME (d);
11514 names.safe_push (name);
11515 bindings.safe_push (IDENTIFIER_BINDING (name));
11516 IDENTIFIER_BINDING (name)
11517 = IDENTIFIER_BINDING (name)->previous;
11521 if (names.is_empty ())
11523 tree name = DECL_NAME (range_decl);
11524 names.safe_push (name);
11525 bindings.safe_push (IDENTIFIER_BINDING (name));
11526 IDENTIFIER_BINDING (name) = IDENTIFIER_BINDING (name)->previous;
11530 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
11532 bool expr_non_constant_p;
11533 range_expr = cp_parser_braced_list (parser, &expr_non_constant_p);
11535 else
11536 range_expr = cp_parser_expression (parser);
11538 /* Put the range declaration(s) back into scope. */
11539 for (unsigned int i = 0; i < names.length (); i++)
11541 cxx_binding *binding = bindings[i];
11542 binding->previous = IDENTIFIER_BINDING (names[i]);
11543 IDENTIFIER_BINDING (names[i]) = binding;
11546 /* If in template, STMT is converted to a normal for-statement
11547 at instantiation. If not, it is done just ahead. */
11548 if (processing_template_decl)
11550 if (check_for_bare_parameter_packs (range_expr))
11551 range_expr = error_mark_node;
11552 stmt = begin_range_for_stmt (scope, init);
11553 if (ivdep)
11554 RANGE_FOR_IVDEP (stmt) = 1;
11555 finish_range_for_decl (stmt, range_decl, range_expr);
11556 if (!type_dependent_expression_p (range_expr)
11557 /* do_auto_deduction doesn't mess with template init-lists. */
11558 && !BRACE_ENCLOSED_INITIALIZER_P (range_expr))
11559 do_range_for_auto_deduction (range_decl, range_expr);
11561 else
11563 stmt = begin_for_stmt (scope, init);
11564 stmt = cp_convert_range_for (stmt, range_decl, range_expr,
11565 decomp_first_name, decomp_cnt, ivdep);
11567 return stmt;
11570 /* Subroutine of cp_convert_range_for: given the initializer expression,
11571 builds up the range temporary. */
11573 static tree
11574 build_range_temp (tree range_expr)
11576 tree range_type, range_temp;
11578 /* Find out the type deduced by the declaration
11579 `auto &&__range = range_expr'. */
11580 range_type = cp_build_reference_type (make_auto (), true);
11581 range_type = do_auto_deduction (range_type, range_expr,
11582 type_uses_auto (range_type));
11584 /* Create the __range variable. */
11585 range_temp = build_decl (input_location, VAR_DECL,
11586 get_identifier ("__for_range"), range_type);
11587 TREE_USED (range_temp) = 1;
11588 DECL_ARTIFICIAL (range_temp) = 1;
11590 return range_temp;
11593 /* Used by cp_parser_range_for in template context: we aren't going to
11594 do a full conversion yet, but we still need to resolve auto in the
11595 type of the for-range-declaration if present. This is basically
11596 a shortcut version of cp_convert_range_for. */
11598 static void
11599 do_range_for_auto_deduction (tree decl, tree range_expr)
11601 tree auto_node = type_uses_auto (TREE_TYPE (decl));
11602 if (auto_node)
11604 tree begin_dummy, end_dummy, range_temp, iter_type, iter_decl;
11605 range_temp = convert_from_reference (build_range_temp (range_expr));
11606 iter_type = (cp_parser_perform_range_for_lookup
11607 (range_temp, &begin_dummy, &end_dummy));
11608 if (iter_type)
11610 iter_decl = build_decl (input_location, VAR_DECL, NULL_TREE,
11611 iter_type);
11612 iter_decl = build_x_indirect_ref (input_location, iter_decl, RO_NULL,
11613 tf_warning_or_error);
11614 TREE_TYPE (decl) = do_auto_deduction (TREE_TYPE (decl),
11615 iter_decl, auto_node);
11620 /* Converts a range-based for-statement into a normal
11621 for-statement, as per the definition.
11623 for (RANGE_DECL : RANGE_EXPR)
11624 BLOCK
11626 should be equivalent to:
11629 auto &&__range = RANGE_EXPR;
11630 for (auto __begin = BEGIN_EXPR, end = END_EXPR;
11631 __begin != __end;
11632 ++__begin)
11634 RANGE_DECL = *__begin;
11635 BLOCK
11639 If RANGE_EXPR is an array:
11640 BEGIN_EXPR = __range
11641 END_EXPR = __range + ARRAY_SIZE(__range)
11642 Else if RANGE_EXPR has a member 'begin' or 'end':
11643 BEGIN_EXPR = __range.begin()
11644 END_EXPR = __range.end()
11645 Else:
11646 BEGIN_EXPR = begin(__range)
11647 END_EXPR = end(__range);
11649 If __range has a member 'begin' but not 'end', or vice versa, we must
11650 still use the second alternative (it will surely fail, however).
11651 When calling begin()/end() in the third alternative we must use
11652 argument dependent lookup, but always considering 'std' as an associated
11653 namespace. */
11655 tree
11656 cp_convert_range_for (tree statement, tree range_decl, tree range_expr,
11657 tree decomp_first_name, unsigned int decomp_cnt,
11658 bool ivdep)
11660 tree begin, end;
11661 tree iter_type, begin_expr, end_expr;
11662 tree condition, expression;
11664 if (range_decl == error_mark_node || range_expr == error_mark_node)
11665 /* If an error happened previously do nothing or else a lot of
11666 unhelpful errors would be issued. */
11667 begin_expr = end_expr = iter_type = error_mark_node;
11668 else
11670 tree range_temp;
11672 if (VAR_P (range_expr)
11673 && array_of_runtime_bound_p (TREE_TYPE (range_expr)))
11674 /* Can't bind a reference to an array of runtime bound. */
11675 range_temp = range_expr;
11676 else
11678 range_temp = build_range_temp (range_expr);
11679 pushdecl (range_temp);
11680 cp_finish_decl (range_temp, range_expr,
11681 /*is_constant_init*/false, NULL_TREE,
11682 LOOKUP_ONLYCONVERTING);
11683 range_temp = convert_from_reference (range_temp);
11685 iter_type = cp_parser_perform_range_for_lookup (range_temp,
11686 &begin_expr, &end_expr);
11689 /* The new for initialization statement. */
11690 begin = build_decl (input_location, VAR_DECL,
11691 get_identifier ("__for_begin"), iter_type);
11692 TREE_USED (begin) = 1;
11693 DECL_ARTIFICIAL (begin) = 1;
11694 pushdecl (begin);
11695 cp_finish_decl (begin, begin_expr,
11696 /*is_constant_init*/false, NULL_TREE,
11697 LOOKUP_ONLYCONVERTING);
11699 if (cxx_dialect >= cxx1z)
11700 iter_type = cv_unqualified (TREE_TYPE (end_expr));
11701 end = build_decl (input_location, VAR_DECL,
11702 get_identifier ("__for_end"), iter_type);
11703 TREE_USED (end) = 1;
11704 DECL_ARTIFICIAL (end) = 1;
11705 pushdecl (end);
11706 cp_finish_decl (end, end_expr,
11707 /*is_constant_init*/false, NULL_TREE,
11708 LOOKUP_ONLYCONVERTING);
11710 finish_init_stmt (statement);
11712 /* The new for condition. */
11713 condition = build_x_binary_op (input_location, NE_EXPR,
11714 begin, ERROR_MARK,
11715 end, ERROR_MARK,
11716 NULL, tf_warning_or_error);
11717 finish_for_cond (condition, statement, ivdep);
11719 /* The new increment expression. */
11720 expression = finish_unary_op_expr (input_location,
11721 PREINCREMENT_EXPR, begin,
11722 tf_warning_or_error);
11723 finish_for_expr (expression, statement);
11725 /* The declaration is initialized with *__begin inside the loop body. */
11726 cp_finish_decl (range_decl,
11727 build_x_indirect_ref (input_location, begin, RO_NULL,
11728 tf_warning_or_error),
11729 /*is_constant_init*/false, NULL_TREE,
11730 LOOKUP_ONLYCONVERTING);
11731 if (VAR_P (range_decl) && DECL_DECOMPOSITION_P (range_decl))
11732 cp_finish_decomp (range_decl, decomp_first_name, decomp_cnt);
11734 return statement;
11737 /* Solves BEGIN_EXPR and END_EXPR as described in cp_convert_range_for.
11738 We need to solve both at the same time because the method used
11739 depends on the existence of members begin or end.
11740 Returns the type deduced for the iterator expression. */
11742 static tree
11743 cp_parser_perform_range_for_lookup (tree range, tree *begin, tree *end)
11745 if (error_operand_p (range))
11747 *begin = *end = error_mark_node;
11748 return error_mark_node;
11751 if (!COMPLETE_TYPE_P (complete_type (TREE_TYPE (range))))
11753 error ("range-based %<for%> expression of type %qT "
11754 "has incomplete type", TREE_TYPE (range));
11755 *begin = *end = error_mark_node;
11756 return error_mark_node;
11758 if (TREE_CODE (TREE_TYPE (range)) == ARRAY_TYPE)
11760 /* If RANGE is an array, we will use pointer arithmetic. */
11761 *begin = decay_conversion (range, tf_warning_or_error);
11762 *end = build_binary_op (input_location, PLUS_EXPR,
11763 range,
11764 array_type_nelts_top (TREE_TYPE (range)),
11766 return TREE_TYPE (*begin);
11768 else
11770 /* If it is not an array, we must do a bit of magic. */
11771 tree id_begin, id_end;
11772 tree member_begin, member_end;
11774 *begin = *end = error_mark_node;
11776 id_begin = get_identifier ("begin");
11777 id_end = get_identifier ("end");
11778 member_begin = lookup_member (TREE_TYPE (range), id_begin,
11779 /*protect=*/2, /*want_type=*/false,
11780 tf_warning_or_error);
11781 member_end = lookup_member (TREE_TYPE (range), id_end,
11782 /*protect=*/2, /*want_type=*/false,
11783 tf_warning_or_error);
11785 if (member_begin != NULL_TREE || member_end != NULL_TREE)
11787 /* Use the member functions. */
11788 if (member_begin != NULL_TREE)
11789 *begin = cp_parser_range_for_member_function (range, id_begin);
11790 else
11791 error ("range-based %<for%> expression of type %qT has an "
11792 "%<end%> member but not a %<begin%>", TREE_TYPE (range));
11794 if (member_end != NULL_TREE)
11795 *end = cp_parser_range_for_member_function (range, id_end);
11796 else
11797 error ("range-based %<for%> expression of type %qT has a "
11798 "%<begin%> member but not an %<end%>", TREE_TYPE (range));
11800 else
11802 /* Use global functions with ADL. */
11803 vec<tree, va_gc> *vec;
11804 vec = make_tree_vector ();
11806 vec_safe_push (vec, range);
11808 member_begin = perform_koenig_lookup (id_begin, vec,
11809 tf_warning_or_error);
11810 *begin = finish_call_expr (member_begin, &vec, false, true,
11811 tf_warning_or_error);
11812 member_end = perform_koenig_lookup (id_end, vec,
11813 tf_warning_or_error);
11814 *end = finish_call_expr (member_end, &vec, false, true,
11815 tf_warning_or_error);
11817 release_tree_vector (vec);
11820 /* Last common checks. */
11821 if (*begin == error_mark_node || *end == error_mark_node)
11823 /* If one of the expressions is an error do no more checks. */
11824 *begin = *end = error_mark_node;
11825 return error_mark_node;
11827 else if (type_dependent_expression_p (*begin)
11828 || type_dependent_expression_p (*end))
11829 /* Can happen, when, eg, in a template context, Koenig lookup
11830 can't resolve begin/end (c++/58503). */
11831 return NULL_TREE;
11832 else
11834 tree iter_type = cv_unqualified (TREE_TYPE (*begin));
11835 /* The unqualified type of the __begin and __end temporaries should
11836 be the same, as required by the multiple auto declaration. */
11837 if (!same_type_p (iter_type, cv_unqualified (TREE_TYPE (*end))))
11839 if (cxx_dialect >= cxx1z
11840 && (build_x_binary_op (input_location, NE_EXPR,
11841 *begin, ERROR_MARK,
11842 *end, ERROR_MARK,
11843 NULL, tf_none)
11844 != error_mark_node))
11845 /* P0184R0 allows __begin and __end to have different types,
11846 but make sure they are comparable so we can give a better
11847 diagnostic. */;
11848 else
11849 error ("inconsistent begin/end types in range-based %<for%> "
11850 "statement: %qT and %qT",
11851 TREE_TYPE (*begin), TREE_TYPE (*end));
11853 return iter_type;
11858 /* Helper function for cp_parser_perform_range_for_lookup.
11859 Builds a tree for RANGE.IDENTIFIER(). */
11861 static tree
11862 cp_parser_range_for_member_function (tree range, tree identifier)
11864 tree member, res;
11865 vec<tree, va_gc> *vec;
11867 member = finish_class_member_access_expr (range, identifier,
11868 false, tf_warning_or_error);
11869 if (member == error_mark_node)
11870 return error_mark_node;
11872 vec = make_tree_vector ();
11873 res = finish_call_expr (member, &vec,
11874 /*disallow_virtual=*/false,
11875 /*koenig_p=*/false,
11876 tf_warning_or_error);
11877 release_tree_vector (vec);
11878 return res;
11881 /* Parse an iteration-statement.
11883 iteration-statement:
11884 while ( condition ) statement
11885 do statement while ( expression ) ;
11886 for ( init-statement condition [opt] ; expression [opt] )
11887 statement
11889 Returns the new WHILE_STMT, DO_STMT, FOR_STMT or RANGE_FOR_STMT. */
11891 static tree
11892 cp_parser_iteration_statement (cp_parser* parser, bool *if_p, bool ivdep)
11894 cp_token *token;
11895 enum rid keyword;
11896 tree statement;
11897 unsigned char in_statement;
11898 token_indent_info guard_tinfo;
11900 /* Peek at the next token. */
11901 token = cp_parser_require (parser, CPP_KEYWORD, RT_INTERATION);
11902 if (!token)
11903 return error_mark_node;
11905 guard_tinfo = get_token_indent_info (token);
11907 /* Remember whether or not we are already within an iteration
11908 statement. */
11909 in_statement = parser->in_statement;
11911 /* See what kind of keyword it is. */
11912 keyword = token->keyword;
11913 switch (keyword)
11915 case RID_WHILE:
11917 tree condition;
11919 /* Begin the while-statement. */
11920 statement = begin_while_stmt ();
11921 /* Look for the `('. */
11922 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
11923 /* Parse the condition. */
11924 condition = cp_parser_condition (parser);
11925 finish_while_stmt_cond (condition, statement, ivdep);
11926 /* Look for the `)'. */
11927 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
11928 /* Parse the dependent statement. */
11929 parser->in_statement = IN_ITERATION_STMT;
11930 cp_parser_already_scoped_statement (parser, if_p, guard_tinfo);
11931 parser->in_statement = in_statement;
11932 /* We're done with the while-statement. */
11933 finish_while_stmt (statement);
11935 break;
11937 case RID_DO:
11939 tree expression;
11941 /* Begin the do-statement. */
11942 statement = begin_do_stmt ();
11943 /* Parse the body of the do-statement. */
11944 parser->in_statement = IN_ITERATION_STMT;
11945 cp_parser_implicitly_scoped_statement (parser, NULL, guard_tinfo);
11946 parser->in_statement = in_statement;
11947 finish_do_body (statement);
11948 /* Look for the `while' keyword. */
11949 cp_parser_require_keyword (parser, RID_WHILE, RT_WHILE);
11950 /* Look for the `('. */
11951 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
11952 /* Parse the expression. */
11953 expression = cp_parser_expression (parser);
11954 /* We're done with the do-statement. */
11955 finish_do_stmt (expression, statement, ivdep);
11956 /* Look for the `)'. */
11957 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
11958 /* Look for the `;'. */
11959 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
11961 break;
11963 case RID_FOR:
11965 /* Look for the `('. */
11966 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
11968 statement = cp_parser_for (parser, ivdep);
11970 /* Look for the `)'. */
11971 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
11973 /* Parse the body of the for-statement. */
11974 parser->in_statement = IN_ITERATION_STMT;
11975 cp_parser_already_scoped_statement (parser, if_p, guard_tinfo);
11976 parser->in_statement = in_statement;
11978 /* We're done with the for-statement. */
11979 finish_for_stmt (statement);
11981 break;
11983 default:
11984 cp_parser_error (parser, "expected iteration-statement");
11985 statement = error_mark_node;
11986 break;
11989 return statement;
11992 /* Parse a init-statement or the declarator of a range-based-for.
11993 Returns true if a range-based-for declaration is seen.
11995 init-statement:
11996 expression-statement
11997 simple-declaration */
11999 static bool
12000 cp_parser_init_statement (cp_parser* parser, tree *decl)
12002 /* If the next token is a `;', then we have an empty
12003 expression-statement. Grammatically, this is also a
12004 simple-declaration, but an invalid one, because it does not
12005 declare anything. Therefore, if we did not handle this case
12006 specially, we would issue an error message about an invalid
12007 declaration. */
12008 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
12010 bool is_range_for = false;
12011 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
12013 /* A colon is used in range-based for. */
12014 parser->colon_corrects_to_scope_p = false;
12016 /* We're going to speculatively look for a declaration, falling back
12017 to an expression, if necessary. */
12018 cp_parser_parse_tentatively (parser);
12019 /* Parse the declaration. */
12020 cp_parser_simple_declaration (parser,
12021 /*function_definition_allowed_p=*/false,
12022 decl);
12023 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
12024 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
12026 /* It is a range-for, consume the ':' */
12027 cp_lexer_consume_token (parser->lexer);
12028 is_range_for = true;
12029 if (cxx_dialect < cxx11)
12031 pedwarn (cp_lexer_peek_token (parser->lexer)->location, 0,
12032 "range-based %<for%> loops only available with "
12033 "-std=c++11 or -std=gnu++11");
12034 *decl = error_mark_node;
12037 else
12038 /* The ';' is not consumed yet because we told
12039 cp_parser_simple_declaration not to. */
12040 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12042 if (cp_parser_parse_definitely (parser))
12043 return is_range_for;
12044 /* If the tentative parse failed, then we shall need to look for an
12045 expression-statement. */
12047 /* If we are here, it is an expression-statement. */
12048 cp_parser_expression_statement (parser, NULL_TREE);
12049 return false;
12052 /* Parse a jump-statement.
12054 jump-statement:
12055 break ;
12056 continue ;
12057 return expression [opt] ;
12058 return braced-init-list ;
12059 goto identifier ;
12061 GNU extension:
12063 jump-statement:
12064 goto * expression ;
12066 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR. */
12068 static tree
12069 cp_parser_jump_statement (cp_parser* parser)
12071 tree statement = error_mark_node;
12072 cp_token *token;
12073 enum rid keyword;
12074 unsigned char in_statement;
12076 /* Peek at the next token. */
12077 token = cp_parser_require (parser, CPP_KEYWORD, RT_JUMP);
12078 if (!token)
12079 return error_mark_node;
12081 /* See what kind of keyword it is. */
12082 keyword = token->keyword;
12083 switch (keyword)
12085 case RID_BREAK:
12086 in_statement = parser->in_statement & ~IN_IF_STMT;
12087 switch (in_statement)
12089 case 0:
12090 error_at (token->location, "break statement not within loop or switch");
12091 break;
12092 default:
12093 gcc_assert ((in_statement & IN_SWITCH_STMT)
12094 || in_statement == IN_ITERATION_STMT);
12095 statement = finish_break_stmt ();
12096 if (in_statement == IN_ITERATION_STMT)
12097 break_maybe_infinite_loop ();
12098 break;
12099 case IN_OMP_BLOCK:
12100 error_at (token->location, "invalid exit from OpenMP structured block");
12101 break;
12102 case IN_OMP_FOR:
12103 error_at (token->location, "break statement used with OpenMP for loop");
12104 break;
12105 case IN_CILK_SIMD_FOR:
12106 error_at (token->location, "break statement used with Cilk Plus for loop");
12107 break;
12109 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12110 break;
12112 case RID_CONTINUE:
12113 switch (parser->in_statement & ~(IN_SWITCH_STMT | IN_IF_STMT))
12115 case 0:
12116 error_at (token->location, "continue statement not within a loop");
12117 break;
12118 case IN_CILK_SIMD_FOR:
12119 error_at (token->location,
12120 "continue statement within %<#pragma simd%> loop body");
12121 /* Fall through. */
12122 case IN_ITERATION_STMT:
12123 case IN_OMP_FOR:
12124 statement = finish_continue_stmt ();
12125 break;
12126 case IN_OMP_BLOCK:
12127 error_at (token->location, "invalid exit from OpenMP structured block");
12128 break;
12129 default:
12130 gcc_unreachable ();
12132 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12133 break;
12135 case RID_RETURN:
12137 tree expr;
12138 bool expr_non_constant_p;
12140 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12142 cp_lexer_set_source_position (parser->lexer);
12143 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
12144 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
12146 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
12147 expr = cp_parser_expression (parser);
12148 else
12149 /* If the next token is a `;', then there is no
12150 expression. */
12151 expr = NULL_TREE;
12152 /* Build the return-statement. */
12153 if (current_function_auto_return_pattern && in_discarded_stmt)
12154 /* Don't deduce from a discarded return statement. */;
12155 else
12156 statement = finish_return_stmt (expr);
12157 /* Look for the final `;'. */
12158 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12160 break;
12162 case RID_GOTO:
12163 if (parser->in_function_body
12164 && DECL_DECLARED_CONSTEXPR_P (current_function_decl))
12166 error ("%<goto%> in %<constexpr%> function");
12167 cp_function_chain->invalid_constexpr = true;
12170 /* Create the goto-statement. */
12171 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
12173 /* Issue a warning about this use of a GNU extension. */
12174 pedwarn (token->location, OPT_Wpedantic, "ISO C++ forbids computed gotos");
12175 /* Consume the '*' token. */
12176 cp_lexer_consume_token (parser->lexer);
12177 /* Parse the dependent expression. */
12178 finish_goto_stmt (cp_parser_expression (parser));
12180 else
12181 finish_goto_stmt (cp_parser_identifier (parser));
12182 /* Look for the final `;'. */
12183 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12184 break;
12186 default:
12187 cp_parser_error (parser, "expected jump-statement");
12188 break;
12191 return statement;
12194 /* Parse a declaration-statement.
12196 declaration-statement:
12197 block-declaration */
12199 static void
12200 cp_parser_declaration_statement (cp_parser* parser)
12202 void *p;
12204 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
12205 p = obstack_alloc (&declarator_obstack, 0);
12207 /* Parse the block-declaration. */
12208 cp_parser_block_declaration (parser, /*statement_p=*/true);
12210 /* Free any declarators allocated. */
12211 obstack_free (&declarator_obstack, p);
12214 /* Some dependent statements (like `if (cond) statement'), are
12215 implicitly in their own scope. In other words, if the statement is
12216 a single statement (as opposed to a compound-statement), it is
12217 none-the-less treated as if it were enclosed in braces. Any
12218 declarations appearing in the dependent statement are out of scope
12219 after control passes that point. This function parses a statement,
12220 but ensures that is in its own scope, even if it is not a
12221 compound-statement.
12223 If IF_P is not NULL, *IF_P is set to indicate whether the statement
12224 is a (possibly labeled) if statement which is not enclosed in
12225 braces and has an else clause. This is used to implement
12226 -Wparentheses.
12228 CHAIN is a vector of if-else-if conditions. This is used to implement
12229 -Wduplicated-cond.
12231 Returns the new statement. */
12233 static tree
12234 cp_parser_implicitly_scoped_statement (cp_parser* parser, bool *if_p,
12235 const token_indent_info &guard_tinfo,
12236 vec<tree> *chain)
12238 tree statement;
12239 location_t body_loc = cp_lexer_peek_token (parser->lexer)->location;
12240 token_indent_info body_tinfo
12241 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
12243 if (if_p != NULL)
12244 *if_p = false;
12246 /* Mark if () ; with a special NOP_EXPR. */
12247 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
12249 cp_lexer_consume_token (parser->lexer);
12250 statement = add_stmt (build_empty_stmt (body_loc));
12252 if (guard_tinfo.keyword == RID_IF
12253 && !cp_lexer_next_token_is_keyword (parser->lexer, RID_ELSE))
12254 warning_at (body_loc, OPT_Wempty_body,
12255 "suggest braces around empty body in an %<if%> statement");
12256 else if (guard_tinfo.keyword == RID_ELSE)
12257 warning_at (body_loc, OPT_Wempty_body,
12258 "suggest braces around empty body in an %<else%> statement");
12260 /* if a compound is opened, we simply parse the statement directly. */
12261 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12262 statement = cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
12263 /* If the token is not a `{', then we must take special action. */
12264 else
12266 /* Create a compound-statement. */
12267 statement = begin_compound_stmt (0);
12268 /* Parse the dependent-statement. */
12269 cp_parser_statement (parser, NULL_TREE, false, if_p, chain);
12270 /* Finish the dummy compound-statement. */
12271 finish_compound_stmt (statement);
12274 token_indent_info next_tinfo
12275 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
12276 warn_for_misleading_indentation (guard_tinfo, body_tinfo, next_tinfo);
12278 /* Return the statement. */
12279 return statement;
12282 /* For some dependent statements (like `while (cond) statement'), we
12283 have already created a scope. Therefore, even if the dependent
12284 statement is a compound-statement, we do not want to create another
12285 scope. */
12287 static void
12288 cp_parser_already_scoped_statement (cp_parser* parser, bool *if_p,
12289 const token_indent_info &guard_tinfo)
12291 /* If the token is a `{', then we must take special action. */
12292 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
12294 token_indent_info body_tinfo
12295 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
12297 cp_parser_statement (parser, NULL_TREE, false, if_p);
12298 token_indent_info next_tinfo
12299 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
12300 warn_for_misleading_indentation (guard_tinfo, body_tinfo, next_tinfo);
12302 else
12304 /* Avoid calling cp_parser_compound_statement, so that we
12305 don't create a new scope. Do everything else by hand. */
12306 cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
12307 /* If the next keyword is `__label__' we have a label declaration. */
12308 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
12309 cp_parser_label_declaration (parser);
12310 /* Parse an (optional) statement-seq. */
12311 cp_parser_statement_seq_opt (parser, NULL_TREE);
12312 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
12316 /* Declarations [gram.dcl.dcl] */
12318 /* Parse an optional declaration-sequence.
12320 declaration-seq:
12321 declaration
12322 declaration-seq declaration */
12324 static void
12325 cp_parser_declaration_seq_opt (cp_parser* parser)
12327 while (true)
12329 cp_token *token;
12331 token = cp_lexer_peek_token (parser->lexer);
12333 if (token->type == CPP_CLOSE_BRACE
12334 || token->type == CPP_EOF
12335 || token->type == CPP_PRAGMA_EOL)
12336 break;
12338 if (token->type == CPP_SEMICOLON)
12340 /* A declaration consisting of a single semicolon is
12341 invalid. Allow it unless we're being pedantic. */
12342 cp_lexer_consume_token (parser->lexer);
12343 if (!in_system_header_at (input_location))
12344 pedwarn (input_location, OPT_Wpedantic, "extra %<;%>");
12345 continue;
12348 /* If we're entering or exiting a region that's implicitly
12349 extern "C", modify the lang context appropriately. */
12350 if (!parser->implicit_extern_c && token->implicit_extern_c)
12352 push_lang_context (lang_name_c);
12353 parser->implicit_extern_c = true;
12355 else if (parser->implicit_extern_c && !token->implicit_extern_c)
12357 pop_lang_context ();
12358 parser->implicit_extern_c = false;
12361 if (token->type == CPP_PRAGMA)
12363 /* A top-level declaration can consist solely of a #pragma.
12364 A nested declaration cannot, so this is done here and not
12365 in cp_parser_declaration. (A #pragma at block scope is
12366 handled in cp_parser_statement.) */
12367 cp_parser_pragma (parser, pragma_external, NULL);
12368 continue;
12371 /* Parse the declaration itself. */
12372 cp_parser_declaration (parser);
12376 /* Parse a declaration.
12378 declaration:
12379 block-declaration
12380 function-definition
12381 template-declaration
12382 explicit-instantiation
12383 explicit-specialization
12384 linkage-specification
12385 namespace-definition
12387 C++17:
12388 deduction-guide
12390 GNU extension:
12392 declaration:
12393 __extension__ declaration */
12395 static void
12396 cp_parser_declaration (cp_parser* parser)
12398 cp_token token1;
12399 cp_token token2;
12400 int saved_pedantic;
12401 void *p;
12402 tree attributes = NULL_TREE;
12404 /* Check for the `__extension__' keyword. */
12405 if (cp_parser_extension_opt (parser, &saved_pedantic))
12407 /* Parse the qualified declaration. */
12408 cp_parser_declaration (parser);
12409 /* Restore the PEDANTIC flag. */
12410 pedantic = saved_pedantic;
12412 return;
12415 /* Try to figure out what kind of declaration is present. */
12416 token1 = *cp_lexer_peek_token (parser->lexer);
12418 if (token1.type != CPP_EOF)
12419 token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
12420 else
12422 token2.type = CPP_EOF;
12423 token2.keyword = RID_MAX;
12426 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
12427 p = obstack_alloc (&declarator_obstack, 0);
12429 /* If the next token is `extern' and the following token is a string
12430 literal, then we have a linkage specification. */
12431 if (token1.keyword == RID_EXTERN
12432 && cp_parser_is_pure_string_literal (&token2))
12433 cp_parser_linkage_specification (parser);
12434 /* If the next token is `template', then we have either a template
12435 declaration, an explicit instantiation, or an explicit
12436 specialization. */
12437 else if (token1.keyword == RID_TEMPLATE)
12439 /* `template <>' indicates a template specialization. */
12440 if (token2.type == CPP_LESS
12441 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
12442 cp_parser_explicit_specialization (parser);
12443 /* `template <' indicates a template declaration. */
12444 else if (token2.type == CPP_LESS)
12445 cp_parser_template_declaration (parser, /*member_p=*/false);
12446 /* Anything else must be an explicit instantiation. */
12447 else
12448 cp_parser_explicit_instantiation (parser);
12450 /* If the next token is `export', then we have a template
12451 declaration. */
12452 else if (token1.keyword == RID_EXPORT)
12453 cp_parser_template_declaration (parser, /*member_p=*/false);
12454 /* If the next token is `extern', 'static' or 'inline' and the one
12455 after that is `template', we have a GNU extended explicit
12456 instantiation directive. */
12457 else if (cp_parser_allow_gnu_extensions_p (parser)
12458 && (token1.keyword == RID_EXTERN
12459 || token1.keyword == RID_STATIC
12460 || token1.keyword == RID_INLINE)
12461 && token2.keyword == RID_TEMPLATE)
12462 cp_parser_explicit_instantiation (parser);
12463 /* If the next token is `namespace', check for a named or unnamed
12464 namespace definition. */
12465 else if (token1.keyword == RID_NAMESPACE
12466 && (/* A named namespace definition. */
12467 (token2.type == CPP_NAME
12468 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
12469 != CPP_EQ))
12470 || (token2.type == CPP_OPEN_SQUARE
12471 && cp_lexer_peek_nth_token (parser->lexer, 3)->type
12472 == CPP_OPEN_SQUARE)
12473 /* An unnamed namespace definition. */
12474 || token2.type == CPP_OPEN_BRACE
12475 || token2.keyword == RID_ATTRIBUTE))
12476 cp_parser_namespace_definition (parser);
12477 /* An inline (associated) namespace definition. */
12478 else if (token1.keyword == RID_INLINE
12479 && token2.keyword == RID_NAMESPACE)
12480 cp_parser_namespace_definition (parser);
12481 /* Objective-C++ declaration/definition. */
12482 else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1.keyword))
12483 cp_parser_objc_declaration (parser, NULL_TREE);
12484 else if (c_dialect_objc ()
12485 && token1.keyword == RID_ATTRIBUTE
12486 && cp_parser_objc_valid_prefix_attributes (parser, &attributes))
12487 cp_parser_objc_declaration (parser, attributes);
12488 /* At this point we may have a template declared by a concept
12489 introduction. */
12490 else if (flag_concepts
12491 && cp_parser_template_declaration_after_export (parser,
12492 /*member_p=*/false))
12493 /* We did. */;
12494 else
12495 /* Try to parse a block-declaration, or a function-definition. */
12496 cp_parser_block_declaration (parser, /*statement_p=*/false);
12498 /* Free any declarators allocated. */
12499 obstack_free (&declarator_obstack, p);
12502 /* Parse a block-declaration.
12504 block-declaration:
12505 simple-declaration
12506 asm-definition
12507 namespace-alias-definition
12508 using-declaration
12509 using-directive
12511 GNU Extension:
12513 block-declaration:
12514 __extension__ block-declaration
12516 C++0x Extension:
12518 block-declaration:
12519 static_assert-declaration
12521 If STATEMENT_P is TRUE, then this block-declaration is occurring as
12522 part of a declaration-statement. */
12524 static void
12525 cp_parser_block_declaration (cp_parser *parser,
12526 bool statement_p)
12528 cp_token *token1;
12529 int saved_pedantic;
12531 /* Check for the `__extension__' keyword. */
12532 if (cp_parser_extension_opt (parser, &saved_pedantic))
12534 /* Parse the qualified declaration. */
12535 cp_parser_block_declaration (parser, statement_p);
12536 /* Restore the PEDANTIC flag. */
12537 pedantic = saved_pedantic;
12539 return;
12542 /* Peek at the next token to figure out which kind of declaration is
12543 present. */
12544 token1 = cp_lexer_peek_token (parser->lexer);
12546 /* If the next keyword is `asm', we have an asm-definition. */
12547 if (token1->keyword == RID_ASM)
12549 if (statement_p)
12550 cp_parser_commit_to_tentative_parse (parser);
12551 cp_parser_asm_definition (parser);
12553 /* If the next keyword is `namespace', we have a
12554 namespace-alias-definition. */
12555 else if (token1->keyword == RID_NAMESPACE)
12556 cp_parser_namespace_alias_definition (parser);
12557 /* If the next keyword is `using', we have a
12558 using-declaration, a using-directive, or an alias-declaration. */
12559 else if (token1->keyword == RID_USING)
12561 cp_token *token2;
12563 if (statement_p)
12564 cp_parser_commit_to_tentative_parse (parser);
12565 /* If the token after `using' is `namespace', then we have a
12566 using-directive. */
12567 token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
12568 if (token2->keyword == RID_NAMESPACE)
12569 cp_parser_using_directive (parser);
12570 /* If the second token after 'using' is '=', then we have an
12571 alias-declaration. */
12572 else if (cxx_dialect >= cxx11
12573 && token2->type == CPP_NAME
12574 && ((cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_EQ)
12575 || (cp_nth_tokens_can_be_attribute_p (parser, 3))))
12576 cp_parser_alias_declaration (parser);
12577 /* Otherwise, it's a using-declaration. */
12578 else
12579 cp_parser_using_declaration (parser,
12580 /*access_declaration_p=*/false);
12582 /* If the next keyword is `__label__' we have a misplaced label
12583 declaration. */
12584 else if (token1->keyword == RID_LABEL)
12586 cp_lexer_consume_token (parser->lexer);
12587 error_at (token1->location, "%<__label__%> not at the beginning of a block");
12588 cp_parser_skip_to_end_of_statement (parser);
12589 /* If the next token is now a `;', consume it. */
12590 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
12591 cp_lexer_consume_token (parser->lexer);
12593 /* If the next token is `static_assert' we have a static assertion. */
12594 else if (token1->keyword == RID_STATIC_ASSERT)
12595 cp_parser_static_assert (parser, /*member_p=*/false);
12596 /* Anything else must be a simple-declaration. */
12597 else
12598 cp_parser_simple_declaration (parser, !statement_p,
12599 /*maybe_range_for_decl*/NULL);
12602 /* Parse a simple-declaration.
12604 simple-declaration:
12605 decl-specifier-seq [opt] init-declarator-list [opt] ;
12606 decl-specifier-seq ref-qualifier [opt] [ identifier-list ]
12607 brace-or-equal-initializer ;
12609 init-declarator-list:
12610 init-declarator
12611 init-declarator-list , init-declarator
12613 If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
12614 function-definition as a simple-declaration.
12616 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
12617 parsed declaration if it is an uninitialized single declarator not followed
12618 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
12619 if present, will not be consumed. */
12621 static void
12622 cp_parser_simple_declaration (cp_parser* parser,
12623 bool function_definition_allowed_p,
12624 tree *maybe_range_for_decl)
12626 cp_decl_specifier_seq decl_specifiers;
12627 int declares_class_or_enum;
12628 bool saw_declarator;
12629 location_t comma_loc = UNKNOWN_LOCATION;
12630 location_t init_loc = UNKNOWN_LOCATION;
12632 if (maybe_range_for_decl)
12633 *maybe_range_for_decl = NULL_TREE;
12635 /* Defer access checks until we know what is being declared; the
12636 checks for names appearing in the decl-specifier-seq should be
12637 done as if we were in the scope of the thing being declared. */
12638 push_deferring_access_checks (dk_deferred);
12640 /* Parse the decl-specifier-seq. We have to keep track of whether
12641 or not the decl-specifier-seq declares a named class or
12642 enumeration type, since that is the only case in which the
12643 init-declarator-list is allowed to be empty.
12645 [dcl.dcl]
12647 In a simple-declaration, the optional init-declarator-list can be
12648 omitted only when declaring a class or enumeration, that is when
12649 the decl-specifier-seq contains either a class-specifier, an
12650 elaborated-type-specifier, or an enum-specifier. */
12651 cp_parser_decl_specifier_seq (parser,
12652 CP_PARSER_FLAGS_OPTIONAL,
12653 &decl_specifiers,
12654 &declares_class_or_enum);
12655 /* We no longer need to defer access checks. */
12656 stop_deferring_access_checks ();
12658 /* In a block scope, a valid declaration must always have a
12659 decl-specifier-seq. By not trying to parse declarators, we can
12660 resolve the declaration/expression ambiguity more quickly. */
12661 if (!function_definition_allowed_p
12662 && !decl_specifiers.any_specifiers_p)
12664 cp_parser_error (parser, "expected declaration");
12665 goto done;
12668 /* If the next two tokens are both identifiers, the code is
12669 erroneous. The usual cause of this situation is code like:
12671 T t;
12673 where "T" should name a type -- but does not. */
12674 if (!decl_specifiers.any_type_specifiers_p
12675 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
12677 /* If parsing tentatively, we should commit; we really are
12678 looking at a declaration. */
12679 cp_parser_commit_to_tentative_parse (parser);
12680 /* Give up. */
12681 goto done;
12684 /* If we have seen at least one decl-specifier, and the next token
12685 is not a parenthesis, then we must be looking at a declaration.
12686 (After "int (" we might be looking at a functional cast.) */
12687 if (decl_specifiers.any_specifiers_p
12688 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN)
12689 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
12690 && !cp_parser_error_occurred (parser))
12691 cp_parser_commit_to_tentative_parse (parser);
12693 /* Look for C++17 decomposition declaration. */
12694 for (size_t n = 1; ; n++)
12695 if (cp_lexer_nth_token_is (parser->lexer, n, CPP_AND)
12696 || cp_lexer_nth_token_is (parser->lexer, n, CPP_AND_AND))
12697 continue;
12698 else if (cp_lexer_nth_token_is (parser->lexer, n, CPP_OPEN_SQUARE)
12699 && !cp_lexer_nth_token_is (parser->lexer, n + 1, CPP_OPEN_SQUARE)
12700 && decl_specifiers.any_specifiers_p)
12702 tree decl
12703 = cp_parser_decomposition_declaration (parser, &decl_specifiers,
12704 maybe_range_for_decl,
12705 &init_loc);
12707 /* The next token should be either a `,' or a `;'. */
12708 cp_token *token = cp_lexer_peek_token (parser->lexer);
12709 /* If it's a `;', we are done. */
12710 if (token->type == CPP_SEMICOLON || maybe_range_for_decl)
12711 goto finish;
12712 /* Anything else is an error. */
12713 else
12715 /* If we have already issued an error message we don't need
12716 to issue another one. */
12717 if ((decl != error_mark_node
12718 && DECL_INITIAL (decl) != error_mark_node)
12719 || cp_parser_uncommitted_to_tentative_parse_p (parser))
12720 cp_parser_error (parser, "expected %<,%> or %<;%>");
12721 /* Skip tokens until we reach the end of the statement. */
12722 cp_parser_skip_to_end_of_statement (parser);
12723 /* If the next token is now a `;', consume it. */
12724 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
12725 cp_lexer_consume_token (parser->lexer);
12726 goto done;
12729 else
12730 break;
12732 tree last_type;
12733 bool auto_specifier_p;
12734 /* NULL_TREE if both variable and function declaration are allowed,
12735 error_mark_node if function declaration are not allowed and
12736 a FUNCTION_DECL that should be diagnosed if it is followed by
12737 variable declarations. */
12738 tree auto_function_declaration;
12740 last_type = NULL_TREE;
12741 auto_specifier_p
12742 = decl_specifiers.type && type_uses_auto (decl_specifiers.type);
12743 auto_function_declaration = NULL_TREE;
12745 /* Keep going until we hit the `;' at the end of the simple
12746 declaration. */
12747 saw_declarator = false;
12748 while (cp_lexer_next_token_is_not (parser->lexer,
12749 CPP_SEMICOLON))
12751 cp_token *token;
12752 bool function_definition_p;
12753 tree decl;
12754 tree auto_result = NULL_TREE;
12756 if (saw_declarator)
12758 /* If we are processing next declarator, comma is expected */
12759 token = cp_lexer_peek_token (parser->lexer);
12760 gcc_assert (token->type == CPP_COMMA);
12761 cp_lexer_consume_token (parser->lexer);
12762 if (maybe_range_for_decl)
12764 *maybe_range_for_decl = error_mark_node;
12765 if (comma_loc == UNKNOWN_LOCATION)
12766 comma_loc = token->location;
12769 else
12770 saw_declarator = true;
12772 /* Parse the init-declarator. */
12773 decl = cp_parser_init_declarator (parser, &decl_specifiers,
12774 /*checks=*/NULL,
12775 function_definition_allowed_p,
12776 /*member_p=*/false,
12777 declares_class_or_enum,
12778 &function_definition_p,
12779 maybe_range_for_decl,
12780 &init_loc,
12781 &auto_result);
12782 /* If an error occurred while parsing tentatively, exit quickly.
12783 (That usually happens when in the body of a function; each
12784 statement is treated as a declaration-statement until proven
12785 otherwise.) */
12786 if (cp_parser_error_occurred (parser))
12787 goto done;
12789 if (auto_specifier_p && cxx_dialect >= cxx14)
12791 /* If the init-declarator-list contains more than one
12792 init-declarator, they shall all form declarations of
12793 variables. */
12794 if (auto_function_declaration == NULL_TREE)
12795 auto_function_declaration
12796 = TREE_CODE (decl) == FUNCTION_DECL ? decl : error_mark_node;
12797 else if (TREE_CODE (decl) == FUNCTION_DECL
12798 || auto_function_declaration != error_mark_node)
12800 error_at (decl_specifiers.locations[ds_type_spec],
12801 "non-variable %qD in declaration with more than one "
12802 "declarator with placeholder type",
12803 TREE_CODE (decl) == FUNCTION_DECL
12804 ? decl : auto_function_declaration);
12805 auto_function_declaration = error_mark_node;
12809 if (auto_result
12810 && (!processing_template_decl || !type_uses_auto (auto_result)))
12812 if (last_type
12813 && last_type != error_mark_node
12814 && !same_type_p (auto_result, last_type))
12816 /* If the list of declarators contains more than one declarator,
12817 the type of each declared variable is determined as described
12818 above. If the type deduced for the template parameter U is not
12819 the same in each deduction, the program is ill-formed. */
12820 error_at (decl_specifiers.locations[ds_type_spec],
12821 "inconsistent deduction for %qT: %qT and then %qT",
12822 decl_specifiers.type, last_type, auto_result);
12823 last_type = error_mark_node;
12825 else
12826 last_type = auto_result;
12829 /* Handle function definitions specially. */
12830 if (function_definition_p)
12832 /* If the next token is a `,', then we are probably
12833 processing something like:
12835 void f() {}, *p;
12837 which is erroneous. */
12838 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
12840 cp_token *token = cp_lexer_peek_token (parser->lexer);
12841 error_at (token->location,
12842 "mixing"
12843 " declarations and function-definitions is forbidden");
12845 /* Otherwise, we're done with the list of declarators. */
12846 else
12848 pop_deferring_access_checks ();
12849 return;
12852 if (maybe_range_for_decl && *maybe_range_for_decl == NULL_TREE)
12853 *maybe_range_for_decl = decl;
12854 /* The next token should be either a `,' or a `;'. */
12855 token = cp_lexer_peek_token (parser->lexer);
12856 /* If it's a `,', there are more declarators to come. */
12857 if (token->type == CPP_COMMA)
12858 /* will be consumed next time around */;
12859 /* If it's a `;', we are done. */
12860 else if (token->type == CPP_SEMICOLON)
12861 break;
12862 else if (maybe_range_for_decl)
12864 if ((declares_class_or_enum & 2) && token->type == CPP_COLON)
12865 permerror (decl_specifiers.locations[ds_type_spec],
12866 "types may not be defined in a for-range-declaration");
12867 break;
12869 /* Anything else is an error. */
12870 else
12872 /* If we have already issued an error message we don't need
12873 to issue another one. */
12874 if ((decl != error_mark_node
12875 && DECL_INITIAL (decl) != error_mark_node)
12876 || cp_parser_uncommitted_to_tentative_parse_p (parser))
12877 cp_parser_error (parser, "expected %<,%> or %<;%>");
12878 /* Skip tokens until we reach the end of the statement. */
12879 cp_parser_skip_to_end_of_statement (parser);
12880 /* If the next token is now a `;', consume it. */
12881 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
12882 cp_lexer_consume_token (parser->lexer);
12883 goto done;
12885 /* After the first time around, a function-definition is not
12886 allowed -- even if it was OK at first. For example:
12888 int i, f() {}
12890 is not valid. */
12891 function_definition_allowed_p = false;
12894 /* Issue an error message if no declarators are present, and the
12895 decl-specifier-seq does not itself declare a class or
12896 enumeration: [dcl.dcl]/3. */
12897 if (!saw_declarator)
12899 if (cp_parser_declares_only_class_p (parser))
12901 if (!declares_class_or_enum
12902 && decl_specifiers.type
12903 && OVERLOAD_TYPE_P (decl_specifiers.type))
12904 /* Ensure an error is issued anyway when finish_decltype_type,
12905 called via cp_parser_decl_specifier_seq, returns a class or
12906 an enumeration (c++/51786). */
12907 decl_specifiers.type = NULL_TREE;
12908 shadow_tag (&decl_specifiers);
12910 /* Perform any deferred access checks. */
12911 perform_deferred_access_checks (tf_warning_or_error);
12914 /* Consume the `;'. */
12915 finish:
12916 if (!maybe_range_for_decl)
12917 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12918 else if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
12920 if (init_loc != UNKNOWN_LOCATION)
12921 error_at (init_loc, "initializer in range-based %<for%> loop");
12922 if (comma_loc != UNKNOWN_LOCATION)
12923 error_at (comma_loc,
12924 "multiple declarations in range-based %<for%> loop");
12927 done:
12928 pop_deferring_access_checks ();
12931 /* Helper of cp_parser_simple_declaration, parse a decomposition declaration.
12932 decl-specifier-seq ref-qualifier [opt] [ identifier-list ]
12933 initializer ; */
12935 static tree
12936 cp_parser_decomposition_declaration (cp_parser *parser,
12937 cp_decl_specifier_seq *decl_specifiers,
12938 tree *maybe_range_for_decl,
12939 location_t *init_loc)
12941 cp_ref_qualifier ref_qual = cp_parser_ref_qualifier_opt (parser);
12942 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
12943 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
12945 /* Parse the identifier-list. */
12946 auto_vec<cp_expr, 10> v;
12947 if (!cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_SQUARE))
12948 while (true)
12950 cp_expr e = cp_parser_identifier (parser);
12951 if (e.get_value () == error_mark_node)
12952 break;
12953 v.safe_push (e);
12954 if (!cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
12955 break;
12956 cp_lexer_consume_token (parser->lexer);
12959 location_t end_loc = cp_lexer_peek_token (parser->lexer)->location;
12960 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
12962 end_loc = UNKNOWN_LOCATION;
12963 cp_parser_skip_to_closing_parenthesis_1 (parser, true, CPP_CLOSE_SQUARE,
12964 false);
12965 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_SQUARE))
12966 cp_lexer_consume_token (parser->lexer);
12967 else
12969 cp_parser_skip_to_end_of_statement (parser);
12970 return error_mark_node;
12974 if (cxx_dialect < cxx1z)
12975 pedwarn (loc, 0, "decomposition declaration only available with "
12976 "-std=c++1z or -std=gnu++1z");
12978 tree pushed_scope;
12979 cp_declarator *declarator = make_declarator (cdk_decomp);
12980 loc = end_loc == UNKNOWN_LOCATION ? loc : make_location (loc, loc, end_loc);
12981 declarator->id_loc = loc;
12982 if (ref_qual != REF_QUAL_NONE)
12983 declarator = make_reference_declarator (TYPE_UNQUALIFIED, declarator,
12984 ref_qual == REF_QUAL_RVALUE,
12985 NULL_TREE);
12986 tree decl = start_decl (declarator, decl_specifiers, SD_INITIALIZED,
12987 NULL_TREE, decl_specifiers->attributes,
12988 &pushed_scope);
12989 tree orig_decl = decl;
12991 unsigned int i;
12992 cp_expr e;
12993 cp_decl_specifier_seq decl_specs;
12994 clear_decl_specs (&decl_specs);
12995 decl_specs.type = make_auto ();
12996 tree prev = decl;
12997 FOR_EACH_VEC_ELT (v, i, e)
12999 if (i == 0)
13000 declarator = make_id_declarator (NULL_TREE, e.get_value (), sfk_none);
13001 else
13002 declarator->u.id.unqualified_name = e.get_value ();
13003 declarator->id_loc = e.get_location ();
13004 tree elt_pushed_scope;
13005 tree decl2 = start_decl (declarator, &decl_specs, SD_INITIALIZED,
13006 NULL_TREE, NULL_TREE, &elt_pushed_scope);
13007 if (decl2 == error_mark_node)
13008 decl = error_mark_node;
13009 else if (decl != error_mark_node && DECL_CHAIN (decl2) != prev)
13011 /* Ensure we've diagnosed redeclaration if we aren't creating
13012 a new VAR_DECL. */
13013 gcc_assert (errorcount);
13014 decl = error_mark_node;
13016 else
13017 prev = decl2;
13018 if (elt_pushed_scope)
13019 pop_scope (elt_pushed_scope);
13022 if (v.is_empty ())
13024 error_at (loc, "empty decomposition declaration");
13025 decl = error_mark_node;
13028 if (maybe_range_for_decl == NULL
13029 || cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
13031 bool non_constant_p = false, is_direct_init = false;
13032 *init_loc = cp_lexer_peek_token (parser->lexer)->location;
13033 tree initializer = cp_parser_initializer (parser, &is_direct_init,
13034 &non_constant_p);
13036 if (decl != error_mark_node)
13038 cp_finish_decl (decl, initializer, non_constant_p, NULL_TREE,
13039 is_direct_init ? LOOKUP_NORMAL : LOOKUP_IMPLICIT);
13040 cp_finish_decomp (decl, prev, v.length ());
13043 else if (decl != error_mark_node)
13045 *maybe_range_for_decl = prev;
13046 /* Ensure DECL_VALUE_EXPR is created for all the decls but
13047 the underlying DECL. */
13048 cp_finish_decomp (decl, prev, v.length ());
13051 if (pushed_scope)
13052 pop_scope (pushed_scope);
13054 if (decl == error_mark_node && DECL_P (orig_decl))
13056 if (DECL_NAMESPACE_SCOPE_P (orig_decl))
13057 SET_DECL_ASSEMBLER_NAME (orig_decl, get_identifier ("<decomp>"));
13060 return decl;
13063 /* Parse a decl-specifier-seq.
13065 decl-specifier-seq:
13066 decl-specifier-seq [opt] decl-specifier
13067 decl-specifier attribute-specifier-seq [opt] (C++11)
13069 decl-specifier:
13070 storage-class-specifier
13071 type-specifier
13072 function-specifier
13073 friend
13074 typedef
13076 GNU Extension:
13078 decl-specifier:
13079 attributes
13081 Concepts Extension:
13083 decl-specifier:
13084 concept
13086 Set *DECL_SPECS to a representation of the decl-specifier-seq.
13088 The parser flags FLAGS is used to control type-specifier parsing.
13090 *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
13091 flags:
13093 1: one of the decl-specifiers is an elaborated-type-specifier
13094 (i.e., a type declaration)
13095 2: one of the decl-specifiers is an enum-specifier or a
13096 class-specifier (i.e., a type definition)
13100 static void
13101 cp_parser_decl_specifier_seq (cp_parser* parser,
13102 cp_parser_flags flags,
13103 cp_decl_specifier_seq *decl_specs,
13104 int* declares_class_or_enum)
13106 bool constructor_possible_p = !parser->in_declarator_p;
13107 bool found_decl_spec = false;
13108 cp_token *start_token = NULL;
13109 cp_decl_spec ds;
13111 /* Clear DECL_SPECS. */
13112 clear_decl_specs (decl_specs);
13114 /* Assume no class or enumeration type is declared. */
13115 *declares_class_or_enum = 0;
13117 /* Keep reading specifiers until there are no more to read. */
13118 while (true)
13120 bool constructor_p;
13121 cp_token *token;
13122 ds = ds_last;
13124 /* Peek at the next token. */
13125 token = cp_lexer_peek_token (parser->lexer);
13127 /* Save the first token of the decl spec list for error
13128 reporting. */
13129 if (!start_token)
13130 start_token = token;
13131 /* Handle attributes. */
13132 if (cp_next_tokens_can_be_attribute_p (parser))
13134 /* Parse the attributes. */
13135 tree attrs = cp_parser_attributes_opt (parser);
13137 /* In a sequence of declaration specifiers, c++11 attributes
13138 appertain to the type that precede them. In that case
13139 [dcl.spec]/1 says:
13141 The attribute-specifier-seq affects the type only for
13142 the declaration it appears in, not other declarations
13143 involving the same type.
13145 But for now let's force the user to position the
13146 attribute either at the beginning of the declaration or
13147 after the declarator-id, which would clearly mean that it
13148 applies to the declarator. */
13149 if (cxx11_attribute_p (attrs))
13151 if (!found_decl_spec)
13152 /* The c++11 attribute is at the beginning of the
13153 declaration. It appertains to the entity being
13154 declared. */;
13155 else
13157 if (decl_specs->type && CLASS_TYPE_P (decl_specs->type))
13159 /* This is an attribute following a
13160 class-specifier. */
13161 if (decl_specs->type_definition_p)
13162 warn_misplaced_attr_for_class_type (token->location,
13163 decl_specs->type);
13164 attrs = NULL_TREE;
13166 else
13168 decl_specs->std_attributes
13169 = chainon (decl_specs->std_attributes,
13170 attrs);
13171 if (decl_specs->locations[ds_std_attribute] == 0)
13172 decl_specs->locations[ds_std_attribute] = token->location;
13174 continue;
13178 decl_specs->attributes
13179 = chainon (decl_specs->attributes,
13180 attrs);
13181 if (decl_specs->locations[ds_attribute] == 0)
13182 decl_specs->locations[ds_attribute] = token->location;
13183 continue;
13185 /* Assume we will find a decl-specifier keyword. */
13186 found_decl_spec = true;
13187 /* If the next token is an appropriate keyword, we can simply
13188 add it to the list. */
13189 switch (token->keyword)
13191 /* decl-specifier:
13192 friend
13193 constexpr */
13194 case RID_FRIEND:
13195 if (!at_class_scope_p ())
13197 error_at (token->location, "%<friend%> used outside of class");
13198 cp_lexer_purge_token (parser->lexer);
13200 else
13202 ds = ds_friend;
13203 /* Consume the token. */
13204 cp_lexer_consume_token (parser->lexer);
13206 break;
13208 case RID_CONSTEXPR:
13209 ds = ds_constexpr;
13210 cp_lexer_consume_token (parser->lexer);
13211 break;
13213 case RID_CONCEPT:
13214 ds = ds_concept;
13215 cp_lexer_consume_token (parser->lexer);
13216 break;
13218 /* function-specifier:
13219 inline
13220 virtual
13221 explicit */
13222 case RID_INLINE:
13223 case RID_VIRTUAL:
13224 case RID_EXPLICIT:
13225 cp_parser_function_specifier_opt (parser, decl_specs);
13226 break;
13228 /* decl-specifier:
13229 typedef */
13230 case RID_TYPEDEF:
13231 ds = ds_typedef;
13232 /* Consume the token. */
13233 cp_lexer_consume_token (parser->lexer);
13234 /* A constructor declarator cannot appear in a typedef. */
13235 constructor_possible_p = false;
13236 /* The "typedef" keyword can only occur in a declaration; we
13237 may as well commit at this point. */
13238 cp_parser_commit_to_tentative_parse (parser);
13240 if (decl_specs->storage_class != sc_none)
13241 decl_specs->conflicting_specifiers_p = true;
13242 break;
13244 /* storage-class-specifier:
13245 auto
13246 register
13247 static
13248 extern
13249 mutable
13251 GNU Extension:
13252 thread */
13253 case RID_AUTO:
13254 if (cxx_dialect == cxx98)
13256 /* Consume the token. */
13257 cp_lexer_consume_token (parser->lexer);
13259 /* Complain about `auto' as a storage specifier, if
13260 we're complaining about C++0x compatibility. */
13261 warning_at (token->location, OPT_Wc__11_compat, "%<auto%>"
13262 " changes meaning in C++11; please remove it");
13264 /* Set the storage class anyway. */
13265 cp_parser_set_storage_class (parser, decl_specs, RID_AUTO,
13266 token);
13268 else
13269 /* C++0x auto type-specifier. */
13270 found_decl_spec = false;
13271 break;
13273 case RID_REGISTER:
13274 case RID_STATIC:
13275 case RID_EXTERN:
13276 case RID_MUTABLE:
13277 /* Consume the token. */
13278 cp_lexer_consume_token (parser->lexer);
13279 cp_parser_set_storage_class (parser, decl_specs, token->keyword,
13280 token);
13281 break;
13282 case RID_THREAD:
13283 /* Consume the token. */
13284 ds = ds_thread;
13285 cp_lexer_consume_token (parser->lexer);
13286 break;
13288 default:
13289 /* We did not yet find a decl-specifier yet. */
13290 found_decl_spec = false;
13291 break;
13294 if (found_decl_spec
13295 && (flags & CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR)
13296 && token->keyword != RID_CONSTEXPR)
13297 error ("decl-specifier invalid in condition");
13299 if (found_decl_spec
13300 && (flags & CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR)
13301 && token->keyword != RID_MUTABLE
13302 && token->keyword != RID_CONSTEXPR)
13303 error_at (token->location, "%qD invalid in lambda",
13304 ridpointers[token->keyword]);
13306 if (ds != ds_last)
13307 set_and_check_decl_spec_loc (decl_specs, ds, token);
13309 /* Constructors are a special case. The `S' in `S()' is not a
13310 decl-specifier; it is the beginning of the declarator. */
13311 constructor_p
13312 = (!found_decl_spec
13313 && constructor_possible_p
13314 && (cp_parser_constructor_declarator_p
13315 (parser, decl_spec_seq_has_spec_p (decl_specs, ds_friend))));
13317 /* If we don't have a DECL_SPEC yet, then we must be looking at
13318 a type-specifier. */
13319 if (!found_decl_spec && !constructor_p)
13321 int decl_spec_declares_class_or_enum;
13322 bool is_cv_qualifier;
13323 tree type_spec;
13325 type_spec
13326 = cp_parser_type_specifier (parser, flags,
13327 decl_specs,
13328 /*is_declaration=*/true,
13329 &decl_spec_declares_class_or_enum,
13330 &is_cv_qualifier);
13331 *declares_class_or_enum |= decl_spec_declares_class_or_enum;
13333 /* If this type-specifier referenced a user-defined type
13334 (a typedef, class-name, etc.), then we can't allow any
13335 more such type-specifiers henceforth.
13337 [dcl.spec]
13339 The longest sequence of decl-specifiers that could
13340 possibly be a type name is taken as the
13341 decl-specifier-seq of a declaration. The sequence shall
13342 be self-consistent as described below.
13344 [dcl.type]
13346 As a general rule, at most one type-specifier is allowed
13347 in the complete decl-specifier-seq of a declaration. The
13348 only exceptions are the following:
13350 -- const or volatile can be combined with any other
13351 type-specifier.
13353 -- signed or unsigned can be combined with char, long,
13354 short, or int.
13356 -- ..
13358 Example:
13360 typedef char* Pc;
13361 void g (const int Pc);
13363 Here, Pc is *not* part of the decl-specifier seq; it's
13364 the declarator. Therefore, once we see a type-specifier
13365 (other than a cv-qualifier), we forbid any additional
13366 user-defined types. We *do* still allow things like `int
13367 int' to be considered a decl-specifier-seq, and issue the
13368 error message later. */
13369 if (type_spec && !is_cv_qualifier)
13370 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
13371 /* A constructor declarator cannot follow a type-specifier. */
13372 if (type_spec)
13374 constructor_possible_p = false;
13375 found_decl_spec = true;
13376 if (!is_cv_qualifier)
13377 decl_specs->any_type_specifiers_p = true;
13381 /* If we still do not have a DECL_SPEC, then there are no more
13382 decl-specifiers. */
13383 if (!found_decl_spec)
13384 break;
13386 decl_specs->any_specifiers_p = true;
13387 /* After we see one decl-specifier, further decl-specifiers are
13388 always optional. */
13389 flags |= CP_PARSER_FLAGS_OPTIONAL;
13392 /* Don't allow a friend specifier with a class definition. */
13393 if (decl_spec_seq_has_spec_p (decl_specs, ds_friend)
13394 && (*declares_class_or_enum & 2))
13395 error_at (decl_specs->locations[ds_friend],
13396 "class definition may not be declared a friend");
13399 /* Parse an (optional) storage-class-specifier.
13401 storage-class-specifier:
13402 auto
13403 register
13404 static
13405 extern
13406 mutable
13408 GNU Extension:
13410 storage-class-specifier:
13411 thread
13413 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
13415 static tree
13416 cp_parser_storage_class_specifier_opt (cp_parser* parser)
13418 switch (cp_lexer_peek_token (parser->lexer)->keyword)
13420 case RID_AUTO:
13421 if (cxx_dialect != cxx98)
13422 return NULL_TREE;
13423 /* Fall through for C++98. */
13424 gcc_fallthrough ();
13426 case RID_REGISTER:
13427 case RID_STATIC:
13428 case RID_EXTERN:
13429 case RID_MUTABLE:
13430 case RID_THREAD:
13431 /* Consume the token. */
13432 return cp_lexer_consume_token (parser->lexer)->u.value;
13434 default:
13435 return NULL_TREE;
13439 /* Parse an (optional) function-specifier.
13441 function-specifier:
13442 inline
13443 virtual
13444 explicit
13446 Returns an IDENTIFIER_NODE corresponding to the keyword used.
13447 Updates DECL_SPECS, if it is non-NULL. */
13449 static tree
13450 cp_parser_function_specifier_opt (cp_parser* parser,
13451 cp_decl_specifier_seq *decl_specs)
13453 cp_token *token = cp_lexer_peek_token (parser->lexer);
13454 switch (token->keyword)
13456 case RID_INLINE:
13457 set_and_check_decl_spec_loc (decl_specs, ds_inline, token);
13458 break;
13460 case RID_VIRTUAL:
13461 /* 14.5.2.3 [temp.mem]
13463 A member function template shall not be virtual. */
13464 if (PROCESSING_REAL_TEMPLATE_DECL_P ()
13465 && current_class_type)
13466 error_at (token->location, "templates may not be %<virtual%>");
13467 else
13468 set_and_check_decl_spec_loc (decl_specs, ds_virtual, token);
13469 break;
13471 case RID_EXPLICIT:
13472 set_and_check_decl_spec_loc (decl_specs, ds_explicit, token);
13473 break;
13475 default:
13476 return NULL_TREE;
13479 /* Consume the token. */
13480 return cp_lexer_consume_token (parser->lexer)->u.value;
13483 /* Parse a linkage-specification.
13485 linkage-specification:
13486 extern string-literal { declaration-seq [opt] }
13487 extern string-literal declaration */
13489 static void
13490 cp_parser_linkage_specification (cp_parser* parser)
13492 tree linkage;
13494 /* Look for the `extern' keyword. */
13495 cp_parser_require_keyword (parser, RID_EXTERN, RT_EXTERN);
13497 /* Look for the string-literal. */
13498 linkage = cp_parser_string_literal (parser, false, false);
13500 /* Transform the literal into an identifier. If the literal is a
13501 wide-character string, or contains embedded NULs, then we can't
13502 handle it as the user wants. */
13503 if (strlen (TREE_STRING_POINTER (linkage))
13504 != (size_t) (TREE_STRING_LENGTH (linkage) - 1))
13506 cp_parser_error (parser, "invalid linkage-specification");
13507 /* Assume C++ linkage. */
13508 linkage = lang_name_cplusplus;
13510 else
13511 linkage = get_identifier (TREE_STRING_POINTER (linkage));
13513 /* We're now using the new linkage. */
13514 push_lang_context (linkage);
13516 /* If the next token is a `{', then we're using the first
13517 production. */
13518 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
13520 cp_ensure_no_omp_declare_simd (parser);
13521 cp_ensure_no_oacc_routine (parser);
13523 /* Consume the `{' token. */
13524 cp_lexer_consume_token (parser->lexer);
13525 /* Parse the declarations. */
13526 cp_parser_declaration_seq_opt (parser);
13527 /* Look for the closing `}'. */
13528 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
13530 /* Otherwise, there's just one declaration. */
13531 else
13533 bool saved_in_unbraced_linkage_specification_p;
13535 saved_in_unbraced_linkage_specification_p
13536 = parser->in_unbraced_linkage_specification_p;
13537 parser->in_unbraced_linkage_specification_p = true;
13538 cp_parser_declaration (parser);
13539 parser->in_unbraced_linkage_specification_p
13540 = saved_in_unbraced_linkage_specification_p;
13543 /* We're done with the linkage-specification. */
13544 pop_lang_context ();
13547 /* Parse a static_assert-declaration.
13549 static_assert-declaration:
13550 static_assert ( constant-expression , string-literal ) ;
13551 static_assert ( constant-expression ) ; (C++1Z)
13553 If MEMBER_P, this static_assert is a class member. */
13555 static void
13556 cp_parser_static_assert(cp_parser *parser, bool member_p)
13558 tree condition;
13559 tree message;
13560 cp_token *token;
13561 location_t saved_loc;
13562 bool dummy;
13564 /* Peek at the `static_assert' token so we can keep track of exactly
13565 where the static assertion started. */
13566 token = cp_lexer_peek_token (parser->lexer);
13567 saved_loc = token->location;
13569 /* Look for the `static_assert' keyword. */
13570 if (!cp_parser_require_keyword (parser, RID_STATIC_ASSERT,
13571 RT_STATIC_ASSERT))
13572 return;
13574 /* We know we are in a static assertion; commit to any tentative
13575 parse. */
13576 if (cp_parser_parsing_tentatively (parser))
13577 cp_parser_commit_to_tentative_parse (parser);
13579 /* Parse the `(' starting the static assertion condition. */
13580 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
13582 /* Parse the constant-expression. Allow a non-constant expression
13583 here in order to give better diagnostics in finish_static_assert. */
13584 condition =
13585 cp_parser_constant_expression (parser,
13586 /*allow_non_constant_p=*/true,
13587 /*non_constant_p=*/&dummy);
13589 if (cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
13591 if (cxx_dialect < cxx1z)
13592 pedwarn (input_location, OPT_Wpedantic,
13593 "static_assert without a message "
13594 "only available with -std=c++1z or -std=gnu++1z");
13595 /* Eat the ')' */
13596 cp_lexer_consume_token (parser->lexer);
13597 message = build_string (1, "");
13598 TREE_TYPE (message) = char_array_type_node;
13599 fix_string_type (message);
13601 else
13603 /* Parse the separating `,'. */
13604 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
13606 /* Parse the string-literal message. */
13607 message = cp_parser_string_literal (parser,
13608 /*translate=*/false,
13609 /*wide_ok=*/true);
13611 /* A `)' completes the static assertion. */
13612 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
13613 cp_parser_skip_to_closing_parenthesis (parser,
13614 /*recovering=*/true,
13615 /*or_comma=*/false,
13616 /*consume_paren=*/true);
13619 /* A semicolon terminates the declaration. */
13620 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
13622 /* Complete the static assertion, which may mean either processing
13623 the static assert now or saving it for template instantiation. */
13624 finish_static_assert (condition, message, saved_loc, member_p);
13627 /* Parse the expression in decltype ( expression ). */
13629 static tree
13630 cp_parser_decltype_expr (cp_parser *parser,
13631 bool &id_expression_or_member_access_p)
13633 cp_token *id_expr_start_token;
13634 tree expr;
13636 /* Since we're going to preserve any side-effects from this parse, set up a
13637 firewall to protect our callers from cp_parser_commit_to_tentative_parse
13638 in the expression. */
13639 tentative_firewall firewall (parser);
13641 /* First, try parsing an id-expression. */
13642 id_expr_start_token = cp_lexer_peek_token (parser->lexer);
13643 cp_parser_parse_tentatively (parser);
13644 expr = cp_parser_id_expression (parser,
13645 /*template_keyword_p=*/false,
13646 /*check_dependency_p=*/true,
13647 /*template_p=*/NULL,
13648 /*declarator_p=*/false,
13649 /*optional_p=*/false);
13651 if (!cp_parser_error_occurred (parser) && expr != error_mark_node)
13653 bool non_integral_constant_expression_p = false;
13654 tree id_expression = expr;
13655 cp_id_kind idk;
13656 const char *error_msg;
13658 if (identifier_p (expr))
13659 /* Lookup the name we got back from the id-expression. */
13660 expr = cp_parser_lookup_name_simple (parser, expr,
13661 id_expr_start_token->location);
13663 if (expr
13664 && expr != error_mark_node
13665 && TREE_CODE (expr) != TYPE_DECL
13666 && (TREE_CODE (expr) != BIT_NOT_EXPR
13667 || !TYPE_P (TREE_OPERAND (expr, 0)))
13668 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
13670 /* Complete lookup of the id-expression. */
13671 expr = (finish_id_expression
13672 (id_expression, expr, parser->scope, &idk,
13673 /*integral_constant_expression_p=*/false,
13674 /*allow_non_integral_constant_expression_p=*/true,
13675 &non_integral_constant_expression_p,
13676 /*template_p=*/false,
13677 /*done=*/true,
13678 /*address_p=*/false,
13679 /*template_arg_p=*/false,
13680 &error_msg,
13681 id_expr_start_token->location));
13683 if (expr == error_mark_node)
13684 /* We found an id-expression, but it was something that we
13685 should not have found. This is an error, not something
13686 we can recover from, so note that we found an
13687 id-expression and we'll recover as gracefully as
13688 possible. */
13689 id_expression_or_member_access_p = true;
13692 if (expr
13693 && expr != error_mark_node
13694 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
13695 /* We have an id-expression. */
13696 id_expression_or_member_access_p = true;
13699 if (!id_expression_or_member_access_p)
13701 /* Abort the id-expression parse. */
13702 cp_parser_abort_tentative_parse (parser);
13704 /* Parsing tentatively, again. */
13705 cp_parser_parse_tentatively (parser);
13707 /* Parse a class member access. */
13708 expr = cp_parser_postfix_expression (parser, /*address_p=*/false,
13709 /*cast_p=*/false, /*decltype*/true,
13710 /*member_access_only_p=*/true, NULL);
13712 if (expr
13713 && expr != error_mark_node
13714 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
13715 /* We have an id-expression. */
13716 id_expression_or_member_access_p = true;
13719 if (id_expression_or_member_access_p)
13720 /* We have parsed the complete id-expression or member access. */
13721 cp_parser_parse_definitely (parser);
13722 else
13724 /* Abort our attempt to parse an id-expression or member access
13725 expression. */
13726 cp_parser_abort_tentative_parse (parser);
13728 /* Parse a full expression. */
13729 expr = cp_parser_expression (parser, /*pidk=*/NULL, /*cast_p=*/false,
13730 /*decltype_p=*/true);
13733 return expr;
13736 /* Parse a `decltype' type. Returns the type.
13738 simple-type-specifier:
13739 decltype ( expression )
13740 C++14 proposal:
13741 decltype ( auto ) */
13743 static tree
13744 cp_parser_decltype (cp_parser *parser)
13746 tree expr;
13747 bool id_expression_or_member_access_p = false;
13748 const char *saved_message;
13749 bool saved_integral_constant_expression_p;
13750 bool saved_non_integral_constant_expression_p;
13751 bool saved_greater_than_is_operator_p;
13752 cp_token *start_token = cp_lexer_peek_token (parser->lexer);
13754 if (start_token->type == CPP_DECLTYPE)
13756 /* Already parsed. */
13757 cp_lexer_consume_token (parser->lexer);
13758 return saved_checks_value (start_token->u.tree_check_value);
13761 /* Look for the `decltype' token. */
13762 if (!cp_parser_require_keyword (parser, RID_DECLTYPE, RT_DECLTYPE))
13763 return error_mark_node;
13765 /* Parse the opening `('. */
13766 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
13767 return error_mark_node;
13769 /* decltype (auto) */
13770 if (cxx_dialect >= cxx14
13771 && cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
13773 cp_lexer_consume_token (parser->lexer);
13774 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
13775 return error_mark_node;
13776 expr = make_decltype_auto ();
13777 AUTO_IS_DECLTYPE (expr) = true;
13778 goto rewrite;
13781 /* Types cannot be defined in a `decltype' expression. Save away the
13782 old message. */
13783 saved_message = parser->type_definition_forbidden_message;
13785 /* And create the new one. */
13786 parser->type_definition_forbidden_message
13787 = G_("types may not be defined in %<decltype%> expressions");
13789 /* The restrictions on constant-expressions do not apply inside
13790 decltype expressions. */
13791 saved_integral_constant_expression_p
13792 = parser->integral_constant_expression_p;
13793 saved_non_integral_constant_expression_p
13794 = parser->non_integral_constant_expression_p;
13795 parser->integral_constant_expression_p = false;
13797 /* Within a parenthesized expression, a `>' token is always
13798 the greater-than operator. */
13799 saved_greater_than_is_operator_p
13800 = parser->greater_than_is_operator_p;
13801 parser->greater_than_is_operator_p = true;
13803 /* Do not actually evaluate the expression. */
13804 ++cp_unevaluated_operand;
13806 /* Do not warn about problems with the expression. */
13807 ++c_inhibit_evaluation_warnings;
13809 expr = cp_parser_decltype_expr (parser, id_expression_or_member_access_p);
13811 /* Go back to evaluating expressions. */
13812 --cp_unevaluated_operand;
13813 --c_inhibit_evaluation_warnings;
13815 /* The `>' token might be the end of a template-id or
13816 template-parameter-list now. */
13817 parser->greater_than_is_operator_p
13818 = saved_greater_than_is_operator_p;
13820 /* Restore the old message and the integral constant expression
13821 flags. */
13822 parser->type_definition_forbidden_message = saved_message;
13823 parser->integral_constant_expression_p
13824 = saved_integral_constant_expression_p;
13825 parser->non_integral_constant_expression_p
13826 = saved_non_integral_constant_expression_p;
13828 /* Parse to the closing `)'. */
13829 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
13831 cp_parser_skip_to_closing_parenthesis (parser, true, false,
13832 /*consume_paren=*/true);
13833 return error_mark_node;
13836 expr = finish_decltype_type (expr, id_expression_or_member_access_p,
13837 tf_warning_or_error);
13839 rewrite:
13840 /* Replace the decltype with a CPP_DECLTYPE so we don't need to parse
13841 it again. */
13842 start_token->type = CPP_DECLTYPE;
13843 start_token->u.tree_check_value = ggc_cleared_alloc<struct tree_check> ();
13844 start_token->u.tree_check_value->value = expr;
13845 start_token->u.tree_check_value->checks = get_deferred_access_checks ();
13846 start_token->keyword = RID_MAX;
13847 cp_lexer_purge_tokens_after (parser->lexer, start_token);
13849 return expr;
13852 /* Special member functions [gram.special] */
13854 /* Parse a conversion-function-id.
13856 conversion-function-id:
13857 operator conversion-type-id
13859 Returns an IDENTIFIER_NODE representing the operator. */
13861 static tree
13862 cp_parser_conversion_function_id (cp_parser* parser)
13864 tree type;
13865 tree saved_scope;
13866 tree saved_qualifying_scope;
13867 tree saved_object_scope;
13868 tree pushed_scope = NULL_TREE;
13870 /* Look for the `operator' token. */
13871 if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
13872 return error_mark_node;
13873 /* When we parse the conversion-type-id, the current scope will be
13874 reset. However, we need that information in able to look up the
13875 conversion function later, so we save it here. */
13876 saved_scope = parser->scope;
13877 saved_qualifying_scope = parser->qualifying_scope;
13878 saved_object_scope = parser->object_scope;
13879 /* We must enter the scope of the class so that the names of
13880 entities declared within the class are available in the
13881 conversion-type-id. For example, consider:
13883 struct S {
13884 typedef int I;
13885 operator I();
13888 S::operator I() { ... }
13890 In order to see that `I' is a type-name in the definition, we
13891 must be in the scope of `S'. */
13892 if (saved_scope)
13893 pushed_scope = push_scope (saved_scope);
13894 /* Parse the conversion-type-id. */
13895 type = cp_parser_conversion_type_id (parser);
13896 /* Leave the scope of the class, if any. */
13897 if (pushed_scope)
13898 pop_scope (pushed_scope);
13899 /* Restore the saved scope. */
13900 parser->scope = saved_scope;
13901 parser->qualifying_scope = saved_qualifying_scope;
13902 parser->object_scope = saved_object_scope;
13903 /* If the TYPE is invalid, indicate failure. */
13904 if (type == error_mark_node)
13905 return error_mark_node;
13906 return mangle_conv_op_name_for_type (type);
13909 /* Parse a conversion-type-id:
13911 conversion-type-id:
13912 type-specifier-seq conversion-declarator [opt]
13914 Returns the TYPE specified. */
13916 static tree
13917 cp_parser_conversion_type_id (cp_parser* parser)
13919 tree attributes;
13920 cp_decl_specifier_seq type_specifiers;
13921 cp_declarator *declarator;
13922 tree type_specified;
13923 const char *saved_message;
13925 /* Parse the attributes. */
13926 attributes = cp_parser_attributes_opt (parser);
13928 saved_message = parser->type_definition_forbidden_message;
13929 parser->type_definition_forbidden_message
13930 = G_("types may not be defined in a conversion-type-id");
13932 /* Parse the type-specifiers. */
13933 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
13934 /*is_trailing_return=*/false,
13935 &type_specifiers);
13937 parser->type_definition_forbidden_message = saved_message;
13939 /* If that didn't work, stop. */
13940 if (type_specifiers.type == error_mark_node)
13941 return error_mark_node;
13942 /* Parse the conversion-declarator. */
13943 declarator = cp_parser_conversion_declarator_opt (parser);
13945 type_specified = grokdeclarator (declarator, &type_specifiers, TYPENAME,
13946 /*initialized=*/0, &attributes);
13947 if (attributes)
13948 cplus_decl_attributes (&type_specified, attributes, /*flags=*/0);
13950 /* Don't give this error when parsing tentatively. This happens to
13951 work because we always parse this definitively once. */
13952 if (! cp_parser_uncommitted_to_tentative_parse_p (parser)
13953 && type_uses_auto (type_specified))
13955 if (cxx_dialect < cxx14)
13957 error ("invalid use of %<auto%> in conversion operator");
13958 return error_mark_node;
13960 else if (template_parm_scope_p ())
13961 warning (0, "use of %<auto%> in member template "
13962 "conversion operator can never be deduced");
13965 return type_specified;
13968 /* Parse an (optional) conversion-declarator.
13970 conversion-declarator:
13971 ptr-operator conversion-declarator [opt]
13975 static cp_declarator *
13976 cp_parser_conversion_declarator_opt (cp_parser* parser)
13978 enum tree_code code;
13979 tree class_type, std_attributes = NULL_TREE;
13980 cp_cv_quals cv_quals;
13982 /* We don't know if there's a ptr-operator next, or not. */
13983 cp_parser_parse_tentatively (parser);
13984 /* Try the ptr-operator. */
13985 code = cp_parser_ptr_operator (parser, &class_type, &cv_quals,
13986 &std_attributes);
13987 /* If it worked, look for more conversion-declarators. */
13988 if (cp_parser_parse_definitely (parser))
13990 cp_declarator *declarator;
13992 /* Parse another optional declarator. */
13993 declarator = cp_parser_conversion_declarator_opt (parser);
13995 declarator = cp_parser_make_indirect_declarator
13996 (code, class_type, cv_quals, declarator, std_attributes);
13998 return declarator;
14001 return NULL;
14004 /* Parse an (optional) ctor-initializer.
14006 ctor-initializer:
14007 : mem-initializer-list
14009 Returns TRUE iff the ctor-initializer was actually present. */
14011 static bool
14012 cp_parser_ctor_initializer_opt (cp_parser* parser)
14014 /* If the next token is not a `:', then there is no
14015 ctor-initializer. */
14016 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
14018 /* Do default initialization of any bases and members. */
14019 if (DECL_CONSTRUCTOR_P (current_function_decl))
14020 finish_mem_initializers (NULL_TREE);
14022 return false;
14025 /* Consume the `:' token. */
14026 cp_lexer_consume_token (parser->lexer);
14027 /* And the mem-initializer-list. */
14028 cp_parser_mem_initializer_list (parser);
14030 return true;
14033 /* Parse a mem-initializer-list.
14035 mem-initializer-list:
14036 mem-initializer ... [opt]
14037 mem-initializer ... [opt] , mem-initializer-list */
14039 static void
14040 cp_parser_mem_initializer_list (cp_parser* parser)
14042 tree mem_initializer_list = NULL_TREE;
14043 tree target_ctor = error_mark_node;
14044 cp_token *token = cp_lexer_peek_token (parser->lexer);
14046 /* Let the semantic analysis code know that we are starting the
14047 mem-initializer-list. */
14048 if (!DECL_CONSTRUCTOR_P (current_function_decl))
14049 error_at (token->location,
14050 "only constructors take member initializers");
14052 /* Loop through the list. */
14053 while (true)
14055 tree mem_initializer;
14057 token = cp_lexer_peek_token (parser->lexer);
14058 /* Parse the mem-initializer. */
14059 mem_initializer = cp_parser_mem_initializer (parser);
14060 /* If the next token is a `...', we're expanding member initializers. */
14061 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
14063 /* Consume the `...'. */
14064 cp_lexer_consume_token (parser->lexer);
14066 /* The TREE_PURPOSE must be a _TYPE, because base-specifiers
14067 can be expanded but members cannot. */
14068 if (mem_initializer != error_mark_node
14069 && !TYPE_P (TREE_PURPOSE (mem_initializer)))
14071 error_at (token->location,
14072 "cannot expand initializer for member %<%D%>",
14073 TREE_PURPOSE (mem_initializer));
14074 mem_initializer = error_mark_node;
14077 /* Construct the pack expansion type. */
14078 if (mem_initializer != error_mark_node)
14079 mem_initializer = make_pack_expansion (mem_initializer);
14081 if (target_ctor != error_mark_node
14082 && mem_initializer != error_mark_node)
14084 error ("mem-initializer for %qD follows constructor delegation",
14085 TREE_PURPOSE (mem_initializer));
14086 mem_initializer = error_mark_node;
14088 /* Look for a target constructor. */
14089 if (mem_initializer != error_mark_node
14090 && CLASS_TYPE_P (TREE_PURPOSE (mem_initializer))
14091 && same_type_p (TREE_PURPOSE (mem_initializer), current_class_type))
14093 maybe_warn_cpp0x (CPP0X_DELEGATING_CTORS);
14094 if (mem_initializer_list)
14096 error ("constructor delegation follows mem-initializer for %qD",
14097 TREE_PURPOSE (mem_initializer_list));
14098 mem_initializer = error_mark_node;
14100 target_ctor = mem_initializer;
14102 /* Add it to the list, unless it was erroneous. */
14103 if (mem_initializer != error_mark_node)
14105 TREE_CHAIN (mem_initializer) = mem_initializer_list;
14106 mem_initializer_list = mem_initializer;
14108 /* If the next token is not a `,', we're done. */
14109 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
14110 break;
14111 /* Consume the `,' token. */
14112 cp_lexer_consume_token (parser->lexer);
14115 /* Perform semantic analysis. */
14116 if (DECL_CONSTRUCTOR_P (current_function_decl))
14117 finish_mem_initializers (mem_initializer_list);
14120 /* Parse a mem-initializer.
14122 mem-initializer:
14123 mem-initializer-id ( expression-list [opt] )
14124 mem-initializer-id braced-init-list
14126 GNU extension:
14128 mem-initializer:
14129 ( expression-list [opt] )
14131 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
14132 class) or FIELD_DECL (for a non-static data member) to initialize;
14133 the TREE_VALUE is the expression-list. An empty initialization
14134 list is represented by void_list_node. */
14136 static tree
14137 cp_parser_mem_initializer (cp_parser* parser)
14139 tree mem_initializer_id;
14140 tree expression_list;
14141 tree member;
14142 cp_token *token = cp_lexer_peek_token (parser->lexer);
14144 /* Find out what is being initialized. */
14145 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
14147 permerror (token->location,
14148 "anachronistic old-style base class initializer");
14149 mem_initializer_id = NULL_TREE;
14151 else
14153 mem_initializer_id = cp_parser_mem_initializer_id (parser);
14154 if (mem_initializer_id == error_mark_node)
14155 return mem_initializer_id;
14157 member = expand_member_init (mem_initializer_id);
14158 if (member && !DECL_P (member))
14159 in_base_initializer = 1;
14161 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
14163 bool expr_non_constant_p;
14164 cp_lexer_set_source_position (parser->lexer);
14165 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
14166 expression_list = cp_parser_braced_list (parser, &expr_non_constant_p);
14167 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
14168 expression_list = build_tree_list (NULL_TREE, expression_list);
14170 else
14172 vec<tree, va_gc> *vec;
14173 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
14174 /*cast_p=*/false,
14175 /*allow_expansion_p=*/true,
14176 /*non_constant_p=*/NULL);
14177 if (vec == NULL)
14178 return error_mark_node;
14179 expression_list = build_tree_list_vec (vec);
14180 release_tree_vector (vec);
14183 if (expression_list == error_mark_node)
14184 return error_mark_node;
14185 if (!expression_list)
14186 expression_list = void_type_node;
14188 in_base_initializer = 0;
14190 return member ? build_tree_list (member, expression_list) : error_mark_node;
14193 /* Parse a mem-initializer-id.
14195 mem-initializer-id:
14196 :: [opt] nested-name-specifier [opt] class-name
14197 decltype-specifier (C++11)
14198 identifier
14200 Returns a TYPE indicating the class to be initialized for the first
14201 production (and the second in C++11). Returns an IDENTIFIER_NODE
14202 indicating the data member to be initialized for the last production. */
14204 static tree
14205 cp_parser_mem_initializer_id (cp_parser* parser)
14207 bool global_scope_p;
14208 bool nested_name_specifier_p;
14209 bool template_p = false;
14210 tree id;
14212 cp_token *token = cp_lexer_peek_token (parser->lexer);
14214 /* `typename' is not allowed in this context ([temp.res]). */
14215 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
14217 error_at (token->location,
14218 "keyword %<typename%> not allowed in this context (a qualified "
14219 "member initializer is implicitly a type)");
14220 cp_lexer_consume_token (parser->lexer);
14222 /* Look for the optional `::' operator. */
14223 global_scope_p
14224 = (cp_parser_global_scope_opt (parser,
14225 /*current_scope_valid_p=*/false)
14226 != NULL_TREE);
14227 /* Look for the optional nested-name-specifier. The simplest way to
14228 implement:
14230 [temp.res]
14232 The keyword `typename' is not permitted in a base-specifier or
14233 mem-initializer; in these contexts a qualified name that
14234 depends on a template-parameter is implicitly assumed to be a
14235 type name.
14237 is to assume that we have seen the `typename' keyword at this
14238 point. */
14239 nested_name_specifier_p
14240 = (cp_parser_nested_name_specifier_opt (parser,
14241 /*typename_keyword_p=*/true,
14242 /*check_dependency_p=*/true,
14243 /*type_p=*/true,
14244 /*is_declaration=*/true)
14245 != NULL_TREE);
14246 if (nested_name_specifier_p)
14247 template_p = cp_parser_optional_template_keyword (parser);
14248 /* If there is a `::' operator or a nested-name-specifier, then we
14249 are definitely looking for a class-name. */
14250 if (global_scope_p || nested_name_specifier_p)
14251 return cp_parser_class_name (parser,
14252 /*typename_keyword_p=*/true,
14253 /*template_keyword_p=*/template_p,
14254 typename_type,
14255 /*check_dependency_p=*/true,
14256 /*class_head_p=*/false,
14257 /*is_declaration=*/true);
14258 /* Otherwise, we could also be looking for an ordinary identifier. */
14259 cp_parser_parse_tentatively (parser);
14260 if (cp_lexer_next_token_is_decltype (parser->lexer))
14261 /* Try a decltype-specifier. */
14262 id = cp_parser_decltype (parser);
14263 else
14264 /* Otherwise, try a class-name. */
14265 id = cp_parser_class_name (parser,
14266 /*typename_keyword_p=*/true,
14267 /*template_keyword_p=*/false,
14268 none_type,
14269 /*check_dependency_p=*/true,
14270 /*class_head_p=*/false,
14271 /*is_declaration=*/true);
14272 /* If we found one, we're done. */
14273 if (cp_parser_parse_definitely (parser))
14274 return id;
14275 /* Otherwise, look for an ordinary identifier. */
14276 return cp_parser_identifier (parser);
14279 /* Overloading [gram.over] */
14281 /* Parse an operator-function-id.
14283 operator-function-id:
14284 operator operator
14286 Returns an IDENTIFIER_NODE for the operator which is a
14287 human-readable spelling of the identifier, e.g., `operator +'. */
14289 static cp_expr
14290 cp_parser_operator_function_id (cp_parser* parser)
14292 /* Look for the `operator' keyword. */
14293 if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
14294 return error_mark_node;
14295 /* And then the name of the operator itself. */
14296 return cp_parser_operator (parser);
14299 /* Return an identifier node for a user-defined literal operator.
14300 The suffix identifier is chained to the operator name identifier. */
14302 tree
14303 cp_literal_operator_id (const char* name)
14305 tree identifier;
14306 char *buffer = XNEWVEC (char, strlen (UDLIT_OP_ANSI_PREFIX)
14307 + strlen (name) + 10);
14308 sprintf (buffer, UDLIT_OP_ANSI_FORMAT, name);
14309 identifier = get_identifier (buffer);
14311 return identifier;
14314 /* Parse an operator.
14316 operator:
14317 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
14318 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
14319 || ++ -- , ->* -> () []
14321 GNU Extensions:
14323 operator:
14324 <? >? <?= >?=
14326 Returns an IDENTIFIER_NODE for the operator which is a
14327 human-readable spelling of the identifier, e.g., `operator +'. */
14329 static cp_expr
14330 cp_parser_operator (cp_parser* parser)
14332 tree id = NULL_TREE;
14333 cp_token *token;
14334 bool utf8 = false;
14336 /* Peek at the next token. */
14337 token = cp_lexer_peek_token (parser->lexer);
14339 location_t start_loc = token->location;
14341 /* Figure out which operator we have. */
14342 switch (token->type)
14344 case CPP_KEYWORD:
14346 enum tree_code op;
14348 /* The keyword should be either `new' or `delete'. */
14349 if (token->keyword == RID_NEW)
14350 op = NEW_EXPR;
14351 else if (token->keyword == RID_DELETE)
14352 op = DELETE_EXPR;
14353 else
14354 break;
14356 /* Consume the `new' or `delete' token. */
14357 location_t end_loc = cp_lexer_consume_token (parser->lexer)->location;
14359 /* Peek at the next token. */
14360 token = cp_lexer_peek_token (parser->lexer);
14361 /* If it's a `[' token then this is the array variant of the
14362 operator. */
14363 if (token->type == CPP_OPEN_SQUARE)
14365 /* Consume the `[' token. */
14366 cp_lexer_consume_token (parser->lexer);
14367 /* Look for the `]' token. */
14368 if (cp_token *close_token
14369 = cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
14370 end_loc = close_token->location;
14371 id = cp_operator_id (op == NEW_EXPR
14372 ? VEC_NEW_EXPR : VEC_DELETE_EXPR);
14374 /* Otherwise, we have the non-array variant. */
14375 else
14376 id = cp_operator_id (op);
14378 location_t loc = make_location (start_loc, start_loc, end_loc);
14380 return cp_expr (id, loc);
14383 case CPP_PLUS:
14384 id = cp_operator_id (PLUS_EXPR);
14385 break;
14387 case CPP_MINUS:
14388 id = cp_operator_id (MINUS_EXPR);
14389 break;
14391 case CPP_MULT:
14392 id = cp_operator_id (MULT_EXPR);
14393 break;
14395 case CPP_DIV:
14396 id = cp_operator_id (TRUNC_DIV_EXPR);
14397 break;
14399 case CPP_MOD:
14400 id = cp_operator_id (TRUNC_MOD_EXPR);
14401 break;
14403 case CPP_XOR:
14404 id = cp_operator_id (BIT_XOR_EXPR);
14405 break;
14407 case CPP_AND:
14408 id = cp_operator_id (BIT_AND_EXPR);
14409 break;
14411 case CPP_OR:
14412 id = cp_operator_id (BIT_IOR_EXPR);
14413 break;
14415 case CPP_COMPL:
14416 id = cp_operator_id (BIT_NOT_EXPR);
14417 break;
14419 case CPP_NOT:
14420 id = cp_operator_id (TRUTH_NOT_EXPR);
14421 break;
14423 case CPP_EQ:
14424 id = cp_assignment_operator_id (NOP_EXPR);
14425 break;
14427 case CPP_LESS:
14428 id = cp_operator_id (LT_EXPR);
14429 break;
14431 case CPP_GREATER:
14432 id = cp_operator_id (GT_EXPR);
14433 break;
14435 case CPP_PLUS_EQ:
14436 id = cp_assignment_operator_id (PLUS_EXPR);
14437 break;
14439 case CPP_MINUS_EQ:
14440 id = cp_assignment_operator_id (MINUS_EXPR);
14441 break;
14443 case CPP_MULT_EQ:
14444 id = cp_assignment_operator_id (MULT_EXPR);
14445 break;
14447 case CPP_DIV_EQ:
14448 id = cp_assignment_operator_id (TRUNC_DIV_EXPR);
14449 break;
14451 case CPP_MOD_EQ:
14452 id = cp_assignment_operator_id (TRUNC_MOD_EXPR);
14453 break;
14455 case CPP_XOR_EQ:
14456 id = cp_assignment_operator_id (BIT_XOR_EXPR);
14457 break;
14459 case CPP_AND_EQ:
14460 id = cp_assignment_operator_id (BIT_AND_EXPR);
14461 break;
14463 case CPP_OR_EQ:
14464 id = cp_assignment_operator_id (BIT_IOR_EXPR);
14465 break;
14467 case CPP_LSHIFT:
14468 id = cp_operator_id (LSHIFT_EXPR);
14469 break;
14471 case CPP_RSHIFT:
14472 id = cp_operator_id (RSHIFT_EXPR);
14473 break;
14475 case CPP_LSHIFT_EQ:
14476 id = cp_assignment_operator_id (LSHIFT_EXPR);
14477 break;
14479 case CPP_RSHIFT_EQ:
14480 id = cp_assignment_operator_id (RSHIFT_EXPR);
14481 break;
14483 case CPP_EQ_EQ:
14484 id = cp_operator_id (EQ_EXPR);
14485 break;
14487 case CPP_NOT_EQ:
14488 id = cp_operator_id (NE_EXPR);
14489 break;
14491 case CPP_LESS_EQ:
14492 id = cp_operator_id (LE_EXPR);
14493 break;
14495 case CPP_GREATER_EQ:
14496 id = cp_operator_id (GE_EXPR);
14497 break;
14499 case CPP_AND_AND:
14500 id = cp_operator_id (TRUTH_ANDIF_EXPR);
14501 break;
14503 case CPP_OR_OR:
14504 id = cp_operator_id (TRUTH_ORIF_EXPR);
14505 break;
14507 case CPP_PLUS_PLUS:
14508 id = cp_operator_id (POSTINCREMENT_EXPR);
14509 break;
14511 case CPP_MINUS_MINUS:
14512 id = cp_operator_id (PREDECREMENT_EXPR);
14513 break;
14515 case CPP_COMMA:
14516 id = cp_operator_id (COMPOUND_EXPR);
14517 break;
14519 case CPP_DEREF_STAR:
14520 id = cp_operator_id (MEMBER_REF);
14521 break;
14523 case CPP_DEREF:
14524 id = cp_operator_id (COMPONENT_REF);
14525 break;
14527 case CPP_OPEN_PAREN:
14528 /* Consume the `('. */
14529 cp_lexer_consume_token (parser->lexer);
14530 /* Look for the matching `)'. */
14531 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
14532 return cp_operator_id (CALL_EXPR);
14534 case CPP_OPEN_SQUARE:
14535 /* Consume the `['. */
14536 cp_lexer_consume_token (parser->lexer);
14537 /* Look for the matching `]'. */
14538 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
14539 return cp_operator_id (ARRAY_REF);
14541 case CPP_UTF8STRING:
14542 case CPP_UTF8STRING_USERDEF:
14543 utf8 = true;
14544 /* FALLTHRU */
14545 case CPP_STRING:
14546 case CPP_WSTRING:
14547 case CPP_STRING16:
14548 case CPP_STRING32:
14549 case CPP_STRING_USERDEF:
14550 case CPP_WSTRING_USERDEF:
14551 case CPP_STRING16_USERDEF:
14552 case CPP_STRING32_USERDEF:
14554 tree str, string_tree;
14555 int sz, len;
14557 if (cxx_dialect == cxx98)
14558 maybe_warn_cpp0x (CPP0X_USER_DEFINED_LITERALS);
14560 /* Consume the string. */
14561 str = cp_parser_string_literal (parser, /*translate=*/true,
14562 /*wide_ok=*/true, /*lookup_udlit=*/false);
14563 if (str == error_mark_node)
14564 return error_mark_node;
14565 else if (TREE_CODE (str) == USERDEF_LITERAL)
14567 string_tree = USERDEF_LITERAL_VALUE (str);
14568 id = USERDEF_LITERAL_SUFFIX_ID (str);
14570 else
14572 string_tree = str;
14573 /* Look for the suffix identifier. */
14574 token = cp_lexer_peek_token (parser->lexer);
14575 if (token->type == CPP_NAME)
14576 id = cp_parser_identifier (parser);
14577 else if (token->type == CPP_KEYWORD)
14579 error ("unexpected keyword;"
14580 " remove space between quotes and suffix identifier");
14581 return error_mark_node;
14583 else
14585 error ("expected suffix identifier");
14586 return error_mark_node;
14589 sz = TREE_INT_CST_LOW (TYPE_SIZE_UNIT
14590 (TREE_TYPE (TREE_TYPE (string_tree))));
14591 len = TREE_STRING_LENGTH (string_tree) / sz - 1;
14592 if (len != 0)
14594 error ("expected empty string after %<operator%> keyword");
14595 return error_mark_node;
14597 if (utf8 || TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (string_tree)))
14598 != char_type_node)
14600 error ("invalid encoding prefix in literal operator");
14601 return error_mark_node;
14603 if (id != error_mark_node)
14605 const char *name = IDENTIFIER_POINTER (id);
14606 id = cp_literal_operator_id (name);
14608 return id;
14611 default:
14612 /* Anything else is an error. */
14613 break;
14616 /* If we have selected an identifier, we need to consume the
14617 operator token. */
14618 if (id)
14619 cp_lexer_consume_token (parser->lexer);
14620 /* Otherwise, no valid operator name was present. */
14621 else
14623 cp_parser_error (parser, "expected operator");
14624 id = error_mark_node;
14627 return cp_expr (id, start_loc);
14630 /* Parse a template-declaration.
14632 template-declaration:
14633 export [opt] template < template-parameter-list > declaration
14635 If MEMBER_P is TRUE, this template-declaration occurs within a
14636 class-specifier.
14638 The grammar rule given by the standard isn't correct. What
14639 is really meant is:
14641 template-declaration:
14642 export [opt] template-parameter-list-seq
14643 decl-specifier-seq [opt] init-declarator [opt] ;
14644 export [opt] template-parameter-list-seq
14645 function-definition
14647 template-parameter-list-seq:
14648 template-parameter-list-seq [opt]
14649 template < template-parameter-list >
14651 Concept Extensions:
14653 template-parameter-list-seq:
14654 template < template-parameter-list > requires-clause [opt]
14656 requires-clause:
14657 requires logical-or-expression */
14659 static void
14660 cp_parser_template_declaration (cp_parser* parser, bool member_p)
14662 /* Check for `export'. */
14663 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
14665 /* Consume the `export' token. */
14666 cp_lexer_consume_token (parser->lexer);
14667 /* Warn that we do not support `export'. */
14668 warning (0, "keyword %<export%> not implemented, and will be ignored");
14671 cp_parser_template_declaration_after_export (parser, member_p);
14674 /* Parse a template-parameter-list.
14676 template-parameter-list:
14677 template-parameter
14678 template-parameter-list , template-parameter
14680 Returns a TREE_LIST. Each node represents a template parameter.
14681 The nodes are connected via their TREE_CHAINs. */
14683 static tree
14684 cp_parser_template_parameter_list (cp_parser* parser)
14686 tree parameter_list = NULL_TREE;
14688 begin_template_parm_list ();
14690 /* The loop below parses the template parms. We first need to know
14691 the total number of template parms to be able to compute proper
14692 canonical types of each dependent type. So after the loop, when
14693 we know the total number of template parms,
14694 end_template_parm_list computes the proper canonical types and
14695 fixes up the dependent types accordingly. */
14696 while (true)
14698 tree parameter;
14699 bool is_non_type;
14700 bool is_parameter_pack;
14701 location_t parm_loc;
14703 /* Parse the template-parameter. */
14704 parm_loc = cp_lexer_peek_token (parser->lexer)->location;
14705 parameter = cp_parser_template_parameter (parser,
14706 &is_non_type,
14707 &is_parameter_pack);
14708 /* Add it to the list. */
14709 if (parameter != error_mark_node)
14710 parameter_list = process_template_parm (parameter_list,
14711 parm_loc,
14712 parameter,
14713 is_non_type,
14714 is_parameter_pack);
14715 else
14717 tree err_parm = build_tree_list (parameter, parameter);
14718 parameter_list = chainon (parameter_list, err_parm);
14721 /* If the next token is not a `,', we're done. */
14722 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
14723 break;
14724 /* Otherwise, consume the `,' token. */
14725 cp_lexer_consume_token (parser->lexer);
14728 return end_template_parm_list (parameter_list);
14731 /* Parse a introduction-list.
14733 introduction-list:
14734 introduced-parameter
14735 introduction-list , introduced-parameter
14737 introduced-parameter:
14738 ...[opt] identifier
14740 Returns a TREE_VEC of WILDCARD_DECLs. If the parameter is a pack
14741 then the introduced parm will have WILDCARD_PACK_P set. In addition, the
14742 WILDCARD_DECL will also have DECL_NAME set and token location in
14743 DECL_SOURCE_LOCATION. */
14745 static tree
14746 cp_parser_introduction_list (cp_parser *parser)
14748 vec<tree, va_gc> *introduction_vec = make_tree_vector ();
14750 while (true)
14752 bool is_pack = cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS);
14753 if (is_pack)
14754 cp_lexer_consume_token (parser->lexer);
14756 /* Build placeholder. */
14757 tree parm = build_nt (WILDCARD_DECL);
14758 DECL_SOURCE_LOCATION (parm)
14759 = cp_lexer_peek_token (parser->lexer)->location;
14760 DECL_NAME (parm) = cp_parser_identifier (parser);
14761 WILDCARD_PACK_P (parm) = is_pack;
14762 vec_safe_push (introduction_vec, parm);
14764 /* If the next token is not a `,', we're done. */
14765 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
14766 break;
14767 /* Otherwise, consume the `,' token. */
14768 cp_lexer_consume_token (parser->lexer);
14771 /* Convert the vec into a TREE_VEC. */
14772 tree introduction_list = make_tree_vec (introduction_vec->length ());
14773 unsigned int n;
14774 tree parm;
14775 FOR_EACH_VEC_ELT (*introduction_vec, n, parm)
14776 TREE_VEC_ELT (introduction_list, n) = parm;
14778 release_tree_vector (introduction_vec);
14779 return introduction_list;
14782 /* Given a declarator, get the declarator-id part, or NULL_TREE if this
14783 is an abstract declarator. */
14785 static inline cp_declarator*
14786 get_id_declarator (cp_declarator *declarator)
14788 cp_declarator *d = declarator;
14789 while (d && d->kind != cdk_id)
14790 d = d->declarator;
14791 return d;
14794 /* Get the unqualified-id from the DECLARATOR or NULL_TREE if this
14795 is an abstract declarator. */
14797 static inline tree
14798 get_unqualified_id (cp_declarator *declarator)
14800 declarator = get_id_declarator (declarator);
14801 if (declarator)
14802 return declarator->u.id.unqualified_name;
14803 else
14804 return NULL_TREE;
14807 /* Returns true if DECL represents a constrained-parameter. */
14809 static inline bool
14810 is_constrained_parameter (tree decl)
14812 return (decl
14813 && TREE_CODE (decl) == TYPE_DECL
14814 && CONSTRAINED_PARM_CONCEPT (decl)
14815 && DECL_P (CONSTRAINED_PARM_CONCEPT (decl)));
14818 /* Returns true if PARM declares a constrained-parameter. */
14820 static inline bool
14821 is_constrained_parameter (cp_parameter_declarator *parm)
14823 return is_constrained_parameter (parm->decl_specifiers.type);
14826 /* Check that the type parameter is only a declarator-id, and that its
14827 type is not cv-qualified. */
14829 bool
14830 cp_parser_check_constrained_type_parm (cp_parser *parser,
14831 cp_parameter_declarator *parm)
14833 if (!parm->declarator)
14834 return true;
14836 if (parm->declarator->kind != cdk_id)
14838 cp_parser_error (parser, "invalid constrained type parameter");
14839 return false;
14842 /* Don't allow cv-qualified type parameters. */
14843 if (decl_spec_seq_has_spec_p (&parm->decl_specifiers, ds_const)
14844 || decl_spec_seq_has_spec_p (&parm->decl_specifiers, ds_volatile))
14846 cp_parser_error (parser, "cv-qualified type parameter");
14847 return false;
14850 return true;
14853 /* Finish parsing/processing a template type parameter and checking
14854 various restrictions. */
14856 static inline tree
14857 cp_parser_constrained_type_template_parm (cp_parser *parser,
14858 tree id,
14859 cp_parameter_declarator* parmdecl)
14861 if (cp_parser_check_constrained_type_parm (parser, parmdecl))
14862 return finish_template_type_parm (class_type_node, id);
14863 else
14864 return error_mark_node;
14867 static tree
14868 finish_constrained_template_template_parm (tree proto, tree id)
14870 /* FIXME: This should probably be copied, and we may need to adjust
14871 the template parameter depths. */
14872 tree saved_parms = current_template_parms;
14873 begin_template_parm_list ();
14874 current_template_parms = DECL_TEMPLATE_PARMS (proto);
14875 end_template_parm_list ();
14877 tree parm = finish_template_template_parm (class_type_node, id);
14878 current_template_parms = saved_parms;
14880 return parm;
14883 /* Finish parsing/processing a template template parameter by borrowing
14884 the template parameter list from the prototype parameter. */
14886 static tree
14887 cp_parser_constrained_template_template_parm (cp_parser *parser,
14888 tree proto,
14889 tree id,
14890 cp_parameter_declarator *parmdecl)
14892 if (!cp_parser_check_constrained_type_parm (parser, parmdecl))
14893 return error_mark_node;
14894 return finish_constrained_template_template_parm (proto, id);
14897 /* Create a new non-type template parameter from the given PARM
14898 declarator. */
14900 static tree
14901 constrained_non_type_template_parm (bool *is_non_type,
14902 cp_parameter_declarator *parm)
14904 *is_non_type = true;
14905 cp_declarator *decl = parm->declarator;
14906 cp_decl_specifier_seq *specs = &parm->decl_specifiers;
14907 specs->type = TREE_TYPE (DECL_INITIAL (specs->type));
14908 return grokdeclarator (decl, specs, TPARM, 0, NULL);
14911 /* Build a constrained template parameter based on the PARMDECL
14912 declarator. The type of PARMDECL is the constrained type, which
14913 refers to the prototype template parameter that ultimately
14914 specifies the type of the declared parameter. */
14916 static tree
14917 finish_constrained_parameter (cp_parser *parser,
14918 cp_parameter_declarator *parmdecl,
14919 bool *is_non_type,
14920 bool *is_parameter_pack)
14922 tree decl = parmdecl->decl_specifiers.type;
14923 tree id = get_unqualified_id (parmdecl->declarator);
14924 tree def = parmdecl->default_argument;
14925 tree proto = DECL_INITIAL (decl);
14927 /* A template parameter constrained by a variadic concept shall also
14928 be declared as a template parameter pack. */
14929 bool is_variadic = template_parameter_pack_p (proto);
14930 if (is_variadic && !*is_parameter_pack)
14931 cp_parser_error (parser, "variadic constraint introduced without %<...%>");
14933 /* Build the parameter. Return an error if the declarator was invalid. */
14934 tree parm;
14935 if (TREE_CODE (proto) == TYPE_DECL)
14936 parm = cp_parser_constrained_type_template_parm (parser, id, parmdecl);
14937 else if (TREE_CODE (proto) == TEMPLATE_DECL)
14938 parm = cp_parser_constrained_template_template_parm (parser, proto, id,
14939 parmdecl);
14940 else
14941 parm = constrained_non_type_template_parm (is_non_type, parmdecl);
14942 if (parm == error_mark_node)
14943 return error_mark_node;
14945 /* Finish the parameter decl and create a node attaching the
14946 default argument and constraint. */
14947 parm = build_tree_list (def, parm);
14948 TEMPLATE_PARM_CONSTRAINTS (parm) = decl;
14950 return parm;
14953 /* Returns true if the parsed type actually represents the declaration
14954 of a type template-parameter. */
14956 static inline bool
14957 declares_constrained_type_template_parameter (tree type)
14959 return (is_constrained_parameter (type)
14960 && TREE_CODE (TREE_TYPE (type)) == TEMPLATE_TYPE_PARM);
14964 /* Returns true if the parsed type actually represents the declaration of
14965 a template template-parameter. */
14967 static bool
14968 declares_constrained_template_template_parameter (tree type)
14970 return (is_constrained_parameter (type)
14971 && TREE_CODE (TREE_TYPE (type)) == TEMPLATE_TEMPLATE_PARM);
14974 /* Parse a default argument for a type template-parameter.
14975 Note that diagnostics are handled in cp_parser_template_parameter. */
14977 static tree
14978 cp_parser_default_type_template_argument (cp_parser *parser)
14980 gcc_assert (cp_lexer_next_token_is (parser->lexer, CPP_EQ));
14982 /* Consume the `=' token. */
14983 cp_lexer_consume_token (parser->lexer);
14985 cp_token *token = cp_lexer_peek_token (parser->lexer);
14987 /* Parse the default-argument. */
14988 push_deferring_access_checks (dk_no_deferred);
14989 tree default_argument = cp_parser_type_id (parser);
14990 pop_deferring_access_checks ();
14992 if (flag_concepts && type_uses_auto (default_argument))
14994 error_at (token->location,
14995 "invalid use of %<auto%> in default template argument");
14996 return error_mark_node;
14999 return default_argument;
15002 /* Parse a default argument for a template template-parameter. */
15004 static tree
15005 cp_parser_default_template_template_argument (cp_parser *parser)
15007 gcc_assert (cp_lexer_next_token_is (parser->lexer, CPP_EQ));
15009 bool is_template;
15011 /* Consume the `='. */
15012 cp_lexer_consume_token (parser->lexer);
15013 /* Parse the id-expression. */
15014 push_deferring_access_checks (dk_no_deferred);
15015 /* save token before parsing the id-expression, for error
15016 reporting */
15017 const cp_token* token = cp_lexer_peek_token (parser->lexer);
15018 tree default_argument
15019 = cp_parser_id_expression (parser,
15020 /*template_keyword_p=*/false,
15021 /*check_dependency_p=*/true,
15022 /*template_p=*/&is_template,
15023 /*declarator_p=*/false,
15024 /*optional_p=*/false);
15025 if (TREE_CODE (default_argument) == TYPE_DECL)
15026 /* If the id-expression was a template-id that refers to
15027 a template-class, we already have the declaration here,
15028 so no further lookup is needed. */
15030 else
15031 /* Look up the name. */
15032 default_argument
15033 = cp_parser_lookup_name (parser, default_argument,
15034 none_type,
15035 /*is_template=*/is_template,
15036 /*is_namespace=*/false,
15037 /*check_dependency=*/true,
15038 /*ambiguous_decls=*/NULL,
15039 token->location);
15040 /* See if the default argument is valid. */
15041 default_argument = check_template_template_default_arg (default_argument);
15042 pop_deferring_access_checks ();
15043 return default_argument;
15046 /* Parse a template-parameter.
15048 template-parameter:
15049 type-parameter
15050 parameter-declaration
15052 If all goes well, returns a TREE_LIST. The TREE_VALUE represents
15053 the parameter. The TREE_PURPOSE is the default value, if any.
15054 Returns ERROR_MARK_NODE on failure. *IS_NON_TYPE is set to true
15055 iff this parameter is a non-type parameter. *IS_PARAMETER_PACK is
15056 set to true iff this parameter is a parameter pack. */
15058 static tree
15059 cp_parser_template_parameter (cp_parser* parser, bool *is_non_type,
15060 bool *is_parameter_pack)
15062 cp_token *token;
15063 cp_parameter_declarator *parameter_declarator;
15064 tree parm;
15066 /* Assume it is a type parameter or a template parameter. */
15067 *is_non_type = false;
15068 /* Assume it not a parameter pack. */
15069 *is_parameter_pack = false;
15070 /* Peek at the next token. */
15071 token = cp_lexer_peek_token (parser->lexer);
15072 /* If it is `class' or `template', we have a type-parameter. */
15073 if (token->keyword == RID_TEMPLATE)
15074 return cp_parser_type_parameter (parser, is_parameter_pack);
15075 /* If it is `class' or `typename' we do not know yet whether it is a
15076 type parameter or a non-type parameter. Consider:
15078 template <typename T, typename T::X X> ...
15082 template <class C, class D*> ...
15084 Here, the first parameter is a type parameter, and the second is
15085 a non-type parameter. We can tell by looking at the token after
15086 the identifier -- if it is a `,', `=', or `>' then we have a type
15087 parameter. */
15088 if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
15090 /* Peek at the token after `class' or `typename'. */
15091 token = cp_lexer_peek_nth_token (parser->lexer, 2);
15092 /* If it's an ellipsis, we have a template type parameter
15093 pack. */
15094 if (token->type == CPP_ELLIPSIS)
15095 return cp_parser_type_parameter (parser, is_parameter_pack);
15096 /* If it's an identifier, skip it. */
15097 if (token->type == CPP_NAME)
15098 token = cp_lexer_peek_nth_token (parser->lexer, 3);
15099 /* Now, see if the token looks like the end of a template
15100 parameter. */
15101 if (token->type == CPP_COMMA
15102 || token->type == CPP_EQ
15103 || token->type == CPP_GREATER)
15104 return cp_parser_type_parameter (parser, is_parameter_pack);
15107 /* Otherwise, it is a non-type parameter or a constrained parameter.
15109 [temp.param]
15111 When parsing a default template-argument for a non-type
15112 template-parameter, the first non-nested `>' is taken as the end
15113 of the template parameter-list rather than a greater-than
15114 operator. */
15115 parameter_declarator
15116 = cp_parser_parameter_declaration (parser, /*template_parm_p=*/true,
15117 /*parenthesized_p=*/NULL);
15119 if (!parameter_declarator)
15120 return error_mark_node;
15122 /* If the parameter declaration is marked as a parameter pack, set
15123 *IS_PARAMETER_PACK to notify the caller. */
15124 if (parameter_declarator->template_parameter_pack_p)
15125 *is_parameter_pack = true;
15127 if (parameter_declarator->default_argument)
15129 /* Can happen in some cases of erroneous input (c++/34892). */
15130 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
15131 /* Consume the `...' for better error recovery. */
15132 cp_lexer_consume_token (parser->lexer);
15135 // The parameter may have been constrained.
15136 if (is_constrained_parameter (parameter_declarator))
15137 return finish_constrained_parameter (parser,
15138 parameter_declarator,
15139 is_non_type,
15140 is_parameter_pack);
15142 // Now we're sure that the parameter is a non-type parameter.
15143 *is_non_type = true;
15145 parm = grokdeclarator (parameter_declarator->declarator,
15146 &parameter_declarator->decl_specifiers,
15147 TPARM, /*initialized=*/0,
15148 /*attrlist=*/NULL);
15149 if (parm == error_mark_node)
15150 return error_mark_node;
15152 return build_tree_list (parameter_declarator->default_argument, parm);
15155 /* Parse a type-parameter.
15157 type-parameter:
15158 class identifier [opt]
15159 class identifier [opt] = type-id
15160 typename identifier [opt]
15161 typename identifier [opt] = type-id
15162 template < template-parameter-list > class identifier [opt]
15163 template < template-parameter-list > class identifier [opt]
15164 = id-expression
15166 GNU Extension (variadic templates):
15168 type-parameter:
15169 class ... identifier [opt]
15170 typename ... identifier [opt]
15172 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
15173 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
15174 the declaration of the parameter.
15176 Sets *IS_PARAMETER_PACK if this is a template parameter pack. */
15178 static tree
15179 cp_parser_type_parameter (cp_parser* parser, bool *is_parameter_pack)
15181 cp_token *token;
15182 tree parameter;
15184 /* Look for a keyword to tell us what kind of parameter this is. */
15185 token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_TYPENAME_TEMPLATE);
15186 if (!token)
15187 return error_mark_node;
15189 switch (token->keyword)
15191 case RID_CLASS:
15192 case RID_TYPENAME:
15194 tree identifier;
15195 tree default_argument;
15197 /* If the next token is an ellipsis, we have a template
15198 argument pack. */
15199 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
15201 /* Consume the `...' token. */
15202 cp_lexer_consume_token (parser->lexer);
15203 maybe_warn_variadic_templates ();
15205 *is_parameter_pack = true;
15208 /* If the next token is an identifier, then it names the
15209 parameter. */
15210 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
15211 identifier = cp_parser_identifier (parser);
15212 else
15213 identifier = NULL_TREE;
15215 /* Create the parameter. */
15216 parameter = finish_template_type_parm (class_type_node, identifier);
15218 /* If the next token is an `=', we have a default argument. */
15219 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
15221 default_argument
15222 = cp_parser_default_type_template_argument (parser);
15224 /* Template parameter packs cannot have default
15225 arguments. */
15226 if (*is_parameter_pack)
15228 if (identifier)
15229 error_at (token->location,
15230 "template parameter pack %qD cannot have a "
15231 "default argument", identifier);
15232 else
15233 error_at (token->location,
15234 "template parameter packs cannot have "
15235 "default arguments");
15236 default_argument = NULL_TREE;
15238 else if (check_for_bare_parameter_packs (default_argument))
15239 default_argument = error_mark_node;
15241 else
15242 default_argument = NULL_TREE;
15244 /* Create the combined representation of the parameter and the
15245 default argument. */
15246 parameter = build_tree_list (default_argument, parameter);
15248 break;
15250 case RID_TEMPLATE:
15252 tree identifier;
15253 tree default_argument;
15255 /* Look for the `<'. */
15256 cp_parser_require (parser, CPP_LESS, RT_LESS);
15257 /* Parse the template-parameter-list. */
15258 cp_parser_template_parameter_list (parser);
15259 /* Look for the `>'. */
15260 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
15262 // If template requirements are present, parse them.
15263 if (flag_concepts)
15265 tree reqs = get_shorthand_constraints (current_template_parms);
15266 if (tree r = cp_parser_requires_clause_opt (parser))
15267 reqs = conjoin_constraints (reqs, normalize_expression (r));
15268 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) = reqs;
15271 /* Look for the `class' or 'typename' keywords. */
15272 cp_parser_type_parameter_key (parser);
15273 /* If the next token is an ellipsis, we have a template
15274 argument pack. */
15275 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
15277 /* Consume the `...' token. */
15278 cp_lexer_consume_token (parser->lexer);
15279 maybe_warn_variadic_templates ();
15281 *is_parameter_pack = true;
15283 /* If the next token is an `=', then there is a
15284 default-argument. If the next token is a `>', we are at
15285 the end of the parameter-list. If the next token is a `,',
15286 then we are at the end of this parameter. */
15287 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
15288 && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
15289 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
15291 identifier = cp_parser_identifier (parser);
15292 /* Treat invalid names as if the parameter were nameless. */
15293 if (identifier == error_mark_node)
15294 identifier = NULL_TREE;
15296 else
15297 identifier = NULL_TREE;
15299 /* Create the template parameter. */
15300 parameter = finish_template_template_parm (class_type_node,
15301 identifier);
15303 /* If the next token is an `=', then there is a
15304 default-argument. */
15305 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
15307 default_argument
15308 = cp_parser_default_template_template_argument (parser);
15310 /* Template parameter packs cannot have default
15311 arguments. */
15312 if (*is_parameter_pack)
15314 if (identifier)
15315 error_at (token->location,
15316 "template parameter pack %qD cannot "
15317 "have a default argument",
15318 identifier);
15319 else
15320 error_at (token->location, "template parameter packs cannot "
15321 "have default arguments");
15322 default_argument = NULL_TREE;
15325 else
15326 default_argument = NULL_TREE;
15328 /* Create the combined representation of the parameter and the
15329 default argument. */
15330 parameter = build_tree_list (default_argument, parameter);
15332 break;
15334 default:
15335 gcc_unreachable ();
15336 break;
15339 return parameter;
15342 /* Parse a template-id.
15344 template-id:
15345 template-name < template-argument-list [opt] >
15347 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
15348 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
15349 returned. Otherwise, if the template-name names a function, or set
15350 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
15351 names a class, returns a TYPE_DECL for the specialization.
15353 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
15354 uninstantiated templates. */
15356 static tree
15357 cp_parser_template_id (cp_parser *parser,
15358 bool template_keyword_p,
15359 bool check_dependency_p,
15360 enum tag_types tag_type,
15361 bool is_declaration)
15363 tree templ;
15364 tree arguments;
15365 tree template_id;
15366 cp_token_position start_of_id = 0;
15367 cp_token *next_token = NULL, *next_token_2 = NULL;
15368 bool is_identifier;
15370 /* If the next token corresponds to a template-id, there is no need
15371 to reparse it. */
15372 next_token = cp_lexer_peek_token (parser->lexer);
15373 if (next_token->type == CPP_TEMPLATE_ID)
15375 cp_lexer_consume_token (parser->lexer);
15376 return saved_checks_value (next_token->u.tree_check_value);
15379 /* Avoid performing name lookup if there is no possibility of
15380 finding a template-id. */
15381 if ((next_token->type != CPP_NAME && next_token->keyword != RID_OPERATOR)
15382 || (next_token->type == CPP_NAME
15383 && !cp_parser_nth_token_starts_template_argument_list_p
15384 (parser, 2)))
15386 cp_parser_error (parser, "expected template-id");
15387 return error_mark_node;
15390 /* Remember where the template-id starts. */
15391 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
15392 start_of_id = cp_lexer_token_position (parser->lexer, false);
15394 push_deferring_access_checks (dk_deferred);
15396 /* Parse the template-name. */
15397 is_identifier = false;
15398 templ = cp_parser_template_name (parser, template_keyword_p,
15399 check_dependency_p,
15400 is_declaration,
15401 tag_type,
15402 &is_identifier);
15403 if (templ == error_mark_node || is_identifier)
15405 pop_deferring_access_checks ();
15406 return templ;
15409 /* Since we're going to preserve any side-effects from this parse, set up a
15410 firewall to protect our callers from cp_parser_commit_to_tentative_parse
15411 in the template arguments. */
15412 tentative_firewall firewall (parser);
15414 /* If we find the sequence `[:' after a template-name, it's probably
15415 a digraph-typo for `< ::'. Substitute the tokens and check if we can
15416 parse correctly the argument list. */
15417 if (((next_token = cp_lexer_peek_token (parser->lexer))->type
15418 == CPP_OPEN_SQUARE)
15419 && next_token->flags & DIGRAPH
15420 && ((next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2))->type
15421 == CPP_COLON)
15422 && !(next_token_2->flags & PREV_WHITE))
15424 cp_parser_parse_tentatively (parser);
15425 /* Change `:' into `::'. */
15426 next_token_2->type = CPP_SCOPE;
15427 /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
15428 CPP_LESS. */
15429 cp_lexer_consume_token (parser->lexer);
15431 /* Parse the arguments. */
15432 arguments = cp_parser_enclosed_template_argument_list (parser);
15433 if (!cp_parser_parse_definitely (parser))
15435 /* If we couldn't parse an argument list, then we revert our changes
15436 and return simply an error. Maybe this is not a template-id
15437 after all. */
15438 next_token_2->type = CPP_COLON;
15439 cp_parser_error (parser, "expected %<<%>");
15440 pop_deferring_access_checks ();
15441 return error_mark_node;
15443 /* Otherwise, emit an error about the invalid digraph, but continue
15444 parsing because we got our argument list. */
15445 if (permerror (next_token->location,
15446 "%<<::%> cannot begin a template-argument list"))
15448 static bool hint = false;
15449 inform (next_token->location,
15450 "%<<:%> is an alternate spelling for %<[%>."
15451 " Insert whitespace between %<<%> and %<::%>");
15452 if (!hint && !flag_permissive)
15454 inform (next_token->location, "(if you use %<-fpermissive%> "
15455 "or %<-std=c++11%>, or %<-std=gnu++11%> G++ will "
15456 "accept your code)");
15457 hint = true;
15461 else
15463 /* Look for the `<' that starts the template-argument-list. */
15464 if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
15466 pop_deferring_access_checks ();
15467 return error_mark_node;
15469 /* Parse the arguments. */
15470 arguments = cp_parser_enclosed_template_argument_list (parser);
15473 /* Build a representation of the specialization. */
15474 if (identifier_p (templ))
15475 template_id = build_min_nt_loc (next_token->location,
15476 TEMPLATE_ID_EXPR,
15477 templ, arguments);
15478 else if (DECL_TYPE_TEMPLATE_P (templ)
15479 || DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
15481 bool entering_scope;
15482 /* In "template <typename T> ... A<T>::", A<T> is the abstract A
15483 template (rather than some instantiation thereof) only if
15484 is not nested within some other construct. For example, in
15485 "template <typename T> void f(T) { A<T>::", A<T> is just an
15486 instantiation of A. */
15487 entering_scope = (template_parm_scope_p ()
15488 && cp_lexer_next_token_is (parser->lexer,
15489 CPP_SCOPE));
15490 template_id
15491 = finish_template_type (templ, arguments, entering_scope);
15493 /* A template-like identifier may be a partial concept id. */
15494 else if (flag_concepts
15495 && (template_id = (cp_parser_maybe_partial_concept_id
15496 (parser, templ, arguments))))
15497 return template_id;
15498 else if (variable_template_p (templ))
15500 template_id = lookup_template_variable (templ, arguments);
15501 if (TREE_CODE (template_id) == TEMPLATE_ID_EXPR)
15502 SET_EXPR_LOCATION (template_id, next_token->location);
15504 else
15506 /* If it's not a class-template or a template-template, it should be
15507 a function-template. */
15508 gcc_assert ((DECL_FUNCTION_TEMPLATE_P (templ)
15509 || TREE_CODE (templ) == OVERLOAD
15510 || BASELINK_P (templ)));
15512 template_id = lookup_template_function (templ, arguments);
15513 if (TREE_CODE (template_id) == TEMPLATE_ID_EXPR)
15514 SET_EXPR_LOCATION (template_id, next_token->location);
15517 /* If parsing tentatively, replace the sequence of tokens that makes
15518 up the template-id with a CPP_TEMPLATE_ID token. That way,
15519 should we re-parse the token stream, we will not have to repeat
15520 the effort required to do the parse, nor will we issue duplicate
15521 error messages about problems during instantiation of the
15522 template. */
15523 if (start_of_id
15524 /* Don't do this if we had a parse error in a declarator; re-parsing
15525 might succeed if a name changes meaning (60361). */
15526 && !(cp_parser_error_occurred (parser)
15527 && cp_parser_parsing_tentatively (parser)
15528 && parser->in_declarator_p))
15530 cp_token *token = cp_lexer_token_at (parser->lexer, start_of_id);
15532 /* Reset the contents of the START_OF_ID token. */
15533 token->type = CPP_TEMPLATE_ID;
15535 /* Update the location to be of the form:
15536 template-name < template-argument-list [opt] >
15537 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
15538 with caret == start at the start of the template-name,
15539 ranging until the closing '>'. */
15540 location_t finish_loc
15541 = get_finish (cp_lexer_previous_token (parser->lexer)->location);
15542 location_t combined_loc
15543 = make_location (token->location, token->location, finish_loc);
15544 token->location = combined_loc;
15546 /* Retrieve any deferred checks. Do not pop this access checks yet
15547 so the memory will not be reclaimed during token replacing below. */
15548 token->u.tree_check_value = ggc_cleared_alloc<struct tree_check> ();
15549 token->u.tree_check_value->value = template_id;
15550 token->u.tree_check_value->checks = get_deferred_access_checks ();
15551 token->keyword = RID_MAX;
15553 /* Purge all subsequent tokens. */
15554 cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
15556 /* ??? Can we actually assume that, if template_id ==
15557 error_mark_node, we will have issued a diagnostic to the
15558 user, as opposed to simply marking the tentative parse as
15559 failed? */
15560 if (cp_parser_error_occurred (parser) && template_id != error_mark_node)
15561 error_at (token->location, "parse error in template argument list");
15564 pop_to_parent_deferring_access_checks ();
15565 return template_id;
15568 /* Parse a template-name.
15570 template-name:
15571 identifier
15573 The standard should actually say:
15575 template-name:
15576 identifier
15577 operator-function-id
15579 A defect report has been filed about this issue.
15581 A conversion-function-id cannot be a template name because they cannot
15582 be part of a template-id. In fact, looking at this code:
15584 a.operator K<int>()
15586 the conversion-function-id is "operator K<int>", and K<int> is a type-id.
15587 It is impossible to call a templated conversion-function-id with an
15588 explicit argument list, since the only allowed template parameter is
15589 the type to which it is converting.
15591 If TEMPLATE_KEYWORD_P is true, then we have just seen the
15592 `template' keyword, in a construction like:
15594 T::template f<3>()
15596 In that case `f' is taken to be a template-name, even though there
15597 is no way of knowing for sure.
15599 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
15600 name refers to a set of overloaded functions, at least one of which
15601 is a template, or an IDENTIFIER_NODE with the name of the template,
15602 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
15603 names are looked up inside uninstantiated templates. */
15605 static tree
15606 cp_parser_template_name (cp_parser* parser,
15607 bool template_keyword_p,
15608 bool check_dependency_p,
15609 bool is_declaration,
15610 enum tag_types tag_type,
15611 bool *is_identifier)
15613 tree identifier;
15614 tree decl;
15615 tree fns;
15616 cp_token *token = cp_lexer_peek_token (parser->lexer);
15618 /* If the next token is `operator', then we have either an
15619 operator-function-id or a conversion-function-id. */
15620 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
15622 /* We don't know whether we're looking at an
15623 operator-function-id or a conversion-function-id. */
15624 cp_parser_parse_tentatively (parser);
15625 /* Try an operator-function-id. */
15626 identifier = cp_parser_operator_function_id (parser);
15627 /* If that didn't work, try a conversion-function-id. */
15628 if (!cp_parser_parse_definitely (parser))
15630 cp_parser_error (parser, "expected template-name");
15631 return error_mark_node;
15634 /* Look for the identifier. */
15635 else
15636 identifier = cp_parser_identifier (parser);
15638 /* If we didn't find an identifier, we don't have a template-id. */
15639 if (identifier == error_mark_node)
15640 return error_mark_node;
15642 /* If the name immediately followed the `template' keyword, then it
15643 is a template-name. However, if the next token is not `<', then
15644 we do not treat it as a template-name, since it is not being used
15645 as part of a template-id. This enables us to handle constructs
15646 like:
15648 template <typename T> struct S { S(); };
15649 template <typename T> S<T>::S();
15651 correctly. We would treat `S' as a template -- if it were `S<T>'
15652 -- but we do not if there is no `<'. */
15654 if (processing_template_decl
15655 && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
15657 /* In a declaration, in a dependent context, we pretend that the
15658 "template" keyword was present in order to improve error
15659 recovery. For example, given:
15661 template <typename T> void f(T::X<int>);
15663 we want to treat "X<int>" as a template-id. */
15664 if (is_declaration
15665 && !template_keyword_p
15666 && parser->scope && TYPE_P (parser->scope)
15667 && check_dependency_p
15668 && dependent_scope_p (parser->scope)
15669 /* Do not do this for dtors (or ctors), since they never
15670 need the template keyword before their name. */
15671 && !constructor_name_p (identifier, parser->scope))
15673 cp_token_position start = 0;
15675 /* Explain what went wrong. */
15676 error_at (token->location, "non-template %qD used as template",
15677 identifier);
15678 inform (token->location, "use %<%T::template %D%> to indicate that it is a template",
15679 parser->scope, identifier);
15680 /* If parsing tentatively, find the location of the "<" token. */
15681 if (cp_parser_simulate_error (parser))
15682 start = cp_lexer_token_position (parser->lexer, true);
15683 /* Parse the template arguments so that we can issue error
15684 messages about them. */
15685 cp_lexer_consume_token (parser->lexer);
15686 cp_parser_enclosed_template_argument_list (parser);
15687 /* Skip tokens until we find a good place from which to
15688 continue parsing. */
15689 cp_parser_skip_to_closing_parenthesis (parser,
15690 /*recovering=*/true,
15691 /*or_comma=*/true,
15692 /*consume_paren=*/false);
15693 /* If parsing tentatively, permanently remove the
15694 template argument list. That will prevent duplicate
15695 error messages from being issued about the missing
15696 "template" keyword. */
15697 if (start)
15698 cp_lexer_purge_tokens_after (parser->lexer, start);
15699 if (is_identifier)
15700 *is_identifier = true;
15701 parser->context->object_type = NULL_TREE;
15702 return identifier;
15705 /* If the "template" keyword is present, then there is generally
15706 no point in doing name-lookup, so we just return IDENTIFIER.
15707 But, if the qualifying scope is non-dependent then we can
15708 (and must) do name-lookup normally. */
15709 if (template_keyword_p
15710 && (!parser->scope
15711 || (TYPE_P (parser->scope)
15712 && dependent_type_p (parser->scope))))
15714 /* We're optimizing away the call to cp_parser_lookup_name, but we
15715 still need to do this. */
15716 parser->context->object_type = NULL_TREE;
15717 return identifier;
15721 /* Look up the name. */
15722 decl = cp_parser_lookup_name (parser, identifier,
15723 tag_type,
15724 /*is_template=*/true,
15725 /*is_namespace=*/false,
15726 check_dependency_p,
15727 /*ambiguous_decls=*/NULL,
15728 token->location);
15730 decl = strip_using_decl (decl);
15732 /* If DECL is a template, then the name was a template-name. */
15733 if (TREE_CODE (decl) == TEMPLATE_DECL)
15735 if (TREE_DEPRECATED (decl)
15736 && deprecated_state != DEPRECATED_SUPPRESS)
15737 warn_deprecated_use (decl, NULL_TREE);
15739 else
15741 tree fn = NULL_TREE;
15743 /* The standard does not explicitly indicate whether a name that
15744 names a set of overloaded declarations, some of which are
15745 templates, is a template-name. However, such a name should
15746 be a template-name; otherwise, there is no way to form a
15747 template-id for the overloaded templates. */
15748 fns = BASELINK_P (decl) ? BASELINK_FUNCTIONS (decl) : decl;
15749 if (TREE_CODE (fns) == OVERLOAD)
15750 for (fn = fns; fn; fn = OVL_NEXT (fn))
15751 if (TREE_CODE (OVL_CURRENT (fn)) == TEMPLATE_DECL)
15752 break;
15754 if (!fn)
15756 /* The name does not name a template. */
15757 cp_parser_error (parser, "expected template-name");
15758 return error_mark_node;
15762 /* If DECL is dependent, and refers to a function, then just return
15763 its name; we will look it up again during template instantiation. */
15764 if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
15766 tree scope = ovl_scope (decl);
15767 if (TYPE_P (scope) && dependent_type_p (scope))
15768 return identifier;
15771 return decl;
15774 /* Parse a template-argument-list.
15776 template-argument-list:
15777 template-argument ... [opt]
15778 template-argument-list , template-argument ... [opt]
15780 Returns a TREE_VEC containing the arguments. */
15782 static tree
15783 cp_parser_template_argument_list (cp_parser* parser)
15785 tree fixed_args[10];
15786 unsigned n_args = 0;
15787 unsigned alloced = 10;
15788 tree *arg_ary = fixed_args;
15789 tree vec;
15790 bool saved_in_template_argument_list_p;
15791 bool saved_ice_p;
15792 bool saved_non_ice_p;
15794 saved_in_template_argument_list_p = parser->in_template_argument_list_p;
15795 parser->in_template_argument_list_p = true;
15796 /* Even if the template-id appears in an integral
15797 constant-expression, the contents of the argument list do
15798 not. */
15799 saved_ice_p = parser->integral_constant_expression_p;
15800 parser->integral_constant_expression_p = false;
15801 saved_non_ice_p = parser->non_integral_constant_expression_p;
15802 parser->non_integral_constant_expression_p = false;
15804 /* Parse the arguments. */
15807 tree argument;
15809 if (n_args)
15810 /* Consume the comma. */
15811 cp_lexer_consume_token (parser->lexer);
15813 /* Parse the template-argument. */
15814 argument = cp_parser_template_argument (parser);
15816 /* If the next token is an ellipsis, we're expanding a template
15817 argument pack. */
15818 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
15820 if (argument == error_mark_node)
15822 cp_token *token = cp_lexer_peek_token (parser->lexer);
15823 error_at (token->location,
15824 "expected parameter pack before %<...%>");
15826 /* Consume the `...' token. */
15827 cp_lexer_consume_token (parser->lexer);
15829 /* Make the argument into a TYPE_PACK_EXPANSION or
15830 EXPR_PACK_EXPANSION. */
15831 argument = make_pack_expansion (argument);
15834 if (n_args == alloced)
15836 alloced *= 2;
15838 if (arg_ary == fixed_args)
15840 arg_ary = XNEWVEC (tree, alloced);
15841 memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
15843 else
15844 arg_ary = XRESIZEVEC (tree, arg_ary, alloced);
15846 arg_ary[n_args++] = argument;
15848 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
15850 vec = make_tree_vec (n_args);
15852 while (n_args--)
15853 TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
15855 if (arg_ary != fixed_args)
15856 free (arg_ary);
15857 parser->non_integral_constant_expression_p = saved_non_ice_p;
15858 parser->integral_constant_expression_p = saved_ice_p;
15859 parser->in_template_argument_list_p = saved_in_template_argument_list_p;
15860 if (CHECKING_P)
15861 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec, TREE_VEC_LENGTH (vec));
15862 return vec;
15865 /* Parse a template-argument.
15867 template-argument:
15868 assignment-expression
15869 type-id
15870 id-expression
15872 The representation is that of an assignment-expression, type-id, or
15873 id-expression -- except that the qualified id-expression is
15874 evaluated, so that the value returned is either a DECL or an
15875 OVERLOAD.
15877 Although the standard says "assignment-expression", it forbids
15878 throw-expressions or assignments in the template argument.
15879 Therefore, we use "conditional-expression" instead. */
15881 static tree
15882 cp_parser_template_argument (cp_parser* parser)
15884 tree argument;
15885 bool template_p;
15886 bool address_p;
15887 bool maybe_type_id = false;
15888 cp_token *token = NULL, *argument_start_token = NULL;
15889 location_t loc = 0;
15890 cp_id_kind idk;
15892 /* There's really no way to know what we're looking at, so we just
15893 try each alternative in order.
15895 [temp.arg]
15897 In a template-argument, an ambiguity between a type-id and an
15898 expression is resolved to a type-id, regardless of the form of
15899 the corresponding template-parameter.
15901 Therefore, we try a type-id first. */
15902 cp_parser_parse_tentatively (parser);
15903 argument = cp_parser_template_type_arg (parser);
15904 /* If there was no error parsing the type-id but the next token is a
15905 '>>', our behavior depends on which dialect of C++ we're
15906 parsing. In C++98, we probably found a typo for '> >'. But there
15907 are type-id which are also valid expressions. For instance:
15909 struct X { int operator >> (int); };
15910 template <int V> struct Foo {};
15911 Foo<X () >> 5> r;
15913 Here 'X()' is a valid type-id of a function type, but the user just
15914 wanted to write the expression "X() >> 5". Thus, we remember that we
15915 found a valid type-id, but we still try to parse the argument as an
15916 expression to see what happens.
15918 In C++0x, the '>>' will be considered two separate '>'
15919 tokens. */
15920 if (!cp_parser_error_occurred (parser)
15921 && cxx_dialect == cxx98
15922 && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
15924 maybe_type_id = true;
15925 cp_parser_abort_tentative_parse (parser);
15927 else
15929 /* If the next token isn't a `,' or a `>', then this argument wasn't
15930 really finished. This means that the argument is not a valid
15931 type-id. */
15932 if (!cp_parser_next_token_ends_template_argument_p (parser))
15933 cp_parser_error (parser, "expected template-argument");
15934 /* If that worked, we're done. */
15935 if (cp_parser_parse_definitely (parser))
15936 return argument;
15938 /* We're still not sure what the argument will be. */
15939 cp_parser_parse_tentatively (parser);
15940 /* Try a template. */
15941 argument_start_token = cp_lexer_peek_token (parser->lexer);
15942 argument = cp_parser_id_expression (parser,
15943 /*template_keyword_p=*/false,
15944 /*check_dependency_p=*/true,
15945 &template_p,
15946 /*declarator_p=*/false,
15947 /*optional_p=*/false);
15948 /* If the next token isn't a `,' or a `>', then this argument wasn't
15949 really finished. */
15950 if (!cp_parser_next_token_ends_template_argument_p (parser))
15951 cp_parser_error (parser, "expected template-argument");
15952 if (!cp_parser_error_occurred (parser))
15954 /* Figure out what is being referred to. If the id-expression
15955 was for a class template specialization, then we will have a
15956 TYPE_DECL at this point. There is no need to do name lookup
15957 at this point in that case. */
15958 if (TREE_CODE (argument) != TYPE_DECL)
15959 argument = cp_parser_lookup_name (parser, argument,
15960 none_type,
15961 /*is_template=*/template_p,
15962 /*is_namespace=*/false,
15963 /*check_dependency=*/true,
15964 /*ambiguous_decls=*/NULL,
15965 argument_start_token->location);
15966 /* Handle a constrained-type-specifier for a non-type template
15967 parameter. */
15968 if (tree decl = cp_parser_maybe_concept_name (parser, argument))
15969 argument = decl;
15970 else if (TREE_CODE (argument) != TEMPLATE_DECL
15971 && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
15972 cp_parser_error (parser, "expected template-name");
15974 if (cp_parser_parse_definitely (parser))
15976 if (TREE_DEPRECATED (argument))
15977 warn_deprecated_use (argument, NULL_TREE);
15978 return argument;
15980 /* It must be a non-type argument. In C++17 any constant-expression is
15981 allowed. */
15982 if (cxx_dialect > cxx14)
15983 goto general_expr;
15985 /* Otherwise, the permitted cases are given in [temp.arg.nontype]:
15987 -- an integral constant-expression of integral or enumeration
15988 type; or
15990 -- the name of a non-type template-parameter; or
15992 -- the name of an object or function with external linkage...
15994 -- the address of an object or function with external linkage...
15996 -- a pointer to member... */
15997 /* Look for a non-type template parameter. */
15998 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
16000 cp_parser_parse_tentatively (parser);
16001 argument = cp_parser_primary_expression (parser,
16002 /*address_p=*/false,
16003 /*cast_p=*/false,
16004 /*template_arg_p=*/true,
16005 &idk);
16006 if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
16007 || !cp_parser_next_token_ends_template_argument_p (parser))
16008 cp_parser_simulate_error (parser);
16009 if (cp_parser_parse_definitely (parser))
16010 return argument;
16013 /* If the next token is "&", the argument must be the address of an
16014 object or function with external linkage. */
16015 address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
16016 if (address_p)
16018 loc = cp_lexer_peek_token (parser->lexer)->location;
16019 cp_lexer_consume_token (parser->lexer);
16021 /* See if we might have an id-expression. */
16022 token = cp_lexer_peek_token (parser->lexer);
16023 if (token->type == CPP_NAME
16024 || token->keyword == RID_OPERATOR
16025 || token->type == CPP_SCOPE
16026 || token->type == CPP_TEMPLATE_ID
16027 || token->type == CPP_NESTED_NAME_SPECIFIER)
16029 cp_parser_parse_tentatively (parser);
16030 argument = cp_parser_primary_expression (parser,
16031 address_p,
16032 /*cast_p=*/false,
16033 /*template_arg_p=*/true,
16034 &idk);
16035 if (cp_parser_error_occurred (parser)
16036 || !cp_parser_next_token_ends_template_argument_p (parser))
16037 cp_parser_abort_tentative_parse (parser);
16038 else
16040 tree probe;
16042 if (INDIRECT_REF_P (argument))
16044 /* Strip the dereference temporarily. */
16045 gcc_assert (REFERENCE_REF_P (argument));
16046 argument = TREE_OPERAND (argument, 0);
16049 /* If we're in a template, we represent a qualified-id referring
16050 to a static data member as a SCOPE_REF even if the scope isn't
16051 dependent so that we can check access control later. */
16052 probe = argument;
16053 if (TREE_CODE (probe) == SCOPE_REF)
16054 probe = TREE_OPERAND (probe, 1);
16055 if (VAR_P (probe))
16057 /* A variable without external linkage might still be a
16058 valid constant-expression, so no error is issued here
16059 if the external-linkage check fails. */
16060 if (!address_p && !DECL_EXTERNAL_LINKAGE_P (probe))
16061 cp_parser_simulate_error (parser);
16063 else if (is_overloaded_fn (argument))
16064 /* All overloaded functions are allowed; if the external
16065 linkage test does not pass, an error will be issued
16066 later. */
16068 else if (address_p
16069 && (TREE_CODE (argument) == OFFSET_REF
16070 || TREE_CODE (argument) == SCOPE_REF))
16071 /* A pointer-to-member. */
16073 else if (TREE_CODE (argument) == TEMPLATE_PARM_INDEX)
16075 else
16076 cp_parser_simulate_error (parser);
16078 if (cp_parser_parse_definitely (parser))
16080 if (address_p)
16081 argument = build_x_unary_op (loc, ADDR_EXPR, argument,
16082 tf_warning_or_error);
16083 else
16084 argument = convert_from_reference (argument);
16085 return argument;
16089 /* If the argument started with "&", there are no other valid
16090 alternatives at this point. */
16091 if (address_p)
16093 cp_parser_error (parser, "invalid non-type template argument");
16094 return error_mark_node;
16097 general_expr:
16098 /* If the argument wasn't successfully parsed as a type-id followed
16099 by '>>', the argument can only be a constant expression now.
16100 Otherwise, we try parsing the constant-expression tentatively,
16101 because the argument could really be a type-id. */
16102 if (maybe_type_id)
16103 cp_parser_parse_tentatively (parser);
16105 if (cxx_dialect <= cxx14)
16106 argument = cp_parser_constant_expression (parser);
16107 else
16109 /* With C++17 generalized non-type template arguments we need to handle
16110 lvalue constant expressions, too. */
16111 argument = cp_parser_assignment_expression (parser);
16112 require_potential_constant_expression (argument);
16115 if (!maybe_type_id)
16116 return argument;
16117 if (!cp_parser_next_token_ends_template_argument_p (parser))
16118 cp_parser_error (parser, "expected template-argument");
16119 if (cp_parser_parse_definitely (parser))
16120 return argument;
16121 /* We did our best to parse the argument as a non type-id, but that
16122 was the only alternative that matched (albeit with a '>' after
16123 it). We can assume it's just a typo from the user, and a
16124 diagnostic will then be issued. */
16125 return cp_parser_template_type_arg (parser);
16128 /* Parse an explicit-instantiation.
16130 explicit-instantiation:
16131 template declaration
16133 Although the standard says `declaration', what it really means is:
16135 explicit-instantiation:
16136 template decl-specifier-seq [opt] declarator [opt] ;
16138 Things like `template int S<int>::i = 5, int S<double>::j;' are not
16139 supposed to be allowed. A defect report has been filed about this
16140 issue.
16142 GNU Extension:
16144 explicit-instantiation:
16145 storage-class-specifier template
16146 decl-specifier-seq [opt] declarator [opt] ;
16147 function-specifier template
16148 decl-specifier-seq [opt] declarator [opt] ; */
16150 static void
16151 cp_parser_explicit_instantiation (cp_parser* parser)
16153 int declares_class_or_enum;
16154 cp_decl_specifier_seq decl_specifiers;
16155 tree extension_specifier = NULL_TREE;
16157 timevar_push (TV_TEMPLATE_INST);
16159 /* Look for an (optional) storage-class-specifier or
16160 function-specifier. */
16161 if (cp_parser_allow_gnu_extensions_p (parser))
16163 extension_specifier
16164 = cp_parser_storage_class_specifier_opt (parser);
16165 if (!extension_specifier)
16166 extension_specifier
16167 = cp_parser_function_specifier_opt (parser,
16168 /*decl_specs=*/NULL);
16171 /* Look for the `template' keyword. */
16172 cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
16173 /* Let the front end know that we are processing an explicit
16174 instantiation. */
16175 begin_explicit_instantiation ();
16176 /* [temp.explicit] says that we are supposed to ignore access
16177 control while processing explicit instantiation directives. */
16178 push_deferring_access_checks (dk_no_check);
16179 /* Parse a decl-specifier-seq. */
16180 cp_parser_decl_specifier_seq (parser,
16181 CP_PARSER_FLAGS_OPTIONAL,
16182 &decl_specifiers,
16183 &declares_class_or_enum);
16184 /* If there was exactly one decl-specifier, and it declared a class,
16185 and there's no declarator, then we have an explicit type
16186 instantiation. */
16187 if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
16189 tree type;
16191 type = check_tag_decl (&decl_specifiers,
16192 /*explicit_type_instantiation_p=*/true);
16193 /* Turn access control back on for names used during
16194 template instantiation. */
16195 pop_deferring_access_checks ();
16196 if (type)
16197 do_type_instantiation (type, extension_specifier,
16198 /*complain=*/tf_error);
16200 else
16202 cp_declarator *declarator;
16203 tree decl;
16205 /* Parse the declarator. */
16206 declarator
16207 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
16208 /*ctor_dtor_or_conv_p=*/NULL,
16209 /*parenthesized_p=*/NULL,
16210 /*member_p=*/false,
16211 /*friend_p=*/false);
16212 if (declares_class_or_enum & 2)
16213 cp_parser_check_for_definition_in_return_type (declarator,
16214 decl_specifiers.type,
16215 decl_specifiers.locations[ds_type_spec]);
16216 if (declarator != cp_error_declarator)
16218 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_inline))
16219 permerror (decl_specifiers.locations[ds_inline],
16220 "explicit instantiation shall not use"
16221 " %<inline%> specifier");
16222 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_constexpr))
16223 permerror (decl_specifiers.locations[ds_constexpr],
16224 "explicit instantiation shall not use"
16225 " %<constexpr%> specifier");
16227 decl = grokdeclarator (declarator, &decl_specifiers,
16228 NORMAL, 0, &decl_specifiers.attributes);
16229 /* Turn access control back on for names used during
16230 template instantiation. */
16231 pop_deferring_access_checks ();
16232 /* Do the explicit instantiation. */
16233 do_decl_instantiation (decl, extension_specifier);
16235 else
16237 pop_deferring_access_checks ();
16238 /* Skip the body of the explicit instantiation. */
16239 cp_parser_skip_to_end_of_statement (parser);
16242 /* We're done with the instantiation. */
16243 end_explicit_instantiation ();
16245 cp_parser_consume_semicolon_at_end_of_statement (parser);
16247 timevar_pop (TV_TEMPLATE_INST);
16250 /* Parse an explicit-specialization.
16252 explicit-specialization:
16253 template < > declaration
16255 Although the standard says `declaration', what it really means is:
16257 explicit-specialization:
16258 template <> decl-specifier [opt] init-declarator [opt] ;
16259 template <> function-definition
16260 template <> explicit-specialization
16261 template <> template-declaration */
16263 static void
16264 cp_parser_explicit_specialization (cp_parser* parser)
16266 bool need_lang_pop;
16267 cp_token *token = cp_lexer_peek_token (parser->lexer);
16269 /* Look for the `template' keyword. */
16270 cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
16271 /* Look for the `<'. */
16272 cp_parser_require (parser, CPP_LESS, RT_LESS);
16273 /* Look for the `>'. */
16274 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
16275 /* We have processed another parameter list. */
16276 ++parser->num_template_parameter_lists;
16277 /* [temp]
16279 A template ... explicit specialization ... shall not have C
16280 linkage. */
16281 if (current_lang_name == lang_name_c)
16283 error_at (token->location, "template specialization with C linkage");
16284 /* Give it C++ linkage to avoid confusing other parts of the
16285 front end. */
16286 push_lang_context (lang_name_cplusplus);
16287 need_lang_pop = true;
16289 else
16290 need_lang_pop = false;
16291 /* Let the front end know that we are beginning a specialization. */
16292 if (!begin_specialization ())
16294 end_specialization ();
16295 return;
16298 /* If the next keyword is `template', we need to figure out whether
16299 or not we're looking a template-declaration. */
16300 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
16302 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
16303 && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
16304 cp_parser_template_declaration_after_export (parser,
16305 /*member_p=*/false);
16306 else
16307 cp_parser_explicit_specialization (parser);
16309 else
16310 /* Parse the dependent declaration. */
16311 cp_parser_single_declaration (parser,
16312 /*checks=*/NULL,
16313 /*member_p=*/false,
16314 /*explicit_specialization_p=*/true,
16315 /*friend_p=*/NULL);
16316 /* We're done with the specialization. */
16317 end_specialization ();
16318 /* For the erroneous case of a template with C linkage, we pushed an
16319 implicit C++ linkage scope; exit that scope now. */
16320 if (need_lang_pop)
16321 pop_lang_context ();
16322 /* We're done with this parameter list. */
16323 --parser->num_template_parameter_lists;
16326 /* Parse a type-specifier.
16328 type-specifier:
16329 simple-type-specifier
16330 class-specifier
16331 enum-specifier
16332 elaborated-type-specifier
16333 cv-qualifier
16335 GNU Extension:
16337 type-specifier:
16338 __complex__
16340 Returns a representation of the type-specifier. For a
16341 class-specifier, enum-specifier, or elaborated-type-specifier, a
16342 TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
16344 The parser flags FLAGS is used to control type-specifier parsing.
16346 If IS_DECLARATION is TRUE, then this type-specifier is appearing
16347 in a decl-specifier-seq.
16349 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
16350 class-specifier, enum-specifier, or elaborated-type-specifier, then
16351 *DECLARES_CLASS_OR_ENUM is set to a nonzero value. The value is 1
16352 if a type is declared; 2 if it is defined. Otherwise, it is set to
16353 zero.
16355 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
16356 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
16357 is set to FALSE. */
16359 static tree
16360 cp_parser_type_specifier (cp_parser* parser,
16361 cp_parser_flags flags,
16362 cp_decl_specifier_seq *decl_specs,
16363 bool is_declaration,
16364 int* declares_class_or_enum,
16365 bool* is_cv_qualifier)
16367 tree type_spec = NULL_TREE;
16368 cp_token *token;
16369 enum rid keyword;
16370 cp_decl_spec ds = ds_last;
16372 /* Assume this type-specifier does not declare a new type. */
16373 if (declares_class_or_enum)
16374 *declares_class_or_enum = 0;
16375 /* And that it does not specify a cv-qualifier. */
16376 if (is_cv_qualifier)
16377 *is_cv_qualifier = false;
16378 /* Peek at the next token. */
16379 token = cp_lexer_peek_token (parser->lexer);
16381 /* If we're looking at a keyword, we can use that to guide the
16382 production we choose. */
16383 keyword = token->keyword;
16384 switch (keyword)
16386 case RID_ENUM:
16387 if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
16388 goto elaborated_type_specifier;
16390 /* Look for the enum-specifier. */
16391 type_spec = cp_parser_enum_specifier (parser);
16392 /* If that worked, we're done. */
16393 if (type_spec)
16395 if (declares_class_or_enum)
16396 *declares_class_or_enum = 2;
16397 if (decl_specs)
16398 cp_parser_set_decl_spec_type (decl_specs,
16399 type_spec,
16400 token,
16401 /*type_definition_p=*/true);
16402 return type_spec;
16404 else
16405 goto elaborated_type_specifier;
16407 /* Any of these indicate either a class-specifier, or an
16408 elaborated-type-specifier. */
16409 case RID_CLASS:
16410 case RID_STRUCT:
16411 case RID_UNION:
16412 if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
16413 goto elaborated_type_specifier;
16415 /* Parse tentatively so that we can back up if we don't find a
16416 class-specifier. */
16417 cp_parser_parse_tentatively (parser);
16418 /* Look for the class-specifier. */
16419 type_spec = cp_parser_class_specifier (parser);
16420 invoke_plugin_callbacks (PLUGIN_FINISH_TYPE, type_spec);
16421 /* If that worked, we're done. */
16422 if (cp_parser_parse_definitely (parser))
16424 if (declares_class_or_enum)
16425 *declares_class_or_enum = 2;
16426 if (decl_specs)
16427 cp_parser_set_decl_spec_type (decl_specs,
16428 type_spec,
16429 token,
16430 /*type_definition_p=*/true);
16431 return type_spec;
16434 /* Fall through. */
16435 elaborated_type_specifier:
16436 /* We're declaring (not defining) a class or enum. */
16437 if (declares_class_or_enum)
16438 *declares_class_or_enum = 1;
16440 /* Fall through. */
16441 case RID_TYPENAME:
16442 /* Look for an elaborated-type-specifier. */
16443 type_spec
16444 = (cp_parser_elaborated_type_specifier
16445 (parser,
16446 decl_spec_seq_has_spec_p (decl_specs, ds_friend),
16447 is_declaration));
16448 if (decl_specs)
16449 cp_parser_set_decl_spec_type (decl_specs,
16450 type_spec,
16451 token,
16452 /*type_definition_p=*/false);
16453 return type_spec;
16455 case RID_CONST:
16456 ds = ds_const;
16457 if (is_cv_qualifier)
16458 *is_cv_qualifier = true;
16459 break;
16461 case RID_VOLATILE:
16462 ds = ds_volatile;
16463 if (is_cv_qualifier)
16464 *is_cv_qualifier = true;
16465 break;
16467 case RID_RESTRICT:
16468 ds = ds_restrict;
16469 if (is_cv_qualifier)
16470 *is_cv_qualifier = true;
16471 break;
16473 case RID_COMPLEX:
16474 /* The `__complex__' keyword is a GNU extension. */
16475 ds = ds_complex;
16476 break;
16478 default:
16479 break;
16482 /* Handle simple keywords. */
16483 if (ds != ds_last)
16485 if (decl_specs)
16487 set_and_check_decl_spec_loc (decl_specs, ds, token);
16488 decl_specs->any_specifiers_p = true;
16490 return cp_lexer_consume_token (parser->lexer)->u.value;
16493 /* If we do not already have a type-specifier, assume we are looking
16494 at a simple-type-specifier. */
16495 type_spec = cp_parser_simple_type_specifier (parser,
16496 decl_specs,
16497 flags);
16499 /* If we didn't find a type-specifier, and a type-specifier was not
16500 optional in this context, issue an error message. */
16501 if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
16503 cp_parser_error (parser, "expected type specifier");
16504 return error_mark_node;
16507 return type_spec;
16510 /* Parse a simple-type-specifier.
16512 simple-type-specifier:
16513 :: [opt] nested-name-specifier [opt] type-name
16514 :: [opt] nested-name-specifier template template-id
16515 char
16516 wchar_t
16517 bool
16518 short
16520 long
16521 signed
16522 unsigned
16523 float
16524 double
16525 void
16527 C++11 Extension:
16529 simple-type-specifier:
16530 auto
16531 decltype ( expression )
16532 char16_t
16533 char32_t
16534 __underlying_type ( type-id )
16536 C++17 extension:
16538 nested-name-specifier(opt) template-name
16540 GNU Extension:
16542 simple-type-specifier:
16543 __int128
16544 __typeof__ unary-expression
16545 __typeof__ ( type-id )
16546 __typeof__ ( type-id ) { initializer-list , [opt] }
16548 Concepts Extension:
16550 simple-type-specifier:
16551 constrained-type-specifier
16553 Returns the indicated TYPE_DECL. If DECL_SPECS is not NULL, it is
16554 appropriately updated. */
16556 static tree
16557 cp_parser_simple_type_specifier (cp_parser* parser,
16558 cp_decl_specifier_seq *decl_specs,
16559 cp_parser_flags flags)
16561 tree type = NULL_TREE;
16562 cp_token *token;
16563 int idx;
16565 /* Peek at the next token. */
16566 token = cp_lexer_peek_token (parser->lexer);
16568 /* If we're looking at a keyword, things are easy. */
16569 switch (token->keyword)
16571 case RID_CHAR:
16572 if (decl_specs)
16573 decl_specs->explicit_char_p = true;
16574 type = char_type_node;
16575 break;
16576 case RID_CHAR16:
16577 type = char16_type_node;
16578 break;
16579 case RID_CHAR32:
16580 type = char32_type_node;
16581 break;
16582 case RID_WCHAR:
16583 type = wchar_type_node;
16584 break;
16585 case RID_BOOL:
16586 type = boolean_type_node;
16587 break;
16588 case RID_SHORT:
16589 set_and_check_decl_spec_loc (decl_specs, ds_short, token);
16590 type = short_integer_type_node;
16591 break;
16592 case RID_INT:
16593 if (decl_specs)
16594 decl_specs->explicit_int_p = true;
16595 type = integer_type_node;
16596 break;
16597 case RID_INT_N_0:
16598 case RID_INT_N_1:
16599 case RID_INT_N_2:
16600 case RID_INT_N_3:
16601 idx = token->keyword - RID_INT_N_0;
16602 if (! int_n_enabled_p [idx])
16603 break;
16604 if (decl_specs)
16606 decl_specs->explicit_intN_p = true;
16607 decl_specs->int_n_idx = idx;
16609 type = int_n_trees [idx].signed_type;
16610 break;
16611 case RID_LONG:
16612 if (decl_specs)
16613 set_and_check_decl_spec_loc (decl_specs, ds_long, token);
16614 type = long_integer_type_node;
16615 break;
16616 case RID_SIGNED:
16617 set_and_check_decl_spec_loc (decl_specs, ds_signed, token);
16618 type = integer_type_node;
16619 break;
16620 case RID_UNSIGNED:
16621 set_and_check_decl_spec_loc (decl_specs, ds_unsigned, token);
16622 type = unsigned_type_node;
16623 break;
16624 case RID_FLOAT:
16625 type = float_type_node;
16626 break;
16627 case RID_DOUBLE:
16628 type = double_type_node;
16629 break;
16630 case RID_VOID:
16631 type = void_type_node;
16632 break;
16634 case RID_AUTO:
16635 maybe_warn_cpp0x (CPP0X_AUTO);
16636 if (parser->auto_is_implicit_function_template_parm_p)
16638 /* The 'auto' might be the placeholder return type for a function decl
16639 with trailing return type. */
16640 bool have_trailing_return_fn_decl = false;
16642 cp_parser_parse_tentatively (parser);
16643 cp_lexer_consume_token (parser->lexer);
16644 while (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
16645 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA)
16646 && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
16647 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
16649 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
16651 cp_lexer_consume_token (parser->lexer);
16652 cp_parser_skip_to_closing_parenthesis (parser,
16653 /*recovering*/false,
16654 /*or_comma*/false,
16655 /*consume_paren*/true);
16656 continue;
16659 if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
16661 have_trailing_return_fn_decl = true;
16662 break;
16665 cp_lexer_consume_token (parser->lexer);
16667 cp_parser_abort_tentative_parse (parser);
16669 if (have_trailing_return_fn_decl)
16671 type = make_auto ();
16672 break;
16675 if (cxx_dialect >= cxx14)
16677 type = synthesize_implicit_template_parm (parser, NULL_TREE);
16678 type = TREE_TYPE (type);
16680 else
16681 type = error_mark_node;
16683 if (current_class_type && LAMBDA_TYPE_P (current_class_type))
16685 if (cxx_dialect < cxx14)
16686 error_at (token->location,
16687 "use of %<auto%> in lambda parameter declaration "
16688 "only available with "
16689 "-std=c++14 or -std=gnu++14");
16691 else if (cxx_dialect < cxx14)
16692 error_at (token->location,
16693 "use of %<auto%> in parameter declaration "
16694 "only available with "
16695 "-std=c++14 or -std=gnu++14");
16696 else if (!flag_concepts)
16697 pedwarn (token->location, OPT_Wpedantic,
16698 "ISO C++ forbids use of %<auto%> in parameter "
16699 "declaration");
16701 else
16702 type = make_auto ();
16703 break;
16705 case RID_DECLTYPE:
16706 /* Since DR 743, decltype can either be a simple-type-specifier by
16707 itself or begin a nested-name-specifier. Parsing it will replace
16708 it with a CPP_DECLTYPE, so just rewind and let the CPP_DECLTYPE
16709 handling below decide what to do. */
16710 cp_parser_decltype (parser);
16711 cp_lexer_set_token_position (parser->lexer, token);
16712 break;
16714 case RID_TYPEOF:
16715 /* Consume the `typeof' token. */
16716 cp_lexer_consume_token (parser->lexer);
16717 /* Parse the operand to `typeof'. */
16718 type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
16719 /* If it is not already a TYPE, take its type. */
16720 if (!TYPE_P (type))
16721 type = finish_typeof (type);
16723 if (decl_specs)
16724 cp_parser_set_decl_spec_type (decl_specs, type,
16725 token,
16726 /*type_definition_p=*/false);
16728 return type;
16730 case RID_UNDERLYING_TYPE:
16731 type = cp_parser_trait_expr (parser, RID_UNDERLYING_TYPE);
16732 if (decl_specs)
16733 cp_parser_set_decl_spec_type (decl_specs, type,
16734 token,
16735 /*type_definition_p=*/false);
16737 return type;
16739 case RID_BASES:
16740 case RID_DIRECT_BASES:
16741 type = cp_parser_trait_expr (parser, token->keyword);
16742 if (decl_specs)
16743 cp_parser_set_decl_spec_type (decl_specs, type,
16744 token,
16745 /*type_definition_p=*/false);
16746 return type;
16747 default:
16748 break;
16751 /* If token is an already-parsed decltype not followed by ::,
16752 it's a simple-type-specifier. */
16753 if (token->type == CPP_DECLTYPE
16754 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
16756 type = saved_checks_value (token->u.tree_check_value);
16757 if (decl_specs)
16759 cp_parser_set_decl_spec_type (decl_specs, type,
16760 token,
16761 /*type_definition_p=*/false);
16762 /* Remember that we are handling a decltype in order to
16763 implement the resolution of DR 1510 when the argument
16764 isn't instantiation dependent. */
16765 decl_specs->decltype_p = true;
16767 cp_lexer_consume_token (parser->lexer);
16768 return type;
16771 /* If the type-specifier was for a built-in type, we're done. */
16772 if (type)
16774 /* Record the type. */
16775 if (decl_specs
16776 && (token->keyword != RID_SIGNED
16777 && token->keyword != RID_UNSIGNED
16778 && token->keyword != RID_SHORT
16779 && token->keyword != RID_LONG))
16780 cp_parser_set_decl_spec_type (decl_specs,
16781 type,
16782 token,
16783 /*type_definition_p=*/false);
16784 if (decl_specs)
16785 decl_specs->any_specifiers_p = true;
16787 /* Consume the token. */
16788 cp_lexer_consume_token (parser->lexer);
16790 if (type == error_mark_node)
16791 return error_mark_node;
16793 /* There is no valid C++ program where a non-template type is
16794 followed by a "<". That usually indicates that the user thought
16795 that the type was a template. */
16796 cp_parser_check_for_invalid_template_id (parser, type, none_type,
16797 token->location);
16799 return TYPE_NAME (type);
16802 /* The type-specifier must be a user-defined type. */
16803 if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
16805 bool qualified_p;
16806 bool global_p;
16808 /* Don't gobble tokens or issue error messages if this is an
16809 optional type-specifier. */
16810 if ((flags & CP_PARSER_FLAGS_OPTIONAL) || cxx_dialect >= cxx1z)
16811 cp_parser_parse_tentatively (parser);
16813 token = cp_lexer_peek_token (parser->lexer);
16815 /* Look for the optional `::' operator. */
16816 global_p
16817 = (cp_parser_global_scope_opt (parser,
16818 /*current_scope_valid_p=*/false)
16819 != NULL_TREE);
16820 /* Look for the nested-name specifier. */
16821 qualified_p
16822 = (cp_parser_nested_name_specifier_opt (parser,
16823 /*typename_keyword_p=*/false,
16824 /*check_dependency_p=*/true,
16825 /*type_p=*/false,
16826 /*is_declaration=*/false)
16827 != NULL_TREE);
16828 /* If we have seen a nested-name-specifier, and the next token
16829 is `template', then we are using the template-id production. */
16830 if (parser->scope
16831 && cp_parser_optional_template_keyword (parser))
16833 /* Look for the template-id. */
16834 type = cp_parser_template_id (parser,
16835 /*template_keyword_p=*/true,
16836 /*check_dependency_p=*/true,
16837 none_type,
16838 /*is_declaration=*/false);
16839 /* If the template-id did not name a type, we are out of
16840 luck. */
16841 if (TREE_CODE (type) != TYPE_DECL)
16843 cp_parser_error (parser, "expected template-id for type");
16844 type = NULL_TREE;
16847 /* Otherwise, look for a type-name. */
16848 else
16849 type = cp_parser_type_name (parser);
16850 /* Keep track of all name-lookups performed in class scopes. */
16851 if (type
16852 && !global_p
16853 && !qualified_p
16854 && TREE_CODE (type) == TYPE_DECL
16855 && identifier_p (DECL_NAME (type)))
16856 maybe_note_name_used_in_class (DECL_NAME (type), type);
16857 /* If it didn't work out, we don't have a TYPE. */
16858 if (((flags & CP_PARSER_FLAGS_OPTIONAL) || cxx_dialect >= cxx1z)
16859 && !cp_parser_parse_definitely (parser))
16860 type = NULL_TREE;
16861 if (!type && cxx_dialect >= cxx1z)
16863 if (flags & CP_PARSER_FLAGS_OPTIONAL)
16864 cp_parser_parse_tentatively (parser);
16866 cp_parser_global_scope_opt (parser,
16867 /*current_scope_valid_p=*/false);
16868 cp_parser_nested_name_specifier_opt (parser,
16869 /*typename_keyword_p=*/false,
16870 /*check_dependency_p=*/true,
16871 /*type_p=*/false,
16872 /*is_declaration=*/false);
16873 tree name = cp_parser_identifier (parser);
16874 if (name && TREE_CODE (name) == IDENTIFIER_NODE
16875 && parser->scope != error_mark_node)
16877 tree tmpl = cp_parser_lookup_name (parser, name,
16878 none_type,
16879 /*is_template=*/false,
16880 /*is_namespace=*/false,
16881 /*check_dependency=*/true,
16882 /*ambiguous_decls=*/NULL,
16883 token->location);
16884 if (tmpl && tmpl != error_mark_node
16885 && (DECL_CLASS_TEMPLATE_P (tmpl)
16886 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl)))
16887 type = make_template_placeholder (tmpl);
16888 else
16890 type = error_mark_node;
16891 if (!cp_parser_simulate_error (parser))
16892 cp_parser_name_lookup_error (parser, name, tmpl,
16893 NLE_TYPE, token->location);
16896 else
16897 type = error_mark_node;
16899 if ((flags & CP_PARSER_FLAGS_OPTIONAL)
16900 && !cp_parser_parse_definitely (parser))
16901 type = NULL_TREE;
16903 if (type && decl_specs)
16904 cp_parser_set_decl_spec_type (decl_specs, type,
16905 token,
16906 /*type_definition_p=*/false);
16909 /* If we didn't get a type-name, issue an error message. */
16910 if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
16912 cp_parser_error (parser, "expected type-name");
16913 return error_mark_node;
16916 if (type && type != error_mark_node)
16918 /* See if TYPE is an Objective-C type, and if so, parse and
16919 accept any protocol references following it. Do this before
16920 the cp_parser_check_for_invalid_template_id() call, because
16921 Objective-C types can be followed by '<...>' which would
16922 enclose protocol names rather than template arguments, and so
16923 everything is fine. */
16924 if (c_dialect_objc () && !parser->scope
16925 && (objc_is_id (type) || objc_is_class_name (type)))
16927 tree protos = cp_parser_objc_protocol_refs_opt (parser);
16928 tree qual_type = objc_get_protocol_qualified_type (type, protos);
16930 /* Clobber the "unqualified" type previously entered into
16931 DECL_SPECS with the new, improved protocol-qualified version. */
16932 if (decl_specs)
16933 decl_specs->type = qual_type;
16935 return qual_type;
16938 /* There is no valid C++ program where a non-template type is
16939 followed by a "<". That usually indicates that the user
16940 thought that the type was a template. */
16941 cp_parser_check_for_invalid_template_id (parser, TREE_TYPE (type),
16942 none_type,
16943 token->location);
16946 return type;
16949 /* Parse a type-name.
16951 type-name:
16952 class-name
16953 enum-name
16954 typedef-name
16955 simple-template-id [in c++0x]
16957 enum-name:
16958 identifier
16960 typedef-name:
16961 identifier
16963 Concepts:
16965 type-name:
16966 concept-name
16967 partial-concept-id
16969 concept-name:
16970 identifier
16972 Returns a TYPE_DECL for the type. */
16974 static tree
16975 cp_parser_type_name (cp_parser* parser)
16977 return cp_parser_type_name (parser, /*typename_keyword_p=*/false);
16980 /* See above. */
16981 static tree
16982 cp_parser_type_name (cp_parser* parser, bool typename_keyword_p)
16984 tree type_decl;
16986 /* We can't know yet whether it is a class-name or not. */
16987 cp_parser_parse_tentatively (parser);
16988 /* Try a class-name. */
16989 type_decl = cp_parser_class_name (parser,
16990 typename_keyword_p,
16991 /*template_keyword_p=*/false,
16992 none_type,
16993 /*check_dependency_p=*/true,
16994 /*class_head_p=*/false,
16995 /*is_declaration=*/false);
16996 /* If it's not a class-name, keep looking. */
16997 if (!cp_parser_parse_definitely (parser))
16999 if (cxx_dialect < cxx11)
17000 /* It must be a typedef-name or an enum-name. */
17001 return cp_parser_nonclass_name (parser);
17003 cp_parser_parse_tentatively (parser);
17004 /* It is either a simple-template-id representing an
17005 instantiation of an alias template... */
17006 type_decl = cp_parser_template_id (parser,
17007 /*template_keyword_p=*/false,
17008 /*check_dependency_p=*/true,
17009 none_type,
17010 /*is_declaration=*/false);
17011 /* Note that this must be an instantiation of an alias template
17012 because [temp.names]/6 says:
17014 A template-id that names an alias template specialization
17015 is a type-name.
17017 Whereas [temp.names]/7 says:
17019 A simple-template-id that names a class template
17020 specialization is a class-name.
17022 With concepts, this could also be a partial-concept-id that
17023 declares a non-type template parameter. */
17024 if (type_decl != NULL_TREE
17025 && TREE_CODE (type_decl) == TYPE_DECL
17026 && TYPE_DECL_ALIAS_P (type_decl))
17027 gcc_assert (DECL_TEMPLATE_INSTANTIATION (type_decl));
17028 else if (is_constrained_parameter (type_decl))
17029 /* Don't do anything. */ ;
17030 else
17031 cp_parser_simulate_error (parser);
17033 if (!cp_parser_parse_definitely (parser))
17034 /* ... Or a typedef-name or an enum-name. */
17035 return cp_parser_nonclass_name (parser);
17038 return type_decl;
17041 /* Check if DECL and ARGS can form a constrained-type-specifier.
17042 If ARGS is non-null, we try to form a concept check of the
17043 form DECL<?, ARGS> where ? is a wildcard that matches any
17044 kind of template argument. If ARGS is NULL, then we try to
17045 form a concept check of the form DECL<?>. */
17047 static tree
17048 cp_parser_maybe_constrained_type_specifier (cp_parser *parser,
17049 tree decl, tree args)
17051 gcc_assert (args ? TREE_CODE (args) == TREE_VEC : true);
17053 /* If we a constrained-type-specifier cannot be deduced. */
17054 if (parser->prevent_constrained_type_specifiers)
17055 return NULL_TREE;
17057 /* A constrained type specifier can only be found in an
17058 overload set or as a reference to a template declaration.
17060 FIXME: This might be masking a bug. It's possible that
17061 that the deduction below is causing template specializations
17062 to be formed with the wildcard as an argument. */
17063 if (TREE_CODE (decl) != OVERLOAD && TREE_CODE (decl) != TEMPLATE_DECL)
17064 return NULL_TREE;
17066 /* Try to build a call expression that evaluates the
17067 concept. This can fail if the overload set refers
17068 only to non-templates. */
17069 tree placeholder = build_nt (WILDCARD_DECL);
17070 tree check = build_concept_check (decl, placeholder, args);
17071 if (check == error_mark_node)
17072 return NULL_TREE;
17074 /* Deduce the checked constraint and the prototype parameter.
17076 FIXME: In certain cases, failure to deduce should be a
17077 diagnosable error. */
17078 tree conc;
17079 tree proto;
17080 if (!deduce_constrained_parameter (check, conc, proto))
17081 return NULL_TREE;
17083 /* In template parameter scope, this results in a constrained
17084 parameter. Return a descriptor of that parm. */
17085 if (processing_template_parmlist)
17086 return build_constrained_parameter (conc, proto, args);
17088 /* In a parameter-declaration-clause, constrained-type
17089 specifiers result in invented template parameters. */
17090 if (parser->auto_is_implicit_function_template_parm_p)
17092 tree x = build_constrained_parameter (conc, proto, args);
17093 return synthesize_implicit_template_parm (parser, x);
17095 else
17097 /* Otherwise, we're in a context where the constrained
17098 type name is deduced and the constraint applies
17099 after deduction. */
17100 return make_constrained_auto (conc, args);
17103 return NULL_TREE;
17106 /* If DECL refers to a concept, return a TYPE_DECL representing
17107 the result of using the constrained type specifier in the
17108 current context. DECL refers to a concept if
17110 - it is an overload set containing a function concept taking a single
17111 type argument, or
17113 - it is a variable concept taking a single type argument. */
17115 static tree
17116 cp_parser_maybe_concept_name (cp_parser* parser, tree decl)
17118 if (flag_concepts
17119 && (TREE_CODE (decl) == OVERLOAD
17120 || BASELINK_P (decl)
17121 || variable_concept_p (decl)))
17122 return cp_parser_maybe_constrained_type_specifier (parser, decl, NULL_TREE);
17123 else
17124 return NULL_TREE;
17127 /* Check if DECL and ARGS form a partial-concept-id. If so,
17128 assign ID to the resulting constrained placeholder.
17130 Returns true if the partial-concept-id designates a placeholder
17131 and false otherwise. Note that *id is set to NULL_TREE in
17132 this case. */
17134 static tree
17135 cp_parser_maybe_partial_concept_id (cp_parser *parser, tree decl, tree args)
17137 return cp_parser_maybe_constrained_type_specifier (parser, decl, args);
17140 /* Parse a non-class type-name, that is, either an enum-name, a typedef-name,
17141 or a concept-name.
17143 enum-name:
17144 identifier
17146 typedef-name:
17147 identifier
17149 concept-name:
17150 identifier
17152 Returns a TYPE_DECL for the type. */
17154 static tree
17155 cp_parser_nonclass_name (cp_parser* parser)
17157 tree type_decl;
17158 tree identifier;
17160 cp_token *token = cp_lexer_peek_token (parser->lexer);
17161 identifier = cp_parser_identifier (parser);
17162 if (identifier == error_mark_node)
17163 return error_mark_node;
17165 /* Look up the type-name. */
17166 type_decl = cp_parser_lookup_name_simple (parser, identifier, token->location);
17168 type_decl = strip_using_decl (type_decl);
17170 /* If we found an overload set, then it may refer to a concept-name. */
17171 if (tree decl = cp_parser_maybe_concept_name (parser, type_decl))
17172 type_decl = decl;
17174 if (TREE_CODE (type_decl) != TYPE_DECL
17175 && (objc_is_id (identifier) || objc_is_class_name (identifier)))
17177 /* See if this is an Objective-C type. */
17178 tree protos = cp_parser_objc_protocol_refs_opt (parser);
17179 tree type = objc_get_protocol_qualified_type (identifier, protos);
17180 if (type)
17181 type_decl = TYPE_NAME (type);
17184 /* Issue an error if we did not find a type-name. */
17185 if (TREE_CODE (type_decl) != TYPE_DECL
17186 /* In Objective-C, we have the complication that class names are
17187 normally type names and start declarations (eg, the
17188 "NSObject" in "NSObject *object;"), but can be used in an
17189 Objective-C 2.0 dot-syntax (as in "NSObject.version") which
17190 is an expression. So, a classname followed by a dot is not a
17191 valid type-name. */
17192 || (objc_is_class_name (TREE_TYPE (type_decl))
17193 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT))
17195 if (!cp_parser_simulate_error (parser))
17196 cp_parser_name_lookup_error (parser, identifier, type_decl,
17197 NLE_TYPE, token->location);
17198 return error_mark_node;
17200 /* Remember that the name was used in the definition of the
17201 current class so that we can check later to see if the
17202 meaning would have been different after the class was
17203 entirely defined. */
17204 else if (type_decl != error_mark_node
17205 && !parser->scope)
17206 maybe_note_name_used_in_class (identifier, type_decl);
17208 return type_decl;
17211 /* Parse an elaborated-type-specifier. Note that the grammar given
17212 here incorporates the resolution to DR68.
17214 elaborated-type-specifier:
17215 class-key :: [opt] nested-name-specifier [opt] identifier
17216 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
17217 enum-key :: [opt] nested-name-specifier [opt] identifier
17218 typename :: [opt] nested-name-specifier identifier
17219 typename :: [opt] nested-name-specifier template [opt]
17220 template-id
17222 GNU extension:
17224 elaborated-type-specifier:
17225 class-key attributes :: [opt] nested-name-specifier [opt] identifier
17226 class-key attributes :: [opt] nested-name-specifier [opt]
17227 template [opt] template-id
17228 enum attributes :: [opt] nested-name-specifier [opt] identifier
17230 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
17231 declared `friend'. If IS_DECLARATION is TRUE, then this
17232 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
17233 something is being declared.
17235 Returns the TYPE specified. */
17237 static tree
17238 cp_parser_elaborated_type_specifier (cp_parser* parser,
17239 bool is_friend,
17240 bool is_declaration)
17242 enum tag_types tag_type;
17243 tree identifier;
17244 tree type = NULL_TREE;
17245 tree attributes = NULL_TREE;
17246 tree globalscope;
17247 cp_token *token = NULL;
17249 /* See if we're looking at the `enum' keyword. */
17250 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
17252 /* Consume the `enum' token. */
17253 cp_lexer_consume_token (parser->lexer);
17254 /* Remember that it's an enumeration type. */
17255 tag_type = enum_type;
17256 /* Issue a warning if the `struct' or `class' key (for C++0x scoped
17257 enums) is used here. */
17258 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
17259 || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
17261 pedwarn (input_location, 0, "elaborated-type-specifier "
17262 "for a scoped enum must not use the %<%D%> keyword",
17263 cp_lexer_peek_token (parser->lexer)->u.value);
17264 /* Consume the `struct' or `class' and parse it anyway. */
17265 cp_lexer_consume_token (parser->lexer);
17267 /* Parse the attributes. */
17268 attributes = cp_parser_attributes_opt (parser);
17270 /* Or, it might be `typename'. */
17271 else if (cp_lexer_next_token_is_keyword (parser->lexer,
17272 RID_TYPENAME))
17274 /* Consume the `typename' token. */
17275 cp_lexer_consume_token (parser->lexer);
17276 /* Remember that it's a `typename' type. */
17277 tag_type = typename_type;
17279 /* Otherwise it must be a class-key. */
17280 else
17282 tag_type = cp_parser_class_key (parser);
17283 if (tag_type == none_type)
17284 return error_mark_node;
17285 /* Parse the attributes. */
17286 attributes = cp_parser_attributes_opt (parser);
17289 /* Look for the `::' operator. */
17290 globalscope = cp_parser_global_scope_opt (parser,
17291 /*current_scope_valid_p=*/false);
17292 /* Look for the nested-name-specifier. */
17293 tree nested_name_specifier;
17294 if (tag_type == typename_type && !globalscope)
17296 nested_name_specifier
17297 = cp_parser_nested_name_specifier (parser,
17298 /*typename_keyword_p=*/true,
17299 /*check_dependency_p=*/true,
17300 /*type_p=*/true,
17301 is_declaration);
17302 if (!nested_name_specifier)
17303 return error_mark_node;
17305 else
17306 /* Even though `typename' is not present, the proposed resolution
17307 to Core Issue 180 says that in `class A<T>::B', `B' should be
17308 considered a type-name, even if `A<T>' is dependent. */
17309 nested_name_specifier
17310 = cp_parser_nested_name_specifier_opt (parser,
17311 /*typename_keyword_p=*/true,
17312 /*check_dependency_p=*/true,
17313 /*type_p=*/true,
17314 is_declaration);
17315 /* For everything but enumeration types, consider a template-id.
17316 For an enumeration type, consider only a plain identifier. */
17317 if (tag_type != enum_type)
17319 bool template_p = false;
17320 tree decl;
17322 /* Allow the `template' keyword. */
17323 template_p = cp_parser_optional_template_keyword (parser);
17324 /* If we didn't see `template', we don't know if there's a
17325 template-id or not. */
17326 if (!template_p)
17327 cp_parser_parse_tentatively (parser);
17328 /* Parse the template-id. */
17329 token = cp_lexer_peek_token (parser->lexer);
17330 decl = cp_parser_template_id (parser, template_p,
17331 /*check_dependency_p=*/true,
17332 tag_type,
17333 is_declaration);
17334 /* If we didn't find a template-id, look for an ordinary
17335 identifier. */
17336 if (!template_p && !cp_parser_parse_definitely (parser))
17338 /* We can get here when cp_parser_template_id, called by
17339 cp_parser_class_name with tag_type == none_type, succeeds
17340 and caches a BASELINK. Then, when called again here,
17341 instead of failing and returning an error_mark_node
17342 returns it (see template/typename17.C in C++11).
17343 ??? Could we diagnose this earlier? */
17344 else if (tag_type == typename_type && BASELINK_P (decl))
17346 cp_parser_diagnose_invalid_type_name (parser, decl, token->location);
17347 type = error_mark_node;
17349 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
17350 in effect, then we must assume that, upon instantiation, the
17351 template will correspond to a class. */
17352 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
17353 && tag_type == typename_type)
17354 type = make_typename_type (parser->scope, decl,
17355 typename_type,
17356 /*complain=*/tf_error);
17357 /* If the `typename' keyword is in effect and DECL is not a type
17358 decl, then type is non existent. */
17359 else if (tag_type == typename_type && TREE_CODE (decl) != TYPE_DECL)
17361 else if (TREE_CODE (decl) == TYPE_DECL)
17363 type = check_elaborated_type_specifier (tag_type, decl,
17364 /*allow_template_p=*/true);
17366 /* If the next token is a semicolon, this must be a specialization,
17367 instantiation, or friend declaration. Check the scope while we
17368 still know whether or not we had a nested-name-specifier. */
17369 if (type != error_mark_node
17370 && !nested_name_specifier && !is_friend
17371 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
17372 check_unqualified_spec_or_inst (type, token->location);
17374 else if (decl == error_mark_node)
17375 type = error_mark_node;
17378 if (!type)
17380 token = cp_lexer_peek_token (parser->lexer);
17381 identifier = cp_parser_identifier (parser);
17383 if (identifier == error_mark_node)
17385 parser->scope = NULL_TREE;
17386 return error_mark_node;
17389 /* For a `typename', we needn't call xref_tag. */
17390 if (tag_type == typename_type
17391 && TREE_CODE (parser->scope) != NAMESPACE_DECL)
17392 return cp_parser_make_typename_type (parser, identifier,
17393 token->location);
17395 /* Template parameter lists apply only if we are not within a
17396 function parameter list. */
17397 bool template_parm_lists_apply
17398 = parser->num_template_parameter_lists;
17399 if (template_parm_lists_apply)
17400 for (cp_binding_level *s = current_binding_level;
17401 s && s->kind != sk_template_parms;
17402 s = s->level_chain)
17403 if (s->kind == sk_function_parms)
17404 template_parm_lists_apply = false;
17406 /* Look up a qualified name in the usual way. */
17407 if (parser->scope)
17409 tree decl;
17410 tree ambiguous_decls;
17412 decl = cp_parser_lookup_name (parser, identifier,
17413 tag_type,
17414 /*is_template=*/false,
17415 /*is_namespace=*/false,
17416 /*check_dependency=*/true,
17417 &ambiguous_decls,
17418 token->location);
17420 /* If the lookup was ambiguous, an error will already have been
17421 issued. */
17422 if (ambiguous_decls)
17423 return error_mark_node;
17425 /* If we are parsing friend declaration, DECL may be a
17426 TEMPLATE_DECL tree node here. However, we need to check
17427 whether this TEMPLATE_DECL results in valid code. Consider
17428 the following example:
17430 namespace N {
17431 template <class T> class C {};
17433 class X {
17434 template <class T> friend class N::C; // #1, valid code
17436 template <class T> class Y {
17437 friend class N::C; // #2, invalid code
17440 For both case #1 and #2, we arrive at a TEMPLATE_DECL after
17441 name lookup of `N::C'. We see that friend declaration must
17442 be template for the code to be valid. Note that
17443 processing_template_decl does not work here since it is
17444 always 1 for the above two cases. */
17446 decl = (cp_parser_maybe_treat_template_as_class
17447 (decl, /*tag_name_p=*/is_friend
17448 && template_parm_lists_apply));
17450 if (TREE_CODE (decl) != TYPE_DECL)
17452 cp_parser_diagnose_invalid_type_name (parser,
17453 identifier,
17454 token->location);
17455 return error_mark_node;
17458 if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
17460 bool allow_template = (template_parm_lists_apply
17461 || DECL_SELF_REFERENCE_P (decl));
17462 type = check_elaborated_type_specifier (tag_type, decl,
17463 allow_template);
17465 if (type == error_mark_node)
17466 return error_mark_node;
17469 /* Forward declarations of nested types, such as
17471 class C1::C2;
17472 class C1::C2::C3;
17474 are invalid unless all components preceding the final '::'
17475 are complete. If all enclosing types are complete, these
17476 declarations become merely pointless.
17478 Invalid forward declarations of nested types are errors
17479 caught elsewhere in parsing. Those that are pointless arrive
17480 here. */
17482 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
17483 && !is_friend && !processing_explicit_instantiation)
17484 warning (0, "declaration %qD does not declare anything", decl);
17486 type = TREE_TYPE (decl);
17488 else
17490 /* An elaborated-type-specifier sometimes introduces a new type and
17491 sometimes names an existing type. Normally, the rule is that it
17492 introduces a new type only if there is not an existing type of
17493 the same name already in scope. For example, given:
17495 struct S {};
17496 void f() { struct S s; }
17498 the `struct S' in the body of `f' is the same `struct S' as in
17499 the global scope; the existing definition is used. However, if
17500 there were no global declaration, this would introduce a new
17501 local class named `S'.
17503 An exception to this rule applies to the following code:
17505 namespace N { struct S; }
17507 Here, the elaborated-type-specifier names a new type
17508 unconditionally; even if there is already an `S' in the
17509 containing scope this declaration names a new type.
17510 This exception only applies if the elaborated-type-specifier
17511 forms the complete declaration:
17513 [class.name]
17515 A declaration consisting solely of `class-key identifier ;' is
17516 either a redeclaration of the name in the current scope or a
17517 forward declaration of the identifier as a class name. It
17518 introduces the name into the current scope.
17520 We are in this situation precisely when the next token is a `;'.
17522 An exception to the exception is that a `friend' declaration does
17523 *not* name a new type; i.e., given:
17525 struct S { friend struct T; };
17527 `T' is not a new type in the scope of `S'.
17529 Also, `new struct S' or `sizeof (struct S)' never results in the
17530 definition of a new type; a new type can only be declared in a
17531 declaration context. */
17533 tag_scope ts;
17534 bool template_p;
17536 if (is_friend)
17537 /* Friends have special name lookup rules. */
17538 ts = ts_within_enclosing_non_class;
17539 else if (is_declaration
17540 && cp_lexer_next_token_is (parser->lexer,
17541 CPP_SEMICOLON))
17542 /* This is a `class-key identifier ;' */
17543 ts = ts_current;
17544 else
17545 ts = ts_global;
17547 template_p =
17548 (template_parm_lists_apply
17549 && (cp_parser_next_token_starts_class_definition_p (parser)
17550 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)));
17551 /* An unqualified name was used to reference this type, so
17552 there were no qualifying templates. */
17553 if (template_parm_lists_apply
17554 && !cp_parser_check_template_parameters (parser,
17555 /*num_templates=*/0,
17556 token->location,
17557 /*declarator=*/NULL))
17558 return error_mark_node;
17559 type = xref_tag (tag_type, identifier, ts, template_p);
17563 if (type == error_mark_node)
17564 return error_mark_node;
17566 /* Allow attributes on forward declarations of classes. */
17567 if (attributes)
17569 if (TREE_CODE (type) == TYPENAME_TYPE)
17570 warning (OPT_Wattributes,
17571 "attributes ignored on uninstantiated type");
17572 else if (tag_type != enum_type && CLASSTYPE_TEMPLATE_INSTANTIATION (type)
17573 && ! processing_explicit_instantiation)
17574 warning (OPT_Wattributes,
17575 "attributes ignored on template instantiation");
17576 else if (is_declaration && cp_parser_declares_only_class_p (parser))
17577 cplus_decl_attributes (&type, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
17578 else
17579 warning (OPT_Wattributes,
17580 "attributes ignored on elaborated-type-specifier that is not a forward declaration");
17583 if (tag_type != enum_type)
17585 /* Indicate whether this class was declared as a `class' or as a
17586 `struct'. */
17587 if (CLASS_TYPE_P (type))
17588 CLASSTYPE_DECLARED_CLASS (type) = (tag_type == class_type);
17589 cp_parser_check_class_key (tag_type, type);
17592 /* A "<" cannot follow an elaborated type specifier. If that
17593 happens, the user was probably trying to form a template-id. */
17594 cp_parser_check_for_invalid_template_id (parser, type, tag_type,
17595 token->location);
17597 return type;
17600 /* Parse an enum-specifier.
17602 enum-specifier:
17603 enum-head { enumerator-list [opt] }
17604 enum-head { enumerator-list , } [C++0x]
17606 enum-head:
17607 enum-key identifier [opt] enum-base [opt]
17608 enum-key nested-name-specifier identifier enum-base [opt]
17610 enum-key:
17611 enum
17612 enum class [C++0x]
17613 enum struct [C++0x]
17615 enum-base: [C++0x]
17616 : type-specifier-seq
17618 opaque-enum-specifier:
17619 enum-key identifier enum-base [opt] ;
17621 GNU Extensions:
17622 enum-key attributes[opt] identifier [opt] enum-base [opt]
17623 { enumerator-list [opt] }attributes[opt]
17624 enum-key attributes[opt] identifier [opt] enum-base [opt]
17625 { enumerator-list, }attributes[opt] [C++0x]
17627 Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
17628 if the token stream isn't an enum-specifier after all. */
17630 static tree
17631 cp_parser_enum_specifier (cp_parser* parser)
17633 tree identifier;
17634 tree type = NULL_TREE;
17635 tree prev_scope;
17636 tree nested_name_specifier = NULL_TREE;
17637 tree attributes;
17638 bool scoped_enum_p = false;
17639 bool has_underlying_type = false;
17640 bool nested_being_defined = false;
17641 bool new_value_list = false;
17642 bool is_new_type = false;
17643 bool is_unnamed = false;
17644 tree underlying_type = NULL_TREE;
17645 cp_token *type_start_token = NULL;
17646 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
17648 parser->colon_corrects_to_scope_p = false;
17650 /* Parse tentatively so that we can back up if we don't find a
17651 enum-specifier. */
17652 cp_parser_parse_tentatively (parser);
17654 /* Caller guarantees that the current token is 'enum', an identifier
17655 possibly follows, and the token after that is an opening brace.
17656 If we don't have an identifier, fabricate an anonymous name for
17657 the enumeration being defined. */
17658 cp_lexer_consume_token (parser->lexer);
17660 /* Parse the "class" or "struct", which indicates a scoped
17661 enumeration type in C++0x. */
17662 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
17663 || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
17665 if (cxx_dialect < cxx11)
17666 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
17668 /* Consume the `struct' or `class' token. */
17669 cp_lexer_consume_token (parser->lexer);
17671 scoped_enum_p = true;
17674 attributes = cp_parser_attributes_opt (parser);
17676 /* Clear the qualification. */
17677 parser->scope = NULL_TREE;
17678 parser->qualifying_scope = NULL_TREE;
17679 parser->object_scope = NULL_TREE;
17681 /* Figure out in what scope the declaration is being placed. */
17682 prev_scope = current_scope ();
17684 type_start_token = cp_lexer_peek_token (parser->lexer);
17686 push_deferring_access_checks (dk_no_check);
17687 nested_name_specifier
17688 = cp_parser_nested_name_specifier_opt (parser,
17689 /*typename_keyword_p=*/true,
17690 /*check_dependency_p=*/false,
17691 /*type_p=*/false,
17692 /*is_declaration=*/false);
17694 if (nested_name_specifier)
17696 tree name;
17698 identifier = cp_parser_identifier (parser);
17699 name = cp_parser_lookup_name (parser, identifier,
17700 enum_type,
17701 /*is_template=*/false,
17702 /*is_namespace=*/false,
17703 /*check_dependency=*/true,
17704 /*ambiguous_decls=*/NULL,
17705 input_location);
17706 if (name && name != error_mark_node)
17708 type = TREE_TYPE (name);
17709 if (TREE_CODE (type) == TYPENAME_TYPE)
17711 /* Are template enums allowed in ISO? */
17712 if (template_parm_scope_p ())
17713 pedwarn (type_start_token->location, OPT_Wpedantic,
17714 "%qD is an enumeration template", name);
17715 /* ignore a typename reference, for it will be solved by name
17716 in start_enum. */
17717 type = NULL_TREE;
17720 else if (nested_name_specifier == error_mark_node)
17721 /* We already issued an error. */;
17722 else
17724 error_at (type_start_token->location,
17725 "%qD does not name an enumeration in %qT",
17726 identifier, nested_name_specifier);
17727 nested_name_specifier = error_mark_node;
17730 else
17732 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
17733 identifier = cp_parser_identifier (parser);
17734 else
17736 identifier = make_anon_name ();
17737 is_unnamed = true;
17738 if (scoped_enum_p)
17739 error_at (type_start_token->location,
17740 "unnamed scoped enum is not allowed");
17743 pop_deferring_access_checks ();
17745 /* Check for the `:' that denotes a specified underlying type in C++0x.
17746 Note that a ':' could also indicate a bitfield width, however. */
17747 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
17749 cp_decl_specifier_seq type_specifiers;
17751 /* Consume the `:'. */
17752 cp_lexer_consume_token (parser->lexer);
17754 /* Parse the type-specifier-seq. */
17755 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
17756 /*is_trailing_return=*/false,
17757 &type_specifiers);
17759 /* At this point this is surely not elaborated type specifier. */
17760 if (!cp_parser_parse_definitely (parser))
17761 return NULL_TREE;
17763 if (cxx_dialect < cxx11)
17764 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
17766 has_underlying_type = true;
17768 /* If that didn't work, stop. */
17769 if (type_specifiers.type != error_mark_node)
17771 underlying_type = grokdeclarator (NULL, &type_specifiers, TYPENAME,
17772 /*initialized=*/0, NULL);
17773 if (underlying_type == error_mark_node
17774 || check_for_bare_parameter_packs (underlying_type))
17775 underlying_type = NULL_TREE;
17779 /* Look for the `{' but don't consume it yet. */
17780 if (!cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
17782 if (cxx_dialect < cxx11 || (!scoped_enum_p && !underlying_type))
17784 cp_parser_error (parser, "expected %<{%>");
17785 if (has_underlying_type)
17787 type = NULL_TREE;
17788 goto out;
17791 /* An opaque-enum-specifier must have a ';' here. */
17792 if ((scoped_enum_p || underlying_type)
17793 && cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
17795 cp_parser_error (parser, "expected %<;%> or %<{%>");
17796 if (has_underlying_type)
17798 type = NULL_TREE;
17799 goto out;
17804 if (!has_underlying_type && !cp_parser_parse_definitely (parser))
17805 return NULL_TREE;
17807 if (nested_name_specifier)
17809 if (CLASS_TYPE_P (nested_name_specifier))
17811 nested_being_defined = TYPE_BEING_DEFINED (nested_name_specifier);
17812 TYPE_BEING_DEFINED (nested_name_specifier) = 1;
17813 push_scope (nested_name_specifier);
17815 else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
17817 push_nested_namespace (nested_name_specifier);
17821 /* Issue an error message if type-definitions are forbidden here. */
17822 if (!cp_parser_check_type_definition (parser))
17823 type = error_mark_node;
17824 else
17825 /* Create the new type. We do this before consuming the opening
17826 brace so the enum will be recorded as being on the line of its
17827 tag (or the 'enum' keyword, if there is no tag). */
17828 type = start_enum (identifier, type, underlying_type,
17829 attributes, scoped_enum_p, &is_new_type);
17831 /* If the next token is not '{' it is an opaque-enum-specifier or an
17832 elaborated-type-specifier. */
17833 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
17835 timevar_push (TV_PARSE_ENUM);
17836 if (nested_name_specifier
17837 && nested_name_specifier != error_mark_node)
17839 /* The following catches invalid code such as:
17840 enum class S<int>::E { A, B, C }; */
17841 if (!processing_specialization
17842 && CLASS_TYPE_P (nested_name_specifier)
17843 && CLASSTYPE_USE_TEMPLATE (nested_name_specifier))
17844 error_at (type_start_token->location, "cannot add an enumerator "
17845 "list to a template instantiation");
17847 if (TREE_CODE (nested_name_specifier) == TYPENAME_TYPE)
17849 error_at (type_start_token->location,
17850 "%<%T::%E%> has not been declared",
17851 TYPE_CONTEXT (nested_name_specifier),
17852 nested_name_specifier);
17853 type = error_mark_node;
17855 else if (TREE_CODE (nested_name_specifier) != NAMESPACE_DECL
17856 && !CLASS_TYPE_P (nested_name_specifier))
17858 error_at (type_start_token->location, "nested name specifier "
17859 "%qT for enum declaration does not name a class "
17860 "or namespace", nested_name_specifier);
17861 type = error_mark_node;
17863 /* If that scope does not contain the scope in which the
17864 class was originally declared, the program is invalid. */
17865 else if (prev_scope && !is_ancestor (prev_scope,
17866 nested_name_specifier))
17868 if (at_namespace_scope_p ())
17869 error_at (type_start_token->location,
17870 "declaration of %qD in namespace %qD which does not "
17871 "enclose %qD",
17872 type, prev_scope, nested_name_specifier);
17873 else
17874 error_at (type_start_token->location,
17875 "declaration of %qD in %qD which does not "
17876 "enclose %qD",
17877 type, prev_scope, nested_name_specifier);
17878 type = error_mark_node;
17880 /* If that scope is the scope where the declaration is being placed
17881 the program is invalid. */
17882 else if (CLASS_TYPE_P (nested_name_specifier)
17883 && CLASS_TYPE_P (prev_scope)
17884 && same_type_p (nested_name_specifier, prev_scope))
17886 permerror (type_start_token->location,
17887 "extra qualification not allowed");
17888 nested_name_specifier = NULL_TREE;
17892 if (scoped_enum_p)
17893 begin_scope (sk_scoped_enum, type);
17895 /* Consume the opening brace. */
17896 cp_lexer_consume_token (parser->lexer);
17898 if (type == error_mark_node)
17899 ; /* Nothing to add */
17900 else if (OPAQUE_ENUM_P (type)
17901 || (cxx_dialect > cxx98 && processing_specialization))
17903 new_value_list = true;
17904 SET_OPAQUE_ENUM_P (type, false);
17905 DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
17907 else
17909 error_at (type_start_token->location,
17910 "multiple definition of %q#T", type);
17911 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)),
17912 "previous definition here");
17913 type = error_mark_node;
17916 if (type == error_mark_node)
17917 cp_parser_skip_to_end_of_block_or_statement (parser);
17918 /* If the next token is not '}', then there are some enumerators. */
17919 else if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
17921 if (is_unnamed && !scoped_enum_p)
17922 pedwarn (type_start_token->location, OPT_Wpedantic,
17923 "ISO C++ forbids empty unnamed enum");
17925 else
17926 cp_parser_enumerator_list (parser, type);
17928 /* Consume the final '}'. */
17929 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
17931 if (scoped_enum_p)
17932 finish_scope ();
17933 timevar_pop (TV_PARSE_ENUM);
17935 else
17937 /* If a ';' follows, then it is an opaque-enum-specifier
17938 and additional restrictions apply. */
17939 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
17941 if (is_unnamed)
17942 error_at (type_start_token->location,
17943 "opaque-enum-specifier without name");
17944 else if (nested_name_specifier)
17945 error_at (type_start_token->location,
17946 "opaque-enum-specifier must use a simple identifier");
17950 /* Look for trailing attributes to apply to this enumeration, and
17951 apply them if appropriate. */
17952 if (cp_parser_allow_gnu_extensions_p (parser))
17954 tree trailing_attr = cp_parser_gnu_attributes_opt (parser);
17955 cplus_decl_attributes (&type,
17956 trailing_attr,
17957 (int) ATTR_FLAG_TYPE_IN_PLACE);
17960 /* Finish up the enumeration. */
17961 if (type != error_mark_node)
17963 if (new_value_list)
17964 finish_enum_value_list (type);
17965 if (is_new_type)
17966 finish_enum (type);
17969 if (nested_name_specifier)
17971 if (CLASS_TYPE_P (nested_name_specifier))
17973 TYPE_BEING_DEFINED (nested_name_specifier) = nested_being_defined;
17974 pop_scope (nested_name_specifier);
17976 else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
17978 pop_nested_namespace (nested_name_specifier);
17981 out:
17982 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
17983 return type;
17986 /* Parse an enumerator-list. The enumerators all have the indicated
17987 TYPE.
17989 enumerator-list:
17990 enumerator-definition
17991 enumerator-list , enumerator-definition */
17993 static void
17994 cp_parser_enumerator_list (cp_parser* parser, tree type)
17996 while (true)
17998 /* Parse an enumerator-definition. */
17999 cp_parser_enumerator_definition (parser, type);
18001 /* If the next token is not a ',', we've reached the end of
18002 the list. */
18003 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
18004 break;
18005 /* Otherwise, consume the `,' and keep going. */
18006 cp_lexer_consume_token (parser->lexer);
18007 /* If the next token is a `}', there is a trailing comma. */
18008 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
18010 if (cxx_dialect < cxx11 && !in_system_header_at (input_location))
18011 pedwarn (input_location, OPT_Wpedantic,
18012 "comma at end of enumerator list");
18013 break;
18018 /* Parse an enumerator-definition. The enumerator has the indicated
18019 TYPE.
18021 enumerator-definition:
18022 enumerator
18023 enumerator = constant-expression
18025 enumerator:
18026 identifier
18028 GNU Extensions:
18030 enumerator-definition:
18031 enumerator attributes [opt]
18032 enumerator attributes [opt] = constant-expression */
18034 static void
18035 cp_parser_enumerator_definition (cp_parser* parser, tree type)
18037 tree identifier;
18038 tree value;
18039 location_t loc;
18041 /* Save the input location because we are interested in the location
18042 of the identifier and not the location of the explicit value. */
18043 loc = cp_lexer_peek_token (parser->lexer)->location;
18045 /* Look for the identifier. */
18046 identifier = cp_parser_identifier (parser);
18047 if (identifier == error_mark_node)
18048 return;
18050 /* Parse any specified attributes. */
18051 tree attrs = cp_parser_attributes_opt (parser);
18053 /* If the next token is an '=', then there is an explicit value. */
18054 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
18056 /* Consume the `=' token. */
18057 cp_lexer_consume_token (parser->lexer);
18058 /* Parse the value. */
18059 value = cp_parser_constant_expression (parser);
18061 else
18062 value = NULL_TREE;
18064 /* If we are processing a template, make sure the initializer of the
18065 enumerator doesn't contain any bare template parameter pack. */
18066 if (check_for_bare_parameter_packs (value))
18067 value = error_mark_node;
18069 /* Create the enumerator. */
18070 build_enumerator (identifier, value, type, attrs, loc);
18073 /* Parse a namespace-name.
18075 namespace-name:
18076 original-namespace-name
18077 namespace-alias
18079 Returns the NAMESPACE_DECL for the namespace. */
18081 static tree
18082 cp_parser_namespace_name (cp_parser* parser)
18084 tree identifier;
18085 tree namespace_decl;
18087 cp_token *token = cp_lexer_peek_token (parser->lexer);
18089 /* Get the name of the namespace. */
18090 identifier = cp_parser_identifier (parser);
18091 if (identifier == error_mark_node)
18092 return error_mark_node;
18094 /* Look up the identifier in the currently active scope. Look only
18095 for namespaces, due to:
18097 [basic.lookup.udir]
18099 When looking up a namespace-name in a using-directive or alias
18100 definition, only namespace names are considered.
18102 And:
18104 [basic.lookup.qual]
18106 During the lookup of a name preceding the :: scope resolution
18107 operator, object, function, and enumerator names are ignored.
18109 (Note that cp_parser_qualifying_entity only calls this
18110 function if the token after the name is the scope resolution
18111 operator.) */
18112 namespace_decl = cp_parser_lookup_name (parser, identifier,
18113 none_type,
18114 /*is_template=*/false,
18115 /*is_namespace=*/true,
18116 /*check_dependency=*/true,
18117 /*ambiguous_decls=*/NULL,
18118 token->location);
18119 /* If it's not a namespace, issue an error. */
18120 if (namespace_decl == error_mark_node
18121 || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
18123 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
18124 error_at (token->location, "%qD is not a namespace-name", identifier);
18125 cp_parser_error (parser, "expected namespace-name");
18126 namespace_decl = error_mark_node;
18129 return namespace_decl;
18132 /* Parse a namespace-definition.
18134 namespace-definition:
18135 named-namespace-definition
18136 unnamed-namespace-definition
18138 named-namespace-definition:
18139 original-namespace-definition
18140 extension-namespace-definition
18142 original-namespace-definition:
18143 namespace identifier { namespace-body }
18145 extension-namespace-definition:
18146 namespace original-namespace-name { namespace-body }
18148 unnamed-namespace-definition:
18149 namespace { namespace-body } */
18151 static void
18152 cp_parser_namespace_definition (cp_parser* parser)
18154 tree identifier, attribs;
18155 bool has_visibility;
18156 bool is_inline;
18157 cp_token* token;
18158 int nested_definition_count = 0;
18160 cp_ensure_no_omp_declare_simd (parser);
18161 cp_ensure_no_oacc_routine (parser);
18162 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_INLINE))
18164 maybe_warn_cpp0x (CPP0X_INLINE_NAMESPACES);
18165 is_inline = true;
18166 cp_lexer_consume_token (parser->lexer);
18168 else
18169 is_inline = false;
18171 /* Look for the `namespace' keyword. */
18172 token = cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
18174 /* Parse any specified attributes before the identifier. */
18175 attribs = cp_parser_attributes_opt (parser);
18177 /* Get the name of the namespace. We do not attempt to distinguish
18178 between an original-namespace-definition and an
18179 extension-namespace-definition at this point. The semantic
18180 analysis routines are responsible for that. */
18181 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
18182 identifier = cp_parser_identifier (parser);
18183 else
18184 identifier = NULL_TREE;
18186 /* Parse any specified attributes after the identifier. */
18187 tree post_ident_attribs = cp_parser_attributes_opt (parser);
18188 if (post_ident_attribs)
18190 if (attribs)
18191 attribs = chainon (attribs, post_ident_attribs);
18192 else
18193 attribs = post_ident_attribs;
18196 /* Start the namespace. */
18197 bool ok = push_namespace (identifier);
18199 /* Parse any nested namespace definition. */
18200 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
18202 if (attribs)
18203 error_at (token->location, "a nested namespace definition cannot have attributes");
18204 if (cxx_dialect < cxx1z)
18205 pedwarn (input_location, OPT_Wpedantic,
18206 "nested namespace definitions only available with "
18207 "-std=c++1z or -std=gnu++1z");
18208 if (is_inline)
18209 error_at (token->location, "a nested namespace definition cannot be inline");
18210 while (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
18212 cp_lexer_consume_token (parser->lexer);
18213 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
18214 identifier = cp_parser_identifier (parser);
18215 else
18217 cp_parser_error (parser, "nested identifier required");
18218 break;
18220 if (push_namespace (identifier))
18221 ++nested_definition_count;
18225 /* Look for the `{' to validate starting the namespace. */
18226 cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
18228 /* "inline namespace" is equivalent to a stub namespace definition
18229 followed by a strong using directive. */
18230 if (is_inline && ok)
18232 tree name_space = current_namespace;
18233 /* Set up namespace association. */
18234 DECL_NAMESPACE_ASSOCIATIONS (name_space)
18235 = tree_cons (CP_DECL_CONTEXT (name_space), NULL_TREE,
18236 DECL_NAMESPACE_ASSOCIATIONS (name_space));
18237 /* Import the contents of the inline namespace. */
18238 pop_namespace ();
18239 do_using_directive (name_space);
18240 push_namespace (identifier);
18243 has_visibility = handle_namespace_attrs (current_namespace, attribs);
18245 warning (OPT_Wnamespaces, "namespace %qD entered", current_namespace);
18247 /* Parse the body of the namespace. */
18248 cp_parser_namespace_body (parser);
18250 if (has_visibility)
18251 pop_visibility (1);
18253 /* Finish the nested namespace definitions. */
18254 while (nested_definition_count--)
18255 pop_namespace ();
18257 /* Finish the namespace. */
18258 if (ok)
18259 pop_namespace ();
18260 /* Look for the final `}'. */
18261 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
18264 /* Parse a namespace-body.
18266 namespace-body:
18267 declaration-seq [opt] */
18269 static void
18270 cp_parser_namespace_body (cp_parser* parser)
18272 cp_parser_declaration_seq_opt (parser);
18275 /* Parse a namespace-alias-definition.
18277 namespace-alias-definition:
18278 namespace identifier = qualified-namespace-specifier ; */
18280 static void
18281 cp_parser_namespace_alias_definition (cp_parser* parser)
18283 tree identifier;
18284 tree namespace_specifier;
18286 cp_token *token = cp_lexer_peek_token (parser->lexer);
18288 /* Look for the `namespace' keyword. */
18289 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
18290 /* Look for the identifier. */
18291 identifier = cp_parser_identifier (parser);
18292 if (identifier == error_mark_node)
18293 return;
18294 /* Look for the `=' token. */
18295 if (!cp_parser_uncommitted_to_tentative_parse_p (parser)
18296 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
18298 error_at (token->location, "%<namespace%> definition is not allowed here");
18299 /* Skip the definition. */
18300 cp_lexer_consume_token (parser->lexer);
18301 if (cp_parser_skip_to_closing_brace (parser))
18302 cp_lexer_consume_token (parser->lexer);
18303 return;
18305 cp_parser_require (parser, CPP_EQ, RT_EQ);
18306 /* Look for the qualified-namespace-specifier. */
18307 namespace_specifier
18308 = cp_parser_qualified_namespace_specifier (parser);
18309 /* Look for the `;' token. */
18310 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
18312 /* Register the alias in the symbol table. */
18313 do_namespace_alias (identifier, namespace_specifier);
18316 /* Parse a qualified-namespace-specifier.
18318 qualified-namespace-specifier:
18319 :: [opt] nested-name-specifier [opt] namespace-name
18321 Returns a NAMESPACE_DECL corresponding to the specified
18322 namespace. */
18324 static tree
18325 cp_parser_qualified_namespace_specifier (cp_parser* parser)
18327 /* Look for the optional `::'. */
18328 cp_parser_global_scope_opt (parser,
18329 /*current_scope_valid_p=*/false);
18331 /* Look for the optional nested-name-specifier. */
18332 cp_parser_nested_name_specifier_opt (parser,
18333 /*typename_keyword_p=*/false,
18334 /*check_dependency_p=*/true,
18335 /*type_p=*/false,
18336 /*is_declaration=*/true);
18338 return cp_parser_namespace_name (parser);
18341 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
18342 access declaration.
18344 using-declaration:
18345 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
18346 using :: unqualified-id ;
18348 access-declaration:
18349 qualified-id ;
18353 static bool
18354 cp_parser_using_declaration (cp_parser* parser,
18355 bool access_declaration_p)
18357 cp_token *token;
18358 bool typename_p = false;
18359 bool global_scope_p;
18360 tree decl;
18361 tree identifier;
18362 tree qscope;
18363 int oldcount = errorcount;
18364 cp_token *diag_token = NULL;
18366 if (access_declaration_p)
18368 diag_token = cp_lexer_peek_token (parser->lexer);
18369 cp_parser_parse_tentatively (parser);
18371 else
18373 /* Look for the `using' keyword. */
18374 cp_parser_require_keyword (parser, RID_USING, RT_USING);
18376 again:
18377 /* Peek at the next token. */
18378 token = cp_lexer_peek_token (parser->lexer);
18379 /* See if it's `typename'. */
18380 if (token->keyword == RID_TYPENAME)
18382 /* Remember that we've seen it. */
18383 typename_p = true;
18384 /* Consume the `typename' token. */
18385 cp_lexer_consume_token (parser->lexer);
18389 /* Look for the optional global scope qualification. */
18390 global_scope_p
18391 = (cp_parser_global_scope_opt (parser,
18392 /*current_scope_valid_p=*/false)
18393 != NULL_TREE);
18395 /* If we saw `typename', or didn't see `::', then there must be a
18396 nested-name-specifier present. */
18397 if (typename_p || !global_scope_p)
18399 qscope = cp_parser_nested_name_specifier (parser, typename_p,
18400 /*check_dependency_p=*/true,
18401 /*type_p=*/false,
18402 /*is_declaration=*/true);
18403 if (!qscope && !cp_parser_uncommitted_to_tentative_parse_p (parser))
18405 cp_parser_skip_to_end_of_block_or_statement (parser);
18406 return false;
18409 /* Otherwise, we could be in either of the two productions. In that
18410 case, treat the nested-name-specifier as optional. */
18411 else
18412 qscope = cp_parser_nested_name_specifier_opt (parser,
18413 /*typename_keyword_p=*/false,
18414 /*check_dependency_p=*/true,
18415 /*type_p=*/false,
18416 /*is_declaration=*/true);
18417 if (!qscope)
18418 qscope = global_namespace;
18419 else if (UNSCOPED_ENUM_P (qscope))
18420 qscope = CP_TYPE_CONTEXT (qscope);
18422 if (access_declaration_p && cp_parser_error_occurred (parser))
18423 /* Something has already gone wrong; there's no need to parse
18424 further. Since an error has occurred, the return value of
18425 cp_parser_parse_definitely will be false, as required. */
18426 return cp_parser_parse_definitely (parser);
18428 token = cp_lexer_peek_token (parser->lexer);
18429 /* Parse the unqualified-id. */
18430 identifier = cp_parser_unqualified_id (parser,
18431 /*template_keyword_p=*/false,
18432 /*check_dependency_p=*/true,
18433 /*declarator_p=*/true,
18434 /*optional_p=*/false);
18436 if (access_declaration_p)
18438 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
18439 cp_parser_simulate_error (parser);
18440 if (!cp_parser_parse_definitely (parser))
18441 return false;
18443 else if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
18445 cp_token *ell = cp_lexer_consume_token (parser->lexer);
18446 if (cxx_dialect < cxx1z
18447 && !in_system_header_at (ell->location))
18448 pedwarn (ell->location, 0,
18449 "pack expansion in using-declaration only available "
18450 "with -std=c++1z or -std=gnu++1z");
18451 qscope = make_pack_expansion (qscope);
18454 /* The function we call to handle a using-declaration is different
18455 depending on what scope we are in. */
18456 if (qscope == error_mark_node || identifier == error_mark_node)
18458 else if (!identifier_p (identifier)
18459 && TREE_CODE (identifier) != BIT_NOT_EXPR)
18460 /* [namespace.udecl]
18462 A using declaration shall not name a template-id. */
18463 error_at (token->location,
18464 "a template-id may not appear in a using-declaration");
18465 else
18467 if (at_class_scope_p ())
18469 /* Create the USING_DECL. */
18470 decl = do_class_using_decl (qscope, identifier);
18472 if (decl && typename_p)
18473 USING_DECL_TYPENAME_P (decl) = 1;
18475 if (check_for_bare_parameter_packs (decl))
18477 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
18478 return false;
18480 else
18481 /* Add it to the list of members in this class. */
18482 finish_member_declaration (decl);
18484 else
18486 decl = cp_parser_lookup_name_simple (parser,
18487 identifier,
18488 token->location);
18489 if (decl == error_mark_node)
18490 cp_parser_name_lookup_error (parser, identifier,
18491 decl, NLE_NULL,
18492 token->location);
18493 else if (check_for_bare_parameter_packs (decl))
18495 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
18496 return false;
18498 else if (!at_namespace_scope_p ())
18499 do_local_using_decl (decl, qscope, identifier);
18500 else
18501 do_toplevel_using_decl (decl, qscope, identifier);
18505 if (!access_declaration_p
18506 && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
18508 cp_token *comma = cp_lexer_consume_token (parser->lexer);
18509 if (cxx_dialect < cxx1z)
18510 pedwarn (comma->location, 0,
18511 "comma-separated list in using-declaration only available "
18512 "with -std=c++1z or -std=gnu++1z");
18513 goto again;
18516 /* Look for the final `;'. */
18517 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
18519 if (access_declaration_p && errorcount == oldcount)
18520 warning_at (diag_token->location, OPT_Wdeprecated,
18521 "access declarations are deprecated "
18522 "in favour of using-declarations; "
18523 "suggestion: add the %<using%> keyword");
18525 return true;
18528 /* Parse an alias-declaration.
18530 alias-declaration:
18531 using identifier attribute-specifier-seq [opt] = type-id */
18533 static tree
18534 cp_parser_alias_declaration (cp_parser* parser)
18536 tree id, type, decl, pushed_scope = NULL_TREE, attributes;
18537 location_t id_location;
18538 cp_declarator *declarator;
18539 cp_decl_specifier_seq decl_specs;
18540 bool member_p;
18541 const char *saved_message = NULL;
18543 /* Look for the `using' keyword. */
18544 cp_token *using_token
18545 = cp_parser_require_keyword (parser, RID_USING, RT_USING);
18546 if (using_token == NULL)
18547 return error_mark_node;
18549 id_location = cp_lexer_peek_token (parser->lexer)->location;
18550 id = cp_parser_identifier (parser);
18551 if (id == error_mark_node)
18552 return error_mark_node;
18554 cp_token *attrs_token = cp_lexer_peek_token (parser->lexer);
18555 attributes = cp_parser_attributes_opt (parser);
18556 if (attributes == error_mark_node)
18557 return error_mark_node;
18559 cp_parser_require (parser, CPP_EQ, RT_EQ);
18561 if (cp_parser_error_occurred (parser))
18562 return error_mark_node;
18564 cp_parser_commit_to_tentative_parse (parser);
18566 /* Now we are going to parse the type-id of the declaration. */
18569 [dcl.type]/3 says:
18571 "A type-specifier-seq shall not define a class or enumeration
18572 unless it appears in the type-id of an alias-declaration (7.1.3) that
18573 is not the declaration of a template-declaration."
18575 In other words, if we currently are in an alias template, the
18576 type-id should not define a type.
18578 So let's set parser->type_definition_forbidden_message in that
18579 case; cp_parser_check_type_definition (called by
18580 cp_parser_class_specifier) will then emit an error if a type is
18581 defined in the type-id. */
18582 if (parser->num_template_parameter_lists)
18584 saved_message = parser->type_definition_forbidden_message;
18585 parser->type_definition_forbidden_message =
18586 G_("types may not be defined in alias template declarations");
18589 type = cp_parser_type_id (parser);
18591 /* Restore the error message if need be. */
18592 if (parser->num_template_parameter_lists)
18593 parser->type_definition_forbidden_message = saved_message;
18595 if (type == error_mark_node
18596 || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
18598 cp_parser_skip_to_end_of_block_or_statement (parser);
18599 return error_mark_node;
18602 /* A typedef-name can also be introduced by an alias-declaration. The
18603 identifier following the using keyword becomes a typedef-name. It has
18604 the same semantics as if it were introduced by the typedef
18605 specifier. In particular, it does not define a new type and it shall
18606 not appear in the type-id. */
18608 clear_decl_specs (&decl_specs);
18609 decl_specs.type = type;
18610 if (attributes != NULL_TREE)
18612 decl_specs.attributes = attributes;
18613 set_and_check_decl_spec_loc (&decl_specs,
18614 ds_attribute,
18615 attrs_token);
18617 set_and_check_decl_spec_loc (&decl_specs,
18618 ds_typedef,
18619 using_token);
18620 set_and_check_decl_spec_loc (&decl_specs,
18621 ds_alias,
18622 using_token);
18624 declarator = make_id_declarator (NULL_TREE, id, sfk_none);
18625 declarator->id_loc = id_location;
18627 member_p = at_class_scope_p ();
18628 if (member_p)
18629 decl = grokfield (declarator, &decl_specs, NULL_TREE, false,
18630 NULL_TREE, attributes);
18631 else
18632 decl = start_decl (declarator, &decl_specs, 0,
18633 attributes, NULL_TREE, &pushed_scope);
18634 if (decl == error_mark_node)
18635 return decl;
18637 // Attach constraints to the alias declaration.
18638 if (flag_concepts && current_template_parms)
18640 tree reqs = TEMPLATE_PARMS_CONSTRAINTS (current_template_parms);
18641 tree constr = build_constraints (reqs, NULL_TREE);
18642 set_constraints (decl, constr);
18645 cp_finish_decl (decl, NULL_TREE, 0, NULL_TREE, 0);
18647 if (pushed_scope)
18648 pop_scope (pushed_scope);
18650 /* If decl is a template, return its TEMPLATE_DECL so that it gets
18651 added into the symbol table; otherwise, return the TYPE_DECL. */
18652 if (DECL_LANG_SPECIFIC (decl)
18653 && DECL_TEMPLATE_INFO (decl)
18654 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl)))
18656 decl = DECL_TI_TEMPLATE (decl);
18657 if (member_p)
18658 check_member_template (decl);
18661 return decl;
18664 /* Parse a using-directive.
18666 using-directive:
18667 using namespace :: [opt] nested-name-specifier [opt]
18668 namespace-name ; */
18670 static void
18671 cp_parser_using_directive (cp_parser* parser)
18673 tree namespace_decl;
18674 tree attribs;
18676 /* Look for the `using' keyword. */
18677 cp_parser_require_keyword (parser, RID_USING, RT_USING);
18678 /* And the `namespace' keyword. */
18679 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
18680 /* Look for the optional `::' operator. */
18681 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
18682 /* And the optional nested-name-specifier. */
18683 cp_parser_nested_name_specifier_opt (parser,
18684 /*typename_keyword_p=*/false,
18685 /*check_dependency_p=*/true,
18686 /*type_p=*/false,
18687 /*is_declaration=*/true);
18688 /* Get the namespace being used. */
18689 namespace_decl = cp_parser_namespace_name (parser);
18690 /* And any specified attributes. */
18691 attribs = cp_parser_attributes_opt (parser);
18692 /* Update the symbol table. */
18693 parse_using_directive (namespace_decl, attribs);
18694 /* Look for the final `;'. */
18695 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
18698 /* Parse an asm-definition.
18700 asm-definition:
18701 asm ( string-literal ) ;
18703 GNU Extension:
18705 asm-definition:
18706 asm volatile [opt] ( string-literal ) ;
18707 asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
18708 asm volatile [opt] ( string-literal : asm-operand-list [opt]
18709 : asm-operand-list [opt] ) ;
18710 asm volatile [opt] ( string-literal : asm-operand-list [opt]
18711 : asm-operand-list [opt]
18712 : asm-clobber-list [opt] ) ;
18713 asm volatile [opt] goto ( string-literal : : asm-operand-list [opt]
18714 : asm-clobber-list [opt]
18715 : asm-goto-list ) ; */
18717 static void
18718 cp_parser_asm_definition (cp_parser* parser)
18720 tree string;
18721 tree outputs = NULL_TREE;
18722 tree inputs = NULL_TREE;
18723 tree clobbers = NULL_TREE;
18724 tree labels = NULL_TREE;
18725 tree asm_stmt;
18726 bool volatile_p = false;
18727 bool extended_p = false;
18728 bool invalid_inputs_p = false;
18729 bool invalid_outputs_p = false;
18730 bool goto_p = false;
18731 required_token missing = RT_NONE;
18733 /* Look for the `asm' keyword. */
18734 cp_parser_require_keyword (parser, RID_ASM, RT_ASM);
18736 if (parser->in_function_body
18737 && DECL_DECLARED_CONSTEXPR_P (current_function_decl))
18739 error ("%<asm%> in %<constexpr%> function");
18740 cp_function_chain->invalid_constexpr = true;
18743 /* See if the next token is `volatile'. */
18744 if (cp_parser_allow_gnu_extensions_p (parser)
18745 && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
18747 /* Remember that we saw the `volatile' keyword. */
18748 volatile_p = true;
18749 /* Consume the token. */
18750 cp_lexer_consume_token (parser->lexer);
18752 if (cp_parser_allow_gnu_extensions_p (parser)
18753 && parser->in_function_body
18754 && cp_lexer_next_token_is_keyword (parser->lexer, RID_GOTO))
18756 /* Remember that we saw the `goto' keyword. */
18757 goto_p = true;
18758 /* Consume the token. */
18759 cp_lexer_consume_token (parser->lexer);
18761 /* Look for the opening `('. */
18762 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
18763 return;
18764 /* Look for the string. */
18765 string = cp_parser_string_literal (parser, false, false);
18766 if (string == error_mark_node)
18768 cp_parser_skip_to_closing_parenthesis (parser, true, false,
18769 /*consume_paren=*/true);
18770 return;
18773 /* If we're allowing GNU extensions, check for the extended assembly
18774 syntax. Unfortunately, the `:' tokens need not be separated by
18775 a space in C, and so, for compatibility, we tolerate that here
18776 too. Doing that means that we have to treat the `::' operator as
18777 two `:' tokens. */
18778 if (cp_parser_allow_gnu_extensions_p (parser)
18779 && parser->in_function_body
18780 && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
18781 || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
18783 bool inputs_p = false;
18784 bool clobbers_p = false;
18785 bool labels_p = false;
18787 /* The extended syntax was used. */
18788 extended_p = true;
18790 /* Look for outputs. */
18791 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
18793 /* Consume the `:'. */
18794 cp_lexer_consume_token (parser->lexer);
18795 /* Parse the output-operands. */
18796 if (cp_lexer_next_token_is_not (parser->lexer,
18797 CPP_COLON)
18798 && cp_lexer_next_token_is_not (parser->lexer,
18799 CPP_SCOPE)
18800 && cp_lexer_next_token_is_not (parser->lexer,
18801 CPP_CLOSE_PAREN)
18802 && !goto_p)
18804 outputs = cp_parser_asm_operand_list (parser);
18805 if (outputs == error_mark_node)
18806 invalid_outputs_p = true;
18809 /* If the next token is `::', there are no outputs, and the
18810 next token is the beginning of the inputs. */
18811 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
18812 /* The inputs are coming next. */
18813 inputs_p = true;
18815 /* Look for inputs. */
18816 if (inputs_p
18817 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
18819 /* Consume the `:' or `::'. */
18820 cp_lexer_consume_token (parser->lexer);
18821 /* Parse the output-operands. */
18822 if (cp_lexer_next_token_is_not (parser->lexer,
18823 CPP_COLON)
18824 && cp_lexer_next_token_is_not (parser->lexer,
18825 CPP_SCOPE)
18826 && cp_lexer_next_token_is_not (parser->lexer,
18827 CPP_CLOSE_PAREN))
18829 inputs = cp_parser_asm_operand_list (parser);
18830 if (inputs == error_mark_node)
18831 invalid_inputs_p = true;
18834 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
18835 /* The clobbers are coming next. */
18836 clobbers_p = true;
18838 /* Look for clobbers. */
18839 if (clobbers_p
18840 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
18842 clobbers_p = true;
18843 /* Consume the `:' or `::'. */
18844 cp_lexer_consume_token (parser->lexer);
18845 /* Parse the clobbers. */
18846 if (cp_lexer_next_token_is_not (parser->lexer,
18847 CPP_COLON)
18848 && cp_lexer_next_token_is_not (parser->lexer,
18849 CPP_CLOSE_PAREN))
18850 clobbers = cp_parser_asm_clobber_list (parser);
18852 else if (goto_p
18853 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
18854 /* The labels are coming next. */
18855 labels_p = true;
18857 /* Look for labels. */
18858 if (labels_p
18859 || (goto_p && cp_lexer_next_token_is (parser->lexer, CPP_COLON)))
18861 labels_p = true;
18862 /* Consume the `:' or `::'. */
18863 cp_lexer_consume_token (parser->lexer);
18864 /* Parse the labels. */
18865 labels = cp_parser_asm_label_list (parser);
18868 if (goto_p && !labels_p)
18869 missing = clobbers_p ? RT_COLON : RT_COLON_SCOPE;
18871 else if (goto_p)
18872 missing = RT_COLON_SCOPE;
18874 /* Look for the closing `)'. */
18875 if (!cp_parser_require (parser, missing ? CPP_COLON : CPP_CLOSE_PAREN,
18876 missing ? missing : RT_CLOSE_PAREN))
18877 cp_parser_skip_to_closing_parenthesis (parser, true, false,
18878 /*consume_paren=*/true);
18879 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
18881 if (!invalid_inputs_p && !invalid_outputs_p)
18883 /* Create the ASM_EXPR. */
18884 if (parser->in_function_body)
18886 asm_stmt = finish_asm_stmt (volatile_p, string, outputs,
18887 inputs, clobbers, labels);
18888 /* If the extended syntax was not used, mark the ASM_EXPR. */
18889 if (!extended_p)
18891 tree temp = asm_stmt;
18892 if (TREE_CODE (temp) == CLEANUP_POINT_EXPR)
18893 temp = TREE_OPERAND (temp, 0);
18895 ASM_INPUT_P (temp) = 1;
18898 else
18899 symtab->finalize_toplevel_asm (string);
18903 /* Given the type TYPE of a declaration with declarator DECLARATOR, return the
18904 type that comes from the decl-specifier-seq. */
18906 static tree
18907 strip_declarator_types (tree type, cp_declarator *declarator)
18909 for (cp_declarator *d = declarator; d;)
18910 switch (d->kind)
18912 case cdk_id:
18913 case cdk_decomp:
18914 case cdk_error:
18915 d = NULL;
18916 break;
18918 default:
18919 if (TYPE_PTRMEMFUNC_P (type))
18920 type = TYPE_PTRMEMFUNC_FN_TYPE (type);
18921 type = TREE_TYPE (type);
18922 d = d->declarator;
18923 break;
18926 return type;
18929 /* Declarators [gram.dcl.decl] */
18931 /* Parse an init-declarator.
18933 init-declarator:
18934 declarator initializer [opt]
18936 GNU Extension:
18938 init-declarator:
18939 declarator asm-specification [opt] attributes [opt] initializer [opt]
18941 function-definition:
18942 decl-specifier-seq [opt] declarator ctor-initializer [opt]
18943 function-body
18944 decl-specifier-seq [opt] declarator function-try-block
18946 GNU Extension:
18948 function-definition:
18949 __extension__ function-definition
18951 TM Extension:
18953 function-definition:
18954 decl-specifier-seq [opt] declarator function-transaction-block
18956 The DECL_SPECIFIERS apply to this declarator. Returns a
18957 representation of the entity declared. If MEMBER_P is TRUE, then
18958 this declarator appears in a class scope. The new DECL created by
18959 this declarator is returned.
18961 The CHECKS are access checks that should be performed once we know
18962 what entity is being declared (and, therefore, what classes have
18963 befriended it).
18965 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
18966 for a function-definition here as well. If the declarator is a
18967 declarator for a function-definition, *FUNCTION_DEFINITION_P will
18968 be TRUE upon return. By that point, the function-definition will
18969 have been completely parsed.
18971 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
18972 is FALSE.
18974 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
18975 parsed declaration if it is an uninitialized single declarator not followed
18976 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
18977 if present, will not be consumed. If returned, this declarator will be
18978 created with SD_INITIALIZED but will not call cp_finish_decl.
18980 If INIT_LOC is not NULL, and *INIT_LOC is equal to UNKNOWN_LOCATION,
18981 and there is an initializer, the pointed location_t is set to the
18982 location of the '=' or `(', or '{' in C++11 token introducing the
18983 initializer. */
18985 static tree
18986 cp_parser_init_declarator (cp_parser* parser,
18987 cp_decl_specifier_seq *decl_specifiers,
18988 vec<deferred_access_check, va_gc> *checks,
18989 bool function_definition_allowed_p,
18990 bool member_p,
18991 int declares_class_or_enum,
18992 bool* function_definition_p,
18993 tree* maybe_range_for_decl,
18994 location_t* init_loc,
18995 tree* auto_result)
18997 cp_token *token = NULL, *asm_spec_start_token = NULL,
18998 *attributes_start_token = NULL;
18999 cp_declarator *declarator;
19000 tree prefix_attributes;
19001 tree attributes = NULL;
19002 tree asm_specification;
19003 tree initializer;
19004 tree decl = NULL_TREE;
19005 tree scope;
19006 int is_initialized;
19007 /* Only valid if IS_INITIALIZED is true. In that case, CPP_EQ if
19008 initialized with "= ..", CPP_OPEN_PAREN if initialized with
19009 "(...)". */
19010 enum cpp_ttype initialization_kind;
19011 bool is_direct_init = false;
19012 bool is_non_constant_init;
19013 int ctor_dtor_or_conv_p;
19014 bool friend_p = cp_parser_friend_p (decl_specifiers);
19015 tree pushed_scope = NULL_TREE;
19016 bool range_for_decl_p = false;
19017 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
19018 location_t tmp_init_loc = UNKNOWN_LOCATION;
19020 /* Gather the attributes that were provided with the
19021 decl-specifiers. */
19022 prefix_attributes = decl_specifiers->attributes;
19024 /* Assume that this is not the declarator for a function
19025 definition. */
19026 if (function_definition_p)
19027 *function_definition_p = false;
19029 /* Default arguments are only permitted for function parameters. */
19030 if (decl_spec_seq_has_spec_p (decl_specifiers, ds_typedef))
19031 parser->default_arg_ok_p = false;
19033 /* Defer access checks while parsing the declarator; we cannot know
19034 what names are accessible until we know what is being
19035 declared. */
19036 resume_deferring_access_checks ();
19038 token = cp_lexer_peek_token (parser->lexer);
19040 /* Parse the declarator. */
19041 declarator
19042 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
19043 &ctor_dtor_or_conv_p,
19044 /*parenthesized_p=*/NULL,
19045 member_p, friend_p);
19046 /* Gather up the deferred checks. */
19047 stop_deferring_access_checks ();
19049 parser->default_arg_ok_p = saved_default_arg_ok_p;
19051 /* If the DECLARATOR was erroneous, there's no need to go
19052 further. */
19053 if (declarator == cp_error_declarator)
19054 return error_mark_node;
19056 /* Check that the number of template-parameter-lists is OK. */
19057 if (!cp_parser_check_declarator_template_parameters (parser, declarator,
19058 token->location))
19059 return error_mark_node;
19061 if (declares_class_or_enum & 2)
19062 cp_parser_check_for_definition_in_return_type (declarator,
19063 decl_specifiers->type,
19064 decl_specifiers->locations[ds_type_spec]);
19066 /* Figure out what scope the entity declared by the DECLARATOR is
19067 located in. `grokdeclarator' sometimes changes the scope, so
19068 we compute it now. */
19069 scope = get_scope_of_declarator (declarator);
19071 /* Perform any lookups in the declared type which were thought to be
19072 dependent, but are not in the scope of the declarator. */
19073 decl_specifiers->type
19074 = maybe_update_decl_type (decl_specifiers->type, scope);
19076 /* If we're allowing GNU extensions, look for an
19077 asm-specification. */
19078 if (cp_parser_allow_gnu_extensions_p (parser))
19080 /* Look for an asm-specification. */
19081 asm_spec_start_token = cp_lexer_peek_token (parser->lexer);
19082 asm_specification = cp_parser_asm_specification_opt (parser);
19084 else
19085 asm_specification = NULL_TREE;
19087 /* Look for attributes. */
19088 attributes_start_token = cp_lexer_peek_token (parser->lexer);
19089 attributes = cp_parser_attributes_opt (parser);
19091 /* Peek at the next token. */
19092 token = cp_lexer_peek_token (parser->lexer);
19094 bool bogus_implicit_tmpl = false;
19096 if (function_declarator_p (declarator))
19098 /* Handle C++17 deduction guides. */
19099 if (!decl_specifiers->type
19100 && ctor_dtor_or_conv_p <= 0
19101 && cxx_dialect >= cxx1z)
19103 cp_declarator *id = get_id_declarator (declarator);
19104 tree name = id->u.id.unqualified_name;
19105 parser->scope = id->u.id.qualifying_scope;
19106 tree tmpl = cp_parser_lookup_name_simple (parser, name, id->id_loc);
19107 if (tmpl
19108 && (DECL_CLASS_TEMPLATE_P (tmpl)
19109 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl)))
19111 id->u.id.unqualified_name = dguide_name (tmpl);
19112 id->u.id.sfk = sfk_deduction_guide;
19113 ctor_dtor_or_conv_p = 1;
19117 /* Check to see if the token indicates the start of a
19118 function-definition. */
19119 if (cp_parser_token_starts_function_definition_p (token))
19121 if (!function_definition_allowed_p)
19123 /* If a function-definition should not appear here, issue an
19124 error message. */
19125 cp_parser_error (parser,
19126 "a function-definition is not allowed here");
19127 return error_mark_node;
19130 location_t func_brace_location
19131 = cp_lexer_peek_token (parser->lexer)->location;
19133 /* Neither attributes nor an asm-specification are allowed
19134 on a function-definition. */
19135 if (asm_specification)
19136 error_at (asm_spec_start_token->location,
19137 "an asm-specification is not allowed "
19138 "on a function-definition");
19139 if (attributes)
19140 error_at (attributes_start_token->location,
19141 "attributes are not allowed "
19142 "on a function-definition");
19143 /* This is a function-definition. */
19144 *function_definition_p = true;
19146 /* Parse the function definition. */
19147 if (member_p)
19148 decl = cp_parser_save_member_function_body (parser,
19149 decl_specifiers,
19150 declarator,
19151 prefix_attributes);
19152 else
19153 decl =
19154 (cp_parser_function_definition_from_specifiers_and_declarator
19155 (parser, decl_specifiers, prefix_attributes, declarator));
19157 if (decl != error_mark_node && DECL_STRUCT_FUNCTION (decl))
19159 /* This is where the prologue starts... */
19160 DECL_STRUCT_FUNCTION (decl)->function_start_locus
19161 = func_brace_location;
19164 return decl;
19167 else if (parser->fully_implicit_function_template_p)
19169 /* A non-template declaration involving a function parameter list
19170 containing an implicit template parameter will be made into a
19171 template. If the resulting declaration is not going to be an
19172 actual function then finish the template scope here to prevent it.
19173 An error message will be issued once we have a decl to talk about.
19175 FIXME probably we should do type deduction rather than create an
19176 implicit template, but the standard currently doesn't allow it. */
19177 bogus_implicit_tmpl = true;
19178 finish_fully_implicit_template (parser, NULL_TREE);
19181 /* [dcl.dcl]
19183 Only in function declarations for constructors, destructors, type
19184 conversions, and deduction guides can the decl-specifier-seq be omitted.
19186 We explicitly postpone this check past the point where we handle
19187 function-definitions because we tolerate function-definitions
19188 that are missing their return types in some modes. */
19189 if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
19191 cp_parser_error (parser,
19192 "expected constructor, destructor, or type conversion");
19193 return error_mark_node;
19196 /* An `=' or an `(', or an '{' in C++0x, indicates an initializer. */
19197 if (token->type == CPP_EQ
19198 || token->type == CPP_OPEN_PAREN
19199 || token->type == CPP_OPEN_BRACE)
19201 is_initialized = SD_INITIALIZED;
19202 initialization_kind = token->type;
19203 if (maybe_range_for_decl)
19204 *maybe_range_for_decl = error_mark_node;
19205 tmp_init_loc = token->location;
19206 if (init_loc && *init_loc == UNKNOWN_LOCATION)
19207 *init_loc = tmp_init_loc;
19209 if (token->type == CPP_EQ
19210 && function_declarator_p (declarator))
19212 cp_token *t2 = cp_lexer_peek_nth_token (parser->lexer, 2);
19213 if (t2->keyword == RID_DEFAULT)
19214 is_initialized = SD_DEFAULTED;
19215 else if (t2->keyword == RID_DELETE)
19216 is_initialized = SD_DELETED;
19219 else
19221 /* If the init-declarator isn't initialized and isn't followed by a
19222 `,' or `;', it's not a valid init-declarator. */
19223 if (token->type != CPP_COMMA
19224 && token->type != CPP_SEMICOLON)
19226 if (maybe_range_for_decl && *maybe_range_for_decl != error_mark_node)
19227 range_for_decl_p = true;
19228 else
19230 if (!maybe_range_for_decl)
19231 cp_parser_error (parser, "expected initializer");
19232 return error_mark_node;
19235 is_initialized = SD_UNINITIALIZED;
19236 initialization_kind = CPP_EOF;
19239 /* Because start_decl has side-effects, we should only call it if we
19240 know we're going ahead. By this point, we know that we cannot
19241 possibly be looking at any other construct. */
19242 cp_parser_commit_to_tentative_parse (parser);
19244 /* Enter the newly declared entry in the symbol table. If we're
19245 processing a declaration in a class-specifier, we wait until
19246 after processing the initializer. */
19247 if (!member_p)
19249 if (parser->in_unbraced_linkage_specification_p)
19250 decl_specifiers->storage_class = sc_extern;
19251 decl = start_decl (declarator, decl_specifiers,
19252 range_for_decl_p? SD_INITIALIZED : is_initialized,
19253 attributes, prefix_attributes, &pushed_scope);
19254 cp_finalize_omp_declare_simd (parser, decl);
19255 cp_finalize_oacc_routine (parser, decl, false);
19256 /* Adjust location of decl if declarator->id_loc is more appropriate:
19257 set, and decl wasn't merged with another decl, in which case its
19258 location would be different from input_location, and more accurate. */
19259 if (DECL_P (decl)
19260 && declarator->id_loc != UNKNOWN_LOCATION
19261 && DECL_SOURCE_LOCATION (decl) == input_location)
19262 DECL_SOURCE_LOCATION (decl) = declarator->id_loc;
19264 else if (scope)
19265 /* Enter the SCOPE. That way unqualified names appearing in the
19266 initializer will be looked up in SCOPE. */
19267 pushed_scope = push_scope (scope);
19269 /* Perform deferred access control checks, now that we know in which
19270 SCOPE the declared entity resides. */
19271 if (!member_p && decl)
19273 tree saved_current_function_decl = NULL_TREE;
19275 /* If the entity being declared is a function, pretend that we
19276 are in its scope. If it is a `friend', it may have access to
19277 things that would not otherwise be accessible. */
19278 if (TREE_CODE (decl) == FUNCTION_DECL)
19280 saved_current_function_decl = current_function_decl;
19281 current_function_decl = decl;
19284 /* Perform access checks for template parameters. */
19285 cp_parser_perform_template_parameter_access_checks (checks);
19287 /* Perform the access control checks for the declarator and the
19288 decl-specifiers. */
19289 perform_deferred_access_checks (tf_warning_or_error);
19291 /* Restore the saved value. */
19292 if (TREE_CODE (decl) == FUNCTION_DECL)
19293 current_function_decl = saved_current_function_decl;
19296 /* Parse the initializer. */
19297 initializer = NULL_TREE;
19298 is_direct_init = false;
19299 is_non_constant_init = true;
19300 if (is_initialized)
19302 if (function_declarator_p (declarator))
19304 if (initialization_kind == CPP_EQ)
19305 initializer = cp_parser_pure_specifier (parser);
19306 else
19308 /* If the declaration was erroneous, we don't really
19309 know what the user intended, so just silently
19310 consume the initializer. */
19311 if (decl != error_mark_node)
19312 error_at (tmp_init_loc, "initializer provided for function");
19313 cp_parser_skip_to_closing_parenthesis (parser,
19314 /*recovering=*/true,
19315 /*or_comma=*/false,
19316 /*consume_paren=*/true);
19319 else
19321 /* We want to record the extra mangling scope for in-class
19322 initializers of class members and initializers of static data
19323 member templates. The former involves deferring
19324 parsing of the initializer until end of class as with default
19325 arguments. So right here we only handle the latter. */
19326 if (!member_p && processing_template_decl)
19327 start_lambda_scope (decl);
19328 initializer = cp_parser_initializer (parser,
19329 &is_direct_init,
19330 &is_non_constant_init);
19331 if (!member_p && processing_template_decl)
19332 finish_lambda_scope ();
19333 if (initializer == error_mark_node)
19334 cp_parser_skip_to_end_of_statement (parser);
19338 /* The old parser allows attributes to appear after a parenthesized
19339 initializer. Mark Mitchell proposed removing this functionality
19340 on the GCC mailing lists on 2002-08-13. This parser accepts the
19341 attributes -- but ignores them. */
19342 if (cp_parser_allow_gnu_extensions_p (parser)
19343 && initialization_kind == CPP_OPEN_PAREN)
19344 if (cp_parser_attributes_opt (parser))
19345 warning (OPT_Wattributes,
19346 "attributes after parenthesized initializer ignored");
19348 /* And now complain about a non-function implicit template. */
19349 if (bogus_implicit_tmpl && decl != error_mark_node)
19350 error_at (DECL_SOURCE_LOCATION (decl),
19351 "non-function %qD declared as implicit template", decl);
19353 /* For an in-class declaration, use `grokfield' to create the
19354 declaration. */
19355 if (member_p)
19357 if (pushed_scope)
19359 pop_scope (pushed_scope);
19360 pushed_scope = NULL_TREE;
19362 decl = grokfield (declarator, decl_specifiers,
19363 initializer, !is_non_constant_init,
19364 /*asmspec=*/NULL_TREE,
19365 chainon (attributes, prefix_attributes));
19366 if (decl && TREE_CODE (decl) == FUNCTION_DECL)
19367 cp_parser_save_default_args (parser, decl);
19368 cp_finalize_omp_declare_simd (parser, decl);
19369 cp_finalize_oacc_routine (parser, decl, false);
19372 /* Finish processing the declaration. But, skip member
19373 declarations. */
19374 if (!member_p && decl && decl != error_mark_node && !range_for_decl_p)
19376 cp_finish_decl (decl,
19377 initializer, !is_non_constant_init,
19378 asm_specification,
19379 /* If the initializer is in parentheses, then this is
19380 a direct-initialization, which means that an
19381 `explicit' constructor is OK. Otherwise, an
19382 `explicit' constructor cannot be used. */
19383 ((is_direct_init || !is_initialized)
19384 ? LOOKUP_NORMAL : LOOKUP_IMPLICIT));
19386 else if ((cxx_dialect != cxx98) && friend_p
19387 && decl && TREE_CODE (decl) == FUNCTION_DECL)
19388 /* Core issue #226 (C++0x only): A default template-argument
19389 shall not be specified in a friend class template
19390 declaration. */
19391 check_default_tmpl_args (decl, current_template_parms, /*is_primary=*/true,
19392 /*is_partial=*/false, /*is_friend_decl=*/1);
19394 if (!friend_p && pushed_scope)
19395 pop_scope (pushed_scope);
19397 if (function_declarator_p (declarator)
19398 && parser->fully_implicit_function_template_p)
19400 if (member_p)
19401 decl = finish_fully_implicit_template (parser, decl);
19402 else
19403 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
19406 if (auto_result && is_initialized && decl_specifiers->type
19407 && type_uses_auto (decl_specifiers->type))
19408 *auto_result = strip_declarator_types (TREE_TYPE (decl), declarator);
19410 return decl;
19413 /* Parse a declarator.
19415 declarator:
19416 direct-declarator
19417 ptr-operator declarator
19419 abstract-declarator:
19420 ptr-operator abstract-declarator [opt]
19421 direct-abstract-declarator
19423 GNU Extensions:
19425 declarator:
19426 attributes [opt] direct-declarator
19427 attributes [opt] ptr-operator declarator
19429 abstract-declarator:
19430 attributes [opt] ptr-operator abstract-declarator [opt]
19431 attributes [opt] direct-abstract-declarator
19433 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
19434 detect constructors, destructors, deduction guides, or conversion operators.
19435 It is set to -1 if the declarator is a name, and +1 if it is a
19436 function. Otherwise it is set to zero. Usually you just want to
19437 test for >0, but internally the negative value is used.
19439 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
19440 a decl-specifier-seq unless it declares a constructor, destructor,
19441 or conversion. It might seem that we could check this condition in
19442 semantic analysis, rather than parsing, but that makes it difficult
19443 to handle something like `f()'. We want to notice that there are
19444 no decl-specifiers, and therefore realize that this is an
19445 expression, not a declaration.)
19447 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
19448 the declarator is a direct-declarator of the form "(...)".
19450 MEMBER_P is true iff this declarator is a member-declarator.
19452 FRIEND_P is true iff this declarator is a friend. */
19454 static cp_declarator *
19455 cp_parser_declarator (cp_parser* parser,
19456 cp_parser_declarator_kind dcl_kind,
19457 int* ctor_dtor_or_conv_p,
19458 bool* parenthesized_p,
19459 bool member_p, bool friend_p)
19461 cp_declarator *declarator;
19462 enum tree_code code;
19463 cp_cv_quals cv_quals;
19464 tree class_type;
19465 tree gnu_attributes = NULL_TREE, std_attributes = NULL_TREE;
19467 /* Assume this is not a constructor, destructor, or type-conversion
19468 operator. */
19469 if (ctor_dtor_or_conv_p)
19470 *ctor_dtor_or_conv_p = 0;
19472 if (cp_parser_allow_gnu_extensions_p (parser))
19473 gnu_attributes = cp_parser_gnu_attributes_opt (parser);
19475 /* Check for the ptr-operator production. */
19476 cp_parser_parse_tentatively (parser);
19477 /* Parse the ptr-operator. */
19478 code = cp_parser_ptr_operator (parser,
19479 &class_type,
19480 &cv_quals,
19481 &std_attributes);
19483 /* If that worked, then we have a ptr-operator. */
19484 if (cp_parser_parse_definitely (parser))
19486 /* If a ptr-operator was found, then this declarator was not
19487 parenthesized. */
19488 if (parenthesized_p)
19489 *parenthesized_p = true;
19490 /* The dependent declarator is optional if we are parsing an
19491 abstract-declarator. */
19492 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
19493 cp_parser_parse_tentatively (parser);
19495 /* Parse the dependent declarator. */
19496 declarator = cp_parser_declarator (parser, dcl_kind,
19497 /*ctor_dtor_or_conv_p=*/NULL,
19498 /*parenthesized_p=*/NULL,
19499 /*member_p=*/false,
19500 friend_p);
19502 /* If we are parsing an abstract-declarator, we must handle the
19503 case where the dependent declarator is absent. */
19504 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
19505 && !cp_parser_parse_definitely (parser))
19506 declarator = NULL;
19508 declarator = cp_parser_make_indirect_declarator
19509 (code, class_type, cv_quals, declarator, std_attributes);
19511 /* Everything else is a direct-declarator. */
19512 else
19514 if (parenthesized_p)
19515 *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
19516 CPP_OPEN_PAREN);
19517 declarator = cp_parser_direct_declarator (parser, dcl_kind,
19518 ctor_dtor_or_conv_p,
19519 member_p, friend_p);
19522 if (gnu_attributes && declarator && declarator != cp_error_declarator)
19523 declarator->attributes = gnu_attributes;
19524 return declarator;
19527 /* Parse a direct-declarator or direct-abstract-declarator.
19529 direct-declarator:
19530 declarator-id
19531 direct-declarator ( parameter-declaration-clause )
19532 cv-qualifier-seq [opt]
19533 ref-qualifier [opt]
19534 exception-specification [opt]
19535 direct-declarator [ constant-expression [opt] ]
19536 ( declarator )
19538 direct-abstract-declarator:
19539 direct-abstract-declarator [opt]
19540 ( parameter-declaration-clause )
19541 cv-qualifier-seq [opt]
19542 ref-qualifier [opt]
19543 exception-specification [opt]
19544 direct-abstract-declarator [opt] [ constant-expression [opt] ]
19545 ( abstract-declarator )
19547 Returns a representation of the declarator. DCL_KIND is
19548 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
19549 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
19550 we are parsing a direct-declarator. It is
19551 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
19552 of ambiguity we prefer an abstract declarator, as per
19553 [dcl.ambig.res]. CTOR_DTOR_OR_CONV_P, MEMBER_P, and FRIEND_P are
19554 as for cp_parser_declarator. */
19556 static cp_declarator *
19557 cp_parser_direct_declarator (cp_parser* parser,
19558 cp_parser_declarator_kind dcl_kind,
19559 int* ctor_dtor_or_conv_p,
19560 bool member_p, bool friend_p)
19562 cp_token *token;
19563 cp_declarator *declarator = NULL;
19564 tree scope = NULL_TREE;
19565 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
19566 bool saved_in_declarator_p = parser->in_declarator_p;
19567 bool first = true;
19568 tree pushed_scope = NULL_TREE;
19570 while (true)
19572 /* Peek at the next token. */
19573 token = cp_lexer_peek_token (parser->lexer);
19574 if (token->type == CPP_OPEN_PAREN)
19576 /* This is either a parameter-declaration-clause, or a
19577 parenthesized declarator. When we know we are parsing a
19578 named declarator, it must be a parenthesized declarator
19579 if FIRST is true. For instance, `(int)' is a
19580 parameter-declaration-clause, with an omitted
19581 direct-abstract-declarator. But `((*))', is a
19582 parenthesized abstract declarator. Finally, when T is a
19583 template parameter `(T)' is a
19584 parameter-declaration-clause, and not a parenthesized
19585 named declarator.
19587 We first try and parse a parameter-declaration-clause,
19588 and then try a nested declarator (if FIRST is true).
19590 It is not an error for it not to be a
19591 parameter-declaration-clause, even when FIRST is
19592 false. Consider,
19594 int i (int);
19595 int i (3);
19597 The first is the declaration of a function while the
19598 second is the definition of a variable, including its
19599 initializer.
19601 Having seen only the parenthesis, we cannot know which of
19602 these two alternatives should be selected. Even more
19603 complex are examples like:
19605 int i (int (a));
19606 int i (int (3));
19608 The former is a function-declaration; the latter is a
19609 variable initialization.
19611 Thus again, we try a parameter-declaration-clause, and if
19612 that fails, we back out and return. */
19614 if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
19616 tree params;
19617 bool is_declarator = false;
19619 /* In a member-declarator, the only valid interpretation
19620 of a parenthesis is the start of a
19621 parameter-declaration-clause. (It is invalid to
19622 initialize a static data member with a parenthesized
19623 initializer; only the "=" form of initialization is
19624 permitted.) */
19625 if (!member_p)
19626 cp_parser_parse_tentatively (parser);
19628 /* Consume the `('. */
19629 cp_lexer_consume_token (parser->lexer);
19630 if (first)
19632 /* If this is going to be an abstract declarator, we're
19633 in a declarator and we can't have default args. */
19634 parser->default_arg_ok_p = false;
19635 parser->in_declarator_p = true;
19638 begin_scope (sk_function_parms, NULL_TREE);
19640 /* Parse the parameter-declaration-clause. */
19641 params = cp_parser_parameter_declaration_clause (parser);
19643 /* Consume the `)'. */
19644 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
19646 /* If all went well, parse the cv-qualifier-seq,
19647 ref-qualifier and the exception-specification. */
19648 if (member_p || cp_parser_parse_definitely (parser))
19650 cp_cv_quals cv_quals;
19651 cp_virt_specifiers virt_specifiers;
19652 cp_ref_qualifier ref_qual;
19653 tree exception_specification;
19654 tree late_return;
19655 tree attrs;
19656 bool memfn = (member_p || (pushed_scope
19657 && CLASS_TYPE_P (pushed_scope)));
19659 is_declarator = true;
19661 if (ctor_dtor_or_conv_p)
19662 *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
19663 first = false;
19665 /* Parse the cv-qualifier-seq. */
19666 cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
19667 /* Parse the ref-qualifier. */
19668 ref_qual = cp_parser_ref_qualifier_opt (parser);
19669 /* Parse the tx-qualifier. */
19670 tree tx_qual = cp_parser_tx_qualifier_opt (parser);
19671 /* And the exception-specification. */
19672 exception_specification
19673 = cp_parser_exception_specification_opt (parser);
19675 attrs = cp_parser_std_attribute_spec_seq (parser);
19677 /* In here, we handle cases where attribute is used after
19678 the function declaration. For example:
19679 void func (int x) __attribute__((vector(..))); */
19680 tree gnu_attrs = NULL_TREE;
19681 if (flag_cilkplus
19682 && cp_next_tokens_can_be_gnu_attribute_p (parser))
19684 cp_parser_parse_tentatively (parser);
19685 tree attr = cp_parser_gnu_attributes_opt (parser);
19686 if (cp_lexer_next_token_is_not (parser->lexer,
19687 CPP_SEMICOLON)
19688 && cp_lexer_next_token_is_not (parser->lexer,
19689 CPP_OPEN_BRACE))
19690 cp_parser_abort_tentative_parse (parser);
19691 else if (!cp_parser_parse_definitely (parser))
19693 else
19694 gnu_attrs = attr;
19696 tree requires_clause = NULL_TREE;
19697 late_return = (cp_parser_late_return_type_opt
19698 (parser, declarator, requires_clause,
19699 memfn ? cv_quals : -1));
19701 /* Parse the virt-specifier-seq. */
19702 virt_specifiers = cp_parser_virt_specifier_seq_opt (parser);
19704 /* Create the function-declarator. */
19705 declarator = make_call_declarator (declarator,
19706 params,
19707 cv_quals,
19708 virt_specifiers,
19709 ref_qual,
19710 tx_qual,
19711 exception_specification,
19712 late_return,
19713 requires_clause);
19714 declarator->std_attributes = attrs;
19715 declarator->attributes = gnu_attrs;
19716 /* Any subsequent parameter lists are to do with
19717 return type, so are not those of the declared
19718 function. */
19719 parser->default_arg_ok_p = false;
19722 /* Remove the function parms from scope. */
19723 pop_bindings_and_leave_scope ();
19725 if (is_declarator)
19726 /* Repeat the main loop. */
19727 continue;
19730 /* If this is the first, we can try a parenthesized
19731 declarator. */
19732 if (first)
19734 bool saved_in_type_id_in_expr_p;
19736 parser->default_arg_ok_p = saved_default_arg_ok_p;
19737 parser->in_declarator_p = saved_in_declarator_p;
19739 /* Consume the `('. */
19740 cp_lexer_consume_token (parser->lexer);
19741 /* Parse the nested declarator. */
19742 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
19743 parser->in_type_id_in_expr_p = true;
19744 declarator
19745 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
19746 /*parenthesized_p=*/NULL,
19747 member_p, friend_p);
19748 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
19749 first = false;
19750 /* Expect a `)'. */
19751 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
19752 declarator = cp_error_declarator;
19753 if (declarator == cp_error_declarator)
19754 break;
19756 goto handle_declarator;
19758 /* Otherwise, we must be done. */
19759 else
19760 break;
19762 else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
19763 && token->type == CPP_OPEN_SQUARE
19764 && !cp_next_tokens_can_be_attribute_p (parser))
19766 /* Parse an array-declarator. */
19767 tree bounds, attrs;
19769 if (ctor_dtor_or_conv_p)
19770 *ctor_dtor_or_conv_p = 0;
19772 first = false;
19773 parser->default_arg_ok_p = false;
19774 parser->in_declarator_p = true;
19775 /* Consume the `['. */
19776 cp_lexer_consume_token (parser->lexer);
19777 /* Peek at the next token. */
19778 token = cp_lexer_peek_token (parser->lexer);
19779 /* If the next token is `]', then there is no
19780 constant-expression. */
19781 if (token->type != CPP_CLOSE_SQUARE)
19783 bool non_constant_p;
19784 bounds
19785 = cp_parser_constant_expression (parser,
19786 /*allow_non_constant=*/true,
19787 &non_constant_p);
19788 if (!non_constant_p)
19789 /* OK */;
19790 else if (error_operand_p (bounds))
19791 /* Already gave an error. */;
19792 else if (!parser->in_function_body
19793 || current_binding_level->kind == sk_function_parms)
19795 /* Normally, the array bound must be an integral constant
19796 expression. However, as an extension, we allow VLAs
19797 in function scopes as long as they aren't part of a
19798 parameter declaration. */
19799 cp_parser_error (parser,
19800 "array bound is not an integer constant");
19801 bounds = error_mark_node;
19803 else if (processing_template_decl
19804 && !type_dependent_expression_p (bounds))
19806 /* Remember this wasn't a constant-expression. */
19807 bounds = build_nop (TREE_TYPE (bounds), bounds);
19808 TREE_SIDE_EFFECTS (bounds) = 1;
19811 else
19812 bounds = NULL_TREE;
19813 /* Look for the closing `]'. */
19814 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
19816 declarator = cp_error_declarator;
19817 break;
19820 attrs = cp_parser_std_attribute_spec_seq (parser);
19821 declarator = make_array_declarator (declarator, bounds);
19822 declarator->std_attributes = attrs;
19824 else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
19827 tree qualifying_scope;
19828 tree unqualified_name;
19829 tree attrs;
19830 special_function_kind sfk;
19831 bool abstract_ok;
19832 bool pack_expansion_p = false;
19833 cp_token *declarator_id_start_token;
19835 /* Parse a declarator-id */
19836 abstract_ok = (dcl_kind == CP_PARSER_DECLARATOR_EITHER);
19837 if (abstract_ok)
19839 cp_parser_parse_tentatively (parser);
19841 /* If we see an ellipsis, we should be looking at a
19842 parameter pack. */
19843 if (token->type == CPP_ELLIPSIS)
19845 /* Consume the `...' */
19846 cp_lexer_consume_token (parser->lexer);
19848 pack_expansion_p = true;
19852 declarator_id_start_token = cp_lexer_peek_token (parser->lexer);
19853 unqualified_name
19854 = cp_parser_declarator_id (parser, /*optional_p=*/abstract_ok);
19855 qualifying_scope = parser->scope;
19856 if (abstract_ok)
19858 bool okay = false;
19860 if (!unqualified_name && pack_expansion_p)
19862 /* Check whether an error occurred. */
19863 okay = !cp_parser_error_occurred (parser);
19865 /* We already consumed the ellipsis to mark a
19866 parameter pack, but we have no way to report it,
19867 so abort the tentative parse. We will be exiting
19868 immediately anyway. */
19869 cp_parser_abort_tentative_parse (parser);
19871 else
19872 okay = cp_parser_parse_definitely (parser);
19874 if (!okay)
19875 unqualified_name = error_mark_node;
19876 else if (unqualified_name
19877 && (qualifying_scope
19878 || (!identifier_p (unqualified_name))))
19880 cp_parser_error (parser, "expected unqualified-id");
19881 unqualified_name = error_mark_node;
19885 if (!unqualified_name)
19886 return NULL;
19887 if (unqualified_name == error_mark_node)
19889 declarator = cp_error_declarator;
19890 pack_expansion_p = false;
19891 declarator->parameter_pack_p = false;
19892 break;
19895 attrs = cp_parser_std_attribute_spec_seq (parser);
19897 if (qualifying_scope && at_namespace_scope_p ()
19898 && TREE_CODE (qualifying_scope) == TYPENAME_TYPE)
19900 /* In the declaration of a member of a template class
19901 outside of the class itself, the SCOPE will sometimes
19902 be a TYPENAME_TYPE. For example, given:
19904 template <typename T>
19905 int S<T>::R::i = 3;
19907 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
19908 this context, we must resolve S<T>::R to an ordinary
19909 type, rather than a typename type.
19911 The reason we normally avoid resolving TYPENAME_TYPEs
19912 is that a specialization of `S' might render
19913 `S<T>::R' not a type. However, if `S' is
19914 specialized, then this `i' will not be used, so there
19915 is no harm in resolving the types here. */
19916 tree type;
19918 /* Resolve the TYPENAME_TYPE. */
19919 type = resolve_typename_type (qualifying_scope,
19920 /*only_current_p=*/false);
19921 /* If that failed, the declarator is invalid. */
19922 if (TREE_CODE (type) == TYPENAME_TYPE)
19924 if (typedef_variant_p (type))
19925 error_at (declarator_id_start_token->location,
19926 "cannot define member of dependent typedef "
19927 "%qT", type);
19928 else
19929 error_at (declarator_id_start_token->location,
19930 "%<%T::%E%> is not a type",
19931 TYPE_CONTEXT (qualifying_scope),
19932 TYPE_IDENTIFIER (qualifying_scope));
19934 qualifying_scope = type;
19937 sfk = sfk_none;
19939 if (unqualified_name)
19941 tree class_type;
19943 if (qualifying_scope
19944 && CLASS_TYPE_P (qualifying_scope))
19945 class_type = qualifying_scope;
19946 else
19947 class_type = current_class_type;
19949 if (TREE_CODE (unqualified_name) == TYPE_DECL)
19951 tree name_type = TREE_TYPE (unqualified_name);
19952 if (class_type && same_type_p (name_type, class_type))
19954 if (qualifying_scope
19955 && CLASSTYPE_USE_TEMPLATE (name_type))
19957 error_at (declarator_id_start_token->location,
19958 "invalid use of constructor as a template");
19959 inform (declarator_id_start_token->location,
19960 "use %<%T::%D%> instead of %<%T::%D%> to "
19961 "name the constructor in a qualified name",
19962 class_type,
19963 DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
19964 class_type, name_type);
19965 declarator = cp_error_declarator;
19966 break;
19968 else
19969 unqualified_name = constructor_name (class_type);
19971 else
19973 /* We do not attempt to print the declarator
19974 here because we do not have enough
19975 information about its original syntactic
19976 form. */
19977 cp_parser_error (parser, "invalid declarator");
19978 declarator = cp_error_declarator;
19979 break;
19983 if (class_type)
19985 if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
19986 sfk = sfk_destructor;
19987 else if (IDENTIFIER_TYPENAME_P (unqualified_name))
19988 sfk = sfk_conversion;
19989 else if (/* There's no way to declare a constructor
19990 for an unnamed type, even if the type
19991 got a name for linkage purposes. */
19992 !TYPE_WAS_UNNAMED (class_type)
19993 /* Handle correctly (c++/19200):
19995 struct S {
19996 struct T{};
19997 friend void S(T);
20000 and also:
20002 namespace N {
20003 void S();
20006 struct S {
20007 friend void N::S();
20008 }; */
20009 && !(friend_p
20010 && class_type != qualifying_scope)
20011 && constructor_name_p (unqualified_name,
20012 class_type))
20014 unqualified_name = constructor_name (class_type);
20015 sfk = sfk_constructor;
20017 else if (is_overloaded_fn (unqualified_name)
20018 && DECL_CONSTRUCTOR_P (get_first_fn
20019 (unqualified_name)))
20020 sfk = sfk_constructor;
20022 if (ctor_dtor_or_conv_p && sfk != sfk_none)
20023 *ctor_dtor_or_conv_p = -1;
20026 declarator = make_id_declarator (qualifying_scope,
20027 unqualified_name,
20028 sfk);
20029 declarator->std_attributes = attrs;
20030 declarator->id_loc = token->location;
20031 declarator->parameter_pack_p = pack_expansion_p;
20033 if (pack_expansion_p)
20034 maybe_warn_variadic_templates ();
20037 handle_declarator:;
20038 scope = get_scope_of_declarator (declarator);
20039 if (scope)
20041 /* Any names that appear after the declarator-id for a
20042 member are looked up in the containing scope. */
20043 if (at_function_scope_p ())
20045 /* But declarations with qualified-ids can't appear in a
20046 function. */
20047 cp_parser_error (parser, "qualified-id in declaration");
20048 declarator = cp_error_declarator;
20049 break;
20051 pushed_scope = push_scope (scope);
20053 parser->in_declarator_p = true;
20054 if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
20055 || (declarator && declarator->kind == cdk_id))
20056 /* Default args are only allowed on function
20057 declarations. */
20058 parser->default_arg_ok_p = saved_default_arg_ok_p;
20059 else
20060 parser->default_arg_ok_p = false;
20062 first = false;
20064 /* We're done. */
20065 else
20066 break;
20069 /* For an abstract declarator, we might wind up with nothing at this
20070 point. That's an error; the declarator is not optional. */
20071 if (!declarator)
20072 cp_parser_error (parser, "expected declarator");
20074 /* If we entered a scope, we must exit it now. */
20075 if (pushed_scope)
20076 pop_scope (pushed_scope);
20078 parser->default_arg_ok_p = saved_default_arg_ok_p;
20079 parser->in_declarator_p = saved_in_declarator_p;
20081 return declarator;
20084 /* Parse a ptr-operator.
20086 ptr-operator:
20087 * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
20088 * cv-qualifier-seq [opt]
20090 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
20091 nested-name-specifier * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
20093 GNU Extension:
20095 ptr-operator:
20096 & cv-qualifier-seq [opt]
20098 Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
20099 Returns ADDR_EXPR if a reference was used, or NON_LVALUE_EXPR for
20100 an rvalue reference. In the case of a pointer-to-member, *TYPE is
20101 filled in with the TYPE containing the member. *CV_QUALS is
20102 filled in with the cv-qualifier-seq, or TYPE_UNQUALIFIED, if there
20103 are no cv-qualifiers. Returns ERROR_MARK if an error occurred.
20104 Note that the tree codes returned by this function have nothing
20105 to do with the types of trees that will be eventually be created
20106 to represent the pointer or reference type being parsed. They are
20107 just constants with suggestive names. */
20108 static enum tree_code
20109 cp_parser_ptr_operator (cp_parser* parser,
20110 tree* type,
20111 cp_cv_quals *cv_quals,
20112 tree *attributes)
20114 enum tree_code code = ERROR_MARK;
20115 cp_token *token;
20116 tree attrs = NULL_TREE;
20118 /* Assume that it's not a pointer-to-member. */
20119 *type = NULL_TREE;
20120 /* And that there are no cv-qualifiers. */
20121 *cv_quals = TYPE_UNQUALIFIED;
20123 /* Peek at the next token. */
20124 token = cp_lexer_peek_token (parser->lexer);
20126 /* If it's a `*', `&' or `&&' we have a pointer or reference. */
20127 if (token->type == CPP_MULT)
20128 code = INDIRECT_REF;
20129 else if (token->type == CPP_AND)
20130 code = ADDR_EXPR;
20131 else if ((cxx_dialect != cxx98) &&
20132 token->type == CPP_AND_AND) /* C++0x only */
20133 code = NON_LVALUE_EXPR;
20135 if (code != ERROR_MARK)
20137 /* Consume the `*', `&' or `&&'. */
20138 cp_lexer_consume_token (parser->lexer);
20140 /* A `*' can be followed by a cv-qualifier-seq, and so can a
20141 `&', if we are allowing GNU extensions. (The only qualifier
20142 that can legally appear after `&' is `restrict', but that is
20143 enforced during semantic analysis. */
20144 if (code == INDIRECT_REF
20145 || cp_parser_allow_gnu_extensions_p (parser))
20146 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
20148 attrs = cp_parser_std_attribute_spec_seq (parser);
20149 if (attributes != NULL)
20150 *attributes = attrs;
20152 else
20154 /* Try the pointer-to-member case. */
20155 cp_parser_parse_tentatively (parser);
20156 /* Look for the optional `::' operator. */
20157 cp_parser_global_scope_opt (parser,
20158 /*current_scope_valid_p=*/false);
20159 /* Look for the nested-name specifier. */
20160 token = cp_lexer_peek_token (parser->lexer);
20161 cp_parser_nested_name_specifier (parser,
20162 /*typename_keyword_p=*/false,
20163 /*check_dependency_p=*/true,
20164 /*type_p=*/false,
20165 /*is_declaration=*/false);
20166 /* If we found it, and the next token is a `*', then we are
20167 indeed looking at a pointer-to-member operator. */
20168 if (!cp_parser_error_occurred (parser)
20169 && cp_parser_require (parser, CPP_MULT, RT_MULT))
20171 /* Indicate that the `*' operator was used. */
20172 code = INDIRECT_REF;
20174 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
20175 error_at (token->location, "%qD is a namespace", parser->scope);
20176 else if (TREE_CODE (parser->scope) == ENUMERAL_TYPE)
20177 error_at (token->location, "cannot form pointer to member of "
20178 "non-class %q#T", parser->scope);
20179 else
20181 /* The type of which the member is a member is given by the
20182 current SCOPE. */
20183 *type = parser->scope;
20184 /* The next name will not be qualified. */
20185 parser->scope = NULL_TREE;
20186 parser->qualifying_scope = NULL_TREE;
20187 parser->object_scope = NULL_TREE;
20188 /* Look for optional c++11 attributes. */
20189 attrs = cp_parser_std_attribute_spec_seq (parser);
20190 if (attributes != NULL)
20191 *attributes = attrs;
20192 /* Look for the optional cv-qualifier-seq. */
20193 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
20196 /* If that didn't work we don't have a ptr-operator. */
20197 if (!cp_parser_parse_definitely (parser))
20198 cp_parser_error (parser, "expected ptr-operator");
20201 return code;
20204 /* Parse an (optional) cv-qualifier-seq.
20206 cv-qualifier-seq:
20207 cv-qualifier cv-qualifier-seq [opt]
20209 cv-qualifier:
20210 const
20211 volatile
20213 GNU Extension:
20215 cv-qualifier:
20216 __restrict__
20218 Returns a bitmask representing the cv-qualifiers. */
20220 static cp_cv_quals
20221 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
20223 cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
20225 while (true)
20227 cp_token *token;
20228 cp_cv_quals cv_qualifier;
20230 /* Peek at the next token. */
20231 token = cp_lexer_peek_token (parser->lexer);
20232 /* See if it's a cv-qualifier. */
20233 switch (token->keyword)
20235 case RID_CONST:
20236 cv_qualifier = TYPE_QUAL_CONST;
20237 break;
20239 case RID_VOLATILE:
20240 cv_qualifier = TYPE_QUAL_VOLATILE;
20241 break;
20243 case RID_RESTRICT:
20244 cv_qualifier = TYPE_QUAL_RESTRICT;
20245 break;
20247 default:
20248 cv_qualifier = TYPE_UNQUALIFIED;
20249 break;
20252 if (!cv_qualifier)
20253 break;
20255 if (cv_quals & cv_qualifier)
20257 error_at (token->location, "duplicate cv-qualifier");
20258 cp_lexer_purge_token (parser->lexer);
20260 else
20262 cp_lexer_consume_token (parser->lexer);
20263 cv_quals |= cv_qualifier;
20267 return cv_quals;
20270 /* Parse an (optional) ref-qualifier
20272 ref-qualifier:
20276 Returns cp_ref_qualifier representing ref-qualifier. */
20278 static cp_ref_qualifier
20279 cp_parser_ref_qualifier_opt (cp_parser* parser)
20281 cp_ref_qualifier ref_qual = REF_QUAL_NONE;
20283 /* Don't try to parse bitwise '&' as a ref-qualifier (c++/57532). */
20284 if (cxx_dialect < cxx11 && cp_parser_parsing_tentatively (parser))
20285 return ref_qual;
20287 while (true)
20289 cp_ref_qualifier curr_ref_qual = REF_QUAL_NONE;
20290 cp_token *token = cp_lexer_peek_token (parser->lexer);
20292 switch (token->type)
20294 case CPP_AND:
20295 curr_ref_qual = REF_QUAL_LVALUE;
20296 break;
20298 case CPP_AND_AND:
20299 curr_ref_qual = REF_QUAL_RVALUE;
20300 break;
20302 default:
20303 curr_ref_qual = REF_QUAL_NONE;
20304 break;
20307 if (!curr_ref_qual)
20308 break;
20309 else if (ref_qual)
20311 error_at (token->location, "multiple ref-qualifiers");
20312 cp_lexer_purge_token (parser->lexer);
20314 else
20316 ref_qual = curr_ref_qual;
20317 cp_lexer_consume_token (parser->lexer);
20321 return ref_qual;
20324 /* Parse an optional tx-qualifier.
20326 tx-qualifier:
20327 transaction_safe
20328 transaction_safe_dynamic */
20330 static tree
20331 cp_parser_tx_qualifier_opt (cp_parser *parser)
20333 cp_token *token = cp_lexer_peek_token (parser->lexer);
20334 if (token->type == CPP_NAME)
20336 tree name = token->u.value;
20337 const char *p = IDENTIFIER_POINTER (name);
20338 const int len = strlen ("transaction_safe");
20339 if (!strncmp (p, "transaction_safe", len))
20341 p += len;
20342 if (*p == '\0'
20343 || !strcmp (p, "_dynamic"))
20345 cp_lexer_consume_token (parser->lexer);
20346 if (!flag_tm)
20348 error ("%E requires %<-fgnu-tm%>", name);
20349 return NULL_TREE;
20351 else
20352 return name;
20356 return NULL_TREE;
20359 /* Parse an (optional) virt-specifier-seq.
20361 virt-specifier-seq:
20362 virt-specifier virt-specifier-seq [opt]
20364 virt-specifier:
20365 override
20366 final
20368 Returns a bitmask representing the virt-specifiers. */
20370 static cp_virt_specifiers
20371 cp_parser_virt_specifier_seq_opt (cp_parser* parser)
20373 cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
20375 while (true)
20377 cp_token *token;
20378 cp_virt_specifiers virt_specifier;
20380 /* Peek at the next token. */
20381 token = cp_lexer_peek_token (parser->lexer);
20382 /* See if it's a virt-specifier-qualifier. */
20383 if (token->type != CPP_NAME)
20384 break;
20385 if (!strcmp (IDENTIFIER_POINTER(token->u.value), "override"))
20387 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS);
20388 virt_specifier = VIRT_SPEC_OVERRIDE;
20390 else if (!strcmp (IDENTIFIER_POINTER(token->u.value), "final"))
20392 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS);
20393 virt_specifier = VIRT_SPEC_FINAL;
20395 else if (!strcmp (IDENTIFIER_POINTER(token->u.value), "__final"))
20397 virt_specifier = VIRT_SPEC_FINAL;
20399 else
20400 break;
20402 if (virt_specifiers & virt_specifier)
20404 error_at (token->location, "duplicate virt-specifier");
20405 cp_lexer_purge_token (parser->lexer);
20407 else
20409 cp_lexer_consume_token (parser->lexer);
20410 virt_specifiers |= virt_specifier;
20413 return virt_specifiers;
20416 /* Used by handling of trailing-return-types and NSDMI, in which 'this'
20417 is in scope even though it isn't real. */
20419 void
20420 inject_this_parameter (tree ctype, cp_cv_quals quals)
20422 tree this_parm;
20424 if (current_class_ptr)
20426 /* We don't clear this between NSDMIs. Is it already what we want? */
20427 tree type = TREE_TYPE (TREE_TYPE (current_class_ptr));
20428 if (same_type_ignoring_top_level_qualifiers_p (ctype, type)
20429 && cp_type_quals (type) == quals)
20430 return;
20433 this_parm = build_this_parm (ctype, quals);
20434 /* Clear this first to avoid shortcut in cp_build_indirect_ref. */
20435 current_class_ptr = NULL_TREE;
20436 current_class_ref
20437 = cp_build_indirect_ref (this_parm, RO_NULL, tf_warning_or_error);
20438 current_class_ptr = this_parm;
20441 /* Return true iff our current scope is a non-static data member
20442 initializer. */
20444 bool
20445 parsing_nsdmi (void)
20447 /* We recognize NSDMI context by the context-less 'this' pointer set up
20448 by the function above. */
20449 if (current_class_ptr
20450 && TREE_CODE (current_class_ptr) == PARM_DECL
20451 && DECL_CONTEXT (current_class_ptr) == NULL_TREE)
20452 return true;
20453 return false;
20456 /* Return true iff our current scope is a default capturing generic lambda
20457 defined within a template. FIXME: This is part of a workaround (see
20458 semantics.c) to handle building lambda closure types correctly in templates
20459 which we ultimately want to defer to instantiation time. */
20461 bool
20462 parsing_default_capturing_generic_lambda_in_template (void)
20464 if (!processing_template_decl || !current_class_type)
20465 return false;
20467 tree lam = CLASSTYPE_LAMBDA_EXPR (current_class_type);
20468 if (!lam || LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lam) == CPLD_NONE)
20469 return false;
20471 tree callop = lambda_function (lam);
20472 if (!callop)
20473 return false;
20475 return (DECL_TEMPLATE_INFO (callop)
20476 && (DECL_TEMPLATE_RESULT (DECL_TI_TEMPLATE (callop)) == callop)
20477 && ((current_nonlambda_class_type ()
20478 && CLASSTYPE_TEMPLATE_INFO (current_nonlambda_class_type ()))
20479 || ((current_nonlambda_function ()
20480 && DECL_TEMPLATE_INFO (current_nonlambda_function ())))));
20483 /* Parse a late-specified return type, if any. This is not a separate
20484 non-terminal, but part of a function declarator, which looks like
20486 -> trailing-type-specifier-seq abstract-declarator(opt)
20488 Returns the type indicated by the type-id.
20490 In addition to this, parse any queued up #pragma omp declare simd
20491 clauses, Cilk Plus SIMD-enabled functions' vector attributes, and
20492 #pragma acc routine clauses.
20494 QUALS is either a bitmask of cv_qualifiers or -1 for a non-member
20495 function. */
20497 static tree
20498 cp_parser_late_return_type_opt (cp_parser* parser, cp_declarator *declarator,
20499 tree& requires_clause, cp_cv_quals quals)
20501 cp_token *token;
20502 tree type = NULL_TREE;
20503 bool declare_simd_p = (parser->omp_declare_simd
20504 && declarator
20505 && declarator->kind == cdk_id);
20507 bool cilk_simd_fn_vector_p = (parser->cilk_simd_fn_info
20508 && declarator && declarator->kind == cdk_id);
20510 bool oacc_routine_p = (parser->oacc_routine
20511 && declarator
20512 && declarator->kind == cdk_id);
20514 /* Peek at the next token. */
20515 token = cp_lexer_peek_token (parser->lexer);
20516 /* A late-specified return type is indicated by an initial '->'. */
20517 if (token->type != CPP_DEREF
20518 && token->keyword != RID_REQUIRES
20519 && !(token->type == CPP_NAME
20520 && token->u.value == ridpointers[RID_REQUIRES])
20521 && !(declare_simd_p || cilk_simd_fn_vector_p || oacc_routine_p))
20522 return NULL_TREE;
20524 tree save_ccp = current_class_ptr;
20525 tree save_ccr = current_class_ref;
20526 if (quals >= 0)
20528 /* DR 1207: 'this' is in scope in the trailing return type. */
20529 inject_this_parameter (current_class_type, quals);
20532 if (token->type == CPP_DEREF)
20534 /* Consume the ->. */
20535 cp_lexer_consume_token (parser->lexer);
20537 type = cp_parser_trailing_type_id (parser);
20540 /* Function declarations may be followed by a trailing
20541 requires-clause. */
20542 requires_clause = cp_parser_requires_clause_opt (parser);
20544 if (cilk_simd_fn_vector_p)
20545 declarator->attributes
20546 = cp_parser_late_parsing_cilk_simd_fn_info (parser,
20547 declarator->attributes);
20548 if (declare_simd_p)
20549 declarator->attributes
20550 = cp_parser_late_parsing_omp_declare_simd (parser,
20551 declarator->attributes);
20552 if (oacc_routine_p)
20553 declarator->attributes
20554 = cp_parser_late_parsing_oacc_routine (parser,
20555 declarator->attributes);
20557 if (quals >= 0)
20559 current_class_ptr = save_ccp;
20560 current_class_ref = save_ccr;
20563 return type;
20566 /* Parse a declarator-id.
20568 declarator-id:
20569 id-expression
20570 :: [opt] nested-name-specifier [opt] type-name
20572 In the `id-expression' case, the value returned is as for
20573 cp_parser_id_expression if the id-expression was an unqualified-id.
20574 If the id-expression was a qualified-id, then a SCOPE_REF is
20575 returned. The first operand is the scope (either a NAMESPACE_DECL
20576 or TREE_TYPE), but the second is still just a representation of an
20577 unqualified-id. */
20579 static tree
20580 cp_parser_declarator_id (cp_parser* parser, bool optional_p)
20582 tree id;
20583 /* The expression must be an id-expression. Assume that qualified
20584 names are the names of types so that:
20586 template <class T>
20587 int S<T>::R::i = 3;
20589 will work; we must treat `S<T>::R' as the name of a type.
20590 Similarly, assume that qualified names are templates, where
20591 required, so that:
20593 template <class T>
20594 int S<T>::R<T>::i = 3;
20596 will work, too. */
20597 id = cp_parser_id_expression (parser,
20598 /*template_keyword_p=*/false,
20599 /*check_dependency_p=*/false,
20600 /*template_p=*/NULL,
20601 /*declarator_p=*/true,
20602 optional_p);
20603 if (id && BASELINK_P (id))
20604 id = BASELINK_FUNCTIONS (id);
20605 return id;
20608 /* Parse a type-id.
20610 type-id:
20611 type-specifier-seq abstract-declarator [opt]
20613 Returns the TYPE specified. */
20615 static tree
20616 cp_parser_type_id_1 (cp_parser* parser, bool is_template_arg,
20617 bool is_trailing_return)
20619 cp_decl_specifier_seq type_specifier_seq;
20620 cp_declarator *abstract_declarator;
20622 /* Parse the type-specifier-seq. */
20623 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
20624 is_trailing_return,
20625 &type_specifier_seq);
20626 if (is_template_arg && type_specifier_seq.type
20627 && TREE_CODE (type_specifier_seq.type) == TEMPLATE_TYPE_PARM
20628 && CLASS_PLACEHOLDER_TEMPLATE (type_specifier_seq.type))
20629 /* A bare template name as a template argument is a template template
20630 argument, not a placeholder, so fail parsing it as a type argument. */
20632 gcc_assert (cp_parser_uncommitted_to_tentative_parse_p (parser));
20633 cp_parser_simulate_error (parser);
20634 return error_mark_node;
20636 if (type_specifier_seq.type == error_mark_node)
20637 return error_mark_node;
20639 /* There might or might not be an abstract declarator. */
20640 cp_parser_parse_tentatively (parser);
20641 /* Look for the declarator. */
20642 abstract_declarator
20643 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
20644 /*parenthesized_p=*/NULL,
20645 /*member_p=*/false,
20646 /*friend_p=*/false);
20647 /* Check to see if there really was a declarator. */
20648 if (!cp_parser_parse_definitely (parser))
20649 abstract_declarator = NULL;
20651 if (type_specifier_seq.type
20652 /* The concepts TS allows 'auto' as a type-id. */
20653 && (!flag_concepts || parser->in_type_id_in_expr_p)
20654 /* None of the valid uses of 'auto' in C++14 involve the type-id
20655 nonterminal, but it is valid in a trailing-return-type. */
20656 && !(cxx_dialect >= cxx14 && is_trailing_return))
20657 if (tree auto_node = type_uses_auto (type_specifier_seq.type))
20659 /* A type-id with type 'auto' is only ok if the abstract declarator
20660 is a function declarator with a late-specified return type.
20662 A type-id with 'auto' is also valid in a trailing-return-type
20663 in a compound-requirement. */
20664 if (abstract_declarator
20665 && abstract_declarator->kind == cdk_function
20666 && abstract_declarator->u.function.late_return_type)
20667 /* OK */;
20668 else if (parser->in_result_type_constraint_p)
20669 /* OK */;
20670 else
20672 location_t loc = type_specifier_seq.locations[ds_type_spec];
20673 if (tree tmpl = CLASS_PLACEHOLDER_TEMPLATE (auto_node))
20675 error_at (loc, "missing template arguments after %qT",
20676 auto_node);
20677 inform (DECL_SOURCE_LOCATION (tmpl), "%qD declared here",
20678 tmpl);
20680 else
20681 error_at (loc, "invalid use of %qT", auto_node);
20682 return error_mark_node;
20686 return groktypename (&type_specifier_seq, abstract_declarator,
20687 is_template_arg);
20690 static tree
20691 cp_parser_type_id (cp_parser *parser)
20693 return cp_parser_type_id_1 (parser, false, false);
20696 static tree
20697 cp_parser_template_type_arg (cp_parser *parser)
20699 tree r;
20700 const char *saved_message = parser->type_definition_forbidden_message;
20701 parser->type_definition_forbidden_message
20702 = G_("types may not be defined in template arguments");
20703 r = cp_parser_type_id_1 (parser, true, false);
20704 parser->type_definition_forbidden_message = saved_message;
20705 if (cxx_dialect >= cxx14 && !flag_concepts && type_uses_auto (r))
20707 error ("invalid use of %<auto%> in template argument");
20708 r = error_mark_node;
20710 return r;
20713 static tree
20714 cp_parser_trailing_type_id (cp_parser *parser)
20716 return cp_parser_type_id_1 (parser, false, true);
20719 /* Parse a type-specifier-seq.
20721 type-specifier-seq:
20722 type-specifier type-specifier-seq [opt]
20724 GNU extension:
20726 type-specifier-seq:
20727 attributes type-specifier-seq [opt]
20729 If IS_DECLARATION is true, we are at the start of a "condition" or
20730 exception-declaration, so we might be followed by a declarator-id.
20732 If IS_TRAILING_RETURN is true, we are in a trailing-return-type,
20733 i.e. we've just seen "->".
20735 Sets *TYPE_SPECIFIER_SEQ to represent the sequence. */
20737 static void
20738 cp_parser_type_specifier_seq (cp_parser* parser,
20739 bool is_declaration,
20740 bool is_trailing_return,
20741 cp_decl_specifier_seq *type_specifier_seq)
20743 bool seen_type_specifier = false;
20744 cp_parser_flags flags = CP_PARSER_FLAGS_OPTIONAL;
20745 cp_token *start_token = NULL;
20747 /* Clear the TYPE_SPECIFIER_SEQ. */
20748 clear_decl_specs (type_specifier_seq);
20750 /* In the context of a trailing return type, enum E { } is an
20751 elaborated-type-specifier followed by a function-body, not an
20752 enum-specifier. */
20753 if (is_trailing_return)
20754 flags |= CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS;
20756 /* Parse the type-specifiers and attributes. */
20757 while (true)
20759 tree type_specifier;
20760 bool is_cv_qualifier;
20762 /* Check for attributes first. */
20763 if (cp_next_tokens_can_be_attribute_p (parser))
20765 type_specifier_seq->attributes =
20766 chainon (type_specifier_seq->attributes,
20767 cp_parser_attributes_opt (parser));
20768 continue;
20771 /* record the token of the beginning of the type specifier seq,
20772 for error reporting purposes*/
20773 if (!start_token)
20774 start_token = cp_lexer_peek_token (parser->lexer);
20776 /* Look for the type-specifier. */
20777 type_specifier = cp_parser_type_specifier (parser,
20778 flags,
20779 type_specifier_seq,
20780 /*is_declaration=*/false,
20781 NULL,
20782 &is_cv_qualifier);
20783 if (!type_specifier)
20785 /* If the first type-specifier could not be found, this is not a
20786 type-specifier-seq at all. */
20787 if (!seen_type_specifier)
20789 /* Set in_declarator_p to avoid skipping to the semicolon. */
20790 int in_decl = parser->in_declarator_p;
20791 parser->in_declarator_p = true;
20793 if (cp_parser_uncommitted_to_tentative_parse_p (parser)
20794 || !cp_parser_parse_and_diagnose_invalid_type_name (parser))
20795 cp_parser_error (parser, "expected type-specifier");
20797 parser->in_declarator_p = in_decl;
20799 type_specifier_seq->type = error_mark_node;
20800 return;
20802 /* If subsequent type-specifiers could not be found, the
20803 type-specifier-seq is complete. */
20804 break;
20807 seen_type_specifier = true;
20808 /* The standard says that a condition can be:
20810 type-specifier-seq declarator = assignment-expression
20812 However, given:
20814 struct S {};
20815 if (int S = ...)
20817 we should treat the "S" as a declarator, not as a
20818 type-specifier. The standard doesn't say that explicitly for
20819 type-specifier-seq, but it does say that for
20820 decl-specifier-seq in an ordinary declaration. Perhaps it
20821 would be clearer just to allow a decl-specifier-seq here, and
20822 then add a semantic restriction that if any decl-specifiers
20823 that are not type-specifiers appear, the program is invalid. */
20824 if (is_declaration && !is_cv_qualifier)
20825 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
20829 /* Return whether the function currently being declared has an associated
20830 template parameter list. */
20832 static bool
20833 function_being_declared_is_template_p (cp_parser* parser)
20835 if (!current_template_parms || processing_template_parmlist)
20836 return false;
20838 if (parser->implicit_template_scope)
20839 return true;
20841 if (at_class_scope_p ()
20842 && TYPE_BEING_DEFINED (current_class_type))
20843 return parser->num_template_parameter_lists != 0;
20845 return ((int) parser->num_template_parameter_lists > template_class_depth
20846 (current_class_type));
20849 /* Parse a parameter-declaration-clause.
20851 parameter-declaration-clause:
20852 parameter-declaration-list [opt] ... [opt]
20853 parameter-declaration-list , ...
20855 Returns a representation for the parameter declarations. A return
20856 value of NULL indicates a parameter-declaration-clause consisting
20857 only of an ellipsis. */
20859 static tree
20860 cp_parser_parameter_declaration_clause (cp_parser* parser)
20862 tree parameters;
20863 cp_token *token;
20864 bool ellipsis_p;
20865 bool is_error;
20867 struct cleanup {
20868 cp_parser* parser;
20869 int auto_is_implicit_function_template_parm_p;
20870 ~cleanup() {
20871 parser->auto_is_implicit_function_template_parm_p
20872 = auto_is_implicit_function_template_parm_p;
20874 } cleanup = { parser, parser->auto_is_implicit_function_template_parm_p };
20876 (void) cleanup;
20878 if (!processing_specialization
20879 && !processing_template_parmlist
20880 && !processing_explicit_instantiation)
20881 if (!current_function_decl
20882 || (current_class_type && LAMBDA_TYPE_P (current_class_type)))
20883 parser->auto_is_implicit_function_template_parm_p = true;
20885 /* Peek at the next token. */
20886 token = cp_lexer_peek_token (parser->lexer);
20887 /* Check for trivial parameter-declaration-clauses. */
20888 if (token->type == CPP_ELLIPSIS)
20890 /* Consume the `...' token. */
20891 cp_lexer_consume_token (parser->lexer);
20892 return NULL_TREE;
20894 else if (token->type == CPP_CLOSE_PAREN)
20895 /* There are no parameters. */
20897 #ifndef NO_IMPLICIT_EXTERN_C
20898 if (in_system_header_at (input_location)
20899 && current_class_type == NULL
20900 && current_lang_name == lang_name_c)
20901 return NULL_TREE;
20902 else
20903 #endif
20904 return void_list_node;
20906 /* Check for `(void)', too, which is a special case. */
20907 else if (token->keyword == RID_VOID
20908 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
20909 == CPP_CLOSE_PAREN))
20911 /* Consume the `void' token. */
20912 cp_lexer_consume_token (parser->lexer);
20913 /* There are no parameters. */
20914 return void_list_node;
20917 /* Parse the parameter-declaration-list. */
20918 parameters = cp_parser_parameter_declaration_list (parser, &is_error);
20919 /* If a parse error occurred while parsing the
20920 parameter-declaration-list, then the entire
20921 parameter-declaration-clause is erroneous. */
20922 if (is_error)
20923 return NULL;
20925 /* Peek at the next token. */
20926 token = cp_lexer_peek_token (parser->lexer);
20927 /* If it's a `,', the clause should terminate with an ellipsis. */
20928 if (token->type == CPP_COMMA)
20930 /* Consume the `,'. */
20931 cp_lexer_consume_token (parser->lexer);
20932 /* Expect an ellipsis. */
20933 ellipsis_p
20934 = (cp_parser_require (parser, CPP_ELLIPSIS, RT_ELLIPSIS) != NULL);
20936 /* It might also be `...' if the optional trailing `,' was
20937 omitted. */
20938 else if (token->type == CPP_ELLIPSIS)
20940 /* Consume the `...' token. */
20941 cp_lexer_consume_token (parser->lexer);
20942 /* And remember that we saw it. */
20943 ellipsis_p = true;
20945 else
20946 ellipsis_p = false;
20948 /* Finish the parameter list. */
20949 if (!ellipsis_p)
20950 parameters = chainon (parameters, void_list_node);
20952 return parameters;
20955 /* Parse a parameter-declaration-list.
20957 parameter-declaration-list:
20958 parameter-declaration
20959 parameter-declaration-list , parameter-declaration
20961 Returns a representation of the parameter-declaration-list, as for
20962 cp_parser_parameter_declaration_clause. However, the
20963 `void_list_node' is never appended to the list. Upon return,
20964 *IS_ERROR will be true iff an error occurred. */
20966 static tree
20967 cp_parser_parameter_declaration_list (cp_parser* parser, bool *is_error)
20969 tree parameters = NULL_TREE;
20970 tree *tail = &parameters;
20971 bool saved_in_unbraced_linkage_specification_p;
20972 int index = 0;
20974 /* Assume all will go well. */
20975 *is_error = false;
20976 /* The special considerations that apply to a function within an
20977 unbraced linkage specifications do not apply to the parameters
20978 to the function. */
20979 saved_in_unbraced_linkage_specification_p
20980 = parser->in_unbraced_linkage_specification_p;
20981 parser->in_unbraced_linkage_specification_p = false;
20983 /* Look for more parameters. */
20984 while (true)
20986 cp_parameter_declarator *parameter;
20987 tree decl = error_mark_node;
20988 bool parenthesized_p = false;
20989 int template_parm_idx = (function_being_declared_is_template_p (parser)?
20990 TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS
20991 (current_template_parms)) : 0);
20993 /* Parse the parameter. */
20994 parameter
20995 = cp_parser_parameter_declaration (parser,
20996 /*template_parm_p=*/false,
20997 &parenthesized_p);
20999 /* We don't know yet if the enclosing context is deprecated, so wait
21000 and warn in grokparms if appropriate. */
21001 deprecated_state = DEPRECATED_SUPPRESS;
21003 if (parameter)
21005 /* If a function parameter pack was specified and an implicit template
21006 parameter was introduced during cp_parser_parameter_declaration,
21007 change any implicit parameters introduced into packs. */
21008 if (parser->implicit_template_parms
21009 && parameter->declarator
21010 && parameter->declarator->parameter_pack_p)
21012 int latest_template_parm_idx = TREE_VEC_LENGTH
21013 (INNERMOST_TEMPLATE_PARMS (current_template_parms));
21015 if (latest_template_parm_idx != template_parm_idx)
21016 parameter->decl_specifiers.type = convert_generic_types_to_packs
21017 (parameter->decl_specifiers.type,
21018 template_parm_idx, latest_template_parm_idx);
21021 decl = grokdeclarator (parameter->declarator,
21022 &parameter->decl_specifiers,
21023 PARM,
21024 parameter->default_argument != NULL_TREE,
21025 &parameter->decl_specifiers.attributes);
21028 deprecated_state = DEPRECATED_NORMAL;
21030 /* If a parse error occurred parsing the parameter declaration,
21031 then the entire parameter-declaration-list is erroneous. */
21032 if (decl == error_mark_node)
21034 *is_error = true;
21035 parameters = error_mark_node;
21036 break;
21039 if (parameter->decl_specifiers.attributes)
21040 cplus_decl_attributes (&decl,
21041 parameter->decl_specifiers.attributes,
21043 if (DECL_NAME (decl))
21044 decl = pushdecl (decl);
21046 if (decl != error_mark_node)
21048 retrofit_lang_decl (decl);
21049 DECL_PARM_INDEX (decl) = ++index;
21050 DECL_PARM_LEVEL (decl) = function_parm_depth ();
21053 /* Add the new parameter to the list. */
21054 *tail = build_tree_list (parameter->default_argument, decl);
21055 tail = &TREE_CHAIN (*tail);
21057 /* Peek at the next token. */
21058 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
21059 || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
21060 /* These are for Objective-C++ */
21061 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
21062 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
21063 /* The parameter-declaration-list is complete. */
21064 break;
21065 else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
21067 cp_token *token;
21069 /* Peek at the next token. */
21070 token = cp_lexer_peek_nth_token (parser->lexer, 2);
21071 /* If it's an ellipsis, then the list is complete. */
21072 if (token->type == CPP_ELLIPSIS)
21073 break;
21074 /* Otherwise, there must be more parameters. Consume the
21075 `,'. */
21076 cp_lexer_consume_token (parser->lexer);
21077 /* When parsing something like:
21079 int i(float f, double d)
21081 we can tell after seeing the declaration for "f" that we
21082 are not looking at an initialization of a variable "i",
21083 but rather at the declaration of a function "i".
21085 Due to the fact that the parsing of template arguments
21086 (as specified to a template-id) requires backtracking we
21087 cannot use this technique when inside a template argument
21088 list. */
21089 if (!parser->in_template_argument_list_p
21090 && !parser->in_type_id_in_expr_p
21091 && cp_parser_uncommitted_to_tentative_parse_p (parser)
21092 /* However, a parameter-declaration of the form
21093 "float(f)" (which is a valid declaration of a
21094 parameter "f") can also be interpreted as an
21095 expression (the conversion of "f" to "float"). */
21096 && !parenthesized_p)
21097 cp_parser_commit_to_tentative_parse (parser);
21099 else
21101 cp_parser_error (parser, "expected %<,%> or %<...%>");
21102 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
21103 cp_parser_skip_to_closing_parenthesis (parser,
21104 /*recovering=*/true,
21105 /*or_comma=*/false,
21106 /*consume_paren=*/false);
21107 break;
21111 parser->in_unbraced_linkage_specification_p
21112 = saved_in_unbraced_linkage_specification_p;
21114 /* Reset implicit_template_scope if we are about to leave the function
21115 parameter list that introduced it. Note that for out-of-line member
21116 definitions, there will be one or more class scopes before we get to
21117 the template parameter scope. */
21119 if (cp_binding_level *its = parser->implicit_template_scope)
21120 if (cp_binding_level *maybe_its = current_binding_level->level_chain)
21122 while (maybe_its->kind == sk_class)
21123 maybe_its = maybe_its->level_chain;
21124 if (maybe_its == its)
21126 parser->implicit_template_parms = 0;
21127 parser->implicit_template_scope = 0;
21131 return parameters;
21134 /* Parse a parameter declaration.
21136 parameter-declaration:
21137 decl-specifier-seq ... [opt] declarator
21138 decl-specifier-seq declarator = assignment-expression
21139 decl-specifier-seq ... [opt] abstract-declarator [opt]
21140 decl-specifier-seq abstract-declarator [opt] = assignment-expression
21142 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
21143 declares a template parameter. (In that case, a non-nested `>'
21144 token encountered during the parsing of the assignment-expression
21145 is not interpreted as a greater-than operator.)
21147 Returns a representation of the parameter, or NULL if an error
21148 occurs. If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
21149 true iff the declarator is of the form "(p)". */
21151 static cp_parameter_declarator *
21152 cp_parser_parameter_declaration (cp_parser *parser,
21153 bool template_parm_p,
21154 bool *parenthesized_p)
21156 int declares_class_or_enum;
21157 cp_decl_specifier_seq decl_specifiers;
21158 cp_declarator *declarator;
21159 tree default_argument;
21160 cp_token *token = NULL, *declarator_token_start = NULL;
21161 const char *saved_message;
21162 bool template_parameter_pack_p = false;
21164 /* In a template parameter, `>' is not an operator.
21166 [temp.param]
21168 When parsing a default template-argument for a non-type
21169 template-parameter, the first non-nested `>' is taken as the end
21170 of the template parameter-list rather than a greater-than
21171 operator. */
21173 /* Type definitions may not appear in parameter types. */
21174 saved_message = parser->type_definition_forbidden_message;
21175 parser->type_definition_forbidden_message
21176 = G_("types may not be defined in parameter types");
21178 /* Parse the declaration-specifiers. */
21179 cp_parser_decl_specifier_seq (parser,
21180 CP_PARSER_FLAGS_NONE,
21181 &decl_specifiers,
21182 &declares_class_or_enum);
21184 /* Complain about missing 'typename' or other invalid type names. */
21185 if (!decl_specifiers.any_type_specifiers_p
21186 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
21187 decl_specifiers.type = error_mark_node;
21189 /* If an error occurred, there's no reason to attempt to parse the
21190 rest of the declaration. */
21191 if (cp_parser_error_occurred (parser))
21193 parser->type_definition_forbidden_message = saved_message;
21194 return NULL;
21197 /* Peek at the next token. */
21198 token = cp_lexer_peek_token (parser->lexer);
21200 /* If the next token is a `)', `,', `=', `>', or `...', then there
21201 is no declarator. However, when variadic templates are enabled,
21202 there may be a declarator following `...'. */
21203 if (token->type == CPP_CLOSE_PAREN
21204 || token->type == CPP_COMMA
21205 || token->type == CPP_EQ
21206 || token->type == CPP_GREATER)
21208 declarator = NULL;
21209 if (parenthesized_p)
21210 *parenthesized_p = false;
21212 /* Otherwise, there should be a declarator. */
21213 else
21215 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
21216 parser->default_arg_ok_p = false;
21218 /* After seeing a decl-specifier-seq, if the next token is not a
21219 "(", there is no possibility that the code is a valid
21220 expression. Therefore, if parsing tentatively, we commit at
21221 this point. */
21222 if (!parser->in_template_argument_list_p
21223 /* In an expression context, having seen:
21225 (int((char ...
21227 we cannot be sure whether we are looking at a
21228 function-type (taking a "char" as a parameter) or a cast
21229 of some object of type "char" to "int". */
21230 && !parser->in_type_id_in_expr_p
21231 && cp_parser_uncommitted_to_tentative_parse_p (parser)
21232 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
21233 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
21234 cp_parser_commit_to_tentative_parse (parser);
21235 /* Parse the declarator. */
21236 declarator_token_start = token;
21237 declarator = cp_parser_declarator (parser,
21238 CP_PARSER_DECLARATOR_EITHER,
21239 /*ctor_dtor_or_conv_p=*/NULL,
21240 parenthesized_p,
21241 /*member_p=*/false,
21242 /*friend_p=*/false);
21243 parser->default_arg_ok_p = saved_default_arg_ok_p;
21244 /* After the declarator, allow more attributes. */
21245 decl_specifiers.attributes
21246 = chainon (decl_specifiers.attributes,
21247 cp_parser_attributes_opt (parser));
21249 /* If the declarator is a template parameter pack, remember that and
21250 clear the flag in the declarator itself so we don't get errors
21251 from grokdeclarator. */
21252 if (template_parm_p && declarator && declarator->parameter_pack_p)
21254 declarator->parameter_pack_p = false;
21255 template_parameter_pack_p = true;
21259 /* If the next token is an ellipsis, and we have not seen a declarator
21260 name, and if either the type of the declarator contains parameter
21261 packs but it is not a TYPE_PACK_EXPANSION or is null (this happens
21262 for, eg, abbreviated integral type names), then we actually have a
21263 parameter pack expansion expression. Otherwise, leave the ellipsis
21264 for a C-style variadic function. */
21265 token = cp_lexer_peek_token (parser->lexer);
21266 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
21268 tree type = decl_specifiers.type;
21270 if (type && DECL_P (type))
21271 type = TREE_TYPE (type);
21273 if (((type
21274 && TREE_CODE (type) != TYPE_PACK_EXPANSION
21275 && (template_parm_p || uses_parameter_packs (type)))
21276 || (!type && template_parm_p))
21277 && declarator_can_be_parameter_pack (declarator))
21279 /* Consume the `...'. */
21280 cp_lexer_consume_token (parser->lexer);
21281 maybe_warn_variadic_templates ();
21283 /* Build a pack expansion type */
21284 if (template_parm_p)
21285 template_parameter_pack_p = true;
21286 else if (declarator)
21287 declarator->parameter_pack_p = true;
21288 else
21289 decl_specifiers.type = make_pack_expansion (type);
21293 /* The restriction on defining new types applies only to the type
21294 of the parameter, not to the default argument. */
21295 parser->type_definition_forbidden_message = saved_message;
21297 /* If the next token is `=', then process a default argument. */
21298 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
21300 tree type = decl_specifiers.type;
21301 token = cp_lexer_peek_token (parser->lexer);
21302 /* If we are defining a class, then the tokens that make up the
21303 default argument must be saved and processed later. */
21304 if (!template_parm_p && at_class_scope_p ()
21305 && TYPE_BEING_DEFINED (current_class_type)
21306 && !LAMBDA_TYPE_P (current_class_type))
21307 default_argument = cp_parser_cache_defarg (parser, /*nsdmi=*/false);
21309 // A constrained-type-specifier may declare a type template-parameter.
21310 else if (declares_constrained_type_template_parameter (type))
21311 default_argument
21312 = cp_parser_default_type_template_argument (parser);
21314 // A constrained-type-specifier may declare a template-template-parameter.
21315 else if (declares_constrained_template_template_parameter (type))
21316 default_argument
21317 = cp_parser_default_template_template_argument (parser);
21319 /* Outside of a class definition, we can just parse the
21320 assignment-expression. */
21321 else
21322 default_argument
21323 = cp_parser_default_argument (parser, template_parm_p);
21325 if (!parser->default_arg_ok_p)
21327 permerror (token->location,
21328 "default arguments are only "
21329 "permitted for function parameters");
21331 else if ((declarator && declarator->parameter_pack_p)
21332 || template_parameter_pack_p
21333 || (decl_specifiers.type
21334 && PACK_EXPANSION_P (decl_specifiers.type)))
21336 /* Find the name of the parameter pack. */
21337 cp_declarator *id_declarator = declarator;
21338 while (id_declarator && id_declarator->kind != cdk_id)
21339 id_declarator = id_declarator->declarator;
21341 if (id_declarator && id_declarator->kind == cdk_id)
21342 error_at (declarator_token_start->location,
21343 template_parm_p
21344 ? G_("template parameter pack %qD "
21345 "cannot have a default argument")
21346 : G_("parameter pack %qD cannot have "
21347 "a default argument"),
21348 id_declarator->u.id.unqualified_name);
21349 else
21350 error_at (declarator_token_start->location,
21351 template_parm_p
21352 ? G_("template parameter pack cannot have "
21353 "a default argument")
21354 : G_("parameter pack cannot have a "
21355 "default argument"));
21357 default_argument = NULL_TREE;
21360 else
21361 default_argument = NULL_TREE;
21363 return make_parameter_declarator (&decl_specifiers,
21364 declarator,
21365 default_argument,
21366 template_parameter_pack_p);
21369 /* Parse a default argument and return it.
21371 TEMPLATE_PARM_P is true if this is a default argument for a
21372 non-type template parameter. */
21373 static tree
21374 cp_parser_default_argument (cp_parser *parser, bool template_parm_p)
21376 tree default_argument = NULL_TREE;
21377 bool saved_greater_than_is_operator_p;
21378 bool saved_local_variables_forbidden_p;
21379 bool non_constant_p, is_direct_init;
21381 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
21382 set correctly. */
21383 saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
21384 parser->greater_than_is_operator_p = !template_parm_p;
21385 /* Local variable names (and the `this' keyword) may not
21386 appear in a default argument. */
21387 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
21388 parser->local_variables_forbidden_p = true;
21389 /* Parse the assignment-expression. */
21390 if (template_parm_p)
21391 push_deferring_access_checks (dk_no_deferred);
21392 tree saved_class_ptr = NULL_TREE;
21393 tree saved_class_ref = NULL_TREE;
21394 /* The "this" pointer is not valid in a default argument. */
21395 if (cfun)
21397 saved_class_ptr = current_class_ptr;
21398 cp_function_chain->x_current_class_ptr = NULL_TREE;
21399 saved_class_ref = current_class_ref;
21400 cp_function_chain->x_current_class_ref = NULL_TREE;
21402 default_argument
21403 = cp_parser_initializer (parser, &is_direct_init, &non_constant_p);
21404 /* Restore the "this" pointer. */
21405 if (cfun)
21407 cp_function_chain->x_current_class_ptr = saved_class_ptr;
21408 cp_function_chain->x_current_class_ref = saved_class_ref;
21410 if (BRACE_ENCLOSED_INITIALIZER_P (default_argument))
21411 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
21412 if (template_parm_p)
21413 pop_deferring_access_checks ();
21414 parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
21415 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
21417 return default_argument;
21420 /* Parse a function-body.
21422 function-body:
21423 compound_statement */
21425 static void
21426 cp_parser_function_body (cp_parser *parser, bool in_function_try_block)
21428 cp_parser_compound_statement (parser, NULL, (in_function_try_block
21429 ? BCS_TRY_BLOCK : BCS_NORMAL),
21430 true);
21433 /* Parse a ctor-initializer-opt followed by a function-body. Return
21434 true if a ctor-initializer was present. When IN_FUNCTION_TRY_BLOCK
21435 is true we are parsing a function-try-block. */
21437 static bool
21438 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser,
21439 bool in_function_try_block)
21441 tree body, list;
21442 bool ctor_initializer_p;
21443 const bool check_body_p =
21444 DECL_CONSTRUCTOR_P (current_function_decl)
21445 && DECL_DECLARED_CONSTEXPR_P (current_function_decl);
21446 tree last = NULL;
21448 /* Begin the function body. */
21449 body = begin_function_body ();
21450 /* Parse the optional ctor-initializer. */
21451 ctor_initializer_p = cp_parser_ctor_initializer_opt (parser);
21453 /* If we're parsing a constexpr constructor definition, we need
21454 to check that the constructor body is indeed empty. However,
21455 before we get to cp_parser_function_body lot of junk has been
21456 generated, so we can't just check that we have an empty block.
21457 Rather we take a snapshot of the outermost block, and check whether
21458 cp_parser_function_body changed its state. */
21459 if (check_body_p)
21461 list = cur_stmt_list;
21462 if (STATEMENT_LIST_TAIL (list))
21463 last = STATEMENT_LIST_TAIL (list)->stmt;
21465 /* Parse the function-body. */
21466 cp_parser_function_body (parser, in_function_try_block);
21467 if (check_body_p)
21468 check_constexpr_ctor_body (last, list, /*complain=*/true);
21469 /* Finish the function body. */
21470 finish_function_body (body);
21472 return ctor_initializer_p;
21475 /* Parse an initializer.
21477 initializer:
21478 = initializer-clause
21479 ( expression-list )
21481 Returns an expression representing the initializer. If no
21482 initializer is present, NULL_TREE is returned.
21484 *IS_DIRECT_INIT is set to FALSE if the `= initializer-clause'
21485 production is used, and TRUE otherwise. *IS_DIRECT_INIT is
21486 set to TRUE if there is no initializer present. If there is an
21487 initializer, and it is not a constant-expression, *NON_CONSTANT_P
21488 is set to true; otherwise it is set to false. */
21490 static tree
21491 cp_parser_initializer (cp_parser* parser, bool* is_direct_init,
21492 bool* non_constant_p)
21494 cp_token *token;
21495 tree init;
21497 /* Peek at the next token. */
21498 token = cp_lexer_peek_token (parser->lexer);
21500 /* Let our caller know whether or not this initializer was
21501 parenthesized. */
21502 *is_direct_init = (token->type != CPP_EQ);
21503 /* Assume that the initializer is constant. */
21504 *non_constant_p = false;
21506 if (token->type == CPP_EQ)
21508 /* Consume the `='. */
21509 cp_lexer_consume_token (parser->lexer);
21510 /* Parse the initializer-clause. */
21511 init = cp_parser_initializer_clause (parser, non_constant_p);
21513 else if (token->type == CPP_OPEN_PAREN)
21515 vec<tree, va_gc> *vec;
21516 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
21517 /*cast_p=*/false,
21518 /*allow_expansion_p=*/true,
21519 non_constant_p);
21520 if (vec == NULL)
21521 return error_mark_node;
21522 init = build_tree_list_vec (vec);
21523 release_tree_vector (vec);
21525 else if (token->type == CPP_OPEN_BRACE)
21527 cp_lexer_set_source_position (parser->lexer);
21528 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
21529 init = cp_parser_braced_list (parser, non_constant_p);
21530 CONSTRUCTOR_IS_DIRECT_INIT (init) = 1;
21532 else
21534 /* Anything else is an error. */
21535 cp_parser_error (parser, "expected initializer");
21536 init = error_mark_node;
21539 if (check_for_bare_parameter_packs (init))
21540 init = error_mark_node;
21542 return init;
21545 /* Parse an initializer-clause.
21547 initializer-clause:
21548 assignment-expression
21549 braced-init-list
21551 Returns an expression representing the initializer.
21553 If the `assignment-expression' production is used the value
21554 returned is simply a representation for the expression.
21556 Otherwise, calls cp_parser_braced_list. */
21558 static cp_expr
21559 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
21561 cp_expr initializer;
21563 /* Assume the expression is constant. */
21564 *non_constant_p = false;
21566 /* If it is not a `{', then we are looking at an
21567 assignment-expression. */
21568 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
21570 initializer
21571 = cp_parser_constant_expression (parser,
21572 /*allow_non_constant_p=*/true,
21573 non_constant_p);
21575 else
21576 initializer = cp_parser_braced_list (parser, non_constant_p);
21578 return initializer;
21581 /* Parse a brace-enclosed initializer list.
21583 braced-init-list:
21584 { initializer-list , [opt] }
21587 Returns a CONSTRUCTOR. The CONSTRUCTOR_ELTS will be
21588 the elements of the initializer-list (or NULL, if the last
21589 production is used). The TREE_TYPE for the CONSTRUCTOR will be
21590 NULL_TREE. There is no way to detect whether or not the optional
21591 trailing `,' was provided. NON_CONSTANT_P is as for
21592 cp_parser_initializer. */
21594 static cp_expr
21595 cp_parser_braced_list (cp_parser* parser, bool* non_constant_p)
21597 tree initializer;
21598 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
21600 /* Consume the `{' token. */
21601 cp_lexer_consume_token (parser->lexer);
21602 /* Create a CONSTRUCTOR to represent the braced-initializer. */
21603 initializer = make_node (CONSTRUCTOR);
21604 /* If it's not a `}', then there is a non-trivial initializer. */
21605 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
21607 /* Parse the initializer list. */
21608 CONSTRUCTOR_ELTS (initializer)
21609 = cp_parser_initializer_list (parser, non_constant_p);
21610 /* A trailing `,' token is allowed. */
21611 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
21612 cp_lexer_consume_token (parser->lexer);
21614 else
21615 *non_constant_p = false;
21616 /* Now, there should be a trailing `}'. */
21617 location_t finish_loc = cp_lexer_peek_token (parser->lexer)->location;
21618 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
21619 TREE_TYPE (initializer) = init_list_type_node;
21621 cp_expr result (initializer);
21622 /* Build a location of the form:
21623 { ... }
21624 ^~~~~~~
21625 with caret==start at the open brace, finish at the close brace. */
21626 location_t combined_loc = make_location (start_loc, start_loc, finish_loc);
21627 result.set_location (combined_loc);
21628 return result;
21631 /* Consume tokens up to, and including, the next non-nested closing `]'.
21632 Returns true iff we found a closing `]'. */
21634 static bool
21635 cp_parser_skip_to_closing_square_bracket (cp_parser *parser)
21637 unsigned square_depth = 0;
21639 while (true)
21641 cp_token * token = cp_lexer_peek_token (parser->lexer);
21643 switch (token->type)
21645 case CPP_EOF:
21646 case CPP_PRAGMA_EOL:
21647 /* If we've run out of tokens, then there is no closing `]'. */
21648 return false;
21650 case CPP_OPEN_SQUARE:
21651 ++square_depth;
21652 break;
21654 case CPP_CLOSE_SQUARE:
21655 if (!square_depth--)
21657 cp_lexer_consume_token (parser->lexer);
21658 return true;
21660 break;
21662 default:
21663 break;
21666 /* Consume the token. */
21667 cp_lexer_consume_token (parser->lexer);
21671 /* Return true if we are looking at an array-designator, false otherwise. */
21673 static bool
21674 cp_parser_array_designator_p (cp_parser *parser)
21676 /* Consume the `['. */
21677 cp_lexer_consume_token (parser->lexer);
21679 cp_lexer_save_tokens (parser->lexer);
21681 /* Skip tokens until the next token is a closing square bracket.
21682 If we find the closing `]', and the next token is a `=', then
21683 we are looking at an array designator. */
21684 bool array_designator_p
21685 = (cp_parser_skip_to_closing_square_bracket (parser)
21686 && cp_lexer_next_token_is (parser->lexer, CPP_EQ));
21688 /* Roll back the tokens we skipped. */
21689 cp_lexer_rollback_tokens (parser->lexer);
21691 return array_designator_p;
21694 /* Parse an initializer-list.
21696 initializer-list:
21697 initializer-clause ... [opt]
21698 initializer-list , initializer-clause ... [opt]
21700 GNU Extension:
21702 initializer-list:
21703 designation initializer-clause ...[opt]
21704 initializer-list , designation initializer-clause ...[opt]
21706 designation:
21707 . identifier =
21708 identifier :
21709 [ constant-expression ] =
21711 Returns a vec of constructor_elt. The VALUE of each elt is an expression
21712 for the initializer. If the INDEX of the elt is non-NULL, it is the
21713 IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is
21714 as for cp_parser_initializer. */
21716 static vec<constructor_elt, va_gc> *
21717 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
21719 vec<constructor_elt, va_gc> *v = NULL;
21721 /* Assume all of the expressions are constant. */
21722 *non_constant_p = false;
21724 /* Parse the rest of the list. */
21725 while (true)
21727 cp_token *token;
21728 tree designator;
21729 tree initializer;
21730 bool clause_non_constant_p;
21732 /* If the next token is an identifier and the following one is a
21733 colon, we are looking at the GNU designated-initializer
21734 syntax. */
21735 if (cp_parser_allow_gnu_extensions_p (parser)
21736 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
21737 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
21739 /* Warn the user that they are using an extension. */
21740 pedwarn (input_location, OPT_Wpedantic,
21741 "ISO C++ does not allow designated initializers");
21742 /* Consume the identifier. */
21743 designator = cp_lexer_consume_token (parser->lexer)->u.value;
21744 /* Consume the `:'. */
21745 cp_lexer_consume_token (parser->lexer);
21747 /* Also handle the C99 syntax, '. id ='. */
21748 else if (cp_parser_allow_gnu_extensions_p (parser)
21749 && cp_lexer_next_token_is (parser->lexer, CPP_DOT)
21750 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME
21751 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_EQ)
21753 /* Warn the user that they are using an extension. */
21754 pedwarn (input_location, OPT_Wpedantic,
21755 "ISO C++ does not allow C99 designated initializers");
21756 /* Consume the `.'. */
21757 cp_lexer_consume_token (parser->lexer);
21758 /* Consume the identifier. */
21759 designator = cp_lexer_consume_token (parser->lexer)->u.value;
21760 /* Consume the `='. */
21761 cp_lexer_consume_token (parser->lexer);
21763 /* Also handle C99 array designators, '[ const ] ='. */
21764 else if (cp_parser_allow_gnu_extensions_p (parser)
21765 && !c_dialect_objc ()
21766 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
21768 /* In C++11, [ could start a lambda-introducer. */
21769 bool non_const = false;
21771 cp_parser_parse_tentatively (parser);
21773 if (!cp_parser_array_designator_p (parser))
21775 cp_parser_simulate_error (parser);
21776 designator = NULL_TREE;
21778 else
21780 designator = cp_parser_constant_expression (parser, true,
21781 &non_const);
21782 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
21783 cp_parser_require (parser, CPP_EQ, RT_EQ);
21786 if (!cp_parser_parse_definitely (parser))
21787 designator = NULL_TREE;
21788 else if (non_const)
21789 require_potential_rvalue_constant_expression (designator);
21791 else
21792 designator = NULL_TREE;
21794 /* Parse the initializer. */
21795 initializer = cp_parser_initializer_clause (parser,
21796 &clause_non_constant_p);
21797 /* If any clause is non-constant, so is the entire initializer. */
21798 if (clause_non_constant_p)
21799 *non_constant_p = true;
21801 /* If we have an ellipsis, this is an initializer pack
21802 expansion. */
21803 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
21805 /* Consume the `...'. */
21806 cp_lexer_consume_token (parser->lexer);
21808 /* Turn the initializer into an initializer expansion. */
21809 initializer = make_pack_expansion (initializer);
21812 /* Add it to the vector. */
21813 CONSTRUCTOR_APPEND_ELT (v, designator, initializer);
21815 /* If the next token is not a comma, we have reached the end of
21816 the list. */
21817 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
21818 break;
21820 /* Peek at the next token. */
21821 token = cp_lexer_peek_nth_token (parser->lexer, 2);
21822 /* If the next token is a `}', then we're still done. An
21823 initializer-clause can have a trailing `,' after the
21824 initializer-list and before the closing `}'. */
21825 if (token->type == CPP_CLOSE_BRACE)
21826 break;
21828 /* Consume the `,' token. */
21829 cp_lexer_consume_token (parser->lexer);
21832 return v;
21835 /* Classes [gram.class] */
21837 /* Parse a class-name.
21839 class-name:
21840 identifier
21841 template-id
21843 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
21844 to indicate that names looked up in dependent types should be
21845 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
21846 keyword has been used to indicate that the name that appears next
21847 is a template. TAG_TYPE indicates the explicit tag given before
21848 the type name, if any. If CHECK_DEPENDENCY_P is FALSE, names are
21849 looked up in dependent scopes. If CLASS_HEAD_P is TRUE, this class
21850 is the class being defined in a class-head. If ENUM_OK is TRUE,
21851 enum-names are also accepted.
21853 Returns the TYPE_DECL representing the class. */
21855 static tree
21856 cp_parser_class_name (cp_parser *parser,
21857 bool typename_keyword_p,
21858 bool template_keyword_p,
21859 enum tag_types tag_type,
21860 bool check_dependency_p,
21861 bool class_head_p,
21862 bool is_declaration,
21863 bool enum_ok)
21865 tree decl;
21866 tree scope;
21867 bool typename_p;
21868 cp_token *token;
21869 tree identifier = NULL_TREE;
21871 /* All class-names start with an identifier. */
21872 token = cp_lexer_peek_token (parser->lexer);
21873 if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
21875 cp_parser_error (parser, "expected class-name");
21876 return error_mark_node;
21879 /* PARSER->SCOPE can be cleared when parsing the template-arguments
21880 to a template-id, so we save it here. */
21881 scope = parser->scope;
21882 if (scope == error_mark_node)
21883 return error_mark_node;
21885 /* Any name names a type if we're following the `typename' keyword
21886 in a qualified name where the enclosing scope is type-dependent. */
21887 typename_p = (typename_keyword_p && scope && TYPE_P (scope)
21888 && dependent_type_p (scope));
21889 /* Handle the common case (an identifier, but not a template-id)
21890 efficiently. */
21891 if (token->type == CPP_NAME
21892 && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
21894 cp_token *identifier_token;
21895 bool ambiguous_p;
21897 /* Look for the identifier. */
21898 identifier_token = cp_lexer_peek_token (parser->lexer);
21899 ambiguous_p = identifier_token->error_reported;
21900 identifier = cp_parser_identifier (parser);
21901 /* If the next token isn't an identifier, we are certainly not
21902 looking at a class-name. */
21903 if (identifier == error_mark_node)
21904 decl = error_mark_node;
21905 /* If we know this is a type-name, there's no need to look it
21906 up. */
21907 else if (typename_p)
21908 decl = identifier;
21909 else
21911 tree ambiguous_decls;
21912 /* If we already know that this lookup is ambiguous, then
21913 we've already issued an error message; there's no reason
21914 to check again. */
21915 if (ambiguous_p)
21917 cp_parser_simulate_error (parser);
21918 return error_mark_node;
21920 /* If the next token is a `::', then the name must be a type
21921 name.
21923 [basic.lookup.qual]
21925 During the lookup for a name preceding the :: scope
21926 resolution operator, object, function, and enumerator
21927 names are ignored. */
21928 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
21929 tag_type = scope_type;
21930 /* Look up the name. */
21931 decl = cp_parser_lookup_name (parser, identifier,
21932 tag_type,
21933 /*is_template=*/false,
21934 /*is_namespace=*/false,
21935 check_dependency_p,
21936 &ambiguous_decls,
21937 identifier_token->location);
21938 if (ambiguous_decls)
21940 if (cp_parser_parsing_tentatively (parser))
21941 cp_parser_simulate_error (parser);
21942 return error_mark_node;
21946 else
21948 /* Try a template-id. */
21949 decl = cp_parser_template_id (parser, template_keyword_p,
21950 check_dependency_p,
21951 tag_type,
21952 is_declaration);
21953 if (decl == error_mark_node)
21954 return error_mark_node;
21957 decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
21959 /* If this is a typename, create a TYPENAME_TYPE. */
21960 if (typename_p && decl != error_mark_node)
21962 decl = make_typename_type (scope, decl, typename_type,
21963 /*complain=*/tf_error);
21964 if (decl != error_mark_node)
21965 decl = TYPE_NAME (decl);
21968 decl = strip_using_decl (decl);
21970 /* Check to see that it is really the name of a class. */
21971 if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
21972 && identifier_p (TREE_OPERAND (decl, 0))
21973 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
21974 /* Situations like this:
21976 template <typename T> struct A {
21977 typename T::template X<int>::I i;
21980 are problematic. Is `T::template X<int>' a class-name? The
21981 standard does not seem to be definitive, but there is no other
21982 valid interpretation of the following `::'. Therefore, those
21983 names are considered class-names. */
21985 decl = make_typename_type (scope, decl, tag_type, tf_error);
21986 if (decl != error_mark_node)
21987 decl = TYPE_NAME (decl);
21989 else if (TREE_CODE (decl) != TYPE_DECL
21990 || TREE_TYPE (decl) == error_mark_node
21991 || !(MAYBE_CLASS_TYPE_P (TREE_TYPE (decl))
21992 || (enum_ok && TREE_CODE (TREE_TYPE (decl)) == ENUMERAL_TYPE))
21993 /* In Objective-C 2.0, a classname followed by '.' starts a
21994 dot-syntax expression, and it's not a type-name. */
21995 || (c_dialect_objc ()
21996 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT
21997 && objc_is_class_name (decl)))
21998 decl = error_mark_node;
22000 if (decl == error_mark_node)
22001 cp_parser_error (parser, "expected class-name");
22002 else if (identifier && !parser->scope)
22003 maybe_note_name_used_in_class (identifier, decl);
22005 return decl;
22008 /* Parse a class-specifier.
22010 class-specifier:
22011 class-head { member-specification [opt] }
22013 Returns the TREE_TYPE representing the class. */
22015 static tree
22016 cp_parser_class_specifier_1 (cp_parser* parser)
22018 tree type;
22019 tree attributes = NULL_TREE;
22020 bool nested_name_specifier_p;
22021 unsigned saved_num_template_parameter_lists;
22022 bool saved_in_function_body;
22023 unsigned char in_statement;
22024 bool in_switch_statement_p;
22025 bool saved_in_unbraced_linkage_specification_p;
22026 tree old_scope = NULL_TREE;
22027 tree scope = NULL_TREE;
22028 cp_token *closing_brace;
22030 push_deferring_access_checks (dk_no_deferred);
22032 /* Parse the class-head. */
22033 type = cp_parser_class_head (parser,
22034 &nested_name_specifier_p);
22035 /* If the class-head was a semantic disaster, skip the entire body
22036 of the class. */
22037 if (!type)
22039 cp_parser_skip_to_end_of_block_or_statement (parser);
22040 pop_deferring_access_checks ();
22041 return error_mark_node;
22044 /* Look for the `{'. */
22045 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
22047 pop_deferring_access_checks ();
22048 return error_mark_node;
22051 cp_ensure_no_omp_declare_simd (parser);
22052 cp_ensure_no_oacc_routine (parser);
22054 /* Issue an error message if type-definitions are forbidden here. */
22055 cp_parser_check_type_definition (parser);
22056 /* Remember that we are defining one more class. */
22057 ++parser->num_classes_being_defined;
22058 /* Inside the class, surrounding template-parameter-lists do not
22059 apply. */
22060 saved_num_template_parameter_lists
22061 = parser->num_template_parameter_lists;
22062 parser->num_template_parameter_lists = 0;
22063 /* We are not in a function body. */
22064 saved_in_function_body = parser->in_function_body;
22065 parser->in_function_body = false;
22066 /* Or in a loop. */
22067 in_statement = parser->in_statement;
22068 parser->in_statement = 0;
22069 /* Or in a switch. */
22070 in_switch_statement_p = parser->in_switch_statement_p;
22071 parser->in_switch_statement_p = false;
22072 /* We are not immediately inside an extern "lang" block. */
22073 saved_in_unbraced_linkage_specification_p
22074 = parser->in_unbraced_linkage_specification_p;
22075 parser->in_unbraced_linkage_specification_p = false;
22077 // Associate constraints with the type.
22078 if (flag_concepts)
22079 type = associate_classtype_constraints (type);
22081 /* Start the class. */
22082 if (nested_name_specifier_p)
22084 scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
22085 old_scope = push_inner_scope (scope);
22087 type = begin_class_definition (type);
22089 if (type == error_mark_node)
22090 /* If the type is erroneous, skip the entire body of the class. */
22091 cp_parser_skip_to_closing_brace (parser);
22092 else
22093 /* Parse the member-specification. */
22094 cp_parser_member_specification_opt (parser);
22096 /* Look for the trailing `}'. */
22097 closing_brace = cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
22098 /* Look for trailing attributes to apply to this class. */
22099 if (cp_parser_allow_gnu_extensions_p (parser))
22100 attributes = cp_parser_gnu_attributes_opt (parser);
22101 if (type != error_mark_node)
22102 type = finish_struct (type, attributes);
22103 if (nested_name_specifier_p)
22104 pop_inner_scope (old_scope, scope);
22106 /* We've finished a type definition. Check for the common syntax
22107 error of forgetting a semicolon after the definition. We need to
22108 be careful, as we can't just check for not-a-semicolon and be done
22109 with it; the user might have typed:
22111 class X { } c = ...;
22112 class X { } *p = ...;
22114 and so forth. Instead, enumerate all the possible tokens that
22115 might follow this production; if we don't see one of them, then
22116 complain and silently insert the semicolon. */
22118 cp_token *token = cp_lexer_peek_token (parser->lexer);
22119 bool want_semicolon = true;
22121 if (cp_next_tokens_can_be_std_attribute_p (parser))
22122 /* Don't try to parse c++11 attributes here. As per the
22123 grammar, that should be a task for
22124 cp_parser_decl_specifier_seq. */
22125 want_semicolon = false;
22127 switch (token->type)
22129 case CPP_NAME:
22130 case CPP_SEMICOLON:
22131 case CPP_MULT:
22132 case CPP_AND:
22133 case CPP_OPEN_PAREN:
22134 case CPP_CLOSE_PAREN:
22135 case CPP_COMMA:
22136 want_semicolon = false;
22137 break;
22139 /* While it's legal for type qualifiers and storage class
22140 specifiers to follow type definitions in the grammar, only
22141 compiler testsuites contain code like that. Assume that if
22142 we see such code, then what we're really seeing is a case
22143 like:
22145 class X { }
22146 const <type> var = ...;
22150 class Y { }
22151 static <type> func (...) ...
22153 i.e. the qualifier or specifier applies to the next
22154 declaration. To do so, however, we need to look ahead one
22155 more token to see if *that* token is a type specifier.
22157 This code could be improved to handle:
22159 class Z { }
22160 static const <type> var = ...; */
22161 case CPP_KEYWORD:
22162 if (keyword_is_decl_specifier (token->keyword))
22164 cp_token *lookahead = cp_lexer_peek_nth_token (parser->lexer, 2);
22166 /* Handling user-defined types here would be nice, but very
22167 tricky. */
22168 want_semicolon
22169 = (lookahead->type == CPP_KEYWORD
22170 && keyword_begins_type_specifier (lookahead->keyword));
22172 break;
22173 default:
22174 break;
22177 /* If we don't have a type, then something is very wrong and we
22178 shouldn't try to do anything clever. Likewise for not seeing the
22179 closing brace. */
22180 if (closing_brace && TYPE_P (type) && want_semicolon)
22182 /* Locate the closing brace. */
22183 cp_token_position prev
22184 = cp_lexer_previous_token_position (parser->lexer);
22185 cp_token *prev_token = cp_lexer_token_at (parser->lexer, prev);
22186 location_t loc = prev_token->location;
22188 /* We want to suggest insertion of a ';' immediately *after* the
22189 closing brace, so, if we can, offset the location by 1 column. */
22190 location_t next_loc = loc;
22191 if (!linemap_location_from_macro_expansion_p (line_table, loc))
22192 next_loc = linemap_position_for_loc_and_offset (line_table, loc, 1);
22194 rich_location richloc (line_table, next_loc);
22196 /* If we successfully offset the location, suggest the fix-it. */
22197 if (next_loc != loc)
22198 richloc.add_fixit_insert_before (next_loc, ";");
22200 if (CLASSTYPE_DECLARED_CLASS (type))
22201 error_at_rich_loc (&richloc,
22202 "expected %<;%> after class definition");
22203 else if (TREE_CODE (type) == RECORD_TYPE)
22204 error_at_rich_loc (&richloc,
22205 "expected %<;%> after struct definition");
22206 else if (TREE_CODE (type) == UNION_TYPE)
22207 error_at_rich_loc (&richloc,
22208 "expected %<;%> after union definition");
22209 else
22210 gcc_unreachable ();
22212 /* Unget one token and smash it to look as though we encountered
22213 a semicolon in the input stream. */
22214 cp_lexer_set_token_position (parser->lexer, prev);
22215 token = cp_lexer_peek_token (parser->lexer);
22216 token->type = CPP_SEMICOLON;
22217 token->keyword = RID_MAX;
22221 /* If this class is not itself within the scope of another class,
22222 then we need to parse the bodies of all of the queued function
22223 definitions. Note that the queued functions defined in a class
22224 are not always processed immediately following the
22225 class-specifier for that class. Consider:
22227 struct A {
22228 struct B { void f() { sizeof (A); } };
22231 If `f' were processed before the processing of `A' were
22232 completed, there would be no way to compute the size of `A'.
22233 Note that the nesting we are interested in here is lexical --
22234 not the semantic nesting given by TYPE_CONTEXT. In particular,
22235 for:
22237 struct A { struct B; };
22238 struct A::B { void f() { } };
22240 there is no need to delay the parsing of `A::B::f'. */
22241 if (--parser->num_classes_being_defined == 0)
22243 tree decl;
22244 tree class_type = NULL_TREE;
22245 tree pushed_scope = NULL_TREE;
22246 unsigned ix;
22247 cp_default_arg_entry *e;
22248 tree save_ccp, save_ccr;
22250 /* In a first pass, parse default arguments to the functions.
22251 Then, in a second pass, parse the bodies of the functions.
22252 This two-phased approach handles cases like:
22254 struct S {
22255 void f() { g(); }
22256 void g(int i = 3);
22260 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_default_args, ix, e)
22262 decl = e->decl;
22263 /* If there are default arguments that have not yet been processed,
22264 take care of them now. */
22265 if (class_type != e->class_type)
22267 if (pushed_scope)
22268 pop_scope (pushed_scope);
22269 class_type = e->class_type;
22270 pushed_scope = push_scope (class_type);
22272 /* Make sure that any template parameters are in scope. */
22273 maybe_begin_member_template_processing (decl);
22274 /* Parse the default argument expressions. */
22275 cp_parser_late_parsing_default_args (parser, decl);
22276 /* Remove any template parameters from the symbol table. */
22277 maybe_end_member_template_processing ();
22279 vec_safe_truncate (unparsed_funs_with_default_args, 0);
22280 /* Now parse any NSDMIs. */
22281 save_ccp = current_class_ptr;
22282 save_ccr = current_class_ref;
22283 FOR_EACH_VEC_SAFE_ELT (unparsed_nsdmis, ix, decl)
22285 if (class_type != DECL_CONTEXT (decl))
22287 if (pushed_scope)
22288 pop_scope (pushed_scope);
22289 class_type = DECL_CONTEXT (decl);
22290 pushed_scope = push_scope (class_type);
22292 inject_this_parameter (class_type, TYPE_UNQUALIFIED);
22293 cp_parser_late_parsing_nsdmi (parser, decl);
22295 vec_safe_truncate (unparsed_nsdmis, 0);
22296 current_class_ptr = save_ccp;
22297 current_class_ref = save_ccr;
22298 if (pushed_scope)
22299 pop_scope (pushed_scope);
22301 /* Now do some post-NSDMI bookkeeping. */
22302 FOR_EACH_VEC_SAFE_ELT (unparsed_classes, ix, class_type)
22303 after_nsdmi_defaulted_late_checks (class_type);
22304 vec_safe_truncate (unparsed_classes, 0);
22305 after_nsdmi_defaulted_late_checks (type);
22307 /* Now parse the body of the functions. */
22308 if (flag_openmp)
22310 /* OpenMP UDRs need to be parsed before all other functions. */
22311 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
22312 if (DECL_OMP_DECLARE_REDUCTION_P (decl))
22313 cp_parser_late_parsing_for_member (parser, decl);
22314 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
22315 if (!DECL_OMP_DECLARE_REDUCTION_P (decl))
22316 cp_parser_late_parsing_for_member (parser, decl);
22318 else
22319 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
22320 cp_parser_late_parsing_for_member (parser, decl);
22321 vec_safe_truncate (unparsed_funs_with_definitions, 0);
22323 else
22324 vec_safe_push (unparsed_classes, type);
22326 /* Put back any saved access checks. */
22327 pop_deferring_access_checks ();
22329 /* Restore saved state. */
22330 parser->in_switch_statement_p = in_switch_statement_p;
22331 parser->in_statement = in_statement;
22332 parser->in_function_body = saved_in_function_body;
22333 parser->num_template_parameter_lists
22334 = saved_num_template_parameter_lists;
22335 parser->in_unbraced_linkage_specification_p
22336 = saved_in_unbraced_linkage_specification_p;
22338 return type;
22341 static tree
22342 cp_parser_class_specifier (cp_parser* parser)
22344 tree ret;
22345 timevar_push (TV_PARSE_STRUCT);
22346 ret = cp_parser_class_specifier_1 (parser);
22347 timevar_pop (TV_PARSE_STRUCT);
22348 return ret;
22351 /* Parse a class-head.
22353 class-head:
22354 class-key identifier [opt] base-clause [opt]
22355 class-key nested-name-specifier identifier class-virt-specifier [opt] base-clause [opt]
22356 class-key nested-name-specifier [opt] template-id
22357 base-clause [opt]
22359 class-virt-specifier:
22360 final
22362 GNU Extensions:
22363 class-key attributes identifier [opt] base-clause [opt]
22364 class-key attributes nested-name-specifier identifier base-clause [opt]
22365 class-key attributes nested-name-specifier [opt] template-id
22366 base-clause [opt]
22368 Upon return BASES is initialized to the list of base classes (or
22369 NULL, if there are none) in the same form returned by
22370 cp_parser_base_clause.
22372 Returns the TYPE of the indicated class. Sets
22373 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
22374 involving a nested-name-specifier was used, and FALSE otherwise.
22376 Returns error_mark_node if this is not a class-head.
22378 Returns NULL_TREE if the class-head is syntactically valid, but
22379 semantically invalid in a way that means we should skip the entire
22380 body of the class. */
22382 static tree
22383 cp_parser_class_head (cp_parser* parser,
22384 bool* nested_name_specifier_p)
22386 tree nested_name_specifier;
22387 enum tag_types class_key;
22388 tree id = NULL_TREE;
22389 tree type = NULL_TREE;
22390 tree attributes;
22391 tree bases;
22392 cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
22393 bool template_id_p = false;
22394 bool qualified_p = false;
22395 bool invalid_nested_name_p = false;
22396 bool invalid_explicit_specialization_p = false;
22397 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
22398 tree pushed_scope = NULL_TREE;
22399 unsigned num_templates;
22400 cp_token *type_start_token = NULL, *nested_name_specifier_token_start = NULL;
22401 /* Assume no nested-name-specifier will be present. */
22402 *nested_name_specifier_p = false;
22403 /* Assume no template parameter lists will be used in defining the
22404 type. */
22405 num_templates = 0;
22406 parser->colon_corrects_to_scope_p = false;
22408 /* Look for the class-key. */
22409 class_key = cp_parser_class_key (parser);
22410 if (class_key == none_type)
22411 return error_mark_node;
22413 location_t class_head_start_location = input_location;
22415 /* Parse the attributes. */
22416 attributes = cp_parser_attributes_opt (parser);
22418 /* If the next token is `::', that is invalid -- but sometimes
22419 people do try to write:
22421 struct ::S {};
22423 Handle this gracefully by accepting the extra qualifier, and then
22424 issuing an error about it later if this really is a
22425 class-head. If it turns out just to be an elaborated type
22426 specifier, remain silent. */
22427 if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
22428 qualified_p = true;
22430 push_deferring_access_checks (dk_no_check);
22432 /* Determine the name of the class. Begin by looking for an
22433 optional nested-name-specifier. */
22434 nested_name_specifier_token_start = cp_lexer_peek_token (parser->lexer);
22435 nested_name_specifier
22436 = cp_parser_nested_name_specifier_opt (parser,
22437 /*typename_keyword_p=*/false,
22438 /*check_dependency_p=*/false,
22439 /*type_p=*/true,
22440 /*is_declaration=*/false);
22441 /* If there was a nested-name-specifier, then there *must* be an
22442 identifier. */
22443 if (nested_name_specifier)
22445 type_start_token = cp_lexer_peek_token (parser->lexer);
22446 /* Although the grammar says `identifier', it really means
22447 `class-name' or `template-name'. You are only allowed to
22448 define a class that has already been declared with this
22449 syntax.
22451 The proposed resolution for Core Issue 180 says that wherever
22452 you see `class T::X' you should treat `X' as a type-name.
22454 It is OK to define an inaccessible class; for example:
22456 class A { class B; };
22457 class A::B {};
22459 We do not know if we will see a class-name, or a
22460 template-name. We look for a class-name first, in case the
22461 class-name is a template-id; if we looked for the
22462 template-name first we would stop after the template-name. */
22463 cp_parser_parse_tentatively (parser);
22464 type = cp_parser_class_name (parser,
22465 /*typename_keyword_p=*/false,
22466 /*template_keyword_p=*/false,
22467 class_type,
22468 /*check_dependency_p=*/false,
22469 /*class_head_p=*/true,
22470 /*is_declaration=*/false);
22471 /* If that didn't work, ignore the nested-name-specifier. */
22472 if (!cp_parser_parse_definitely (parser))
22474 invalid_nested_name_p = true;
22475 type_start_token = cp_lexer_peek_token (parser->lexer);
22476 id = cp_parser_identifier (parser);
22477 if (id == error_mark_node)
22478 id = NULL_TREE;
22480 /* If we could not find a corresponding TYPE, treat this
22481 declaration like an unqualified declaration. */
22482 if (type == error_mark_node)
22483 nested_name_specifier = NULL_TREE;
22484 /* Otherwise, count the number of templates used in TYPE and its
22485 containing scopes. */
22486 else
22488 tree scope;
22490 for (scope = TREE_TYPE (type);
22491 scope && TREE_CODE (scope) != NAMESPACE_DECL;
22492 scope = get_containing_scope (scope))
22493 if (TYPE_P (scope)
22494 && CLASS_TYPE_P (scope)
22495 && CLASSTYPE_TEMPLATE_INFO (scope)
22496 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
22497 && (!CLASSTYPE_TEMPLATE_SPECIALIZATION (scope)
22498 || uses_template_parms (CLASSTYPE_TI_ARGS (scope))))
22499 ++num_templates;
22502 /* Otherwise, the identifier is optional. */
22503 else
22505 /* We don't know whether what comes next is a template-id,
22506 an identifier, or nothing at all. */
22507 cp_parser_parse_tentatively (parser);
22508 /* Check for a template-id. */
22509 type_start_token = cp_lexer_peek_token (parser->lexer);
22510 id = cp_parser_template_id (parser,
22511 /*template_keyword_p=*/false,
22512 /*check_dependency_p=*/true,
22513 class_key,
22514 /*is_declaration=*/true);
22515 /* If that didn't work, it could still be an identifier. */
22516 if (!cp_parser_parse_definitely (parser))
22518 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
22520 type_start_token = cp_lexer_peek_token (parser->lexer);
22521 id = cp_parser_identifier (parser);
22523 else
22524 id = NULL_TREE;
22526 else
22528 template_id_p = true;
22529 ++num_templates;
22533 pop_deferring_access_checks ();
22535 if (id)
22537 cp_parser_check_for_invalid_template_id (parser, id,
22538 class_key,
22539 type_start_token->location);
22541 virt_specifiers = cp_parser_virt_specifier_seq_opt (parser);
22543 /* If it's not a `:' or a `{' then we can't really be looking at a
22544 class-head, since a class-head only appears as part of a
22545 class-specifier. We have to detect this situation before calling
22546 xref_tag, since that has irreversible side-effects. */
22547 if (!cp_parser_next_token_starts_class_definition_p (parser))
22549 cp_parser_error (parser, "expected %<{%> or %<:%>");
22550 type = error_mark_node;
22551 goto out;
22554 /* At this point, we're going ahead with the class-specifier, even
22555 if some other problem occurs. */
22556 cp_parser_commit_to_tentative_parse (parser);
22557 if (virt_specifiers & VIRT_SPEC_OVERRIDE)
22559 cp_parser_error (parser,
22560 "cannot specify %<override%> for a class");
22561 type = error_mark_node;
22562 goto out;
22564 /* Issue the error about the overly-qualified name now. */
22565 if (qualified_p)
22567 cp_parser_error (parser,
22568 "global qualification of class name is invalid");
22569 type = error_mark_node;
22570 goto out;
22572 else if (invalid_nested_name_p)
22574 cp_parser_error (parser,
22575 "qualified name does not name a class");
22576 type = error_mark_node;
22577 goto out;
22579 else if (nested_name_specifier)
22581 tree scope;
22583 /* Reject typedef-names in class heads. */
22584 if (!DECL_IMPLICIT_TYPEDEF_P (type))
22586 error_at (type_start_token->location,
22587 "invalid class name in declaration of %qD",
22588 type);
22589 type = NULL_TREE;
22590 goto done;
22593 /* Figure out in what scope the declaration is being placed. */
22594 scope = current_scope ();
22595 /* If that scope does not contain the scope in which the
22596 class was originally declared, the program is invalid. */
22597 if (scope && !is_ancestor (scope, nested_name_specifier))
22599 if (at_namespace_scope_p ())
22600 error_at (type_start_token->location,
22601 "declaration of %qD in namespace %qD which does not "
22602 "enclose %qD",
22603 type, scope, nested_name_specifier);
22604 else
22605 error_at (type_start_token->location,
22606 "declaration of %qD in %qD which does not enclose %qD",
22607 type, scope, nested_name_specifier);
22608 type = NULL_TREE;
22609 goto done;
22611 /* [dcl.meaning]
22613 A declarator-id shall not be qualified except for the
22614 definition of a ... nested class outside of its class
22615 ... [or] the definition or explicit instantiation of a
22616 class member of a namespace outside of its namespace. */
22617 if (scope == nested_name_specifier)
22619 permerror (nested_name_specifier_token_start->location,
22620 "extra qualification not allowed");
22621 nested_name_specifier = NULL_TREE;
22622 num_templates = 0;
22625 /* An explicit-specialization must be preceded by "template <>". If
22626 it is not, try to recover gracefully. */
22627 if (at_namespace_scope_p ()
22628 && parser->num_template_parameter_lists == 0
22629 && !processing_template_parmlist
22630 && template_id_p)
22632 /* Build a location of this form:
22633 struct typename <ARGS>
22634 ^~~~~~~~~~~~~~~~~~~~~~
22635 with caret==start at the start token, and
22636 finishing at the end of the type. */
22637 location_t reported_loc
22638 = make_location (class_head_start_location,
22639 class_head_start_location,
22640 get_finish (type_start_token->location));
22641 rich_location richloc (line_table, reported_loc);
22642 richloc.add_fixit_insert_before (class_head_start_location,
22643 "template <> ");
22644 error_at_rich_loc
22645 (&richloc,
22646 "an explicit specialization must be preceded by %<template <>%>");
22647 invalid_explicit_specialization_p = true;
22648 /* Take the same action that would have been taken by
22649 cp_parser_explicit_specialization. */
22650 ++parser->num_template_parameter_lists;
22651 begin_specialization ();
22653 /* There must be no "return" statements between this point and the
22654 end of this function; set "type "to the correct return value and
22655 use "goto done;" to return. */
22656 /* Make sure that the right number of template parameters were
22657 present. */
22658 if (!cp_parser_check_template_parameters (parser, num_templates,
22659 type_start_token->location,
22660 /*declarator=*/NULL))
22662 /* If something went wrong, there is no point in even trying to
22663 process the class-definition. */
22664 type = NULL_TREE;
22665 goto done;
22668 /* Look up the type. */
22669 if (template_id_p)
22671 if (TREE_CODE (id) == TEMPLATE_ID_EXPR
22672 && (DECL_FUNCTION_TEMPLATE_P (TREE_OPERAND (id, 0))
22673 || TREE_CODE (TREE_OPERAND (id, 0)) == OVERLOAD))
22675 error_at (type_start_token->location,
22676 "function template %qD redeclared as a class template", id);
22677 type = error_mark_node;
22679 else
22681 type = TREE_TYPE (id);
22682 type = maybe_process_partial_specialization (type);
22684 /* Check the scope while we still know whether or not we had a
22685 nested-name-specifier. */
22686 if (type != error_mark_node)
22687 check_unqualified_spec_or_inst (type, type_start_token->location);
22689 if (nested_name_specifier)
22690 pushed_scope = push_scope (nested_name_specifier);
22692 else if (nested_name_specifier)
22694 tree class_type;
22696 /* Given:
22698 template <typename T> struct S { struct T };
22699 template <typename T> struct S<T>::T { };
22701 we will get a TYPENAME_TYPE when processing the definition of
22702 `S::T'. We need to resolve it to the actual type before we
22703 try to define it. */
22704 if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
22706 class_type = resolve_typename_type (TREE_TYPE (type),
22707 /*only_current_p=*/false);
22708 if (TREE_CODE (class_type) != TYPENAME_TYPE)
22709 type = TYPE_NAME (class_type);
22710 else
22712 cp_parser_error (parser, "could not resolve typename type");
22713 type = error_mark_node;
22717 if (maybe_process_partial_specialization (TREE_TYPE (type))
22718 == error_mark_node)
22720 type = NULL_TREE;
22721 goto done;
22724 class_type = current_class_type;
22725 /* Enter the scope indicated by the nested-name-specifier. */
22726 pushed_scope = push_scope (nested_name_specifier);
22727 /* Get the canonical version of this type. */
22728 type = TYPE_MAIN_DECL (TREE_TYPE (type));
22729 /* Call push_template_decl if it seems like we should be defining a
22730 template either from the template headers or the type we're
22731 defining, so that we diagnose both extra and missing headers. */
22732 if ((PROCESSING_REAL_TEMPLATE_DECL_P ()
22733 || CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (type)))
22734 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
22736 type = push_template_decl (type);
22737 if (type == error_mark_node)
22739 type = NULL_TREE;
22740 goto done;
22744 type = TREE_TYPE (type);
22745 *nested_name_specifier_p = true;
22747 else /* The name is not a nested name. */
22749 /* If the class was unnamed, create a dummy name. */
22750 if (!id)
22751 id = make_anon_name ();
22752 tag_scope tag_scope = (parser->in_type_id_in_expr_p
22753 ? ts_within_enclosing_non_class
22754 : ts_current);
22755 type = xref_tag (class_key, id, tag_scope,
22756 parser->num_template_parameter_lists);
22759 /* Indicate whether this class was declared as a `class' or as a
22760 `struct'. */
22761 if (TREE_CODE (type) == RECORD_TYPE)
22762 CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
22763 cp_parser_check_class_key (class_key, type);
22765 /* If this type was already complete, and we see another definition,
22766 that's an error. */
22767 if (type != error_mark_node && COMPLETE_TYPE_P (type))
22769 error_at (type_start_token->location, "redefinition of %q#T",
22770 type);
22771 inform (location_of (type), "previous definition of %q#T",
22772 type);
22773 type = NULL_TREE;
22774 goto done;
22776 else if (type == error_mark_node)
22777 type = NULL_TREE;
22779 if (type)
22781 /* Apply attributes now, before any use of the class as a template
22782 argument in its base list. */
22783 cplus_decl_attributes (&type, attributes, (int)ATTR_FLAG_TYPE_IN_PLACE);
22784 fixup_attribute_variants (type);
22787 /* We will have entered the scope containing the class; the names of
22788 base classes should be looked up in that context. For example:
22790 struct A { struct B {}; struct C; };
22791 struct A::C : B {};
22793 is valid. */
22795 /* Get the list of base-classes, if there is one. */
22796 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
22798 /* PR59482: enter the class scope so that base-specifiers are looked
22799 up correctly. */
22800 if (type)
22801 pushclass (type);
22802 bases = cp_parser_base_clause (parser);
22803 /* PR59482: get out of the previously pushed class scope so that the
22804 subsequent pops pop the right thing. */
22805 if (type)
22806 popclass ();
22808 else
22809 bases = NULL_TREE;
22811 /* If we're really defining a class, process the base classes.
22812 If they're invalid, fail. */
22813 if (type && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
22814 xref_basetypes (type, bases);
22816 done:
22817 /* Leave the scope given by the nested-name-specifier. We will
22818 enter the class scope itself while processing the members. */
22819 if (pushed_scope)
22820 pop_scope (pushed_scope);
22822 if (invalid_explicit_specialization_p)
22824 end_specialization ();
22825 --parser->num_template_parameter_lists;
22828 if (type)
22829 DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
22830 if (type && (virt_specifiers & VIRT_SPEC_FINAL))
22831 CLASSTYPE_FINAL (type) = 1;
22832 out:
22833 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
22834 return type;
22837 /* Parse a class-key.
22839 class-key:
22840 class
22841 struct
22842 union
22844 Returns the kind of class-key specified, or none_type to indicate
22845 error. */
22847 static enum tag_types
22848 cp_parser_class_key (cp_parser* parser)
22850 cp_token *token;
22851 enum tag_types tag_type;
22853 /* Look for the class-key. */
22854 token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_KEY);
22855 if (!token)
22856 return none_type;
22858 /* Check to see if the TOKEN is a class-key. */
22859 tag_type = cp_parser_token_is_class_key (token);
22860 if (!tag_type)
22861 cp_parser_error (parser, "expected class-key");
22862 return tag_type;
22865 /* Parse a type-parameter-key.
22867 type-parameter-key:
22868 class
22869 typename
22872 static void
22873 cp_parser_type_parameter_key (cp_parser* parser)
22875 /* Look for the type-parameter-key. */
22876 enum tag_types tag_type = none_type;
22877 cp_token *token = cp_lexer_peek_token (parser->lexer);
22878 if ((tag_type = cp_parser_token_is_type_parameter_key (token)) != none_type)
22880 cp_lexer_consume_token (parser->lexer);
22881 if (pedantic && tag_type == typename_type && cxx_dialect < cxx1z)
22882 /* typename is not allowed in a template template parameter
22883 by the standard until C++1Z. */
22884 pedwarn (token->location, OPT_Wpedantic,
22885 "ISO C++ forbids typename key in template template parameter;"
22886 " use -std=c++1z or -std=gnu++1z");
22888 else
22889 cp_parser_error (parser, "expected %<class%> or %<typename%>");
22891 return;
22894 /* Parse an (optional) member-specification.
22896 member-specification:
22897 member-declaration member-specification [opt]
22898 access-specifier : member-specification [opt] */
22900 static void
22901 cp_parser_member_specification_opt (cp_parser* parser)
22903 while (true)
22905 cp_token *token;
22906 enum rid keyword;
22908 /* Peek at the next token. */
22909 token = cp_lexer_peek_token (parser->lexer);
22910 /* If it's a `}', or EOF then we've seen all the members. */
22911 if (token->type == CPP_CLOSE_BRACE
22912 || token->type == CPP_EOF
22913 || token->type == CPP_PRAGMA_EOL)
22914 break;
22916 /* See if this token is a keyword. */
22917 keyword = token->keyword;
22918 switch (keyword)
22920 case RID_PUBLIC:
22921 case RID_PROTECTED:
22922 case RID_PRIVATE:
22923 /* Consume the access-specifier. */
22924 cp_lexer_consume_token (parser->lexer);
22925 /* Remember which access-specifier is active. */
22926 current_access_specifier = token->u.value;
22927 /* Look for the `:'. */
22928 cp_parser_require (parser, CPP_COLON, RT_COLON);
22929 break;
22931 default:
22932 /* Accept #pragmas at class scope. */
22933 if (token->type == CPP_PRAGMA)
22935 cp_parser_pragma (parser, pragma_member, NULL);
22936 break;
22939 /* Otherwise, the next construction must be a
22940 member-declaration. */
22941 cp_parser_member_declaration (parser);
22946 /* Parse a member-declaration.
22948 member-declaration:
22949 decl-specifier-seq [opt] member-declarator-list [opt] ;
22950 function-definition ; [opt]
22951 :: [opt] nested-name-specifier template [opt] unqualified-id ;
22952 using-declaration
22953 template-declaration
22954 alias-declaration
22956 member-declarator-list:
22957 member-declarator
22958 member-declarator-list , member-declarator
22960 member-declarator:
22961 declarator pure-specifier [opt]
22962 declarator constant-initializer [opt]
22963 identifier [opt] : constant-expression
22965 GNU Extensions:
22967 member-declaration:
22968 __extension__ member-declaration
22970 member-declarator:
22971 declarator attributes [opt] pure-specifier [opt]
22972 declarator attributes [opt] constant-initializer [opt]
22973 identifier [opt] attributes [opt] : constant-expression
22975 C++0x Extensions:
22977 member-declaration:
22978 static_assert-declaration */
22980 static void
22981 cp_parser_member_declaration (cp_parser* parser)
22983 cp_decl_specifier_seq decl_specifiers;
22984 tree prefix_attributes;
22985 tree decl;
22986 int declares_class_or_enum;
22987 bool friend_p;
22988 cp_token *token = NULL;
22989 cp_token *decl_spec_token_start = NULL;
22990 cp_token *initializer_token_start = NULL;
22991 int saved_pedantic;
22992 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
22994 /* Check for the `__extension__' keyword. */
22995 if (cp_parser_extension_opt (parser, &saved_pedantic))
22997 /* Recurse. */
22998 cp_parser_member_declaration (parser);
22999 /* Restore the old value of the PEDANTIC flag. */
23000 pedantic = saved_pedantic;
23002 return;
23005 /* Check for a template-declaration. */
23006 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
23008 /* An explicit specialization here is an error condition, and we
23009 expect the specialization handler to detect and report this. */
23010 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
23011 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
23012 cp_parser_explicit_specialization (parser);
23013 else
23014 cp_parser_template_declaration (parser, /*member_p=*/true);
23016 return;
23018 /* Check for a template introduction. */
23019 else if (cp_parser_template_declaration_after_export (parser, true))
23020 return;
23022 /* Check for a using-declaration. */
23023 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
23025 if (cxx_dialect < cxx11)
23027 /* Parse the using-declaration. */
23028 cp_parser_using_declaration (parser,
23029 /*access_declaration_p=*/false);
23030 return;
23032 else
23034 tree decl;
23035 bool alias_decl_expected;
23036 cp_parser_parse_tentatively (parser);
23037 decl = cp_parser_alias_declaration (parser);
23038 /* Note that if we actually see the '=' token after the
23039 identifier, cp_parser_alias_declaration commits the
23040 tentative parse. In that case, we really expect an
23041 alias-declaration. Otherwise, we expect a using
23042 declaration. */
23043 alias_decl_expected =
23044 !cp_parser_uncommitted_to_tentative_parse_p (parser);
23045 cp_parser_parse_definitely (parser);
23047 if (alias_decl_expected)
23048 finish_member_declaration (decl);
23049 else
23050 cp_parser_using_declaration (parser,
23051 /*access_declaration_p=*/false);
23052 return;
23056 /* Check for @defs. */
23057 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_DEFS))
23059 tree ivar, member;
23060 tree ivar_chains = cp_parser_objc_defs_expression (parser);
23061 ivar = ivar_chains;
23062 while (ivar)
23064 member = ivar;
23065 ivar = TREE_CHAIN (member);
23066 TREE_CHAIN (member) = NULL_TREE;
23067 finish_member_declaration (member);
23069 return;
23072 /* If the next token is `static_assert' we have a static assertion. */
23073 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC_ASSERT))
23075 cp_parser_static_assert (parser, /*member_p=*/true);
23076 return;
23079 parser->colon_corrects_to_scope_p = false;
23081 if (cp_parser_using_declaration (parser, /*access_declaration=*/true))
23082 goto out;
23084 /* Parse the decl-specifier-seq. */
23085 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
23086 cp_parser_decl_specifier_seq (parser,
23087 CP_PARSER_FLAGS_OPTIONAL,
23088 &decl_specifiers,
23089 &declares_class_or_enum);
23090 /* Check for an invalid type-name. */
23091 if (!decl_specifiers.any_type_specifiers_p
23092 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
23093 goto out;
23094 /* If there is no declarator, then the decl-specifier-seq should
23095 specify a type. */
23096 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
23098 /* If there was no decl-specifier-seq, and the next token is a
23099 `;', then we have something like:
23101 struct S { ; };
23103 [class.mem]
23105 Each member-declaration shall declare at least one member
23106 name of the class. */
23107 if (!decl_specifiers.any_specifiers_p)
23109 cp_token *token = cp_lexer_peek_token (parser->lexer);
23110 if (!in_system_header_at (token->location))
23111 pedwarn (token->location, OPT_Wpedantic, "extra %<;%>");
23113 else
23115 tree type;
23117 /* See if this declaration is a friend. */
23118 friend_p = cp_parser_friend_p (&decl_specifiers);
23119 /* If there were decl-specifiers, check to see if there was
23120 a class-declaration. */
23121 type = check_tag_decl (&decl_specifiers,
23122 /*explicit_type_instantiation_p=*/false);
23123 /* Nested classes have already been added to the class, but
23124 a `friend' needs to be explicitly registered. */
23125 if (friend_p)
23127 /* If the `friend' keyword was present, the friend must
23128 be introduced with a class-key. */
23129 if (!declares_class_or_enum && cxx_dialect < cxx11)
23130 pedwarn (decl_spec_token_start->location, OPT_Wpedantic,
23131 "in C++03 a class-key must be used "
23132 "when declaring a friend");
23133 /* In this case:
23135 template <typename T> struct A {
23136 friend struct A<T>::B;
23139 A<T>::B will be represented by a TYPENAME_TYPE, and
23140 therefore not recognized by check_tag_decl. */
23141 if (!type)
23143 type = decl_specifiers.type;
23144 if (type && TREE_CODE (type) == TYPE_DECL)
23145 type = TREE_TYPE (type);
23147 if (!type || !TYPE_P (type))
23148 error_at (decl_spec_token_start->location,
23149 "friend declaration does not name a class or "
23150 "function");
23151 else
23152 make_friend_class (current_class_type, type,
23153 /*complain=*/true);
23155 /* If there is no TYPE, an error message will already have
23156 been issued. */
23157 else if (!type || type == error_mark_node)
23159 /* An anonymous aggregate has to be handled specially; such
23160 a declaration really declares a data member (with a
23161 particular type), as opposed to a nested class. */
23162 else if (ANON_AGGR_TYPE_P (type))
23164 /* C++11 9.5/6. */
23165 if (decl_specifiers.storage_class != sc_none)
23166 error_at (decl_spec_token_start->location,
23167 "a storage class on an anonymous aggregate "
23168 "in class scope is not allowed");
23170 /* Remove constructors and such from TYPE, now that we
23171 know it is an anonymous aggregate. */
23172 fixup_anonymous_aggr (type);
23173 /* And make the corresponding data member. */
23174 decl = build_decl (decl_spec_token_start->location,
23175 FIELD_DECL, NULL_TREE, type);
23176 /* Add it to the class. */
23177 finish_member_declaration (decl);
23179 else
23180 cp_parser_check_access_in_redeclaration
23181 (TYPE_NAME (type),
23182 decl_spec_token_start->location);
23185 else
23187 bool assume_semicolon = false;
23189 /* Clear attributes from the decl_specifiers but keep them
23190 around as prefix attributes that apply them to the entity
23191 being declared. */
23192 prefix_attributes = decl_specifiers.attributes;
23193 decl_specifiers.attributes = NULL_TREE;
23195 /* See if these declarations will be friends. */
23196 friend_p = cp_parser_friend_p (&decl_specifiers);
23198 /* Keep going until we hit the `;' at the end of the
23199 declaration. */
23200 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
23202 tree attributes = NULL_TREE;
23203 tree first_attribute;
23205 /* Peek at the next token. */
23206 token = cp_lexer_peek_token (parser->lexer);
23208 /* Check for a bitfield declaration. */
23209 if (token->type == CPP_COLON
23210 || (token->type == CPP_NAME
23211 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
23212 == CPP_COLON))
23214 tree identifier;
23215 tree width;
23217 /* Get the name of the bitfield. Note that we cannot just
23218 check TOKEN here because it may have been invalidated by
23219 the call to cp_lexer_peek_nth_token above. */
23220 if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
23221 identifier = cp_parser_identifier (parser);
23222 else
23223 identifier = NULL_TREE;
23225 /* Consume the `:' token. */
23226 cp_lexer_consume_token (parser->lexer);
23227 /* Get the width of the bitfield. */
23228 width
23229 = cp_parser_constant_expression (parser);
23231 /* Look for attributes that apply to the bitfield. */
23232 attributes = cp_parser_attributes_opt (parser);
23233 /* Remember which attributes are prefix attributes and
23234 which are not. */
23235 first_attribute = attributes;
23236 /* Combine the attributes. */
23237 attributes = chainon (prefix_attributes, attributes);
23239 /* Create the bitfield declaration. */
23240 decl = grokbitfield (identifier
23241 ? make_id_declarator (NULL_TREE,
23242 identifier,
23243 sfk_none)
23244 : NULL,
23245 &decl_specifiers,
23246 width,
23247 attributes);
23249 else
23251 cp_declarator *declarator;
23252 tree initializer;
23253 tree asm_specification;
23254 int ctor_dtor_or_conv_p;
23256 /* Parse the declarator. */
23257 declarator
23258 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
23259 &ctor_dtor_or_conv_p,
23260 /*parenthesized_p=*/NULL,
23261 /*member_p=*/true,
23262 friend_p);
23264 /* If something went wrong parsing the declarator, make sure
23265 that we at least consume some tokens. */
23266 if (declarator == cp_error_declarator)
23268 /* Skip to the end of the statement. */
23269 cp_parser_skip_to_end_of_statement (parser);
23270 /* If the next token is not a semicolon, that is
23271 probably because we just skipped over the body of
23272 a function. So, we consume a semicolon if
23273 present, but do not issue an error message if it
23274 is not present. */
23275 if (cp_lexer_next_token_is (parser->lexer,
23276 CPP_SEMICOLON))
23277 cp_lexer_consume_token (parser->lexer);
23278 goto out;
23281 if (declares_class_or_enum & 2)
23282 cp_parser_check_for_definition_in_return_type
23283 (declarator, decl_specifiers.type,
23284 decl_specifiers.locations[ds_type_spec]);
23286 /* Look for an asm-specification. */
23287 asm_specification = cp_parser_asm_specification_opt (parser);
23288 /* Look for attributes that apply to the declaration. */
23289 attributes = cp_parser_attributes_opt (parser);
23290 /* Remember which attributes are prefix attributes and
23291 which are not. */
23292 first_attribute = attributes;
23293 /* Combine the attributes. */
23294 attributes = chainon (prefix_attributes, attributes);
23296 /* If it's an `=', then we have a constant-initializer or a
23297 pure-specifier. It is not correct to parse the
23298 initializer before registering the member declaration
23299 since the member declaration should be in scope while
23300 its initializer is processed. However, the rest of the
23301 front end does not yet provide an interface that allows
23302 us to handle this correctly. */
23303 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
23305 /* In [class.mem]:
23307 A pure-specifier shall be used only in the declaration of
23308 a virtual function.
23310 A member-declarator can contain a constant-initializer
23311 only if it declares a static member of integral or
23312 enumeration type.
23314 Therefore, if the DECLARATOR is for a function, we look
23315 for a pure-specifier; otherwise, we look for a
23316 constant-initializer. When we call `grokfield', it will
23317 perform more stringent semantics checks. */
23318 initializer_token_start = cp_lexer_peek_token (parser->lexer);
23319 if (function_declarator_p (declarator)
23320 || (decl_specifiers.type
23321 && TREE_CODE (decl_specifiers.type) == TYPE_DECL
23322 && declarator->kind == cdk_id
23323 && (TREE_CODE (TREE_TYPE (decl_specifiers.type))
23324 == FUNCTION_TYPE)))
23325 initializer = cp_parser_pure_specifier (parser);
23326 else if (decl_specifiers.storage_class != sc_static)
23327 initializer = cp_parser_save_nsdmi (parser);
23328 else if (cxx_dialect >= cxx11)
23330 bool nonconst;
23331 /* Don't require a constant rvalue in C++11, since we
23332 might want a reference constant. We'll enforce
23333 constancy later. */
23334 cp_lexer_consume_token (parser->lexer);
23335 /* Parse the initializer. */
23336 initializer = cp_parser_initializer_clause (parser,
23337 &nonconst);
23339 else
23340 /* Parse the initializer. */
23341 initializer = cp_parser_constant_initializer (parser);
23343 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
23344 && !function_declarator_p (declarator))
23346 bool x;
23347 if (decl_specifiers.storage_class != sc_static)
23348 initializer = cp_parser_save_nsdmi (parser);
23349 else
23350 initializer = cp_parser_initializer (parser, &x, &x);
23352 /* Otherwise, there is no initializer. */
23353 else
23354 initializer = NULL_TREE;
23356 /* See if we are probably looking at a function
23357 definition. We are certainly not looking at a
23358 member-declarator. Calling `grokfield' has
23359 side-effects, so we must not do it unless we are sure
23360 that we are looking at a member-declarator. */
23361 if (cp_parser_token_starts_function_definition_p
23362 (cp_lexer_peek_token (parser->lexer)))
23364 /* The grammar does not allow a pure-specifier to be
23365 used when a member function is defined. (It is
23366 possible that this fact is an oversight in the
23367 standard, since a pure function may be defined
23368 outside of the class-specifier. */
23369 if (initializer && initializer_token_start)
23370 error_at (initializer_token_start->location,
23371 "pure-specifier on function-definition");
23372 decl = cp_parser_save_member_function_body (parser,
23373 &decl_specifiers,
23374 declarator,
23375 attributes);
23376 if (parser->fully_implicit_function_template_p)
23377 decl = finish_fully_implicit_template (parser, decl);
23378 /* If the member was not a friend, declare it here. */
23379 if (!friend_p)
23380 finish_member_declaration (decl);
23381 /* Peek at the next token. */
23382 token = cp_lexer_peek_token (parser->lexer);
23383 /* If the next token is a semicolon, consume it. */
23384 if (token->type == CPP_SEMICOLON)
23385 cp_lexer_consume_token (parser->lexer);
23386 goto out;
23388 else
23389 if (declarator->kind == cdk_function)
23390 declarator->id_loc = token->location;
23391 /* Create the declaration. */
23392 decl = grokfield (declarator, &decl_specifiers,
23393 initializer, /*init_const_expr_p=*/true,
23394 asm_specification, attributes);
23395 if (parser->fully_implicit_function_template_p)
23397 if (friend_p)
23398 finish_fully_implicit_template (parser, 0);
23399 else
23400 decl = finish_fully_implicit_template (parser, decl);
23404 cp_finalize_omp_declare_simd (parser, decl);
23405 cp_finalize_oacc_routine (parser, decl, false);
23407 /* Reset PREFIX_ATTRIBUTES. */
23408 while (attributes && TREE_CHAIN (attributes) != first_attribute)
23409 attributes = TREE_CHAIN (attributes);
23410 if (attributes)
23411 TREE_CHAIN (attributes) = NULL_TREE;
23413 /* If there is any qualification still in effect, clear it
23414 now; we will be starting fresh with the next declarator. */
23415 parser->scope = NULL_TREE;
23416 parser->qualifying_scope = NULL_TREE;
23417 parser->object_scope = NULL_TREE;
23418 /* If it's a `,', then there are more declarators. */
23419 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
23421 cp_lexer_consume_token (parser->lexer);
23422 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
23424 cp_token *token = cp_lexer_previous_token (parser->lexer);
23425 error_at (token->location,
23426 "stray %<,%> at end of member declaration");
23429 /* If the next token isn't a `;', then we have a parse error. */
23430 else if (cp_lexer_next_token_is_not (parser->lexer,
23431 CPP_SEMICOLON))
23433 /* The next token might be a ways away from where the
23434 actual semicolon is missing. Find the previous token
23435 and use that for our error position. */
23436 cp_token *token = cp_lexer_previous_token (parser->lexer);
23437 error_at (token->location,
23438 "expected %<;%> at end of member declaration");
23440 /* Assume that the user meant to provide a semicolon. If
23441 we were to cp_parser_skip_to_end_of_statement, we might
23442 skip to a semicolon inside a member function definition
23443 and issue nonsensical error messages. */
23444 assume_semicolon = true;
23447 if (decl)
23449 /* Add DECL to the list of members. */
23450 if (!friend_p
23451 /* Explicitly include, eg, NSDMIs, for better error
23452 recovery (c++/58650). */
23453 || !DECL_DECLARES_FUNCTION_P (decl))
23454 finish_member_declaration (decl);
23456 if (TREE_CODE (decl) == FUNCTION_DECL)
23457 cp_parser_save_default_args (parser, decl);
23458 else if (TREE_CODE (decl) == FIELD_DECL
23459 && !DECL_C_BIT_FIELD (decl)
23460 && DECL_INITIAL (decl))
23461 /* Add DECL to the queue of NSDMI to be parsed later. */
23462 vec_safe_push (unparsed_nsdmis, decl);
23465 if (assume_semicolon)
23466 goto out;
23470 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
23471 out:
23472 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
23475 /* Parse a pure-specifier.
23477 pure-specifier:
23480 Returns INTEGER_ZERO_NODE if a pure specifier is found.
23481 Otherwise, ERROR_MARK_NODE is returned. */
23483 static tree
23484 cp_parser_pure_specifier (cp_parser* parser)
23486 cp_token *token;
23488 /* Look for the `=' token. */
23489 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
23490 return error_mark_node;
23491 /* Look for the `0' token. */
23492 token = cp_lexer_peek_token (parser->lexer);
23494 if (token->type == CPP_EOF
23495 || token->type == CPP_PRAGMA_EOL)
23496 return error_mark_node;
23498 cp_lexer_consume_token (parser->lexer);
23500 /* Accept = default or = delete in c++0x mode. */
23501 if (token->keyword == RID_DEFAULT
23502 || token->keyword == RID_DELETE)
23504 maybe_warn_cpp0x (CPP0X_DEFAULTED_DELETED);
23505 return token->u.value;
23508 /* c_lex_with_flags marks a single digit '0' with PURE_ZERO. */
23509 if (token->type != CPP_NUMBER || !(token->flags & PURE_ZERO))
23511 cp_parser_error (parser,
23512 "invalid pure specifier (only %<= 0%> is allowed)");
23513 cp_parser_skip_to_end_of_statement (parser);
23514 return error_mark_node;
23516 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
23518 error_at (token->location, "templates may not be %<virtual%>");
23519 return error_mark_node;
23522 return integer_zero_node;
23525 /* Parse a constant-initializer.
23527 constant-initializer:
23528 = constant-expression
23530 Returns a representation of the constant-expression. */
23532 static tree
23533 cp_parser_constant_initializer (cp_parser* parser)
23535 /* Look for the `=' token. */
23536 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
23537 return error_mark_node;
23539 /* It is invalid to write:
23541 struct S { static const int i = { 7 }; };
23544 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
23546 cp_parser_error (parser,
23547 "a brace-enclosed initializer is not allowed here");
23548 /* Consume the opening brace. */
23549 cp_lexer_consume_token (parser->lexer);
23550 /* Skip the initializer. */
23551 cp_parser_skip_to_closing_brace (parser);
23552 /* Look for the trailing `}'. */
23553 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
23555 return error_mark_node;
23558 return cp_parser_constant_expression (parser);
23561 /* Derived classes [gram.class.derived] */
23563 /* Parse a base-clause.
23565 base-clause:
23566 : base-specifier-list
23568 base-specifier-list:
23569 base-specifier ... [opt]
23570 base-specifier-list , base-specifier ... [opt]
23572 Returns a TREE_LIST representing the base-classes, in the order in
23573 which they were declared. The representation of each node is as
23574 described by cp_parser_base_specifier.
23576 In the case that no bases are specified, this function will return
23577 NULL_TREE, not ERROR_MARK_NODE. */
23579 static tree
23580 cp_parser_base_clause (cp_parser* parser)
23582 tree bases = NULL_TREE;
23584 /* Look for the `:' that begins the list. */
23585 cp_parser_require (parser, CPP_COLON, RT_COLON);
23587 /* Scan the base-specifier-list. */
23588 while (true)
23590 cp_token *token;
23591 tree base;
23592 bool pack_expansion_p = false;
23594 /* Look for the base-specifier. */
23595 base = cp_parser_base_specifier (parser);
23596 /* Look for the (optional) ellipsis. */
23597 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
23599 /* Consume the `...'. */
23600 cp_lexer_consume_token (parser->lexer);
23602 pack_expansion_p = true;
23605 /* Add BASE to the front of the list. */
23606 if (base && base != error_mark_node)
23608 if (pack_expansion_p)
23609 /* Make this a pack expansion type. */
23610 TREE_VALUE (base) = make_pack_expansion (TREE_VALUE (base));
23612 if (!check_for_bare_parameter_packs (TREE_VALUE (base)))
23614 TREE_CHAIN (base) = bases;
23615 bases = base;
23618 /* Peek at the next token. */
23619 token = cp_lexer_peek_token (parser->lexer);
23620 /* If it's not a comma, then the list is complete. */
23621 if (token->type != CPP_COMMA)
23622 break;
23623 /* Consume the `,'. */
23624 cp_lexer_consume_token (parser->lexer);
23627 /* PARSER->SCOPE may still be non-NULL at this point, if the last
23628 base class had a qualified name. However, the next name that
23629 appears is certainly not qualified. */
23630 parser->scope = NULL_TREE;
23631 parser->qualifying_scope = NULL_TREE;
23632 parser->object_scope = NULL_TREE;
23634 return nreverse (bases);
23637 /* Parse a base-specifier.
23639 base-specifier:
23640 :: [opt] nested-name-specifier [opt] class-name
23641 virtual access-specifier [opt] :: [opt] nested-name-specifier
23642 [opt] class-name
23643 access-specifier virtual [opt] :: [opt] nested-name-specifier
23644 [opt] class-name
23646 Returns a TREE_LIST. The TREE_PURPOSE will be one of
23647 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
23648 indicate the specifiers provided. The TREE_VALUE will be a TYPE
23649 (or the ERROR_MARK_NODE) indicating the type that was specified. */
23651 static tree
23652 cp_parser_base_specifier (cp_parser* parser)
23654 cp_token *token;
23655 bool done = false;
23656 bool virtual_p = false;
23657 bool duplicate_virtual_error_issued_p = false;
23658 bool duplicate_access_error_issued_p = false;
23659 bool class_scope_p, template_p;
23660 tree access = access_default_node;
23661 tree type;
23663 /* Process the optional `virtual' and `access-specifier'. */
23664 while (!done)
23666 /* Peek at the next token. */
23667 token = cp_lexer_peek_token (parser->lexer);
23668 /* Process `virtual'. */
23669 switch (token->keyword)
23671 case RID_VIRTUAL:
23672 /* If `virtual' appears more than once, issue an error. */
23673 if (virtual_p && !duplicate_virtual_error_issued_p)
23675 cp_parser_error (parser,
23676 "%<virtual%> specified more than once in base-specified");
23677 duplicate_virtual_error_issued_p = true;
23680 virtual_p = true;
23682 /* Consume the `virtual' token. */
23683 cp_lexer_consume_token (parser->lexer);
23685 break;
23687 case RID_PUBLIC:
23688 case RID_PROTECTED:
23689 case RID_PRIVATE:
23690 /* If more than one access specifier appears, issue an
23691 error. */
23692 if (access != access_default_node
23693 && !duplicate_access_error_issued_p)
23695 cp_parser_error (parser,
23696 "more than one access specifier in base-specified");
23697 duplicate_access_error_issued_p = true;
23700 access = ridpointers[(int) token->keyword];
23702 /* Consume the access-specifier. */
23703 cp_lexer_consume_token (parser->lexer);
23705 break;
23707 default:
23708 done = true;
23709 break;
23712 /* It is not uncommon to see programs mechanically, erroneously, use
23713 the 'typename' keyword to denote (dependent) qualified types
23714 as base classes. */
23715 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
23717 token = cp_lexer_peek_token (parser->lexer);
23718 if (!processing_template_decl)
23719 error_at (token->location,
23720 "keyword %<typename%> not allowed outside of templates");
23721 else
23722 error_at (token->location,
23723 "keyword %<typename%> not allowed in this context "
23724 "(the base class is implicitly a type)");
23725 cp_lexer_consume_token (parser->lexer);
23728 /* Look for the optional `::' operator. */
23729 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
23730 /* Look for the nested-name-specifier. The simplest way to
23731 implement:
23733 [temp.res]
23735 The keyword `typename' is not permitted in a base-specifier or
23736 mem-initializer; in these contexts a qualified name that
23737 depends on a template-parameter is implicitly assumed to be a
23738 type name.
23740 is to pretend that we have seen the `typename' keyword at this
23741 point. */
23742 cp_parser_nested_name_specifier_opt (parser,
23743 /*typename_keyword_p=*/true,
23744 /*check_dependency_p=*/true,
23745 /*type_p=*/true,
23746 /*is_declaration=*/true);
23747 /* If the base class is given by a qualified name, assume that names
23748 we see are type names or templates, as appropriate. */
23749 class_scope_p = (parser->scope && TYPE_P (parser->scope));
23750 template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
23752 if (!parser->scope
23753 && cp_lexer_next_token_is_decltype (parser->lexer))
23754 /* DR 950 allows decltype as a base-specifier. */
23755 type = cp_parser_decltype (parser);
23756 else
23758 /* Otherwise, look for the class-name. */
23759 type = cp_parser_class_name (parser,
23760 class_scope_p,
23761 template_p,
23762 typename_type,
23763 /*check_dependency_p=*/true,
23764 /*class_head_p=*/false,
23765 /*is_declaration=*/true);
23766 type = TREE_TYPE (type);
23769 if (type == error_mark_node)
23770 return error_mark_node;
23772 return finish_base_specifier (type, access, virtual_p);
23775 /* Exception handling [gram.exception] */
23777 /* Parse an (optional) noexcept-specification.
23779 noexcept-specification:
23780 noexcept ( constant-expression ) [opt]
23782 If no noexcept-specification is present, returns NULL_TREE.
23783 Otherwise, if REQUIRE_CONSTEXPR is false, then either parse and return any
23784 expression if parentheses follow noexcept, or return BOOLEAN_TRUE_NODE if
23785 there are no parentheses. CONSUMED_EXPR will be set accordingly.
23786 Otherwise, returns a noexcept specification unless RETURN_COND is true,
23787 in which case a boolean condition is returned instead. */
23789 static tree
23790 cp_parser_noexcept_specification_opt (cp_parser* parser,
23791 bool require_constexpr,
23792 bool* consumed_expr,
23793 bool return_cond)
23795 cp_token *token;
23796 const char *saved_message;
23798 /* Peek at the next token. */
23799 token = cp_lexer_peek_token (parser->lexer);
23801 /* Is it a noexcept-specification? */
23802 if (cp_parser_is_keyword (token, RID_NOEXCEPT))
23804 tree expr;
23805 cp_lexer_consume_token (parser->lexer);
23807 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
23809 cp_lexer_consume_token (parser->lexer);
23811 if (require_constexpr)
23813 /* Types may not be defined in an exception-specification. */
23814 saved_message = parser->type_definition_forbidden_message;
23815 parser->type_definition_forbidden_message
23816 = G_("types may not be defined in an exception-specification");
23818 expr = cp_parser_constant_expression (parser);
23820 /* Restore the saved message. */
23821 parser->type_definition_forbidden_message = saved_message;
23823 else
23825 expr = cp_parser_expression (parser);
23826 *consumed_expr = true;
23829 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
23831 else
23833 expr = boolean_true_node;
23834 if (!require_constexpr)
23835 *consumed_expr = false;
23838 /* We cannot build a noexcept-spec right away because this will check
23839 that expr is a constexpr. */
23840 if (!return_cond)
23841 return build_noexcept_spec (expr, tf_warning_or_error);
23842 else
23843 return expr;
23845 else
23846 return NULL_TREE;
23849 /* Parse an (optional) exception-specification.
23851 exception-specification:
23852 throw ( type-id-list [opt] )
23854 Returns a TREE_LIST representing the exception-specification. The
23855 TREE_VALUE of each node is a type. */
23857 static tree
23858 cp_parser_exception_specification_opt (cp_parser* parser)
23860 cp_token *token;
23861 tree type_id_list;
23862 const char *saved_message;
23864 /* Peek at the next token. */
23865 token = cp_lexer_peek_token (parser->lexer);
23867 /* Is it a noexcept-specification? */
23868 type_id_list = cp_parser_noexcept_specification_opt (parser, true, NULL,
23869 false);
23870 if (type_id_list != NULL_TREE)
23871 return type_id_list;
23873 /* If it's not `throw', then there's no exception-specification. */
23874 if (!cp_parser_is_keyword (token, RID_THROW))
23875 return NULL_TREE;
23877 location_t loc = token->location;
23879 /* Consume the `throw'. */
23880 cp_lexer_consume_token (parser->lexer);
23882 /* Look for the `('. */
23883 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
23885 /* Peek at the next token. */
23886 token = cp_lexer_peek_token (parser->lexer);
23887 /* If it's not a `)', then there is a type-id-list. */
23888 if (token->type != CPP_CLOSE_PAREN)
23890 /* Types may not be defined in an exception-specification. */
23891 saved_message = parser->type_definition_forbidden_message;
23892 parser->type_definition_forbidden_message
23893 = G_("types may not be defined in an exception-specification");
23894 /* Parse the type-id-list. */
23895 type_id_list = cp_parser_type_id_list (parser);
23896 /* Restore the saved message. */
23897 parser->type_definition_forbidden_message = saved_message;
23899 if (cxx_dialect >= cxx1z)
23901 error_at (loc, "ISO C++1z does not allow dynamic exception "
23902 "specifications");
23903 type_id_list = NULL_TREE;
23905 else if (cxx_dialect >= cxx11 && !in_system_header_at (loc))
23906 warning_at (loc, OPT_Wdeprecated,
23907 "dynamic exception specifications are deprecated in "
23908 "C++11");
23910 /* In C++17, throw() is equivalent to noexcept (true). throw()
23911 is deprecated in C++11 and above as well, but is still widely used,
23912 so don't warn about it yet. */
23913 else if (cxx_dialect >= cxx1z)
23914 type_id_list = noexcept_true_spec;
23915 else
23916 type_id_list = empty_except_spec;
23918 /* Look for the `)'. */
23919 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
23921 return type_id_list;
23924 /* Parse an (optional) type-id-list.
23926 type-id-list:
23927 type-id ... [opt]
23928 type-id-list , type-id ... [opt]
23930 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
23931 in the order that the types were presented. */
23933 static tree
23934 cp_parser_type_id_list (cp_parser* parser)
23936 tree types = NULL_TREE;
23938 while (true)
23940 cp_token *token;
23941 tree type;
23943 token = cp_lexer_peek_token (parser->lexer);
23945 /* Get the next type-id. */
23946 type = cp_parser_type_id (parser);
23947 /* Check for invalid 'auto'. */
23948 if (flag_concepts && type_uses_auto (type))
23950 error_at (token->location,
23951 "invalid use of %<auto%> in exception-specification");
23952 type = error_mark_node;
23954 /* Parse the optional ellipsis. */
23955 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
23957 /* Consume the `...'. */
23958 cp_lexer_consume_token (parser->lexer);
23960 /* Turn the type into a pack expansion expression. */
23961 type = make_pack_expansion (type);
23963 /* Add it to the list. */
23964 types = add_exception_specifier (types, type, /*complain=*/1);
23965 /* Peek at the next token. */
23966 token = cp_lexer_peek_token (parser->lexer);
23967 /* If it is not a `,', we are done. */
23968 if (token->type != CPP_COMMA)
23969 break;
23970 /* Consume the `,'. */
23971 cp_lexer_consume_token (parser->lexer);
23974 return nreverse (types);
23977 /* Parse a try-block.
23979 try-block:
23980 try compound-statement handler-seq */
23982 static tree
23983 cp_parser_try_block (cp_parser* parser)
23985 tree try_block;
23987 cp_parser_require_keyword (parser, RID_TRY, RT_TRY);
23988 if (parser->in_function_body
23989 && DECL_DECLARED_CONSTEXPR_P (current_function_decl))
23990 error ("%<try%> in %<constexpr%> function");
23992 try_block = begin_try_block ();
23993 cp_parser_compound_statement (parser, NULL, BCS_TRY_BLOCK, false);
23994 finish_try_block (try_block);
23995 cp_parser_handler_seq (parser);
23996 finish_handler_sequence (try_block);
23998 return try_block;
24001 /* Parse a function-try-block.
24003 function-try-block:
24004 try ctor-initializer [opt] function-body handler-seq */
24006 static bool
24007 cp_parser_function_try_block (cp_parser* parser)
24009 tree compound_stmt;
24010 tree try_block;
24011 bool ctor_initializer_p;
24013 /* Look for the `try' keyword. */
24014 if (!cp_parser_require_keyword (parser, RID_TRY, RT_TRY))
24015 return false;
24016 /* Let the rest of the front end know where we are. */
24017 try_block = begin_function_try_block (&compound_stmt);
24018 /* Parse the function-body. */
24019 ctor_initializer_p = cp_parser_ctor_initializer_opt_and_function_body
24020 (parser, /*in_function_try_block=*/true);
24021 /* We're done with the `try' part. */
24022 finish_function_try_block (try_block);
24023 /* Parse the handlers. */
24024 cp_parser_handler_seq (parser);
24025 /* We're done with the handlers. */
24026 finish_function_handler_sequence (try_block, compound_stmt);
24028 return ctor_initializer_p;
24031 /* Parse a handler-seq.
24033 handler-seq:
24034 handler handler-seq [opt] */
24036 static void
24037 cp_parser_handler_seq (cp_parser* parser)
24039 while (true)
24041 cp_token *token;
24043 /* Parse the handler. */
24044 cp_parser_handler (parser);
24045 /* Peek at the next token. */
24046 token = cp_lexer_peek_token (parser->lexer);
24047 /* If it's not `catch' then there are no more handlers. */
24048 if (!cp_parser_is_keyword (token, RID_CATCH))
24049 break;
24053 /* Parse a handler.
24055 handler:
24056 catch ( exception-declaration ) compound-statement */
24058 static void
24059 cp_parser_handler (cp_parser* parser)
24061 tree handler;
24062 tree declaration;
24064 cp_parser_require_keyword (parser, RID_CATCH, RT_CATCH);
24065 handler = begin_handler ();
24066 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
24067 declaration = cp_parser_exception_declaration (parser);
24068 finish_handler_parms (declaration, handler);
24069 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
24070 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
24071 finish_handler (handler);
24074 /* Parse an exception-declaration.
24076 exception-declaration:
24077 type-specifier-seq declarator
24078 type-specifier-seq abstract-declarator
24079 type-specifier-seq
24082 Returns a VAR_DECL for the declaration, or NULL_TREE if the
24083 ellipsis variant is used. */
24085 static tree
24086 cp_parser_exception_declaration (cp_parser* parser)
24088 cp_decl_specifier_seq type_specifiers;
24089 cp_declarator *declarator;
24090 const char *saved_message;
24092 /* If it's an ellipsis, it's easy to handle. */
24093 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
24095 /* Consume the `...' token. */
24096 cp_lexer_consume_token (parser->lexer);
24097 return NULL_TREE;
24100 /* Types may not be defined in exception-declarations. */
24101 saved_message = parser->type_definition_forbidden_message;
24102 parser->type_definition_forbidden_message
24103 = G_("types may not be defined in exception-declarations");
24105 /* Parse the type-specifier-seq. */
24106 cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
24107 /*is_trailing_return=*/false,
24108 &type_specifiers);
24109 /* If it's a `)', then there is no declarator. */
24110 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
24111 declarator = NULL;
24112 else
24113 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
24114 /*ctor_dtor_or_conv_p=*/NULL,
24115 /*parenthesized_p=*/NULL,
24116 /*member_p=*/false,
24117 /*friend_p=*/false);
24119 /* Restore the saved message. */
24120 parser->type_definition_forbidden_message = saved_message;
24122 if (!type_specifiers.any_specifiers_p)
24123 return error_mark_node;
24125 return grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
24128 /* Parse a throw-expression.
24130 throw-expression:
24131 throw assignment-expression [opt]
24133 Returns a THROW_EXPR representing the throw-expression. */
24135 static tree
24136 cp_parser_throw_expression (cp_parser* parser)
24138 tree expression;
24139 cp_token* token;
24141 cp_parser_require_keyword (parser, RID_THROW, RT_THROW);
24142 token = cp_lexer_peek_token (parser->lexer);
24143 /* Figure out whether or not there is an assignment-expression
24144 following the "throw" keyword. */
24145 if (token->type == CPP_COMMA
24146 || token->type == CPP_SEMICOLON
24147 || token->type == CPP_CLOSE_PAREN
24148 || token->type == CPP_CLOSE_SQUARE
24149 || token->type == CPP_CLOSE_BRACE
24150 || token->type == CPP_COLON)
24151 expression = NULL_TREE;
24152 else
24153 expression = cp_parser_assignment_expression (parser);
24155 return build_throw (expression);
24158 /* GNU Extensions */
24160 /* Parse an (optional) asm-specification.
24162 asm-specification:
24163 asm ( string-literal )
24165 If the asm-specification is present, returns a STRING_CST
24166 corresponding to the string-literal. Otherwise, returns
24167 NULL_TREE. */
24169 static tree
24170 cp_parser_asm_specification_opt (cp_parser* parser)
24172 cp_token *token;
24173 tree asm_specification;
24175 /* Peek at the next token. */
24176 token = cp_lexer_peek_token (parser->lexer);
24177 /* If the next token isn't the `asm' keyword, then there's no
24178 asm-specification. */
24179 if (!cp_parser_is_keyword (token, RID_ASM))
24180 return NULL_TREE;
24182 /* Consume the `asm' token. */
24183 cp_lexer_consume_token (parser->lexer);
24184 /* Look for the `('. */
24185 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
24187 /* Look for the string-literal. */
24188 asm_specification = cp_parser_string_literal (parser, false, false);
24190 /* Look for the `)'. */
24191 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
24193 return asm_specification;
24196 /* Parse an asm-operand-list.
24198 asm-operand-list:
24199 asm-operand
24200 asm-operand-list , asm-operand
24202 asm-operand:
24203 string-literal ( expression )
24204 [ string-literal ] string-literal ( expression )
24206 Returns a TREE_LIST representing the operands. The TREE_VALUE of
24207 each node is the expression. The TREE_PURPOSE is itself a
24208 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
24209 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
24210 is a STRING_CST for the string literal before the parenthesis. Returns
24211 ERROR_MARK_NODE if any of the operands are invalid. */
24213 static tree
24214 cp_parser_asm_operand_list (cp_parser* parser)
24216 tree asm_operands = NULL_TREE;
24217 bool invalid_operands = false;
24219 while (true)
24221 tree string_literal;
24222 tree expression;
24223 tree name;
24225 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
24227 /* Consume the `[' token. */
24228 cp_lexer_consume_token (parser->lexer);
24229 /* Read the operand name. */
24230 name = cp_parser_identifier (parser);
24231 if (name != error_mark_node)
24232 name = build_string (IDENTIFIER_LENGTH (name),
24233 IDENTIFIER_POINTER (name));
24234 /* Look for the closing `]'. */
24235 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
24237 else
24238 name = NULL_TREE;
24239 /* Look for the string-literal. */
24240 string_literal = cp_parser_string_literal (parser, false, false);
24242 /* Look for the `('. */
24243 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
24244 /* Parse the expression. */
24245 expression = cp_parser_expression (parser);
24246 /* Look for the `)'. */
24247 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
24249 if (name == error_mark_node
24250 || string_literal == error_mark_node
24251 || expression == error_mark_node)
24252 invalid_operands = true;
24254 /* Add this operand to the list. */
24255 asm_operands = tree_cons (build_tree_list (name, string_literal),
24256 expression,
24257 asm_operands);
24258 /* If the next token is not a `,', there are no more
24259 operands. */
24260 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
24261 break;
24262 /* Consume the `,'. */
24263 cp_lexer_consume_token (parser->lexer);
24266 return invalid_operands ? error_mark_node : nreverse (asm_operands);
24269 /* Parse an asm-clobber-list.
24271 asm-clobber-list:
24272 string-literal
24273 asm-clobber-list , string-literal
24275 Returns a TREE_LIST, indicating the clobbers in the order that they
24276 appeared. The TREE_VALUE of each node is a STRING_CST. */
24278 static tree
24279 cp_parser_asm_clobber_list (cp_parser* parser)
24281 tree clobbers = NULL_TREE;
24283 while (true)
24285 tree string_literal;
24287 /* Look for the string literal. */
24288 string_literal = cp_parser_string_literal (parser, false, false);
24289 /* Add it to the list. */
24290 clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
24291 /* If the next token is not a `,', then the list is
24292 complete. */
24293 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
24294 break;
24295 /* Consume the `,' token. */
24296 cp_lexer_consume_token (parser->lexer);
24299 return clobbers;
24302 /* Parse an asm-label-list.
24304 asm-label-list:
24305 identifier
24306 asm-label-list , identifier
24308 Returns a TREE_LIST, indicating the labels in the order that they
24309 appeared. The TREE_VALUE of each node is a label. */
24311 static tree
24312 cp_parser_asm_label_list (cp_parser* parser)
24314 tree labels = NULL_TREE;
24316 while (true)
24318 tree identifier, label, name;
24320 /* Look for the identifier. */
24321 identifier = cp_parser_identifier (parser);
24322 if (!error_operand_p (identifier))
24324 label = lookup_label (identifier);
24325 if (TREE_CODE (label) == LABEL_DECL)
24327 TREE_USED (label) = 1;
24328 check_goto (label);
24329 name = build_string (IDENTIFIER_LENGTH (identifier),
24330 IDENTIFIER_POINTER (identifier));
24331 labels = tree_cons (name, label, labels);
24334 /* If the next token is not a `,', then the list is
24335 complete. */
24336 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
24337 break;
24338 /* Consume the `,' token. */
24339 cp_lexer_consume_token (parser->lexer);
24342 return nreverse (labels);
24345 /* Return TRUE iff the next tokens in the stream are possibly the
24346 beginning of a GNU extension attribute. */
24348 static bool
24349 cp_next_tokens_can_be_gnu_attribute_p (cp_parser *parser)
24351 return cp_nth_tokens_can_be_gnu_attribute_p (parser, 1);
24354 /* Return TRUE iff the next tokens in the stream are possibly the
24355 beginning of a standard C++-11 attribute specifier. */
24357 static bool
24358 cp_next_tokens_can_be_std_attribute_p (cp_parser *parser)
24360 return cp_nth_tokens_can_be_std_attribute_p (parser, 1);
24363 /* Return TRUE iff the next Nth tokens in the stream are possibly the
24364 beginning of a standard C++-11 attribute specifier. */
24366 static bool
24367 cp_nth_tokens_can_be_std_attribute_p (cp_parser *parser, size_t n)
24369 cp_token *token = cp_lexer_peek_nth_token (parser->lexer, n);
24371 return (cxx_dialect >= cxx11
24372 && ((token->type == CPP_KEYWORD && token->keyword == RID_ALIGNAS)
24373 || (token->type == CPP_OPEN_SQUARE
24374 && (token = cp_lexer_peek_nth_token (parser->lexer, n + 1))
24375 && token->type == CPP_OPEN_SQUARE)));
24378 /* Return TRUE iff the next Nth tokens in the stream are possibly the
24379 beginning of a GNU extension attribute. */
24381 static bool
24382 cp_nth_tokens_can_be_gnu_attribute_p (cp_parser *parser, size_t n)
24384 cp_token *token = cp_lexer_peek_nth_token (parser->lexer, n);
24386 return token->type == CPP_KEYWORD && token->keyword == RID_ATTRIBUTE;
24389 /* Return true iff the next tokens can be the beginning of either a
24390 GNU attribute list, or a standard C++11 attribute sequence. */
24392 static bool
24393 cp_next_tokens_can_be_attribute_p (cp_parser *parser)
24395 return (cp_next_tokens_can_be_gnu_attribute_p (parser)
24396 || cp_next_tokens_can_be_std_attribute_p (parser));
24399 /* Return true iff the next Nth tokens can be the beginning of either
24400 a GNU attribute list, or a standard C++11 attribute sequence. */
24402 static bool
24403 cp_nth_tokens_can_be_attribute_p (cp_parser *parser, size_t n)
24405 return (cp_nth_tokens_can_be_gnu_attribute_p (parser, n)
24406 || cp_nth_tokens_can_be_std_attribute_p (parser, n));
24409 /* Parse either a standard C++-11 attribute-specifier-seq, or a series
24410 of GNU attributes, or return NULL. */
24412 static tree
24413 cp_parser_attributes_opt (cp_parser *parser)
24415 if (cp_next_tokens_can_be_gnu_attribute_p (parser))
24416 return cp_parser_gnu_attributes_opt (parser);
24417 return cp_parser_std_attribute_spec_seq (parser);
24420 #define CILK_SIMD_FN_CLAUSE_MASK \
24421 ((OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_VECTORLENGTH) \
24422 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_LINEAR) \
24423 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_UNIFORM) \
24424 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_MASK) \
24425 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_NOMASK))
24427 /* Parses the Cilk Plus SIMD-enabled function's attribute. Syntax:
24428 vector [(<clauses>)] */
24430 static void
24431 cp_parser_cilk_simd_fn_vector_attrs (cp_parser *parser, cp_token *v_token)
24433 bool first_p = parser->cilk_simd_fn_info == NULL;
24434 cp_token *token = v_token;
24435 if (first_p)
24437 parser->cilk_simd_fn_info = XNEW (cp_omp_declare_simd_data);
24438 parser->cilk_simd_fn_info->error_seen = false;
24439 parser->cilk_simd_fn_info->fndecl_seen = false;
24440 parser->cilk_simd_fn_info->tokens = vNULL;
24441 parser->cilk_simd_fn_info->clauses = NULL_TREE;
24443 int paren_scope = 0;
24444 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
24446 cp_lexer_consume_token (parser->lexer);
24447 v_token = cp_lexer_peek_token (parser->lexer);
24448 paren_scope++;
24450 while (paren_scope > 0)
24452 token = cp_lexer_peek_token (parser->lexer);
24453 if (token->type == CPP_OPEN_PAREN)
24454 paren_scope++;
24455 else if (token->type == CPP_CLOSE_PAREN)
24456 paren_scope--;
24457 /* Do not push the last ')' */
24458 if (!(token->type == CPP_CLOSE_PAREN && paren_scope == 0))
24459 cp_lexer_consume_token (parser->lexer);
24462 token->type = CPP_PRAGMA_EOL;
24463 parser->lexer->next_token = token;
24464 cp_lexer_consume_token (parser->lexer);
24466 struct cp_token_cache *cp
24467 = cp_token_cache_new (v_token, cp_lexer_peek_token (parser->lexer));
24468 parser->cilk_simd_fn_info->tokens.safe_push (cp);
24471 /* Parse an (optional) series of attributes.
24473 attributes:
24474 attributes attribute
24476 attribute:
24477 __attribute__ (( attribute-list [opt] ))
24479 The return value is as for cp_parser_gnu_attribute_list. */
24481 static tree
24482 cp_parser_gnu_attributes_opt (cp_parser* parser)
24484 tree attributes = NULL_TREE;
24486 while (true)
24488 cp_token *token;
24489 tree attribute_list;
24490 bool ok = true;
24492 /* Peek at the next token. */
24493 token = cp_lexer_peek_token (parser->lexer);
24494 /* If it's not `__attribute__', then we're done. */
24495 if (token->keyword != RID_ATTRIBUTE)
24496 break;
24498 /* Consume the `__attribute__' keyword. */
24499 cp_lexer_consume_token (parser->lexer);
24500 /* Look for the two `(' tokens. */
24501 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
24502 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
24504 /* Peek at the next token. */
24505 token = cp_lexer_peek_token (parser->lexer);
24506 if (token->type != CPP_CLOSE_PAREN)
24507 /* Parse the attribute-list. */
24508 attribute_list = cp_parser_gnu_attribute_list (parser);
24509 else
24510 /* If the next token is a `)', then there is no attribute
24511 list. */
24512 attribute_list = NULL;
24514 /* Look for the two `)' tokens. */
24515 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
24516 ok = false;
24517 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
24518 ok = false;
24519 if (!ok)
24520 cp_parser_skip_to_end_of_statement (parser);
24522 /* Add these new attributes to the list. */
24523 attributes = chainon (attributes, attribute_list);
24526 return attributes;
24529 /* Parse a GNU attribute-list.
24531 attribute-list:
24532 attribute
24533 attribute-list , attribute
24535 attribute:
24536 identifier
24537 identifier ( identifier )
24538 identifier ( identifier , expression-list )
24539 identifier ( expression-list )
24541 Returns a TREE_LIST, or NULL_TREE on error. Each node corresponds
24542 to an attribute. The TREE_PURPOSE of each node is the identifier
24543 indicating which attribute is in use. The TREE_VALUE represents
24544 the arguments, if any. */
24546 static tree
24547 cp_parser_gnu_attribute_list (cp_parser* parser)
24549 tree attribute_list = NULL_TREE;
24550 bool save_translate_strings_p = parser->translate_strings_p;
24552 parser->translate_strings_p = false;
24553 while (true)
24555 cp_token *token;
24556 tree identifier;
24557 tree attribute;
24559 /* Look for the identifier. We also allow keywords here; for
24560 example `__attribute__ ((const))' is legal. */
24561 token = cp_lexer_peek_token (parser->lexer);
24562 if (token->type == CPP_NAME
24563 || token->type == CPP_KEYWORD)
24565 tree arguments = NULL_TREE;
24567 /* Consume the token, but save it since we need it for the
24568 SIMD enabled function parsing. */
24569 cp_token *id_token = cp_lexer_consume_token (parser->lexer);
24571 /* Save away the identifier that indicates which attribute
24572 this is. */
24573 identifier = (token->type == CPP_KEYWORD)
24574 /* For keywords, use the canonical spelling, not the
24575 parsed identifier. */
24576 ? ridpointers[(int) token->keyword]
24577 : id_token->u.value;
24579 attribute = build_tree_list (identifier, NULL_TREE);
24581 /* Peek at the next token. */
24582 token = cp_lexer_peek_token (parser->lexer);
24583 /* If it's an `(', then parse the attribute arguments. */
24584 if (token->type == CPP_OPEN_PAREN)
24586 vec<tree, va_gc> *vec;
24587 int attr_flag = (attribute_takes_identifier_p (identifier)
24588 ? id_attr : normal_attr);
24589 if (is_cilkplus_vector_p (identifier))
24591 cp_parser_cilk_simd_fn_vector_attrs (parser, id_token);
24592 continue;
24594 else
24595 vec = cp_parser_parenthesized_expression_list
24596 (parser, attr_flag, /*cast_p=*/false,
24597 /*allow_expansion_p=*/false,
24598 /*non_constant_p=*/NULL);
24599 if (vec == NULL)
24600 arguments = error_mark_node;
24601 else
24603 arguments = build_tree_list_vec (vec);
24604 release_tree_vector (vec);
24606 /* Save the arguments away. */
24607 TREE_VALUE (attribute) = arguments;
24609 else if (is_cilkplus_vector_p (identifier))
24611 cp_parser_cilk_simd_fn_vector_attrs (parser, id_token);
24612 continue;
24615 if (arguments != error_mark_node)
24617 /* Add this attribute to the list. */
24618 TREE_CHAIN (attribute) = attribute_list;
24619 attribute_list = attribute;
24622 token = cp_lexer_peek_token (parser->lexer);
24624 /* Now, look for more attributes. If the next token isn't a
24625 `,', we're done. */
24626 if (token->type != CPP_COMMA)
24627 break;
24629 /* Consume the comma and keep going. */
24630 cp_lexer_consume_token (parser->lexer);
24632 parser->translate_strings_p = save_translate_strings_p;
24634 /* We built up the list in reverse order. */
24635 return nreverse (attribute_list);
24638 /* Parse a standard C++11 attribute.
24640 The returned representation is a TREE_LIST which TREE_PURPOSE is
24641 the scoped name of the attribute, and the TREE_VALUE is its
24642 arguments list.
24644 Note that the scoped name of the attribute is itself a TREE_LIST
24645 which TREE_PURPOSE is the namespace of the attribute, and
24646 TREE_VALUE its name. This is unlike a GNU attribute -- as parsed
24647 by cp_parser_gnu_attribute_list -- that doesn't have any namespace
24648 and which TREE_PURPOSE is directly the attribute name.
24650 Clients of the attribute code should use get_attribute_namespace
24651 and get_attribute_name to get the actual namespace and name of
24652 attributes, regardless of their being GNU or C++11 attributes.
24654 attribute:
24655 attribute-token attribute-argument-clause [opt]
24657 attribute-token:
24658 identifier
24659 attribute-scoped-token
24661 attribute-scoped-token:
24662 attribute-namespace :: identifier
24664 attribute-namespace:
24665 identifier
24667 attribute-argument-clause:
24668 ( balanced-token-seq )
24670 balanced-token-seq:
24671 balanced-token [opt]
24672 balanced-token-seq balanced-token
24674 balanced-token:
24675 ( balanced-token-seq )
24676 [ balanced-token-seq ]
24677 { balanced-token-seq }. */
24679 static tree
24680 cp_parser_std_attribute (cp_parser *parser, tree attr_ns)
24682 tree attribute, attr_id = NULL_TREE, arguments;
24683 cp_token *token;
24685 /* First, parse name of the attribute, a.k.a attribute-token. */
24687 token = cp_lexer_peek_token (parser->lexer);
24688 if (token->type == CPP_NAME)
24689 attr_id = token->u.value;
24690 else if (token->type == CPP_KEYWORD)
24691 attr_id = ridpointers[(int) token->keyword];
24692 else if (token->flags & NAMED_OP)
24693 attr_id = get_identifier (cpp_type2name (token->type, token->flags));
24695 if (attr_id == NULL_TREE)
24696 return NULL_TREE;
24698 cp_lexer_consume_token (parser->lexer);
24700 token = cp_lexer_peek_token (parser->lexer);
24701 if (token->type == CPP_SCOPE)
24703 /* We are seeing a scoped attribute token. */
24705 cp_lexer_consume_token (parser->lexer);
24706 if (attr_ns)
24707 error_at (token->location, "attribute using prefix used together "
24708 "with scoped attribute token");
24709 attr_ns = attr_id;
24711 token = cp_lexer_consume_token (parser->lexer);
24712 if (token->type == CPP_NAME)
24713 attr_id = token->u.value;
24714 else if (token->type == CPP_KEYWORD)
24715 attr_id = ridpointers[(int) token->keyword];
24716 else if (token->flags & NAMED_OP)
24717 attr_id = get_identifier (cpp_type2name (token->type, token->flags));
24718 else
24720 error_at (token->location,
24721 "expected an identifier for the attribute name");
24722 return error_mark_node;
24724 attribute = build_tree_list (build_tree_list (attr_ns, attr_id),
24725 NULL_TREE);
24726 token = cp_lexer_peek_token (parser->lexer);
24728 else if (attr_ns)
24729 attribute = build_tree_list (build_tree_list (attr_ns, attr_id),
24730 NULL_TREE);
24731 else
24733 attribute = build_tree_list (build_tree_list (NULL_TREE, attr_id),
24734 NULL_TREE);
24735 /* C++11 noreturn attribute is equivalent to GNU's. */
24736 if (is_attribute_p ("noreturn", attr_id))
24737 TREE_PURPOSE (TREE_PURPOSE (attribute)) = get_identifier ("gnu");
24738 /* C++14 deprecated attribute is equivalent to GNU's. */
24739 else if (is_attribute_p ("deprecated", attr_id))
24740 TREE_PURPOSE (TREE_PURPOSE (attribute)) = get_identifier ("gnu");
24741 /* C++17 fallthrough attribute is equivalent to GNU's. */
24742 else if (is_attribute_p ("fallthrough", attr_id))
24743 TREE_PURPOSE (TREE_PURPOSE (attribute)) = get_identifier ("gnu");
24744 /* Transactional Memory TS optimize_for_synchronized attribute is
24745 equivalent to GNU transaction_callable. */
24746 else if (is_attribute_p ("optimize_for_synchronized", attr_id))
24747 TREE_PURPOSE (attribute)
24748 = get_identifier ("transaction_callable");
24749 /* Transactional Memory attributes are GNU attributes. */
24750 else if (tm_attr_to_mask (attr_id))
24751 TREE_PURPOSE (attribute) = attr_id;
24754 /* Now parse the optional argument clause of the attribute. */
24756 if (token->type != CPP_OPEN_PAREN)
24757 return attribute;
24760 vec<tree, va_gc> *vec;
24761 int attr_flag = normal_attr;
24763 if (attr_ns == get_identifier ("gnu")
24764 && attribute_takes_identifier_p (attr_id))
24765 /* A GNU attribute that takes an identifier in parameter. */
24766 attr_flag = id_attr;
24768 vec = cp_parser_parenthesized_expression_list
24769 (parser, attr_flag, /*cast_p=*/false,
24770 /*allow_expansion_p=*/true,
24771 /*non_constant_p=*/NULL);
24772 if (vec == NULL)
24773 arguments = error_mark_node;
24774 else
24776 arguments = build_tree_list_vec (vec);
24777 release_tree_vector (vec);
24780 if (arguments == error_mark_node)
24781 attribute = error_mark_node;
24782 else
24783 TREE_VALUE (attribute) = arguments;
24786 return attribute;
24789 /* Check that the attribute ATTRIBUTE appears at most once in the
24790 attribute-list ATTRIBUTES. This is enforced for noreturn (7.6.3)
24791 and deprecated (7.6.5). Note that carries_dependency (7.6.4)
24792 isn't implemented yet in GCC. */
24794 static void
24795 cp_parser_check_std_attribute (tree attributes, tree attribute)
24797 if (attributes)
24799 tree name = get_attribute_name (attribute);
24800 if (is_attribute_p ("noreturn", name)
24801 && lookup_attribute ("noreturn", attributes))
24802 error ("attribute %<noreturn%> can appear at most once "
24803 "in an attribute-list");
24804 else if (is_attribute_p ("deprecated", name)
24805 && lookup_attribute ("deprecated", attributes))
24806 error ("attribute %<deprecated%> can appear at most once "
24807 "in an attribute-list");
24811 /* Parse a list of standard C++-11 attributes.
24813 attribute-list:
24814 attribute [opt]
24815 attribute-list , attribute[opt]
24816 attribute ...
24817 attribute-list , attribute ...
24820 static tree
24821 cp_parser_std_attribute_list (cp_parser *parser, tree attr_ns)
24823 tree attributes = NULL_TREE, attribute = NULL_TREE;
24824 cp_token *token = NULL;
24826 while (true)
24828 attribute = cp_parser_std_attribute (parser, attr_ns);
24829 if (attribute == error_mark_node)
24830 break;
24831 if (attribute != NULL_TREE)
24833 cp_parser_check_std_attribute (attributes, attribute);
24834 TREE_CHAIN (attribute) = attributes;
24835 attributes = attribute;
24837 token = cp_lexer_peek_token (parser->lexer);
24838 if (token->type == CPP_ELLIPSIS)
24840 cp_lexer_consume_token (parser->lexer);
24841 if (attribute == NULL_TREE)
24842 error_at (token->location,
24843 "expected attribute before %<...%>");
24844 else
24845 TREE_VALUE (attribute)
24846 = make_pack_expansion (TREE_VALUE (attribute));
24847 token = cp_lexer_peek_token (parser->lexer);
24849 if (token->type != CPP_COMMA)
24850 break;
24851 cp_lexer_consume_token (parser->lexer);
24853 attributes = nreverse (attributes);
24854 return attributes;
24857 /* Parse a standard C++-11 attribute specifier.
24859 attribute-specifier:
24860 [ [ attribute-using-prefix [opt] attribute-list ] ]
24861 alignment-specifier
24863 attribute-using-prefix:
24864 using attribute-namespace :
24866 alignment-specifier:
24867 alignas ( type-id ... [opt] )
24868 alignas ( alignment-expression ... [opt] ). */
24870 static tree
24871 cp_parser_std_attribute_spec (cp_parser *parser)
24873 tree attributes = NULL_TREE;
24874 cp_token *token = cp_lexer_peek_token (parser->lexer);
24876 if (token->type == CPP_OPEN_SQUARE
24877 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_OPEN_SQUARE)
24879 tree attr_ns = NULL_TREE;
24881 cp_lexer_consume_token (parser->lexer);
24882 cp_lexer_consume_token (parser->lexer);
24884 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
24886 token = cp_lexer_peek_nth_token (parser->lexer, 2);
24887 if (token->type == CPP_NAME)
24888 attr_ns = token->u.value;
24889 else if (token->type == CPP_KEYWORD)
24890 attr_ns = ridpointers[(int) token->keyword];
24891 else if (token->flags & NAMED_OP)
24892 attr_ns = get_identifier (cpp_type2name (token->type,
24893 token->flags));
24894 if (attr_ns
24895 && cp_lexer_nth_token_is (parser->lexer, 3, CPP_COLON))
24897 if (cxx_dialect < cxx1z
24898 && !in_system_header_at (input_location))
24899 pedwarn (input_location, 0,
24900 "attribute using prefix only available "
24901 "with -std=c++1z or -std=gnu++1z");
24903 cp_lexer_consume_token (parser->lexer);
24904 cp_lexer_consume_token (parser->lexer);
24905 cp_lexer_consume_token (parser->lexer);
24907 else
24908 attr_ns = NULL_TREE;
24911 attributes = cp_parser_std_attribute_list (parser, attr_ns);
24913 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE)
24914 || !cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
24915 cp_parser_skip_to_end_of_statement (parser);
24916 else
24917 /* Warn about parsing c++11 attribute in non-c++1 mode, only
24918 when we are sure that we have actually parsed them. */
24919 maybe_warn_cpp0x (CPP0X_ATTRIBUTES);
24921 else
24923 tree alignas_expr;
24925 /* Look for an alignment-specifier. */
24927 token = cp_lexer_peek_token (parser->lexer);
24929 if (token->type != CPP_KEYWORD
24930 || token->keyword != RID_ALIGNAS)
24931 return NULL_TREE;
24933 cp_lexer_consume_token (parser->lexer);
24934 maybe_warn_cpp0x (CPP0X_ATTRIBUTES);
24936 if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN) == NULL)
24938 cp_parser_error (parser, "expected %<(%>");
24939 return error_mark_node;
24942 cp_parser_parse_tentatively (parser);
24943 alignas_expr = cp_parser_type_id (parser);
24945 if (!cp_parser_parse_definitely (parser))
24947 alignas_expr = cp_parser_assignment_expression (parser);
24948 if (alignas_expr == error_mark_node)
24949 cp_parser_skip_to_end_of_statement (parser);
24950 if (alignas_expr == NULL_TREE
24951 || alignas_expr == error_mark_node)
24952 return alignas_expr;
24955 alignas_expr = cxx_alignas_expr (alignas_expr);
24956 alignas_expr = build_tree_list (NULL_TREE, alignas_expr);
24958 /* Handle alignas (pack...). */
24959 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
24961 cp_lexer_consume_token (parser->lexer);
24962 alignas_expr = make_pack_expansion (alignas_expr);
24965 /* Something went wrong, so don't build the attribute. */
24966 if (alignas_expr == error_mark_node)
24967 return error_mark_node;
24969 if (cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN) == NULL)
24971 cp_parser_error (parser, "expected %<)%>");
24972 return error_mark_node;
24975 /* Build the C++-11 representation of an 'aligned'
24976 attribute. */
24977 attributes =
24978 build_tree_list (build_tree_list (get_identifier ("gnu"),
24979 get_identifier ("aligned")),
24980 alignas_expr);
24983 return attributes;
24986 /* Parse a standard C++-11 attribute-specifier-seq.
24988 attribute-specifier-seq:
24989 attribute-specifier-seq [opt] attribute-specifier
24992 static tree
24993 cp_parser_std_attribute_spec_seq (cp_parser *parser)
24995 tree attr_specs = NULL_TREE;
24996 tree attr_last = NULL_TREE;
24998 while (true)
25000 tree attr_spec = cp_parser_std_attribute_spec (parser);
25001 if (attr_spec == NULL_TREE)
25002 break;
25003 if (attr_spec == error_mark_node)
25004 return error_mark_node;
25006 if (attr_last)
25007 TREE_CHAIN (attr_last) = attr_spec;
25008 else
25009 attr_specs = attr_last = attr_spec;
25010 attr_last = tree_last (attr_last);
25013 return attr_specs;
25016 /* Parse an optional `__extension__' keyword. Returns TRUE if it is
25017 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
25018 current value of the PEDANTIC flag, regardless of whether or not
25019 the `__extension__' keyword is present. The caller is responsible
25020 for restoring the value of the PEDANTIC flag. */
25022 static bool
25023 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
25025 /* Save the old value of the PEDANTIC flag. */
25026 *saved_pedantic = pedantic;
25028 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
25030 /* Consume the `__extension__' token. */
25031 cp_lexer_consume_token (parser->lexer);
25032 /* We're not being pedantic while the `__extension__' keyword is
25033 in effect. */
25034 pedantic = 0;
25036 return true;
25039 return false;
25042 /* Parse a label declaration.
25044 label-declaration:
25045 __label__ label-declarator-seq ;
25047 label-declarator-seq:
25048 identifier , label-declarator-seq
25049 identifier */
25051 static void
25052 cp_parser_label_declaration (cp_parser* parser)
25054 /* Look for the `__label__' keyword. */
25055 cp_parser_require_keyword (parser, RID_LABEL, RT_LABEL);
25057 while (true)
25059 tree identifier;
25061 /* Look for an identifier. */
25062 identifier = cp_parser_identifier (parser);
25063 /* If we failed, stop. */
25064 if (identifier == error_mark_node)
25065 break;
25066 /* Declare it as a label. */
25067 finish_label_decl (identifier);
25068 /* If the next token is a `;', stop. */
25069 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
25070 break;
25071 /* Look for the `,' separating the label declarations. */
25072 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
25075 /* Look for the final `;'. */
25076 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
25079 // -------------------------------------------------------------------------- //
25080 // Requires Clause
25082 // Parse a requires clause.
25084 // requires-clause:
25085 // 'requires' logical-or-expression
25087 // The required logical-or-expression must be a constant expression. Note
25088 // that we don't check that the expression is constepxr here. We defer until
25089 // we analyze constraints and then, we only check atomic constraints.
25090 static tree
25091 cp_parser_requires_clause (cp_parser *parser)
25093 // Parse the requires clause so that it is not automatically folded.
25094 ++processing_template_decl;
25095 tree expr = cp_parser_binary_expression (parser, false, false,
25096 PREC_NOT_OPERATOR, NULL);
25097 if (check_for_bare_parameter_packs (expr))
25098 expr = error_mark_node;
25099 --processing_template_decl;
25100 return expr;
25103 // Optionally parse a requires clause:
25104 static tree
25105 cp_parser_requires_clause_opt (cp_parser *parser)
25107 cp_token *tok = cp_lexer_peek_token (parser->lexer);
25108 if (tok->keyword != RID_REQUIRES)
25110 if (!flag_concepts && tok->type == CPP_NAME
25111 && tok->u.value == ridpointers[RID_REQUIRES])
25113 error_at (cp_lexer_peek_token (parser->lexer)->location,
25114 "%<requires%> only available with -fconcepts");
25115 /* Parse and discard the requires-clause. */
25116 cp_lexer_consume_token (parser->lexer);
25117 cp_parser_requires_clause (parser);
25119 return NULL_TREE;
25121 cp_lexer_consume_token (parser->lexer);
25122 return cp_parser_requires_clause (parser);
25126 /*---------------------------------------------------------------------------
25127 Requires expressions
25128 ---------------------------------------------------------------------------*/
25130 /* Parse a requires expression
25132 requirement-expression:
25133 'requires' requirement-parameter-list [opt] requirement-body */
25134 static tree
25135 cp_parser_requires_expression (cp_parser *parser)
25137 gcc_assert (cp_lexer_next_token_is_keyword (parser->lexer, RID_REQUIRES));
25138 location_t loc = cp_lexer_consume_token (parser->lexer)->location;
25140 /* A requires-expression shall appear only within a concept
25141 definition or a requires-clause.
25143 TODO: Implement this diagnostic correctly. */
25144 if (!processing_template_decl)
25146 error_at (loc, "a requires expression cannot appear outside a template");
25147 cp_parser_skip_to_end_of_statement (parser);
25148 return error_mark_node;
25151 tree parms, reqs;
25153 /* Local parameters are delared as variables within the scope
25154 of the expression. They are not visible past the end of
25155 the expression. Expressions within the requires-expression
25156 are unevaluated. */
25157 struct scope_sentinel
25159 scope_sentinel ()
25161 ++cp_unevaluated_operand;
25162 begin_scope (sk_block, NULL_TREE);
25165 ~scope_sentinel ()
25167 pop_bindings_and_leave_scope ();
25168 --cp_unevaluated_operand;
25170 } s;
25172 /* Parse the optional parameter list. */
25173 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
25175 parms = cp_parser_requirement_parameter_list (parser);
25176 if (parms == error_mark_node)
25177 return error_mark_node;
25179 else
25180 parms = NULL_TREE;
25182 /* Parse the requirement body. */
25183 reqs = cp_parser_requirement_body (parser);
25184 if (reqs == error_mark_node)
25185 return error_mark_node;
25188 /* This needs to happen after pop_bindings_and_leave_scope, as it reverses
25189 the parm chain. */
25190 grokparms (parms, &parms);
25191 return finish_requires_expr (parms, reqs);
25194 /* Parse a parameterized requirement.
25196 requirement-parameter-list:
25197 '(' parameter-declaration-clause ')' */
25198 static tree
25199 cp_parser_requirement_parameter_list (cp_parser *parser)
25201 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
25202 return error_mark_node;
25204 tree parms = cp_parser_parameter_declaration_clause (parser);
25206 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
25207 return error_mark_node;
25209 return parms;
25212 /* Parse the body of a requirement.
25214 requirement-body:
25215 '{' requirement-list '}' */
25216 static tree
25217 cp_parser_requirement_body (cp_parser *parser)
25219 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
25220 return error_mark_node;
25222 tree reqs = cp_parser_requirement_list (parser);
25224 if (!cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE))
25225 return error_mark_node;
25227 return reqs;
25230 /* Parse a list of requirements.
25232 requirement-list:
25233 requirement
25234 requirement-list ';' requirement[opt] */
25235 static tree
25236 cp_parser_requirement_list (cp_parser *parser)
25238 tree result = NULL_TREE;
25239 while (true)
25241 tree req = cp_parser_requirement (parser);
25242 if (req == error_mark_node)
25243 return error_mark_node;
25245 result = tree_cons (NULL_TREE, req, result);
25247 /* If we see a semi-colon, consume it. */
25248 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
25249 cp_lexer_consume_token (parser->lexer);
25251 /* Stop processing at the end of the list. */
25252 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
25253 break;
25256 /* Reverse the order of requirements so they are analyzed in
25257 declaration order. */
25258 return nreverse (result);
25261 /* Parse a syntactic requirement or type requirement.
25263 requirement:
25264 simple-requirement
25265 compound-requirement
25266 type-requirement
25267 nested-requirement */
25268 static tree
25269 cp_parser_requirement (cp_parser *parser)
25271 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
25272 return cp_parser_compound_requirement (parser);
25273 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
25274 return cp_parser_type_requirement (parser);
25275 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_REQUIRES))
25276 return cp_parser_nested_requirement (parser);
25277 else
25278 return cp_parser_simple_requirement (parser);
25281 /* Parse a simple requirement.
25283 simple-requirement:
25284 expression ';' */
25285 static tree
25286 cp_parser_simple_requirement (cp_parser *parser)
25288 tree expr = cp_parser_expression (parser, NULL, false, false);
25289 if (!expr || expr == error_mark_node)
25290 return error_mark_node;
25292 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
25293 return error_mark_node;
25295 return finish_simple_requirement (expr);
25298 /* Parse a type requirement
25300 type-requirement
25301 nested-name-specifier [opt] required-type-name ';'
25303 required-type-name:
25304 type-name
25305 'template' [opt] simple-template-id */
25306 static tree
25307 cp_parser_type_requirement (cp_parser *parser)
25309 cp_lexer_consume_token (parser->lexer);
25311 // Save the scope before parsing name specifiers.
25312 tree saved_scope = parser->scope;
25313 tree saved_object_scope = parser->object_scope;
25314 tree saved_qualifying_scope = parser->qualifying_scope;
25315 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
25316 cp_parser_nested_name_specifier_opt (parser,
25317 /*typename_keyword_p=*/true,
25318 /*check_dependency_p=*/false,
25319 /*type_p=*/true,
25320 /*is_declaration=*/false);
25322 tree type;
25323 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
25325 cp_lexer_consume_token (parser->lexer);
25326 type = cp_parser_template_id (parser,
25327 /*template_keyword_p=*/true,
25328 /*check_dependency=*/false,
25329 /*tag_type=*/none_type,
25330 /*is_declaration=*/false);
25331 type = make_typename_type (parser->scope, type, typename_type,
25332 /*complain=*/tf_error);
25334 else
25335 type = cp_parser_type_name (parser, /*typename_keyword_p=*/true);
25337 if (TREE_CODE (type) == TYPE_DECL)
25338 type = TREE_TYPE (type);
25340 parser->scope = saved_scope;
25341 parser->object_scope = saved_object_scope;
25342 parser->qualifying_scope = saved_qualifying_scope;
25344 if (type == error_mark_node)
25345 cp_parser_skip_to_end_of_statement (parser);
25347 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
25348 return error_mark_node;
25349 if (type == error_mark_node)
25350 return error_mark_node;
25352 return finish_type_requirement (type);
25355 /* Parse a compound requirement
25357 compound-requirement:
25358 '{' expression '}' 'noexcept' [opt] trailing-return-type [opt] ';' */
25359 static tree
25360 cp_parser_compound_requirement (cp_parser *parser)
25362 /* Parse an expression enclosed in '{ }'s. */
25363 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
25364 return error_mark_node;
25366 tree expr = cp_parser_expression (parser, NULL, false, false);
25367 if (!expr || expr == error_mark_node)
25368 return error_mark_node;
25370 if (!cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE))
25371 return error_mark_node;
25373 /* Parse the optional noexcept. */
25374 bool noexcept_p = false;
25375 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_NOEXCEPT))
25377 cp_lexer_consume_token (parser->lexer);
25378 noexcept_p = true;
25381 /* Parse the optional trailing return type. */
25382 tree type = NULL_TREE;
25383 if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
25385 cp_lexer_consume_token (parser->lexer);
25386 bool saved_result_type_constraint_p = parser->in_result_type_constraint_p;
25387 parser->in_result_type_constraint_p = true;
25388 type = cp_parser_trailing_type_id (parser);
25389 parser->in_result_type_constraint_p = saved_result_type_constraint_p;
25390 if (type == error_mark_node)
25391 return error_mark_node;
25394 return finish_compound_requirement (expr, type, noexcept_p);
25397 /* Parse a nested requirement. This is the same as a requires clause.
25399 nested-requirement:
25400 requires-clause */
25401 static tree
25402 cp_parser_nested_requirement (cp_parser *parser)
25404 cp_lexer_consume_token (parser->lexer);
25405 tree req = cp_parser_requires_clause (parser);
25406 if (req == error_mark_node)
25407 return error_mark_node;
25408 return finish_nested_requirement (req);
25411 /* Support Functions */
25413 /* Return the appropriate prefer_type argument for lookup_name_real based on
25414 tag_type and template_mem_access. */
25416 static inline int
25417 prefer_type_arg (tag_types tag_type, bool template_mem_access = false)
25419 /* DR 141: When looking in the current enclosing context for a template-name
25420 after -> or ., only consider class templates. */
25421 if (template_mem_access)
25422 return 2;
25423 switch (tag_type)
25425 case none_type: return 0; // No preference.
25426 case scope_type: return 1; // Type or namespace.
25427 default: return 2; // Type only.
25431 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
25432 NAME should have one of the representations used for an
25433 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
25434 is returned. If PARSER->SCOPE is a dependent type, then a
25435 SCOPE_REF is returned.
25437 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
25438 returned; the name was already resolved when the TEMPLATE_ID_EXPR
25439 was formed. Abstractly, such entities should not be passed to this
25440 function, because they do not need to be looked up, but it is
25441 simpler to check for this special case here, rather than at the
25442 call-sites.
25444 In cases not explicitly covered above, this function returns a
25445 DECL, OVERLOAD, or baselink representing the result of the lookup.
25446 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
25447 is returned.
25449 If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
25450 (e.g., "struct") that was used. In that case bindings that do not
25451 refer to types are ignored.
25453 If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
25454 ignored.
25456 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
25457 are ignored.
25459 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
25460 types.
25462 If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
25463 TREE_LIST of candidates if name-lookup results in an ambiguity, and
25464 NULL_TREE otherwise. */
25466 static cp_expr
25467 cp_parser_lookup_name (cp_parser *parser, tree name,
25468 enum tag_types tag_type,
25469 bool is_template,
25470 bool is_namespace,
25471 bool check_dependency,
25472 tree *ambiguous_decls,
25473 location_t name_location)
25475 tree decl;
25476 tree object_type = parser->context->object_type;
25478 /* Assume that the lookup will be unambiguous. */
25479 if (ambiguous_decls)
25480 *ambiguous_decls = NULL_TREE;
25482 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
25483 no longer valid. Note that if we are parsing tentatively, and
25484 the parse fails, OBJECT_TYPE will be automatically restored. */
25485 parser->context->object_type = NULL_TREE;
25487 if (name == error_mark_node)
25488 return error_mark_node;
25490 /* A template-id has already been resolved; there is no lookup to
25491 do. */
25492 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
25493 return name;
25494 if (BASELINK_P (name))
25496 gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
25497 == TEMPLATE_ID_EXPR);
25498 return name;
25501 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
25502 it should already have been checked to make sure that the name
25503 used matches the type being destroyed. */
25504 if (TREE_CODE (name) == BIT_NOT_EXPR)
25506 tree type;
25508 /* Figure out to which type this destructor applies. */
25509 if (parser->scope)
25510 type = parser->scope;
25511 else if (object_type)
25512 type = object_type;
25513 else
25514 type = current_class_type;
25515 /* If that's not a class type, there is no destructor. */
25516 if (!type || !CLASS_TYPE_P (type))
25517 return error_mark_node;
25518 if (CLASSTYPE_LAZY_DESTRUCTOR (type))
25519 lazily_declare_fn (sfk_destructor, type);
25520 if (!CLASSTYPE_DESTRUCTORS (type))
25521 return error_mark_node;
25522 /* If it was a class type, return the destructor. */
25523 return CLASSTYPE_DESTRUCTORS (type);
25526 /* By this point, the NAME should be an ordinary identifier. If
25527 the id-expression was a qualified name, the qualifying scope is
25528 stored in PARSER->SCOPE at this point. */
25529 gcc_assert (identifier_p (name));
25531 /* Perform the lookup. */
25532 if (parser->scope)
25534 bool dependent_p;
25536 if (parser->scope == error_mark_node)
25537 return error_mark_node;
25539 /* If the SCOPE is dependent, the lookup must be deferred until
25540 the template is instantiated -- unless we are explicitly
25541 looking up names in uninstantiated templates. Even then, we
25542 cannot look up the name if the scope is not a class type; it
25543 might, for example, be a template type parameter. */
25544 dependent_p = (TYPE_P (parser->scope)
25545 && dependent_scope_p (parser->scope));
25546 if ((check_dependency || !CLASS_TYPE_P (parser->scope))
25547 && dependent_p)
25548 /* Defer lookup. */
25549 decl = error_mark_node;
25550 else
25552 tree pushed_scope = NULL_TREE;
25554 /* If PARSER->SCOPE is a dependent type, then it must be a
25555 class type, and we must not be checking dependencies;
25556 otherwise, we would have processed this lookup above. So
25557 that PARSER->SCOPE is not considered a dependent base by
25558 lookup_member, we must enter the scope here. */
25559 if (dependent_p)
25560 pushed_scope = push_scope (parser->scope);
25562 /* If the PARSER->SCOPE is a template specialization, it
25563 may be instantiated during name lookup. In that case,
25564 errors may be issued. Even if we rollback the current
25565 tentative parse, those errors are valid. */
25566 decl = lookup_qualified_name (parser->scope, name,
25567 prefer_type_arg (tag_type),
25568 /*complain=*/true);
25570 /* 3.4.3.1: In a lookup in which the constructor is an acceptable
25571 lookup result and the nested-name-specifier nominates a class C:
25572 * if the name specified after the nested-name-specifier, when
25573 looked up in C, is the injected-class-name of C (Clause 9), or
25574 * if the name specified after the nested-name-specifier is the
25575 same as the identifier or the simple-template-id's template-
25576 name in the last component of the nested-name-specifier,
25577 the name is instead considered to name the constructor of
25578 class C. [ Note: for example, the constructor is not an
25579 acceptable lookup result in an elaborated-type-specifier so
25580 the constructor would not be used in place of the
25581 injected-class-name. --end note ] Such a constructor name
25582 shall be used only in the declarator-id of a declaration that
25583 names a constructor or in a using-declaration. */
25584 if (tag_type == none_type
25585 && DECL_SELF_REFERENCE_P (decl)
25586 && same_type_p (DECL_CONTEXT (decl), parser->scope))
25587 decl = lookup_qualified_name (parser->scope, ctor_identifier,
25588 prefer_type_arg (tag_type),
25589 /*complain=*/true);
25591 /* If we have a single function from a using decl, pull it out. */
25592 if (TREE_CODE (decl) == OVERLOAD
25593 && !really_overloaded_fn (decl))
25594 decl = OVL_FUNCTION (decl);
25596 if (pushed_scope)
25597 pop_scope (pushed_scope);
25600 /* If the scope is a dependent type and either we deferred lookup or
25601 we did lookup but didn't find the name, rememeber the name. */
25602 if (decl == error_mark_node && TYPE_P (parser->scope)
25603 && dependent_type_p (parser->scope))
25605 if (tag_type)
25607 tree type;
25609 /* The resolution to Core Issue 180 says that `struct
25610 A::B' should be considered a type-name, even if `A'
25611 is dependent. */
25612 type = make_typename_type (parser->scope, name, tag_type,
25613 /*complain=*/tf_error);
25614 if (type != error_mark_node)
25615 decl = TYPE_NAME (type);
25617 else if (is_template
25618 && (cp_parser_next_token_ends_template_argument_p (parser)
25619 || cp_lexer_next_token_is (parser->lexer,
25620 CPP_CLOSE_PAREN)))
25621 decl = make_unbound_class_template (parser->scope,
25622 name, NULL_TREE,
25623 /*complain=*/tf_error);
25624 else
25625 decl = build_qualified_name (/*type=*/NULL_TREE,
25626 parser->scope, name,
25627 is_template);
25629 parser->qualifying_scope = parser->scope;
25630 parser->object_scope = NULL_TREE;
25632 else if (object_type)
25634 /* Look up the name in the scope of the OBJECT_TYPE, unless the
25635 OBJECT_TYPE is not a class. */
25636 if (CLASS_TYPE_P (object_type))
25637 /* If the OBJECT_TYPE is a template specialization, it may
25638 be instantiated during name lookup. In that case, errors
25639 may be issued. Even if we rollback the current tentative
25640 parse, those errors are valid. */
25641 decl = lookup_member (object_type,
25642 name,
25643 /*protect=*/0,
25644 prefer_type_arg (tag_type),
25645 tf_warning_or_error);
25646 else
25647 decl = NULL_TREE;
25649 if (!decl)
25650 /* Look it up in the enclosing context. DR 141: When looking for a
25651 template-name after -> or ., only consider class templates. */
25652 decl = lookup_name_real (name, prefer_type_arg (tag_type, is_template),
25653 /*nonclass=*/0,
25654 /*block_p=*/true, is_namespace, 0);
25655 if (object_type == unknown_type_node)
25656 /* The object is type-dependent, so we can't look anything up; we used
25657 this to get the DR 141 behavior. */
25658 object_type = NULL_TREE;
25659 parser->object_scope = object_type;
25660 parser->qualifying_scope = NULL_TREE;
25662 else
25664 decl = lookup_name_real (name, prefer_type_arg (tag_type),
25665 /*nonclass=*/0,
25666 /*block_p=*/true, is_namespace, 0);
25667 parser->qualifying_scope = NULL_TREE;
25668 parser->object_scope = NULL_TREE;
25671 /* If the lookup failed, let our caller know. */
25672 if (!decl || decl == error_mark_node)
25673 return error_mark_node;
25675 /* Pull out the template from an injected-class-name (or multiple). */
25676 if (is_template)
25677 decl = maybe_get_template_decl_from_type_decl (decl);
25679 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
25680 if (TREE_CODE (decl) == TREE_LIST)
25682 if (ambiguous_decls)
25683 *ambiguous_decls = decl;
25684 /* The error message we have to print is too complicated for
25685 cp_parser_error, so we incorporate its actions directly. */
25686 if (!cp_parser_simulate_error (parser))
25688 error_at (name_location, "reference to %qD is ambiguous",
25689 name);
25690 print_candidates (decl);
25692 return error_mark_node;
25695 gcc_assert (DECL_P (decl)
25696 || TREE_CODE (decl) == OVERLOAD
25697 || TREE_CODE (decl) == SCOPE_REF
25698 || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
25699 || BASELINK_P (decl));
25701 /* If we have resolved the name of a member declaration, check to
25702 see if the declaration is accessible. When the name resolves to
25703 set of overloaded functions, accessibility is checked when
25704 overload resolution is done.
25706 During an explicit instantiation, access is not checked at all,
25707 as per [temp.explicit]. */
25708 if (DECL_P (decl))
25709 check_accessibility_of_qualified_id (decl, object_type, parser->scope);
25711 maybe_record_typedef_use (decl);
25713 return cp_expr (decl, name_location);
25716 /* Like cp_parser_lookup_name, but for use in the typical case where
25717 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
25718 IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE. */
25720 static tree
25721 cp_parser_lookup_name_simple (cp_parser* parser, tree name, location_t location)
25723 return cp_parser_lookup_name (parser, name,
25724 none_type,
25725 /*is_template=*/false,
25726 /*is_namespace=*/false,
25727 /*check_dependency=*/true,
25728 /*ambiguous_decls=*/NULL,
25729 location);
25732 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
25733 the current context, return the TYPE_DECL. If TAG_NAME_P is
25734 true, the DECL indicates the class being defined in a class-head,
25735 or declared in an elaborated-type-specifier.
25737 Otherwise, return DECL. */
25739 static tree
25740 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
25742 /* If the TEMPLATE_DECL is being declared as part of a class-head,
25743 the translation from TEMPLATE_DECL to TYPE_DECL occurs:
25745 struct A {
25746 template <typename T> struct B;
25749 template <typename T> struct A::B {};
25751 Similarly, in an elaborated-type-specifier:
25753 namespace N { struct X{}; }
25755 struct A {
25756 template <typename T> friend struct N::X;
25759 However, if the DECL refers to a class type, and we are in
25760 the scope of the class, then the name lookup automatically
25761 finds the TYPE_DECL created by build_self_reference rather
25762 than a TEMPLATE_DECL. For example, in:
25764 template <class T> struct S {
25765 S s;
25768 there is no need to handle such case. */
25770 if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
25771 return DECL_TEMPLATE_RESULT (decl);
25773 return decl;
25776 /* If too many, or too few, template-parameter lists apply to the
25777 declarator, issue an error message. Returns TRUE if all went well,
25778 and FALSE otherwise. */
25780 static bool
25781 cp_parser_check_declarator_template_parameters (cp_parser* parser,
25782 cp_declarator *declarator,
25783 location_t declarator_location)
25785 switch (declarator->kind)
25787 case cdk_id:
25789 unsigned num_templates = 0;
25790 tree scope = declarator->u.id.qualifying_scope;
25792 if (scope)
25793 num_templates = num_template_headers_for_class (scope);
25794 else if (TREE_CODE (declarator->u.id.unqualified_name)
25795 == TEMPLATE_ID_EXPR)
25796 /* If the DECLARATOR has the form `X<y>' then it uses one
25797 additional level of template parameters. */
25798 ++num_templates;
25800 return cp_parser_check_template_parameters
25801 (parser, num_templates, declarator_location, declarator);
25804 case cdk_function:
25805 case cdk_array:
25806 case cdk_pointer:
25807 case cdk_reference:
25808 case cdk_ptrmem:
25809 return (cp_parser_check_declarator_template_parameters
25810 (parser, declarator->declarator, declarator_location));
25812 case cdk_decomp:
25813 case cdk_error:
25814 return true;
25816 default:
25817 gcc_unreachable ();
25819 return false;
25822 /* NUM_TEMPLATES were used in the current declaration. If that is
25823 invalid, return FALSE and issue an error messages. Otherwise,
25824 return TRUE. If DECLARATOR is non-NULL, then we are checking a
25825 declarator and we can print more accurate diagnostics. */
25827 static bool
25828 cp_parser_check_template_parameters (cp_parser* parser,
25829 unsigned num_templates,
25830 location_t location,
25831 cp_declarator *declarator)
25833 /* If there are the same number of template classes and parameter
25834 lists, that's OK. */
25835 if (parser->num_template_parameter_lists == num_templates)
25836 return true;
25837 /* If there are more, but only one more, then we are referring to a
25838 member template. That's OK too. */
25839 if (parser->num_template_parameter_lists == num_templates + 1)
25840 return true;
25841 /* If there are more template classes than parameter lists, we have
25842 something like:
25844 template <class T> void S<T>::R<T>::f (); */
25845 if (parser->num_template_parameter_lists < num_templates)
25847 if (declarator && !current_function_decl)
25848 error_at (location, "specializing member %<%T::%E%> "
25849 "requires %<template<>%> syntax",
25850 declarator->u.id.qualifying_scope,
25851 declarator->u.id.unqualified_name);
25852 else if (declarator)
25853 error_at (location, "invalid declaration of %<%T::%E%>",
25854 declarator->u.id.qualifying_scope,
25855 declarator->u.id.unqualified_name);
25856 else
25857 error_at (location, "too few template-parameter-lists");
25858 return false;
25860 /* Otherwise, there are too many template parameter lists. We have
25861 something like:
25863 template <class T> template <class U> void S::f(); */
25864 error_at (location, "too many template-parameter-lists");
25865 return false;
25868 /* Parse an optional `::' token indicating that the following name is
25869 from the global namespace. If so, PARSER->SCOPE is set to the
25870 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
25871 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
25872 Returns the new value of PARSER->SCOPE, if the `::' token is
25873 present, and NULL_TREE otherwise. */
25875 static tree
25876 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
25878 cp_token *token;
25880 /* Peek at the next token. */
25881 token = cp_lexer_peek_token (parser->lexer);
25882 /* If we're looking at a `::' token then we're starting from the
25883 global namespace, not our current location. */
25884 if (token->type == CPP_SCOPE)
25886 /* Consume the `::' token. */
25887 cp_lexer_consume_token (parser->lexer);
25888 /* Set the SCOPE so that we know where to start the lookup. */
25889 parser->scope = global_namespace;
25890 parser->qualifying_scope = global_namespace;
25891 parser->object_scope = NULL_TREE;
25893 return parser->scope;
25895 else if (!current_scope_valid_p)
25897 parser->scope = NULL_TREE;
25898 parser->qualifying_scope = NULL_TREE;
25899 parser->object_scope = NULL_TREE;
25902 return NULL_TREE;
25905 /* Returns TRUE if the upcoming token sequence is the start of a
25906 constructor declarator or C++17 deduction guide. If FRIEND_P is true, the
25907 declarator is preceded by the `friend' specifier. */
25909 static bool
25910 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
25912 bool constructor_p;
25913 bool outside_class_specifier_p;
25914 tree nested_name_specifier;
25915 cp_token *next_token;
25917 /* The common case is that this is not a constructor declarator, so
25918 try to avoid doing lots of work if at all possible. It's not
25919 valid declare a constructor at function scope. */
25920 if (parser->in_function_body)
25921 return false;
25922 /* And only certain tokens can begin a constructor declarator. */
25923 next_token = cp_lexer_peek_token (parser->lexer);
25924 if (next_token->type != CPP_NAME
25925 && next_token->type != CPP_SCOPE
25926 && next_token->type != CPP_NESTED_NAME_SPECIFIER
25927 && next_token->type != CPP_TEMPLATE_ID)
25928 return false;
25930 /* Parse tentatively; we are going to roll back all of the tokens
25931 consumed here. */
25932 cp_parser_parse_tentatively (parser);
25933 /* Assume that we are looking at a constructor declarator. */
25934 constructor_p = true;
25936 /* Look for the optional `::' operator. */
25937 cp_parser_global_scope_opt (parser,
25938 /*current_scope_valid_p=*/false);
25939 /* Look for the nested-name-specifier. */
25940 nested_name_specifier
25941 = (cp_parser_nested_name_specifier_opt (parser,
25942 /*typename_keyword_p=*/false,
25943 /*check_dependency_p=*/false,
25944 /*type_p=*/false,
25945 /*is_declaration=*/false));
25947 outside_class_specifier_p = (!at_class_scope_p ()
25948 || !TYPE_BEING_DEFINED (current_class_type)
25949 || friend_p);
25951 /* Outside of a class-specifier, there must be a
25952 nested-name-specifier. Except in C++17 mode, where we
25953 might be declaring a guiding declaration. */
25954 if (!nested_name_specifier && outside_class_specifier_p
25955 && cxx_dialect < cxx1z)
25956 constructor_p = false;
25957 else if (nested_name_specifier == error_mark_node)
25958 constructor_p = false;
25960 /* If we have a class scope, this is easy; DR 147 says that S::S always
25961 names the constructor, and no other qualified name could. */
25962 if (constructor_p && nested_name_specifier
25963 && CLASS_TYPE_P (nested_name_specifier))
25965 tree id = cp_parser_unqualified_id (parser,
25966 /*template_keyword_p=*/false,
25967 /*check_dependency_p=*/false,
25968 /*declarator_p=*/true,
25969 /*optional_p=*/false);
25970 if (is_overloaded_fn (id))
25971 id = DECL_NAME (get_first_fn (id));
25972 if (!constructor_name_p (id, nested_name_specifier))
25973 constructor_p = false;
25975 /* If we still think that this might be a constructor-declarator,
25976 look for a class-name. */
25977 else if (constructor_p)
25979 /* If we have:
25981 template <typename T> struct S {
25982 S();
25985 we must recognize that the nested `S' names a class. */
25986 if (cxx_dialect >= cxx1z)
25987 cp_parser_parse_tentatively (parser);
25989 tree type_decl;
25990 type_decl = cp_parser_class_name (parser,
25991 /*typename_keyword_p=*/false,
25992 /*template_keyword_p=*/false,
25993 none_type,
25994 /*check_dependency_p=*/false,
25995 /*class_head_p=*/false,
25996 /*is_declaration=*/false);
25998 if (cxx_dialect >= cxx1z
25999 && !cp_parser_parse_definitely (parser))
26001 type_decl = NULL_TREE;
26002 tree tmpl = cp_parser_template_name (parser,
26003 /*template_keyword*/false,
26004 /*check_dependency_p*/false,
26005 /*is_declaration*/false,
26006 none_type,
26007 /*is_identifier*/NULL);
26008 if (DECL_CLASS_TEMPLATE_P (tmpl)
26009 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl))
26010 /* It's a deduction guide, return true. */;
26011 else
26012 cp_parser_simulate_error (parser);
26015 /* If there was no class-name, then this is not a constructor.
26016 Otherwise, if we are in a class-specifier and we aren't
26017 handling a friend declaration, check that its type matches
26018 current_class_type (c++/38313). Note: error_mark_node
26019 is left alone for error recovery purposes. */
26020 constructor_p = (!cp_parser_error_occurred (parser)
26021 && (outside_class_specifier_p
26022 || type_decl == NULL_TREE
26023 || type_decl == error_mark_node
26024 || same_type_p (current_class_type,
26025 TREE_TYPE (type_decl))));
26027 /* If we're still considering a constructor, we have to see a `(',
26028 to begin the parameter-declaration-clause, followed by either a
26029 `)', an `...', or a decl-specifier. We need to check for a
26030 type-specifier to avoid being fooled into thinking that:
26032 S (f) (int);
26034 is a constructor. (It is actually a function named `f' that
26035 takes one parameter (of type `int') and returns a value of type
26036 `S'. */
26037 if (constructor_p
26038 && !cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
26039 constructor_p = false;
26041 if (constructor_p
26042 && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
26043 && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
26044 /* A parameter declaration begins with a decl-specifier,
26045 which is either the "attribute" keyword, a storage class
26046 specifier, or (usually) a type-specifier. */
26047 && !cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer))
26049 tree type;
26050 tree pushed_scope = NULL_TREE;
26051 unsigned saved_num_template_parameter_lists;
26053 /* Names appearing in the type-specifier should be looked up
26054 in the scope of the class. */
26055 if (current_class_type)
26056 type = NULL_TREE;
26057 else if (type_decl)
26059 type = TREE_TYPE (type_decl);
26060 if (TREE_CODE (type) == TYPENAME_TYPE)
26062 type = resolve_typename_type (type,
26063 /*only_current_p=*/false);
26064 if (TREE_CODE (type) == TYPENAME_TYPE)
26066 cp_parser_abort_tentative_parse (parser);
26067 return false;
26070 pushed_scope = push_scope (type);
26073 /* Inside the constructor parameter list, surrounding
26074 template-parameter-lists do not apply. */
26075 saved_num_template_parameter_lists
26076 = parser->num_template_parameter_lists;
26077 parser->num_template_parameter_lists = 0;
26079 /* Look for the type-specifier. */
26080 cp_parser_type_specifier (parser,
26081 CP_PARSER_FLAGS_NONE,
26082 /*decl_specs=*/NULL,
26083 /*is_declarator=*/true,
26084 /*declares_class_or_enum=*/NULL,
26085 /*is_cv_qualifier=*/NULL);
26087 parser->num_template_parameter_lists
26088 = saved_num_template_parameter_lists;
26090 /* Leave the scope of the class. */
26091 if (pushed_scope)
26092 pop_scope (pushed_scope);
26094 constructor_p = !cp_parser_error_occurred (parser);
26098 /* We did not really want to consume any tokens. */
26099 cp_parser_abort_tentative_parse (parser);
26101 return constructor_p;
26104 /* Parse the definition of the function given by the DECL_SPECIFIERS,
26105 ATTRIBUTES, and DECLARATOR. The access checks have been deferred;
26106 they must be performed once we are in the scope of the function.
26108 Returns the function defined. */
26110 static tree
26111 cp_parser_function_definition_from_specifiers_and_declarator
26112 (cp_parser* parser,
26113 cp_decl_specifier_seq *decl_specifiers,
26114 tree attributes,
26115 const cp_declarator *declarator)
26117 tree fn;
26118 bool success_p;
26120 /* Begin the function-definition. */
26121 success_p = start_function (decl_specifiers, declarator, attributes);
26123 /* The things we're about to see are not directly qualified by any
26124 template headers we've seen thus far. */
26125 reset_specialization ();
26127 /* If there were names looked up in the decl-specifier-seq that we
26128 did not check, check them now. We must wait until we are in the
26129 scope of the function to perform the checks, since the function
26130 might be a friend. */
26131 perform_deferred_access_checks (tf_warning_or_error);
26133 if (success_p)
26135 cp_finalize_omp_declare_simd (parser, current_function_decl);
26136 parser->omp_declare_simd = NULL;
26137 cp_finalize_oacc_routine (parser, current_function_decl, true);
26138 parser->oacc_routine = NULL;
26141 if (!success_p)
26143 /* Skip the entire function. */
26144 cp_parser_skip_to_end_of_block_or_statement (parser);
26145 fn = error_mark_node;
26147 else if (DECL_INITIAL (current_function_decl) != error_mark_node)
26149 /* Seen already, skip it. An error message has already been output. */
26150 cp_parser_skip_to_end_of_block_or_statement (parser);
26151 fn = current_function_decl;
26152 current_function_decl = NULL_TREE;
26153 /* If this is a function from a class, pop the nested class. */
26154 if (current_class_name)
26155 pop_nested_class ();
26157 else
26159 timevar_id_t tv;
26160 if (DECL_DECLARED_INLINE_P (current_function_decl))
26161 tv = TV_PARSE_INLINE;
26162 else
26163 tv = TV_PARSE_FUNC;
26164 timevar_push (tv);
26165 fn = cp_parser_function_definition_after_declarator (parser,
26166 /*inline_p=*/false);
26167 timevar_pop (tv);
26170 return fn;
26173 /* Parse the part of a function-definition that follows the
26174 declarator. INLINE_P is TRUE iff this function is an inline
26175 function defined within a class-specifier.
26177 Returns the function defined. */
26179 static tree
26180 cp_parser_function_definition_after_declarator (cp_parser* parser,
26181 bool inline_p)
26183 tree fn;
26184 bool ctor_initializer_p = false;
26185 bool saved_in_unbraced_linkage_specification_p;
26186 bool saved_in_function_body;
26187 unsigned saved_num_template_parameter_lists;
26188 cp_token *token;
26189 bool fully_implicit_function_template_p
26190 = parser->fully_implicit_function_template_p;
26191 parser->fully_implicit_function_template_p = false;
26192 tree implicit_template_parms
26193 = parser->implicit_template_parms;
26194 parser->implicit_template_parms = 0;
26195 cp_binding_level* implicit_template_scope
26196 = parser->implicit_template_scope;
26197 parser->implicit_template_scope = 0;
26199 saved_in_function_body = parser->in_function_body;
26200 parser->in_function_body = true;
26201 /* If the next token is `return', then the code may be trying to
26202 make use of the "named return value" extension that G++ used to
26203 support. */
26204 token = cp_lexer_peek_token (parser->lexer);
26205 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
26207 /* Consume the `return' keyword. */
26208 cp_lexer_consume_token (parser->lexer);
26209 /* Look for the identifier that indicates what value is to be
26210 returned. */
26211 cp_parser_identifier (parser);
26212 /* Issue an error message. */
26213 error_at (token->location,
26214 "named return values are no longer supported");
26215 /* Skip tokens until we reach the start of the function body. */
26216 while (true)
26218 cp_token *token = cp_lexer_peek_token (parser->lexer);
26219 if (token->type == CPP_OPEN_BRACE
26220 || token->type == CPP_EOF
26221 || token->type == CPP_PRAGMA_EOL)
26222 break;
26223 cp_lexer_consume_token (parser->lexer);
26226 /* The `extern' in `extern "C" void f () { ... }' does not apply to
26227 anything declared inside `f'. */
26228 saved_in_unbraced_linkage_specification_p
26229 = parser->in_unbraced_linkage_specification_p;
26230 parser->in_unbraced_linkage_specification_p = false;
26231 /* Inside the function, surrounding template-parameter-lists do not
26232 apply. */
26233 saved_num_template_parameter_lists
26234 = parser->num_template_parameter_lists;
26235 parser->num_template_parameter_lists = 0;
26237 start_lambda_scope (current_function_decl);
26239 /* If the next token is `try', `__transaction_atomic', or
26240 `__transaction_relaxed`, then we are looking at either function-try-block
26241 or function-transaction-block. Note that all of these include the
26242 function-body. */
26243 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRANSACTION_ATOMIC))
26244 ctor_initializer_p = cp_parser_function_transaction (parser,
26245 RID_TRANSACTION_ATOMIC);
26246 else if (cp_lexer_next_token_is_keyword (parser->lexer,
26247 RID_TRANSACTION_RELAXED))
26248 ctor_initializer_p = cp_parser_function_transaction (parser,
26249 RID_TRANSACTION_RELAXED);
26250 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
26251 ctor_initializer_p = cp_parser_function_try_block (parser);
26252 else
26253 ctor_initializer_p = cp_parser_ctor_initializer_opt_and_function_body
26254 (parser, /*in_function_try_block=*/false);
26256 finish_lambda_scope ();
26258 /* Finish the function. */
26259 fn = finish_function ((ctor_initializer_p ? 1 : 0) |
26260 (inline_p ? 2 : 0));
26261 /* Generate code for it, if necessary. */
26262 expand_or_defer_fn (fn);
26263 /* Restore the saved values. */
26264 parser->in_unbraced_linkage_specification_p
26265 = saved_in_unbraced_linkage_specification_p;
26266 parser->num_template_parameter_lists
26267 = saved_num_template_parameter_lists;
26268 parser->in_function_body = saved_in_function_body;
26270 parser->fully_implicit_function_template_p
26271 = fully_implicit_function_template_p;
26272 parser->implicit_template_parms
26273 = implicit_template_parms;
26274 parser->implicit_template_scope
26275 = implicit_template_scope;
26277 if (parser->fully_implicit_function_template_p)
26278 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
26280 return fn;
26283 /* Parse a template-declaration body (following argument list). */
26285 static void
26286 cp_parser_template_declaration_after_parameters (cp_parser* parser,
26287 tree parameter_list,
26288 bool member_p)
26290 tree decl = NULL_TREE;
26291 bool friend_p = false;
26293 /* We just processed one more parameter list. */
26294 ++parser->num_template_parameter_lists;
26296 /* Get the deferred access checks from the parameter list. These
26297 will be checked once we know what is being declared, as for a
26298 member template the checks must be performed in the scope of the
26299 class containing the member. */
26300 vec<deferred_access_check, va_gc> *checks = get_deferred_access_checks ();
26302 /* Tentatively parse for a new template parameter list, which can either be
26303 the template keyword or a template introduction. */
26304 if (cp_parser_template_declaration_after_export (parser, member_p))
26305 /* OK */;
26306 else if (cxx_dialect >= cxx11
26307 && cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
26308 decl = cp_parser_alias_declaration (parser);
26309 else
26311 /* There are no access checks when parsing a template, as we do not
26312 know if a specialization will be a friend. */
26313 push_deferring_access_checks (dk_no_check);
26314 cp_token *token = cp_lexer_peek_token (parser->lexer);
26315 decl = cp_parser_single_declaration (parser,
26316 checks,
26317 member_p,
26318 /*explicit_specialization_p=*/false,
26319 &friend_p);
26320 pop_deferring_access_checks ();
26322 /* If this is a member template declaration, let the front
26323 end know. */
26324 if (member_p && !friend_p && decl)
26326 if (TREE_CODE (decl) == TYPE_DECL)
26327 cp_parser_check_access_in_redeclaration (decl, token->location);
26329 decl = finish_member_template_decl (decl);
26331 else if (friend_p && decl
26332 && DECL_DECLARES_TYPE_P (decl))
26333 make_friend_class (current_class_type, TREE_TYPE (decl),
26334 /*complain=*/true);
26336 /* We are done with the current parameter list. */
26337 --parser->num_template_parameter_lists;
26339 pop_deferring_access_checks ();
26341 /* Finish up. */
26342 finish_template_decl (parameter_list);
26344 /* Check the template arguments for a literal operator template. */
26345 if (decl
26346 && DECL_DECLARES_FUNCTION_P (decl)
26347 && UDLIT_OPER_P (DECL_NAME (decl)))
26349 bool ok = true;
26350 if (parameter_list == NULL_TREE)
26351 ok = false;
26352 else
26354 int num_parms = TREE_VEC_LENGTH (parameter_list);
26355 if (num_parms == 1)
26357 tree parm_list = TREE_VEC_ELT (parameter_list, 0);
26358 tree parm = INNERMOST_TEMPLATE_PARMS (parm_list);
26359 if (TREE_TYPE (parm) != char_type_node
26360 || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
26361 ok = false;
26363 else if (num_parms == 2 && cxx_dialect >= cxx14)
26365 tree parm_type = TREE_VEC_ELT (parameter_list, 0);
26366 tree type = INNERMOST_TEMPLATE_PARMS (parm_type);
26367 tree parm_list = TREE_VEC_ELT (parameter_list, 1);
26368 tree parm = INNERMOST_TEMPLATE_PARMS (parm_list);
26369 if (parm == error_mark_node
26370 || TREE_TYPE (parm) != TREE_TYPE (type)
26371 || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
26372 ok = false;
26374 else
26375 ok = false;
26377 if (!ok)
26379 if (cxx_dialect >= cxx14)
26380 error ("literal operator template %qD has invalid parameter list."
26381 " Expected non-type template argument pack <char...>"
26382 " or <typename CharT, CharT...>",
26383 decl);
26384 else
26385 error ("literal operator template %qD has invalid parameter list."
26386 " Expected non-type template argument pack <char...>",
26387 decl);
26391 /* Register member declarations. */
26392 if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
26393 finish_member_declaration (decl);
26394 /* If DECL is a function template, we must return to parse it later.
26395 (Even though there is no definition, there might be default
26396 arguments that need handling.) */
26397 if (member_p && decl
26398 && DECL_DECLARES_FUNCTION_P (decl))
26399 vec_safe_push (unparsed_funs_with_definitions, decl);
26402 /* Parse a template introduction header for a template-declaration. Returns
26403 false if tentative parse fails. */
26405 static bool
26406 cp_parser_template_introduction (cp_parser* parser, bool member_p)
26408 cp_parser_parse_tentatively (parser);
26410 tree saved_scope = parser->scope;
26411 tree saved_object_scope = parser->object_scope;
26412 tree saved_qualifying_scope = parser->qualifying_scope;
26414 /* Look for the optional `::' operator. */
26415 cp_parser_global_scope_opt (parser,
26416 /*current_scope_valid_p=*/false);
26417 /* Look for the nested-name-specifier. */
26418 cp_parser_nested_name_specifier_opt (parser,
26419 /*typename_keyword_p=*/false,
26420 /*check_dependency_p=*/true,
26421 /*type_p=*/false,
26422 /*is_declaration=*/false);
26424 cp_token *token = cp_lexer_peek_token (parser->lexer);
26425 tree concept_name = cp_parser_identifier (parser);
26427 /* Look up the concept for which we will be matching
26428 template parameters. */
26429 tree tmpl_decl = cp_parser_lookup_name_simple (parser, concept_name,
26430 token->location);
26431 parser->scope = saved_scope;
26432 parser->object_scope = saved_object_scope;
26433 parser->qualifying_scope = saved_qualifying_scope;
26435 if (concept_name == error_mark_node)
26436 cp_parser_simulate_error (parser);
26438 /* Look for opening brace for introduction. */
26439 cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
26441 if (!cp_parser_parse_definitely (parser))
26442 return false;
26444 push_deferring_access_checks (dk_deferred);
26446 /* Build vector of placeholder parameters and grab
26447 matching identifiers. */
26448 tree introduction_list = cp_parser_introduction_list (parser);
26450 /* The introduction-list shall not be empty. */
26451 int nargs = TREE_VEC_LENGTH (introduction_list);
26452 if (nargs == 0)
26454 error ("empty introduction-list");
26455 return true;
26458 /* Look for closing brace for introduction. */
26459 if (!cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE))
26460 return true;
26462 if (tmpl_decl == error_mark_node)
26464 cp_parser_name_lookup_error (parser, concept_name, tmpl_decl, NLE_NULL,
26465 token->location);
26466 return true;
26469 /* Build and associate the constraint. */
26470 tree parms = finish_template_introduction (tmpl_decl, introduction_list);
26471 if (parms && parms != error_mark_node)
26473 cp_parser_template_declaration_after_parameters (parser, parms,
26474 member_p);
26475 return true;
26478 error_at (token->location, "no matching concept for template-introduction");
26479 return true;
26482 /* Parse a normal template-declaration following the template keyword. */
26484 static void
26485 cp_parser_explicit_template_declaration (cp_parser* parser, bool member_p)
26487 tree parameter_list;
26488 bool need_lang_pop;
26489 location_t location = input_location;
26491 /* Look for the `<' token. */
26492 if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
26493 return;
26494 if (at_class_scope_p () && current_function_decl)
26496 /* 14.5.2.2 [temp.mem]
26498 A local class shall not have member templates. */
26499 error_at (location,
26500 "invalid declaration of member template in local class");
26501 cp_parser_skip_to_end_of_block_or_statement (parser);
26502 return;
26504 /* [temp]
26506 A template ... shall not have C linkage. */
26507 if (current_lang_name == lang_name_c)
26509 error_at (location, "template with C linkage");
26510 /* Give it C++ linkage to avoid confusing other parts of the
26511 front end. */
26512 push_lang_context (lang_name_cplusplus);
26513 need_lang_pop = true;
26515 else
26516 need_lang_pop = false;
26518 /* We cannot perform access checks on the template parameter
26519 declarations until we know what is being declared, just as we
26520 cannot check the decl-specifier list. */
26521 push_deferring_access_checks (dk_deferred);
26523 /* If the next token is `>', then we have an invalid
26524 specialization. Rather than complain about an invalid template
26525 parameter, issue an error message here. */
26526 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
26528 cp_parser_error (parser, "invalid explicit specialization");
26529 begin_specialization ();
26530 parameter_list = NULL_TREE;
26532 else
26534 /* Parse the template parameters. */
26535 parameter_list = cp_parser_template_parameter_list (parser);
26538 /* Look for the `>'. */
26539 cp_parser_skip_to_end_of_template_parameter_list (parser);
26541 /* Manage template requirements */
26542 if (flag_concepts)
26544 tree reqs = get_shorthand_constraints (current_template_parms);
26545 if (tree r = cp_parser_requires_clause_opt (parser))
26546 reqs = conjoin_constraints (reqs, normalize_expression (r));
26547 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) = reqs;
26550 cp_parser_template_declaration_after_parameters (parser, parameter_list,
26551 member_p);
26553 /* For the erroneous case of a template with C linkage, we pushed an
26554 implicit C++ linkage scope; exit that scope now. */
26555 if (need_lang_pop)
26556 pop_lang_context ();
26559 /* Parse a template-declaration, assuming that the `export' (and
26560 `extern') keywords, if present, has already been scanned. MEMBER_P
26561 is as for cp_parser_template_declaration. */
26563 static bool
26564 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
26566 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
26568 cp_lexer_consume_token (parser->lexer);
26569 cp_parser_explicit_template_declaration (parser, member_p);
26570 return true;
26572 else if (flag_concepts)
26573 return cp_parser_template_introduction (parser, member_p);
26575 return false;
26578 /* Perform the deferred access checks from a template-parameter-list.
26579 CHECKS is a TREE_LIST of access checks, as returned by
26580 get_deferred_access_checks. */
26582 static void
26583 cp_parser_perform_template_parameter_access_checks (vec<deferred_access_check, va_gc> *checks)
26585 ++processing_template_parmlist;
26586 perform_access_checks (checks, tf_warning_or_error);
26587 --processing_template_parmlist;
26590 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
26591 `function-definition' sequence that follows a template header.
26592 If MEMBER_P is true, this declaration appears in a class scope.
26594 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
26595 *FRIEND_P is set to TRUE iff the declaration is a friend. */
26597 static tree
26598 cp_parser_single_declaration (cp_parser* parser,
26599 vec<deferred_access_check, va_gc> *checks,
26600 bool member_p,
26601 bool explicit_specialization_p,
26602 bool* friend_p)
26604 int declares_class_or_enum;
26605 tree decl = NULL_TREE;
26606 cp_decl_specifier_seq decl_specifiers;
26607 bool function_definition_p = false;
26608 cp_token *decl_spec_token_start;
26610 /* This function is only used when processing a template
26611 declaration. */
26612 gcc_assert (innermost_scope_kind () == sk_template_parms
26613 || innermost_scope_kind () == sk_template_spec);
26615 /* Defer access checks until we know what is being declared. */
26616 push_deferring_access_checks (dk_deferred);
26618 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
26619 alternative. */
26620 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
26621 cp_parser_decl_specifier_seq (parser,
26622 CP_PARSER_FLAGS_OPTIONAL,
26623 &decl_specifiers,
26624 &declares_class_or_enum);
26625 if (friend_p)
26626 *friend_p = cp_parser_friend_p (&decl_specifiers);
26628 /* There are no template typedefs. */
26629 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_typedef))
26631 error_at (decl_spec_token_start->location,
26632 "template declaration of %<typedef%>");
26633 decl = error_mark_node;
26636 /* Gather up the access checks that occurred the
26637 decl-specifier-seq. */
26638 stop_deferring_access_checks ();
26640 /* Check for the declaration of a template class. */
26641 if (declares_class_or_enum)
26643 if (cp_parser_declares_only_class_p (parser)
26644 || (declares_class_or_enum & 2))
26646 // If this is a declaration, but not a definition, associate
26647 // any constraints with the type declaration. Constraints
26648 // are associated with definitions in cp_parser_class_specifier.
26649 if (declares_class_or_enum == 1)
26650 associate_classtype_constraints (decl_specifiers.type);
26652 decl = shadow_tag (&decl_specifiers);
26654 /* In this case:
26656 struct C {
26657 friend template <typename T> struct A<T>::B;
26660 A<T>::B will be represented by a TYPENAME_TYPE, and
26661 therefore not recognized by shadow_tag. */
26662 if (friend_p && *friend_p
26663 && !decl
26664 && decl_specifiers.type
26665 && TYPE_P (decl_specifiers.type))
26666 decl = decl_specifiers.type;
26668 if (decl && decl != error_mark_node)
26669 decl = TYPE_NAME (decl);
26670 else
26671 decl = error_mark_node;
26673 /* Perform access checks for template parameters. */
26674 cp_parser_perform_template_parameter_access_checks (checks);
26676 /* Give a helpful diagnostic for
26677 template <class T> struct A { } a;
26678 if we aren't already recovering from an error. */
26679 if (!cp_parser_declares_only_class_p (parser)
26680 && !seen_error ())
26682 error_at (cp_lexer_peek_token (parser->lexer)->location,
26683 "a class template declaration must not declare "
26684 "anything else");
26685 cp_parser_skip_to_end_of_block_or_statement (parser);
26686 goto out;
26691 /* Complain about missing 'typename' or other invalid type names. */
26692 if (!decl_specifiers.any_type_specifiers_p
26693 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
26695 /* cp_parser_parse_and_diagnose_invalid_type_name calls
26696 cp_parser_skip_to_end_of_block_or_statement, so don't try to parse
26697 the rest of this declaration. */
26698 decl = error_mark_node;
26699 goto out;
26702 /* If it's not a template class, try for a template function. If
26703 the next token is a `;', then this declaration does not declare
26704 anything. But, if there were errors in the decl-specifiers, then
26705 the error might well have come from an attempted class-specifier.
26706 In that case, there's no need to warn about a missing declarator. */
26707 if (!decl
26708 && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
26709 || decl_specifiers.type != error_mark_node))
26711 decl = cp_parser_init_declarator (parser,
26712 &decl_specifiers,
26713 checks,
26714 /*function_definition_allowed_p=*/true,
26715 member_p,
26716 declares_class_or_enum,
26717 &function_definition_p,
26718 NULL, NULL, NULL);
26720 /* 7.1.1-1 [dcl.stc]
26722 A storage-class-specifier shall not be specified in an explicit
26723 specialization... */
26724 if (decl
26725 && explicit_specialization_p
26726 && decl_specifiers.storage_class != sc_none)
26728 error_at (decl_spec_token_start->location,
26729 "explicit template specialization cannot have a storage class");
26730 decl = error_mark_node;
26733 if (decl && VAR_P (decl))
26734 check_template_variable (decl);
26737 /* Look for a trailing `;' after the declaration. */
26738 if (!function_definition_p
26739 && (decl == error_mark_node
26740 || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON)))
26741 cp_parser_skip_to_end_of_block_or_statement (parser);
26743 out:
26744 pop_deferring_access_checks ();
26746 /* Clear any current qualification; whatever comes next is the start
26747 of something new. */
26748 parser->scope = NULL_TREE;
26749 parser->qualifying_scope = NULL_TREE;
26750 parser->object_scope = NULL_TREE;
26752 return decl;
26755 /* Parse a cast-expression that is not the operand of a unary "&". */
26757 static cp_expr
26758 cp_parser_simple_cast_expression (cp_parser *parser)
26760 return cp_parser_cast_expression (parser, /*address_p=*/false,
26761 /*cast_p=*/false, /*decltype*/false, NULL);
26764 /* Parse a functional cast to TYPE. Returns an expression
26765 representing the cast. */
26767 static cp_expr
26768 cp_parser_functional_cast (cp_parser* parser, tree type)
26770 vec<tree, va_gc> *vec;
26771 tree expression_list;
26772 cp_expr cast;
26773 bool nonconst_p;
26775 location_t start_loc = input_location;
26777 if (!type)
26778 type = error_mark_node;
26780 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
26782 cp_lexer_set_source_position (parser->lexer);
26783 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
26784 expression_list = cp_parser_braced_list (parser, &nonconst_p);
26785 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
26786 if (TREE_CODE (type) == TYPE_DECL)
26787 type = TREE_TYPE (type);
26789 cast = finish_compound_literal (type, expression_list,
26790 tf_warning_or_error);
26791 /* Create a location of the form:
26792 type_name{i, f}
26793 ^~~~~~~~~~~~~~~
26794 with caret == start at the start of the type name,
26795 finishing at the closing brace. */
26796 location_t finish_loc
26797 = get_finish (cp_lexer_previous_token (parser->lexer)->location);
26798 location_t combined_loc = make_location (start_loc, start_loc,
26799 finish_loc);
26800 cast.set_location (combined_loc);
26801 return cast;
26805 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
26806 /*cast_p=*/true,
26807 /*allow_expansion_p=*/true,
26808 /*non_constant_p=*/NULL);
26809 if (vec == NULL)
26810 expression_list = error_mark_node;
26811 else
26813 expression_list = build_tree_list_vec (vec);
26814 release_tree_vector (vec);
26817 cast = build_functional_cast (type, expression_list,
26818 tf_warning_or_error);
26819 /* [expr.const]/1: In an integral constant expression "only type
26820 conversions to integral or enumeration type can be used". */
26821 if (TREE_CODE (type) == TYPE_DECL)
26822 type = TREE_TYPE (type);
26823 if (cast != error_mark_node
26824 && !cast_valid_in_integral_constant_expression_p (type)
26825 && cp_parser_non_integral_constant_expression (parser,
26826 NIC_CONSTRUCTOR))
26827 return error_mark_node;
26829 /* Create a location of the form:
26830 float(i)
26831 ^~~~~~~~
26832 with caret == start at the start of the type name,
26833 finishing at the closing paren. */
26834 location_t finish_loc
26835 = get_finish (cp_lexer_previous_token (parser->lexer)->location);
26836 location_t combined_loc = make_location (start_loc, start_loc, finish_loc);
26837 cast.set_location (combined_loc);
26838 return cast;
26841 /* Save the tokens that make up the body of a member function defined
26842 in a class-specifier. The DECL_SPECIFIERS and DECLARATOR have
26843 already been parsed. The ATTRIBUTES are any GNU "__attribute__"
26844 specifiers applied to the declaration. Returns the FUNCTION_DECL
26845 for the member function. */
26847 static tree
26848 cp_parser_save_member_function_body (cp_parser* parser,
26849 cp_decl_specifier_seq *decl_specifiers,
26850 cp_declarator *declarator,
26851 tree attributes)
26853 cp_token *first;
26854 cp_token *last;
26855 tree fn;
26856 bool function_try_block = false;
26858 /* Create the FUNCTION_DECL. */
26859 fn = grokmethod (decl_specifiers, declarator, attributes);
26860 cp_finalize_omp_declare_simd (parser, fn);
26861 cp_finalize_oacc_routine (parser, fn, true);
26862 /* If something went badly wrong, bail out now. */
26863 if (fn == error_mark_node)
26865 /* If there's a function-body, skip it. */
26866 if (cp_parser_token_starts_function_definition_p
26867 (cp_lexer_peek_token (parser->lexer)))
26868 cp_parser_skip_to_end_of_block_or_statement (parser);
26869 return error_mark_node;
26872 /* Remember it, if there default args to post process. */
26873 cp_parser_save_default_args (parser, fn);
26875 /* Save away the tokens that make up the body of the
26876 function. */
26877 first = parser->lexer->next_token;
26879 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRANSACTION_RELAXED))
26880 cp_lexer_consume_token (parser->lexer);
26881 else if (cp_lexer_next_token_is_keyword (parser->lexer,
26882 RID_TRANSACTION_ATOMIC))
26884 cp_lexer_consume_token (parser->lexer);
26885 /* Match cp_parser_txn_attribute_opt [[ identifier ]]. */
26886 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE)
26887 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_SQUARE)
26888 && (cp_lexer_nth_token_is (parser->lexer, 3, CPP_NAME)
26889 || cp_lexer_nth_token_is (parser->lexer, 3, CPP_KEYWORD))
26890 && cp_lexer_nth_token_is (parser->lexer, 4, CPP_CLOSE_SQUARE)
26891 && cp_lexer_nth_token_is (parser->lexer, 5, CPP_CLOSE_SQUARE))
26893 cp_lexer_consume_token (parser->lexer);
26894 cp_lexer_consume_token (parser->lexer);
26895 cp_lexer_consume_token (parser->lexer);
26896 cp_lexer_consume_token (parser->lexer);
26897 cp_lexer_consume_token (parser->lexer);
26899 else
26900 while (cp_next_tokens_can_be_gnu_attribute_p (parser)
26901 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_PAREN))
26903 cp_lexer_consume_token (parser->lexer);
26904 if (cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0))
26905 break;
26909 /* Handle function try blocks. */
26910 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
26912 cp_lexer_consume_token (parser->lexer);
26913 function_try_block = true;
26915 /* We can have braced-init-list mem-initializers before the fn body. */
26916 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
26918 cp_lexer_consume_token (parser->lexer);
26919 while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
26921 /* cache_group will stop after an un-nested { } pair, too. */
26922 if (cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0))
26923 break;
26925 /* variadic mem-inits have ... after the ')'. */
26926 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
26927 cp_lexer_consume_token (parser->lexer);
26930 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
26931 /* Handle function try blocks. */
26932 if (function_try_block)
26933 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
26934 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
26935 last = parser->lexer->next_token;
26937 /* Save away the inline definition; we will process it when the
26938 class is complete. */
26939 DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
26940 DECL_PENDING_INLINE_P (fn) = 1;
26942 /* We need to know that this was defined in the class, so that
26943 friend templates are handled correctly. */
26944 DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
26946 /* Add FN to the queue of functions to be parsed later. */
26947 vec_safe_push (unparsed_funs_with_definitions, fn);
26949 return fn;
26952 /* Save the tokens that make up the in-class initializer for a non-static
26953 data member. Returns a DEFAULT_ARG. */
26955 static tree
26956 cp_parser_save_nsdmi (cp_parser* parser)
26958 return cp_parser_cache_defarg (parser, /*nsdmi=*/true);
26961 /* Parse a template-argument-list, as well as the trailing ">" (but
26962 not the opening "<"). See cp_parser_template_argument_list for the
26963 return value. */
26965 static tree
26966 cp_parser_enclosed_template_argument_list (cp_parser* parser)
26968 tree arguments;
26969 tree saved_scope;
26970 tree saved_qualifying_scope;
26971 tree saved_object_scope;
26972 bool saved_greater_than_is_operator_p;
26973 int saved_unevaluated_operand;
26974 int saved_inhibit_evaluation_warnings;
26976 /* [temp.names]
26978 When parsing a template-id, the first non-nested `>' is taken as
26979 the end of the template-argument-list rather than a greater-than
26980 operator. */
26981 saved_greater_than_is_operator_p
26982 = parser->greater_than_is_operator_p;
26983 parser->greater_than_is_operator_p = false;
26984 /* Parsing the argument list may modify SCOPE, so we save it
26985 here. */
26986 saved_scope = parser->scope;
26987 saved_qualifying_scope = parser->qualifying_scope;
26988 saved_object_scope = parser->object_scope;
26989 /* We need to evaluate the template arguments, even though this
26990 template-id may be nested within a "sizeof". */
26991 saved_unevaluated_operand = cp_unevaluated_operand;
26992 cp_unevaluated_operand = 0;
26993 saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
26994 c_inhibit_evaluation_warnings = 0;
26995 /* Parse the template-argument-list itself. */
26996 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER)
26997 || cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
26998 arguments = NULL_TREE;
26999 else
27000 arguments = cp_parser_template_argument_list (parser);
27001 /* Look for the `>' that ends the template-argument-list. If we find
27002 a '>>' instead, it's probably just a typo. */
27003 if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
27005 if (cxx_dialect != cxx98)
27007 /* In C++0x, a `>>' in a template argument list or cast
27008 expression is considered to be two separate `>'
27009 tokens. So, change the current token to a `>', but don't
27010 consume it: it will be consumed later when the outer
27011 template argument list (or cast expression) is parsed.
27012 Note that this replacement of `>' for `>>' is necessary
27013 even if we are parsing tentatively: in the tentative
27014 case, after calling
27015 cp_parser_enclosed_template_argument_list we will always
27016 throw away all of the template arguments and the first
27017 closing `>', either because the template argument list
27018 was erroneous or because we are replacing those tokens
27019 with a CPP_TEMPLATE_ID token. The second `>' (which will
27020 not have been thrown away) is needed either to close an
27021 outer template argument list or to complete a new-style
27022 cast. */
27023 cp_token *token = cp_lexer_peek_token (parser->lexer);
27024 token->type = CPP_GREATER;
27026 else if (!saved_greater_than_is_operator_p)
27028 /* If we're in a nested template argument list, the '>>' has
27029 to be a typo for '> >'. We emit the error message, but we
27030 continue parsing and we push a '>' as next token, so that
27031 the argument list will be parsed correctly. Note that the
27032 global source location is still on the token before the
27033 '>>', so we need to say explicitly where we want it. */
27034 cp_token *token = cp_lexer_peek_token (parser->lexer);
27035 gcc_rich_location richloc (token->location);
27036 richloc.add_fixit_replace ("> >");
27037 error_at_rich_loc (&richloc, "%<>>%> should be %<> >%> "
27038 "within a nested template argument list");
27040 token->type = CPP_GREATER;
27042 else
27044 /* If this is not a nested template argument list, the '>>'
27045 is a typo for '>'. Emit an error message and continue.
27046 Same deal about the token location, but here we can get it
27047 right by consuming the '>>' before issuing the diagnostic. */
27048 cp_token *token = cp_lexer_consume_token (parser->lexer);
27049 error_at (token->location,
27050 "spurious %<>>%>, use %<>%> to terminate "
27051 "a template argument list");
27054 else
27055 cp_parser_skip_to_end_of_template_parameter_list (parser);
27056 /* The `>' token might be a greater-than operator again now. */
27057 parser->greater_than_is_operator_p
27058 = saved_greater_than_is_operator_p;
27059 /* Restore the SAVED_SCOPE. */
27060 parser->scope = saved_scope;
27061 parser->qualifying_scope = saved_qualifying_scope;
27062 parser->object_scope = saved_object_scope;
27063 cp_unevaluated_operand = saved_unevaluated_operand;
27064 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
27066 return arguments;
27069 /* MEMBER_FUNCTION is a member function, or a friend. If default
27070 arguments, or the body of the function have not yet been parsed,
27071 parse them now. */
27073 static void
27074 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
27076 timevar_push (TV_PARSE_INMETH);
27077 /* If this member is a template, get the underlying
27078 FUNCTION_DECL. */
27079 if (DECL_FUNCTION_TEMPLATE_P (member_function))
27080 member_function = DECL_TEMPLATE_RESULT (member_function);
27082 /* There should not be any class definitions in progress at this
27083 point; the bodies of members are only parsed outside of all class
27084 definitions. */
27085 gcc_assert (parser->num_classes_being_defined == 0);
27086 /* While we're parsing the member functions we might encounter more
27087 classes. We want to handle them right away, but we don't want
27088 them getting mixed up with functions that are currently in the
27089 queue. */
27090 push_unparsed_function_queues (parser);
27092 /* Make sure that any template parameters are in scope. */
27093 maybe_begin_member_template_processing (member_function);
27095 /* If the body of the function has not yet been parsed, parse it
27096 now. */
27097 if (DECL_PENDING_INLINE_P (member_function))
27099 tree function_scope;
27100 cp_token_cache *tokens;
27102 /* The function is no longer pending; we are processing it. */
27103 tokens = DECL_PENDING_INLINE_INFO (member_function);
27104 DECL_PENDING_INLINE_INFO (member_function) = NULL;
27105 DECL_PENDING_INLINE_P (member_function) = 0;
27107 /* If this is a local class, enter the scope of the containing
27108 function. */
27109 function_scope = current_function_decl;
27110 if (function_scope)
27111 push_function_context ();
27113 /* Push the body of the function onto the lexer stack. */
27114 cp_parser_push_lexer_for_tokens (parser, tokens);
27116 /* Let the front end know that we going to be defining this
27117 function. */
27118 start_preparsed_function (member_function, NULL_TREE,
27119 SF_PRE_PARSED | SF_INCLASS_INLINE);
27121 /* Don't do access checking if it is a templated function. */
27122 if (processing_template_decl)
27123 push_deferring_access_checks (dk_no_check);
27125 /* #pragma omp declare reduction needs special parsing. */
27126 if (DECL_OMP_DECLARE_REDUCTION_P (member_function))
27128 parser->lexer->in_pragma = true;
27129 cp_parser_omp_declare_reduction_exprs (member_function, parser);
27130 finish_function (/*inline*/2);
27131 cp_check_omp_declare_reduction (member_function);
27133 else
27134 /* Now, parse the body of the function. */
27135 cp_parser_function_definition_after_declarator (parser,
27136 /*inline_p=*/true);
27138 if (processing_template_decl)
27139 pop_deferring_access_checks ();
27141 /* Leave the scope of the containing function. */
27142 if (function_scope)
27143 pop_function_context ();
27144 cp_parser_pop_lexer (parser);
27147 /* Remove any template parameters from the symbol table. */
27148 maybe_end_member_template_processing ();
27150 /* Restore the queue. */
27151 pop_unparsed_function_queues (parser);
27152 timevar_pop (TV_PARSE_INMETH);
27155 /* If DECL contains any default args, remember it on the unparsed
27156 functions queue. */
27158 static void
27159 cp_parser_save_default_args (cp_parser* parser, tree decl)
27161 tree probe;
27163 for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
27164 probe;
27165 probe = TREE_CHAIN (probe))
27166 if (TREE_PURPOSE (probe))
27168 cp_default_arg_entry entry = {current_class_type, decl};
27169 vec_safe_push (unparsed_funs_with_default_args, entry);
27170 break;
27174 /* DEFAULT_ARG contains the saved tokens for the initializer of DECL,
27175 which is either a FIELD_DECL or PARM_DECL. Parse it and return
27176 the result. For a PARM_DECL, PARMTYPE is the corresponding type
27177 from the parameter-type-list. */
27179 static tree
27180 cp_parser_late_parse_one_default_arg (cp_parser *parser, tree decl,
27181 tree default_arg, tree parmtype)
27183 cp_token_cache *tokens;
27184 tree parsed_arg;
27185 bool dummy;
27187 if (default_arg == error_mark_node)
27188 return error_mark_node;
27190 /* Push the saved tokens for the default argument onto the parser's
27191 lexer stack. */
27192 tokens = DEFARG_TOKENS (default_arg);
27193 cp_parser_push_lexer_for_tokens (parser, tokens);
27195 start_lambda_scope (decl);
27197 /* Parse the default argument. */
27198 parsed_arg = cp_parser_initializer (parser, &dummy, &dummy);
27199 if (BRACE_ENCLOSED_INITIALIZER_P (parsed_arg))
27200 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
27202 finish_lambda_scope ();
27204 if (parsed_arg == error_mark_node)
27205 cp_parser_skip_to_end_of_statement (parser);
27207 if (!processing_template_decl)
27209 /* In a non-template class, check conversions now. In a template,
27210 we'll wait and instantiate these as needed. */
27211 if (TREE_CODE (decl) == PARM_DECL)
27212 parsed_arg = check_default_argument (parmtype, parsed_arg,
27213 tf_warning_or_error);
27214 else if (maybe_reject_flexarray_init (decl, parsed_arg))
27215 parsed_arg = error_mark_node;
27216 else
27217 parsed_arg = digest_nsdmi_init (decl, parsed_arg);
27220 /* If the token stream has not been completely used up, then
27221 there was extra junk after the end of the default
27222 argument. */
27223 if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
27225 if (TREE_CODE (decl) == PARM_DECL)
27226 cp_parser_error (parser, "expected %<,%>");
27227 else
27228 cp_parser_error (parser, "expected %<;%>");
27231 /* Revert to the main lexer. */
27232 cp_parser_pop_lexer (parser);
27234 return parsed_arg;
27237 /* FIELD is a non-static data member with an initializer which we saved for
27238 later; parse it now. */
27240 static void
27241 cp_parser_late_parsing_nsdmi (cp_parser *parser, tree field)
27243 tree def;
27245 maybe_begin_member_template_processing (field);
27247 push_unparsed_function_queues (parser);
27248 def = cp_parser_late_parse_one_default_arg (parser, field,
27249 DECL_INITIAL (field),
27250 NULL_TREE);
27251 pop_unparsed_function_queues (parser);
27253 maybe_end_member_template_processing ();
27255 DECL_INITIAL (field) = def;
27258 /* FN is a FUNCTION_DECL which may contains a parameter with an
27259 unparsed DEFAULT_ARG. Parse the default args now. This function
27260 assumes that the current scope is the scope in which the default
27261 argument should be processed. */
27263 static void
27264 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
27266 bool saved_local_variables_forbidden_p;
27267 tree parm, parmdecl;
27269 /* While we're parsing the default args, we might (due to the
27270 statement expression extension) encounter more classes. We want
27271 to handle them right away, but we don't want them getting mixed
27272 up with default args that are currently in the queue. */
27273 push_unparsed_function_queues (parser);
27275 /* Local variable names (and the `this' keyword) may not appear
27276 in a default argument. */
27277 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
27278 parser->local_variables_forbidden_p = true;
27280 push_defarg_context (fn);
27282 for (parm = TYPE_ARG_TYPES (TREE_TYPE (fn)),
27283 parmdecl = DECL_ARGUMENTS (fn);
27284 parm && parm != void_list_node;
27285 parm = TREE_CHAIN (parm),
27286 parmdecl = DECL_CHAIN (parmdecl))
27288 tree default_arg = TREE_PURPOSE (parm);
27289 tree parsed_arg;
27290 vec<tree, va_gc> *insts;
27291 tree copy;
27292 unsigned ix;
27294 if (!default_arg)
27295 continue;
27297 if (TREE_CODE (default_arg) != DEFAULT_ARG)
27298 /* This can happen for a friend declaration for a function
27299 already declared with default arguments. */
27300 continue;
27302 parsed_arg
27303 = cp_parser_late_parse_one_default_arg (parser, parmdecl,
27304 default_arg,
27305 TREE_VALUE (parm));
27306 if (parsed_arg == error_mark_node)
27308 continue;
27311 TREE_PURPOSE (parm) = parsed_arg;
27313 /* Update any instantiations we've already created. */
27314 for (insts = DEFARG_INSTANTIATIONS (default_arg), ix = 0;
27315 vec_safe_iterate (insts, ix, &copy); ix++)
27316 TREE_PURPOSE (copy) = parsed_arg;
27319 pop_defarg_context ();
27321 /* Make sure no default arg is missing. */
27322 check_default_args (fn);
27324 /* Restore the state of local_variables_forbidden_p. */
27325 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
27327 /* Restore the queue. */
27328 pop_unparsed_function_queues (parser);
27331 /* Subroutine of cp_parser_sizeof_operand, for handling C++11
27333 sizeof ... ( identifier )
27335 where the 'sizeof' token has already been consumed. */
27337 static tree
27338 cp_parser_sizeof_pack (cp_parser *parser)
27340 /* Consume the `...'. */
27341 cp_lexer_consume_token (parser->lexer);
27342 maybe_warn_variadic_templates ();
27344 bool paren = cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN);
27345 if (paren)
27346 cp_lexer_consume_token (parser->lexer);
27347 else
27348 permerror (cp_lexer_peek_token (parser->lexer)->location,
27349 "%<sizeof...%> argument must be surrounded by parentheses");
27351 cp_token *token = cp_lexer_peek_token (parser->lexer);
27352 tree name = cp_parser_identifier (parser);
27353 if (name == error_mark_node)
27354 return error_mark_node;
27355 /* The name is not qualified. */
27356 parser->scope = NULL_TREE;
27357 parser->qualifying_scope = NULL_TREE;
27358 parser->object_scope = NULL_TREE;
27359 tree expr = cp_parser_lookup_name_simple (parser, name, token->location);
27360 if (expr == error_mark_node)
27361 cp_parser_name_lookup_error (parser, name, expr, NLE_NULL,
27362 token->location);
27363 if (TREE_CODE (expr) == TYPE_DECL || TREE_CODE (expr) == TEMPLATE_DECL)
27364 expr = TREE_TYPE (expr);
27365 else if (TREE_CODE (expr) == CONST_DECL)
27366 expr = DECL_INITIAL (expr);
27367 expr = make_pack_expansion (expr);
27368 PACK_EXPANSION_SIZEOF_P (expr) = true;
27370 if (paren)
27371 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
27373 return expr;
27376 /* Parse the operand of `sizeof' (or a similar operator). Returns
27377 either a TYPE or an expression, depending on the form of the
27378 input. The KEYWORD indicates which kind of expression we have
27379 encountered. */
27381 static tree
27382 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
27384 tree expr = NULL_TREE;
27385 const char *saved_message;
27386 char *tmp;
27387 bool saved_integral_constant_expression_p;
27388 bool saved_non_integral_constant_expression_p;
27390 /* If it's a `...', then we are computing the length of a parameter
27391 pack. */
27392 if (keyword == RID_SIZEOF
27393 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
27394 return cp_parser_sizeof_pack (parser);
27396 /* Types cannot be defined in a `sizeof' expression. Save away the
27397 old message. */
27398 saved_message = parser->type_definition_forbidden_message;
27399 /* And create the new one. */
27400 tmp = concat ("types may not be defined in %<",
27401 IDENTIFIER_POINTER (ridpointers[keyword]),
27402 "%> expressions", NULL);
27403 parser->type_definition_forbidden_message = tmp;
27405 /* The restrictions on constant-expressions do not apply inside
27406 sizeof expressions. */
27407 saved_integral_constant_expression_p
27408 = parser->integral_constant_expression_p;
27409 saved_non_integral_constant_expression_p
27410 = parser->non_integral_constant_expression_p;
27411 parser->integral_constant_expression_p = false;
27413 /* Do not actually evaluate the expression. */
27414 ++cp_unevaluated_operand;
27415 ++c_inhibit_evaluation_warnings;
27416 /* If it's a `(', then we might be looking at the type-id
27417 construction. */
27418 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
27420 tree type = NULL_TREE;
27422 /* We can't be sure yet whether we're looking at a type-id or an
27423 expression. */
27424 cp_parser_parse_tentatively (parser);
27425 /* Note: as a GNU Extension, compound literals are considered
27426 postfix-expressions as they are in C99, so they are valid
27427 arguments to sizeof. See comment in cp_parser_cast_expression
27428 for details. */
27429 if (cp_parser_compound_literal_p (parser))
27430 cp_parser_simulate_error (parser);
27431 else
27433 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
27434 parser->in_type_id_in_expr_p = true;
27435 /* Look for the type-id. */
27436 type = cp_parser_type_id (parser);
27437 /* Look for the closing `)'. */
27438 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
27439 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
27442 /* If all went well, then we're done. */
27443 if (cp_parser_parse_definitely (parser))
27445 cp_decl_specifier_seq decl_specs;
27447 /* Build a trivial decl-specifier-seq. */
27448 clear_decl_specs (&decl_specs);
27449 decl_specs.type = type;
27451 /* Call grokdeclarator to figure out what type this is. */
27452 expr = grokdeclarator (NULL,
27453 &decl_specs,
27454 TYPENAME,
27455 /*initialized=*/0,
27456 /*attrlist=*/NULL);
27460 /* If the type-id production did not work out, then we must be
27461 looking at the unary-expression production. */
27462 if (!expr)
27463 expr = cp_parser_unary_expression (parser);
27465 /* Go back to evaluating expressions. */
27466 --cp_unevaluated_operand;
27467 --c_inhibit_evaluation_warnings;
27469 /* Free the message we created. */
27470 free (tmp);
27471 /* And restore the old one. */
27472 parser->type_definition_forbidden_message = saved_message;
27473 parser->integral_constant_expression_p
27474 = saved_integral_constant_expression_p;
27475 parser->non_integral_constant_expression_p
27476 = saved_non_integral_constant_expression_p;
27478 return expr;
27481 /* If the current declaration has no declarator, return true. */
27483 static bool
27484 cp_parser_declares_only_class_p (cp_parser *parser)
27486 /* If the next token is a `;' or a `,' then there is no
27487 declarator. */
27488 return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
27489 || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
27492 /* Update the DECL_SPECS to reflect the storage class indicated by
27493 KEYWORD. */
27495 static void
27496 cp_parser_set_storage_class (cp_parser *parser,
27497 cp_decl_specifier_seq *decl_specs,
27498 enum rid keyword,
27499 cp_token *token)
27501 cp_storage_class storage_class;
27503 if (parser->in_unbraced_linkage_specification_p)
27505 error_at (token->location, "invalid use of %qD in linkage specification",
27506 ridpointers[keyword]);
27507 return;
27509 else if (decl_specs->storage_class != sc_none)
27511 decl_specs->conflicting_specifiers_p = true;
27512 return;
27515 if ((keyword == RID_EXTERN || keyword == RID_STATIC)
27516 && decl_spec_seq_has_spec_p (decl_specs, ds_thread)
27517 && decl_specs->gnu_thread_keyword_p)
27519 pedwarn (decl_specs->locations[ds_thread], 0,
27520 "%<__thread%> before %qD", ridpointers[keyword]);
27523 switch (keyword)
27525 case RID_AUTO:
27526 storage_class = sc_auto;
27527 break;
27528 case RID_REGISTER:
27529 storage_class = sc_register;
27530 break;
27531 case RID_STATIC:
27532 storage_class = sc_static;
27533 break;
27534 case RID_EXTERN:
27535 storage_class = sc_extern;
27536 break;
27537 case RID_MUTABLE:
27538 storage_class = sc_mutable;
27539 break;
27540 default:
27541 gcc_unreachable ();
27543 decl_specs->storage_class = storage_class;
27544 set_and_check_decl_spec_loc (decl_specs, ds_storage_class, token);
27546 /* A storage class specifier cannot be applied alongside a typedef
27547 specifier. If there is a typedef specifier present then set
27548 conflicting_specifiers_p which will trigger an error later
27549 on in grokdeclarator. */
27550 if (decl_spec_seq_has_spec_p (decl_specs, ds_typedef))
27551 decl_specs->conflicting_specifiers_p = true;
27554 /* Update the DECL_SPECS to reflect the TYPE_SPEC. If TYPE_DEFINITION_P
27555 is true, the type is a class or enum definition. */
27557 static void
27558 cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
27559 tree type_spec,
27560 cp_token *token,
27561 bool type_definition_p)
27563 decl_specs->any_specifiers_p = true;
27565 /* If the user tries to redeclare bool, char16_t, char32_t, or wchar_t
27566 (with, for example, in "typedef int wchar_t;") we remember that
27567 this is what happened. In system headers, we ignore these
27568 declarations so that G++ can work with system headers that are not
27569 C++-safe. */
27570 if (decl_spec_seq_has_spec_p (decl_specs, ds_typedef)
27571 && !type_definition_p
27572 && (type_spec == boolean_type_node
27573 || type_spec == char16_type_node
27574 || type_spec == char32_type_node
27575 || type_spec == wchar_type_node)
27576 && (decl_specs->type
27577 || decl_spec_seq_has_spec_p (decl_specs, ds_long)
27578 || decl_spec_seq_has_spec_p (decl_specs, ds_short)
27579 || decl_spec_seq_has_spec_p (decl_specs, ds_unsigned)
27580 || decl_spec_seq_has_spec_p (decl_specs, ds_signed)))
27582 decl_specs->redefined_builtin_type = type_spec;
27583 set_and_check_decl_spec_loc (decl_specs,
27584 ds_redefined_builtin_type_spec,
27585 token);
27586 if (!decl_specs->type)
27588 decl_specs->type = type_spec;
27589 decl_specs->type_definition_p = false;
27590 set_and_check_decl_spec_loc (decl_specs,ds_type_spec, token);
27593 else if (decl_specs->type)
27594 decl_specs->multiple_types_p = true;
27595 else
27597 decl_specs->type = type_spec;
27598 decl_specs->type_definition_p = type_definition_p;
27599 decl_specs->redefined_builtin_type = NULL_TREE;
27600 set_and_check_decl_spec_loc (decl_specs, ds_type_spec, token);
27604 /* True iff TOKEN is the GNU keyword __thread. */
27606 static bool
27607 token_is__thread (cp_token *token)
27609 gcc_assert (token->keyword == RID_THREAD);
27610 return !strcmp (IDENTIFIER_POINTER (token->u.value), "__thread");
27613 /* Set the location for a declarator specifier and check if it is
27614 duplicated.
27616 DECL_SPECS is the sequence of declarator specifiers onto which to
27617 set the location.
27619 DS is the single declarator specifier to set which location is to
27620 be set onto the existing sequence of declarators.
27622 LOCATION is the location for the declarator specifier to
27623 consider. */
27625 static void
27626 set_and_check_decl_spec_loc (cp_decl_specifier_seq *decl_specs,
27627 cp_decl_spec ds, cp_token *token)
27629 gcc_assert (ds < ds_last);
27631 if (decl_specs == NULL)
27632 return;
27634 source_location location = token->location;
27636 if (decl_specs->locations[ds] == 0)
27638 decl_specs->locations[ds] = location;
27639 if (ds == ds_thread)
27640 decl_specs->gnu_thread_keyword_p = token_is__thread (token);
27642 else
27644 if (ds == ds_long)
27646 if (decl_specs->locations[ds_long_long] != 0)
27647 error_at (location,
27648 "%<long long long%> is too long for GCC");
27649 else
27651 decl_specs->locations[ds_long_long] = location;
27652 pedwarn_cxx98 (location,
27653 OPT_Wlong_long,
27654 "ISO C++ 1998 does not support %<long long%>");
27657 else if (ds == ds_thread)
27659 bool gnu = token_is__thread (token);
27660 if (gnu != decl_specs->gnu_thread_keyword_p)
27661 error_at (location,
27662 "both %<__thread%> and %<thread_local%> specified");
27663 else
27664 error_at (location, "duplicate %qD", token->u.value);
27666 else
27668 static const char *const decl_spec_names[] = {
27669 "signed",
27670 "unsigned",
27671 "short",
27672 "long",
27673 "const",
27674 "volatile",
27675 "restrict",
27676 "inline",
27677 "virtual",
27678 "explicit",
27679 "friend",
27680 "typedef",
27681 "using",
27682 "constexpr",
27683 "__complex"
27685 error_at (location,
27686 "duplicate %qs", decl_spec_names[ds]);
27691 /* Return true iff the declarator specifier DS is present in the
27692 sequence of declarator specifiers DECL_SPECS. */
27694 bool
27695 decl_spec_seq_has_spec_p (const cp_decl_specifier_seq * decl_specs,
27696 cp_decl_spec ds)
27698 gcc_assert (ds < ds_last);
27700 if (decl_specs == NULL)
27701 return false;
27703 return decl_specs->locations[ds] != 0;
27706 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
27707 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
27709 static bool
27710 cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
27712 return decl_spec_seq_has_spec_p (decl_specifiers, ds_friend);
27715 /* Issue an error message indicating that TOKEN_DESC was expected.
27716 If KEYWORD is true, it indicated this function is called by
27717 cp_parser_require_keword and the required token can only be
27718 a indicated keyword. */
27720 static void
27721 cp_parser_required_error (cp_parser *parser,
27722 required_token token_desc,
27723 bool keyword)
27725 switch (token_desc)
27727 case RT_NEW:
27728 cp_parser_error (parser, "expected %<new%>");
27729 return;
27730 case RT_DELETE:
27731 cp_parser_error (parser, "expected %<delete%>");
27732 return;
27733 case RT_RETURN:
27734 cp_parser_error (parser, "expected %<return%>");
27735 return;
27736 case RT_WHILE:
27737 cp_parser_error (parser, "expected %<while%>");
27738 return;
27739 case RT_EXTERN:
27740 cp_parser_error (parser, "expected %<extern%>");
27741 return;
27742 case RT_STATIC_ASSERT:
27743 cp_parser_error (parser, "expected %<static_assert%>");
27744 return;
27745 case RT_DECLTYPE:
27746 cp_parser_error (parser, "expected %<decltype%>");
27747 return;
27748 case RT_OPERATOR:
27749 cp_parser_error (parser, "expected %<operator%>");
27750 return;
27751 case RT_CLASS:
27752 cp_parser_error (parser, "expected %<class%>");
27753 return;
27754 case RT_TEMPLATE:
27755 cp_parser_error (parser, "expected %<template%>");
27756 return;
27757 case RT_NAMESPACE:
27758 cp_parser_error (parser, "expected %<namespace%>");
27759 return;
27760 case RT_USING:
27761 cp_parser_error (parser, "expected %<using%>");
27762 return;
27763 case RT_ASM:
27764 cp_parser_error (parser, "expected %<asm%>");
27765 return;
27766 case RT_TRY:
27767 cp_parser_error (parser, "expected %<try%>");
27768 return;
27769 case RT_CATCH:
27770 cp_parser_error (parser, "expected %<catch%>");
27771 return;
27772 case RT_THROW:
27773 cp_parser_error (parser, "expected %<throw%>");
27774 return;
27775 case RT_LABEL:
27776 cp_parser_error (parser, "expected %<__label__%>");
27777 return;
27778 case RT_AT_TRY:
27779 cp_parser_error (parser, "expected %<@try%>");
27780 return;
27781 case RT_AT_SYNCHRONIZED:
27782 cp_parser_error (parser, "expected %<@synchronized%>");
27783 return;
27784 case RT_AT_THROW:
27785 cp_parser_error (parser, "expected %<@throw%>");
27786 return;
27787 case RT_TRANSACTION_ATOMIC:
27788 cp_parser_error (parser, "expected %<__transaction_atomic%>");
27789 return;
27790 case RT_TRANSACTION_RELAXED:
27791 cp_parser_error (parser, "expected %<__transaction_relaxed%>");
27792 return;
27793 default:
27794 break;
27796 if (!keyword)
27798 switch (token_desc)
27800 case RT_SEMICOLON:
27801 cp_parser_error (parser, "expected %<;%>");
27802 return;
27803 case RT_OPEN_PAREN:
27804 cp_parser_error (parser, "expected %<(%>");
27805 return;
27806 case RT_CLOSE_BRACE:
27807 cp_parser_error (parser, "expected %<}%>");
27808 return;
27809 case RT_OPEN_BRACE:
27810 cp_parser_error (parser, "expected %<{%>");
27811 return;
27812 case RT_CLOSE_SQUARE:
27813 cp_parser_error (parser, "expected %<]%>");
27814 return;
27815 case RT_OPEN_SQUARE:
27816 cp_parser_error (parser, "expected %<[%>");
27817 return;
27818 case RT_COMMA:
27819 cp_parser_error (parser, "expected %<,%>");
27820 return;
27821 case RT_SCOPE:
27822 cp_parser_error (parser, "expected %<::%>");
27823 return;
27824 case RT_LESS:
27825 cp_parser_error (parser, "expected %<<%>");
27826 return;
27827 case RT_GREATER:
27828 cp_parser_error (parser, "expected %<>%>");
27829 return;
27830 case RT_EQ:
27831 cp_parser_error (parser, "expected %<=%>");
27832 return;
27833 case RT_ELLIPSIS:
27834 cp_parser_error (parser, "expected %<...%>");
27835 return;
27836 case RT_MULT:
27837 cp_parser_error (parser, "expected %<*%>");
27838 return;
27839 case RT_COMPL:
27840 cp_parser_error (parser, "expected %<~%>");
27841 return;
27842 case RT_COLON:
27843 cp_parser_error (parser, "expected %<:%>");
27844 return;
27845 case RT_COLON_SCOPE:
27846 cp_parser_error (parser, "expected %<:%> or %<::%>");
27847 return;
27848 case RT_CLOSE_PAREN:
27849 cp_parser_error (parser, "expected %<)%>");
27850 return;
27851 case RT_COMMA_CLOSE_PAREN:
27852 cp_parser_error (parser, "expected %<,%> or %<)%>");
27853 return;
27854 case RT_PRAGMA_EOL:
27855 cp_parser_error (parser, "expected end of line");
27856 return;
27857 case RT_NAME:
27858 cp_parser_error (parser, "expected identifier");
27859 return;
27860 case RT_SELECT:
27861 cp_parser_error (parser, "expected selection-statement");
27862 return;
27863 case RT_INTERATION:
27864 cp_parser_error (parser, "expected iteration-statement");
27865 return;
27866 case RT_JUMP:
27867 cp_parser_error (parser, "expected jump-statement");
27868 return;
27869 case RT_CLASS_KEY:
27870 cp_parser_error (parser, "expected class-key");
27871 return;
27872 case RT_CLASS_TYPENAME_TEMPLATE:
27873 cp_parser_error (parser,
27874 "expected %<class%>, %<typename%>, or %<template%>");
27875 return;
27876 default:
27877 gcc_unreachable ();
27880 else
27881 gcc_unreachable ();
27886 /* If the next token is of the indicated TYPE, consume it. Otherwise,
27887 issue an error message indicating that TOKEN_DESC was expected.
27889 Returns the token consumed, if the token had the appropriate type.
27890 Otherwise, returns NULL. */
27892 static cp_token *
27893 cp_parser_require (cp_parser* parser,
27894 enum cpp_ttype type,
27895 required_token token_desc)
27897 if (cp_lexer_next_token_is (parser->lexer, type))
27898 return cp_lexer_consume_token (parser->lexer);
27899 else
27901 /* Output the MESSAGE -- unless we're parsing tentatively. */
27902 if (!cp_parser_simulate_error (parser))
27903 cp_parser_required_error (parser, token_desc, /*keyword=*/false);
27904 return NULL;
27908 /* An error message is produced if the next token is not '>'.
27909 All further tokens are skipped until the desired token is
27910 found or '{', '}', ';' or an unbalanced ')' or ']'. */
27912 static void
27913 cp_parser_skip_to_end_of_template_parameter_list (cp_parser* parser)
27915 /* Current level of '< ... >'. */
27916 unsigned level = 0;
27917 /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'. */
27918 unsigned nesting_depth = 0;
27920 /* Are we ready, yet? If not, issue error message. */
27921 if (cp_parser_require (parser, CPP_GREATER, RT_GREATER))
27922 return;
27924 /* Skip tokens until the desired token is found. */
27925 while (true)
27927 /* Peek at the next token. */
27928 switch (cp_lexer_peek_token (parser->lexer)->type)
27930 case CPP_LESS:
27931 if (!nesting_depth)
27932 ++level;
27933 break;
27935 case CPP_RSHIFT:
27936 if (cxx_dialect == cxx98)
27937 /* C++0x views the `>>' operator as two `>' tokens, but
27938 C++98 does not. */
27939 break;
27940 else if (!nesting_depth && level-- == 0)
27942 /* We've hit a `>>' where the first `>' closes the
27943 template argument list, and the second `>' is
27944 spurious. Just consume the `>>' and stop; we've
27945 already produced at least one error. */
27946 cp_lexer_consume_token (parser->lexer);
27947 return;
27949 /* Fall through for C++0x, so we handle the second `>' in
27950 the `>>'. */
27951 gcc_fallthrough ();
27953 case CPP_GREATER:
27954 if (!nesting_depth && level-- == 0)
27956 /* We've reached the token we want, consume it and stop. */
27957 cp_lexer_consume_token (parser->lexer);
27958 return;
27960 break;
27962 case CPP_OPEN_PAREN:
27963 case CPP_OPEN_SQUARE:
27964 ++nesting_depth;
27965 break;
27967 case CPP_CLOSE_PAREN:
27968 case CPP_CLOSE_SQUARE:
27969 if (nesting_depth-- == 0)
27970 return;
27971 break;
27973 case CPP_EOF:
27974 case CPP_PRAGMA_EOL:
27975 case CPP_SEMICOLON:
27976 case CPP_OPEN_BRACE:
27977 case CPP_CLOSE_BRACE:
27978 /* The '>' was probably forgotten, don't look further. */
27979 return;
27981 default:
27982 break;
27985 /* Consume this token. */
27986 cp_lexer_consume_token (parser->lexer);
27990 /* If the next token is the indicated keyword, consume it. Otherwise,
27991 issue an error message indicating that TOKEN_DESC was expected.
27993 Returns the token consumed, if the token had the appropriate type.
27994 Otherwise, returns NULL. */
27996 static cp_token *
27997 cp_parser_require_keyword (cp_parser* parser,
27998 enum rid keyword,
27999 required_token token_desc)
28001 cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
28003 if (token && token->keyword != keyword)
28005 cp_parser_required_error (parser, token_desc, /*keyword=*/true);
28006 return NULL;
28009 return token;
28012 /* Returns TRUE iff TOKEN is a token that can begin the body of a
28013 function-definition. */
28015 static bool
28016 cp_parser_token_starts_function_definition_p (cp_token* token)
28018 return (/* An ordinary function-body begins with an `{'. */
28019 token->type == CPP_OPEN_BRACE
28020 /* A ctor-initializer begins with a `:'. */
28021 || token->type == CPP_COLON
28022 /* A function-try-block begins with `try'. */
28023 || token->keyword == RID_TRY
28024 /* A function-transaction-block begins with `__transaction_atomic'
28025 or `__transaction_relaxed'. */
28026 || token->keyword == RID_TRANSACTION_ATOMIC
28027 || token->keyword == RID_TRANSACTION_RELAXED
28028 /* The named return value extension begins with `return'. */
28029 || token->keyword == RID_RETURN);
28032 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
28033 definition. */
28035 static bool
28036 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
28038 cp_token *token;
28040 token = cp_lexer_peek_token (parser->lexer);
28041 return (token->type == CPP_OPEN_BRACE
28042 || (token->type == CPP_COLON
28043 && !parser->colon_doesnt_start_class_def_p));
28046 /* Returns TRUE iff the next token is the "," or ">" (or `>>', in
28047 C++0x) ending a template-argument. */
28049 static bool
28050 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
28052 cp_token *token;
28054 token = cp_lexer_peek_token (parser->lexer);
28055 return (token->type == CPP_COMMA
28056 || token->type == CPP_GREATER
28057 || token->type == CPP_ELLIPSIS
28058 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT));
28061 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
28062 (n+1)-th is a ":" (which is a possible digraph typo for "< ::"). */
28064 static bool
28065 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
28066 size_t n)
28068 cp_token *token;
28070 token = cp_lexer_peek_nth_token (parser->lexer, n);
28071 if (token->type == CPP_LESS)
28072 return true;
28073 /* Check for the sequence `<::' in the original code. It would be lexed as
28074 `[:', where `[' is a digraph, and there is no whitespace before
28075 `:'. */
28076 if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
28078 cp_token *token2;
28079 token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
28080 if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
28081 return true;
28083 return false;
28086 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
28087 or none_type otherwise. */
28089 static enum tag_types
28090 cp_parser_token_is_class_key (cp_token* token)
28092 switch (token->keyword)
28094 case RID_CLASS:
28095 return class_type;
28096 case RID_STRUCT:
28097 return record_type;
28098 case RID_UNION:
28099 return union_type;
28101 default:
28102 return none_type;
28106 /* Returns the kind of tag indicated by TOKEN, if it is a type-parameter-key,
28107 or none_type otherwise or if the token is null. */
28109 static enum tag_types
28110 cp_parser_token_is_type_parameter_key (cp_token* token)
28112 if (!token)
28113 return none_type;
28115 switch (token->keyword)
28117 case RID_CLASS:
28118 return class_type;
28119 case RID_TYPENAME:
28120 return typename_type;
28122 default:
28123 return none_type;
28127 /* Issue an error message if the CLASS_KEY does not match the TYPE. */
28129 static void
28130 cp_parser_check_class_key (enum tag_types class_key, tree type)
28132 if (type == error_mark_node)
28133 return;
28134 if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
28136 if (permerror (input_location, "%qs tag used in naming %q#T",
28137 class_key == union_type ? "union"
28138 : class_key == record_type ? "struct" : "class",
28139 type))
28140 inform (DECL_SOURCE_LOCATION (TYPE_NAME (type)),
28141 "%q#T was previously declared here", type);
28145 /* Issue an error message if DECL is redeclared with different
28146 access than its original declaration [class.access.spec/3].
28147 This applies to nested classes, nested class templates and
28148 enumerations [class.mem/1]. */
28150 static void
28151 cp_parser_check_access_in_redeclaration (tree decl, location_t location)
28153 if (!decl
28154 || (!CLASS_TYPE_P (TREE_TYPE (decl))
28155 && TREE_CODE (TREE_TYPE (decl)) != ENUMERAL_TYPE))
28156 return;
28158 if ((TREE_PRIVATE (decl)
28159 != (current_access_specifier == access_private_node))
28160 || (TREE_PROTECTED (decl)
28161 != (current_access_specifier == access_protected_node)))
28162 error_at (location, "%qD redeclared with different access", decl);
28165 /* Look for the `template' keyword, as a syntactic disambiguator.
28166 Return TRUE iff it is present, in which case it will be
28167 consumed. */
28169 static bool
28170 cp_parser_optional_template_keyword (cp_parser *parser)
28172 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
28174 /* In C++98 the `template' keyword can only be used within templates;
28175 outside templates the parser can always figure out what is a
28176 template and what is not. In C++11, per the resolution of DR 468,
28177 `template' is allowed in cases where it is not strictly necessary. */
28178 if (!processing_template_decl
28179 && pedantic && cxx_dialect == cxx98)
28181 cp_token *token = cp_lexer_peek_token (parser->lexer);
28182 pedwarn (token->location, OPT_Wpedantic,
28183 "in C++98 %<template%> (as a disambiguator) is only "
28184 "allowed within templates");
28185 /* If this part of the token stream is rescanned, the same
28186 error message would be generated. So, we purge the token
28187 from the stream. */
28188 cp_lexer_purge_token (parser->lexer);
28189 return false;
28191 else
28193 /* Consume the `template' keyword. */
28194 cp_lexer_consume_token (parser->lexer);
28195 return true;
28198 return false;
28201 /* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
28202 set PARSER->SCOPE, and perform other related actions. */
28204 static void
28205 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
28207 struct tree_check *check_value;
28209 /* Get the stored value. */
28210 check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
28211 /* Set the scope from the stored value. */
28212 parser->scope = saved_checks_value (check_value);
28213 parser->qualifying_scope = check_value->qualifying_scope;
28214 parser->object_scope = NULL_TREE;
28217 /* Consume tokens up through a non-nested END token. Returns TRUE if we
28218 encounter the end of a block before what we were looking for. */
28220 static bool
28221 cp_parser_cache_group (cp_parser *parser,
28222 enum cpp_ttype end,
28223 unsigned depth)
28225 while (true)
28227 cp_token *token = cp_lexer_peek_token (parser->lexer);
28229 /* Abort a parenthesized expression if we encounter a semicolon. */
28230 if ((end == CPP_CLOSE_PAREN || depth == 0)
28231 && token->type == CPP_SEMICOLON)
28232 return true;
28233 /* If we've reached the end of the file, stop. */
28234 if (token->type == CPP_EOF
28235 || (end != CPP_PRAGMA_EOL
28236 && token->type == CPP_PRAGMA_EOL))
28237 return true;
28238 if (token->type == CPP_CLOSE_BRACE && depth == 0)
28239 /* We've hit the end of an enclosing block, so there's been some
28240 kind of syntax error. */
28241 return true;
28243 /* Consume the token. */
28244 cp_lexer_consume_token (parser->lexer);
28245 /* See if it starts a new group. */
28246 if (token->type == CPP_OPEN_BRACE)
28248 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
28249 /* In theory this should probably check end == '}', but
28250 cp_parser_save_member_function_body needs it to exit
28251 after either '}' or ')' when called with ')'. */
28252 if (depth == 0)
28253 return false;
28255 else if (token->type == CPP_OPEN_PAREN)
28257 cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
28258 if (depth == 0 && end == CPP_CLOSE_PAREN)
28259 return false;
28261 else if (token->type == CPP_PRAGMA)
28262 cp_parser_cache_group (parser, CPP_PRAGMA_EOL, depth + 1);
28263 else if (token->type == end)
28264 return false;
28268 /* Like above, for caching a default argument or NSDMI. Both of these are
28269 terminated by a non-nested comma, but it can be unclear whether or not a
28270 comma is nested in a template argument list unless we do more parsing.
28271 In order to handle this ambiguity, when we encounter a ',' after a '<'
28272 we try to parse what follows as a parameter-declaration-list (in the
28273 case of a default argument) or a member-declarator (in the case of an
28274 NSDMI). If that succeeds, then we stop caching. */
28276 static tree
28277 cp_parser_cache_defarg (cp_parser *parser, bool nsdmi)
28279 unsigned depth = 0;
28280 int maybe_template_id = 0;
28281 cp_token *first_token;
28282 cp_token *token;
28283 tree default_argument;
28285 /* Add tokens until we have processed the entire default
28286 argument. We add the range [first_token, token). */
28287 first_token = cp_lexer_peek_token (parser->lexer);
28288 if (first_token->type == CPP_OPEN_BRACE)
28290 /* For list-initialization, this is straightforward. */
28291 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
28292 token = cp_lexer_peek_token (parser->lexer);
28294 else while (true)
28296 bool done = false;
28298 /* Peek at the next token. */
28299 token = cp_lexer_peek_token (parser->lexer);
28300 /* What we do depends on what token we have. */
28301 switch (token->type)
28303 /* In valid code, a default argument must be
28304 immediately followed by a `,' `)', or `...'. */
28305 case CPP_COMMA:
28306 if (depth == 0 && maybe_template_id)
28308 /* If we've seen a '<', we might be in a
28309 template-argument-list. Until Core issue 325 is
28310 resolved, we don't know how this situation ought
28311 to be handled, so try to DTRT. We check whether
28312 what comes after the comma is a valid parameter
28313 declaration list. If it is, then the comma ends
28314 the default argument; otherwise the default
28315 argument continues. */
28316 bool error = false;
28317 cp_token *peek;
28319 /* Set ITALP so cp_parser_parameter_declaration_list
28320 doesn't decide to commit to this parse. */
28321 bool saved_italp = parser->in_template_argument_list_p;
28322 parser->in_template_argument_list_p = true;
28324 cp_parser_parse_tentatively (parser);
28326 if (nsdmi)
28328 /* Parse declarators until we reach a non-comma or
28329 somthing that cannot be an initializer.
28330 Just checking whether we're looking at a single
28331 declarator is insufficient. Consider:
28332 int var = tuple<T,U>::x;
28333 The template parameter 'U' looks exactly like a
28334 declarator. */
28337 int ctor_dtor_or_conv_p;
28338 cp_lexer_consume_token (parser->lexer);
28339 cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
28340 &ctor_dtor_or_conv_p,
28341 /*parenthesized_p=*/NULL,
28342 /*member_p=*/true,
28343 /*friend_p=*/false);
28344 peek = cp_lexer_peek_token (parser->lexer);
28345 if (cp_parser_error_occurred (parser))
28346 break;
28348 while (peek->type == CPP_COMMA);
28349 /* If we met an '=' or ';' then the original comma
28350 was the end of the NSDMI. Otherwise assume
28351 we're still in the NSDMI. */
28352 error = (peek->type != CPP_EQ
28353 && peek->type != CPP_SEMICOLON);
28355 else
28357 cp_lexer_consume_token (parser->lexer);
28358 begin_scope (sk_function_parms, NULL_TREE);
28359 cp_parser_parameter_declaration_list (parser, &error);
28360 pop_bindings_and_leave_scope ();
28362 if (!cp_parser_error_occurred (parser) && !error)
28363 done = true;
28364 cp_parser_abort_tentative_parse (parser);
28366 parser->in_template_argument_list_p = saved_italp;
28367 break;
28369 /* FALLTHRU */
28370 case CPP_CLOSE_PAREN:
28371 case CPP_ELLIPSIS:
28372 /* If we run into a non-nested `;', `}', or `]',
28373 then the code is invalid -- but the default
28374 argument is certainly over. */
28375 case CPP_SEMICOLON:
28376 case CPP_CLOSE_BRACE:
28377 case CPP_CLOSE_SQUARE:
28378 if (depth == 0
28379 /* Handle correctly int n = sizeof ... ( p ); */
28380 && token->type != CPP_ELLIPSIS)
28381 done = true;
28382 /* Update DEPTH, if necessary. */
28383 else if (token->type == CPP_CLOSE_PAREN
28384 || token->type == CPP_CLOSE_BRACE
28385 || token->type == CPP_CLOSE_SQUARE)
28386 --depth;
28387 break;
28389 case CPP_OPEN_PAREN:
28390 case CPP_OPEN_SQUARE:
28391 case CPP_OPEN_BRACE:
28392 ++depth;
28393 break;
28395 case CPP_LESS:
28396 if (depth == 0)
28397 /* This might be the comparison operator, or it might
28398 start a template argument list. */
28399 ++maybe_template_id;
28400 break;
28402 case CPP_RSHIFT:
28403 if (cxx_dialect == cxx98)
28404 break;
28405 /* Fall through for C++0x, which treats the `>>'
28406 operator like two `>' tokens in certain
28407 cases. */
28408 gcc_fallthrough ();
28410 case CPP_GREATER:
28411 if (depth == 0)
28413 /* This might be an operator, or it might close a
28414 template argument list. But if a previous '<'
28415 started a template argument list, this will have
28416 closed it, so we can't be in one anymore. */
28417 maybe_template_id -= 1 + (token->type == CPP_RSHIFT);
28418 if (maybe_template_id < 0)
28419 maybe_template_id = 0;
28421 break;
28423 /* If we run out of tokens, issue an error message. */
28424 case CPP_EOF:
28425 case CPP_PRAGMA_EOL:
28426 error_at (token->location, "file ends in default argument");
28427 return error_mark_node;
28429 case CPP_NAME:
28430 case CPP_SCOPE:
28431 /* In these cases, we should look for template-ids.
28432 For example, if the default argument is
28433 `X<int, double>()', we need to do name lookup to
28434 figure out whether or not `X' is a template; if
28435 so, the `,' does not end the default argument.
28437 That is not yet done. */
28438 break;
28440 default:
28441 break;
28444 /* If we've reached the end, stop. */
28445 if (done)
28446 break;
28448 /* Add the token to the token block. */
28449 token = cp_lexer_consume_token (parser->lexer);
28452 /* Create a DEFAULT_ARG to represent the unparsed default
28453 argument. */
28454 default_argument = make_node (DEFAULT_ARG);
28455 DEFARG_TOKENS (default_argument)
28456 = cp_token_cache_new (first_token, token);
28457 DEFARG_INSTANTIATIONS (default_argument) = NULL;
28459 return default_argument;
28462 /* Begin parsing tentatively. We always save tokens while parsing
28463 tentatively so that if the tentative parsing fails we can restore the
28464 tokens. */
28466 static void
28467 cp_parser_parse_tentatively (cp_parser* parser)
28469 /* Enter a new parsing context. */
28470 parser->context = cp_parser_context_new (parser->context);
28471 /* Begin saving tokens. */
28472 cp_lexer_save_tokens (parser->lexer);
28473 /* In order to avoid repetitive access control error messages,
28474 access checks are queued up until we are no longer parsing
28475 tentatively. */
28476 push_deferring_access_checks (dk_deferred);
28479 /* Commit to the currently active tentative parse. */
28481 static void
28482 cp_parser_commit_to_tentative_parse (cp_parser* parser)
28484 cp_parser_context *context;
28485 cp_lexer *lexer;
28487 /* Mark all of the levels as committed. */
28488 lexer = parser->lexer;
28489 for (context = parser->context; context->next; context = context->next)
28491 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
28492 break;
28493 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
28494 while (!cp_lexer_saving_tokens (lexer))
28495 lexer = lexer->next;
28496 cp_lexer_commit_tokens (lexer);
28500 /* Commit to the topmost currently active tentative parse.
28502 Note that this function shouldn't be called when there are
28503 irreversible side-effects while in a tentative state. For
28504 example, we shouldn't create a permanent entry in the symbol
28505 table, or issue an error message that might not apply if the
28506 tentative parse is aborted. */
28508 static void
28509 cp_parser_commit_to_topmost_tentative_parse (cp_parser* parser)
28511 cp_parser_context *context = parser->context;
28512 cp_lexer *lexer = parser->lexer;
28514 if (context)
28516 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
28517 return;
28518 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
28520 while (!cp_lexer_saving_tokens (lexer))
28521 lexer = lexer->next;
28522 cp_lexer_commit_tokens (lexer);
28526 /* Abort the currently active tentative parse. All consumed tokens
28527 will be rolled back, and no diagnostics will be issued. */
28529 static void
28530 cp_parser_abort_tentative_parse (cp_parser* parser)
28532 gcc_assert (parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED
28533 || errorcount > 0);
28534 cp_parser_simulate_error (parser);
28535 /* Now, pretend that we want to see if the construct was
28536 successfully parsed. */
28537 cp_parser_parse_definitely (parser);
28540 /* Stop parsing tentatively. If a parse error has occurred, restore the
28541 token stream. Otherwise, commit to the tokens we have consumed.
28542 Returns true if no error occurred; false otherwise. */
28544 static bool
28545 cp_parser_parse_definitely (cp_parser* parser)
28547 bool error_occurred;
28548 cp_parser_context *context;
28550 /* Remember whether or not an error occurred, since we are about to
28551 destroy that information. */
28552 error_occurred = cp_parser_error_occurred (parser);
28553 /* Remove the topmost context from the stack. */
28554 context = parser->context;
28555 parser->context = context->next;
28556 /* If no parse errors occurred, commit to the tentative parse. */
28557 if (!error_occurred)
28559 /* Commit to the tokens read tentatively, unless that was
28560 already done. */
28561 if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
28562 cp_lexer_commit_tokens (parser->lexer);
28564 pop_to_parent_deferring_access_checks ();
28566 /* Otherwise, if errors occurred, roll back our state so that things
28567 are just as they were before we began the tentative parse. */
28568 else
28570 cp_lexer_rollback_tokens (parser->lexer);
28571 pop_deferring_access_checks ();
28573 /* Add the context to the front of the free list. */
28574 context->next = cp_parser_context_free_list;
28575 cp_parser_context_free_list = context;
28577 return !error_occurred;
28580 /* Returns true if we are parsing tentatively and are not committed to
28581 this tentative parse. */
28583 static bool
28584 cp_parser_uncommitted_to_tentative_parse_p (cp_parser* parser)
28586 return (cp_parser_parsing_tentatively (parser)
28587 && parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED);
28590 /* Returns nonzero iff an error has occurred during the most recent
28591 tentative parse. */
28593 static bool
28594 cp_parser_error_occurred (cp_parser* parser)
28596 return (cp_parser_parsing_tentatively (parser)
28597 && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
28600 /* Returns nonzero if GNU extensions are allowed. */
28602 static bool
28603 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
28605 return parser->allow_gnu_extensions_p;
28608 /* Objective-C++ Productions */
28611 /* Parse an Objective-C expression, which feeds into a primary-expression
28612 above.
28614 objc-expression:
28615 objc-message-expression
28616 objc-string-literal
28617 objc-encode-expression
28618 objc-protocol-expression
28619 objc-selector-expression
28621 Returns a tree representation of the expression. */
28623 static cp_expr
28624 cp_parser_objc_expression (cp_parser* parser)
28626 /* Try to figure out what kind of declaration is present. */
28627 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
28629 switch (kwd->type)
28631 case CPP_OPEN_SQUARE:
28632 return cp_parser_objc_message_expression (parser);
28634 case CPP_OBJC_STRING:
28635 kwd = cp_lexer_consume_token (parser->lexer);
28636 return objc_build_string_object (kwd->u.value);
28638 case CPP_KEYWORD:
28639 switch (kwd->keyword)
28641 case RID_AT_ENCODE:
28642 return cp_parser_objc_encode_expression (parser);
28644 case RID_AT_PROTOCOL:
28645 return cp_parser_objc_protocol_expression (parser);
28647 case RID_AT_SELECTOR:
28648 return cp_parser_objc_selector_expression (parser);
28650 default:
28651 break;
28653 default:
28654 error_at (kwd->location,
28655 "misplaced %<@%D%> Objective-C++ construct",
28656 kwd->u.value);
28657 cp_parser_skip_to_end_of_block_or_statement (parser);
28660 return error_mark_node;
28663 /* Parse an Objective-C message expression.
28665 objc-message-expression:
28666 [ objc-message-receiver objc-message-args ]
28668 Returns a representation of an Objective-C message. */
28670 static tree
28671 cp_parser_objc_message_expression (cp_parser* parser)
28673 tree receiver, messageargs;
28675 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
28676 cp_lexer_consume_token (parser->lexer); /* Eat '['. */
28677 receiver = cp_parser_objc_message_receiver (parser);
28678 messageargs = cp_parser_objc_message_args (parser);
28679 location_t end_loc = cp_lexer_peek_token (parser->lexer)->location;
28680 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
28682 tree result = objc_build_message_expr (receiver, messageargs);
28684 /* Construct a location e.g.
28685 [self func1:5]
28686 ^~~~~~~~~~~~~~
28687 ranging from the '[' to the ']', with the caret at the start. */
28688 location_t combined_loc = make_location (start_loc, start_loc, end_loc);
28689 protected_set_expr_location (result, combined_loc);
28691 return result;
28694 /* Parse an objc-message-receiver.
28696 objc-message-receiver:
28697 expression
28698 simple-type-specifier
28700 Returns a representation of the type or expression. */
28702 static tree
28703 cp_parser_objc_message_receiver (cp_parser* parser)
28705 tree rcv;
28707 /* An Objective-C message receiver may be either (1) a type
28708 or (2) an expression. */
28709 cp_parser_parse_tentatively (parser);
28710 rcv = cp_parser_expression (parser);
28712 /* If that worked out, fine. */
28713 if (cp_parser_parse_definitely (parser))
28714 return rcv;
28716 cp_parser_parse_tentatively (parser);
28717 rcv = cp_parser_simple_type_specifier (parser,
28718 /*decl_specs=*/NULL,
28719 CP_PARSER_FLAGS_NONE);
28721 if (cp_parser_parse_definitely (parser))
28722 return objc_get_class_reference (rcv);
28724 cp_parser_error (parser, "objective-c++ message receiver expected");
28725 return error_mark_node;
28728 /* Parse the arguments and selectors comprising an Objective-C message.
28730 objc-message-args:
28731 objc-selector
28732 objc-selector-args
28733 objc-selector-args , objc-comma-args
28735 objc-selector-args:
28736 objc-selector [opt] : assignment-expression
28737 objc-selector-args objc-selector [opt] : assignment-expression
28739 objc-comma-args:
28740 assignment-expression
28741 objc-comma-args , assignment-expression
28743 Returns a TREE_LIST, with TREE_PURPOSE containing a list of
28744 selector arguments and TREE_VALUE containing a list of comma
28745 arguments. */
28747 static tree
28748 cp_parser_objc_message_args (cp_parser* parser)
28750 tree sel_args = NULL_TREE, addl_args = NULL_TREE;
28751 bool maybe_unary_selector_p = true;
28752 cp_token *token = cp_lexer_peek_token (parser->lexer);
28754 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
28756 tree selector = NULL_TREE, arg;
28758 if (token->type != CPP_COLON)
28759 selector = cp_parser_objc_selector (parser);
28761 /* Detect if we have a unary selector. */
28762 if (maybe_unary_selector_p
28763 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
28764 return build_tree_list (selector, NULL_TREE);
28766 maybe_unary_selector_p = false;
28767 cp_parser_require (parser, CPP_COLON, RT_COLON);
28768 arg = cp_parser_assignment_expression (parser);
28770 sel_args
28771 = chainon (sel_args,
28772 build_tree_list (selector, arg));
28774 token = cp_lexer_peek_token (parser->lexer);
28777 /* Handle non-selector arguments, if any. */
28778 while (token->type == CPP_COMMA)
28780 tree arg;
28782 cp_lexer_consume_token (parser->lexer);
28783 arg = cp_parser_assignment_expression (parser);
28785 addl_args
28786 = chainon (addl_args,
28787 build_tree_list (NULL_TREE, arg));
28789 token = cp_lexer_peek_token (parser->lexer);
28792 if (sel_args == NULL_TREE && addl_args == NULL_TREE)
28794 cp_parser_error (parser, "objective-c++ message argument(s) are expected");
28795 return build_tree_list (error_mark_node, error_mark_node);
28798 return build_tree_list (sel_args, addl_args);
28801 /* Parse an Objective-C encode expression.
28803 objc-encode-expression:
28804 @encode objc-typename
28806 Returns an encoded representation of the type argument. */
28808 static cp_expr
28809 cp_parser_objc_encode_expression (cp_parser* parser)
28811 tree type;
28812 cp_token *token;
28813 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
28815 cp_lexer_consume_token (parser->lexer); /* Eat '@encode'. */
28816 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
28817 token = cp_lexer_peek_token (parser->lexer);
28818 type = complete_type (cp_parser_type_id (parser));
28819 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
28821 if (!type)
28823 error_at (token->location,
28824 "%<@encode%> must specify a type as an argument");
28825 return error_mark_node;
28828 /* This happens if we find @encode(T) (where T is a template
28829 typename or something dependent on a template typename) when
28830 parsing a template. In that case, we can't compile it
28831 immediately, but we rather create an AT_ENCODE_EXPR which will
28832 need to be instantiated when the template is used.
28834 if (dependent_type_p (type))
28836 tree value = build_min (AT_ENCODE_EXPR, size_type_node, type);
28837 TREE_READONLY (value) = 1;
28838 return value;
28842 /* Build a location of the form:
28843 @encode(int)
28844 ^~~~~~~~~~~~
28845 with caret==start at the @ token, finishing at the close paren. */
28846 location_t combined_loc
28847 = make_location (start_loc, start_loc,
28848 cp_lexer_previous_token (parser->lexer)->location);
28850 return cp_expr (objc_build_encode_expr (type), combined_loc);
28853 /* Parse an Objective-C @defs expression. */
28855 static tree
28856 cp_parser_objc_defs_expression (cp_parser *parser)
28858 tree name;
28860 cp_lexer_consume_token (parser->lexer); /* Eat '@defs'. */
28861 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
28862 name = cp_parser_identifier (parser);
28863 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
28865 return objc_get_class_ivars (name);
28868 /* Parse an Objective-C protocol expression.
28870 objc-protocol-expression:
28871 @protocol ( identifier )
28873 Returns a representation of the protocol expression. */
28875 static tree
28876 cp_parser_objc_protocol_expression (cp_parser* parser)
28878 tree proto;
28879 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
28881 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
28882 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
28883 proto = cp_parser_identifier (parser);
28884 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
28886 /* Build a location of the form:
28887 @protocol(prot)
28888 ^~~~~~~~~~~~~~~
28889 with caret==start at the @ token, finishing at the close paren. */
28890 location_t combined_loc
28891 = make_location (start_loc, start_loc,
28892 cp_lexer_previous_token (parser->lexer)->location);
28893 tree result = objc_build_protocol_expr (proto);
28894 protected_set_expr_location (result, combined_loc);
28895 return result;
28898 /* Parse an Objective-C selector expression.
28900 objc-selector-expression:
28901 @selector ( objc-method-signature )
28903 objc-method-signature:
28904 objc-selector
28905 objc-selector-seq
28907 objc-selector-seq:
28908 objc-selector :
28909 objc-selector-seq objc-selector :
28911 Returns a representation of the method selector. */
28913 static tree
28914 cp_parser_objc_selector_expression (cp_parser* parser)
28916 tree sel_seq = NULL_TREE;
28917 bool maybe_unary_selector_p = true;
28918 cp_token *token;
28919 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
28921 cp_lexer_consume_token (parser->lexer); /* Eat '@selector'. */
28922 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
28923 token = cp_lexer_peek_token (parser->lexer);
28925 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON
28926 || token->type == CPP_SCOPE)
28928 tree selector = NULL_TREE;
28930 if (token->type != CPP_COLON
28931 || token->type == CPP_SCOPE)
28932 selector = cp_parser_objc_selector (parser);
28934 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON)
28935 && cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
28937 /* Detect if we have a unary selector. */
28938 if (maybe_unary_selector_p)
28940 sel_seq = selector;
28941 goto finish_selector;
28943 else
28945 cp_parser_error (parser, "expected %<:%>");
28948 maybe_unary_selector_p = false;
28949 token = cp_lexer_consume_token (parser->lexer);
28951 if (token->type == CPP_SCOPE)
28953 sel_seq
28954 = chainon (sel_seq,
28955 build_tree_list (selector, NULL_TREE));
28956 sel_seq
28957 = chainon (sel_seq,
28958 build_tree_list (NULL_TREE, NULL_TREE));
28960 else
28961 sel_seq
28962 = chainon (sel_seq,
28963 build_tree_list (selector, NULL_TREE));
28965 token = cp_lexer_peek_token (parser->lexer);
28968 finish_selector:
28969 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
28972 /* Build a location of the form:
28973 @selector(func)
28974 ^~~~~~~~~~~~~~~
28975 with caret==start at the @ token, finishing at the close paren. */
28976 location_t combined_loc
28977 = make_location (loc, loc,
28978 cp_lexer_previous_token (parser->lexer)->location);
28979 tree result = objc_build_selector_expr (combined_loc, sel_seq);
28980 /* TODO: objc_build_selector_expr doesn't always honor the location. */
28981 protected_set_expr_location (result, combined_loc);
28982 return result;
28985 /* Parse a list of identifiers.
28987 objc-identifier-list:
28988 identifier
28989 objc-identifier-list , identifier
28991 Returns a TREE_LIST of identifier nodes. */
28993 static tree
28994 cp_parser_objc_identifier_list (cp_parser* parser)
28996 tree identifier;
28997 tree list;
28998 cp_token *sep;
29000 identifier = cp_parser_identifier (parser);
29001 if (identifier == error_mark_node)
29002 return error_mark_node;
29004 list = build_tree_list (NULL_TREE, identifier);
29005 sep = cp_lexer_peek_token (parser->lexer);
29007 while (sep->type == CPP_COMMA)
29009 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
29010 identifier = cp_parser_identifier (parser);
29011 if (identifier == error_mark_node)
29012 return list;
29014 list = chainon (list, build_tree_list (NULL_TREE,
29015 identifier));
29016 sep = cp_lexer_peek_token (parser->lexer);
29019 return list;
29022 /* Parse an Objective-C alias declaration.
29024 objc-alias-declaration:
29025 @compatibility_alias identifier identifier ;
29027 This function registers the alias mapping with the Objective-C front end.
29028 It returns nothing. */
29030 static void
29031 cp_parser_objc_alias_declaration (cp_parser* parser)
29033 tree alias, orig;
29035 cp_lexer_consume_token (parser->lexer); /* Eat '@compatibility_alias'. */
29036 alias = cp_parser_identifier (parser);
29037 orig = cp_parser_identifier (parser);
29038 objc_declare_alias (alias, orig);
29039 cp_parser_consume_semicolon_at_end_of_statement (parser);
29042 /* Parse an Objective-C class forward-declaration.
29044 objc-class-declaration:
29045 @class objc-identifier-list ;
29047 The function registers the forward declarations with the Objective-C
29048 front end. It returns nothing. */
29050 static void
29051 cp_parser_objc_class_declaration (cp_parser* parser)
29053 cp_lexer_consume_token (parser->lexer); /* Eat '@class'. */
29054 while (true)
29056 tree id;
29058 id = cp_parser_identifier (parser);
29059 if (id == error_mark_node)
29060 break;
29062 objc_declare_class (id);
29064 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
29065 cp_lexer_consume_token (parser->lexer);
29066 else
29067 break;
29069 cp_parser_consume_semicolon_at_end_of_statement (parser);
29072 /* Parse a list of Objective-C protocol references.
29074 objc-protocol-refs-opt:
29075 objc-protocol-refs [opt]
29077 objc-protocol-refs:
29078 < objc-identifier-list >
29080 Returns a TREE_LIST of identifiers, if any. */
29082 static tree
29083 cp_parser_objc_protocol_refs_opt (cp_parser* parser)
29085 tree protorefs = NULL_TREE;
29087 if(cp_lexer_next_token_is (parser->lexer, CPP_LESS))
29089 cp_lexer_consume_token (parser->lexer); /* Eat '<'. */
29090 protorefs = cp_parser_objc_identifier_list (parser);
29091 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
29094 return protorefs;
29097 /* Parse a Objective-C visibility specification. */
29099 static void
29100 cp_parser_objc_visibility_spec (cp_parser* parser)
29102 cp_token *vis = cp_lexer_peek_token (parser->lexer);
29104 switch (vis->keyword)
29106 case RID_AT_PRIVATE:
29107 objc_set_visibility (OBJC_IVAR_VIS_PRIVATE);
29108 break;
29109 case RID_AT_PROTECTED:
29110 objc_set_visibility (OBJC_IVAR_VIS_PROTECTED);
29111 break;
29112 case RID_AT_PUBLIC:
29113 objc_set_visibility (OBJC_IVAR_VIS_PUBLIC);
29114 break;
29115 case RID_AT_PACKAGE:
29116 objc_set_visibility (OBJC_IVAR_VIS_PACKAGE);
29117 break;
29118 default:
29119 return;
29122 /* Eat '@private'/'@protected'/'@public'. */
29123 cp_lexer_consume_token (parser->lexer);
29126 /* Parse an Objective-C method type. Return 'true' if it is a class
29127 (+) method, and 'false' if it is an instance (-) method. */
29129 static inline bool
29130 cp_parser_objc_method_type (cp_parser* parser)
29132 if (cp_lexer_consume_token (parser->lexer)->type == CPP_PLUS)
29133 return true;
29134 else
29135 return false;
29138 /* Parse an Objective-C protocol qualifier. */
29140 static tree
29141 cp_parser_objc_protocol_qualifiers (cp_parser* parser)
29143 tree quals = NULL_TREE, node;
29144 cp_token *token = cp_lexer_peek_token (parser->lexer);
29146 node = token->u.value;
29148 while (node && identifier_p (node)
29149 && (node == ridpointers [(int) RID_IN]
29150 || node == ridpointers [(int) RID_OUT]
29151 || node == ridpointers [(int) RID_INOUT]
29152 || node == ridpointers [(int) RID_BYCOPY]
29153 || node == ridpointers [(int) RID_BYREF]
29154 || node == ridpointers [(int) RID_ONEWAY]))
29156 quals = tree_cons (NULL_TREE, node, quals);
29157 cp_lexer_consume_token (parser->lexer);
29158 token = cp_lexer_peek_token (parser->lexer);
29159 node = token->u.value;
29162 return quals;
29165 /* Parse an Objective-C typename. */
29167 static tree
29168 cp_parser_objc_typename (cp_parser* parser)
29170 tree type_name = NULL_TREE;
29172 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
29174 tree proto_quals, cp_type = NULL_TREE;
29176 cp_lexer_consume_token (parser->lexer); /* Eat '('. */
29177 proto_quals = cp_parser_objc_protocol_qualifiers (parser);
29179 /* An ObjC type name may consist of just protocol qualifiers, in which
29180 case the type shall default to 'id'. */
29181 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
29183 cp_type = cp_parser_type_id (parser);
29185 /* If the type could not be parsed, an error has already
29186 been produced. For error recovery, behave as if it had
29187 not been specified, which will use the default type
29188 'id'. */
29189 if (cp_type == error_mark_node)
29191 cp_type = NULL_TREE;
29192 /* We need to skip to the closing parenthesis as
29193 cp_parser_type_id() does not seem to do it for
29194 us. */
29195 cp_parser_skip_to_closing_parenthesis (parser,
29196 /*recovering=*/true,
29197 /*or_comma=*/false,
29198 /*consume_paren=*/false);
29202 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
29203 type_name = build_tree_list (proto_quals, cp_type);
29206 return type_name;
29209 /* Check to see if TYPE refers to an Objective-C selector name. */
29211 static bool
29212 cp_parser_objc_selector_p (enum cpp_ttype type)
29214 return (type == CPP_NAME || type == CPP_KEYWORD
29215 || type == CPP_AND_AND || type == CPP_AND_EQ || type == CPP_AND
29216 || type == CPP_OR || type == CPP_COMPL || type == CPP_NOT
29217 || type == CPP_NOT_EQ || type == CPP_OR_OR || type == CPP_OR_EQ
29218 || type == CPP_XOR || type == CPP_XOR_EQ);
29221 /* Parse an Objective-C selector. */
29223 static tree
29224 cp_parser_objc_selector (cp_parser* parser)
29226 cp_token *token = cp_lexer_consume_token (parser->lexer);
29228 if (!cp_parser_objc_selector_p (token->type))
29230 error_at (token->location, "invalid Objective-C++ selector name");
29231 return error_mark_node;
29234 /* C++ operator names are allowed to appear in ObjC selectors. */
29235 switch (token->type)
29237 case CPP_AND_AND: return get_identifier ("and");
29238 case CPP_AND_EQ: return get_identifier ("and_eq");
29239 case CPP_AND: return get_identifier ("bitand");
29240 case CPP_OR: return get_identifier ("bitor");
29241 case CPP_COMPL: return get_identifier ("compl");
29242 case CPP_NOT: return get_identifier ("not");
29243 case CPP_NOT_EQ: return get_identifier ("not_eq");
29244 case CPP_OR_OR: return get_identifier ("or");
29245 case CPP_OR_EQ: return get_identifier ("or_eq");
29246 case CPP_XOR: return get_identifier ("xor");
29247 case CPP_XOR_EQ: return get_identifier ("xor_eq");
29248 default: return token->u.value;
29252 /* Parse an Objective-C params list. */
29254 static tree
29255 cp_parser_objc_method_keyword_params (cp_parser* parser, tree* attributes)
29257 tree params = NULL_TREE;
29258 bool maybe_unary_selector_p = true;
29259 cp_token *token = cp_lexer_peek_token (parser->lexer);
29261 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
29263 tree selector = NULL_TREE, type_name, identifier;
29264 tree parm_attr = NULL_TREE;
29266 if (token->keyword == RID_ATTRIBUTE)
29267 break;
29269 if (token->type != CPP_COLON)
29270 selector = cp_parser_objc_selector (parser);
29272 /* Detect if we have a unary selector. */
29273 if (maybe_unary_selector_p
29274 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
29276 params = selector; /* Might be followed by attributes. */
29277 break;
29280 maybe_unary_selector_p = false;
29281 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
29283 /* Something went quite wrong. There should be a colon
29284 here, but there is not. Stop parsing parameters. */
29285 break;
29287 type_name = cp_parser_objc_typename (parser);
29288 /* New ObjC allows attributes on parameters too. */
29289 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
29290 parm_attr = cp_parser_attributes_opt (parser);
29291 identifier = cp_parser_identifier (parser);
29293 params
29294 = chainon (params,
29295 objc_build_keyword_decl (selector,
29296 type_name,
29297 identifier,
29298 parm_attr));
29300 token = cp_lexer_peek_token (parser->lexer);
29303 if (params == NULL_TREE)
29305 cp_parser_error (parser, "objective-c++ method declaration is expected");
29306 return error_mark_node;
29309 /* We allow tail attributes for the method. */
29310 if (token->keyword == RID_ATTRIBUTE)
29312 *attributes = cp_parser_attributes_opt (parser);
29313 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
29314 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
29315 return params;
29316 cp_parser_error (parser,
29317 "method attributes must be specified at the end");
29318 return error_mark_node;
29321 if (params == NULL_TREE)
29323 cp_parser_error (parser, "objective-c++ method declaration is expected");
29324 return error_mark_node;
29326 return params;
29329 /* Parse the non-keyword Objective-C params. */
29331 static tree
29332 cp_parser_objc_method_tail_params_opt (cp_parser* parser, bool *ellipsisp,
29333 tree* attributes)
29335 tree params = make_node (TREE_LIST);
29336 cp_token *token = cp_lexer_peek_token (parser->lexer);
29337 *ellipsisp = false; /* Initially, assume no ellipsis. */
29339 while (token->type == CPP_COMMA)
29341 cp_parameter_declarator *parmdecl;
29342 tree parm;
29344 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
29345 token = cp_lexer_peek_token (parser->lexer);
29347 if (token->type == CPP_ELLIPSIS)
29349 cp_lexer_consume_token (parser->lexer); /* Eat '...'. */
29350 *ellipsisp = true;
29351 token = cp_lexer_peek_token (parser->lexer);
29352 break;
29355 /* TODO: parse attributes for tail parameters. */
29356 parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
29357 parm = grokdeclarator (parmdecl->declarator,
29358 &parmdecl->decl_specifiers,
29359 PARM, /*initialized=*/0,
29360 /*attrlist=*/NULL);
29362 chainon (params, build_tree_list (NULL_TREE, parm));
29363 token = cp_lexer_peek_token (parser->lexer);
29366 /* We allow tail attributes for the method. */
29367 if (token->keyword == RID_ATTRIBUTE)
29369 if (*attributes == NULL_TREE)
29371 *attributes = cp_parser_attributes_opt (parser);
29372 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
29373 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
29374 return params;
29376 else
29377 /* We have an error, but parse the attributes, so that we can
29378 carry on. */
29379 *attributes = cp_parser_attributes_opt (parser);
29381 cp_parser_error (parser,
29382 "method attributes must be specified at the end");
29383 return error_mark_node;
29386 return params;
29389 /* Parse a linkage specification, a pragma, an extra semicolon or a block. */
29391 static void
29392 cp_parser_objc_interstitial_code (cp_parser* parser)
29394 cp_token *token = cp_lexer_peek_token (parser->lexer);
29396 /* If the next token is `extern' and the following token is a string
29397 literal, then we have a linkage specification. */
29398 if (token->keyword == RID_EXTERN
29399 && cp_parser_is_pure_string_literal
29400 (cp_lexer_peek_nth_token (parser->lexer, 2)))
29401 cp_parser_linkage_specification (parser);
29402 /* Handle #pragma, if any. */
29403 else if (token->type == CPP_PRAGMA)
29404 cp_parser_pragma (parser, pragma_objc_icode, NULL);
29405 /* Allow stray semicolons. */
29406 else if (token->type == CPP_SEMICOLON)
29407 cp_lexer_consume_token (parser->lexer);
29408 /* Mark methods as optional or required, when building protocols. */
29409 else if (token->keyword == RID_AT_OPTIONAL)
29411 cp_lexer_consume_token (parser->lexer);
29412 objc_set_method_opt (true);
29414 else if (token->keyword == RID_AT_REQUIRED)
29416 cp_lexer_consume_token (parser->lexer);
29417 objc_set_method_opt (false);
29419 else if (token->keyword == RID_NAMESPACE)
29420 cp_parser_namespace_definition (parser);
29421 /* Other stray characters must generate errors. */
29422 else if (token->type == CPP_OPEN_BRACE || token->type == CPP_CLOSE_BRACE)
29424 cp_lexer_consume_token (parser->lexer);
29425 error ("stray %qs between Objective-C++ methods",
29426 token->type == CPP_OPEN_BRACE ? "{" : "}");
29428 /* Finally, try to parse a block-declaration, or a function-definition. */
29429 else
29430 cp_parser_block_declaration (parser, /*statement_p=*/false);
29433 /* Parse a method signature. */
29435 static tree
29436 cp_parser_objc_method_signature (cp_parser* parser, tree* attributes)
29438 tree rettype, kwdparms, optparms;
29439 bool ellipsis = false;
29440 bool is_class_method;
29442 is_class_method = cp_parser_objc_method_type (parser);
29443 rettype = cp_parser_objc_typename (parser);
29444 *attributes = NULL_TREE;
29445 kwdparms = cp_parser_objc_method_keyword_params (parser, attributes);
29446 if (kwdparms == error_mark_node)
29447 return error_mark_node;
29448 optparms = cp_parser_objc_method_tail_params_opt (parser, &ellipsis, attributes);
29449 if (optparms == error_mark_node)
29450 return error_mark_node;
29452 return objc_build_method_signature (is_class_method, rettype, kwdparms, optparms, ellipsis);
29455 static bool
29456 cp_parser_objc_method_maybe_bad_prefix_attributes (cp_parser* parser)
29458 tree tattr;
29459 cp_lexer_save_tokens (parser->lexer);
29460 tattr = cp_parser_attributes_opt (parser);
29461 gcc_assert (tattr) ;
29463 /* If the attributes are followed by a method introducer, this is not allowed.
29464 Dump the attributes and flag the situation. */
29465 if (cp_lexer_next_token_is (parser->lexer, CPP_PLUS)
29466 || cp_lexer_next_token_is (parser->lexer, CPP_MINUS))
29467 return true;
29469 /* Otherwise, the attributes introduce some interstitial code, possibly so
29470 rewind to allow that check. */
29471 cp_lexer_rollback_tokens (parser->lexer);
29472 return false;
29475 /* Parse an Objective-C method prototype list. */
29477 static void
29478 cp_parser_objc_method_prototype_list (cp_parser* parser)
29480 cp_token *token = cp_lexer_peek_token (parser->lexer);
29482 while (token->keyword != RID_AT_END && token->type != CPP_EOF)
29484 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
29486 tree attributes, sig;
29487 bool is_class_method;
29488 if (token->type == CPP_PLUS)
29489 is_class_method = true;
29490 else
29491 is_class_method = false;
29492 sig = cp_parser_objc_method_signature (parser, &attributes);
29493 if (sig == error_mark_node)
29495 cp_parser_skip_to_end_of_block_or_statement (parser);
29496 token = cp_lexer_peek_token (parser->lexer);
29497 continue;
29499 objc_add_method_declaration (is_class_method, sig, attributes);
29500 cp_parser_consume_semicolon_at_end_of_statement (parser);
29502 else if (token->keyword == RID_AT_PROPERTY)
29503 cp_parser_objc_at_property_declaration (parser);
29504 else if (token->keyword == RID_ATTRIBUTE
29505 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
29506 warning_at (cp_lexer_peek_token (parser->lexer)->location,
29507 OPT_Wattributes,
29508 "prefix attributes are ignored for methods");
29509 else
29510 /* Allow for interspersed non-ObjC++ code. */
29511 cp_parser_objc_interstitial_code (parser);
29513 token = cp_lexer_peek_token (parser->lexer);
29516 if (token->type != CPP_EOF)
29517 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
29518 else
29519 cp_parser_error (parser, "expected %<@end%>");
29521 objc_finish_interface ();
29524 /* Parse an Objective-C method definition list. */
29526 static void
29527 cp_parser_objc_method_definition_list (cp_parser* parser)
29529 cp_token *token = cp_lexer_peek_token (parser->lexer);
29531 while (token->keyword != RID_AT_END && token->type != CPP_EOF)
29533 tree meth;
29535 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
29537 cp_token *ptk;
29538 tree sig, attribute;
29539 bool is_class_method;
29540 if (token->type == CPP_PLUS)
29541 is_class_method = true;
29542 else
29543 is_class_method = false;
29544 push_deferring_access_checks (dk_deferred);
29545 sig = cp_parser_objc_method_signature (parser, &attribute);
29546 if (sig == error_mark_node)
29548 cp_parser_skip_to_end_of_block_or_statement (parser);
29549 token = cp_lexer_peek_token (parser->lexer);
29550 continue;
29552 objc_start_method_definition (is_class_method, sig, attribute,
29553 NULL_TREE);
29555 /* For historical reasons, we accept an optional semicolon. */
29556 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
29557 cp_lexer_consume_token (parser->lexer);
29559 ptk = cp_lexer_peek_token (parser->lexer);
29560 if (!(ptk->type == CPP_PLUS || ptk->type == CPP_MINUS
29561 || ptk->type == CPP_EOF || ptk->keyword == RID_AT_END))
29563 perform_deferred_access_checks (tf_warning_or_error);
29564 stop_deferring_access_checks ();
29565 meth = cp_parser_function_definition_after_declarator (parser,
29566 false);
29567 pop_deferring_access_checks ();
29568 objc_finish_method_definition (meth);
29571 /* The following case will be removed once @synthesize is
29572 completely implemented. */
29573 else if (token->keyword == RID_AT_PROPERTY)
29574 cp_parser_objc_at_property_declaration (parser);
29575 else if (token->keyword == RID_AT_SYNTHESIZE)
29576 cp_parser_objc_at_synthesize_declaration (parser);
29577 else if (token->keyword == RID_AT_DYNAMIC)
29578 cp_parser_objc_at_dynamic_declaration (parser);
29579 else if (token->keyword == RID_ATTRIBUTE
29580 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
29581 warning_at (token->location, OPT_Wattributes,
29582 "prefix attributes are ignored for methods");
29583 else
29584 /* Allow for interspersed non-ObjC++ code. */
29585 cp_parser_objc_interstitial_code (parser);
29587 token = cp_lexer_peek_token (parser->lexer);
29590 if (token->type != CPP_EOF)
29591 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
29592 else
29593 cp_parser_error (parser, "expected %<@end%>");
29595 objc_finish_implementation ();
29598 /* Parse Objective-C ivars. */
29600 static void
29601 cp_parser_objc_class_ivars (cp_parser* parser)
29603 cp_token *token = cp_lexer_peek_token (parser->lexer);
29605 if (token->type != CPP_OPEN_BRACE)
29606 return; /* No ivars specified. */
29608 cp_lexer_consume_token (parser->lexer); /* Eat '{'. */
29609 token = cp_lexer_peek_token (parser->lexer);
29611 while (token->type != CPP_CLOSE_BRACE
29612 && token->keyword != RID_AT_END && token->type != CPP_EOF)
29614 cp_decl_specifier_seq declspecs;
29615 int decl_class_or_enum_p;
29616 tree prefix_attributes;
29618 cp_parser_objc_visibility_spec (parser);
29620 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
29621 break;
29623 cp_parser_decl_specifier_seq (parser,
29624 CP_PARSER_FLAGS_OPTIONAL,
29625 &declspecs,
29626 &decl_class_or_enum_p);
29628 /* auto, register, static, extern, mutable. */
29629 if (declspecs.storage_class != sc_none)
29631 cp_parser_error (parser, "invalid type for instance variable");
29632 declspecs.storage_class = sc_none;
29635 /* thread_local. */
29636 if (decl_spec_seq_has_spec_p (&declspecs, ds_thread))
29638 cp_parser_error (parser, "invalid type for instance variable");
29639 declspecs.locations[ds_thread] = 0;
29642 /* typedef. */
29643 if (decl_spec_seq_has_spec_p (&declspecs, ds_typedef))
29645 cp_parser_error (parser, "invalid type for instance variable");
29646 declspecs.locations[ds_typedef] = 0;
29649 prefix_attributes = declspecs.attributes;
29650 declspecs.attributes = NULL_TREE;
29652 /* Keep going until we hit the `;' at the end of the
29653 declaration. */
29654 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
29656 tree width = NULL_TREE, attributes, first_attribute, decl;
29657 cp_declarator *declarator = NULL;
29658 int ctor_dtor_or_conv_p;
29660 /* Check for a (possibly unnamed) bitfield declaration. */
29661 token = cp_lexer_peek_token (parser->lexer);
29662 if (token->type == CPP_COLON)
29663 goto eat_colon;
29665 if (token->type == CPP_NAME
29666 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
29667 == CPP_COLON))
29669 /* Get the name of the bitfield. */
29670 declarator = make_id_declarator (NULL_TREE,
29671 cp_parser_identifier (parser),
29672 sfk_none);
29674 eat_colon:
29675 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
29676 /* Get the width of the bitfield. */
29677 width
29678 = cp_parser_constant_expression (parser);
29680 else
29682 /* Parse the declarator. */
29683 declarator
29684 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
29685 &ctor_dtor_or_conv_p,
29686 /*parenthesized_p=*/NULL,
29687 /*member_p=*/false,
29688 /*friend_p=*/false);
29691 /* Look for attributes that apply to the ivar. */
29692 attributes = cp_parser_attributes_opt (parser);
29693 /* Remember which attributes are prefix attributes and
29694 which are not. */
29695 first_attribute = attributes;
29696 /* Combine the attributes. */
29697 attributes = chainon (prefix_attributes, attributes);
29699 if (width)
29700 /* Create the bitfield declaration. */
29701 decl = grokbitfield (declarator, &declspecs,
29702 width,
29703 attributes);
29704 else
29705 decl = grokfield (declarator, &declspecs,
29706 NULL_TREE, /*init_const_expr_p=*/false,
29707 NULL_TREE, attributes);
29709 /* Add the instance variable. */
29710 if (decl != error_mark_node && decl != NULL_TREE)
29711 objc_add_instance_variable (decl);
29713 /* Reset PREFIX_ATTRIBUTES. */
29714 while (attributes && TREE_CHAIN (attributes) != first_attribute)
29715 attributes = TREE_CHAIN (attributes);
29716 if (attributes)
29717 TREE_CHAIN (attributes) = NULL_TREE;
29719 token = cp_lexer_peek_token (parser->lexer);
29721 if (token->type == CPP_COMMA)
29723 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
29724 continue;
29726 break;
29729 cp_parser_consume_semicolon_at_end_of_statement (parser);
29730 token = cp_lexer_peek_token (parser->lexer);
29733 if (token->keyword == RID_AT_END)
29734 cp_parser_error (parser, "expected %<}%>");
29736 /* Do not consume the RID_AT_END, so it will be read again as terminating
29737 the @interface of @implementation. */
29738 if (token->keyword != RID_AT_END && token->type != CPP_EOF)
29739 cp_lexer_consume_token (parser->lexer); /* Eat '}'. */
29741 /* For historical reasons, we accept an optional semicolon. */
29742 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
29743 cp_lexer_consume_token (parser->lexer);
29746 /* Parse an Objective-C protocol declaration. */
29748 static void
29749 cp_parser_objc_protocol_declaration (cp_parser* parser, tree attributes)
29751 tree proto, protorefs;
29752 cp_token *tok;
29754 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
29755 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
29757 tok = cp_lexer_peek_token (parser->lexer);
29758 error_at (tok->location, "identifier expected after %<@protocol%>");
29759 cp_parser_consume_semicolon_at_end_of_statement (parser);
29760 return;
29763 /* See if we have a forward declaration or a definition. */
29764 tok = cp_lexer_peek_nth_token (parser->lexer, 2);
29766 /* Try a forward declaration first. */
29767 if (tok->type == CPP_COMMA || tok->type == CPP_SEMICOLON)
29769 while (true)
29771 tree id;
29773 id = cp_parser_identifier (parser);
29774 if (id == error_mark_node)
29775 break;
29777 objc_declare_protocol (id, attributes);
29779 if(cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
29780 cp_lexer_consume_token (parser->lexer);
29781 else
29782 break;
29784 cp_parser_consume_semicolon_at_end_of_statement (parser);
29787 /* Ok, we got a full-fledged definition (or at least should). */
29788 else
29790 proto = cp_parser_identifier (parser);
29791 protorefs = cp_parser_objc_protocol_refs_opt (parser);
29792 objc_start_protocol (proto, protorefs, attributes);
29793 cp_parser_objc_method_prototype_list (parser);
29797 /* Parse an Objective-C superclass or category. */
29799 static void
29800 cp_parser_objc_superclass_or_category (cp_parser *parser,
29801 bool iface_p,
29802 tree *super,
29803 tree *categ, bool *is_class_extension)
29805 cp_token *next = cp_lexer_peek_token (parser->lexer);
29807 *super = *categ = NULL_TREE;
29808 *is_class_extension = false;
29809 if (next->type == CPP_COLON)
29811 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
29812 *super = cp_parser_identifier (parser);
29814 else if (next->type == CPP_OPEN_PAREN)
29816 cp_lexer_consume_token (parser->lexer); /* Eat '('. */
29818 /* If there is no category name, and this is an @interface, we
29819 have a class extension. */
29820 if (iface_p && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
29822 *categ = NULL_TREE;
29823 *is_class_extension = true;
29825 else
29826 *categ = cp_parser_identifier (parser);
29828 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
29832 /* Parse an Objective-C class interface. */
29834 static void
29835 cp_parser_objc_class_interface (cp_parser* parser, tree attributes)
29837 tree name, super, categ, protos;
29838 bool is_class_extension;
29840 cp_lexer_consume_token (parser->lexer); /* Eat '@interface'. */
29841 name = cp_parser_identifier (parser);
29842 if (name == error_mark_node)
29844 /* It's hard to recover because even if valid @interface stuff
29845 is to follow, we can't compile it (or validate it) if we
29846 don't even know which class it refers to. Let's assume this
29847 was a stray '@interface' token in the stream and skip it.
29849 return;
29851 cp_parser_objc_superclass_or_category (parser, true, &super, &categ,
29852 &is_class_extension);
29853 protos = cp_parser_objc_protocol_refs_opt (parser);
29855 /* We have either a class or a category on our hands. */
29856 if (categ || is_class_extension)
29857 objc_start_category_interface (name, categ, protos, attributes);
29858 else
29860 objc_start_class_interface (name, super, protos, attributes);
29861 /* Handle instance variable declarations, if any. */
29862 cp_parser_objc_class_ivars (parser);
29863 objc_continue_interface ();
29866 cp_parser_objc_method_prototype_list (parser);
29869 /* Parse an Objective-C class implementation. */
29871 static void
29872 cp_parser_objc_class_implementation (cp_parser* parser)
29874 tree name, super, categ;
29875 bool is_class_extension;
29877 cp_lexer_consume_token (parser->lexer); /* Eat '@implementation'. */
29878 name = cp_parser_identifier (parser);
29879 if (name == error_mark_node)
29881 /* It's hard to recover because even if valid @implementation
29882 stuff is to follow, we can't compile it (or validate it) if
29883 we don't even know which class it refers to. Let's assume
29884 this was a stray '@implementation' token in the stream and
29885 skip it.
29887 return;
29889 cp_parser_objc_superclass_or_category (parser, false, &super, &categ,
29890 &is_class_extension);
29892 /* We have either a class or a category on our hands. */
29893 if (categ)
29894 objc_start_category_implementation (name, categ);
29895 else
29897 objc_start_class_implementation (name, super);
29898 /* Handle instance variable declarations, if any. */
29899 cp_parser_objc_class_ivars (parser);
29900 objc_continue_implementation ();
29903 cp_parser_objc_method_definition_list (parser);
29906 /* Consume the @end token and finish off the implementation. */
29908 static void
29909 cp_parser_objc_end_implementation (cp_parser* parser)
29911 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
29912 objc_finish_implementation ();
29915 /* Parse an Objective-C declaration. */
29917 static void
29918 cp_parser_objc_declaration (cp_parser* parser, tree attributes)
29920 /* Try to figure out what kind of declaration is present. */
29921 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
29923 if (attributes)
29924 switch (kwd->keyword)
29926 case RID_AT_ALIAS:
29927 case RID_AT_CLASS:
29928 case RID_AT_END:
29929 error_at (kwd->location, "attributes may not be specified before"
29930 " the %<@%D%> Objective-C++ keyword",
29931 kwd->u.value);
29932 attributes = NULL;
29933 break;
29934 case RID_AT_IMPLEMENTATION:
29935 warning_at (kwd->location, OPT_Wattributes,
29936 "prefix attributes are ignored before %<@%D%>",
29937 kwd->u.value);
29938 attributes = NULL;
29939 default:
29940 break;
29943 switch (kwd->keyword)
29945 case RID_AT_ALIAS:
29946 cp_parser_objc_alias_declaration (parser);
29947 break;
29948 case RID_AT_CLASS:
29949 cp_parser_objc_class_declaration (parser);
29950 break;
29951 case RID_AT_PROTOCOL:
29952 cp_parser_objc_protocol_declaration (parser, attributes);
29953 break;
29954 case RID_AT_INTERFACE:
29955 cp_parser_objc_class_interface (parser, attributes);
29956 break;
29957 case RID_AT_IMPLEMENTATION:
29958 cp_parser_objc_class_implementation (parser);
29959 break;
29960 case RID_AT_END:
29961 cp_parser_objc_end_implementation (parser);
29962 break;
29963 default:
29964 error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
29965 kwd->u.value);
29966 cp_parser_skip_to_end_of_block_or_statement (parser);
29970 /* Parse an Objective-C try-catch-finally statement.
29972 objc-try-catch-finally-stmt:
29973 @try compound-statement objc-catch-clause-seq [opt]
29974 objc-finally-clause [opt]
29976 objc-catch-clause-seq:
29977 objc-catch-clause objc-catch-clause-seq [opt]
29979 objc-catch-clause:
29980 @catch ( objc-exception-declaration ) compound-statement
29982 objc-finally-clause:
29983 @finally compound-statement
29985 objc-exception-declaration:
29986 parameter-declaration
29987 '...'
29989 where '...' is to be interpreted literally, that is, it means CPP_ELLIPSIS.
29991 Returns NULL_TREE.
29993 PS: This function is identical to c_parser_objc_try_catch_finally_statement
29994 for C. Keep them in sync. */
29996 static tree
29997 cp_parser_objc_try_catch_finally_statement (cp_parser *parser)
29999 location_t location;
30000 tree stmt;
30002 cp_parser_require_keyword (parser, RID_AT_TRY, RT_AT_TRY);
30003 location = cp_lexer_peek_token (parser->lexer)->location;
30004 objc_maybe_warn_exceptions (location);
30005 /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
30006 node, lest it get absorbed into the surrounding block. */
30007 stmt = push_stmt_list ();
30008 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
30009 objc_begin_try_stmt (location, pop_stmt_list (stmt));
30011 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_CATCH))
30013 cp_parameter_declarator *parm;
30014 tree parameter_declaration = error_mark_node;
30015 bool seen_open_paren = false;
30017 cp_lexer_consume_token (parser->lexer);
30018 if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
30019 seen_open_paren = true;
30020 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
30022 /* We have "@catch (...)" (where the '...' are literally
30023 what is in the code). Skip the '...'.
30024 parameter_declaration is set to NULL_TREE, and
30025 objc_being_catch_clauses() knows that that means
30026 '...'. */
30027 cp_lexer_consume_token (parser->lexer);
30028 parameter_declaration = NULL_TREE;
30030 else
30032 /* We have "@catch (NSException *exception)" or something
30033 like that. Parse the parameter declaration. */
30034 parm = cp_parser_parameter_declaration (parser, false, NULL);
30035 if (parm == NULL)
30036 parameter_declaration = error_mark_node;
30037 else
30038 parameter_declaration = grokdeclarator (parm->declarator,
30039 &parm->decl_specifiers,
30040 PARM, /*initialized=*/0,
30041 /*attrlist=*/NULL);
30043 if (seen_open_paren)
30044 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
30045 else
30047 /* If there was no open parenthesis, we are recovering from
30048 an error, and we are trying to figure out what mistake
30049 the user has made. */
30051 /* If there is an immediate closing parenthesis, the user
30052 probably forgot the opening one (ie, they typed "@catch
30053 NSException *e)". Parse the closing parenthesis and keep
30054 going. */
30055 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
30056 cp_lexer_consume_token (parser->lexer);
30058 /* If these is no immediate closing parenthesis, the user
30059 probably doesn't know that parenthesis are required at
30060 all (ie, they typed "@catch NSException *e"). So, just
30061 forget about the closing parenthesis and keep going. */
30063 objc_begin_catch_clause (parameter_declaration);
30064 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
30065 objc_finish_catch_clause ();
30067 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_FINALLY))
30069 cp_lexer_consume_token (parser->lexer);
30070 location = cp_lexer_peek_token (parser->lexer)->location;
30071 /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
30072 node, lest it get absorbed into the surrounding block. */
30073 stmt = push_stmt_list ();
30074 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
30075 objc_build_finally_clause (location, pop_stmt_list (stmt));
30078 return objc_finish_try_stmt ();
30081 /* Parse an Objective-C synchronized statement.
30083 objc-synchronized-stmt:
30084 @synchronized ( expression ) compound-statement
30086 Returns NULL_TREE. */
30088 static tree
30089 cp_parser_objc_synchronized_statement (cp_parser *parser)
30091 location_t location;
30092 tree lock, stmt;
30094 cp_parser_require_keyword (parser, RID_AT_SYNCHRONIZED, RT_AT_SYNCHRONIZED);
30096 location = cp_lexer_peek_token (parser->lexer)->location;
30097 objc_maybe_warn_exceptions (location);
30098 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
30099 lock = cp_parser_expression (parser);
30100 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
30102 /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
30103 node, lest it get absorbed into the surrounding block. */
30104 stmt = push_stmt_list ();
30105 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
30107 return objc_build_synchronized (location, lock, pop_stmt_list (stmt));
30110 /* Parse an Objective-C throw statement.
30112 objc-throw-stmt:
30113 @throw assignment-expression [opt] ;
30115 Returns a constructed '@throw' statement. */
30117 static tree
30118 cp_parser_objc_throw_statement (cp_parser *parser)
30120 tree expr = NULL_TREE;
30121 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
30123 cp_parser_require_keyword (parser, RID_AT_THROW, RT_AT_THROW);
30125 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
30126 expr = cp_parser_expression (parser);
30128 cp_parser_consume_semicolon_at_end_of_statement (parser);
30130 return objc_build_throw_stmt (loc, expr);
30133 /* Parse an Objective-C statement. */
30135 static tree
30136 cp_parser_objc_statement (cp_parser * parser)
30138 /* Try to figure out what kind of declaration is present. */
30139 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
30141 switch (kwd->keyword)
30143 case RID_AT_TRY:
30144 return cp_parser_objc_try_catch_finally_statement (parser);
30145 case RID_AT_SYNCHRONIZED:
30146 return cp_parser_objc_synchronized_statement (parser);
30147 case RID_AT_THROW:
30148 return cp_parser_objc_throw_statement (parser);
30149 default:
30150 error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
30151 kwd->u.value);
30152 cp_parser_skip_to_end_of_block_or_statement (parser);
30155 return error_mark_node;
30158 /* If we are compiling ObjC++ and we see an __attribute__ we neeed to
30159 look ahead to see if an objc keyword follows the attributes. This
30160 is to detect the use of prefix attributes on ObjC @interface and
30161 @protocol. */
30163 static bool
30164 cp_parser_objc_valid_prefix_attributes (cp_parser* parser, tree *attrib)
30166 cp_lexer_save_tokens (parser->lexer);
30167 *attrib = cp_parser_attributes_opt (parser);
30168 gcc_assert (*attrib);
30169 if (OBJC_IS_AT_KEYWORD (cp_lexer_peek_token (parser->lexer)->keyword))
30171 cp_lexer_commit_tokens (parser->lexer);
30172 return true;
30174 cp_lexer_rollback_tokens (parser->lexer);
30175 return false;
30178 /* This routine is a minimal replacement for
30179 c_parser_struct_declaration () used when parsing the list of
30180 types/names or ObjC++ properties. For example, when parsing the
30181 code
30183 @property (readonly) int a, b, c;
30185 this function is responsible for parsing "int a, int b, int c" and
30186 returning the declarations as CHAIN of DECLs.
30188 TODO: Share this code with cp_parser_objc_class_ivars. It's very
30189 similar parsing. */
30190 static tree
30191 cp_parser_objc_struct_declaration (cp_parser *parser)
30193 tree decls = NULL_TREE;
30194 cp_decl_specifier_seq declspecs;
30195 int decl_class_or_enum_p;
30196 tree prefix_attributes;
30198 cp_parser_decl_specifier_seq (parser,
30199 CP_PARSER_FLAGS_NONE,
30200 &declspecs,
30201 &decl_class_or_enum_p);
30203 if (declspecs.type == error_mark_node)
30204 return error_mark_node;
30206 /* auto, register, static, extern, mutable. */
30207 if (declspecs.storage_class != sc_none)
30209 cp_parser_error (parser, "invalid type for property");
30210 declspecs.storage_class = sc_none;
30213 /* thread_local. */
30214 if (decl_spec_seq_has_spec_p (&declspecs, ds_thread))
30216 cp_parser_error (parser, "invalid type for property");
30217 declspecs.locations[ds_thread] = 0;
30220 /* typedef. */
30221 if (decl_spec_seq_has_spec_p (&declspecs, ds_typedef))
30223 cp_parser_error (parser, "invalid type for property");
30224 declspecs.locations[ds_typedef] = 0;
30227 prefix_attributes = declspecs.attributes;
30228 declspecs.attributes = NULL_TREE;
30230 /* Keep going until we hit the `;' at the end of the declaration. */
30231 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
30233 tree attributes, first_attribute, decl;
30234 cp_declarator *declarator;
30235 cp_token *token;
30237 /* Parse the declarator. */
30238 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
30239 NULL, NULL, false, false);
30241 /* Look for attributes that apply to the ivar. */
30242 attributes = cp_parser_attributes_opt (parser);
30243 /* Remember which attributes are prefix attributes and
30244 which are not. */
30245 first_attribute = attributes;
30246 /* Combine the attributes. */
30247 attributes = chainon (prefix_attributes, attributes);
30249 decl = grokfield (declarator, &declspecs,
30250 NULL_TREE, /*init_const_expr_p=*/false,
30251 NULL_TREE, attributes);
30253 if (decl == error_mark_node || decl == NULL_TREE)
30254 return error_mark_node;
30256 /* Reset PREFIX_ATTRIBUTES. */
30257 while (attributes && TREE_CHAIN (attributes) != first_attribute)
30258 attributes = TREE_CHAIN (attributes);
30259 if (attributes)
30260 TREE_CHAIN (attributes) = NULL_TREE;
30262 DECL_CHAIN (decl) = decls;
30263 decls = decl;
30265 token = cp_lexer_peek_token (parser->lexer);
30266 if (token->type == CPP_COMMA)
30268 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
30269 continue;
30271 else
30272 break;
30274 return decls;
30277 /* Parse an Objective-C @property declaration. The syntax is:
30279 objc-property-declaration:
30280 '@property' objc-property-attributes[opt] struct-declaration ;
30282 objc-property-attributes:
30283 '(' objc-property-attribute-list ')'
30285 objc-property-attribute-list:
30286 objc-property-attribute
30287 objc-property-attribute-list, objc-property-attribute
30289 objc-property-attribute
30290 'getter' = identifier
30291 'setter' = identifier
30292 'readonly'
30293 'readwrite'
30294 'assign'
30295 'retain'
30296 'copy'
30297 'nonatomic'
30299 For example:
30300 @property NSString *name;
30301 @property (readonly) id object;
30302 @property (retain, nonatomic, getter=getTheName) id name;
30303 @property int a, b, c;
30305 PS: This function is identical to
30306 c_parser_objc_at_property_declaration for C. Keep them in sync. */
30307 static void
30308 cp_parser_objc_at_property_declaration (cp_parser *parser)
30310 /* The following variables hold the attributes of the properties as
30311 parsed. They are 'false' or 'NULL_TREE' if the attribute was not
30312 seen. When we see an attribute, we set them to 'true' (if they
30313 are boolean properties) or to the identifier (if they have an
30314 argument, ie, for getter and setter). Note that here we only
30315 parse the list of attributes, check the syntax and accumulate the
30316 attributes that we find. objc_add_property_declaration() will
30317 then process the information. */
30318 bool property_assign = false;
30319 bool property_copy = false;
30320 tree property_getter_ident = NULL_TREE;
30321 bool property_nonatomic = false;
30322 bool property_readonly = false;
30323 bool property_readwrite = false;
30324 bool property_retain = false;
30325 tree property_setter_ident = NULL_TREE;
30327 /* 'properties' is the list of properties that we read. Usually a
30328 single one, but maybe more (eg, in "@property int a, b, c;" there
30329 are three). */
30330 tree properties;
30331 location_t loc;
30333 loc = cp_lexer_peek_token (parser->lexer)->location;
30335 cp_lexer_consume_token (parser->lexer); /* Eat '@property'. */
30337 /* Parse the optional attribute list... */
30338 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
30340 /* Eat the '('. */
30341 cp_lexer_consume_token (parser->lexer);
30343 while (true)
30345 bool syntax_error = false;
30346 cp_token *token = cp_lexer_peek_token (parser->lexer);
30347 enum rid keyword;
30349 if (token->type != CPP_NAME)
30351 cp_parser_error (parser, "expected identifier");
30352 break;
30354 keyword = C_RID_CODE (token->u.value);
30355 cp_lexer_consume_token (parser->lexer);
30356 switch (keyword)
30358 case RID_ASSIGN: property_assign = true; break;
30359 case RID_COPY: property_copy = true; break;
30360 case RID_NONATOMIC: property_nonatomic = true; break;
30361 case RID_READONLY: property_readonly = true; break;
30362 case RID_READWRITE: property_readwrite = true; break;
30363 case RID_RETAIN: property_retain = true; break;
30365 case RID_GETTER:
30366 case RID_SETTER:
30367 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
30369 if (keyword == RID_GETTER)
30370 cp_parser_error (parser,
30371 "missing %<=%> (after %<getter%> attribute)");
30372 else
30373 cp_parser_error (parser,
30374 "missing %<=%> (after %<setter%> attribute)");
30375 syntax_error = true;
30376 break;
30378 cp_lexer_consume_token (parser->lexer); /* eat the = */
30379 if (!cp_parser_objc_selector_p (cp_lexer_peek_token (parser->lexer)->type))
30381 cp_parser_error (parser, "expected identifier");
30382 syntax_error = true;
30383 break;
30385 if (keyword == RID_SETTER)
30387 if (property_setter_ident != NULL_TREE)
30389 cp_parser_error (parser, "the %<setter%> attribute may only be specified once");
30390 cp_lexer_consume_token (parser->lexer);
30392 else
30393 property_setter_ident = cp_parser_objc_selector (parser);
30394 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
30395 cp_parser_error (parser, "setter name must terminate with %<:%>");
30396 else
30397 cp_lexer_consume_token (parser->lexer);
30399 else
30401 if (property_getter_ident != NULL_TREE)
30403 cp_parser_error (parser, "the %<getter%> attribute may only be specified once");
30404 cp_lexer_consume_token (parser->lexer);
30406 else
30407 property_getter_ident = cp_parser_objc_selector (parser);
30409 break;
30410 default:
30411 cp_parser_error (parser, "unknown property attribute");
30412 syntax_error = true;
30413 break;
30416 if (syntax_error)
30417 break;
30419 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
30420 cp_lexer_consume_token (parser->lexer);
30421 else
30422 break;
30425 /* FIXME: "@property (setter, assign);" will generate a spurious
30426 "error: expected ‘)’ before ‘,’ token". This is because
30427 cp_parser_require, unlike the C counterpart, will produce an
30428 error even if we are in error recovery. */
30429 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
30431 cp_parser_skip_to_closing_parenthesis (parser,
30432 /*recovering=*/true,
30433 /*or_comma=*/false,
30434 /*consume_paren=*/true);
30438 /* ... and the property declaration(s). */
30439 properties = cp_parser_objc_struct_declaration (parser);
30441 if (properties == error_mark_node)
30443 cp_parser_skip_to_end_of_statement (parser);
30444 /* If the next token is now a `;', consume it. */
30445 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
30446 cp_lexer_consume_token (parser->lexer);
30447 return;
30450 if (properties == NULL_TREE)
30451 cp_parser_error (parser, "expected identifier");
30452 else
30454 /* Comma-separated properties are chained together in
30455 reverse order; add them one by one. */
30456 properties = nreverse (properties);
30458 for (; properties; properties = TREE_CHAIN (properties))
30459 objc_add_property_declaration (loc, copy_node (properties),
30460 property_readonly, property_readwrite,
30461 property_assign, property_retain,
30462 property_copy, property_nonatomic,
30463 property_getter_ident, property_setter_ident);
30466 cp_parser_consume_semicolon_at_end_of_statement (parser);
30469 /* Parse an Objective-C++ @synthesize declaration. The syntax is:
30471 objc-synthesize-declaration:
30472 @synthesize objc-synthesize-identifier-list ;
30474 objc-synthesize-identifier-list:
30475 objc-synthesize-identifier
30476 objc-synthesize-identifier-list, objc-synthesize-identifier
30478 objc-synthesize-identifier
30479 identifier
30480 identifier = identifier
30482 For example:
30483 @synthesize MyProperty;
30484 @synthesize OneProperty, AnotherProperty=MyIvar, YetAnotherProperty;
30486 PS: This function is identical to c_parser_objc_at_synthesize_declaration
30487 for C. Keep them in sync.
30489 static void
30490 cp_parser_objc_at_synthesize_declaration (cp_parser *parser)
30492 tree list = NULL_TREE;
30493 location_t loc;
30494 loc = cp_lexer_peek_token (parser->lexer)->location;
30496 cp_lexer_consume_token (parser->lexer); /* Eat '@synthesize'. */
30497 while (true)
30499 tree property, ivar;
30500 property = cp_parser_identifier (parser);
30501 if (property == error_mark_node)
30503 cp_parser_consume_semicolon_at_end_of_statement (parser);
30504 return;
30506 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
30508 cp_lexer_consume_token (parser->lexer);
30509 ivar = cp_parser_identifier (parser);
30510 if (ivar == error_mark_node)
30512 cp_parser_consume_semicolon_at_end_of_statement (parser);
30513 return;
30516 else
30517 ivar = NULL_TREE;
30518 list = chainon (list, build_tree_list (ivar, property));
30519 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
30520 cp_lexer_consume_token (parser->lexer);
30521 else
30522 break;
30524 cp_parser_consume_semicolon_at_end_of_statement (parser);
30525 objc_add_synthesize_declaration (loc, list);
30528 /* Parse an Objective-C++ @dynamic declaration. The syntax is:
30530 objc-dynamic-declaration:
30531 @dynamic identifier-list ;
30533 For example:
30534 @dynamic MyProperty;
30535 @dynamic MyProperty, AnotherProperty;
30537 PS: This function is identical to c_parser_objc_at_dynamic_declaration
30538 for C. Keep them in sync.
30540 static void
30541 cp_parser_objc_at_dynamic_declaration (cp_parser *parser)
30543 tree list = NULL_TREE;
30544 location_t loc;
30545 loc = cp_lexer_peek_token (parser->lexer)->location;
30547 cp_lexer_consume_token (parser->lexer); /* Eat '@dynamic'. */
30548 while (true)
30550 tree property;
30551 property = cp_parser_identifier (parser);
30552 if (property == error_mark_node)
30554 cp_parser_consume_semicolon_at_end_of_statement (parser);
30555 return;
30557 list = chainon (list, build_tree_list (NULL, property));
30558 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
30559 cp_lexer_consume_token (parser->lexer);
30560 else
30561 break;
30563 cp_parser_consume_semicolon_at_end_of_statement (parser);
30564 objc_add_dynamic_declaration (loc, list);
30568 /* OpenMP 2.5 / 3.0 / 3.1 / 4.0 parsing routines. */
30570 /* Returns name of the next clause.
30571 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
30572 the token is not consumed. Otherwise appropriate pragma_omp_clause is
30573 returned and the token is consumed. */
30575 static pragma_omp_clause
30576 cp_parser_omp_clause_name (cp_parser *parser)
30578 pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
30580 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
30581 result = PRAGMA_OACC_CLAUSE_AUTO;
30582 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
30583 result = PRAGMA_OMP_CLAUSE_IF;
30584 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT))
30585 result = PRAGMA_OMP_CLAUSE_DEFAULT;
30586 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DELETE))
30587 result = PRAGMA_OACC_CLAUSE_DELETE;
30588 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_PRIVATE))
30589 result = PRAGMA_OMP_CLAUSE_PRIVATE;
30590 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
30591 result = PRAGMA_OMP_CLAUSE_FOR;
30592 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
30594 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
30595 const char *p = IDENTIFIER_POINTER (id);
30597 switch (p[0])
30599 case 'a':
30600 if (!strcmp ("aligned", p))
30601 result = PRAGMA_OMP_CLAUSE_ALIGNED;
30602 else if (!strcmp ("async", p))
30603 result = PRAGMA_OACC_CLAUSE_ASYNC;
30604 break;
30605 case 'c':
30606 if (!strcmp ("collapse", p))
30607 result = PRAGMA_OMP_CLAUSE_COLLAPSE;
30608 else if (!strcmp ("copy", p))
30609 result = PRAGMA_OACC_CLAUSE_COPY;
30610 else if (!strcmp ("copyin", p))
30611 result = PRAGMA_OMP_CLAUSE_COPYIN;
30612 else if (!strcmp ("copyout", p))
30613 result = PRAGMA_OACC_CLAUSE_COPYOUT;
30614 else if (!strcmp ("copyprivate", p))
30615 result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
30616 else if (!strcmp ("create", p))
30617 result = PRAGMA_OACC_CLAUSE_CREATE;
30618 break;
30619 case 'd':
30620 if (!strcmp ("defaultmap", p))
30621 result = PRAGMA_OMP_CLAUSE_DEFAULTMAP;
30622 else if (!strcmp ("depend", p))
30623 result = PRAGMA_OMP_CLAUSE_DEPEND;
30624 else if (!strcmp ("device", p))
30625 result = PRAGMA_OMP_CLAUSE_DEVICE;
30626 else if (!strcmp ("deviceptr", p))
30627 result = PRAGMA_OACC_CLAUSE_DEVICEPTR;
30628 else if (!strcmp ("device_resident", p))
30629 result = PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT;
30630 else if (!strcmp ("dist_schedule", p))
30631 result = PRAGMA_OMP_CLAUSE_DIST_SCHEDULE;
30632 break;
30633 case 'f':
30634 if (!strcmp ("final", p))
30635 result = PRAGMA_OMP_CLAUSE_FINAL;
30636 else if (!strcmp ("firstprivate", p))
30637 result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
30638 else if (!strcmp ("from", p))
30639 result = PRAGMA_OMP_CLAUSE_FROM;
30640 break;
30641 case 'g':
30642 if (!strcmp ("gang", p))
30643 result = PRAGMA_OACC_CLAUSE_GANG;
30644 else if (!strcmp ("grainsize", p))
30645 result = PRAGMA_OMP_CLAUSE_GRAINSIZE;
30646 break;
30647 case 'h':
30648 if (!strcmp ("hint", p))
30649 result = PRAGMA_OMP_CLAUSE_HINT;
30650 else if (!strcmp ("host", p))
30651 result = PRAGMA_OACC_CLAUSE_HOST;
30652 break;
30653 case 'i':
30654 if (!strcmp ("inbranch", p))
30655 result = PRAGMA_OMP_CLAUSE_INBRANCH;
30656 else if (!strcmp ("independent", p))
30657 result = PRAGMA_OACC_CLAUSE_INDEPENDENT;
30658 else if (!strcmp ("is_device_ptr", p))
30659 result = PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR;
30660 break;
30661 case 'l':
30662 if (!strcmp ("lastprivate", p))
30663 result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
30664 else if (!strcmp ("linear", p))
30665 result = PRAGMA_OMP_CLAUSE_LINEAR;
30666 else if (!strcmp ("link", p))
30667 result = PRAGMA_OMP_CLAUSE_LINK;
30668 break;
30669 case 'm':
30670 if (!strcmp ("map", p))
30671 result = PRAGMA_OMP_CLAUSE_MAP;
30672 else if (!strcmp ("mergeable", p))
30673 result = PRAGMA_OMP_CLAUSE_MERGEABLE;
30674 else if (flag_cilkplus && !strcmp ("mask", p))
30675 result = PRAGMA_CILK_CLAUSE_MASK;
30676 break;
30677 case 'n':
30678 if (!strcmp ("nogroup", p))
30679 result = PRAGMA_OMP_CLAUSE_NOGROUP;
30680 else if (!strcmp ("notinbranch", p))
30681 result = PRAGMA_OMP_CLAUSE_NOTINBRANCH;
30682 else if (!strcmp ("nowait", p))
30683 result = PRAGMA_OMP_CLAUSE_NOWAIT;
30684 else if (flag_cilkplus && !strcmp ("nomask", p))
30685 result = PRAGMA_CILK_CLAUSE_NOMASK;
30686 else if (!strcmp ("num_gangs", p))
30687 result = PRAGMA_OACC_CLAUSE_NUM_GANGS;
30688 else if (!strcmp ("num_tasks", p))
30689 result = PRAGMA_OMP_CLAUSE_NUM_TASKS;
30690 else if (!strcmp ("num_teams", p))
30691 result = PRAGMA_OMP_CLAUSE_NUM_TEAMS;
30692 else if (!strcmp ("num_threads", p))
30693 result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
30694 else if (!strcmp ("num_workers", p))
30695 result = PRAGMA_OACC_CLAUSE_NUM_WORKERS;
30696 break;
30697 case 'o':
30698 if (!strcmp ("ordered", p))
30699 result = PRAGMA_OMP_CLAUSE_ORDERED;
30700 break;
30701 case 'p':
30702 if (!strcmp ("parallel", p))
30703 result = PRAGMA_OMP_CLAUSE_PARALLEL;
30704 else if (!strcmp ("present", p))
30705 result = PRAGMA_OACC_CLAUSE_PRESENT;
30706 else if (!strcmp ("present_or_copy", p)
30707 || !strcmp ("pcopy", p))
30708 result = PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY;
30709 else if (!strcmp ("present_or_copyin", p)
30710 || !strcmp ("pcopyin", p))
30711 result = PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN;
30712 else if (!strcmp ("present_or_copyout", p)
30713 || !strcmp ("pcopyout", p))
30714 result = PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT;
30715 else if (!strcmp ("present_or_create", p)
30716 || !strcmp ("pcreate", p))
30717 result = PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE;
30718 else if (!strcmp ("priority", p))
30719 result = PRAGMA_OMP_CLAUSE_PRIORITY;
30720 else if (!strcmp ("proc_bind", p))
30721 result = PRAGMA_OMP_CLAUSE_PROC_BIND;
30722 break;
30723 case 'r':
30724 if (!strcmp ("reduction", p))
30725 result = PRAGMA_OMP_CLAUSE_REDUCTION;
30726 break;
30727 case 's':
30728 if (!strcmp ("safelen", p))
30729 result = PRAGMA_OMP_CLAUSE_SAFELEN;
30730 else if (!strcmp ("schedule", p))
30731 result = PRAGMA_OMP_CLAUSE_SCHEDULE;
30732 else if (!strcmp ("sections", p))
30733 result = PRAGMA_OMP_CLAUSE_SECTIONS;
30734 else if (!strcmp ("self", p))
30735 result = PRAGMA_OACC_CLAUSE_SELF;
30736 else if (!strcmp ("seq", p))
30737 result = PRAGMA_OACC_CLAUSE_SEQ;
30738 else if (!strcmp ("shared", p))
30739 result = PRAGMA_OMP_CLAUSE_SHARED;
30740 else if (!strcmp ("simd", p))
30741 result = PRAGMA_OMP_CLAUSE_SIMD;
30742 else if (!strcmp ("simdlen", p))
30743 result = PRAGMA_OMP_CLAUSE_SIMDLEN;
30744 break;
30745 case 't':
30746 if (!strcmp ("taskgroup", p))
30747 result = PRAGMA_OMP_CLAUSE_TASKGROUP;
30748 else if (!strcmp ("thread_limit", p))
30749 result = PRAGMA_OMP_CLAUSE_THREAD_LIMIT;
30750 else if (!strcmp ("threads", p))
30751 result = PRAGMA_OMP_CLAUSE_THREADS;
30752 else if (!strcmp ("tile", p))
30753 result = PRAGMA_OACC_CLAUSE_TILE;
30754 else if (!strcmp ("to", p))
30755 result = PRAGMA_OMP_CLAUSE_TO;
30756 break;
30757 case 'u':
30758 if (!strcmp ("uniform", p))
30759 result = PRAGMA_OMP_CLAUSE_UNIFORM;
30760 else if (!strcmp ("untied", p))
30761 result = PRAGMA_OMP_CLAUSE_UNTIED;
30762 else if (!strcmp ("use_device", p))
30763 result = PRAGMA_OACC_CLAUSE_USE_DEVICE;
30764 else if (!strcmp ("use_device_ptr", p))
30765 result = PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR;
30766 break;
30767 case 'v':
30768 if (!strcmp ("vector", p))
30769 result = PRAGMA_OACC_CLAUSE_VECTOR;
30770 else if (!strcmp ("vector_length", p))
30771 result = PRAGMA_OACC_CLAUSE_VECTOR_LENGTH;
30772 else if (flag_cilkplus && !strcmp ("vectorlength", p))
30773 result = PRAGMA_CILK_CLAUSE_VECTORLENGTH;
30774 break;
30775 case 'w':
30776 if (!strcmp ("wait", p))
30777 result = PRAGMA_OACC_CLAUSE_WAIT;
30778 else if (!strcmp ("worker", p))
30779 result = PRAGMA_OACC_CLAUSE_WORKER;
30780 break;
30784 if (result != PRAGMA_OMP_CLAUSE_NONE)
30785 cp_lexer_consume_token (parser->lexer);
30787 return result;
30790 /* Validate that a clause of the given type does not already exist. */
30792 static void
30793 check_no_duplicate_clause (tree clauses, enum omp_clause_code code,
30794 const char *name, location_t location)
30796 tree c;
30798 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
30799 if (OMP_CLAUSE_CODE (c) == code)
30801 error_at (location, "too many %qs clauses", name);
30802 break;
30806 /* OpenMP 2.5:
30807 variable-list:
30808 identifier
30809 variable-list , identifier
30811 In addition, we match a closing parenthesis (or, if COLON is non-NULL,
30812 colon). An opening parenthesis will have been consumed by the caller.
30814 If KIND is nonzero, create the appropriate node and install the decl
30815 in OMP_CLAUSE_DECL and add the node to the head of the list.
30817 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
30818 return the list created.
30820 COLON can be NULL if only closing parenthesis should end the list,
30821 or pointer to bool which will receive false if the list is terminated
30822 by closing parenthesis or true if the list is terminated by colon. */
30824 static tree
30825 cp_parser_omp_var_list_no_open (cp_parser *parser, enum omp_clause_code kind,
30826 tree list, bool *colon)
30828 cp_token *token;
30829 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
30830 if (colon)
30832 parser->colon_corrects_to_scope_p = false;
30833 *colon = false;
30835 while (1)
30837 tree name, decl;
30839 token = cp_lexer_peek_token (parser->lexer);
30840 if (kind != 0
30841 && current_class_ptr
30842 && cp_parser_is_keyword (token, RID_THIS))
30844 decl = finish_this_expr ();
30845 if (TREE_CODE (decl) == NON_LVALUE_EXPR
30846 || CONVERT_EXPR_P (decl))
30847 decl = TREE_OPERAND (decl, 0);
30848 cp_lexer_consume_token (parser->lexer);
30850 else
30852 name = cp_parser_id_expression (parser, /*template_p=*/false,
30853 /*check_dependency_p=*/true,
30854 /*template_p=*/NULL,
30855 /*declarator_p=*/false,
30856 /*optional_p=*/false);
30857 if (name == error_mark_node)
30858 goto skip_comma;
30860 decl = cp_parser_lookup_name_simple (parser, name, token->location);
30861 if (decl == error_mark_node)
30862 cp_parser_name_lookup_error (parser, name, decl, NLE_NULL,
30863 token->location);
30865 if (decl == error_mark_node)
30867 else if (kind != 0)
30869 switch (kind)
30871 case OMP_CLAUSE__CACHE_:
30872 /* The OpenACC cache directive explicitly only allows "array
30873 elements or subarrays". */
30874 if (cp_lexer_peek_token (parser->lexer)->type != CPP_OPEN_SQUARE)
30876 error_at (token->location, "expected %<[%>");
30877 decl = error_mark_node;
30878 break;
30880 /* FALLTHROUGH. */
30881 case OMP_CLAUSE_MAP:
30882 case OMP_CLAUSE_FROM:
30883 case OMP_CLAUSE_TO:
30884 while (cp_lexer_next_token_is (parser->lexer, CPP_DOT))
30886 location_t loc
30887 = cp_lexer_peek_token (parser->lexer)->location;
30888 cp_id_kind idk = CP_ID_KIND_NONE;
30889 cp_lexer_consume_token (parser->lexer);
30890 decl = convert_from_reference (decl);
30891 decl
30892 = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT,
30893 decl, false,
30894 &idk, loc);
30896 /* FALLTHROUGH. */
30897 case OMP_CLAUSE_DEPEND:
30898 case OMP_CLAUSE_REDUCTION:
30899 while (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
30901 tree low_bound = NULL_TREE, length = NULL_TREE;
30903 parser->colon_corrects_to_scope_p = false;
30904 cp_lexer_consume_token (parser->lexer);
30905 if (!cp_lexer_next_token_is (parser->lexer, CPP_COLON))
30906 low_bound = cp_parser_expression (parser);
30907 if (!colon)
30908 parser->colon_corrects_to_scope_p
30909 = saved_colon_corrects_to_scope_p;
30910 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_SQUARE))
30911 length = integer_one_node;
30912 else
30914 /* Look for `:'. */
30915 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
30916 goto skip_comma;
30917 if (!cp_lexer_next_token_is (parser->lexer,
30918 CPP_CLOSE_SQUARE))
30919 length = cp_parser_expression (parser);
30921 /* Look for the closing `]'. */
30922 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE,
30923 RT_CLOSE_SQUARE))
30924 goto skip_comma;
30926 decl = tree_cons (low_bound, length, decl);
30928 break;
30929 default:
30930 break;
30933 tree u = build_omp_clause (token->location, kind);
30934 OMP_CLAUSE_DECL (u) = decl;
30935 OMP_CLAUSE_CHAIN (u) = list;
30936 list = u;
30938 else
30939 list = tree_cons (decl, NULL_TREE, list);
30941 get_comma:
30942 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
30943 break;
30944 cp_lexer_consume_token (parser->lexer);
30947 if (colon)
30948 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
30950 if (colon != NULL && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
30952 *colon = true;
30953 cp_parser_require (parser, CPP_COLON, RT_COLON);
30954 return list;
30957 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
30959 int ending;
30961 /* Try to resync to an unnested comma. Copied from
30962 cp_parser_parenthesized_expression_list. */
30963 skip_comma:
30964 if (colon)
30965 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
30966 ending = cp_parser_skip_to_closing_parenthesis (parser,
30967 /*recovering=*/true,
30968 /*or_comma=*/true,
30969 /*consume_paren=*/true);
30970 if (ending < 0)
30971 goto get_comma;
30974 return list;
30977 /* Similarly, but expect leading and trailing parenthesis. This is a very
30978 common case for omp clauses. */
30980 static tree
30981 cp_parser_omp_var_list (cp_parser *parser, enum omp_clause_code kind, tree list)
30983 if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
30984 return cp_parser_omp_var_list_no_open (parser, kind, list, NULL);
30985 return list;
30988 /* OpenACC 2.0:
30989 copy ( variable-list )
30990 copyin ( variable-list )
30991 copyout ( variable-list )
30992 create ( variable-list )
30993 delete ( variable-list )
30994 present ( variable-list )
30995 present_or_copy ( variable-list )
30996 pcopy ( variable-list )
30997 present_or_copyin ( variable-list )
30998 pcopyin ( variable-list )
30999 present_or_copyout ( variable-list )
31000 pcopyout ( variable-list )
31001 present_or_create ( variable-list )
31002 pcreate ( variable-list ) */
31004 static tree
31005 cp_parser_oacc_data_clause (cp_parser *parser, pragma_omp_clause c_kind,
31006 tree list)
31008 enum gomp_map_kind kind;
31009 switch (c_kind)
31011 case PRAGMA_OACC_CLAUSE_COPY:
31012 kind = GOMP_MAP_FORCE_TOFROM;
31013 break;
31014 case PRAGMA_OACC_CLAUSE_COPYIN:
31015 kind = GOMP_MAP_FORCE_TO;
31016 break;
31017 case PRAGMA_OACC_CLAUSE_COPYOUT:
31018 kind = GOMP_MAP_FORCE_FROM;
31019 break;
31020 case PRAGMA_OACC_CLAUSE_CREATE:
31021 kind = GOMP_MAP_FORCE_ALLOC;
31022 break;
31023 case PRAGMA_OACC_CLAUSE_DELETE:
31024 kind = GOMP_MAP_DELETE;
31025 break;
31026 case PRAGMA_OACC_CLAUSE_DEVICE:
31027 kind = GOMP_MAP_FORCE_TO;
31028 break;
31029 case PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT:
31030 kind = GOMP_MAP_DEVICE_RESIDENT;
31031 break;
31032 case PRAGMA_OACC_CLAUSE_HOST:
31033 case PRAGMA_OACC_CLAUSE_SELF:
31034 kind = GOMP_MAP_FORCE_FROM;
31035 break;
31036 case PRAGMA_OACC_CLAUSE_LINK:
31037 kind = GOMP_MAP_LINK;
31038 break;
31039 case PRAGMA_OACC_CLAUSE_PRESENT:
31040 kind = GOMP_MAP_FORCE_PRESENT;
31041 break;
31042 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY:
31043 kind = GOMP_MAP_TOFROM;
31044 break;
31045 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN:
31046 kind = GOMP_MAP_TO;
31047 break;
31048 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT:
31049 kind = GOMP_MAP_FROM;
31050 break;
31051 case PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE:
31052 kind = GOMP_MAP_ALLOC;
31053 break;
31054 default:
31055 gcc_unreachable ();
31057 tree nl, c;
31058 nl = cp_parser_omp_var_list (parser, OMP_CLAUSE_MAP, list);
31060 for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
31061 OMP_CLAUSE_SET_MAP_KIND (c, kind);
31063 return nl;
31066 /* OpenACC 2.0:
31067 deviceptr ( variable-list ) */
31069 static tree
31070 cp_parser_oacc_data_clause_deviceptr (cp_parser *parser, tree list)
31072 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
31073 tree vars, t;
31075 /* Can't use OMP_CLAUSE_MAP here (that is, can't use the generic
31076 cp_parser_oacc_data_clause), as for PRAGMA_OACC_CLAUSE_DEVICEPTR,
31077 variable-list must only allow for pointer variables. */
31078 vars = cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
31079 for (t = vars; t; t = TREE_CHAIN (t))
31081 tree v = TREE_PURPOSE (t);
31082 tree u = build_omp_clause (loc, OMP_CLAUSE_MAP);
31083 OMP_CLAUSE_SET_MAP_KIND (u, GOMP_MAP_FORCE_DEVICEPTR);
31084 OMP_CLAUSE_DECL (u) = v;
31085 OMP_CLAUSE_CHAIN (u) = list;
31086 list = u;
31089 return list;
31092 /* OpenACC 2.0:
31093 auto
31094 independent
31095 nohost
31096 seq */
31098 static tree
31099 cp_parser_oacc_simple_clause (cp_parser * /* parser */,
31100 enum omp_clause_code code,
31101 tree list, location_t location)
31103 check_no_duplicate_clause (list, code, omp_clause_code_name[code], location);
31104 tree c = build_omp_clause (location, code);
31105 OMP_CLAUSE_CHAIN (c) = list;
31106 return c;
31109 /* OpenACC:
31110 num_gangs ( expression )
31111 num_workers ( expression )
31112 vector_length ( expression ) */
31114 static tree
31115 cp_parser_oacc_single_int_clause (cp_parser *parser, omp_clause_code code,
31116 const char *str, tree list)
31118 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
31120 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31121 return list;
31123 tree t = cp_parser_assignment_expression (parser, NULL, false, false);
31125 if (t == error_mark_node
31126 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31128 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31129 /*or_comma=*/false,
31130 /*consume_paren=*/true);
31131 return list;
31134 check_no_duplicate_clause (list, code, str, loc);
31136 tree c = build_omp_clause (loc, code);
31137 OMP_CLAUSE_OPERAND (c, 0) = t;
31138 OMP_CLAUSE_CHAIN (c) = list;
31139 return c;
31142 /* OpenACC:
31144 gang [( gang-arg-list )]
31145 worker [( [num:] int-expr )]
31146 vector [( [length:] int-expr )]
31148 where gang-arg is one of:
31150 [num:] int-expr
31151 static: size-expr
31153 and size-expr may be:
31156 int-expr
31159 static tree
31160 cp_parser_oacc_shape_clause (cp_parser *parser, omp_clause_code kind,
31161 const char *str, tree list)
31163 const char *id = "num";
31164 cp_lexer *lexer = parser->lexer;
31165 tree ops[2] = { NULL_TREE, NULL_TREE }, c;
31166 location_t loc = cp_lexer_peek_token (lexer)->location;
31168 if (kind == OMP_CLAUSE_VECTOR)
31169 id = "length";
31171 if (cp_lexer_next_token_is (lexer, CPP_OPEN_PAREN))
31173 cp_lexer_consume_token (lexer);
31177 cp_token *next = cp_lexer_peek_token (lexer);
31178 int idx = 0;
31180 /* Gang static argument. */
31181 if (kind == OMP_CLAUSE_GANG
31182 && cp_lexer_next_token_is_keyword (lexer, RID_STATIC))
31184 cp_lexer_consume_token (lexer);
31186 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
31187 goto cleanup_error;
31189 idx = 1;
31190 if (ops[idx] != NULL)
31192 cp_parser_error (parser, "too many %<static%> arguments");
31193 goto cleanup_error;
31196 /* Check for the '*' argument. */
31197 if (cp_lexer_next_token_is (lexer, CPP_MULT)
31198 && (cp_lexer_nth_token_is (parser->lexer, 2, CPP_COMMA)
31199 || cp_lexer_nth_token_is (parser->lexer, 2,
31200 CPP_CLOSE_PAREN)))
31202 cp_lexer_consume_token (lexer);
31203 ops[idx] = integer_minus_one_node;
31205 if (cp_lexer_next_token_is (lexer, CPP_COMMA))
31207 cp_lexer_consume_token (lexer);
31208 continue;
31210 else break;
31213 /* Worker num: argument and vector length: arguments. */
31214 else if (cp_lexer_next_token_is (lexer, CPP_NAME)
31215 && strcmp (id, IDENTIFIER_POINTER (next->u.value)) == 0
31216 && cp_lexer_nth_token_is (lexer, 2, CPP_COLON))
31218 cp_lexer_consume_token (lexer); /* id */
31219 cp_lexer_consume_token (lexer); /* ':' */
31222 /* Now collect the actual argument. */
31223 if (ops[idx] != NULL_TREE)
31225 cp_parser_error (parser, "unexpected argument");
31226 goto cleanup_error;
31229 tree expr = cp_parser_assignment_expression (parser, NULL, false,
31230 false);
31231 if (expr == error_mark_node)
31232 goto cleanup_error;
31234 mark_exp_read (expr);
31235 ops[idx] = expr;
31237 if (kind == OMP_CLAUSE_GANG
31238 && cp_lexer_next_token_is (lexer, CPP_COMMA))
31240 cp_lexer_consume_token (lexer);
31241 continue;
31243 break;
31245 while (1);
31247 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31248 goto cleanup_error;
31251 check_no_duplicate_clause (list, kind, str, loc);
31253 c = build_omp_clause (loc, kind);
31255 if (ops[1])
31256 OMP_CLAUSE_OPERAND (c, 1) = ops[1];
31258 OMP_CLAUSE_OPERAND (c, 0) = ops[0];
31259 OMP_CLAUSE_CHAIN (c) = list;
31261 return c;
31263 cleanup_error:
31264 cp_parser_skip_to_closing_parenthesis (parser, false, false, true);
31265 return list;
31268 /* OpenACC 2.0:
31269 tile ( size-expr-list ) */
31271 static tree
31272 cp_parser_oacc_clause_tile (cp_parser *parser, location_t clause_loc, tree list)
31274 tree c, expr = error_mark_node;
31275 tree tile = NULL_TREE;
31277 /* Collapse and tile are mutually exclusive. (The spec doesn't say
31278 so, but the spec authors never considered such a case and have
31279 differing opinions on what it might mean, including 'not
31280 allowed'.) */
31281 check_no_duplicate_clause (list, OMP_CLAUSE_TILE, "tile", clause_loc);
31282 check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse",
31283 clause_loc);
31285 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31286 return list;
31290 if (tile && !cp_parser_require (parser, CPP_COMMA, RT_COMMA))
31291 return list;
31293 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT)
31294 && (cp_lexer_nth_token_is (parser->lexer, 2, CPP_COMMA)
31295 || cp_lexer_nth_token_is (parser->lexer, 2, CPP_CLOSE_PAREN)))
31297 cp_lexer_consume_token (parser->lexer);
31298 expr = integer_zero_node;
31300 else
31301 expr = cp_parser_constant_expression (parser);
31303 tile = tree_cons (NULL_TREE, expr, tile);
31305 while (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN));
31307 /* Consume the trailing ')'. */
31308 cp_lexer_consume_token (parser->lexer);
31310 c = build_omp_clause (clause_loc, OMP_CLAUSE_TILE);
31311 tile = nreverse (tile);
31312 OMP_CLAUSE_TILE_LIST (c) = tile;
31313 OMP_CLAUSE_CHAIN (c) = list;
31314 return c;
31317 /* OpenACC 2.0
31318 Parse wait clause or directive parameters. */
31320 static tree
31321 cp_parser_oacc_wait_list (cp_parser *parser, location_t clause_loc, tree list)
31323 vec<tree, va_gc> *args;
31324 tree t, args_tree;
31326 args = cp_parser_parenthesized_expression_list (parser, non_attr,
31327 /*cast_p=*/false,
31328 /*allow_expansion_p=*/true,
31329 /*non_constant_p=*/NULL);
31331 if (args == NULL || args->length () == 0)
31333 cp_parser_error (parser, "expected integer expression before ')'");
31334 if (args != NULL)
31335 release_tree_vector (args);
31336 return list;
31339 args_tree = build_tree_list_vec (args);
31341 release_tree_vector (args);
31343 for (t = args_tree; t; t = TREE_CHAIN (t))
31345 tree targ = TREE_VALUE (t);
31347 if (targ != error_mark_node)
31349 if (!INTEGRAL_TYPE_P (TREE_TYPE (targ)))
31350 error ("%<wait%> expression must be integral");
31351 else
31353 tree c = build_omp_clause (clause_loc, OMP_CLAUSE_WAIT);
31355 mark_rvalue_use (targ);
31356 OMP_CLAUSE_DECL (c) = targ;
31357 OMP_CLAUSE_CHAIN (c) = list;
31358 list = c;
31363 return list;
31366 /* OpenACC:
31367 wait ( int-expr-list ) */
31369 static tree
31370 cp_parser_oacc_clause_wait (cp_parser *parser, tree list)
31372 location_t location = cp_lexer_peek_token (parser->lexer)->location;
31374 if (cp_lexer_peek_token (parser->lexer)->type != CPP_OPEN_PAREN)
31375 return list;
31377 list = cp_parser_oacc_wait_list (parser, location, list);
31379 return list;
31382 /* OpenMP 3.0:
31383 collapse ( constant-expression ) */
31385 static tree
31386 cp_parser_omp_clause_collapse (cp_parser *parser, tree list, location_t location)
31388 tree c, num;
31389 location_t loc;
31390 HOST_WIDE_INT n;
31392 loc = cp_lexer_peek_token (parser->lexer)->location;
31393 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31394 return list;
31396 num = cp_parser_constant_expression (parser);
31398 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31399 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31400 /*or_comma=*/false,
31401 /*consume_paren=*/true);
31403 if (num == error_mark_node)
31404 return list;
31405 num = fold_non_dependent_expr (num);
31406 if (!tree_fits_shwi_p (num)
31407 || !INTEGRAL_TYPE_P (TREE_TYPE (num))
31408 || (n = tree_to_shwi (num)) <= 0
31409 || (int) n != n)
31411 error_at (loc, "collapse argument needs positive constant integer expression");
31412 return list;
31415 check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse", location);
31416 check_no_duplicate_clause (list, OMP_CLAUSE_TILE, "tile", location);
31417 c = build_omp_clause (loc, OMP_CLAUSE_COLLAPSE);
31418 OMP_CLAUSE_CHAIN (c) = list;
31419 OMP_CLAUSE_COLLAPSE_EXPR (c) = num;
31421 return c;
31424 /* OpenMP 2.5:
31425 default ( shared | none )
31427 OpenACC 2.0
31428 default (none) */
31430 static tree
31431 cp_parser_omp_clause_default (cp_parser *parser, tree list,
31432 location_t location, bool is_oacc)
31434 enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
31435 tree c;
31437 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31438 return list;
31439 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
31441 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
31442 const char *p = IDENTIFIER_POINTER (id);
31444 switch (p[0])
31446 case 'n':
31447 if (strcmp ("none", p) != 0)
31448 goto invalid_kind;
31449 kind = OMP_CLAUSE_DEFAULT_NONE;
31450 break;
31452 case 's':
31453 if (strcmp ("shared", p) != 0 || is_oacc)
31454 goto invalid_kind;
31455 kind = OMP_CLAUSE_DEFAULT_SHARED;
31456 break;
31458 default:
31459 goto invalid_kind;
31462 cp_lexer_consume_token (parser->lexer);
31464 else
31466 invalid_kind:
31467 if (is_oacc)
31468 cp_parser_error (parser, "expected %<none%>");
31469 else
31470 cp_parser_error (parser, "expected %<none%> or %<shared%>");
31473 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31474 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31475 /*or_comma=*/false,
31476 /*consume_paren=*/true);
31478 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
31479 return list;
31481 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default", location);
31482 c = build_omp_clause (location, OMP_CLAUSE_DEFAULT);
31483 OMP_CLAUSE_CHAIN (c) = list;
31484 OMP_CLAUSE_DEFAULT_KIND (c) = kind;
31486 return c;
31489 /* OpenMP 3.1:
31490 final ( expression ) */
31492 static tree
31493 cp_parser_omp_clause_final (cp_parser *parser, tree list, location_t location)
31495 tree t, c;
31497 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31498 return list;
31500 t = cp_parser_condition (parser);
31502 if (t == error_mark_node
31503 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31504 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31505 /*or_comma=*/false,
31506 /*consume_paren=*/true);
31508 check_no_duplicate_clause (list, OMP_CLAUSE_FINAL, "final", location);
31510 c = build_omp_clause (location, OMP_CLAUSE_FINAL);
31511 OMP_CLAUSE_FINAL_EXPR (c) = t;
31512 OMP_CLAUSE_CHAIN (c) = list;
31514 return c;
31517 /* OpenMP 2.5:
31518 if ( expression )
31520 OpenMP 4.5:
31521 if ( directive-name-modifier : expression )
31523 directive-name-modifier:
31524 parallel | task | taskloop | target data | target | target update
31525 | target enter data | target exit data */
31527 static tree
31528 cp_parser_omp_clause_if (cp_parser *parser, tree list, location_t location,
31529 bool is_omp)
31531 tree t, c;
31532 enum tree_code if_modifier = ERROR_MARK;
31534 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31535 return list;
31537 if (is_omp && cp_lexer_next_token_is (parser->lexer, CPP_NAME))
31539 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
31540 const char *p = IDENTIFIER_POINTER (id);
31541 int n = 2;
31543 if (strcmp ("parallel", p) == 0)
31544 if_modifier = OMP_PARALLEL;
31545 else if (strcmp ("task", p) == 0)
31546 if_modifier = OMP_TASK;
31547 else if (strcmp ("taskloop", p) == 0)
31548 if_modifier = OMP_TASKLOOP;
31549 else if (strcmp ("target", p) == 0)
31551 if_modifier = OMP_TARGET;
31552 if (cp_lexer_nth_token_is (parser->lexer, 2, CPP_NAME))
31554 id = cp_lexer_peek_nth_token (parser->lexer, 2)->u.value;
31555 p = IDENTIFIER_POINTER (id);
31556 if (strcmp ("data", p) == 0)
31557 if_modifier = OMP_TARGET_DATA;
31558 else if (strcmp ("update", p) == 0)
31559 if_modifier = OMP_TARGET_UPDATE;
31560 else if (strcmp ("enter", p) == 0)
31561 if_modifier = OMP_TARGET_ENTER_DATA;
31562 else if (strcmp ("exit", p) == 0)
31563 if_modifier = OMP_TARGET_EXIT_DATA;
31564 if (if_modifier != OMP_TARGET)
31565 n = 3;
31566 else
31568 location_t loc
31569 = cp_lexer_peek_nth_token (parser->lexer, 2)->location;
31570 error_at (loc, "expected %<data%>, %<update%>, %<enter%> "
31571 "or %<exit%>");
31572 if_modifier = ERROR_MARK;
31574 if (if_modifier == OMP_TARGET_ENTER_DATA
31575 || if_modifier == OMP_TARGET_EXIT_DATA)
31577 if (cp_lexer_nth_token_is (parser->lexer, 3, CPP_NAME))
31579 id = cp_lexer_peek_nth_token (parser->lexer, 3)->u.value;
31580 p = IDENTIFIER_POINTER (id);
31581 if (strcmp ("data", p) == 0)
31582 n = 4;
31584 if (n != 4)
31586 location_t loc
31587 = cp_lexer_peek_nth_token (parser->lexer, 3)->location;
31588 error_at (loc, "expected %<data%>");
31589 if_modifier = ERROR_MARK;
31594 if (if_modifier != ERROR_MARK)
31596 if (cp_lexer_nth_token_is (parser->lexer, n, CPP_COLON))
31598 while (n-- > 0)
31599 cp_lexer_consume_token (parser->lexer);
31601 else
31603 if (n > 2)
31605 location_t loc
31606 = cp_lexer_peek_nth_token (parser->lexer, n)->location;
31607 error_at (loc, "expected %<:%>");
31609 if_modifier = ERROR_MARK;
31614 t = cp_parser_condition (parser);
31616 if (t == error_mark_node
31617 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31618 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31619 /*or_comma=*/false,
31620 /*consume_paren=*/true);
31622 for (c = list; c ; c = OMP_CLAUSE_CHAIN (c))
31623 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IF)
31625 if (if_modifier != ERROR_MARK
31626 && OMP_CLAUSE_IF_MODIFIER (c) == if_modifier)
31628 const char *p = NULL;
31629 switch (if_modifier)
31631 case OMP_PARALLEL: p = "parallel"; break;
31632 case OMP_TASK: p = "task"; break;
31633 case OMP_TASKLOOP: p = "taskloop"; break;
31634 case OMP_TARGET_DATA: p = "target data"; break;
31635 case OMP_TARGET: p = "target"; break;
31636 case OMP_TARGET_UPDATE: p = "target update"; break;
31637 case OMP_TARGET_ENTER_DATA: p = "enter data"; break;
31638 case OMP_TARGET_EXIT_DATA: p = "exit data"; break;
31639 default: gcc_unreachable ();
31641 error_at (location, "too many %<if%> clauses with %qs modifier",
31643 return list;
31645 else if (OMP_CLAUSE_IF_MODIFIER (c) == if_modifier)
31647 if (!is_omp)
31648 error_at (location, "too many %<if%> clauses");
31649 else
31650 error_at (location, "too many %<if%> clauses without modifier");
31651 return list;
31653 else if (if_modifier == ERROR_MARK
31654 || OMP_CLAUSE_IF_MODIFIER (c) == ERROR_MARK)
31656 error_at (location, "if any %<if%> clause has modifier, then all "
31657 "%<if%> clauses have to use modifier");
31658 return list;
31662 c = build_omp_clause (location, OMP_CLAUSE_IF);
31663 OMP_CLAUSE_IF_MODIFIER (c) = if_modifier;
31664 OMP_CLAUSE_IF_EXPR (c) = t;
31665 OMP_CLAUSE_CHAIN (c) = list;
31667 return c;
31670 /* OpenMP 3.1:
31671 mergeable */
31673 static tree
31674 cp_parser_omp_clause_mergeable (cp_parser * /*parser*/,
31675 tree list, location_t location)
31677 tree c;
31679 check_no_duplicate_clause (list, OMP_CLAUSE_MERGEABLE, "mergeable",
31680 location);
31682 c = build_omp_clause (location, OMP_CLAUSE_MERGEABLE);
31683 OMP_CLAUSE_CHAIN (c) = list;
31684 return c;
31687 /* OpenMP 2.5:
31688 nowait */
31690 static tree
31691 cp_parser_omp_clause_nowait (cp_parser * /*parser*/,
31692 tree list, location_t location)
31694 tree c;
31696 check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait", location);
31698 c = build_omp_clause (location, OMP_CLAUSE_NOWAIT);
31699 OMP_CLAUSE_CHAIN (c) = list;
31700 return c;
31703 /* OpenMP 2.5:
31704 num_threads ( expression ) */
31706 static tree
31707 cp_parser_omp_clause_num_threads (cp_parser *parser, tree list,
31708 location_t location)
31710 tree t, c;
31712 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31713 return list;
31715 t = cp_parser_expression (parser);
31717 if (t == error_mark_node
31718 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31719 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31720 /*or_comma=*/false,
31721 /*consume_paren=*/true);
31723 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS,
31724 "num_threads", location);
31726 c = build_omp_clause (location, OMP_CLAUSE_NUM_THREADS);
31727 OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
31728 OMP_CLAUSE_CHAIN (c) = list;
31730 return c;
31733 /* OpenMP 4.5:
31734 num_tasks ( expression ) */
31736 static tree
31737 cp_parser_omp_clause_num_tasks (cp_parser *parser, tree list,
31738 location_t location)
31740 tree t, c;
31742 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31743 return list;
31745 t = cp_parser_expression (parser);
31747 if (t == error_mark_node
31748 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31749 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31750 /*or_comma=*/false,
31751 /*consume_paren=*/true);
31753 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_TASKS,
31754 "num_tasks", location);
31756 c = build_omp_clause (location, OMP_CLAUSE_NUM_TASKS);
31757 OMP_CLAUSE_NUM_TASKS_EXPR (c) = t;
31758 OMP_CLAUSE_CHAIN (c) = list;
31760 return c;
31763 /* OpenMP 4.5:
31764 grainsize ( expression ) */
31766 static tree
31767 cp_parser_omp_clause_grainsize (cp_parser *parser, tree list,
31768 location_t location)
31770 tree t, c;
31772 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31773 return list;
31775 t = cp_parser_expression (parser);
31777 if (t == error_mark_node
31778 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31779 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31780 /*or_comma=*/false,
31781 /*consume_paren=*/true);
31783 check_no_duplicate_clause (list, OMP_CLAUSE_GRAINSIZE,
31784 "grainsize", location);
31786 c = build_omp_clause (location, OMP_CLAUSE_GRAINSIZE);
31787 OMP_CLAUSE_GRAINSIZE_EXPR (c) = t;
31788 OMP_CLAUSE_CHAIN (c) = list;
31790 return c;
31793 /* OpenMP 4.5:
31794 priority ( expression ) */
31796 static tree
31797 cp_parser_omp_clause_priority (cp_parser *parser, tree list,
31798 location_t location)
31800 tree t, c;
31802 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31803 return list;
31805 t = cp_parser_expression (parser);
31807 if (t == error_mark_node
31808 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31809 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31810 /*or_comma=*/false,
31811 /*consume_paren=*/true);
31813 check_no_duplicate_clause (list, OMP_CLAUSE_PRIORITY,
31814 "priority", location);
31816 c = build_omp_clause (location, OMP_CLAUSE_PRIORITY);
31817 OMP_CLAUSE_PRIORITY_EXPR (c) = t;
31818 OMP_CLAUSE_CHAIN (c) = list;
31820 return c;
31823 /* OpenMP 4.5:
31824 hint ( expression ) */
31826 static tree
31827 cp_parser_omp_clause_hint (cp_parser *parser, tree list,
31828 location_t location)
31830 tree t, c;
31832 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31833 return list;
31835 t = cp_parser_expression (parser);
31837 if (t == error_mark_node
31838 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31839 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31840 /*or_comma=*/false,
31841 /*consume_paren=*/true);
31843 check_no_duplicate_clause (list, OMP_CLAUSE_HINT, "hint", location);
31845 c = build_omp_clause (location, OMP_CLAUSE_HINT);
31846 OMP_CLAUSE_HINT_EXPR (c) = t;
31847 OMP_CLAUSE_CHAIN (c) = list;
31849 return c;
31852 /* OpenMP 4.5:
31853 defaultmap ( tofrom : scalar ) */
31855 static tree
31856 cp_parser_omp_clause_defaultmap (cp_parser *parser, tree list,
31857 location_t location)
31859 tree c, id;
31860 const char *p;
31862 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31863 return list;
31865 if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME))
31867 cp_parser_error (parser, "expected %<tofrom%>");
31868 goto out_err;
31870 id = cp_lexer_peek_token (parser->lexer)->u.value;
31871 p = IDENTIFIER_POINTER (id);
31872 if (strcmp (p, "tofrom") != 0)
31874 cp_parser_error (parser, "expected %<tofrom%>");
31875 goto out_err;
31877 cp_lexer_consume_token (parser->lexer);
31878 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
31879 goto out_err;
31881 if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME))
31883 cp_parser_error (parser, "expected %<scalar%>");
31884 goto out_err;
31886 id = cp_lexer_peek_token (parser->lexer)->u.value;
31887 p = IDENTIFIER_POINTER (id);
31888 if (strcmp (p, "scalar") != 0)
31890 cp_parser_error (parser, "expected %<scalar%>");
31891 goto out_err;
31893 cp_lexer_consume_token (parser->lexer);
31894 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31895 goto out_err;
31897 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULTMAP, "defaultmap",
31898 location);
31900 c = build_omp_clause (location, OMP_CLAUSE_DEFAULTMAP);
31901 OMP_CLAUSE_CHAIN (c) = list;
31902 return c;
31904 out_err:
31905 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31906 /*or_comma=*/false,
31907 /*consume_paren=*/true);
31908 return list;
31911 /* OpenMP 2.5:
31912 ordered
31914 OpenMP 4.5:
31915 ordered ( constant-expression ) */
31917 static tree
31918 cp_parser_omp_clause_ordered (cp_parser *parser,
31919 tree list, location_t location)
31921 tree c, num = NULL_TREE;
31922 HOST_WIDE_INT n;
31924 check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED,
31925 "ordered", location);
31927 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
31929 cp_lexer_consume_token (parser->lexer);
31931 num = cp_parser_constant_expression (parser);
31933 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31934 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31935 /*or_comma=*/false,
31936 /*consume_paren=*/true);
31938 if (num == error_mark_node)
31939 return list;
31940 num = fold_non_dependent_expr (num);
31941 if (!tree_fits_shwi_p (num)
31942 || !INTEGRAL_TYPE_P (TREE_TYPE (num))
31943 || (n = tree_to_shwi (num)) <= 0
31944 || (int) n != n)
31946 error_at (location,
31947 "ordered argument needs positive constant integer "
31948 "expression");
31949 return list;
31953 c = build_omp_clause (location, OMP_CLAUSE_ORDERED);
31954 OMP_CLAUSE_ORDERED_EXPR (c) = num;
31955 OMP_CLAUSE_CHAIN (c) = list;
31956 return c;
31959 /* OpenMP 2.5:
31960 reduction ( reduction-operator : variable-list )
31962 reduction-operator:
31963 One of: + * - & ^ | && ||
31965 OpenMP 3.1:
31967 reduction-operator:
31968 One of: + * - & ^ | && || min max
31970 OpenMP 4.0:
31972 reduction-operator:
31973 One of: + * - & ^ | && ||
31974 id-expression */
31976 static tree
31977 cp_parser_omp_clause_reduction (cp_parser *parser, tree list)
31979 enum tree_code code = ERROR_MARK;
31980 tree nlist, c, id = NULL_TREE;
31982 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31983 return list;
31985 switch (cp_lexer_peek_token (parser->lexer)->type)
31987 case CPP_PLUS: code = PLUS_EXPR; break;
31988 case CPP_MULT: code = MULT_EXPR; break;
31989 case CPP_MINUS: code = MINUS_EXPR; break;
31990 case CPP_AND: code = BIT_AND_EXPR; break;
31991 case CPP_XOR: code = BIT_XOR_EXPR; break;
31992 case CPP_OR: code = BIT_IOR_EXPR; break;
31993 case CPP_AND_AND: code = TRUTH_ANDIF_EXPR; break;
31994 case CPP_OR_OR: code = TRUTH_ORIF_EXPR; break;
31995 default: break;
31998 if (code != ERROR_MARK)
31999 cp_lexer_consume_token (parser->lexer);
32000 else
32002 bool saved_colon_corrects_to_scope_p;
32003 saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
32004 parser->colon_corrects_to_scope_p = false;
32005 id = cp_parser_id_expression (parser, /*template_p=*/false,
32006 /*check_dependency_p=*/true,
32007 /*template_p=*/NULL,
32008 /*declarator_p=*/false,
32009 /*optional_p=*/false);
32010 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
32011 if (identifier_p (id))
32013 const char *p = IDENTIFIER_POINTER (id);
32015 if (strcmp (p, "min") == 0)
32016 code = MIN_EXPR;
32017 else if (strcmp (p, "max") == 0)
32018 code = MAX_EXPR;
32019 else if (id == cp_operator_id (PLUS_EXPR))
32020 code = PLUS_EXPR;
32021 else if (id == cp_operator_id (MULT_EXPR))
32022 code = MULT_EXPR;
32023 else if (id == cp_operator_id (MINUS_EXPR))
32024 code = MINUS_EXPR;
32025 else if (id == cp_operator_id (BIT_AND_EXPR))
32026 code = BIT_AND_EXPR;
32027 else if (id == cp_operator_id (BIT_IOR_EXPR))
32028 code = BIT_IOR_EXPR;
32029 else if (id == cp_operator_id (BIT_XOR_EXPR))
32030 code = BIT_XOR_EXPR;
32031 else if (id == cp_operator_id (TRUTH_ANDIF_EXPR))
32032 code = TRUTH_ANDIF_EXPR;
32033 else if (id == cp_operator_id (TRUTH_ORIF_EXPR))
32034 code = TRUTH_ORIF_EXPR;
32035 id = omp_reduction_id (code, id, NULL_TREE);
32036 tree scope = parser->scope;
32037 if (scope)
32038 id = build_qualified_name (NULL_TREE, scope, id, false);
32039 parser->scope = NULL_TREE;
32040 parser->qualifying_scope = NULL_TREE;
32041 parser->object_scope = NULL_TREE;
32043 else
32045 error ("invalid reduction-identifier");
32046 resync_fail:
32047 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32048 /*or_comma=*/false,
32049 /*consume_paren=*/true);
32050 return list;
32054 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
32055 goto resync_fail;
32057 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_REDUCTION, list,
32058 NULL);
32059 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
32061 OMP_CLAUSE_REDUCTION_CODE (c) = code;
32062 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = id;
32065 return nlist;
32068 /* OpenMP 2.5:
32069 schedule ( schedule-kind )
32070 schedule ( schedule-kind , expression )
32072 schedule-kind:
32073 static | dynamic | guided | runtime | auto
32075 OpenMP 4.5:
32076 schedule ( schedule-modifier : schedule-kind )
32077 schedule ( schedule-modifier [ , schedule-modifier ] : schedule-kind , expression )
32079 schedule-modifier:
32080 simd
32081 monotonic
32082 nonmonotonic */
32084 static tree
32085 cp_parser_omp_clause_schedule (cp_parser *parser, tree list, location_t location)
32087 tree c, t;
32088 int modifiers = 0, nmodifiers = 0;
32090 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
32091 return list;
32093 c = build_omp_clause (location, OMP_CLAUSE_SCHEDULE);
32095 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32097 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
32098 const char *p = IDENTIFIER_POINTER (id);
32099 if (strcmp ("simd", p) == 0)
32100 OMP_CLAUSE_SCHEDULE_SIMD (c) = 1;
32101 else if (strcmp ("monotonic", p) == 0)
32102 modifiers |= OMP_CLAUSE_SCHEDULE_MONOTONIC;
32103 else if (strcmp ("nonmonotonic", p) == 0)
32104 modifiers |= OMP_CLAUSE_SCHEDULE_NONMONOTONIC;
32105 else
32106 break;
32107 cp_lexer_consume_token (parser->lexer);
32108 if (nmodifiers++ == 0
32109 && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
32110 cp_lexer_consume_token (parser->lexer);
32111 else
32113 cp_parser_require (parser, CPP_COLON, RT_COLON);
32114 break;
32118 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32120 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
32121 const char *p = IDENTIFIER_POINTER (id);
32123 switch (p[0])
32125 case 'd':
32126 if (strcmp ("dynamic", p) != 0)
32127 goto invalid_kind;
32128 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
32129 break;
32131 case 'g':
32132 if (strcmp ("guided", p) != 0)
32133 goto invalid_kind;
32134 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
32135 break;
32137 case 'r':
32138 if (strcmp ("runtime", p) != 0)
32139 goto invalid_kind;
32140 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
32141 break;
32143 default:
32144 goto invalid_kind;
32147 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
32148 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
32149 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
32150 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_AUTO;
32151 else
32152 goto invalid_kind;
32153 cp_lexer_consume_token (parser->lexer);
32155 if ((modifiers & (OMP_CLAUSE_SCHEDULE_MONOTONIC
32156 | OMP_CLAUSE_SCHEDULE_NONMONOTONIC))
32157 == (OMP_CLAUSE_SCHEDULE_MONOTONIC
32158 | OMP_CLAUSE_SCHEDULE_NONMONOTONIC))
32160 error_at (location, "both %<monotonic%> and %<nonmonotonic%> modifiers "
32161 "specified");
32162 modifiers = 0;
32165 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
32167 cp_token *token;
32168 cp_lexer_consume_token (parser->lexer);
32170 token = cp_lexer_peek_token (parser->lexer);
32171 t = cp_parser_assignment_expression (parser);
32173 if (t == error_mark_node)
32174 goto resync_fail;
32175 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
32176 error_at (token->location, "schedule %<runtime%> does not take "
32177 "a %<chunk_size%> parameter");
32178 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_AUTO)
32179 error_at (token->location, "schedule %<auto%> does not take "
32180 "a %<chunk_size%> parameter");
32181 else
32182 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
32184 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
32185 goto resync_fail;
32187 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
32188 goto resync_fail;
32190 OMP_CLAUSE_SCHEDULE_KIND (c)
32191 = (enum omp_clause_schedule_kind)
32192 (OMP_CLAUSE_SCHEDULE_KIND (c) | modifiers);
32194 check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule", location);
32195 OMP_CLAUSE_CHAIN (c) = list;
32196 return c;
32198 invalid_kind:
32199 cp_parser_error (parser, "invalid schedule kind");
32200 resync_fail:
32201 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32202 /*or_comma=*/false,
32203 /*consume_paren=*/true);
32204 return list;
32207 /* OpenMP 3.0:
32208 untied */
32210 static tree
32211 cp_parser_omp_clause_untied (cp_parser * /*parser*/,
32212 tree list, location_t location)
32214 tree c;
32216 check_no_duplicate_clause (list, OMP_CLAUSE_UNTIED, "untied", location);
32218 c = build_omp_clause (location, OMP_CLAUSE_UNTIED);
32219 OMP_CLAUSE_CHAIN (c) = list;
32220 return c;
32223 /* OpenMP 4.0:
32224 inbranch
32225 notinbranch */
32227 static tree
32228 cp_parser_omp_clause_branch (cp_parser * /*parser*/, enum omp_clause_code code,
32229 tree list, location_t location)
32231 check_no_duplicate_clause (list, code, omp_clause_code_name[code], location);
32232 tree c = build_omp_clause (location, code);
32233 OMP_CLAUSE_CHAIN (c) = list;
32234 return c;
32237 /* OpenMP 4.0:
32238 parallel
32240 sections
32241 taskgroup */
32243 static tree
32244 cp_parser_omp_clause_cancelkind (cp_parser * /*parser*/,
32245 enum omp_clause_code code,
32246 tree list, location_t location)
32248 tree c = build_omp_clause (location, code);
32249 OMP_CLAUSE_CHAIN (c) = list;
32250 return c;
32253 /* OpenMP 4.5:
32254 nogroup */
32256 static tree
32257 cp_parser_omp_clause_nogroup (cp_parser * /*parser*/,
32258 tree list, location_t location)
32260 check_no_duplicate_clause (list, OMP_CLAUSE_NOGROUP, "nogroup", location);
32261 tree c = build_omp_clause (location, OMP_CLAUSE_NOGROUP);
32262 OMP_CLAUSE_CHAIN (c) = list;
32263 return c;
32266 /* OpenMP 4.5:
32267 simd
32268 threads */
32270 static tree
32271 cp_parser_omp_clause_orderedkind (cp_parser * /*parser*/,
32272 enum omp_clause_code code,
32273 tree list, location_t location)
32275 check_no_duplicate_clause (list, code, omp_clause_code_name[code], location);
32276 tree c = build_omp_clause (location, code);
32277 OMP_CLAUSE_CHAIN (c) = list;
32278 return c;
32281 /* OpenMP 4.0:
32282 num_teams ( expression ) */
32284 static tree
32285 cp_parser_omp_clause_num_teams (cp_parser *parser, tree list,
32286 location_t location)
32288 tree t, c;
32290 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
32291 return list;
32293 t = cp_parser_expression (parser);
32295 if (t == error_mark_node
32296 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
32297 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32298 /*or_comma=*/false,
32299 /*consume_paren=*/true);
32301 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_TEAMS,
32302 "num_teams", location);
32304 c = build_omp_clause (location, OMP_CLAUSE_NUM_TEAMS);
32305 OMP_CLAUSE_NUM_TEAMS_EXPR (c) = t;
32306 OMP_CLAUSE_CHAIN (c) = list;
32308 return c;
32311 /* OpenMP 4.0:
32312 thread_limit ( expression ) */
32314 static tree
32315 cp_parser_omp_clause_thread_limit (cp_parser *parser, tree list,
32316 location_t location)
32318 tree t, c;
32320 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
32321 return list;
32323 t = cp_parser_expression (parser);
32325 if (t == error_mark_node
32326 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
32327 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32328 /*or_comma=*/false,
32329 /*consume_paren=*/true);
32331 check_no_duplicate_clause (list, OMP_CLAUSE_THREAD_LIMIT,
32332 "thread_limit", location);
32334 c = build_omp_clause (location, OMP_CLAUSE_THREAD_LIMIT);
32335 OMP_CLAUSE_THREAD_LIMIT_EXPR (c) = t;
32336 OMP_CLAUSE_CHAIN (c) = list;
32338 return c;
32341 /* OpenMP 4.0:
32342 aligned ( variable-list )
32343 aligned ( variable-list : constant-expression ) */
32345 static tree
32346 cp_parser_omp_clause_aligned (cp_parser *parser, tree list)
32348 tree nlist, c, alignment = NULL_TREE;
32349 bool colon;
32351 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
32352 return list;
32354 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_ALIGNED, list,
32355 &colon);
32357 if (colon)
32359 alignment = cp_parser_constant_expression (parser);
32361 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
32362 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32363 /*or_comma=*/false,
32364 /*consume_paren=*/true);
32366 if (alignment == error_mark_node)
32367 alignment = NULL_TREE;
32370 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
32371 OMP_CLAUSE_ALIGNED_ALIGNMENT (c) = alignment;
32373 return nlist;
32376 /* OpenMP 4.0:
32377 linear ( variable-list )
32378 linear ( variable-list : expression )
32380 OpenMP 4.5:
32381 linear ( modifier ( variable-list ) )
32382 linear ( modifier ( variable-list ) : expression ) */
32384 static tree
32385 cp_parser_omp_clause_linear (cp_parser *parser, tree list,
32386 bool is_cilk_simd_fn, bool declare_simd)
32388 tree nlist, c, step = integer_one_node;
32389 bool colon;
32390 enum omp_clause_linear_kind kind = OMP_CLAUSE_LINEAR_DEFAULT;
32392 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
32393 return list;
32395 if (!is_cilk_simd_fn
32396 && cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32398 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
32399 const char *p = IDENTIFIER_POINTER (id);
32401 if (strcmp ("ref", p) == 0)
32402 kind = OMP_CLAUSE_LINEAR_REF;
32403 else if (strcmp ("val", p) == 0)
32404 kind = OMP_CLAUSE_LINEAR_VAL;
32405 else if (strcmp ("uval", p) == 0)
32406 kind = OMP_CLAUSE_LINEAR_UVAL;
32407 if (cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_PAREN))
32408 cp_lexer_consume_token (parser->lexer);
32409 else
32410 kind = OMP_CLAUSE_LINEAR_DEFAULT;
32413 if (kind == OMP_CLAUSE_LINEAR_DEFAULT)
32414 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_LINEAR, list,
32415 &colon);
32416 else
32418 nlist = cp_parser_omp_var_list (parser, OMP_CLAUSE_LINEAR, list);
32419 colon = cp_lexer_next_token_is (parser->lexer, CPP_COLON);
32420 if (colon)
32421 cp_parser_require (parser, CPP_COLON, RT_COLON);
32422 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
32423 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32424 /*or_comma=*/false,
32425 /*consume_paren=*/true);
32428 if (colon)
32430 step = NULL_TREE;
32431 if (declare_simd
32432 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
32433 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_CLOSE_PAREN))
32435 cp_token *token = cp_lexer_peek_token (parser->lexer);
32436 cp_parser_parse_tentatively (parser);
32437 step = cp_parser_id_expression (parser, /*template_p=*/false,
32438 /*check_dependency_p=*/true,
32439 /*template_p=*/NULL,
32440 /*declarator_p=*/false,
32441 /*optional_p=*/false);
32442 if (step != error_mark_node)
32443 step = cp_parser_lookup_name_simple (parser, step, token->location);
32444 if (step == error_mark_node)
32446 step = NULL_TREE;
32447 cp_parser_abort_tentative_parse (parser);
32449 else if (!cp_parser_parse_definitely (parser))
32450 step = NULL_TREE;
32452 if (!step)
32453 step = cp_parser_expression (parser);
32455 if (is_cilk_simd_fn && TREE_CODE (step) == PARM_DECL)
32457 sorry ("using parameters for %<linear%> step is not supported yet");
32458 step = integer_one_node;
32460 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
32461 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32462 /*or_comma=*/false,
32463 /*consume_paren=*/true);
32465 if (step == error_mark_node)
32466 return list;
32469 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
32471 OMP_CLAUSE_LINEAR_STEP (c) = step;
32472 OMP_CLAUSE_LINEAR_KIND (c) = kind;
32475 return nlist;
32478 /* OpenMP 4.0:
32479 safelen ( constant-expression ) */
32481 static tree
32482 cp_parser_omp_clause_safelen (cp_parser *parser, tree list,
32483 location_t location)
32485 tree t, c;
32487 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
32488 return list;
32490 t = cp_parser_constant_expression (parser);
32492 if (t == 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);
32498 check_no_duplicate_clause (list, OMP_CLAUSE_SAFELEN, "safelen", location);
32500 c = build_omp_clause (location, OMP_CLAUSE_SAFELEN);
32501 OMP_CLAUSE_SAFELEN_EXPR (c) = t;
32502 OMP_CLAUSE_CHAIN (c) = list;
32504 return c;
32507 /* OpenMP 4.0:
32508 simdlen ( constant-expression ) */
32510 static tree
32511 cp_parser_omp_clause_simdlen (cp_parser *parser, tree list,
32512 location_t location)
32514 tree t, c;
32516 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
32517 return list;
32519 t = cp_parser_constant_expression (parser);
32521 if (t == error_mark_node
32522 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
32523 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32524 /*or_comma=*/false,
32525 /*consume_paren=*/true);
32527 check_no_duplicate_clause (list, OMP_CLAUSE_SIMDLEN, "simdlen", location);
32529 c = build_omp_clause (location, OMP_CLAUSE_SIMDLEN);
32530 OMP_CLAUSE_SIMDLEN_EXPR (c) = t;
32531 OMP_CLAUSE_CHAIN (c) = list;
32533 return c;
32536 /* OpenMP 4.5:
32537 vec:
32538 identifier [+/- integer]
32539 vec , identifier [+/- integer]
32542 static tree
32543 cp_parser_omp_clause_depend_sink (cp_parser *parser, location_t clause_loc,
32544 tree list)
32546 tree vec = NULL;
32548 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
32550 cp_parser_error (parser, "expected identifier");
32551 return list;
32554 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32556 location_t id_loc = cp_lexer_peek_token (parser->lexer)->location;
32557 tree t, identifier = cp_parser_identifier (parser);
32558 tree addend = NULL;
32560 if (identifier == error_mark_node)
32561 t = error_mark_node;
32562 else
32564 t = cp_parser_lookup_name_simple
32565 (parser, identifier,
32566 cp_lexer_peek_token (parser->lexer)->location);
32567 if (t == error_mark_node)
32568 cp_parser_name_lookup_error (parser, identifier, t, NLE_NULL,
32569 id_loc);
32572 bool neg = false;
32573 if (cp_lexer_next_token_is (parser->lexer, CPP_MINUS))
32574 neg = true;
32575 else if (!cp_lexer_next_token_is (parser->lexer, CPP_PLUS))
32577 addend = integer_zero_node;
32578 goto add_to_vector;
32580 cp_lexer_consume_token (parser->lexer);
32582 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NUMBER))
32584 cp_parser_error (parser, "expected integer");
32585 return list;
32588 addend = cp_lexer_peek_token (parser->lexer)->u.value;
32589 if (TREE_CODE (addend) != INTEGER_CST)
32591 cp_parser_error (parser, "expected integer");
32592 return list;
32594 cp_lexer_consume_token (parser->lexer);
32596 add_to_vector:
32597 if (t != error_mark_node)
32599 vec = tree_cons (addend, t, vec);
32600 if (neg)
32601 OMP_CLAUSE_DEPEND_SINK_NEGATIVE (vec) = 1;
32604 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
32605 break;
32607 cp_lexer_consume_token (parser->lexer);
32610 if (cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN) && vec)
32612 tree u = build_omp_clause (clause_loc, OMP_CLAUSE_DEPEND);
32613 OMP_CLAUSE_DEPEND_KIND (u) = OMP_CLAUSE_DEPEND_SINK;
32614 OMP_CLAUSE_DECL (u) = nreverse (vec);
32615 OMP_CLAUSE_CHAIN (u) = list;
32616 return u;
32618 return list;
32621 /* OpenMP 4.0:
32622 depend ( depend-kind : variable-list )
32624 depend-kind:
32625 in | out | inout
32627 OpenMP 4.5:
32628 depend ( source )
32630 depend ( sink : vec ) */
32632 static tree
32633 cp_parser_omp_clause_depend (cp_parser *parser, tree list, location_t loc)
32635 tree nlist, c;
32636 enum omp_clause_depend_kind kind = OMP_CLAUSE_DEPEND_INOUT;
32638 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
32639 return list;
32641 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32643 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
32644 const char *p = IDENTIFIER_POINTER (id);
32646 if (strcmp ("in", p) == 0)
32647 kind = OMP_CLAUSE_DEPEND_IN;
32648 else if (strcmp ("inout", p) == 0)
32649 kind = OMP_CLAUSE_DEPEND_INOUT;
32650 else if (strcmp ("out", p) == 0)
32651 kind = OMP_CLAUSE_DEPEND_OUT;
32652 else if (strcmp ("source", p) == 0)
32653 kind = OMP_CLAUSE_DEPEND_SOURCE;
32654 else if (strcmp ("sink", p) == 0)
32655 kind = OMP_CLAUSE_DEPEND_SINK;
32656 else
32657 goto invalid_kind;
32659 else
32660 goto invalid_kind;
32662 cp_lexer_consume_token (parser->lexer);
32664 if (kind == OMP_CLAUSE_DEPEND_SOURCE)
32666 c = build_omp_clause (loc, OMP_CLAUSE_DEPEND);
32667 OMP_CLAUSE_DEPEND_KIND (c) = kind;
32668 OMP_CLAUSE_DECL (c) = NULL_TREE;
32669 OMP_CLAUSE_CHAIN (c) = list;
32670 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
32671 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32672 /*or_comma=*/false,
32673 /*consume_paren=*/true);
32674 return c;
32677 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
32678 goto resync_fail;
32680 if (kind == OMP_CLAUSE_DEPEND_SINK)
32681 nlist = cp_parser_omp_clause_depend_sink (parser, loc, list);
32682 else
32684 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_DEPEND,
32685 list, NULL);
32687 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
32688 OMP_CLAUSE_DEPEND_KIND (c) = kind;
32690 return nlist;
32692 invalid_kind:
32693 cp_parser_error (parser, "invalid depend kind");
32694 resync_fail:
32695 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32696 /*or_comma=*/false,
32697 /*consume_paren=*/true);
32698 return list;
32701 /* OpenMP 4.0:
32702 map ( map-kind : variable-list )
32703 map ( variable-list )
32705 map-kind:
32706 alloc | to | from | tofrom
32708 OpenMP 4.5:
32709 map-kind:
32710 alloc | to | from | tofrom | release | delete
32712 map ( always [,] map-kind: variable-list ) */
32714 static tree
32715 cp_parser_omp_clause_map (cp_parser *parser, tree list)
32717 tree nlist, c;
32718 enum gomp_map_kind kind = GOMP_MAP_TOFROM;
32719 bool always = false;
32721 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
32722 return list;
32724 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32726 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
32727 const char *p = IDENTIFIER_POINTER (id);
32729 if (strcmp ("always", p) == 0)
32731 int nth = 2;
32732 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COMMA)
32733 nth++;
32734 if ((cp_lexer_peek_nth_token (parser->lexer, nth)->type == CPP_NAME
32735 || (cp_lexer_peek_nth_token (parser->lexer, nth)->keyword
32736 == RID_DELETE))
32737 && (cp_lexer_peek_nth_token (parser->lexer, nth + 1)->type
32738 == CPP_COLON))
32740 always = true;
32741 cp_lexer_consume_token (parser->lexer);
32742 if (nth == 3)
32743 cp_lexer_consume_token (parser->lexer);
32748 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
32749 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
32751 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
32752 const char *p = IDENTIFIER_POINTER (id);
32754 if (strcmp ("alloc", p) == 0)
32755 kind = GOMP_MAP_ALLOC;
32756 else if (strcmp ("to", p) == 0)
32757 kind = always ? GOMP_MAP_ALWAYS_TO : GOMP_MAP_TO;
32758 else if (strcmp ("from", p) == 0)
32759 kind = always ? GOMP_MAP_ALWAYS_FROM : GOMP_MAP_FROM;
32760 else if (strcmp ("tofrom", p) == 0)
32761 kind = always ? GOMP_MAP_ALWAYS_TOFROM : GOMP_MAP_TOFROM;
32762 else if (strcmp ("release", p) == 0)
32763 kind = GOMP_MAP_RELEASE;
32764 else
32766 cp_parser_error (parser, "invalid map kind");
32767 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32768 /*or_comma=*/false,
32769 /*consume_paren=*/true);
32770 return list;
32772 cp_lexer_consume_token (parser->lexer);
32773 cp_lexer_consume_token (parser->lexer);
32775 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DELETE)
32776 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
32778 kind = GOMP_MAP_DELETE;
32779 cp_lexer_consume_token (parser->lexer);
32780 cp_lexer_consume_token (parser->lexer);
32783 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_MAP, list,
32784 NULL);
32786 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
32787 OMP_CLAUSE_SET_MAP_KIND (c, kind);
32789 return nlist;
32792 /* OpenMP 4.0:
32793 device ( expression ) */
32795 static tree
32796 cp_parser_omp_clause_device (cp_parser *parser, tree list,
32797 location_t location)
32799 tree t, c;
32801 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
32802 return list;
32804 t = cp_parser_expression (parser);
32806 if (t == error_mark_node
32807 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
32808 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32809 /*or_comma=*/false,
32810 /*consume_paren=*/true);
32812 check_no_duplicate_clause (list, OMP_CLAUSE_DEVICE,
32813 "device", location);
32815 c = build_omp_clause (location, OMP_CLAUSE_DEVICE);
32816 OMP_CLAUSE_DEVICE_ID (c) = t;
32817 OMP_CLAUSE_CHAIN (c) = list;
32819 return c;
32822 /* OpenMP 4.0:
32823 dist_schedule ( static )
32824 dist_schedule ( static , expression ) */
32826 static tree
32827 cp_parser_omp_clause_dist_schedule (cp_parser *parser, tree list,
32828 location_t location)
32830 tree c, t;
32832 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
32833 return list;
32835 c = build_omp_clause (location, OMP_CLAUSE_DIST_SCHEDULE);
32837 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
32838 goto invalid_kind;
32839 cp_lexer_consume_token (parser->lexer);
32841 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
32843 cp_lexer_consume_token (parser->lexer);
32845 t = cp_parser_assignment_expression (parser);
32847 if (t == error_mark_node)
32848 goto resync_fail;
32849 OMP_CLAUSE_DIST_SCHEDULE_CHUNK_EXPR (c) = t;
32851 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
32852 goto resync_fail;
32854 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
32855 goto resync_fail;
32857 check_no_duplicate_clause (list, OMP_CLAUSE_DIST_SCHEDULE, "dist_schedule",
32858 location);
32859 OMP_CLAUSE_CHAIN (c) = list;
32860 return c;
32862 invalid_kind:
32863 cp_parser_error (parser, "invalid dist_schedule kind");
32864 resync_fail:
32865 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32866 /*or_comma=*/false,
32867 /*consume_paren=*/true);
32868 return list;
32871 /* OpenMP 4.0:
32872 proc_bind ( proc-bind-kind )
32874 proc-bind-kind:
32875 master | close | spread */
32877 static tree
32878 cp_parser_omp_clause_proc_bind (cp_parser *parser, tree list,
32879 location_t location)
32881 tree c;
32882 enum omp_clause_proc_bind_kind kind;
32884 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
32885 return list;
32887 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32889 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
32890 const char *p = IDENTIFIER_POINTER (id);
32892 if (strcmp ("master", p) == 0)
32893 kind = OMP_CLAUSE_PROC_BIND_MASTER;
32894 else if (strcmp ("close", p) == 0)
32895 kind = OMP_CLAUSE_PROC_BIND_CLOSE;
32896 else if (strcmp ("spread", p) == 0)
32897 kind = OMP_CLAUSE_PROC_BIND_SPREAD;
32898 else
32899 goto invalid_kind;
32901 else
32902 goto invalid_kind;
32904 cp_lexer_consume_token (parser->lexer);
32905 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
32906 goto resync_fail;
32908 c = build_omp_clause (location, OMP_CLAUSE_PROC_BIND);
32909 check_no_duplicate_clause (list, OMP_CLAUSE_PROC_BIND, "proc_bind",
32910 location);
32911 OMP_CLAUSE_PROC_BIND_KIND (c) = kind;
32912 OMP_CLAUSE_CHAIN (c) = list;
32913 return c;
32915 invalid_kind:
32916 cp_parser_error (parser, "invalid depend kind");
32917 resync_fail:
32918 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32919 /*or_comma=*/false,
32920 /*consume_paren=*/true);
32921 return list;
32924 /* OpenACC:
32925 async [( int-expr )] */
32927 static tree
32928 cp_parser_oacc_clause_async (cp_parser *parser, tree list)
32930 tree c, t;
32931 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
32933 t = build_int_cst (integer_type_node, GOMP_ASYNC_NOVAL);
32935 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
32937 cp_lexer_consume_token (parser->lexer);
32939 t = cp_parser_expression (parser);
32940 if (t == error_mark_node
32941 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
32942 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32943 /*or_comma=*/false,
32944 /*consume_paren=*/true);
32947 check_no_duplicate_clause (list, OMP_CLAUSE_ASYNC, "async", loc);
32949 c = build_omp_clause (loc, OMP_CLAUSE_ASYNC);
32950 OMP_CLAUSE_ASYNC_EXPR (c) = t;
32951 OMP_CLAUSE_CHAIN (c) = list;
32952 list = c;
32954 return list;
32957 /* Parse all OpenACC clauses. The set clauses allowed by the directive
32958 is a bitmask in MASK. Return the list of clauses found. */
32960 static tree
32961 cp_parser_oacc_all_clauses (cp_parser *parser, omp_clause_mask mask,
32962 const char *where, cp_token *pragma_tok,
32963 bool finish_p = true)
32965 tree clauses = NULL;
32966 bool first = true;
32968 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
32970 location_t here;
32971 pragma_omp_clause c_kind;
32972 omp_clause_code code;
32973 const char *c_name;
32974 tree prev = clauses;
32976 if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
32977 cp_lexer_consume_token (parser->lexer);
32979 here = cp_lexer_peek_token (parser->lexer)->location;
32980 c_kind = cp_parser_omp_clause_name (parser);
32982 switch (c_kind)
32984 case PRAGMA_OACC_CLAUSE_ASYNC:
32985 clauses = cp_parser_oacc_clause_async (parser, clauses);
32986 c_name = "async";
32987 break;
32988 case PRAGMA_OACC_CLAUSE_AUTO:
32989 clauses = cp_parser_oacc_simple_clause (parser, OMP_CLAUSE_AUTO,
32990 clauses, here);
32991 c_name = "auto";
32992 break;
32993 case PRAGMA_OACC_CLAUSE_COLLAPSE:
32994 clauses = cp_parser_omp_clause_collapse (parser, clauses, here);
32995 c_name = "collapse";
32996 break;
32997 case PRAGMA_OACC_CLAUSE_COPY:
32998 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
32999 c_name = "copy";
33000 break;
33001 case PRAGMA_OACC_CLAUSE_COPYIN:
33002 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33003 c_name = "copyin";
33004 break;
33005 case PRAGMA_OACC_CLAUSE_COPYOUT:
33006 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33007 c_name = "copyout";
33008 break;
33009 case PRAGMA_OACC_CLAUSE_CREATE:
33010 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33011 c_name = "create";
33012 break;
33013 case PRAGMA_OACC_CLAUSE_DELETE:
33014 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33015 c_name = "delete";
33016 break;
33017 case PRAGMA_OMP_CLAUSE_DEFAULT:
33018 clauses = cp_parser_omp_clause_default (parser, clauses, here, true);
33019 c_name = "default";
33020 break;
33021 case PRAGMA_OACC_CLAUSE_DEVICE:
33022 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33023 c_name = "device";
33024 break;
33025 case PRAGMA_OACC_CLAUSE_DEVICEPTR:
33026 clauses = cp_parser_oacc_data_clause_deviceptr (parser, clauses);
33027 c_name = "deviceptr";
33028 break;
33029 case PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT:
33030 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33031 c_name = "device_resident";
33032 break;
33033 case PRAGMA_OACC_CLAUSE_FIRSTPRIVATE:
33034 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
33035 clauses);
33036 c_name = "firstprivate";
33037 break;
33038 case PRAGMA_OACC_CLAUSE_GANG:
33039 c_name = "gang";
33040 clauses = cp_parser_oacc_shape_clause (parser, OMP_CLAUSE_GANG,
33041 c_name, clauses);
33042 break;
33043 case PRAGMA_OACC_CLAUSE_HOST:
33044 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33045 c_name = "host";
33046 break;
33047 case PRAGMA_OACC_CLAUSE_IF:
33048 clauses = cp_parser_omp_clause_if (parser, clauses, here, false);
33049 c_name = "if";
33050 break;
33051 case PRAGMA_OACC_CLAUSE_INDEPENDENT:
33052 clauses = cp_parser_oacc_simple_clause (parser,
33053 OMP_CLAUSE_INDEPENDENT,
33054 clauses, here);
33055 c_name = "independent";
33056 break;
33057 case PRAGMA_OACC_CLAUSE_LINK:
33058 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33059 c_name = "link";
33060 break;
33061 case PRAGMA_OACC_CLAUSE_NUM_GANGS:
33062 code = OMP_CLAUSE_NUM_GANGS;
33063 c_name = "num_gangs";
33064 clauses = cp_parser_oacc_single_int_clause (parser, code, c_name,
33065 clauses);
33066 break;
33067 case PRAGMA_OACC_CLAUSE_NUM_WORKERS:
33068 c_name = "num_workers";
33069 code = OMP_CLAUSE_NUM_WORKERS;
33070 clauses = cp_parser_oacc_single_int_clause (parser, code, c_name,
33071 clauses);
33072 break;
33073 case PRAGMA_OACC_CLAUSE_PRESENT:
33074 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33075 c_name = "present";
33076 break;
33077 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY:
33078 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33079 c_name = "present_or_copy";
33080 break;
33081 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN:
33082 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33083 c_name = "present_or_copyin";
33084 break;
33085 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT:
33086 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33087 c_name = "present_or_copyout";
33088 break;
33089 case PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE:
33090 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33091 c_name = "present_or_create";
33092 break;
33093 case PRAGMA_OACC_CLAUSE_PRIVATE:
33094 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
33095 clauses);
33096 c_name = "private";
33097 break;
33098 case PRAGMA_OACC_CLAUSE_REDUCTION:
33099 clauses = cp_parser_omp_clause_reduction (parser, clauses);
33100 c_name = "reduction";
33101 break;
33102 case PRAGMA_OACC_CLAUSE_SELF:
33103 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33104 c_name = "self";
33105 break;
33106 case PRAGMA_OACC_CLAUSE_SEQ:
33107 clauses = cp_parser_oacc_simple_clause (parser, OMP_CLAUSE_SEQ,
33108 clauses, here);
33109 c_name = "seq";
33110 break;
33111 case PRAGMA_OACC_CLAUSE_TILE:
33112 clauses = cp_parser_oacc_clause_tile (parser, here, clauses);
33113 c_name = "tile";
33114 break;
33115 case PRAGMA_OACC_CLAUSE_USE_DEVICE:
33116 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_USE_DEVICE_PTR,
33117 clauses);
33118 c_name = "use_device";
33119 break;
33120 case PRAGMA_OACC_CLAUSE_VECTOR:
33121 c_name = "vector";
33122 clauses = cp_parser_oacc_shape_clause (parser, OMP_CLAUSE_VECTOR,
33123 c_name, clauses);
33124 break;
33125 case PRAGMA_OACC_CLAUSE_VECTOR_LENGTH:
33126 c_name = "vector_length";
33127 code = OMP_CLAUSE_VECTOR_LENGTH;
33128 clauses = cp_parser_oacc_single_int_clause (parser, code, c_name,
33129 clauses);
33130 break;
33131 case PRAGMA_OACC_CLAUSE_WAIT:
33132 clauses = cp_parser_oacc_clause_wait (parser, clauses);
33133 c_name = "wait";
33134 break;
33135 case PRAGMA_OACC_CLAUSE_WORKER:
33136 c_name = "worker";
33137 clauses = cp_parser_oacc_shape_clause (parser, OMP_CLAUSE_WORKER,
33138 c_name, clauses);
33139 break;
33140 default:
33141 cp_parser_error (parser, "expected %<#pragma acc%> clause");
33142 goto saw_error;
33145 first = false;
33147 if (((mask >> c_kind) & 1) == 0)
33149 /* Remove the invalid clause(s) from the list to avoid
33150 confusing the rest of the compiler. */
33151 clauses = prev;
33152 error_at (here, "%qs is not valid for %qs", c_name, where);
33156 saw_error:
33157 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
33159 if (finish_p)
33160 return finish_omp_clauses (clauses, C_ORT_ACC);
33162 return clauses;
33165 /* Parse all OpenMP clauses. The set clauses allowed by the directive
33166 is a bitmask in MASK. Return the list of clauses found; the result
33167 of clause default goes in *pdefault. */
33169 static tree
33170 cp_parser_omp_all_clauses (cp_parser *parser, omp_clause_mask mask,
33171 const char *where, cp_token *pragma_tok,
33172 bool finish_p = true)
33174 tree clauses = NULL;
33175 bool first = true;
33176 cp_token *token = NULL;
33178 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
33180 pragma_omp_clause c_kind;
33181 const char *c_name;
33182 tree prev = clauses;
33184 if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
33185 cp_lexer_consume_token (parser->lexer);
33187 token = cp_lexer_peek_token (parser->lexer);
33188 c_kind = cp_parser_omp_clause_name (parser);
33190 switch (c_kind)
33192 case PRAGMA_OMP_CLAUSE_COLLAPSE:
33193 clauses = cp_parser_omp_clause_collapse (parser, clauses,
33194 token->location);
33195 c_name = "collapse";
33196 break;
33197 case PRAGMA_OMP_CLAUSE_COPYIN:
33198 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYIN, clauses);
33199 c_name = "copyin";
33200 break;
33201 case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
33202 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYPRIVATE,
33203 clauses);
33204 c_name = "copyprivate";
33205 break;
33206 case PRAGMA_OMP_CLAUSE_DEFAULT:
33207 clauses = cp_parser_omp_clause_default (parser, clauses,
33208 token->location, false);
33209 c_name = "default";
33210 break;
33211 case PRAGMA_OMP_CLAUSE_FINAL:
33212 clauses = cp_parser_omp_clause_final (parser, clauses, token->location);
33213 c_name = "final";
33214 break;
33215 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
33216 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
33217 clauses);
33218 c_name = "firstprivate";
33219 break;
33220 case PRAGMA_OMP_CLAUSE_GRAINSIZE:
33221 clauses = cp_parser_omp_clause_grainsize (parser, clauses,
33222 token->location);
33223 c_name = "grainsize";
33224 break;
33225 case PRAGMA_OMP_CLAUSE_HINT:
33226 clauses = cp_parser_omp_clause_hint (parser, clauses,
33227 token->location);
33228 c_name = "hint";
33229 break;
33230 case PRAGMA_OMP_CLAUSE_DEFAULTMAP:
33231 clauses = cp_parser_omp_clause_defaultmap (parser, clauses,
33232 token->location);
33233 c_name = "defaultmap";
33234 break;
33235 case PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR:
33236 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_USE_DEVICE_PTR,
33237 clauses);
33238 c_name = "use_device_ptr";
33239 break;
33240 case PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR:
33241 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_IS_DEVICE_PTR,
33242 clauses);
33243 c_name = "is_device_ptr";
33244 break;
33245 case PRAGMA_OMP_CLAUSE_IF:
33246 clauses = cp_parser_omp_clause_if (parser, clauses, token->location,
33247 true);
33248 c_name = "if";
33249 break;
33250 case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
33251 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LASTPRIVATE,
33252 clauses);
33253 c_name = "lastprivate";
33254 break;
33255 case PRAGMA_OMP_CLAUSE_MERGEABLE:
33256 clauses = cp_parser_omp_clause_mergeable (parser, clauses,
33257 token->location);
33258 c_name = "mergeable";
33259 break;
33260 case PRAGMA_OMP_CLAUSE_NOWAIT:
33261 clauses = cp_parser_omp_clause_nowait (parser, clauses, token->location);
33262 c_name = "nowait";
33263 break;
33264 case PRAGMA_OMP_CLAUSE_NUM_TASKS:
33265 clauses = cp_parser_omp_clause_num_tasks (parser, clauses,
33266 token->location);
33267 c_name = "num_tasks";
33268 break;
33269 case PRAGMA_OMP_CLAUSE_NUM_THREADS:
33270 clauses = cp_parser_omp_clause_num_threads (parser, clauses,
33271 token->location);
33272 c_name = "num_threads";
33273 break;
33274 case PRAGMA_OMP_CLAUSE_ORDERED:
33275 clauses = cp_parser_omp_clause_ordered (parser, clauses,
33276 token->location);
33277 c_name = "ordered";
33278 break;
33279 case PRAGMA_OMP_CLAUSE_PRIORITY:
33280 clauses = cp_parser_omp_clause_priority (parser, clauses,
33281 token->location);
33282 c_name = "priority";
33283 break;
33284 case PRAGMA_OMP_CLAUSE_PRIVATE:
33285 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
33286 clauses);
33287 c_name = "private";
33288 break;
33289 case PRAGMA_OMP_CLAUSE_REDUCTION:
33290 clauses = cp_parser_omp_clause_reduction (parser, clauses);
33291 c_name = "reduction";
33292 break;
33293 case PRAGMA_OMP_CLAUSE_SCHEDULE:
33294 clauses = cp_parser_omp_clause_schedule (parser, clauses,
33295 token->location);
33296 c_name = "schedule";
33297 break;
33298 case PRAGMA_OMP_CLAUSE_SHARED:
33299 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_SHARED,
33300 clauses);
33301 c_name = "shared";
33302 break;
33303 case PRAGMA_OMP_CLAUSE_UNTIED:
33304 clauses = cp_parser_omp_clause_untied (parser, clauses,
33305 token->location);
33306 c_name = "untied";
33307 break;
33308 case PRAGMA_OMP_CLAUSE_INBRANCH:
33309 case PRAGMA_CILK_CLAUSE_MASK:
33310 clauses = cp_parser_omp_clause_branch (parser, OMP_CLAUSE_INBRANCH,
33311 clauses, token->location);
33312 c_name = "inbranch";
33313 break;
33314 case PRAGMA_OMP_CLAUSE_NOTINBRANCH:
33315 case PRAGMA_CILK_CLAUSE_NOMASK:
33316 clauses = cp_parser_omp_clause_branch (parser,
33317 OMP_CLAUSE_NOTINBRANCH,
33318 clauses, token->location);
33319 c_name = "notinbranch";
33320 break;
33321 case PRAGMA_OMP_CLAUSE_PARALLEL:
33322 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_PARALLEL,
33323 clauses, token->location);
33324 c_name = "parallel";
33325 if (!first)
33327 clause_not_first:
33328 error_at (token->location, "%qs must be the first clause of %qs",
33329 c_name, where);
33330 clauses = prev;
33332 break;
33333 case PRAGMA_OMP_CLAUSE_FOR:
33334 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_FOR,
33335 clauses, token->location);
33336 c_name = "for";
33337 if (!first)
33338 goto clause_not_first;
33339 break;
33340 case PRAGMA_OMP_CLAUSE_SECTIONS:
33341 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_SECTIONS,
33342 clauses, token->location);
33343 c_name = "sections";
33344 if (!first)
33345 goto clause_not_first;
33346 break;
33347 case PRAGMA_OMP_CLAUSE_TASKGROUP:
33348 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_TASKGROUP,
33349 clauses, token->location);
33350 c_name = "taskgroup";
33351 if (!first)
33352 goto clause_not_first;
33353 break;
33354 case PRAGMA_OMP_CLAUSE_LINK:
33355 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LINK, clauses);
33356 c_name = "to";
33357 break;
33358 case PRAGMA_OMP_CLAUSE_TO:
33359 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINK)) != 0)
33360 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_TO_DECLARE,
33361 clauses);
33362 else
33363 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_TO, clauses);
33364 c_name = "to";
33365 break;
33366 case PRAGMA_OMP_CLAUSE_FROM:
33367 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FROM, clauses);
33368 c_name = "from";
33369 break;
33370 case PRAGMA_OMP_CLAUSE_UNIFORM:
33371 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_UNIFORM,
33372 clauses);
33373 c_name = "uniform";
33374 break;
33375 case PRAGMA_OMP_CLAUSE_NUM_TEAMS:
33376 clauses = cp_parser_omp_clause_num_teams (parser, clauses,
33377 token->location);
33378 c_name = "num_teams";
33379 break;
33380 case PRAGMA_OMP_CLAUSE_THREAD_LIMIT:
33381 clauses = cp_parser_omp_clause_thread_limit (parser, clauses,
33382 token->location);
33383 c_name = "thread_limit";
33384 break;
33385 case PRAGMA_OMP_CLAUSE_ALIGNED:
33386 clauses = cp_parser_omp_clause_aligned (parser, clauses);
33387 c_name = "aligned";
33388 break;
33389 case PRAGMA_OMP_CLAUSE_LINEAR:
33391 bool cilk_simd_fn = false, declare_simd = false;
33392 if (((mask >> PRAGMA_CILK_CLAUSE_VECTORLENGTH) & 1) != 0)
33393 cilk_simd_fn = true;
33394 else if (((mask >> PRAGMA_OMP_CLAUSE_UNIFORM) & 1) != 0)
33395 declare_simd = true;
33396 clauses = cp_parser_omp_clause_linear (parser, clauses,
33397 cilk_simd_fn, declare_simd);
33399 c_name = "linear";
33400 break;
33401 case PRAGMA_OMP_CLAUSE_DEPEND:
33402 clauses = cp_parser_omp_clause_depend (parser, clauses,
33403 token->location);
33404 c_name = "depend";
33405 break;
33406 case PRAGMA_OMP_CLAUSE_MAP:
33407 clauses = cp_parser_omp_clause_map (parser, clauses);
33408 c_name = "map";
33409 break;
33410 case PRAGMA_OMP_CLAUSE_DEVICE:
33411 clauses = cp_parser_omp_clause_device (parser, clauses,
33412 token->location);
33413 c_name = "device";
33414 break;
33415 case PRAGMA_OMP_CLAUSE_DIST_SCHEDULE:
33416 clauses = cp_parser_omp_clause_dist_schedule (parser, clauses,
33417 token->location);
33418 c_name = "dist_schedule";
33419 break;
33420 case PRAGMA_OMP_CLAUSE_PROC_BIND:
33421 clauses = cp_parser_omp_clause_proc_bind (parser, clauses,
33422 token->location);
33423 c_name = "proc_bind";
33424 break;
33425 case PRAGMA_OMP_CLAUSE_SAFELEN:
33426 clauses = cp_parser_omp_clause_safelen (parser, clauses,
33427 token->location);
33428 c_name = "safelen";
33429 break;
33430 case PRAGMA_OMP_CLAUSE_SIMDLEN:
33431 clauses = cp_parser_omp_clause_simdlen (parser, clauses,
33432 token->location);
33433 c_name = "simdlen";
33434 break;
33435 case PRAGMA_OMP_CLAUSE_NOGROUP:
33436 clauses = cp_parser_omp_clause_nogroup (parser, clauses,
33437 token->location);
33438 c_name = "nogroup";
33439 break;
33440 case PRAGMA_OMP_CLAUSE_THREADS:
33441 clauses
33442 = cp_parser_omp_clause_orderedkind (parser, OMP_CLAUSE_THREADS,
33443 clauses, token->location);
33444 c_name = "threads";
33445 break;
33446 case PRAGMA_OMP_CLAUSE_SIMD:
33447 clauses
33448 = cp_parser_omp_clause_orderedkind (parser, OMP_CLAUSE_SIMD,
33449 clauses, token->location);
33450 c_name = "simd";
33451 break;
33452 case PRAGMA_CILK_CLAUSE_VECTORLENGTH:
33453 clauses = cp_parser_cilk_simd_vectorlength (parser, clauses, true);
33454 c_name = "simdlen";
33455 break;
33456 default:
33457 cp_parser_error (parser, "expected %<#pragma omp%> clause");
33458 goto saw_error;
33461 first = false;
33463 if (((mask >> c_kind) & 1) == 0)
33465 /* Remove the invalid clause(s) from the list to avoid
33466 confusing the rest of the compiler. */
33467 clauses = prev;
33468 error_at (token->location, "%qs is not valid for %qs", c_name, where);
33471 saw_error:
33472 /* In Cilk Plus SIMD enabled functions there is no pragma_token, so
33473 no reason to skip to the end. */
33474 if (!(flag_cilkplus && pragma_tok == NULL))
33475 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
33476 if (finish_p)
33478 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNIFORM)) != 0)
33479 return finish_omp_clauses (clauses, C_ORT_OMP_DECLARE_SIMD);
33480 else
33481 return finish_omp_clauses (clauses, C_ORT_OMP);
33483 return clauses;
33486 /* OpenMP 2.5:
33487 structured-block:
33488 statement
33490 In practice, we're also interested in adding the statement to an
33491 outer node. So it is convenient if we work around the fact that
33492 cp_parser_statement calls add_stmt. */
33494 static unsigned
33495 cp_parser_begin_omp_structured_block (cp_parser *parser)
33497 unsigned save = parser->in_statement;
33499 /* Only move the values to IN_OMP_BLOCK if they weren't false.
33500 This preserves the "not within loop or switch" style error messages
33501 for nonsense cases like
33502 void foo() {
33503 #pragma omp single
33504 break;
33507 if (parser->in_statement)
33508 parser->in_statement = IN_OMP_BLOCK;
33510 return save;
33513 static void
33514 cp_parser_end_omp_structured_block (cp_parser *parser, unsigned save)
33516 parser->in_statement = save;
33519 static tree
33520 cp_parser_omp_structured_block (cp_parser *parser, bool *if_p)
33522 tree stmt = begin_omp_structured_block ();
33523 unsigned int save = cp_parser_begin_omp_structured_block (parser);
33525 cp_parser_statement (parser, NULL_TREE, false, if_p);
33527 cp_parser_end_omp_structured_block (parser, save);
33528 return finish_omp_structured_block (stmt);
33531 /* OpenMP 2.5:
33532 # pragma omp atomic new-line
33533 expression-stmt
33535 expression-stmt:
33536 x binop= expr | x++ | ++x | x-- | --x
33537 binop:
33538 +, *, -, /, &, ^, |, <<, >>
33540 where x is an lvalue expression with scalar type.
33542 OpenMP 3.1:
33543 # pragma omp atomic new-line
33544 update-stmt
33546 # pragma omp atomic read new-line
33547 read-stmt
33549 # pragma omp atomic write new-line
33550 write-stmt
33552 # pragma omp atomic update new-line
33553 update-stmt
33555 # pragma omp atomic capture new-line
33556 capture-stmt
33558 # pragma omp atomic capture new-line
33559 capture-block
33561 read-stmt:
33562 v = x
33563 write-stmt:
33564 x = expr
33565 update-stmt:
33566 expression-stmt | x = x binop expr
33567 capture-stmt:
33568 v = expression-stmt
33569 capture-block:
33570 { v = x; update-stmt; } | { update-stmt; v = x; }
33572 OpenMP 4.0:
33573 update-stmt:
33574 expression-stmt | x = x binop expr | x = expr binop x
33575 capture-stmt:
33576 v = update-stmt
33577 capture-block:
33578 { v = x; update-stmt; } | { update-stmt; v = x; } | { v = x; x = expr; }
33580 where x and v are lvalue expressions with scalar type. */
33582 static void
33583 cp_parser_omp_atomic (cp_parser *parser, cp_token *pragma_tok)
33585 tree lhs = NULL_TREE, rhs = NULL_TREE, v = NULL_TREE, lhs1 = NULL_TREE;
33586 tree rhs1 = NULL_TREE, orig_lhs;
33587 enum tree_code code = OMP_ATOMIC, opcode = NOP_EXPR;
33588 bool structured_block = false;
33589 bool seq_cst = false;
33591 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
33593 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
33594 const char *p = IDENTIFIER_POINTER (id);
33596 if (!strcmp (p, "seq_cst"))
33598 seq_cst = true;
33599 cp_lexer_consume_token (parser->lexer);
33600 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)
33601 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME)
33602 cp_lexer_consume_token (parser->lexer);
33605 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
33607 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
33608 const char *p = IDENTIFIER_POINTER (id);
33610 if (!strcmp (p, "read"))
33611 code = OMP_ATOMIC_READ;
33612 else if (!strcmp (p, "write"))
33613 code = NOP_EXPR;
33614 else if (!strcmp (p, "update"))
33615 code = OMP_ATOMIC;
33616 else if (!strcmp (p, "capture"))
33617 code = OMP_ATOMIC_CAPTURE_NEW;
33618 else
33619 p = NULL;
33620 if (p)
33621 cp_lexer_consume_token (parser->lexer);
33623 if (!seq_cst)
33625 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)
33626 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME)
33627 cp_lexer_consume_token (parser->lexer);
33629 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
33631 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
33632 const char *p = IDENTIFIER_POINTER (id);
33634 if (!strcmp (p, "seq_cst"))
33636 seq_cst = true;
33637 cp_lexer_consume_token (parser->lexer);
33641 cp_parser_require_pragma_eol (parser, pragma_tok);
33643 switch (code)
33645 case OMP_ATOMIC_READ:
33646 case NOP_EXPR: /* atomic write */
33647 v = cp_parser_unary_expression (parser);
33648 if (v == error_mark_node)
33649 goto saw_error;
33650 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
33651 goto saw_error;
33652 if (code == NOP_EXPR)
33653 lhs = cp_parser_expression (parser);
33654 else
33655 lhs = cp_parser_unary_expression (parser);
33656 if (lhs == error_mark_node)
33657 goto saw_error;
33658 if (code == NOP_EXPR)
33660 /* atomic write is represented by OMP_ATOMIC with NOP_EXPR
33661 opcode. */
33662 code = OMP_ATOMIC;
33663 rhs = lhs;
33664 lhs = v;
33665 v = NULL_TREE;
33667 goto done;
33668 case OMP_ATOMIC_CAPTURE_NEW:
33669 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
33671 cp_lexer_consume_token (parser->lexer);
33672 structured_block = true;
33674 else
33676 v = cp_parser_unary_expression (parser);
33677 if (v == error_mark_node)
33678 goto saw_error;
33679 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
33680 goto saw_error;
33682 default:
33683 break;
33686 restart:
33687 lhs = cp_parser_unary_expression (parser);
33688 orig_lhs = lhs;
33689 switch (TREE_CODE (lhs))
33691 case ERROR_MARK:
33692 goto saw_error;
33694 case POSTINCREMENT_EXPR:
33695 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
33696 code = OMP_ATOMIC_CAPTURE_OLD;
33697 /* FALLTHROUGH */
33698 case PREINCREMENT_EXPR:
33699 lhs = TREE_OPERAND (lhs, 0);
33700 opcode = PLUS_EXPR;
33701 rhs = integer_one_node;
33702 break;
33704 case POSTDECREMENT_EXPR:
33705 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
33706 code = OMP_ATOMIC_CAPTURE_OLD;
33707 /* FALLTHROUGH */
33708 case PREDECREMENT_EXPR:
33709 lhs = TREE_OPERAND (lhs, 0);
33710 opcode = MINUS_EXPR;
33711 rhs = integer_one_node;
33712 break;
33714 case COMPOUND_EXPR:
33715 if (TREE_CODE (TREE_OPERAND (lhs, 0)) == SAVE_EXPR
33716 && TREE_CODE (TREE_OPERAND (lhs, 1)) == COMPOUND_EXPR
33717 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (lhs, 1), 0)) == MODIFY_EXPR
33718 && TREE_OPERAND (TREE_OPERAND (lhs, 1), 1) == TREE_OPERAND (lhs, 0)
33719 && TREE_CODE (TREE_TYPE (TREE_OPERAND (TREE_OPERAND
33720 (TREE_OPERAND (lhs, 1), 0), 0)))
33721 == BOOLEAN_TYPE)
33722 /* Undo effects of boolean_increment for post {in,de}crement. */
33723 lhs = TREE_OPERAND (TREE_OPERAND (lhs, 1), 0);
33724 /* FALLTHRU */
33725 case MODIFY_EXPR:
33726 if (TREE_CODE (lhs) == MODIFY_EXPR
33727 && TREE_CODE (TREE_TYPE (TREE_OPERAND (lhs, 0))) == BOOLEAN_TYPE)
33729 /* Undo effects of boolean_increment. */
33730 if (integer_onep (TREE_OPERAND (lhs, 1)))
33732 /* This is pre or post increment. */
33733 rhs = TREE_OPERAND (lhs, 1);
33734 lhs = TREE_OPERAND (lhs, 0);
33735 opcode = NOP_EXPR;
33736 if (code == OMP_ATOMIC_CAPTURE_NEW
33737 && !structured_block
33738 && TREE_CODE (orig_lhs) == COMPOUND_EXPR)
33739 code = OMP_ATOMIC_CAPTURE_OLD;
33740 break;
33743 /* FALLTHRU */
33744 default:
33745 switch (cp_lexer_peek_token (parser->lexer)->type)
33747 case CPP_MULT_EQ:
33748 opcode = MULT_EXPR;
33749 break;
33750 case CPP_DIV_EQ:
33751 opcode = TRUNC_DIV_EXPR;
33752 break;
33753 case CPP_PLUS_EQ:
33754 opcode = PLUS_EXPR;
33755 break;
33756 case CPP_MINUS_EQ:
33757 opcode = MINUS_EXPR;
33758 break;
33759 case CPP_LSHIFT_EQ:
33760 opcode = LSHIFT_EXPR;
33761 break;
33762 case CPP_RSHIFT_EQ:
33763 opcode = RSHIFT_EXPR;
33764 break;
33765 case CPP_AND_EQ:
33766 opcode = BIT_AND_EXPR;
33767 break;
33768 case CPP_OR_EQ:
33769 opcode = BIT_IOR_EXPR;
33770 break;
33771 case CPP_XOR_EQ:
33772 opcode = BIT_XOR_EXPR;
33773 break;
33774 case CPP_EQ:
33775 enum cp_parser_prec oprec;
33776 cp_token *token;
33777 cp_lexer_consume_token (parser->lexer);
33778 cp_parser_parse_tentatively (parser);
33779 rhs1 = cp_parser_simple_cast_expression (parser);
33780 if (rhs1 == error_mark_node)
33782 cp_parser_abort_tentative_parse (parser);
33783 cp_parser_simple_cast_expression (parser);
33784 goto saw_error;
33786 token = cp_lexer_peek_token (parser->lexer);
33787 if (token->type != CPP_SEMICOLON && !cp_tree_equal (lhs, rhs1))
33789 cp_parser_abort_tentative_parse (parser);
33790 cp_parser_parse_tentatively (parser);
33791 rhs = cp_parser_binary_expression (parser, false, true,
33792 PREC_NOT_OPERATOR, NULL);
33793 if (rhs == error_mark_node)
33795 cp_parser_abort_tentative_parse (parser);
33796 cp_parser_binary_expression (parser, false, true,
33797 PREC_NOT_OPERATOR, NULL);
33798 goto saw_error;
33800 switch (TREE_CODE (rhs))
33802 case MULT_EXPR:
33803 case TRUNC_DIV_EXPR:
33804 case RDIV_EXPR:
33805 case PLUS_EXPR:
33806 case MINUS_EXPR:
33807 case LSHIFT_EXPR:
33808 case RSHIFT_EXPR:
33809 case BIT_AND_EXPR:
33810 case BIT_IOR_EXPR:
33811 case BIT_XOR_EXPR:
33812 if (cp_tree_equal (lhs, TREE_OPERAND (rhs, 1)))
33814 if (cp_parser_parse_definitely (parser))
33816 opcode = TREE_CODE (rhs);
33817 rhs1 = TREE_OPERAND (rhs, 0);
33818 rhs = TREE_OPERAND (rhs, 1);
33819 goto stmt_done;
33821 else
33822 goto saw_error;
33824 break;
33825 default:
33826 break;
33828 cp_parser_abort_tentative_parse (parser);
33829 if (structured_block && code == OMP_ATOMIC_CAPTURE_OLD)
33831 rhs = cp_parser_expression (parser);
33832 if (rhs == error_mark_node)
33833 goto saw_error;
33834 opcode = NOP_EXPR;
33835 rhs1 = NULL_TREE;
33836 goto stmt_done;
33838 cp_parser_error (parser,
33839 "invalid form of %<#pragma omp atomic%>");
33840 goto saw_error;
33842 if (!cp_parser_parse_definitely (parser))
33843 goto saw_error;
33844 switch (token->type)
33846 case CPP_SEMICOLON:
33847 if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
33849 code = OMP_ATOMIC_CAPTURE_OLD;
33850 v = lhs;
33851 lhs = NULL_TREE;
33852 lhs1 = rhs1;
33853 rhs1 = NULL_TREE;
33854 cp_lexer_consume_token (parser->lexer);
33855 goto restart;
33857 else if (structured_block)
33859 opcode = NOP_EXPR;
33860 rhs = rhs1;
33861 rhs1 = NULL_TREE;
33862 goto stmt_done;
33864 cp_parser_error (parser,
33865 "invalid form of %<#pragma omp atomic%>");
33866 goto saw_error;
33867 case CPP_MULT:
33868 opcode = MULT_EXPR;
33869 break;
33870 case CPP_DIV:
33871 opcode = TRUNC_DIV_EXPR;
33872 break;
33873 case CPP_PLUS:
33874 opcode = PLUS_EXPR;
33875 break;
33876 case CPP_MINUS:
33877 opcode = MINUS_EXPR;
33878 break;
33879 case CPP_LSHIFT:
33880 opcode = LSHIFT_EXPR;
33881 break;
33882 case CPP_RSHIFT:
33883 opcode = RSHIFT_EXPR;
33884 break;
33885 case CPP_AND:
33886 opcode = BIT_AND_EXPR;
33887 break;
33888 case CPP_OR:
33889 opcode = BIT_IOR_EXPR;
33890 break;
33891 case CPP_XOR:
33892 opcode = BIT_XOR_EXPR;
33893 break;
33894 default:
33895 cp_parser_error (parser,
33896 "invalid operator for %<#pragma omp atomic%>");
33897 goto saw_error;
33899 oprec = TOKEN_PRECEDENCE (token);
33900 gcc_assert (oprec != PREC_NOT_OPERATOR);
33901 if (commutative_tree_code (opcode))
33902 oprec = (enum cp_parser_prec) (oprec - 1);
33903 cp_lexer_consume_token (parser->lexer);
33904 rhs = cp_parser_binary_expression (parser, false, false,
33905 oprec, NULL);
33906 if (rhs == error_mark_node)
33907 goto saw_error;
33908 goto stmt_done;
33909 /* FALLTHROUGH */
33910 default:
33911 cp_parser_error (parser,
33912 "invalid operator for %<#pragma omp atomic%>");
33913 goto saw_error;
33915 cp_lexer_consume_token (parser->lexer);
33917 rhs = cp_parser_expression (parser);
33918 if (rhs == error_mark_node)
33919 goto saw_error;
33920 break;
33922 stmt_done:
33923 if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
33925 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
33926 goto saw_error;
33927 v = cp_parser_unary_expression (parser);
33928 if (v == error_mark_node)
33929 goto saw_error;
33930 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
33931 goto saw_error;
33932 lhs1 = cp_parser_unary_expression (parser);
33933 if (lhs1 == error_mark_node)
33934 goto saw_error;
33936 if (structured_block)
33938 cp_parser_consume_semicolon_at_end_of_statement (parser);
33939 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
33941 done:
33942 finish_omp_atomic (code, opcode, lhs, rhs, v, lhs1, rhs1, seq_cst);
33943 if (!structured_block)
33944 cp_parser_consume_semicolon_at_end_of_statement (parser);
33945 return;
33947 saw_error:
33948 cp_parser_skip_to_end_of_block_or_statement (parser);
33949 if (structured_block)
33951 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
33952 cp_lexer_consume_token (parser->lexer);
33953 else if (code == OMP_ATOMIC_CAPTURE_NEW)
33955 cp_parser_skip_to_end_of_block_or_statement (parser);
33956 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
33957 cp_lexer_consume_token (parser->lexer);
33963 /* OpenMP 2.5:
33964 # pragma omp barrier new-line */
33966 static void
33967 cp_parser_omp_barrier (cp_parser *parser, cp_token *pragma_tok)
33969 cp_parser_require_pragma_eol (parser, pragma_tok);
33970 finish_omp_barrier ();
33973 /* OpenMP 2.5:
33974 # pragma omp critical [(name)] new-line
33975 structured-block
33977 OpenMP 4.5:
33978 # pragma omp critical [(name) [hint(expression)]] new-line
33979 structured-block */
33981 #define OMP_CRITICAL_CLAUSE_MASK \
33982 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_HINT) )
33984 static tree
33985 cp_parser_omp_critical (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
33987 tree stmt, name = NULL_TREE, clauses = NULL_TREE;
33989 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
33991 cp_lexer_consume_token (parser->lexer);
33993 name = cp_parser_identifier (parser);
33995 if (name == error_mark_node
33996 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
33997 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33998 /*or_comma=*/false,
33999 /*consume_paren=*/true);
34000 if (name == error_mark_node)
34001 name = NULL;
34003 clauses = cp_parser_omp_all_clauses (parser,
34004 OMP_CRITICAL_CLAUSE_MASK,
34005 "#pragma omp critical", pragma_tok);
34007 else
34008 cp_parser_require_pragma_eol (parser, pragma_tok);
34010 stmt = cp_parser_omp_structured_block (parser, if_p);
34011 return c_finish_omp_critical (input_location, stmt, name, clauses);
34014 /* OpenMP 2.5:
34015 # pragma omp flush flush-vars[opt] new-line
34017 flush-vars:
34018 ( variable-list ) */
34020 static void
34021 cp_parser_omp_flush (cp_parser *parser, cp_token *pragma_tok)
34023 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
34024 (void) cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
34025 cp_parser_require_pragma_eol (parser, pragma_tok);
34027 finish_omp_flush ();
34030 /* Helper function, to parse omp for increment expression. */
34032 static tree
34033 cp_parser_omp_for_cond (cp_parser *parser, tree decl, enum tree_code code)
34035 tree cond = cp_parser_binary_expression (parser, false, true,
34036 PREC_NOT_OPERATOR, NULL);
34037 if (cond == error_mark_node
34038 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
34040 cp_parser_skip_to_end_of_statement (parser);
34041 return error_mark_node;
34044 switch (TREE_CODE (cond))
34046 case GT_EXPR:
34047 case GE_EXPR:
34048 case LT_EXPR:
34049 case LE_EXPR:
34050 break;
34051 case NE_EXPR:
34052 if (code == CILK_SIMD || code == CILK_FOR)
34053 break;
34054 /* Fall through: OpenMP disallows NE_EXPR. */
34055 gcc_fallthrough ();
34056 default:
34057 return error_mark_node;
34060 /* If decl is an iterator, preserve LHS and RHS of the relational
34061 expr until finish_omp_for. */
34062 if (decl
34063 && (type_dependent_expression_p (decl)
34064 || CLASS_TYPE_P (TREE_TYPE (decl))))
34065 return cond;
34067 return build_x_binary_op (EXPR_LOC_OR_LOC (cond, input_location),
34068 TREE_CODE (cond),
34069 TREE_OPERAND (cond, 0), ERROR_MARK,
34070 TREE_OPERAND (cond, 1), ERROR_MARK,
34071 /*overload=*/NULL, tf_warning_or_error);
34074 /* Helper function, to parse omp for increment expression. */
34076 static tree
34077 cp_parser_omp_for_incr (cp_parser *parser, tree decl)
34079 cp_token *token = cp_lexer_peek_token (parser->lexer);
34080 enum tree_code op;
34081 tree lhs, rhs;
34082 cp_id_kind idk;
34083 bool decl_first;
34085 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
34087 op = (token->type == CPP_PLUS_PLUS
34088 ? PREINCREMENT_EXPR : PREDECREMENT_EXPR);
34089 cp_lexer_consume_token (parser->lexer);
34090 lhs = cp_parser_simple_cast_expression (parser);
34091 if (lhs != decl
34092 && (!processing_template_decl || !cp_tree_equal (lhs, decl)))
34093 return error_mark_node;
34094 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
34097 lhs = cp_parser_primary_expression (parser, false, false, false, &idk);
34098 if (lhs != decl
34099 && (!processing_template_decl || !cp_tree_equal (lhs, decl)))
34100 return error_mark_node;
34102 token = cp_lexer_peek_token (parser->lexer);
34103 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
34105 op = (token->type == CPP_PLUS_PLUS
34106 ? POSTINCREMENT_EXPR : POSTDECREMENT_EXPR);
34107 cp_lexer_consume_token (parser->lexer);
34108 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
34111 op = cp_parser_assignment_operator_opt (parser);
34112 if (op == ERROR_MARK)
34113 return error_mark_node;
34115 if (op != NOP_EXPR)
34117 rhs = cp_parser_assignment_expression (parser);
34118 rhs = build2 (op, TREE_TYPE (decl), decl, rhs);
34119 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
34122 lhs = cp_parser_binary_expression (parser, false, false,
34123 PREC_ADDITIVE_EXPRESSION, NULL);
34124 token = cp_lexer_peek_token (parser->lexer);
34125 decl_first = (lhs == decl
34126 || (processing_template_decl && cp_tree_equal (lhs, decl)));
34127 if (decl_first)
34128 lhs = NULL_TREE;
34129 if (token->type != CPP_PLUS
34130 && token->type != CPP_MINUS)
34131 return error_mark_node;
34135 op = token->type == CPP_PLUS ? PLUS_EXPR : MINUS_EXPR;
34136 cp_lexer_consume_token (parser->lexer);
34137 rhs = cp_parser_binary_expression (parser, false, false,
34138 PREC_ADDITIVE_EXPRESSION, NULL);
34139 token = cp_lexer_peek_token (parser->lexer);
34140 if (token->type == CPP_PLUS || token->type == CPP_MINUS || decl_first)
34142 if (lhs == NULL_TREE)
34144 if (op == PLUS_EXPR)
34145 lhs = rhs;
34146 else
34147 lhs = build_x_unary_op (input_location, NEGATE_EXPR, rhs,
34148 tf_warning_or_error);
34150 else
34151 lhs = build_x_binary_op (input_location, op, lhs, ERROR_MARK, rhs,
34152 ERROR_MARK, NULL, tf_warning_or_error);
34155 while (token->type == CPP_PLUS || token->type == CPP_MINUS);
34157 if (!decl_first)
34159 if ((rhs != decl
34160 && (!processing_template_decl || !cp_tree_equal (rhs, decl)))
34161 || op == MINUS_EXPR)
34162 return error_mark_node;
34163 rhs = build2 (op, TREE_TYPE (decl), lhs, decl);
34165 else
34166 rhs = build2 (PLUS_EXPR, TREE_TYPE (decl), decl, lhs);
34168 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
34171 /* Parse the initialization statement of either an OpenMP for loop or
34172 a Cilk Plus for loop.
34174 Return true if the resulting construct should have an
34175 OMP_CLAUSE_PRIVATE added to it. */
34177 static tree
34178 cp_parser_omp_for_loop_init (cp_parser *parser,
34179 enum tree_code code,
34180 tree &this_pre_body,
34181 vec<tree, va_gc> *for_block,
34182 tree &init,
34183 tree &orig_init,
34184 tree &decl,
34185 tree &real_decl)
34187 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
34188 return NULL_TREE;
34190 tree add_private_clause = NULL_TREE;
34192 /* See 2.5.1 (in OpenMP 3.0, similar wording is in 2.5 standard too):
34194 init-expr:
34195 var = lb
34196 integer-type var = lb
34197 random-access-iterator-type var = lb
34198 pointer-type var = lb
34200 cp_decl_specifier_seq type_specifiers;
34202 /* First, try to parse as an initialized declaration. See
34203 cp_parser_condition, from whence the bulk of this is copied. */
34205 cp_parser_parse_tentatively (parser);
34206 cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
34207 /*is_trailing_return=*/false,
34208 &type_specifiers);
34209 if (cp_parser_parse_definitely (parser))
34211 /* If parsing a type specifier seq succeeded, then this
34212 MUST be a initialized declaration. */
34213 tree asm_specification, attributes;
34214 cp_declarator *declarator;
34216 declarator = cp_parser_declarator (parser,
34217 CP_PARSER_DECLARATOR_NAMED,
34218 /*ctor_dtor_or_conv_p=*/NULL,
34219 /*parenthesized_p=*/NULL,
34220 /*member_p=*/false,
34221 /*friend_p=*/false);
34222 attributes = cp_parser_attributes_opt (parser);
34223 asm_specification = cp_parser_asm_specification_opt (parser);
34225 if (declarator == cp_error_declarator)
34226 cp_parser_skip_to_end_of_statement (parser);
34228 else
34230 tree pushed_scope, auto_node;
34232 decl = start_decl (declarator, &type_specifiers,
34233 SD_INITIALIZED, attributes,
34234 /*prefix_attributes=*/NULL_TREE,
34235 &pushed_scope);
34237 auto_node = type_uses_auto (TREE_TYPE (decl));
34238 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
34240 if (cp_lexer_next_token_is (parser->lexer,
34241 CPP_OPEN_PAREN))
34243 if (code != CILK_SIMD && code != CILK_FOR)
34244 error ("parenthesized initialization is not allowed in "
34245 "OpenMP %<for%> loop");
34246 else
34247 error ("parenthesized initialization is "
34248 "not allowed in for-loop");
34250 else
34251 /* Trigger an error. */
34252 cp_parser_require (parser, CPP_EQ, RT_EQ);
34254 init = error_mark_node;
34255 cp_parser_skip_to_end_of_statement (parser);
34257 else if (CLASS_TYPE_P (TREE_TYPE (decl))
34258 || type_dependent_expression_p (decl)
34259 || auto_node)
34261 bool is_direct_init, is_non_constant_init;
34263 init = cp_parser_initializer (parser,
34264 &is_direct_init,
34265 &is_non_constant_init);
34267 if (auto_node)
34269 TREE_TYPE (decl)
34270 = do_auto_deduction (TREE_TYPE (decl), init,
34271 auto_node);
34273 if (!CLASS_TYPE_P (TREE_TYPE (decl))
34274 && !type_dependent_expression_p (decl))
34275 goto non_class;
34278 cp_finish_decl (decl, init, !is_non_constant_init,
34279 asm_specification,
34280 LOOKUP_ONLYCONVERTING);
34281 orig_init = init;
34282 if (CLASS_TYPE_P (TREE_TYPE (decl)))
34284 vec_safe_push (for_block, this_pre_body);
34285 init = NULL_TREE;
34287 else
34289 init = pop_stmt_list (this_pre_body);
34290 if (init && TREE_CODE (init) == STATEMENT_LIST)
34292 tree_stmt_iterator i = tsi_start (init);
34293 /* Move lambda DECL_EXPRs to FOR_BLOCK. */
34294 while (!tsi_end_p (i))
34296 tree t = tsi_stmt (i);
34297 if (TREE_CODE (t) == DECL_EXPR
34298 && TREE_CODE (DECL_EXPR_DECL (t)) == TYPE_DECL)
34300 tsi_delink (&i);
34301 vec_safe_push (for_block, t);
34302 continue;
34304 break;
34306 if (tsi_one_before_end_p (i))
34308 tree t = tsi_stmt (i);
34309 tsi_delink (&i);
34310 free_stmt_list (init);
34311 init = t;
34315 this_pre_body = NULL_TREE;
34317 else
34319 /* Consume '='. */
34320 cp_lexer_consume_token (parser->lexer);
34321 init = cp_parser_assignment_expression (parser);
34323 non_class:
34324 if (TREE_CODE (TREE_TYPE (decl)) == REFERENCE_TYPE)
34325 init = error_mark_node;
34326 else
34327 cp_finish_decl (decl, NULL_TREE,
34328 /*init_const_expr_p=*/false,
34329 asm_specification,
34330 LOOKUP_ONLYCONVERTING);
34333 if (pushed_scope)
34334 pop_scope (pushed_scope);
34337 else
34339 cp_id_kind idk;
34340 /* If parsing a type specifier sequence failed, then
34341 this MUST be a simple expression. */
34342 if (code == CILK_FOR)
34343 error ("%<_Cilk_for%> allows expression instead of declaration only "
34344 "in C, not in C++");
34345 cp_parser_parse_tentatively (parser);
34346 decl = cp_parser_primary_expression (parser, false, false,
34347 false, &idk);
34348 cp_token *last_tok = cp_lexer_peek_token (parser->lexer);
34349 if (!cp_parser_error_occurred (parser)
34350 && decl
34351 && (TREE_CODE (decl) == COMPONENT_REF
34352 || (TREE_CODE (decl) == SCOPE_REF && TREE_TYPE (decl))))
34354 cp_parser_abort_tentative_parse (parser);
34355 cp_parser_parse_tentatively (parser);
34356 cp_token *token = cp_lexer_peek_token (parser->lexer);
34357 tree name = cp_parser_id_expression (parser, /*template_p=*/false,
34358 /*check_dependency_p=*/true,
34359 /*template_p=*/NULL,
34360 /*declarator_p=*/false,
34361 /*optional_p=*/false);
34362 if (name != error_mark_node
34363 && last_tok == cp_lexer_peek_token (parser->lexer))
34365 decl = cp_parser_lookup_name_simple (parser, name,
34366 token->location);
34367 if (TREE_CODE (decl) == FIELD_DECL)
34368 add_private_clause = omp_privatize_field (decl, false);
34370 cp_parser_abort_tentative_parse (parser);
34371 cp_parser_parse_tentatively (parser);
34372 decl = cp_parser_primary_expression (parser, false, false,
34373 false, &idk);
34375 if (!cp_parser_error_occurred (parser)
34376 && decl
34377 && DECL_P (decl)
34378 && CLASS_TYPE_P (TREE_TYPE (decl)))
34380 tree rhs;
34382 cp_parser_parse_definitely (parser);
34383 cp_parser_require (parser, CPP_EQ, RT_EQ);
34384 rhs = cp_parser_assignment_expression (parser);
34385 orig_init = rhs;
34386 finish_expr_stmt (build_x_modify_expr (EXPR_LOCATION (rhs),
34387 decl, NOP_EXPR,
34388 rhs,
34389 tf_warning_or_error));
34390 if (!add_private_clause)
34391 add_private_clause = decl;
34393 else
34395 decl = NULL;
34396 cp_parser_abort_tentative_parse (parser);
34397 init = cp_parser_expression (parser);
34398 if (init)
34400 if (TREE_CODE (init) == MODIFY_EXPR
34401 || TREE_CODE (init) == MODOP_EXPR)
34402 real_decl = TREE_OPERAND (init, 0);
34406 return add_private_clause;
34409 /* Parse the restricted form of the for statement allowed by OpenMP. */
34411 static tree
34412 cp_parser_omp_for_loop (cp_parser *parser, enum tree_code code, tree clauses,
34413 tree *cclauses, bool *if_p)
34415 tree init, orig_init, cond, incr, body, decl, pre_body = NULL_TREE, ret;
34416 tree real_decl, initv, condv, incrv, declv;
34417 tree this_pre_body, cl, ordered_cl = NULL_TREE;
34418 location_t loc_first;
34419 bool collapse_err = false;
34420 int i, collapse = 1, ordered = 0, count, nbraces = 0;
34421 vec<tree, va_gc> *for_block = make_tree_vector ();
34422 auto_vec<tree, 4> orig_inits;
34423 bool tiling = false;
34425 for (cl = clauses; cl; cl = OMP_CLAUSE_CHAIN (cl))
34426 if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_COLLAPSE)
34427 collapse = tree_to_shwi (OMP_CLAUSE_COLLAPSE_EXPR (cl));
34428 else if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_TILE)
34430 tiling = true;
34431 collapse = list_length (OMP_CLAUSE_TILE_LIST (cl));
34433 else if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_ORDERED
34434 && OMP_CLAUSE_ORDERED_EXPR (cl))
34436 ordered_cl = cl;
34437 ordered = tree_to_shwi (OMP_CLAUSE_ORDERED_EXPR (cl));
34440 if (ordered && ordered < collapse)
34442 error_at (OMP_CLAUSE_LOCATION (ordered_cl),
34443 "%<ordered%> clause parameter is less than %<collapse%>");
34444 OMP_CLAUSE_ORDERED_EXPR (ordered_cl)
34445 = build_int_cst (NULL_TREE, collapse);
34446 ordered = collapse;
34448 if (ordered)
34450 for (tree *pc = &clauses; *pc; )
34451 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_LINEAR)
34453 error_at (OMP_CLAUSE_LOCATION (*pc),
34454 "%<linear%> clause may not be specified together "
34455 "with %<ordered%> clause with a parameter");
34456 *pc = OMP_CLAUSE_CHAIN (*pc);
34458 else
34459 pc = &OMP_CLAUSE_CHAIN (*pc);
34462 gcc_assert (tiling || (collapse >= 1 && ordered >= 0));
34463 count = ordered ? ordered : collapse;
34465 declv = make_tree_vec (count);
34466 initv = make_tree_vec (count);
34467 condv = make_tree_vec (count);
34468 incrv = make_tree_vec (count);
34470 loc_first = cp_lexer_peek_token (parser->lexer)->location;
34472 for (i = 0; i < count; i++)
34474 int bracecount = 0;
34475 tree add_private_clause = NULL_TREE;
34476 location_t loc;
34478 if (code != CILK_FOR
34479 && !cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
34481 if (!collapse_err)
34482 cp_parser_error (parser, "for statement expected");
34483 return NULL;
34485 if (code == CILK_FOR
34486 && !cp_lexer_next_token_is_keyword (parser->lexer, RID_CILK_FOR))
34488 if (!collapse_err)
34489 cp_parser_error (parser, "_Cilk_for statement expected");
34490 return NULL;
34492 loc = cp_lexer_consume_token (parser->lexer)->location;
34494 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
34495 return NULL;
34497 init = orig_init = decl = real_decl = NULL;
34498 this_pre_body = push_stmt_list ();
34500 add_private_clause
34501 = cp_parser_omp_for_loop_init (parser, code,
34502 this_pre_body, for_block,
34503 init, orig_init, decl, real_decl);
34505 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
34506 if (this_pre_body)
34508 this_pre_body = pop_stmt_list (this_pre_body);
34509 if (pre_body)
34511 tree t = pre_body;
34512 pre_body = push_stmt_list ();
34513 add_stmt (t);
34514 add_stmt (this_pre_body);
34515 pre_body = pop_stmt_list (pre_body);
34517 else
34518 pre_body = this_pre_body;
34521 if (decl)
34522 real_decl = decl;
34523 if (cclauses != NULL
34524 && cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL] != NULL
34525 && real_decl != NULL_TREE)
34527 tree *c;
34528 for (c = &cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL]; *c ; )
34529 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_FIRSTPRIVATE
34530 && OMP_CLAUSE_DECL (*c) == real_decl)
34532 error_at (loc, "iteration variable %qD"
34533 " should not be firstprivate", real_decl);
34534 *c = OMP_CLAUSE_CHAIN (*c);
34536 else if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_LASTPRIVATE
34537 && OMP_CLAUSE_DECL (*c) == real_decl)
34539 /* Move lastprivate (decl) clause to OMP_FOR_CLAUSES. */
34540 tree l = *c;
34541 *c = OMP_CLAUSE_CHAIN (*c);
34542 if (code == OMP_SIMD)
34544 OMP_CLAUSE_CHAIN (l) = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
34545 cclauses[C_OMP_CLAUSE_SPLIT_FOR] = l;
34547 else
34549 OMP_CLAUSE_CHAIN (l) = clauses;
34550 clauses = l;
34552 add_private_clause = NULL_TREE;
34554 else
34556 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_PRIVATE
34557 && OMP_CLAUSE_DECL (*c) == real_decl)
34558 add_private_clause = NULL_TREE;
34559 c = &OMP_CLAUSE_CHAIN (*c);
34563 if (add_private_clause)
34565 tree c;
34566 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
34568 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
34569 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
34570 && OMP_CLAUSE_DECL (c) == decl)
34571 break;
34572 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
34573 && OMP_CLAUSE_DECL (c) == decl)
34574 error_at (loc, "iteration variable %qD "
34575 "should not be firstprivate",
34576 decl);
34577 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
34578 && OMP_CLAUSE_DECL (c) == decl)
34579 error_at (loc, "iteration variable %qD should not be reduction",
34580 decl);
34582 if (c == NULL)
34584 if (code != OMP_SIMD)
34585 c = build_omp_clause (loc, OMP_CLAUSE_PRIVATE);
34586 else if (collapse == 1)
34587 c = build_omp_clause (loc, OMP_CLAUSE_LINEAR);
34588 else
34589 c = build_omp_clause (loc, OMP_CLAUSE_LASTPRIVATE);
34590 OMP_CLAUSE_DECL (c) = add_private_clause;
34591 c = finish_omp_clauses (c, C_ORT_OMP);
34592 if (c)
34594 OMP_CLAUSE_CHAIN (c) = clauses;
34595 clauses = c;
34596 /* For linear, signal that we need to fill up
34597 the so far unknown linear step. */
34598 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINEAR)
34599 OMP_CLAUSE_LINEAR_STEP (c) = NULL_TREE;
34604 cond = NULL;
34605 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
34606 cond = cp_parser_omp_for_cond (parser, decl, code);
34607 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
34609 incr = NULL;
34610 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
34612 /* If decl is an iterator, preserve the operator on decl
34613 until finish_omp_for. */
34614 if (real_decl
34615 && ((processing_template_decl
34616 && (TREE_TYPE (real_decl) == NULL_TREE
34617 || !POINTER_TYPE_P (TREE_TYPE (real_decl))))
34618 || CLASS_TYPE_P (TREE_TYPE (real_decl))))
34619 incr = cp_parser_omp_for_incr (parser, real_decl);
34620 else
34621 incr = cp_parser_expression (parser);
34622 if (!EXPR_HAS_LOCATION (incr))
34623 protected_set_expr_location (incr, input_location);
34626 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
34627 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
34628 /*or_comma=*/false,
34629 /*consume_paren=*/true);
34631 TREE_VEC_ELT (declv, i) = decl;
34632 TREE_VEC_ELT (initv, i) = init;
34633 TREE_VEC_ELT (condv, i) = cond;
34634 TREE_VEC_ELT (incrv, i) = incr;
34635 if (orig_init)
34637 orig_inits.safe_grow_cleared (i + 1);
34638 orig_inits[i] = orig_init;
34641 if (i == count - 1)
34642 break;
34644 /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
34645 in between the collapsed for loops to be still considered perfectly
34646 nested. Hopefully the final version clarifies this.
34647 For now handle (multiple) {'s and empty statements. */
34648 cp_parser_parse_tentatively (parser);
34649 for (;;)
34651 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
34652 break;
34653 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
34655 cp_lexer_consume_token (parser->lexer);
34656 bracecount++;
34658 else if (bracecount
34659 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
34660 cp_lexer_consume_token (parser->lexer);
34661 else
34663 loc = cp_lexer_peek_token (parser->lexer)->location;
34664 error_at (loc, "not enough for loops to collapse");
34665 collapse_err = true;
34666 cp_parser_abort_tentative_parse (parser);
34667 declv = NULL_TREE;
34668 break;
34672 if (declv)
34674 cp_parser_parse_definitely (parser);
34675 nbraces += bracecount;
34679 if (nbraces)
34680 if_p = NULL;
34682 /* Note that we saved the original contents of this flag when we entered
34683 the structured block, and so we don't need to re-save it here. */
34684 if (code == CILK_SIMD || code == CILK_FOR)
34685 parser->in_statement = IN_CILK_SIMD_FOR;
34686 else
34687 parser->in_statement = IN_OMP_FOR;
34689 /* Note that the grammar doesn't call for a structured block here,
34690 though the loop as a whole is a structured block. */
34691 body = push_stmt_list ();
34692 cp_parser_statement (parser, NULL_TREE, false, if_p);
34693 body = pop_stmt_list (body);
34695 if (declv == NULL_TREE)
34696 ret = NULL_TREE;
34697 else
34698 ret = finish_omp_for (loc_first, code, declv, NULL, initv, condv, incrv,
34699 body, pre_body, &orig_inits, clauses);
34701 while (nbraces)
34703 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
34705 cp_lexer_consume_token (parser->lexer);
34706 nbraces--;
34708 else if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
34709 cp_lexer_consume_token (parser->lexer);
34710 else
34712 if (!collapse_err)
34714 error_at (cp_lexer_peek_token (parser->lexer)->location,
34715 "collapsed loops not perfectly nested");
34717 collapse_err = true;
34718 cp_parser_statement_seq_opt (parser, NULL);
34719 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
34720 break;
34724 while (!for_block->is_empty ())
34726 tree t = for_block->pop ();
34727 if (TREE_CODE (t) == STATEMENT_LIST)
34728 add_stmt (pop_stmt_list (t));
34729 else
34730 add_stmt (t);
34732 release_tree_vector (for_block);
34734 return ret;
34737 /* Helper function for OpenMP parsing, split clauses and call
34738 finish_omp_clauses on each of the set of clauses afterwards. */
34740 static void
34741 cp_omp_split_clauses (location_t loc, enum tree_code code,
34742 omp_clause_mask mask, tree clauses, tree *cclauses)
34744 int i;
34745 c_omp_split_clauses (loc, code, mask, clauses, cclauses);
34746 for (i = 0; i < C_OMP_CLAUSE_SPLIT_COUNT; i++)
34747 if (cclauses[i])
34748 cclauses[i] = finish_omp_clauses (cclauses[i], C_ORT_OMP);
34751 /* OpenMP 4.0:
34752 #pragma omp simd simd-clause[optseq] new-line
34753 for-loop */
34755 #define OMP_SIMD_CLAUSE_MASK \
34756 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SAFELEN) \
34757 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN) \
34758 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
34759 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
34760 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
34761 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
34762 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
34763 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
34765 static tree
34766 cp_parser_omp_simd (cp_parser *parser, cp_token *pragma_tok,
34767 char *p_name, omp_clause_mask mask, tree *cclauses,
34768 bool *if_p)
34770 tree clauses, sb, ret;
34771 unsigned int save;
34772 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
34774 strcat (p_name, " simd");
34775 mask |= OMP_SIMD_CLAUSE_MASK;
34777 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
34778 cclauses == NULL);
34779 if (cclauses)
34781 cp_omp_split_clauses (loc, OMP_SIMD, mask, clauses, cclauses);
34782 clauses = cclauses[C_OMP_CLAUSE_SPLIT_SIMD];
34783 tree c = omp_find_clause (cclauses[C_OMP_CLAUSE_SPLIT_FOR],
34784 OMP_CLAUSE_ORDERED);
34785 if (c && OMP_CLAUSE_ORDERED_EXPR (c))
34787 error_at (OMP_CLAUSE_LOCATION (c),
34788 "%<ordered%> clause with parameter may not be specified "
34789 "on %qs construct", p_name);
34790 OMP_CLAUSE_ORDERED_EXPR (c) = NULL_TREE;
34794 sb = begin_omp_structured_block ();
34795 save = cp_parser_begin_omp_structured_block (parser);
34797 ret = cp_parser_omp_for_loop (parser, OMP_SIMD, clauses, cclauses, if_p);
34799 cp_parser_end_omp_structured_block (parser, save);
34800 add_stmt (finish_omp_structured_block (sb));
34802 return ret;
34805 /* OpenMP 2.5:
34806 #pragma omp for for-clause[optseq] new-line
34807 for-loop
34809 OpenMP 4.0:
34810 #pragma omp for simd for-simd-clause[optseq] new-line
34811 for-loop */
34813 #define OMP_FOR_CLAUSE_MASK \
34814 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
34815 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
34816 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
34817 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
34818 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
34819 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED) \
34820 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SCHEDULE) \
34821 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT) \
34822 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
34824 static tree
34825 cp_parser_omp_for (cp_parser *parser, cp_token *pragma_tok,
34826 char *p_name, omp_clause_mask mask, tree *cclauses,
34827 bool *if_p)
34829 tree clauses, sb, ret;
34830 unsigned int save;
34831 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
34833 strcat (p_name, " for");
34834 mask |= OMP_FOR_CLAUSE_MASK;
34835 /* parallel for{, simd} disallows nowait clause, but for
34836 target {teams distribute ,}parallel for{, simd} it should be accepted. */
34837 if (cclauses && (mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP)) == 0)
34838 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT);
34839 /* Composite distribute parallel for{, simd} disallows ordered clause. */
34840 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
34841 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED);
34843 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
34845 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
34846 const char *p = IDENTIFIER_POINTER (id);
34848 if (strcmp (p, "simd") == 0)
34850 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
34851 if (cclauses == NULL)
34852 cclauses = cclauses_buf;
34854 cp_lexer_consume_token (parser->lexer);
34855 if (!flag_openmp) /* flag_openmp_simd */
34856 return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
34857 cclauses, if_p);
34858 sb = begin_omp_structured_block ();
34859 save = cp_parser_begin_omp_structured_block (parser);
34860 ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
34861 cclauses, if_p);
34862 cp_parser_end_omp_structured_block (parser, save);
34863 tree body = finish_omp_structured_block (sb);
34864 if (ret == NULL)
34865 return ret;
34866 ret = make_node (OMP_FOR);
34867 TREE_TYPE (ret) = void_type_node;
34868 OMP_FOR_BODY (ret) = body;
34869 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
34870 SET_EXPR_LOCATION (ret, loc);
34871 add_stmt (ret);
34872 return ret;
34875 if (!flag_openmp) /* flag_openmp_simd */
34877 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
34878 return NULL_TREE;
34881 /* Composite distribute parallel for disallows linear clause. */
34882 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
34883 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR);
34885 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
34886 cclauses == NULL);
34887 if (cclauses)
34889 cp_omp_split_clauses (loc, OMP_FOR, mask, clauses, cclauses);
34890 clauses = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
34893 sb = begin_omp_structured_block ();
34894 save = cp_parser_begin_omp_structured_block (parser);
34896 ret = cp_parser_omp_for_loop (parser, OMP_FOR, clauses, cclauses, if_p);
34898 cp_parser_end_omp_structured_block (parser, save);
34899 add_stmt (finish_omp_structured_block (sb));
34901 return ret;
34904 /* OpenMP 2.5:
34905 # pragma omp master new-line
34906 structured-block */
34908 static tree
34909 cp_parser_omp_master (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
34911 cp_parser_require_pragma_eol (parser, pragma_tok);
34912 return c_finish_omp_master (input_location,
34913 cp_parser_omp_structured_block (parser, if_p));
34916 /* OpenMP 2.5:
34917 # pragma omp ordered new-line
34918 structured-block
34920 OpenMP 4.5:
34921 # pragma omp ordered ordered-clauses new-line
34922 structured-block */
34924 #define OMP_ORDERED_CLAUSE_MASK \
34925 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREADS) \
34926 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMD))
34928 #define OMP_ORDERED_DEPEND_CLAUSE_MASK \
34929 (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND)
34931 static bool
34932 cp_parser_omp_ordered (cp_parser *parser, cp_token *pragma_tok,
34933 enum pragma_context context, bool *if_p)
34935 location_t loc = pragma_tok->location;
34937 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
34939 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
34940 const char *p = IDENTIFIER_POINTER (id);
34942 if (strcmp (p, "depend") == 0)
34944 if (context == pragma_stmt)
34946 error_at (pragma_tok->location, "%<#pragma omp ordered%> with "
34947 "%<depend%> clause may only be used in compound "
34948 "statements");
34949 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
34950 return false;
34952 tree clauses
34953 = cp_parser_omp_all_clauses (parser,
34954 OMP_ORDERED_DEPEND_CLAUSE_MASK,
34955 "#pragma omp ordered", pragma_tok);
34956 c_finish_omp_ordered (loc, clauses, NULL_TREE);
34957 return false;
34961 tree clauses
34962 = cp_parser_omp_all_clauses (parser, OMP_ORDERED_CLAUSE_MASK,
34963 "#pragma omp ordered", pragma_tok);
34964 c_finish_omp_ordered (loc, clauses,
34965 cp_parser_omp_structured_block (parser, if_p));
34966 return true;
34969 /* OpenMP 2.5:
34971 section-scope:
34972 { section-sequence }
34974 section-sequence:
34975 section-directive[opt] structured-block
34976 section-sequence section-directive structured-block */
34978 static tree
34979 cp_parser_omp_sections_scope (cp_parser *parser)
34981 tree stmt, substmt;
34982 bool error_suppress = false;
34983 cp_token *tok;
34985 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
34986 return NULL_TREE;
34988 stmt = push_stmt_list ();
34990 if (cp_parser_pragma_kind (cp_lexer_peek_token (parser->lexer))
34991 != PRAGMA_OMP_SECTION)
34993 substmt = cp_parser_omp_structured_block (parser, NULL);
34994 substmt = build1 (OMP_SECTION, void_type_node, substmt);
34995 add_stmt (substmt);
34998 while (1)
35000 tok = cp_lexer_peek_token (parser->lexer);
35001 if (tok->type == CPP_CLOSE_BRACE)
35002 break;
35003 if (tok->type == CPP_EOF)
35004 break;
35006 if (cp_parser_pragma_kind (tok) == PRAGMA_OMP_SECTION)
35008 cp_lexer_consume_token (parser->lexer);
35009 cp_parser_require_pragma_eol (parser, tok);
35010 error_suppress = false;
35012 else if (!error_suppress)
35014 cp_parser_error (parser, "expected %<#pragma omp section%> or %<}%>");
35015 error_suppress = true;
35018 substmt = cp_parser_omp_structured_block (parser, NULL);
35019 substmt = build1 (OMP_SECTION, void_type_node, substmt);
35020 add_stmt (substmt);
35022 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
35024 substmt = pop_stmt_list (stmt);
35026 stmt = make_node (OMP_SECTIONS);
35027 TREE_TYPE (stmt) = void_type_node;
35028 OMP_SECTIONS_BODY (stmt) = substmt;
35030 add_stmt (stmt);
35031 return stmt;
35034 /* OpenMP 2.5:
35035 # pragma omp sections sections-clause[optseq] newline
35036 sections-scope */
35038 #define OMP_SECTIONS_CLAUSE_MASK \
35039 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35040 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35041 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
35042 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
35043 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
35045 static tree
35046 cp_parser_omp_sections (cp_parser *parser, cp_token *pragma_tok,
35047 char *p_name, omp_clause_mask mask, tree *cclauses)
35049 tree clauses, ret;
35050 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
35052 strcat (p_name, " sections");
35053 mask |= OMP_SECTIONS_CLAUSE_MASK;
35054 if (cclauses)
35055 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT);
35057 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
35058 cclauses == NULL);
35059 if (cclauses)
35061 cp_omp_split_clauses (loc, OMP_SECTIONS, mask, clauses, cclauses);
35062 clauses = cclauses[C_OMP_CLAUSE_SPLIT_SECTIONS];
35065 ret = cp_parser_omp_sections_scope (parser);
35066 if (ret)
35067 OMP_SECTIONS_CLAUSES (ret) = clauses;
35069 return ret;
35072 /* OpenMP 2.5:
35073 # pragma omp parallel parallel-clause[optseq] new-line
35074 structured-block
35075 # pragma omp parallel for parallel-for-clause[optseq] new-line
35076 structured-block
35077 # pragma omp parallel sections parallel-sections-clause[optseq] new-line
35078 structured-block
35080 OpenMP 4.0:
35081 # pragma omp parallel for simd parallel-for-simd-clause[optseq] new-line
35082 structured-block */
35084 #define OMP_PARALLEL_CLAUSE_MASK \
35085 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
35086 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35087 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35088 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
35089 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
35090 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYIN) \
35091 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
35092 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_THREADS) \
35093 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PROC_BIND))
35095 static tree
35096 cp_parser_omp_parallel (cp_parser *parser, cp_token *pragma_tok,
35097 char *p_name, omp_clause_mask mask, tree *cclauses,
35098 bool *if_p)
35100 tree stmt, clauses, block;
35101 unsigned int save;
35102 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
35104 strcat (p_name, " parallel");
35105 mask |= OMP_PARALLEL_CLAUSE_MASK;
35106 /* #pragma omp target parallel{, for, for simd} disallow copyin clause. */
35107 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP)) != 0
35108 && (mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) == 0)
35109 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYIN);
35111 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
35113 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
35114 if (cclauses == NULL)
35115 cclauses = cclauses_buf;
35117 cp_lexer_consume_token (parser->lexer);
35118 if (!flag_openmp) /* flag_openmp_simd */
35119 return cp_parser_omp_for (parser, pragma_tok, p_name, mask, cclauses,
35120 if_p);
35121 block = begin_omp_parallel ();
35122 save = cp_parser_begin_omp_structured_block (parser);
35123 tree ret = cp_parser_omp_for (parser, pragma_tok, p_name, mask, cclauses,
35124 if_p);
35125 cp_parser_end_omp_structured_block (parser, save);
35126 stmt = finish_omp_parallel (cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
35127 block);
35128 if (ret == NULL_TREE)
35129 return ret;
35130 OMP_PARALLEL_COMBINED (stmt) = 1;
35131 return stmt;
35133 /* When combined with distribute, parallel has to be followed by for.
35134 #pragma omp target parallel is allowed though. */
35135 else if (cclauses
35136 && (mask & (OMP_CLAUSE_MASK_1
35137 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
35139 error_at (loc, "expected %<for%> after %qs", p_name);
35140 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35141 return NULL_TREE;
35143 else if (!flag_openmp) /* flag_openmp_simd */
35145 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35146 return NULL_TREE;
35148 else if (cclauses == NULL && cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35150 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
35151 const char *p = IDENTIFIER_POINTER (id);
35152 if (strcmp (p, "sections") == 0)
35154 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
35155 cclauses = cclauses_buf;
35157 cp_lexer_consume_token (parser->lexer);
35158 block = begin_omp_parallel ();
35159 save = cp_parser_begin_omp_structured_block (parser);
35160 cp_parser_omp_sections (parser, pragma_tok, p_name, mask, cclauses);
35161 cp_parser_end_omp_structured_block (parser, save);
35162 stmt = finish_omp_parallel (cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
35163 block);
35164 OMP_PARALLEL_COMBINED (stmt) = 1;
35165 return stmt;
35169 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
35170 cclauses == NULL);
35171 if (cclauses)
35173 cp_omp_split_clauses (loc, OMP_PARALLEL, mask, clauses, cclauses);
35174 clauses = cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL];
35177 block = begin_omp_parallel ();
35178 save = cp_parser_begin_omp_structured_block (parser);
35179 cp_parser_statement (parser, NULL_TREE, false, if_p);
35180 cp_parser_end_omp_structured_block (parser, save);
35181 stmt = finish_omp_parallel (clauses, block);
35182 return stmt;
35185 /* OpenMP 2.5:
35186 # pragma omp single single-clause[optseq] new-line
35187 structured-block */
35189 #define OMP_SINGLE_CLAUSE_MASK \
35190 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35191 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35192 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \
35193 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
35195 static tree
35196 cp_parser_omp_single (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
35198 tree stmt = make_node (OMP_SINGLE);
35199 TREE_TYPE (stmt) = void_type_node;
35201 OMP_SINGLE_CLAUSES (stmt)
35202 = cp_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
35203 "#pragma omp single", pragma_tok);
35204 OMP_SINGLE_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
35206 return add_stmt (stmt);
35209 /* OpenMP 3.0:
35210 # pragma omp task task-clause[optseq] new-line
35211 structured-block */
35213 #define OMP_TASK_CLAUSE_MASK \
35214 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
35215 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED) \
35216 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
35217 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35218 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35219 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
35220 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL) \
35221 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE) \
35222 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
35223 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIORITY))
35225 static tree
35226 cp_parser_omp_task (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
35228 tree clauses, block;
35229 unsigned int save;
35231 clauses = cp_parser_omp_all_clauses (parser, OMP_TASK_CLAUSE_MASK,
35232 "#pragma omp task", pragma_tok);
35233 block = begin_omp_task ();
35234 save = cp_parser_begin_omp_structured_block (parser);
35235 cp_parser_statement (parser, NULL_TREE, false, if_p);
35236 cp_parser_end_omp_structured_block (parser, save);
35237 return finish_omp_task (clauses, block);
35240 /* OpenMP 3.0:
35241 # pragma omp taskwait new-line */
35243 static void
35244 cp_parser_omp_taskwait (cp_parser *parser, cp_token *pragma_tok)
35246 cp_parser_require_pragma_eol (parser, pragma_tok);
35247 finish_omp_taskwait ();
35250 /* OpenMP 3.1:
35251 # pragma omp taskyield new-line */
35253 static void
35254 cp_parser_omp_taskyield (cp_parser *parser, cp_token *pragma_tok)
35256 cp_parser_require_pragma_eol (parser, pragma_tok);
35257 finish_omp_taskyield ();
35260 /* OpenMP 4.0:
35261 # pragma omp taskgroup new-line
35262 structured-block */
35264 static tree
35265 cp_parser_omp_taskgroup (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
35267 cp_parser_require_pragma_eol (parser, pragma_tok);
35268 return c_finish_omp_taskgroup (input_location,
35269 cp_parser_omp_structured_block (parser,
35270 if_p));
35274 /* OpenMP 2.5:
35275 # pragma omp threadprivate (variable-list) */
35277 static void
35278 cp_parser_omp_threadprivate (cp_parser *parser, cp_token *pragma_tok)
35280 tree vars;
35282 vars = cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
35283 cp_parser_require_pragma_eol (parser, pragma_tok);
35285 finish_omp_threadprivate (vars);
35288 /* OpenMP 4.0:
35289 # pragma omp cancel cancel-clause[optseq] new-line */
35291 #define OMP_CANCEL_CLAUSE_MASK \
35292 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
35293 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
35294 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
35295 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP) \
35296 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
35298 static void
35299 cp_parser_omp_cancel (cp_parser *parser, cp_token *pragma_tok)
35301 tree clauses = cp_parser_omp_all_clauses (parser, OMP_CANCEL_CLAUSE_MASK,
35302 "#pragma omp cancel", pragma_tok);
35303 finish_omp_cancel (clauses);
35306 /* OpenMP 4.0:
35307 # pragma omp cancellation point cancelpt-clause[optseq] new-line */
35309 #define OMP_CANCELLATION_POINT_CLAUSE_MASK \
35310 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
35311 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
35312 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
35313 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP))
35315 static void
35316 cp_parser_omp_cancellation_point (cp_parser *parser, cp_token *pragma_tok,
35317 enum pragma_context context)
35319 tree clauses;
35320 bool point_seen = false;
35322 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35324 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
35325 const char *p = IDENTIFIER_POINTER (id);
35327 if (strcmp (p, "point") == 0)
35329 cp_lexer_consume_token (parser->lexer);
35330 point_seen = true;
35333 if (!point_seen)
35335 cp_parser_error (parser, "expected %<point%>");
35336 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35337 return;
35340 if (context != pragma_compound)
35342 if (context == pragma_stmt)
35343 error_at (pragma_tok->location,
35344 "%<#pragma omp cancellation point%> may only be used in"
35345 " compound statements");
35346 else
35347 cp_parser_error (parser, "expected declaration specifiers");
35348 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35349 return;
35352 clauses = cp_parser_omp_all_clauses (parser,
35353 OMP_CANCELLATION_POINT_CLAUSE_MASK,
35354 "#pragma omp cancellation point",
35355 pragma_tok);
35356 finish_omp_cancellation_point (clauses);
35359 /* OpenMP 4.0:
35360 #pragma omp distribute distribute-clause[optseq] new-line
35361 for-loop */
35363 #define OMP_DISTRIBUTE_CLAUSE_MASK \
35364 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35365 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35366 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
35367 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)\
35368 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
35370 static tree
35371 cp_parser_omp_distribute (cp_parser *parser, cp_token *pragma_tok,
35372 char *p_name, omp_clause_mask mask, tree *cclauses,
35373 bool *if_p)
35375 tree clauses, sb, ret;
35376 unsigned int save;
35377 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
35379 strcat (p_name, " distribute");
35380 mask |= OMP_DISTRIBUTE_CLAUSE_MASK;
35382 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35384 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
35385 const char *p = IDENTIFIER_POINTER (id);
35386 bool simd = false;
35387 bool parallel = false;
35389 if (strcmp (p, "simd") == 0)
35390 simd = true;
35391 else
35392 parallel = strcmp (p, "parallel") == 0;
35393 if (parallel || simd)
35395 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
35396 if (cclauses == NULL)
35397 cclauses = cclauses_buf;
35398 cp_lexer_consume_token (parser->lexer);
35399 if (!flag_openmp) /* flag_openmp_simd */
35401 if (simd)
35402 return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
35403 cclauses, if_p);
35404 else
35405 return cp_parser_omp_parallel (parser, pragma_tok, p_name, mask,
35406 cclauses, if_p);
35408 sb = begin_omp_structured_block ();
35409 save = cp_parser_begin_omp_structured_block (parser);
35410 if (simd)
35411 ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
35412 cclauses, if_p);
35413 else
35414 ret = cp_parser_omp_parallel (parser, pragma_tok, p_name, mask,
35415 cclauses, if_p);
35416 cp_parser_end_omp_structured_block (parser, save);
35417 tree body = finish_omp_structured_block (sb);
35418 if (ret == NULL)
35419 return ret;
35420 ret = make_node (OMP_DISTRIBUTE);
35421 TREE_TYPE (ret) = void_type_node;
35422 OMP_FOR_BODY (ret) = body;
35423 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_DISTRIBUTE];
35424 SET_EXPR_LOCATION (ret, loc);
35425 add_stmt (ret);
35426 return ret;
35429 if (!flag_openmp) /* flag_openmp_simd */
35431 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35432 return NULL_TREE;
35435 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
35436 cclauses == NULL);
35437 if (cclauses)
35439 cp_omp_split_clauses (loc, OMP_DISTRIBUTE, mask, clauses, cclauses);
35440 clauses = cclauses[C_OMP_CLAUSE_SPLIT_DISTRIBUTE];
35443 sb = begin_omp_structured_block ();
35444 save = cp_parser_begin_omp_structured_block (parser);
35446 ret = cp_parser_omp_for_loop (parser, OMP_DISTRIBUTE, clauses, NULL, if_p);
35448 cp_parser_end_omp_structured_block (parser, save);
35449 add_stmt (finish_omp_structured_block (sb));
35451 return ret;
35454 /* OpenMP 4.0:
35455 # pragma omp teams teams-clause[optseq] new-line
35456 structured-block */
35458 #define OMP_TEAMS_CLAUSE_MASK \
35459 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35460 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35461 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
35462 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
35463 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TEAMS) \
35464 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREAD_LIMIT) \
35465 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT))
35467 static tree
35468 cp_parser_omp_teams (cp_parser *parser, cp_token *pragma_tok,
35469 char *p_name, omp_clause_mask mask, tree *cclauses,
35470 bool *if_p)
35472 tree clauses, sb, ret;
35473 unsigned int save;
35474 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
35476 strcat (p_name, " teams");
35477 mask |= OMP_TEAMS_CLAUSE_MASK;
35479 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35481 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
35482 const char *p = IDENTIFIER_POINTER (id);
35483 if (strcmp (p, "distribute") == 0)
35485 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
35486 if (cclauses == NULL)
35487 cclauses = cclauses_buf;
35489 cp_lexer_consume_token (parser->lexer);
35490 if (!flag_openmp) /* flag_openmp_simd */
35491 return cp_parser_omp_distribute (parser, pragma_tok, p_name, mask,
35492 cclauses, if_p);
35493 sb = begin_omp_structured_block ();
35494 save = cp_parser_begin_omp_structured_block (parser);
35495 ret = cp_parser_omp_distribute (parser, pragma_tok, p_name, mask,
35496 cclauses, if_p);
35497 cp_parser_end_omp_structured_block (parser, save);
35498 tree body = finish_omp_structured_block (sb);
35499 if (ret == NULL)
35500 return ret;
35501 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
35502 ret = make_node (OMP_TEAMS);
35503 TREE_TYPE (ret) = void_type_node;
35504 OMP_TEAMS_CLAUSES (ret) = clauses;
35505 OMP_TEAMS_BODY (ret) = body;
35506 OMP_TEAMS_COMBINED (ret) = 1;
35507 SET_EXPR_LOCATION (ret, loc);
35508 return add_stmt (ret);
35511 if (!flag_openmp) /* flag_openmp_simd */
35513 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35514 return NULL_TREE;
35517 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
35518 cclauses == NULL);
35519 if (cclauses)
35521 cp_omp_split_clauses (loc, OMP_TEAMS, mask, clauses, cclauses);
35522 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
35525 tree stmt = make_node (OMP_TEAMS);
35526 TREE_TYPE (stmt) = void_type_node;
35527 OMP_TEAMS_CLAUSES (stmt) = clauses;
35528 OMP_TEAMS_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
35529 SET_EXPR_LOCATION (stmt, loc);
35531 return add_stmt (stmt);
35534 /* OpenMP 4.0:
35535 # pragma omp target data target-data-clause[optseq] new-line
35536 structured-block */
35538 #define OMP_TARGET_DATA_CLAUSE_MASK \
35539 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
35540 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
35541 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
35542 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR))
35544 static tree
35545 cp_parser_omp_target_data (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
35547 tree clauses
35548 = cp_parser_omp_all_clauses (parser, OMP_TARGET_DATA_CLAUSE_MASK,
35549 "#pragma omp target data", pragma_tok);
35550 int map_seen = 0;
35551 for (tree *pc = &clauses; *pc;)
35553 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
35554 switch (OMP_CLAUSE_MAP_KIND (*pc))
35556 case GOMP_MAP_TO:
35557 case GOMP_MAP_ALWAYS_TO:
35558 case GOMP_MAP_FROM:
35559 case GOMP_MAP_ALWAYS_FROM:
35560 case GOMP_MAP_TOFROM:
35561 case GOMP_MAP_ALWAYS_TOFROM:
35562 case GOMP_MAP_ALLOC:
35563 map_seen = 3;
35564 break;
35565 case GOMP_MAP_FIRSTPRIVATE_POINTER:
35566 case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
35567 case GOMP_MAP_ALWAYS_POINTER:
35568 break;
35569 default:
35570 map_seen |= 1;
35571 error_at (OMP_CLAUSE_LOCATION (*pc),
35572 "%<#pragma omp target data%> with map-type other "
35573 "than %<to%>, %<from%>, %<tofrom%> or %<alloc%> "
35574 "on %<map%> clause");
35575 *pc = OMP_CLAUSE_CHAIN (*pc);
35576 continue;
35578 pc = &OMP_CLAUSE_CHAIN (*pc);
35581 if (map_seen != 3)
35583 if (map_seen == 0)
35584 error_at (pragma_tok->location,
35585 "%<#pragma omp target data%> must contain at least "
35586 "one %<map%> clause");
35587 return NULL_TREE;
35590 tree stmt = make_node (OMP_TARGET_DATA);
35591 TREE_TYPE (stmt) = void_type_node;
35592 OMP_TARGET_DATA_CLAUSES (stmt) = clauses;
35594 keep_next_level (true);
35595 OMP_TARGET_DATA_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
35597 SET_EXPR_LOCATION (stmt, pragma_tok->location);
35598 return add_stmt (stmt);
35601 /* OpenMP 4.5:
35602 # pragma omp target enter data target-enter-data-clause[optseq] new-line
35603 structured-block */
35605 #define OMP_TARGET_ENTER_DATA_CLAUSE_MASK \
35606 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
35607 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
35608 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
35609 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
35610 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
35612 static tree
35613 cp_parser_omp_target_enter_data (cp_parser *parser, cp_token *pragma_tok,
35614 enum pragma_context context)
35616 bool data_seen = false;
35617 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35619 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
35620 const char *p = IDENTIFIER_POINTER (id);
35622 if (strcmp (p, "data") == 0)
35624 cp_lexer_consume_token (parser->lexer);
35625 data_seen = true;
35628 if (!data_seen)
35630 cp_parser_error (parser, "expected %<data%>");
35631 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35632 return NULL_TREE;
35635 if (context == pragma_stmt)
35637 error_at (pragma_tok->location,
35638 "%<#pragma omp target enter data%> may only be "
35639 "used in compound statements");
35640 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35641 return NULL_TREE;
35644 tree clauses
35645 = cp_parser_omp_all_clauses (parser, OMP_TARGET_ENTER_DATA_CLAUSE_MASK,
35646 "#pragma omp target enter data", pragma_tok);
35647 int map_seen = 0;
35648 for (tree *pc = &clauses; *pc;)
35650 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
35651 switch (OMP_CLAUSE_MAP_KIND (*pc))
35653 case GOMP_MAP_TO:
35654 case GOMP_MAP_ALWAYS_TO:
35655 case GOMP_MAP_ALLOC:
35656 map_seen = 3;
35657 break;
35658 case GOMP_MAP_FIRSTPRIVATE_POINTER:
35659 case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
35660 case GOMP_MAP_ALWAYS_POINTER:
35661 break;
35662 default:
35663 map_seen |= 1;
35664 error_at (OMP_CLAUSE_LOCATION (*pc),
35665 "%<#pragma omp target enter data%> with map-type other "
35666 "than %<to%> or %<alloc%> on %<map%> clause");
35667 *pc = OMP_CLAUSE_CHAIN (*pc);
35668 continue;
35670 pc = &OMP_CLAUSE_CHAIN (*pc);
35673 if (map_seen != 3)
35675 if (map_seen == 0)
35676 error_at (pragma_tok->location,
35677 "%<#pragma omp target enter data%> must contain at least "
35678 "one %<map%> clause");
35679 return NULL_TREE;
35682 tree stmt = make_node (OMP_TARGET_ENTER_DATA);
35683 TREE_TYPE (stmt) = void_type_node;
35684 OMP_TARGET_ENTER_DATA_CLAUSES (stmt) = clauses;
35685 SET_EXPR_LOCATION (stmt, pragma_tok->location);
35686 return add_stmt (stmt);
35689 /* OpenMP 4.5:
35690 # pragma omp target exit data target-enter-data-clause[optseq] new-line
35691 structured-block */
35693 #define OMP_TARGET_EXIT_DATA_CLAUSE_MASK \
35694 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
35695 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
35696 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
35697 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
35698 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
35700 static tree
35701 cp_parser_omp_target_exit_data (cp_parser *parser, cp_token *pragma_tok,
35702 enum pragma_context context)
35704 bool data_seen = false;
35705 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35707 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
35708 const char *p = IDENTIFIER_POINTER (id);
35710 if (strcmp (p, "data") == 0)
35712 cp_lexer_consume_token (parser->lexer);
35713 data_seen = true;
35716 if (!data_seen)
35718 cp_parser_error (parser, "expected %<data%>");
35719 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35720 return NULL_TREE;
35723 if (context == pragma_stmt)
35725 error_at (pragma_tok->location,
35726 "%<#pragma omp target exit data%> may only be "
35727 "used in compound statements");
35728 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35729 return NULL_TREE;
35732 tree clauses
35733 = cp_parser_omp_all_clauses (parser, OMP_TARGET_EXIT_DATA_CLAUSE_MASK,
35734 "#pragma omp target exit data", pragma_tok);
35735 int map_seen = 0;
35736 for (tree *pc = &clauses; *pc;)
35738 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
35739 switch (OMP_CLAUSE_MAP_KIND (*pc))
35741 case GOMP_MAP_FROM:
35742 case GOMP_MAP_ALWAYS_FROM:
35743 case GOMP_MAP_RELEASE:
35744 case GOMP_MAP_DELETE:
35745 map_seen = 3;
35746 break;
35747 case GOMP_MAP_FIRSTPRIVATE_POINTER:
35748 case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
35749 case GOMP_MAP_ALWAYS_POINTER:
35750 break;
35751 default:
35752 map_seen |= 1;
35753 error_at (OMP_CLAUSE_LOCATION (*pc),
35754 "%<#pragma omp target exit data%> with map-type other "
35755 "than %<from%>, %<release%> or %<delete%> on %<map%>"
35756 " clause");
35757 *pc = OMP_CLAUSE_CHAIN (*pc);
35758 continue;
35760 pc = &OMP_CLAUSE_CHAIN (*pc);
35763 if (map_seen != 3)
35765 if (map_seen == 0)
35766 error_at (pragma_tok->location,
35767 "%<#pragma omp target exit data%> must contain at least "
35768 "one %<map%> clause");
35769 return NULL_TREE;
35772 tree stmt = make_node (OMP_TARGET_EXIT_DATA);
35773 TREE_TYPE (stmt) = void_type_node;
35774 OMP_TARGET_EXIT_DATA_CLAUSES (stmt) = clauses;
35775 SET_EXPR_LOCATION (stmt, pragma_tok->location);
35776 return add_stmt (stmt);
35779 /* OpenMP 4.0:
35780 # pragma omp target update target-update-clause[optseq] new-line */
35782 #define OMP_TARGET_UPDATE_CLAUSE_MASK \
35783 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FROM) \
35784 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO) \
35785 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
35786 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
35787 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
35788 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
35790 static bool
35791 cp_parser_omp_target_update (cp_parser *parser, cp_token *pragma_tok,
35792 enum pragma_context context)
35794 if (context == pragma_stmt)
35796 error_at (pragma_tok->location,
35797 "%<#pragma omp target update%> may only be "
35798 "used in compound statements");
35799 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35800 return false;
35803 tree clauses
35804 = cp_parser_omp_all_clauses (parser, OMP_TARGET_UPDATE_CLAUSE_MASK,
35805 "#pragma omp target update", pragma_tok);
35806 if (omp_find_clause (clauses, OMP_CLAUSE_TO) == NULL_TREE
35807 && omp_find_clause (clauses, OMP_CLAUSE_FROM) == NULL_TREE)
35809 error_at (pragma_tok->location,
35810 "%<#pragma omp target update%> must contain at least one "
35811 "%<from%> or %<to%> clauses");
35812 return false;
35815 tree stmt = make_node (OMP_TARGET_UPDATE);
35816 TREE_TYPE (stmt) = void_type_node;
35817 OMP_TARGET_UPDATE_CLAUSES (stmt) = clauses;
35818 SET_EXPR_LOCATION (stmt, pragma_tok->location);
35819 add_stmt (stmt);
35820 return false;
35823 /* OpenMP 4.0:
35824 # pragma omp target target-clause[optseq] new-line
35825 structured-block */
35827 #define OMP_TARGET_CLAUSE_MASK \
35828 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
35829 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
35830 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
35831 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
35832 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT) \
35833 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35834 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35835 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULTMAP) \
35836 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR))
35838 static bool
35839 cp_parser_omp_target (cp_parser *parser, cp_token *pragma_tok,
35840 enum pragma_context context, bool *if_p)
35842 tree *pc = NULL, stmt;
35844 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35846 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
35847 const char *p = IDENTIFIER_POINTER (id);
35848 enum tree_code ccode = ERROR_MARK;
35850 if (strcmp (p, "teams") == 0)
35851 ccode = OMP_TEAMS;
35852 else if (strcmp (p, "parallel") == 0)
35853 ccode = OMP_PARALLEL;
35854 else if (strcmp (p, "simd") == 0)
35855 ccode = OMP_SIMD;
35856 if (ccode != ERROR_MARK)
35858 tree cclauses[C_OMP_CLAUSE_SPLIT_COUNT];
35859 char p_name[sizeof ("#pragma omp target teams distribute "
35860 "parallel for simd")];
35862 cp_lexer_consume_token (parser->lexer);
35863 strcpy (p_name, "#pragma omp target");
35864 if (!flag_openmp) /* flag_openmp_simd */
35866 tree stmt;
35867 switch (ccode)
35869 case OMP_TEAMS:
35870 stmt = cp_parser_omp_teams (parser, pragma_tok, p_name,
35871 OMP_TARGET_CLAUSE_MASK,
35872 cclauses, if_p);
35873 break;
35874 case OMP_PARALLEL:
35875 stmt = cp_parser_omp_parallel (parser, pragma_tok, p_name,
35876 OMP_TARGET_CLAUSE_MASK,
35877 cclauses, if_p);
35878 break;
35879 case OMP_SIMD:
35880 stmt = cp_parser_omp_simd (parser, pragma_tok, p_name,
35881 OMP_TARGET_CLAUSE_MASK,
35882 cclauses, if_p);
35883 break;
35884 default:
35885 gcc_unreachable ();
35887 return stmt != NULL_TREE;
35889 keep_next_level (true);
35890 tree sb = begin_omp_structured_block (), ret;
35891 unsigned save = cp_parser_begin_omp_structured_block (parser);
35892 switch (ccode)
35894 case OMP_TEAMS:
35895 ret = cp_parser_omp_teams (parser, pragma_tok, p_name,
35896 OMP_TARGET_CLAUSE_MASK, cclauses,
35897 if_p);
35898 break;
35899 case OMP_PARALLEL:
35900 ret = cp_parser_omp_parallel (parser, pragma_tok, p_name,
35901 OMP_TARGET_CLAUSE_MASK, cclauses,
35902 if_p);
35903 break;
35904 case OMP_SIMD:
35905 ret = cp_parser_omp_simd (parser, pragma_tok, p_name,
35906 OMP_TARGET_CLAUSE_MASK, cclauses,
35907 if_p);
35908 break;
35909 default:
35910 gcc_unreachable ();
35912 cp_parser_end_omp_structured_block (parser, save);
35913 tree body = finish_omp_structured_block (sb);
35914 if (ret == NULL_TREE)
35915 return false;
35916 if (ccode == OMP_TEAMS && !processing_template_decl)
35918 /* For combined target teams, ensure the num_teams and
35919 thread_limit clause expressions are evaluated on the host,
35920 before entering the target construct. */
35921 tree c;
35922 for (c = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
35923 c; c = OMP_CLAUSE_CHAIN (c))
35924 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_NUM_TEAMS
35925 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_THREAD_LIMIT)
35926 && TREE_CODE (OMP_CLAUSE_OPERAND (c, 0)) != INTEGER_CST)
35928 tree expr = OMP_CLAUSE_OPERAND (c, 0);
35929 expr = force_target_expr (TREE_TYPE (expr), expr, tf_none);
35930 if (expr == error_mark_node)
35931 continue;
35932 tree tmp = TARGET_EXPR_SLOT (expr);
35933 add_stmt (expr);
35934 OMP_CLAUSE_OPERAND (c, 0) = expr;
35935 tree tc = build_omp_clause (OMP_CLAUSE_LOCATION (c),
35936 OMP_CLAUSE_FIRSTPRIVATE);
35937 OMP_CLAUSE_DECL (tc) = tmp;
35938 OMP_CLAUSE_CHAIN (tc)
35939 = cclauses[C_OMP_CLAUSE_SPLIT_TARGET];
35940 cclauses[C_OMP_CLAUSE_SPLIT_TARGET] = tc;
35943 tree stmt = make_node (OMP_TARGET);
35944 TREE_TYPE (stmt) = void_type_node;
35945 OMP_TARGET_CLAUSES (stmt) = cclauses[C_OMP_CLAUSE_SPLIT_TARGET];
35946 OMP_TARGET_BODY (stmt) = body;
35947 OMP_TARGET_COMBINED (stmt) = 1;
35948 SET_EXPR_LOCATION (stmt, pragma_tok->location);
35949 add_stmt (stmt);
35950 pc = &OMP_TARGET_CLAUSES (stmt);
35951 goto check_clauses;
35953 else if (!flag_openmp) /* flag_openmp_simd */
35955 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35956 return false;
35958 else if (strcmp (p, "data") == 0)
35960 cp_lexer_consume_token (parser->lexer);
35961 cp_parser_omp_target_data (parser, pragma_tok, if_p);
35962 return true;
35964 else if (strcmp (p, "enter") == 0)
35966 cp_lexer_consume_token (parser->lexer);
35967 cp_parser_omp_target_enter_data (parser, pragma_tok, context);
35968 return false;
35970 else if (strcmp (p, "exit") == 0)
35972 cp_lexer_consume_token (parser->lexer);
35973 cp_parser_omp_target_exit_data (parser, pragma_tok, context);
35974 return false;
35976 else if (strcmp (p, "update") == 0)
35978 cp_lexer_consume_token (parser->lexer);
35979 return cp_parser_omp_target_update (parser, pragma_tok, context);
35982 if (!flag_openmp) /* flag_openmp_simd */
35984 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35985 return false;
35988 stmt = make_node (OMP_TARGET);
35989 TREE_TYPE (stmt) = void_type_node;
35991 OMP_TARGET_CLAUSES (stmt)
35992 = cp_parser_omp_all_clauses (parser, OMP_TARGET_CLAUSE_MASK,
35993 "#pragma omp target", pragma_tok);
35994 pc = &OMP_TARGET_CLAUSES (stmt);
35995 keep_next_level (true);
35996 OMP_TARGET_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
35998 SET_EXPR_LOCATION (stmt, pragma_tok->location);
35999 add_stmt (stmt);
36001 check_clauses:
36002 while (*pc)
36004 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
36005 switch (OMP_CLAUSE_MAP_KIND (*pc))
36007 case GOMP_MAP_TO:
36008 case GOMP_MAP_ALWAYS_TO:
36009 case GOMP_MAP_FROM:
36010 case GOMP_MAP_ALWAYS_FROM:
36011 case GOMP_MAP_TOFROM:
36012 case GOMP_MAP_ALWAYS_TOFROM:
36013 case GOMP_MAP_ALLOC:
36014 case GOMP_MAP_FIRSTPRIVATE_POINTER:
36015 case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
36016 case GOMP_MAP_ALWAYS_POINTER:
36017 break;
36018 default:
36019 error_at (OMP_CLAUSE_LOCATION (*pc),
36020 "%<#pragma omp target%> with map-type other "
36021 "than %<to%>, %<from%>, %<tofrom%> or %<alloc%> "
36022 "on %<map%> clause");
36023 *pc = OMP_CLAUSE_CHAIN (*pc);
36024 continue;
36026 pc = &OMP_CLAUSE_CHAIN (*pc);
36028 return true;
36031 /* OpenACC 2.0:
36032 # pragma acc cache (variable-list) new-line
36035 static tree
36036 cp_parser_oacc_cache (cp_parser *parser, cp_token *pragma_tok)
36038 tree stmt, clauses;
36040 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE__CACHE_, NULL_TREE);
36041 clauses = finish_omp_clauses (clauses, C_ORT_ACC);
36043 cp_parser_require_pragma_eol (parser, cp_lexer_peek_token (parser->lexer));
36045 stmt = make_node (OACC_CACHE);
36046 TREE_TYPE (stmt) = void_type_node;
36047 OACC_CACHE_CLAUSES (stmt) = clauses;
36048 SET_EXPR_LOCATION (stmt, pragma_tok->location);
36049 add_stmt (stmt);
36051 return stmt;
36054 /* OpenACC 2.0:
36055 # pragma acc data oacc-data-clause[optseq] new-line
36056 structured-block */
36058 #define OACC_DATA_CLAUSE_MASK \
36059 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
36060 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
36061 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
36062 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
36063 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
36064 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
36065 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
36066 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
36067 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
36068 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
36069 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE))
36071 static tree
36072 cp_parser_oacc_data (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
36074 tree stmt, clauses, block;
36075 unsigned int save;
36077 clauses = cp_parser_oacc_all_clauses (parser, OACC_DATA_CLAUSE_MASK,
36078 "#pragma acc data", pragma_tok);
36080 block = begin_omp_parallel ();
36081 save = cp_parser_begin_omp_structured_block (parser);
36082 cp_parser_statement (parser, NULL_TREE, false, if_p);
36083 cp_parser_end_omp_structured_block (parser, save);
36084 stmt = finish_oacc_data (clauses, block);
36085 return stmt;
36088 /* OpenACC 2.0:
36089 # pragma acc host_data <clauses> new-line
36090 structured-block */
36092 #define OACC_HOST_DATA_CLAUSE_MASK \
36093 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_USE_DEVICE) )
36095 static tree
36096 cp_parser_oacc_host_data (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
36098 tree stmt, clauses, block;
36099 unsigned int save;
36101 clauses = cp_parser_oacc_all_clauses (parser, OACC_HOST_DATA_CLAUSE_MASK,
36102 "#pragma acc host_data", pragma_tok);
36104 block = begin_omp_parallel ();
36105 save = cp_parser_begin_omp_structured_block (parser);
36106 cp_parser_statement (parser, NULL_TREE, false, if_p);
36107 cp_parser_end_omp_structured_block (parser, save);
36108 stmt = finish_oacc_host_data (clauses, block);
36109 return stmt;
36112 /* OpenACC 2.0:
36113 # pragma acc declare oacc-data-clause[optseq] new-line
36116 #define OACC_DECLARE_CLAUSE_MASK \
36117 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
36118 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
36119 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
36120 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
36121 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
36122 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT) \
36123 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_LINK) \
36124 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
36125 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
36126 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
36127 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
36128 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE))
36130 static tree
36131 cp_parser_oacc_declare (cp_parser *parser, cp_token *pragma_tok)
36133 tree clauses, stmt;
36134 bool error = false;
36136 clauses = cp_parser_oacc_all_clauses (parser, OACC_DECLARE_CLAUSE_MASK,
36137 "#pragma acc declare", pragma_tok, true);
36140 if (omp_find_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
36142 error_at (pragma_tok->location,
36143 "no valid clauses specified in %<#pragma acc declare%>");
36144 return NULL_TREE;
36147 for (tree t = clauses; t; t = OMP_CLAUSE_CHAIN (t))
36149 location_t loc = OMP_CLAUSE_LOCATION (t);
36150 tree decl = OMP_CLAUSE_DECL (t);
36151 if (!DECL_P (decl))
36153 error_at (loc, "array section in %<#pragma acc declare%>");
36154 error = true;
36155 continue;
36157 gcc_assert (OMP_CLAUSE_CODE (t) == OMP_CLAUSE_MAP);
36158 switch (OMP_CLAUSE_MAP_KIND (t))
36160 case GOMP_MAP_FIRSTPRIVATE_POINTER:
36161 case GOMP_MAP_FORCE_ALLOC:
36162 case GOMP_MAP_FORCE_TO:
36163 case GOMP_MAP_FORCE_DEVICEPTR:
36164 case GOMP_MAP_DEVICE_RESIDENT:
36165 break;
36167 case GOMP_MAP_LINK:
36168 if (!global_bindings_p ()
36169 && (TREE_STATIC (decl)
36170 || !DECL_EXTERNAL (decl)))
36172 error_at (loc,
36173 "%qD must be a global variable in "
36174 "%<#pragma acc declare link%>",
36175 decl);
36176 error = true;
36177 continue;
36179 break;
36181 default:
36182 if (global_bindings_p ())
36184 error_at (loc, "invalid OpenACC clause at file scope");
36185 error = true;
36186 continue;
36188 if (DECL_EXTERNAL (decl))
36190 error_at (loc,
36191 "invalid use of %<extern%> variable %qD "
36192 "in %<#pragma acc declare%>", decl);
36193 error = true;
36194 continue;
36196 else if (TREE_PUBLIC (decl))
36198 error_at (loc,
36199 "invalid use of %<global%> variable %qD "
36200 "in %<#pragma acc declare%>", decl);
36201 error = true;
36202 continue;
36204 break;
36207 if (lookup_attribute ("omp declare target", DECL_ATTRIBUTES (decl))
36208 || lookup_attribute ("omp declare target link",
36209 DECL_ATTRIBUTES (decl)))
36211 error_at (loc, "variable %qD used more than once with "
36212 "%<#pragma acc declare%>", decl);
36213 error = true;
36214 continue;
36217 if (!error)
36219 tree id;
36221 if (OMP_CLAUSE_MAP_KIND (t) == GOMP_MAP_LINK)
36222 id = get_identifier ("omp declare target link");
36223 else
36224 id = get_identifier ("omp declare target");
36226 DECL_ATTRIBUTES (decl)
36227 = tree_cons (id, NULL_TREE, DECL_ATTRIBUTES (decl));
36228 if (global_bindings_p ())
36230 symtab_node *node = symtab_node::get (decl);
36231 if (node != NULL)
36233 node->offloadable = 1;
36234 if (ENABLE_OFFLOADING)
36236 g->have_offload = true;
36237 if (is_a <varpool_node *> (node))
36238 vec_safe_push (offload_vars, decl);
36245 if (error || global_bindings_p ())
36246 return NULL_TREE;
36248 stmt = make_node (OACC_DECLARE);
36249 TREE_TYPE (stmt) = void_type_node;
36250 OACC_DECLARE_CLAUSES (stmt) = clauses;
36251 SET_EXPR_LOCATION (stmt, pragma_tok->location);
36253 add_stmt (stmt);
36255 return NULL_TREE;
36258 /* OpenACC 2.0:
36259 # pragma acc enter data oacc-enter-data-clause[optseq] new-line
36263 # pragma acc exit data oacc-exit-data-clause[optseq] new-line
36265 LOC is the location of the #pragma token.
36268 #define OACC_ENTER_DATA_CLAUSE_MASK \
36269 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
36270 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
36271 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
36272 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
36273 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
36274 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE) \
36275 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
36277 #define OACC_EXIT_DATA_CLAUSE_MASK \
36278 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
36279 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
36280 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
36281 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DELETE) \
36282 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
36284 static tree
36285 cp_parser_oacc_enter_exit_data (cp_parser *parser, cp_token *pragma_tok,
36286 bool enter)
36288 location_t loc = pragma_tok->location;
36289 tree stmt, clauses;
36290 const char *p = "";
36292 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36293 p = IDENTIFIER_POINTER (cp_lexer_peek_token (parser->lexer)->u.value);
36295 if (strcmp (p, "data") != 0)
36297 error_at (loc, "expected %<data%> after %<#pragma acc %s%>",
36298 enter ? "enter" : "exit");
36299 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36300 return NULL_TREE;
36303 cp_lexer_consume_token (parser->lexer);
36305 if (enter)
36306 clauses = cp_parser_oacc_all_clauses (parser, OACC_ENTER_DATA_CLAUSE_MASK,
36307 "#pragma acc enter data", pragma_tok);
36308 else
36309 clauses = cp_parser_oacc_all_clauses (parser, OACC_EXIT_DATA_CLAUSE_MASK,
36310 "#pragma acc exit data", pragma_tok);
36312 if (omp_find_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
36314 error_at (loc, "%<#pragma acc %s data%> has no data movement clause",
36315 enter ? "enter" : "exit");
36316 return NULL_TREE;
36319 stmt = enter ? make_node (OACC_ENTER_DATA) : make_node (OACC_EXIT_DATA);
36320 TREE_TYPE (stmt) = void_type_node;
36321 OMP_STANDALONE_CLAUSES (stmt) = clauses;
36322 SET_EXPR_LOCATION (stmt, pragma_tok->location);
36323 add_stmt (stmt);
36324 return stmt;
36327 /* OpenACC 2.0:
36328 # pragma acc loop oacc-loop-clause[optseq] new-line
36329 structured-block */
36331 #define OACC_LOOP_CLAUSE_MASK \
36332 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COLLAPSE) \
36333 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRIVATE) \
36334 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION) \
36335 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_GANG) \
36336 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR) \
36337 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WORKER) \
36338 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_AUTO) \
36339 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_INDEPENDENT) \
36340 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SEQ) \
36341 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_TILE))
36343 static tree
36344 cp_parser_oacc_loop (cp_parser *parser, cp_token *pragma_tok, char *p_name,
36345 omp_clause_mask mask, tree *cclauses, bool *if_p)
36347 bool is_parallel = ((mask >> PRAGMA_OACC_CLAUSE_REDUCTION) & 1) == 1;
36349 strcat (p_name, " loop");
36350 mask |= OACC_LOOP_CLAUSE_MASK;
36352 tree clauses = cp_parser_oacc_all_clauses (parser, mask, p_name, pragma_tok,
36353 cclauses == NULL);
36354 if (cclauses)
36356 clauses = c_oacc_split_loop_clauses (clauses, cclauses, is_parallel);
36357 if (*cclauses)
36358 *cclauses = finish_omp_clauses (*cclauses, C_ORT_ACC);
36359 if (clauses)
36360 clauses = finish_omp_clauses (clauses, C_ORT_ACC);
36363 tree block = begin_omp_structured_block ();
36364 int save = cp_parser_begin_omp_structured_block (parser);
36365 tree stmt = cp_parser_omp_for_loop (parser, OACC_LOOP, clauses, NULL, if_p);
36366 cp_parser_end_omp_structured_block (parser, save);
36367 add_stmt (finish_omp_structured_block (block));
36369 return stmt;
36372 /* OpenACC 2.0:
36373 # pragma acc kernels oacc-kernels-clause[optseq] new-line
36374 structured-block
36378 # pragma acc parallel oacc-parallel-clause[optseq] new-line
36379 structured-block
36382 #define OACC_KERNELS_CLAUSE_MASK \
36383 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
36384 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
36385 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
36386 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
36387 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
36388 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEFAULT) \
36389 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
36390 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
36391 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
36392 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
36393 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
36394 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
36395 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE) \
36396 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
36398 #define OACC_PARALLEL_CLAUSE_MASK \
36399 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
36400 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
36401 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
36402 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
36403 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
36404 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEFAULT) \
36405 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
36406 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_FIRSTPRIVATE) \
36407 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
36408 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_GANGS) \
36409 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_WORKERS) \
36410 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
36411 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
36412 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
36413 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
36414 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE) \
36415 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRIVATE) \
36416 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION) \
36417 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR_LENGTH) \
36418 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
36420 static tree
36421 cp_parser_oacc_kernels_parallel (cp_parser *parser, cp_token *pragma_tok,
36422 char *p_name, bool *if_p)
36424 omp_clause_mask mask;
36425 enum tree_code code;
36426 switch (cp_parser_pragma_kind (pragma_tok))
36428 case PRAGMA_OACC_KERNELS:
36429 strcat (p_name, " kernels");
36430 mask = OACC_KERNELS_CLAUSE_MASK;
36431 code = OACC_KERNELS;
36432 break;
36433 case PRAGMA_OACC_PARALLEL:
36434 strcat (p_name, " parallel");
36435 mask = OACC_PARALLEL_CLAUSE_MASK;
36436 code = OACC_PARALLEL;
36437 break;
36438 default:
36439 gcc_unreachable ();
36442 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36444 const char *p
36445 = IDENTIFIER_POINTER (cp_lexer_peek_token (parser->lexer)->u.value);
36446 if (strcmp (p, "loop") == 0)
36448 cp_lexer_consume_token (parser->lexer);
36449 tree block = begin_omp_parallel ();
36450 tree clauses;
36451 cp_parser_oacc_loop (parser, pragma_tok, p_name, mask, &clauses,
36452 if_p);
36453 return finish_omp_construct (code, block, clauses);
36457 tree clauses = cp_parser_oacc_all_clauses (parser, mask, p_name, pragma_tok);
36459 tree block = begin_omp_parallel ();
36460 unsigned int save = cp_parser_begin_omp_structured_block (parser);
36461 cp_parser_statement (parser, NULL_TREE, false, if_p);
36462 cp_parser_end_omp_structured_block (parser, save);
36463 return finish_omp_construct (code, block, clauses);
36466 /* OpenACC 2.0:
36467 # pragma acc update oacc-update-clause[optseq] new-line
36470 #define OACC_UPDATE_CLAUSE_MASK \
36471 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
36472 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICE) \
36473 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_HOST) \
36474 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
36475 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SELF) \
36476 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT))
36478 static tree
36479 cp_parser_oacc_update (cp_parser *parser, cp_token *pragma_tok)
36481 tree stmt, clauses;
36483 clauses = cp_parser_oacc_all_clauses (parser, OACC_UPDATE_CLAUSE_MASK,
36484 "#pragma acc update", pragma_tok);
36486 if (omp_find_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
36488 error_at (pragma_tok->location,
36489 "%<#pragma acc update%> must contain at least one "
36490 "%<device%> or %<host%> or %<self%> clause");
36491 return NULL_TREE;
36494 stmt = make_node (OACC_UPDATE);
36495 TREE_TYPE (stmt) = void_type_node;
36496 OACC_UPDATE_CLAUSES (stmt) = clauses;
36497 SET_EXPR_LOCATION (stmt, pragma_tok->location);
36498 add_stmt (stmt);
36499 return stmt;
36502 /* OpenACC 2.0:
36503 # pragma acc wait [(intseq)] oacc-wait-clause[optseq] new-line
36505 LOC is the location of the #pragma token.
36508 #define OACC_WAIT_CLAUSE_MASK \
36509 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC))
36511 static tree
36512 cp_parser_oacc_wait (cp_parser *parser, cp_token *pragma_tok)
36514 tree clauses, list = NULL_TREE, stmt = NULL_TREE;
36515 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
36517 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
36518 list = cp_parser_oacc_wait_list (parser, loc, list);
36520 clauses = cp_parser_oacc_all_clauses (parser, OACC_WAIT_CLAUSE_MASK,
36521 "#pragma acc wait", pragma_tok);
36523 stmt = c_finish_oacc_wait (loc, list, clauses);
36524 stmt = finish_expr_stmt (stmt);
36526 return stmt;
36529 /* OpenMP 4.0:
36530 # pragma omp declare simd declare-simd-clauses[optseq] new-line */
36532 #define OMP_DECLARE_SIMD_CLAUSE_MASK \
36533 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN) \
36534 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
36535 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
36536 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNIFORM) \
36537 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_INBRANCH) \
36538 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOTINBRANCH))
36540 static void
36541 cp_parser_omp_declare_simd (cp_parser *parser, cp_token *pragma_tok,
36542 enum pragma_context context)
36544 bool first_p = parser->omp_declare_simd == NULL;
36545 cp_omp_declare_simd_data data;
36546 if (first_p)
36548 data.error_seen = false;
36549 data.fndecl_seen = false;
36550 data.tokens = vNULL;
36551 data.clauses = NULL_TREE;
36552 /* It is safe to take the address of a local variable; it will only be
36553 used while this scope is live. */
36554 parser->omp_declare_simd = &data;
36557 /* Store away all pragma tokens. */
36558 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
36559 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
36560 cp_lexer_consume_token (parser->lexer);
36561 if (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
36562 parser->omp_declare_simd->error_seen = true;
36563 cp_parser_require_pragma_eol (parser, pragma_tok);
36564 struct cp_token_cache *cp
36565 = cp_token_cache_new (pragma_tok, cp_lexer_peek_token (parser->lexer));
36566 parser->omp_declare_simd->tokens.safe_push (cp);
36568 if (first_p)
36570 while (cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA))
36571 cp_parser_pragma (parser, context, NULL);
36572 switch (context)
36574 case pragma_external:
36575 cp_parser_declaration (parser);
36576 break;
36577 case pragma_member:
36578 cp_parser_member_declaration (parser);
36579 break;
36580 case pragma_objc_icode:
36581 cp_parser_block_declaration (parser, /*statement_p=*/false);
36582 break;
36583 default:
36584 cp_parser_declaration_statement (parser);
36585 break;
36587 if (parser->omp_declare_simd
36588 && !parser->omp_declare_simd->error_seen
36589 && !parser->omp_declare_simd->fndecl_seen)
36590 error_at (pragma_tok->location,
36591 "%<#pragma omp declare simd%> not immediately followed by "
36592 "function declaration or definition");
36593 data.tokens.release ();
36594 parser->omp_declare_simd = NULL;
36598 /* Handles the delayed parsing of the Cilk Plus SIMD-enabled function.
36599 This function is modelled similar to the late parsing of omp declare
36600 simd. */
36602 static tree
36603 cp_parser_late_parsing_cilk_simd_fn_info (cp_parser *parser, tree attrs)
36605 struct cp_token_cache *ce;
36606 cp_omp_declare_simd_data *info = parser->cilk_simd_fn_info;
36607 int ii = 0;
36609 if (parser->omp_declare_simd != NULL
36610 || lookup_attribute ("simd", attrs))
36612 error ("%<#pragma omp declare simd%> or %<simd%> attribute cannot be "
36613 "used in the same function marked as a Cilk Plus SIMD-enabled "
36614 "function");
36615 parser->cilk_simd_fn_info->tokens.release ();
36616 XDELETE (parser->cilk_simd_fn_info);
36617 parser->cilk_simd_fn_info = NULL;
36618 return attrs;
36620 if (!info->error_seen && info->fndecl_seen)
36622 error ("vector attribute not immediately followed by a single function"
36623 " declaration or definition");
36624 info->error_seen = true;
36626 if (info->error_seen)
36627 return attrs;
36629 FOR_EACH_VEC_ELT (info->tokens, ii, ce)
36631 tree c, cl;
36633 cp_parser_push_lexer_for_tokens (parser, ce);
36634 parser->lexer->in_pragma = true;
36635 cl = cp_parser_omp_all_clauses (parser, CILK_SIMD_FN_CLAUSE_MASK,
36636 "SIMD-enabled functions attribute",
36637 NULL);
36638 cp_parser_pop_lexer (parser);
36639 if (cl)
36640 cl = tree_cons (NULL_TREE, cl, NULL_TREE);
36642 c = build_tree_list (get_identifier ("cilk simd function"), NULL_TREE);
36643 TREE_CHAIN (c) = attrs;
36644 attrs = c;
36646 c = build_tree_list (get_identifier ("omp declare simd"), cl);
36647 TREE_CHAIN (c) = attrs;
36648 if (processing_template_decl)
36649 ATTR_IS_DEPENDENT (c) = 1;
36650 attrs = c;
36652 info->fndecl_seen = true;
36653 parser->cilk_simd_fn_info->tokens.release ();
36654 XDELETE (parser->cilk_simd_fn_info);
36655 parser->cilk_simd_fn_info = NULL;
36656 return attrs;
36659 /* Finalize #pragma omp declare simd clauses after direct declarator has
36660 been parsed, and put that into "omp declare simd" attribute. */
36662 static tree
36663 cp_parser_late_parsing_omp_declare_simd (cp_parser *parser, tree attrs)
36665 struct cp_token_cache *ce;
36666 cp_omp_declare_simd_data *data = parser->omp_declare_simd;
36667 int i;
36669 if (!data->error_seen && data->fndecl_seen)
36671 error ("%<#pragma omp declare simd%> not immediately followed by "
36672 "a single function declaration or definition");
36673 data->error_seen = true;
36675 if (data->error_seen)
36676 return attrs;
36678 FOR_EACH_VEC_ELT (data->tokens, i, ce)
36680 tree c, cl;
36682 cp_parser_push_lexer_for_tokens (parser, ce);
36683 parser->lexer->in_pragma = true;
36684 gcc_assert (cp_lexer_peek_token (parser->lexer)->type == CPP_PRAGMA);
36685 cp_token *pragma_tok = cp_lexer_consume_token (parser->lexer);
36686 cp_lexer_consume_token (parser->lexer);
36687 cl = cp_parser_omp_all_clauses (parser, OMP_DECLARE_SIMD_CLAUSE_MASK,
36688 "#pragma omp declare simd", pragma_tok);
36689 cp_parser_pop_lexer (parser);
36690 if (cl)
36691 cl = tree_cons (NULL_TREE, cl, NULL_TREE);
36692 c = build_tree_list (get_identifier ("omp declare simd"), cl);
36693 TREE_CHAIN (c) = attrs;
36694 if (processing_template_decl)
36695 ATTR_IS_DEPENDENT (c) = 1;
36696 attrs = c;
36699 data->fndecl_seen = true;
36700 return attrs;
36704 /* OpenMP 4.0:
36705 # pragma omp declare target new-line
36706 declarations and definitions
36707 # pragma omp end declare target new-line
36709 OpenMP 4.5:
36710 # pragma omp declare target ( extended-list ) new-line
36712 # pragma omp declare target declare-target-clauses[seq] new-line */
36714 #define OMP_DECLARE_TARGET_CLAUSE_MASK \
36715 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO) \
36716 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINK))
36718 static void
36719 cp_parser_omp_declare_target (cp_parser *parser, cp_token *pragma_tok)
36721 tree clauses = NULL_TREE;
36722 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36723 clauses
36724 = cp_parser_omp_all_clauses (parser, OMP_DECLARE_TARGET_CLAUSE_MASK,
36725 "#pragma omp declare target", pragma_tok);
36726 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
36728 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_TO_DECLARE,
36729 clauses);
36730 clauses = finish_omp_clauses (clauses, C_ORT_OMP);
36731 cp_parser_require_pragma_eol (parser, pragma_tok);
36733 else
36735 cp_parser_require_pragma_eol (parser, pragma_tok);
36736 scope_chain->omp_declare_target_attribute++;
36737 return;
36739 if (scope_chain->omp_declare_target_attribute)
36740 error_at (pragma_tok->location,
36741 "%<#pragma omp declare target%> with clauses in between "
36742 "%<#pragma omp declare target%> without clauses and "
36743 "%<#pragma omp end declare target%>");
36744 for (tree c = clauses; c; c = OMP_CLAUSE_CHAIN (c))
36746 tree t = OMP_CLAUSE_DECL (c), id;
36747 tree at1 = lookup_attribute ("omp declare target", DECL_ATTRIBUTES (t));
36748 tree at2 = lookup_attribute ("omp declare target link",
36749 DECL_ATTRIBUTES (t));
36750 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINK)
36752 id = get_identifier ("omp declare target link");
36753 std::swap (at1, at2);
36755 else
36756 id = get_identifier ("omp declare target");
36757 if (at2)
36759 error_at (OMP_CLAUSE_LOCATION (c),
36760 "%qD specified both in declare target %<link%> and %<to%>"
36761 " clauses", t);
36762 continue;
36764 if (!at1)
36766 DECL_ATTRIBUTES (t) = tree_cons (id, NULL_TREE, DECL_ATTRIBUTES (t));
36767 if (TREE_CODE (t) != FUNCTION_DECL && !is_global_var (t))
36768 continue;
36770 symtab_node *node = symtab_node::get (t);
36771 if (node != NULL)
36773 node->offloadable = 1;
36774 if (ENABLE_OFFLOADING)
36776 g->have_offload = true;
36777 if (is_a <varpool_node *> (node))
36778 vec_safe_push (offload_vars, t);
36785 static void
36786 cp_parser_omp_end_declare_target (cp_parser *parser, cp_token *pragma_tok)
36788 const char *p = "";
36789 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36791 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
36792 p = IDENTIFIER_POINTER (id);
36794 if (strcmp (p, "declare") == 0)
36796 cp_lexer_consume_token (parser->lexer);
36797 p = "";
36798 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36800 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
36801 p = IDENTIFIER_POINTER (id);
36803 if (strcmp (p, "target") == 0)
36804 cp_lexer_consume_token (parser->lexer);
36805 else
36807 cp_parser_error (parser, "expected %<target%>");
36808 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36809 return;
36812 else
36814 cp_parser_error (parser, "expected %<declare%>");
36815 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36816 return;
36818 cp_parser_require_pragma_eol (parser, pragma_tok);
36819 if (!scope_chain->omp_declare_target_attribute)
36820 error_at (pragma_tok->location,
36821 "%<#pragma omp end declare target%> without corresponding "
36822 "%<#pragma omp declare target%>");
36823 else
36824 scope_chain->omp_declare_target_attribute--;
36827 /* Helper function of cp_parser_omp_declare_reduction. Parse the combiner
36828 expression and optional initializer clause of
36829 #pragma omp declare reduction. We store the expression(s) as
36830 either 3, 6 or 7 special statements inside of the artificial function's
36831 body. The first two statements are DECL_EXPRs for the artificial
36832 OMP_OUT resp. OMP_IN variables, followed by a statement with the combiner
36833 expression that uses those variables.
36834 If there was any INITIALIZER clause, this is followed by further statements,
36835 the fourth and fifth statements are DECL_EXPRs for the artificial
36836 OMP_PRIV resp. OMP_ORIG variables. If the INITIALIZER clause wasn't the
36837 constructor variant (first token after open paren is not omp_priv),
36838 then the sixth statement is a statement with the function call expression
36839 that uses the OMP_PRIV and optionally OMP_ORIG variable.
36840 Otherwise, the sixth statement is whatever statement cp_finish_decl emits
36841 to initialize the OMP_PRIV artificial variable and there is seventh
36842 statement, a DECL_EXPR of the OMP_PRIV statement again. */
36844 static bool
36845 cp_parser_omp_declare_reduction_exprs (tree fndecl, cp_parser *parser)
36847 tree type = TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (fndecl)));
36848 gcc_assert (TREE_CODE (type) == REFERENCE_TYPE);
36849 type = TREE_TYPE (type);
36850 tree omp_out = build_lang_decl (VAR_DECL, get_identifier ("omp_out"), type);
36851 DECL_ARTIFICIAL (omp_out) = 1;
36852 pushdecl (omp_out);
36853 add_decl_expr (omp_out);
36854 tree omp_in = build_lang_decl (VAR_DECL, get_identifier ("omp_in"), type);
36855 DECL_ARTIFICIAL (omp_in) = 1;
36856 pushdecl (omp_in);
36857 add_decl_expr (omp_in);
36858 tree combiner;
36859 tree omp_priv = NULL_TREE, omp_orig = NULL_TREE, initializer = NULL_TREE;
36861 keep_next_level (true);
36862 tree block = begin_omp_structured_block ();
36863 combiner = cp_parser_expression (parser);
36864 finish_expr_stmt (combiner);
36865 block = finish_omp_structured_block (block);
36866 add_stmt (block);
36868 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
36869 return false;
36871 const char *p = "";
36872 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36874 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
36875 p = IDENTIFIER_POINTER (id);
36878 if (strcmp (p, "initializer") == 0)
36880 cp_lexer_consume_token (parser->lexer);
36881 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
36882 return false;
36884 p = "";
36885 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36887 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
36888 p = IDENTIFIER_POINTER (id);
36891 omp_priv = build_lang_decl (VAR_DECL, get_identifier ("omp_priv"), type);
36892 DECL_ARTIFICIAL (omp_priv) = 1;
36893 pushdecl (omp_priv);
36894 add_decl_expr (omp_priv);
36895 omp_orig = build_lang_decl (VAR_DECL, get_identifier ("omp_orig"), type);
36896 DECL_ARTIFICIAL (omp_orig) = 1;
36897 pushdecl (omp_orig);
36898 add_decl_expr (omp_orig);
36900 keep_next_level (true);
36901 block = begin_omp_structured_block ();
36903 bool ctor = false;
36904 if (strcmp (p, "omp_priv") == 0)
36906 bool is_direct_init, is_non_constant_init;
36907 ctor = true;
36908 cp_lexer_consume_token (parser->lexer);
36909 /* Reject initializer (omp_priv) and initializer (omp_priv ()). */
36910 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
36911 || (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
36912 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
36913 == CPP_CLOSE_PAREN
36914 && cp_lexer_peek_nth_token (parser->lexer, 3)->type
36915 == CPP_CLOSE_PAREN))
36917 finish_omp_structured_block (block);
36918 error ("invalid initializer clause");
36919 return false;
36921 initializer = cp_parser_initializer (parser, &is_direct_init,
36922 &is_non_constant_init);
36923 cp_finish_decl (omp_priv, initializer, !is_non_constant_init,
36924 NULL_TREE, LOOKUP_ONLYCONVERTING);
36926 else
36928 cp_parser_parse_tentatively (parser);
36929 tree fn_name = cp_parser_id_expression (parser, /*template_p=*/false,
36930 /*check_dependency_p=*/true,
36931 /*template_p=*/NULL,
36932 /*declarator_p=*/false,
36933 /*optional_p=*/false);
36934 vec<tree, va_gc> *args;
36935 if (fn_name == error_mark_node
36936 || cp_parser_error_occurred (parser)
36937 || !cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
36938 || ((args = cp_parser_parenthesized_expression_list
36939 (parser, non_attr, /*cast_p=*/false,
36940 /*allow_expansion_p=*/true,
36941 /*non_constant_p=*/NULL)),
36942 cp_parser_error_occurred (parser)))
36944 finish_omp_structured_block (block);
36945 cp_parser_abort_tentative_parse (parser);
36946 cp_parser_error (parser, "expected id-expression (arguments)");
36947 return false;
36949 unsigned int i;
36950 tree arg;
36951 FOR_EACH_VEC_SAFE_ELT (args, i, arg)
36952 if (arg == omp_priv
36953 || (TREE_CODE (arg) == ADDR_EXPR
36954 && TREE_OPERAND (arg, 0) == omp_priv))
36955 break;
36956 cp_parser_abort_tentative_parse (parser);
36957 if (arg == NULL_TREE)
36958 error ("one of the initializer call arguments should be %<omp_priv%>"
36959 " or %<&omp_priv%>");
36960 initializer = cp_parser_postfix_expression (parser, false, false, false,
36961 false, NULL);
36962 finish_expr_stmt (initializer);
36965 block = finish_omp_structured_block (block);
36966 cp_walk_tree (&block, cp_remove_omp_priv_cleanup_stmt, omp_priv, NULL);
36967 add_stmt (block);
36969 if (ctor)
36970 add_decl_expr (omp_orig);
36972 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
36973 return false;
36976 if (!cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA_EOL))
36977 cp_parser_required_error (parser, RT_PRAGMA_EOL, /*keyword=*/false);
36979 return true;
36982 /* OpenMP 4.0
36983 #pragma omp declare reduction (reduction-id : typename-list : expression) \
36984 initializer-clause[opt] new-line
36986 initializer-clause:
36987 initializer (omp_priv initializer)
36988 initializer (function-name (argument-list)) */
36990 static void
36991 cp_parser_omp_declare_reduction (cp_parser *parser, cp_token *pragma_tok,
36992 enum pragma_context)
36994 auto_vec<tree> types;
36995 enum tree_code reduc_code = ERROR_MARK;
36996 tree reduc_id = NULL_TREE, orig_reduc_id = NULL_TREE, type;
36997 unsigned int i;
36998 cp_token *first_token;
36999 cp_token_cache *cp;
37000 int errs;
37001 void *p;
37003 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
37004 p = obstack_alloc (&declarator_obstack, 0);
37006 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
37007 goto fail;
37009 switch (cp_lexer_peek_token (parser->lexer)->type)
37011 case CPP_PLUS:
37012 reduc_code = PLUS_EXPR;
37013 break;
37014 case CPP_MULT:
37015 reduc_code = MULT_EXPR;
37016 break;
37017 case CPP_MINUS:
37018 reduc_code = MINUS_EXPR;
37019 break;
37020 case CPP_AND:
37021 reduc_code = BIT_AND_EXPR;
37022 break;
37023 case CPP_XOR:
37024 reduc_code = BIT_XOR_EXPR;
37025 break;
37026 case CPP_OR:
37027 reduc_code = BIT_IOR_EXPR;
37028 break;
37029 case CPP_AND_AND:
37030 reduc_code = TRUTH_ANDIF_EXPR;
37031 break;
37032 case CPP_OR_OR:
37033 reduc_code = TRUTH_ORIF_EXPR;
37034 break;
37035 case CPP_NAME:
37036 reduc_id = orig_reduc_id = cp_parser_identifier (parser);
37037 break;
37038 default:
37039 cp_parser_error (parser, "expected %<+%>, %<*%>, %<-%>, %<&%>, %<^%>, "
37040 "%<|%>, %<&&%>, %<||%> or identifier");
37041 goto fail;
37044 if (reduc_code != ERROR_MARK)
37045 cp_lexer_consume_token (parser->lexer);
37047 reduc_id = omp_reduction_id (reduc_code, reduc_id, NULL_TREE);
37048 if (reduc_id == error_mark_node)
37049 goto fail;
37051 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
37052 goto fail;
37054 /* Types may not be defined in declare reduction type list. */
37055 const char *saved_message;
37056 saved_message = parser->type_definition_forbidden_message;
37057 parser->type_definition_forbidden_message
37058 = G_("types may not be defined in declare reduction type list");
37059 bool saved_colon_corrects_to_scope_p;
37060 saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
37061 parser->colon_corrects_to_scope_p = false;
37062 bool saved_colon_doesnt_start_class_def_p;
37063 saved_colon_doesnt_start_class_def_p
37064 = parser->colon_doesnt_start_class_def_p;
37065 parser->colon_doesnt_start_class_def_p = true;
37067 while (true)
37069 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
37070 type = cp_parser_type_id (parser);
37071 if (type == error_mark_node)
37073 else if (ARITHMETIC_TYPE_P (type)
37074 && (orig_reduc_id == NULL_TREE
37075 || (TREE_CODE (type) != COMPLEX_TYPE
37076 && (strcmp (IDENTIFIER_POINTER (orig_reduc_id),
37077 "min") == 0
37078 || strcmp (IDENTIFIER_POINTER (orig_reduc_id),
37079 "max") == 0))))
37080 error_at (loc, "predeclared arithmetic type %qT in "
37081 "%<#pragma omp declare reduction%>", type);
37082 else if (TREE_CODE (type) == FUNCTION_TYPE
37083 || TREE_CODE (type) == METHOD_TYPE
37084 || TREE_CODE (type) == ARRAY_TYPE)
37085 error_at (loc, "function or array type %qT in "
37086 "%<#pragma omp declare reduction%>", type);
37087 else if (TREE_CODE (type) == REFERENCE_TYPE)
37088 error_at (loc, "reference type %qT in "
37089 "%<#pragma omp declare reduction%>", type);
37090 else if (TYPE_QUALS_NO_ADDR_SPACE (type))
37091 error_at (loc, "const, volatile or __restrict qualified type %qT in "
37092 "%<#pragma omp declare reduction%>", type);
37093 else
37094 types.safe_push (type);
37096 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
37097 cp_lexer_consume_token (parser->lexer);
37098 else
37099 break;
37102 /* Restore the saved message. */
37103 parser->type_definition_forbidden_message = saved_message;
37104 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
37105 parser->colon_doesnt_start_class_def_p
37106 = saved_colon_doesnt_start_class_def_p;
37108 if (!cp_parser_require (parser, CPP_COLON, RT_COLON)
37109 || types.is_empty ())
37111 fail:
37112 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37113 goto done;
37116 first_token = cp_lexer_peek_token (parser->lexer);
37117 cp = NULL;
37118 errs = errorcount;
37119 FOR_EACH_VEC_ELT (types, i, type)
37121 tree fntype
37122 = build_function_type_list (void_type_node,
37123 cp_build_reference_type (type, false),
37124 NULL_TREE);
37125 tree this_reduc_id = reduc_id;
37126 if (!dependent_type_p (type))
37127 this_reduc_id = omp_reduction_id (ERROR_MARK, reduc_id, type);
37128 tree fndecl = build_lang_decl (FUNCTION_DECL, this_reduc_id, fntype);
37129 DECL_SOURCE_LOCATION (fndecl) = pragma_tok->location;
37130 DECL_ARTIFICIAL (fndecl) = 1;
37131 DECL_EXTERNAL (fndecl) = 1;
37132 DECL_DECLARED_INLINE_P (fndecl) = 1;
37133 DECL_IGNORED_P (fndecl) = 1;
37134 DECL_OMP_DECLARE_REDUCTION_P (fndecl) = 1;
37135 SET_DECL_ASSEMBLER_NAME (fndecl, get_identifier ("<udr>"));
37136 DECL_ATTRIBUTES (fndecl)
37137 = tree_cons (get_identifier ("gnu_inline"), NULL_TREE,
37138 DECL_ATTRIBUTES (fndecl));
37139 if (processing_template_decl)
37140 fndecl = push_template_decl (fndecl);
37141 bool block_scope = false;
37142 tree block = NULL_TREE;
37143 if (current_function_decl)
37145 block_scope = true;
37146 DECL_CONTEXT (fndecl) = global_namespace;
37147 if (!processing_template_decl)
37148 pushdecl (fndecl);
37150 else if (current_class_type)
37152 if (cp == NULL)
37154 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
37155 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
37156 cp_lexer_consume_token (parser->lexer);
37157 if (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
37158 goto fail;
37159 cp = cp_token_cache_new (first_token,
37160 cp_lexer_peek_nth_token (parser->lexer,
37161 2));
37163 DECL_STATIC_FUNCTION_P (fndecl) = 1;
37164 finish_member_declaration (fndecl);
37165 DECL_PENDING_INLINE_INFO (fndecl) = cp;
37166 DECL_PENDING_INLINE_P (fndecl) = 1;
37167 vec_safe_push (unparsed_funs_with_definitions, fndecl);
37168 continue;
37170 else
37172 DECL_CONTEXT (fndecl) = current_namespace;
37173 pushdecl (fndecl);
37175 if (!block_scope)
37176 start_preparsed_function (fndecl, NULL_TREE, SF_PRE_PARSED);
37177 else
37178 block = begin_omp_structured_block ();
37179 if (cp)
37181 cp_parser_push_lexer_for_tokens (parser, cp);
37182 parser->lexer->in_pragma = true;
37184 if (!cp_parser_omp_declare_reduction_exprs (fndecl, parser))
37186 if (!block_scope)
37187 finish_function (0);
37188 else
37189 DECL_CONTEXT (fndecl) = current_function_decl;
37190 if (cp)
37191 cp_parser_pop_lexer (parser);
37192 goto fail;
37194 if (cp)
37195 cp_parser_pop_lexer (parser);
37196 if (!block_scope)
37197 finish_function (0);
37198 else
37200 DECL_CONTEXT (fndecl) = current_function_decl;
37201 block = finish_omp_structured_block (block);
37202 if (TREE_CODE (block) == BIND_EXPR)
37203 DECL_SAVED_TREE (fndecl) = BIND_EXPR_BODY (block);
37204 else if (TREE_CODE (block) == STATEMENT_LIST)
37205 DECL_SAVED_TREE (fndecl) = block;
37206 if (processing_template_decl)
37207 add_decl_expr (fndecl);
37209 cp_check_omp_declare_reduction (fndecl);
37210 if (cp == NULL && types.length () > 1)
37211 cp = cp_token_cache_new (first_token,
37212 cp_lexer_peek_nth_token (parser->lexer, 2));
37213 if (errs != errorcount)
37214 break;
37217 cp_parser_require_pragma_eol (parser, pragma_tok);
37219 done:
37220 /* Free any declarators allocated. */
37221 obstack_free (&declarator_obstack, p);
37224 /* OpenMP 4.0
37225 #pragma omp declare simd declare-simd-clauses[optseq] new-line
37226 #pragma omp declare reduction (reduction-id : typename-list : expression) \
37227 initializer-clause[opt] new-line
37228 #pragma omp declare target new-line */
37230 static void
37231 cp_parser_omp_declare (cp_parser *parser, cp_token *pragma_tok,
37232 enum pragma_context context)
37234 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37236 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37237 const char *p = IDENTIFIER_POINTER (id);
37239 if (strcmp (p, "simd") == 0)
37241 cp_lexer_consume_token (parser->lexer);
37242 cp_parser_omp_declare_simd (parser, pragma_tok,
37243 context);
37244 return;
37246 cp_ensure_no_omp_declare_simd (parser);
37247 if (strcmp (p, "reduction") == 0)
37249 cp_lexer_consume_token (parser->lexer);
37250 cp_parser_omp_declare_reduction (parser, pragma_tok,
37251 context);
37252 return;
37254 if (!flag_openmp) /* flag_openmp_simd */
37256 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37257 return;
37259 if (strcmp (p, "target") == 0)
37261 cp_lexer_consume_token (parser->lexer);
37262 cp_parser_omp_declare_target (parser, pragma_tok);
37263 return;
37266 cp_parser_error (parser, "expected %<simd%> or %<reduction%> "
37267 "or %<target%>");
37268 cp_parser_require_pragma_eol (parser, pragma_tok);
37271 /* OpenMP 4.5:
37272 #pragma omp taskloop taskloop-clause[optseq] new-line
37273 for-loop
37275 #pragma omp taskloop simd taskloop-simd-clause[optseq] new-line
37276 for-loop */
37278 #define OMP_TASKLOOP_CLAUSE_MASK \
37279 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
37280 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
37281 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
37282 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
37283 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
37284 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_GRAINSIZE) \
37285 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TASKS) \
37286 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE) \
37287 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED) \
37288 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
37289 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL) \
37290 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE) \
37291 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOGROUP) \
37292 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIORITY))
37294 static tree
37295 cp_parser_omp_taskloop (cp_parser *parser, cp_token *pragma_tok,
37296 char *p_name, omp_clause_mask mask, tree *cclauses,
37297 bool *if_p)
37299 tree clauses, sb, ret;
37300 unsigned int save;
37301 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
37303 strcat (p_name, " taskloop");
37304 mask |= OMP_TASKLOOP_CLAUSE_MASK;
37306 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37308 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37309 const char *p = IDENTIFIER_POINTER (id);
37311 if (strcmp (p, "simd") == 0)
37313 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
37314 if (cclauses == NULL)
37315 cclauses = cclauses_buf;
37317 cp_lexer_consume_token (parser->lexer);
37318 if (!flag_openmp) /* flag_openmp_simd */
37319 return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
37320 cclauses, if_p);
37321 sb = begin_omp_structured_block ();
37322 save = cp_parser_begin_omp_structured_block (parser);
37323 ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
37324 cclauses, if_p);
37325 cp_parser_end_omp_structured_block (parser, save);
37326 tree body = finish_omp_structured_block (sb);
37327 if (ret == NULL)
37328 return ret;
37329 ret = make_node (OMP_TASKLOOP);
37330 TREE_TYPE (ret) = void_type_node;
37331 OMP_FOR_BODY (ret) = body;
37332 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_TASKLOOP];
37333 SET_EXPR_LOCATION (ret, loc);
37334 add_stmt (ret);
37335 return ret;
37338 if (!flag_openmp) /* flag_openmp_simd */
37340 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37341 return NULL_TREE;
37344 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
37345 cclauses == NULL);
37346 if (cclauses)
37348 cp_omp_split_clauses (loc, OMP_TASKLOOP, mask, clauses, cclauses);
37349 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TASKLOOP];
37352 sb = begin_omp_structured_block ();
37353 save = cp_parser_begin_omp_structured_block (parser);
37355 ret = cp_parser_omp_for_loop (parser, OMP_TASKLOOP, clauses, cclauses,
37356 if_p);
37358 cp_parser_end_omp_structured_block (parser, save);
37359 add_stmt (finish_omp_structured_block (sb));
37361 return ret;
37365 /* OpenACC 2.0:
37366 # pragma acc routine oacc-routine-clause[optseq] new-line
37367 function-definition
37369 # pragma acc routine ( name ) oacc-routine-clause[optseq] new-line
37372 #define OACC_ROUTINE_CLAUSE_MASK \
37373 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_GANG) \
37374 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WORKER) \
37375 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR) \
37376 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SEQ))
37379 /* Parse the OpenACC routine pragma. This has an optional '( name )'
37380 component, which must resolve to a declared namespace-scope
37381 function. The clauses are either processed directly (for a named
37382 function), or defered until the immediatley following declaration
37383 is parsed. */
37385 static void
37386 cp_parser_oacc_routine (cp_parser *parser, cp_token *pragma_tok,
37387 enum pragma_context context)
37389 gcc_checking_assert (context == pragma_external);
37390 /* The checking for "another pragma following this one" in the "no optional
37391 '( name )'" case makes sure that we dont re-enter. */
37392 gcc_checking_assert (parser->oacc_routine == NULL);
37394 cp_oacc_routine_data data;
37395 data.error_seen = false;
37396 data.fndecl_seen = false;
37397 data.tokens = vNULL;
37398 data.clauses = NULL_TREE;
37399 data.loc = pragma_tok->location;
37400 /* It is safe to take the address of a local variable; it will only be
37401 used while this scope is live. */
37402 parser->oacc_routine = &data;
37404 /* Look for optional '( name )'. */
37405 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
37407 cp_lexer_consume_token (parser->lexer); /* '(' */
37409 /* We parse the name as an id-expression. If it resolves to
37410 anything other than a non-overloaded function at namespace
37411 scope, it's an error. */
37412 location_t name_loc = cp_lexer_peek_token (parser->lexer)->location;
37413 tree name = cp_parser_id_expression (parser,
37414 /*template_keyword_p=*/false,
37415 /*check_dependency_p=*/false,
37416 /*template_p=*/NULL,
37417 /*declarator_p=*/false,
37418 /*optional_p=*/false);
37419 tree decl = cp_parser_lookup_name_simple (parser, name, name_loc);
37420 if (name != error_mark_node && decl == error_mark_node)
37421 cp_parser_name_lookup_error (parser, name, decl, NLE_NULL, name_loc);
37423 if (decl == error_mark_node
37424 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
37426 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37427 parser->oacc_routine = NULL;
37428 return;
37431 data.clauses
37432 = cp_parser_oacc_all_clauses (parser, OACC_ROUTINE_CLAUSE_MASK,
37433 "#pragma acc routine",
37434 cp_lexer_peek_token (parser->lexer));
37436 if (decl && is_overloaded_fn (decl)
37437 && (TREE_CODE (decl) != FUNCTION_DECL
37438 || DECL_FUNCTION_TEMPLATE_P (decl)))
37440 error_at (name_loc,
37441 "%<#pragma acc routine%> names a set of overloads");
37442 parser->oacc_routine = NULL;
37443 return;
37446 /* Perhaps we should use the same rule as declarations in different
37447 namespaces? */
37448 if (!DECL_NAMESPACE_SCOPE_P (decl))
37450 error_at (name_loc,
37451 "%qD does not refer to a namespace scope function", decl);
37452 parser->oacc_routine = NULL;
37453 return;
37456 if (TREE_CODE (decl) != FUNCTION_DECL)
37458 error_at (name_loc, "%qD does not refer to a function", decl);
37459 parser->oacc_routine = NULL;
37460 return;
37463 cp_finalize_oacc_routine (parser, decl, false);
37464 parser->oacc_routine = NULL;
37466 else /* No optional '( name )'. */
37468 /* Store away all pragma tokens. */
37469 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
37470 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
37471 cp_lexer_consume_token (parser->lexer);
37472 if (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
37473 parser->oacc_routine->error_seen = true;
37474 cp_parser_require_pragma_eol (parser, pragma_tok);
37475 struct cp_token_cache *cp
37476 = cp_token_cache_new (pragma_tok, cp_lexer_peek_token (parser->lexer));
37477 parser->oacc_routine->tokens.safe_push (cp);
37479 /* Emit a helpful diagnostic if there's another pragma following this
37480 one. */
37481 if (cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA))
37483 cp_ensure_no_oacc_routine (parser);
37484 data.tokens.release ();
37485 /* ..., and then just keep going. */
37486 return;
37489 /* We only have to consider the pragma_external case here. */
37490 cp_parser_declaration (parser);
37491 if (parser->oacc_routine
37492 && !parser->oacc_routine->fndecl_seen)
37493 cp_ensure_no_oacc_routine (parser);
37494 else
37495 parser->oacc_routine = NULL;
37496 data.tokens.release ();
37500 /* Finalize #pragma acc routine clauses after direct declarator has
37501 been parsed. */
37503 static tree
37504 cp_parser_late_parsing_oacc_routine (cp_parser *parser, tree attrs)
37506 struct cp_token_cache *ce;
37507 cp_oacc_routine_data *data = parser->oacc_routine;
37509 if (!data->error_seen && data->fndecl_seen)
37511 error_at (data->loc,
37512 "%<#pragma acc routine%> not immediately followed by "
37513 "a single function declaration or definition");
37514 data->error_seen = true;
37516 if (data->error_seen)
37517 return attrs;
37519 gcc_checking_assert (data->tokens.length () == 1);
37520 ce = data->tokens[0];
37522 cp_parser_push_lexer_for_tokens (parser, ce);
37523 parser->lexer->in_pragma = true;
37524 gcc_assert (cp_lexer_peek_token (parser->lexer)->type == CPP_PRAGMA);
37526 cp_token *pragma_tok = cp_lexer_consume_token (parser->lexer);
37527 gcc_checking_assert (parser->oacc_routine->clauses == NULL_TREE);
37528 parser->oacc_routine->clauses
37529 = cp_parser_oacc_all_clauses (parser, OACC_ROUTINE_CLAUSE_MASK,
37530 "#pragma acc routine", pragma_tok);
37531 cp_parser_pop_lexer (parser);
37532 /* Later, cp_finalize_oacc_routine will process the clauses, and then set
37533 fndecl_seen. */
37535 return attrs;
37538 /* Apply any saved OpenACC routine clauses to a just-parsed
37539 declaration. */
37541 static void
37542 cp_finalize_oacc_routine (cp_parser *parser, tree fndecl, bool is_defn)
37544 if (__builtin_expect (parser->oacc_routine != NULL, 0))
37546 /* Keep going if we're in error reporting mode. */
37547 if (parser->oacc_routine->error_seen
37548 || fndecl == error_mark_node)
37549 return;
37551 if (parser->oacc_routine->fndecl_seen)
37553 error_at (parser->oacc_routine->loc,
37554 "%<#pragma acc routine%> not immediately followed by"
37555 " a single function declaration or definition");
37556 parser->oacc_routine = NULL;
37557 return;
37559 if (TREE_CODE (fndecl) != FUNCTION_DECL)
37561 cp_ensure_no_oacc_routine (parser);
37562 return;
37565 if (oacc_get_fn_attrib (fndecl))
37567 error_at (parser->oacc_routine->loc,
37568 "%<#pragma acc routine%> already applied to %qD", fndecl);
37569 parser->oacc_routine = NULL;
37570 return;
37573 if (TREE_USED (fndecl) || (!is_defn && DECL_SAVED_TREE (fndecl)))
37575 error_at (parser->oacc_routine->loc,
37576 TREE_USED (fndecl)
37577 ? G_("%<#pragma acc routine%> must be applied before use")
37578 : G_("%<#pragma acc routine%> must be applied before "
37579 "definition"));
37580 parser->oacc_routine = NULL;
37581 return;
37584 /* Process the routine's dimension clauses. */
37585 tree dims = oacc_build_routine_dims (parser->oacc_routine->clauses);
37586 oacc_replace_fn_attrib (fndecl, dims);
37588 /* Add an "omp declare target" attribute. */
37589 DECL_ATTRIBUTES (fndecl)
37590 = tree_cons (get_identifier ("omp declare target"),
37591 NULL_TREE, DECL_ATTRIBUTES (fndecl));
37593 /* Don't unset parser->oacc_routine here: we may still need it to
37594 diagnose wrong usage. But, remember that we've used this "#pragma acc
37595 routine". */
37596 parser->oacc_routine->fndecl_seen = true;
37600 /* Main entry point to OpenMP statement pragmas. */
37602 static void
37603 cp_parser_omp_construct (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
37605 tree stmt;
37606 char p_name[sizeof "#pragma omp teams distribute parallel for simd"];
37607 omp_clause_mask mask (0);
37609 switch (cp_parser_pragma_kind (pragma_tok))
37611 case PRAGMA_OACC_ATOMIC:
37612 cp_parser_omp_atomic (parser, pragma_tok);
37613 return;
37614 case PRAGMA_OACC_CACHE:
37615 stmt = cp_parser_oacc_cache (parser, pragma_tok);
37616 break;
37617 case PRAGMA_OACC_DATA:
37618 stmt = cp_parser_oacc_data (parser, pragma_tok, if_p);
37619 break;
37620 case PRAGMA_OACC_ENTER_DATA:
37621 stmt = cp_parser_oacc_enter_exit_data (parser, pragma_tok, true);
37622 break;
37623 case PRAGMA_OACC_EXIT_DATA:
37624 stmt = cp_parser_oacc_enter_exit_data (parser, pragma_tok, false);
37625 break;
37626 case PRAGMA_OACC_HOST_DATA:
37627 stmt = cp_parser_oacc_host_data (parser, pragma_tok, if_p);
37628 break;
37629 case PRAGMA_OACC_KERNELS:
37630 case PRAGMA_OACC_PARALLEL:
37631 strcpy (p_name, "#pragma acc");
37632 stmt = cp_parser_oacc_kernels_parallel (parser, pragma_tok, p_name,
37633 if_p);
37634 break;
37635 case PRAGMA_OACC_LOOP:
37636 strcpy (p_name, "#pragma acc");
37637 stmt = cp_parser_oacc_loop (parser, pragma_tok, p_name, mask, NULL,
37638 if_p);
37639 break;
37640 case PRAGMA_OACC_UPDATE:
37641 stmt = cp_parser_oacc_update (parser, pragma_tok);
37642 break;
37643 case PRAGMA_OACC_WAIT:
37644 stmt = cp_parser_oacc_wait (parser, pragma_tok);
37645 break;
37646 case PRAGMA_OMP_ATOMIC:
37647 cp_parser_omp_atomic (parser, pragma_tok);
37648 return;
37649 case PRAGMA_OMP_CRITICAL:
37650 stmt = cp_parser_omp_critical (parser, pragma_tok, if_p);
37651 break;
37652 case PRAGMA_OMP_DISTRIBUTE:
37653 strcpy (p_name, "#pragma omp");
37654 stmt = cp_parser_omp_distribute (parser, pragma_tok, p_name, mask, NULL,
37655 if_p);
37656 break;
37657 case PRAGMA_OMP_FOR:
37658 strcpy (p_name, "#pragma omp");
37659 stmt = cp_parser_omp_for (parser, pragma_tok, p_name, mask, NULL,
37660 if_p);
37661 break;
37662 case PRAGMA_OMP_MASTER:
37663 stmt = cp_parser_omp_master (parser, pragma_tok, if_p);
37664 break;
37665 case PRAGMA_OMP_PARALLEL:
37666 strcpy (p_name, "#pragma omp");
37667 stmt = cp_parser_omp_parallel (parser, pragma_tok, p_name, mask, NULL,
37668 if_p);
37669 break;
37670 case PRAGMA_OMP_SECTIONS:
37671 strcpy (p_name, "#pragma omp");
37672 stmt = cp_parser_omp_sections (parser, pragma_tok, p_name, mask, NULL);
37673 break;
37674 case PRAGMA_OMP_SIMD:
37675 strcpy (p_name, "#pragma omp");
37676 stmt = cp_parser_omp_simd (parser, pragma_tok, p_name, mask, NULL,
37677 if_p);
37678 break;
37679 case PRAGMA_OMP_SINGLE:
37680 stmt = cp_parser_omp_single (parser, pragma_tok, if_p);
37681 break;
37682 case PRAGMA_OMP_TASK:
37683 stmt = cp_parser_omp_task (parser, pragma_tok, if_p);
37684 break;
37685 case PRAGMA_OMP_TASKGROUP:
37686 stmt = cp_parser_omp_taskgroup (parser, pragma_tok, if_p);
37687 break;
37688 case PRAGMA_OMP_TASKLOOP:
37689 strcpy (p_name, "#pragma omp");
37690 stmt = cp_parser_omp_taskloop (parser, pragma_tok, p_name, mask, NULL,
37691 if_p);
37692 break;
37693 case PRAGMA_OMP_TEAMS:
37694 strcpy (p_name, "#pragma omp");
37695 stmt = cp_parser_omp_teams (parser, pragma_tok, p_name, mask, NULL,
37696 if_p);
37697 break;
37698 default:
37699 gcc_unreachable ();
37702 protected_set_expr_location (stmt, pragma_tok->location);
37705 /* Transactional Memory parsing routines. */
37707 /* Parse a transaction attribute.
37709 txn-attribute:
37710 attribute
37711 [ [ identifier ] ]
37713 We use this instead of cp_parser_attributes_opt for transactions to avoid
37714 the pedwarn in C++98 mode. */
37716 static tree
37717 cp_parser_txn_attribute_opt (cp_parser *parser)
37719 cp_token *token;
37720 tree attr_name, attr = NULL;
37722 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
37723 return cp_parser_attributes_opt (parser);
37725 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
37726 return NULL_TREE;
37727 cp_lexer_consume_token (parser->lexer);
37728 if (!cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE))
37729 goto error1;
37731 token = cp_lexer_peek_token (parser->lexer);
37732 if (token->type == CPP_NAME || token->type == CPP_KEYWORD)
37734 token = cp_lexer_consume_token (parser->lexer);
37736 attr_name = (token->type == CPP_KEYWORD
37737 /* For keywords, use the canonical spelling,
37738 not the parsed identifier. */
37739 ? ridpointers[(int) token->keyword]
37740 : token->u.value);
37741 attr = build_tree_list (attr_name, NULL_TREE);
37743 else
37744 cp_parser_error (parser, "expected identifier");
37746 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
37747 error1:
37748 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
37749 return attr;
37752 /* Parse a __transaction_atomic or __transaction_relaxed statement.
37754 transaction-statement:
37755 __transaction_atomic txn-attribute[opt] txn-noexcept-spec[opt]
37756 compound-statement
37757 __transaction_relaxed txn-noexcept-spec[opt] compound-statement
37760 static tree
37761 cp_parser_transaction (cp_parser *parser, cp_token *token)
37763 unsigned char old_in = parser->in_transaction;
37764 unsigned char this_in = 1, new_in;
37765 enum rid keyword = token->keyword;
37766 tree stmt, attrs, noex;
37768 cp_lexer_consume_token (parser->lexer);
37770 if (keyword == RID_TRANSACTION_RELAXED
37771 || keyword == RID_SYNCHRONIZED)
37772 this_in |= TM_STMT_ATTR_RELAXED;
37773 else
37775 attrs = cp_parser_txn_attribute_opt (parser);
37776 if (attrs)
37777 this_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
37780 /* Parse a noexcept specification. */
37781 if (keyword == RID_ATOMIC_NOEXCEPT)
37782 noex = boolean_true_node;
37783 else if (keyword == RID_ATOMIC_CANCEL)
37785 /* cancel-and-throw is unimplemented. */
37786 sorry ("atomic_cancel");
37787 noex = NULL_TREE;
37789 else
37790 noex = cp_parser_noexcept_specification_opt (parser, true, NULL, true);
37792 /* Keep track if we're in the lexical scope of an outer transaction. */
37793 new_in = this_in | (old_in & TM_STMT_ATTR_OUTER);
37795 stmt = begin_transaction_stmt (token->location, NULL, this_in);
37797 parser->in_transaction = new_in;
37798 cp_parser_compound_statement (parser, NULL, BCS_TRANSACTION, false);
37799 parser->in_transaction = old_in;
37801 finish_transaction_stmt (stmt, NULL, this_in, noex);
37803 return stmt;
37806 /* Parse a __transaction_atomic or __transaction_relaxed expression.
37808 transaction-expression:
37809 __transaction_atomic txn-noexcept-spec[opt] ( expression )
37810 __transaction_relaxed txn-noexcept-spec[opt] ( expression )
37813 static tree
37814 cp_parser_transaction_expression (cp_parser *parser, enum rid keyword)
37816 unsigned char old_in = parser->in_transaction;
37817 unsigned char this_in = 1;
37818 cp_token *token;
37819 tree expr, noex;
37820 bool noex_expr;
37821 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
37823 gcc_assert (keyword == RID_TRANSACTION_ATOMIC
37824 || keyword == RID_TRANSACTION_RELAXED);
37826 if (!flag_tm)
37827 error_at (loc,
37828 keyword == RID_TRANSACTION_RELAXED
37829 ? G_("%<__transaction_relaxed%> without transactional memory "
37830 "support enabled")
37831 : G_("%<__transaction_atomic%> without transactional memory "
37832 "support enabled"));
37834 token = cp_parser_require_keyword (parser, keyword,
37835 (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
37836 : RT_TRANSACTION_RELAXED));
37837 gcc_assert (token != NULL);
37839 if (keyword == RID_TRANSACTION_RELAXED)
37840 this_in |= TM_STMT_ATTR_RELAXED;
37842 /* Set this early. This might mean that we allow transaction_cancel in
37843 an expression that we find out later actually has to be a constexpr.
37844 However, we expect that cxx_constant_value will be able to deal with
37845 this; also, if the noexcept has no constexpr, then what we parse next
37846 really is a transaction's body. */
37847 parser->in_transaction = this_in;
37849 /* Parse a noexcept specification. */
37850 noex = cp_parser_noexcept_specification_opt (parser, false, &noex_expr,
37851 true);
37853 if (!noex || !noex_expr
37854 || cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
37856 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
37858 expr = cp_parser_expression (parser);
37859 expr = finish_parenthesized_expr (expr);
37861 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
37863 else
37865 /* The only expression that is available got parsed for the noexcept
37866 already. noexcept is true then. */
37867 expr = noex;
37868 noex = boolean_true_node;
37871 expr = build_transaction_expr (token->location, expr, this_in, noex);
37872 parser->in_transaction = old_in;
37874 if (cp_parser_non_integral_constant_expression (parser, NIC_TRANSACTION))
37875 return error_mark_node;
37877 return (flag_tm ? expr : error_mark_node);
37880 /* Parse a function-transaction-block.
37882 function-transaction-block:
37883 __transaction_atomic txn-attribute[opt] ctor-initializer[opt]
37884 function-body
37885 __transaction_atomic txn-attribute[opt] function-try-block
37886 __transaction_relaxed ctor-initializer[opt] function-body
37887 __transaction_relaxed function-try-block
37890 static bool
37891 cp_parser_function_transaction (cp_parser *parser, enum rid keyword)
37893 unsigned char old_in = parser->in_transaction;
37894 unsigned char new_in = 1;
37895 tree compound_stmt, stmt, attrs;
37896 bool ctor_initializer_p;
37897 cp_token *token;
37899 gcc_assert (keyword == RID_TRANSACTION_ATOMIC
37900 || keyword == RID_TRANSACTION_RELAXED);
37901 token = cp_parser_require_keyword (parser, keyword,
37902 (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
37903 : RT_TRANSACTION_RELAXED));
37904 gcc_assert (token != NULL);
37906 if (keyword == RID_TRANSACTION_RELAXED)
37907 new_in |= TM_STMT_ATTR_RELAXED;
37908 else
37910 attrs = cp_parser_txn_attribute_opt (parser);
37911 if (attrs)
37912 new_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
37915 stmt = begin_transaction_stmt (token->location, &compound_stmt, new_in);
37917 parser->in_transaction = new_in;
37919 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
37920 ctor_initializer_p = cp_parser_function_try_block (parser);
37921 else
37922 ctor_initializer_p = cp_parser_ctor_initializer_opt_and_function_body
37923 (parser, /*in_function_try_block=*/false);
37925 parser->in_transaction = old_in;
37927 finish_transaction_stmt (stmt, compound_stmt, new_in, NULL_TREE);
37929 return ctor_initializer_p;
37932 /* Parse a __transaction_cancel statement.
37934 cancel-statement:
37935 __transaction_cancel txn-attribute[opt] ;
37936 __transaction_cancel txn-attribute[opt] throw-expression ;
37938 ??? Cancel and throw is not yet implemented. */
37940 static tree
37941 cp_parser_transaction_cancel (cp_parser *parser)
37943 cp_token *token;
37944 bool is_outer = false;
37945 tree stmt, attrs;
37947 token = cp_parser_require_keyword (parser, RID_TRANSACTION_CANCEL,
37948 RT_TRANSACTION_CANCEL);
37949 gcc_assert (token != NULL);
37951 attrs = cp_parser_txn_attribute_opt (parser);
37952 if (attrs)
37953 is_outer = (parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER) != 0);
37955 /* ??? Parse cancel-and-throw here. */
37957 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
37959 if (!flag_tm)
37961 error_at (token->location, "%<__transaction_cancel%> without "
37962 "transactional memory support enabled");
37963 return error_mark_node;
37965 else if (parser->in_transaction & TM_STMT_ATTR_RELAXED)
37967 error_at (token->location, "%<__transaction_cancel%> within a "
37968 "%<__transaction_relaxed%>");
37969 return error_mark_node;
37971 else if (is_outer)
37973 if ((parser->in_transaction & TM_STMT_ATTR_OUTER) == 0
37974 && !is_tm_may_cancel_outer (current_function_decl))
37976 error_at (token->location, "outer %<__transaction_cancel%> not "
37977 "within outer %<__transaction_atomic%>");
37978 error_at (token->location,
37979 " or a %<transaction_may_cancel_outer%> function");
37980 return error_mark_node;
37983 else if (parser->in_transaction == 0)
37985 error_at (token->location, "%<__transaction_cancel%> not within "
37986 "%<__transaction_atomic%>");
37987 return error_mark_node;
37990 stmt = build_tm_abort_call (token->location, is_outer);
37991 add_stmt (stmt);
37993 return stmt;
37996 /* The parser. */
37998 static GTY (()) cp_parser *the_parser;
38001 /* Special handling for the first token or line in the file. The first
38002 thing in the file might be #pragma GCC pch_preprocess, which loads a
38003 PCH file, which is a GC collection point. So we need to handle this
38004 first pragma without benefit of an existing lexer structure.
38006 Always returns one token to the caller in *FIRST_TOKEN. This is
38007 either the true first token of the file, or the first token after
38008 the initial pragma. */
38010 static void
38011 cp_parser_initial_pragma (cp_token *first_token)
38013 tree name = NULL;
38015 cp_lexer_get_preprocessor_token (NULL, first_token);
38016 if (cp_parser_pragma_kind (first_token) != PRAGMA_GCC_PCH_PREPROCESS)
38017 return;
38019 cp_lexer_get_preprocessor_token (NULL, first_token);
38020 if (first_token->type == CPP_STRING)
38022 name = first_token->u.value;
38024 cp_lexer_get_preprocessor_token (NULL, first_token);
38025 if (first_token->type != CPP_PRAGMA_EOL)
38026 error_at (first_token->location,
38027 "junk at end of %<#pragma GCC pch_preprocess%>");
38029 else
38030 error_at (first_token->location, "expected string literal");
38032 /* Skip to the end of the pragma. */
38033 while (first_token->type != CPP_PRAGMA_EOL && first_token->type != CPP_EOF)
38034 cp_lexer_get_preprocessor_token (NULL, first_token);
38036 /* Now actually load the PCH file. */
38037 if (name)
38038 c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
38040 /* Read one more token to return to our caller. We have to do this
38041 after reading the PCH file in, since its pointers have to be
38042 live. */
38043 cp_lexer_get_preprocessor_token (NULL, first_token);
38046 /* Parses the grainsize pragma for the _Cilk_for statement.
38047 Syntax:
38048 #pragma cilk grainsize = <VALUE>. */
38050 static void
38051 cp_parser_cilk_grainsize (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
38053 if (cp_parser_require (parser, CPP_EQ, RT_EQ))
38055 tree exp = cp_parser_binary_expression (parser, false, false,
38056 PREC_NOT_OPERATOR, NULL);
38057 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
38058 if (!exp || exp == error_mark_node)
38060 error_at (pragma_tok->location, "invalid grainsize for _Cilk_for");
38061 return;
38064 /* Make sure the next token is _Cilk_for, it is invalid otherwise. */
38065 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CILK_FOR))
38066 cp_parser_cilk_for (parser, exp, if_p);
38067 else
38068 warning_at (cp_lexer_peek_token (parser->lexer)->location, 0,
38069 "%<#pragma cilk grainsize%> is not followed by "
38070 "%<_Cilk_for%>");
38071 return;
38073 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
38076 /* Normal parsing of a pragma token. Here we can (and must) use the
38077 regular lexer. */
38079 static bool
38080 cp_parser_pragma (cp_parser *parser, enum pragma_context context, bool *if_p)
38082 cp_token *pragma_tok;
38083 unsigned int id;
38084 tree stmt;
38085 bool ret;
38087 pragma_tok = cp_lexer_consume_token (parser->lexer);
38088 gcc_assert (pragma_tok->type == CPP_PRAGMA);
38089 parser->lexer->in_pragma = true;
38091 id = cp_parser_pragma_kind (pragma_tok);
38092 if (id != PRAGMA_OMP_DECLARE && id != PRAGMA_OACC_ROUTINE)
38093 cp_ensure_no_omp_declare_simd (parser);
38094 switch (id)
38096 case PRAGMA_GCC_PCH_PREPROCESS:
38097 error_at (pragma_tok->location,
38098 "%<#pragma GCC pch_preprocess%> must be first");
38099 break;
38101 case PRAGMA_OMP_BARRIER:
38102 switch (context)
38104 case pragma_compound:
38105 cp_parser_omp_barrier (parser, pragma_tok);
38106 return false;
38107 case pragma_stmt:
38108 error_at (pragma_tok->location, "%<#pragma omp barrier%> may only be "
38109 "used in compound statements");
38110 break;
38111 default:
38112 goto bad_stmt;
38114 break;
38116 case PRAGMA_OMP_FLUSH:
38117 switch (context)
38119 case pragma_compound:
38120 cp_parser_omp_flush (parser, pragma_tok);
38121 return false;
38122 case pragma_stmt:
38123 error_at (pragma_tok->location, "%<#pragma omp flush%> may only be "
38124 "used in compound statements");
38125 break;
38126 default:
38127 goto bad_stmt;
38129 break;
38131 case PRAGMA_OMP_TASKWAIT:
38132 switch (context)
38134 case pragma_compound:
38135 cp_parser_omp_taskwait (parser, pragma_tok);
38136 return false;
38137 case pragma_stmt:
38138 error_at (pragma_tok->location,
38139 "%<#pragma omp taskwait%> may only be "
38140 "used in compound statements");
38141 break;
38142 default:
38143 goto bad_stmt;
38145 break;
38147 case PRAGMA_OMP_TASKYIELD:
38148 switch (context)
38150 case pragma_compound:
38151 cp_parser_omp_taskyield (parser, pragma_tok);
38152 return false;
38153 case pragma_stmt:
38154 error_at (pragma_tok->location,
38155 "%<#pragma omp taskyield%> may only be "
38156 "used in compound statements");
38157 break;
38158 default:
38159 goto bad_stmt;
38161 break;
38163 case PRAGMA_OMP_CANCEL:
38164 switch (context)
38166 case pragma_compound:
38167 cp_parser_omp_cancel (parser, pragma_tok);
38168 return false;
38169 case pragma_stmt:
38170 error_at (pragma_tok->location,
38171 "%<#pragma omp cancel%> may only be "
38172 "used in compound statements");
38173 break;
38174 default:
38175 goto bad_stmt;
38177 break;
38179 case PRAGMA_OMP_CANCELLATION_POINT:
38180 cp_parser_omp_cancellation_point (parser, pragma_tok, context);
38181 return false;
38183 case PRAGMA_OMP_THREADPRIVATE:
38184 cp_parser_omp_threadprivate (parser, pragma_tok);
38185 return false;
38187 case PRAGMA_OMP_DECLARE:
38188 cp_parser_omp_declare (parser, pragma_tok, context);
38189 return false;
38191 case PRAGMA_OACC_DECLARE:
38192 cp_parser_oacc_declare (parser, pragma_tok);
38193 return false;
38195 case PRAGMA_OACC_ENTER_DATA:
38196 if (context == pragma_stmt)
38198 cp_parser_error (parser, "%<#pragma acc enter data%> may only be "
38199 "used in compound statements");
38200 break;
38202 else if (context != pragma_compound)
38203 goto bad_stmt;
38204 cp_parser_omp_construct (parser, pragma_tok, if_p);
38205 return true;
38207 case PRAGMA_OACC_EXIT_DATA:
38208 if (context == pragma_stmt)
38210 cp_parser_error (parser, "%<#pragma acc exit data%> may only be "
38211 "used in compound statements");
38212 break;
38214 else if (context != pragma_compound)
38215 goto bad_stmt;
38216 cp_parser_omp_construct (parser, pragma_tok, if_p);
38217 return true;
38219 case PRAGMA_OACC_ROUTINE:
38220 if (context != pragma_external)
38222 error_at (pragma_tok->location,
38223 "%<#pragma acc routine%> must be at file scope");
38224 break;
38226 cp_parser_oacc_routine (parser, pragma_tok, context);
38227 return false;
38229 case PRAGMA_OACC_UPDATE:
38230 if (context == pragma_stmt)
38232 cp_parser_error (parser, "%<#pragma acc update%> may only be "
38233 "used in compound statements");
38234 break;
38236 else if (context != pragma_compound)
38237 goto bad_stmt;
38238 cp_parser_omp_construct (parser, pragma_tok, if_p);
38239 return true;
38241 case PRAGMA_OACC_WAIT:
38242 if (context == pragma_stmt)
38244 cp_parser_error (parser, "%<#pragma acc wait%> may only be "
38245 "used in compound statements");
38246 break;
38248 else if (context != pragma_compound)
38249 goto bad_stmt;
38250 cp_parser_omp_construct (parser, pragma_tok, if_p);
38251 return true;
38253 case PRAGMA_OACC_ATOMIC:
38254 case PRAGMA_OACC_CACHE:
38255 case PRAGMA_OACC_DATA:
38256 case PRAGMA_OACC_HOST_DATA:
38257 case PRAGMA_OACC_KERNELS:
38258 case PRAGMA_OACC_PARALLEL:
38259 case PRAGMA_OACC_LOOP:
38260 case PRAGMA_OMP_ATOMIC:
38261 case PRAGMA_OMP_CRITICAL:
38262 case PRAGMA_OMP_DISTRIBUTE:
38263 case PRAGMA_OMP_FOR:
38264 case PRAGMA_OMP_MASTER:
38265 case PRAGMA_OMP_PARALLEL:
38266 case PRAGMA_OMP_SECTIONS:
38267 case PRAGMA_OMP_SIMD:
38268 case PRAGMA_OMP_SINGLE:
38269 case PRAGMA_OMP_TASK:
38270 case PRAGMA_OMP_TASKGROUP:
38271 case PRAGMA_OMP_TASKLOOP:
38272 case PRAGMA_OMP_TEAMS:
38273 if (context != pragma_stmt && context != pragma_compound)
38274 goto bad_stmt;
38275 stmt = push_omp_privatization_clauses (false);
38276 cp_parser_omp_construct (parser, pragma_tok, if_p);
38277 pop_omp_privatization_clauses (stmt);
38278 return true;
38280 case PRAGMA_OMP_ORDERED:
38281 if (context != pragma_stmt && context != pragma_compound)
38282 goto bad_stmt;
38283 stmt = push_omp_privatization_clauses (false);
38284 ret = cp_parser_omp_ordered (parser, pragma_tok, context, if_p);
38285 pop_omp_privatization_clauses (stmt);
38286 return ret;
38288 case PRAGMA_OMP_TARGET:
38289 if (context != pragma_stmt && context != pragma_compound)
38290 goto bad_stmt;
38291 stmt = push_omp_privatization_clauses (false);
38292 ret = cp_parser_omp_target (parser, pragma_tok, context, if_p);
38293 pop_omp_privatization_clauses (stmt);
38294 return ret;
38296 case PRAGMA_OMP_END_DECLARE_TARGET:
38297 cp_parser_omp_end_declare_target (parser, pragma_tok);
38298 return false;
38300 case PRAGMA_OMP_SECTION:
38301 error_at (pragma_tok->location,
38302 "%<#pragma omp section%> may only be used in "
38303 "%<#pragma omp sections%> construct");
38304 break;
38306 case PRAGMA_IVDEP:
38308 if (context == pragma_external)
38310 error_at (pragma_tok->location,
38311 "%<#pragma GCC ivdep%> must be inside a function");
38312 break;
38314 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
38315 cp_token *tok;
38316 tok = cp_lexer_peek_token (the_parser->lexer);
38317 if (tok->type != CPP_KEYWORD
38318 || (tok->keyword != RID_FOR && tok->keyword != RID_WHILE
38319 && tok->keyword != RID_DO))
38321 cp_parser_error (parser, "for, while or do statement expected");
38322 return false;
38324 cp_parser_iteration_statement (parser, if_p, true);
38325 return true;
38328 case PRAGMA_CILK_SIMD:
38329 if (context == pragma_external)
38331 error_at (pragma_tok->location,
38332 "%<#pragma simd%> must be inside a function");
38333 break;
38335 stmt = push_omp_privatization_clauses (false);
38336 cp_parser_cilk_simd (parser, pragma_tok, if_p);
38337 pop_omp_privatization_clauses (stmt);
38338 return true;
38340 case PRAGMA_CILK_GRAINSIZE:
38341 if (context == pragma_external)
38343 error_at (pragma_tok->location,
38344 "%<#pragma cilk grainsize%> must be inside a function");
38345 break;
38348 /* Ignore the pragma if Cilk Plus is not enabled. */
38349 if (flag_cilkplus)
38351 cp_parser_cilk_grainsize (parser, pragma_tok, if_p);
38352 return true;
38354 else
38356 error_at (pragma_tok->location, "-fcilkplus must be enabled to use "
38357 "%<#pragma cilk grainsize%>");
38358 break;
38361 default:
38362 gcc_assert (id >= PRAGMA_FIRST_EXTERNAL);
38363 c_invoke_pragma_handler (id);
38364 break;
38366 bad_stmt:
38367 cp_parser_error (parser, "expected declaration specifiers");
38368 break;
38371 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
38372 return false;
38375 /* The interface the pragma parsers have to the lexer. */
38377 enum cpp_ttype
38378 pragma_lex (tree *value, location_t *loc)
38380 cp_token *tok = cp_lexer_peek_token (the_parser->lexer);
38381 enum cpp_ttype ret = tok->type;
38383 *value = tok->u.value;
38384 if (loc)
38385 *loc = tok->location;
38387 if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
38388 ret = CPP_EOF;
38389 else if (ret == CPP_STRING)
38390 *value = cp_parser_string_literal (the_parser, false, false);
38391 else
38393 if (ret == CPP_KEYWORD)
38394 ret = CPP_NAME;
38395 cp_lexer_consume_token (the_parser->lexer);
38398 return ret;
38402 /* External interface. */
38404 /* Parse one entire translation unit. */
38406 void
38407 c_parse_file (void)
38409 static bool already_called = false;
38411 if (already_called)
38412 fatal_error (input_location,
38413 "inter-module optimizations not implemented for C++");
38414 already_called = true;
38416 the_parser = cp_parser_new ();
38417 push_deferring_access_checks (flag_access_control
38418 ? dk_no_deferred : dk_no_check);
38419 cp_parser_translation_unit (the_parser);
38420 the_parser = NULL;
38423 /* Parses the Cilk Plus #pragma simd and SIMD-enabled function attribute's
38424 vectorlength clause:
38425 Syntax:
38426 vectorlength ( constant-expression ) */
38428 static tree
38429 cp_parser_cilk_simd_vectorlength (cp_parser *parser, tree clauses,
38430 bool is_simd_fn)
38432 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
38433 tree expr;
38434 /* The vectorlength clause in #pragma simd behaves exactly like OpenMP's
38435 safelen clause. Thus, vectorlength is represented as OMP 4.0
38436 safelen. For SIMD-enabled function it is represented by OMP 4.0
38437 simdlen. */
38438 if (!is_simd_fn)
38439 check_no_duplicate_clause (clauses, OMP_CLAUSE_SAFELEN, "vectorlength",
38440 loc);
38441 else
38442 check_no_duplicate_clause (clauses, OMP_CLAUSE_SIMDLEN, "vectorlength",
38443 loc);
38445 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
38446 return error_mark_node;
38448 expr = cp_parser_constant_expression (parser);
38449 expr = maybe_constant_value (expr);
38451 /* If expr == error_mark_node, then don't emit any errors nor
38452 create a clause. if any of the above functions returns
38453 error mark node then they would have emitted an error message. */
38454 if (expr == error_mark_node)
38456 else if (!TREE_TYPE (expr)
38457 || !TREE_CONSTANT (expr)
38458 || !INTEGRAL_TYPE_P (TREE_TYPE (expr)))
38459 error_at (loc, "vectorlength must be an integer constant");
38460 else if (TREE_CONSTANT (expr)
38461 && !pow2p_hwi (TREE_INT_CST_LOW (expr)))
38462 error_at (loc, "vectorlength must be a power of 2");
38463 else
38465 tree c;
38466 if (!is_simd_fn)
38468 c = build_omp_clause (loc, OMP_CLAUSE_SAFELEN);
38469 OMP_CLAUSE_SAFELEN_EXPR (c) = expr;
38470 OMP_CLAUSE_CHAIN (c) = clauses;
38471 clauses = c;
38473 else
38475 c = build_omp_clause (loc, OMP_CLAUSE_SIMDLEN);
38476 OMP_CLAUSE_SIMDLEN_EXPR (c) = expr;
38477 OMP_CLAUSE_CHAIN (c) = clauses;
38478 clauses = c;
38482 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
38483 return error_mark_node;
38484 return clauses;
38487 /* Handles the Cilk Plus #pragma simd linear clause.
38488 Syntax:
38489 linear ( simd-linear-variable-list )
38491 simd-linear-variable-list:
38492 simd-linear-variable
38493 simd-linear-variable-list , simd-linear-variable
38495 simd-linear-variable:
38496 id-expression
38497 id-expression : simd-linear-step
38499 simd-linear-step:
38500 conditional-expression */
38502 static tree
38503 cp_parser_cilk_simd_linear (cp_parser *parser, tree clauses)
38505 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
38507 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
38508 return clauses;
38509 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
38511 cp_parser_error (parser, "expected identifier");
38512 cp_parser_skip_to_closing_parenthesis (parser, false, false, true);
38513 return error_mark_node;
38516 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
38517 parser->colon_corrects_to_scope_p = false;
38518 while (1)
38520 cp_token *token = cp_lexer_peek_token (parser->lexer);
38521 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
38523 cp_parser_error (parser, "expected variable-name");
38524 clauses = error_mark_node;
38525 break;
38528 tree var_name = cp_parser_id_expression (parser, false, true, NULL,
38529 false, false);
38530 tree decl = cp_parser_lookup_name_simple (parser, var_name,
38531 token->location);
38532 if (decl == error_mark_node)
38534 cp_parser_name_lookup_error (parser, var_name, decl, NLE_NULL,
38535 token->location);
38536 clauses = error_mark_node;
38538 else
38540 tree e = NULL_TREE;
38541 tree step_size = integer_one_node;
38543 /* If present, parse the linear step. Otherwise, assume the default
38544 value of 1. */
38545 if (cp_lexer_peek_token (parser->lexer)->type == CPP_COLON)
38547 cp_lexer_consume_token (parser->lexer);
38549 e = cp_parser_assignment_expression (parser);
38550 e = maybe_constant_value (e);
38552 if (e == error_mark_node)
38554 /* If an error has occurred, then the whole pragma is
38555 considered ill-formed. Thus, no reason to keep
38556 parsing. */
38557 clauses = error_mark_node;
38558 break;
38560 else if (type_dependent_expression_p (e)
38561 || value_dependent_expression_p (e)
38562 || (TREE_TYPE (e)
38563 && INTEGRAL_TYPE_P (TREE_TYPE (e))
38564 && (TREE_CONSTANT (e)
38565 || DECL_P (e))))
38566 step_size = e;
38567 else
38568 cp_parser_error (parser,
38569 "step size must be an integer constant "
38570 "expression or an integer variable");
38573 /* Use the OMP_CLAUSE_LINEAR, which has the same semantics. */
38574 tree l = build_omp_clause (loc, OMP_CLAUSE_LINEAR);
38575 OMP_CLAUSE_DECL (l) = decl;
38576 OMP_CLAUSE_LINEAR_STEP (l) = step_size;
38577 OMP_CLAUSE_CHAIN (l) = clauses;
38578 clauses = l;
38580 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
38581 cp_lexer_consume_token (parser->lexer);
38582 else if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
38583 break;
38584 else
38586 error_at (cp_lexer_peek_token (parser->lexer)->location,
38587 "expected %<,%> or %<)%> after %qE", decl);
38588 clauses = error_mark_node;
38589 break;
38592 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
38593 cp_parser_skip_to_closing_parenthesis (parser, false, false, true);
38594 return clauses;
38597 /* Returns the name of the next clause. If the clause is not
38598 recognized, then PRAGMA_CILK_CLAUSE_NONE is returned and the next
38599 token is not consumed. Otherwise, the appropriate enum from the
38600 pragma_simd_clause is returned and the token is consumed. */
38602 static pragma_omp_clause
38603 cp_parser_cilk_simd_clause_name (cp_parser *parser)
38605 pragma_omp_clause clause_type;
38606 cp_token *token = cp_lexer_peek_token (parser->lexer);
38608 if (token->keyword == RID_PRIVATE)
38609 clause_type = PRAGMA_CILK_CLAUSE_PRIVATE;
38610 else if (!token->u.value || token->type != CPP_NAME)
38611 return PRAGMA_CILK_CLAUSE_NONE;
38612 else if (!strcmp (IDENTIFIER_POINTER (token->u.value), "vectorlength"))
38613 clause_type = PRAGMA_CILK_CLAUSE_VECTORLENGTH;
38614 else if (!strcmp (IDENTIFIER_POINTER (token->u.value), "linear"))
38615 clause_type = PRAGMA_CILK_CLAUSE_LINEAR;
38616 else if (!strcmp (IDENTIFIER_POINTER (token->u.value), "firstprivate"))
38617 clause_type = PRAGMA_CILK_CLAUSE_FIRSTPRIVATE;
38618 else if (!strcmp (IDENTIFIER_POINTER (token->u.value), "lastprivate"))
38619 clause_type = PRAGMA_CILK_CLAUSE_LASTPRIVATE;
38620 else if (!strcmp (IDENTIFIER_POINTER (token->u.value), "reduction"))
38621 clause_type = PRAGMA_CILK_CLAUSE_REDUCTION;
38622 else
38623 return PRAGMA_CILK_CLAUSE_NONE;
38625 cp_lexer_consume_token (parser->lexer);
38626 return clause_type;
38629 /* Parses all the #pragma simd clauses. Returns a list of clauses found. */
38631 static tree
38632 cp_parser_cilk_simd_all_clauses (cp_parser *parser, cp_token *pragma_token)
38634 tree clauses = NULL_TREE;
38636 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
38637 && clauses != error_mark_node)
38639 pragma_omp_clause c_kind;
38640 c_kind = cp_parser_cilk_simd_clause_name (parser);
38641 if (c_kind == PRAGMA_CILK_CLAUSE_VECTORLENGTH)
38642 clauses = cp_parser_cilk_simd_vectorlength (parser, clauses, false);
38643 else if (c_kind == PRAGMA_CILK_CLAUSE_LINEAR)
38644 clauses = cp_parser_cilk_simd_linear (parser, clauses);
38645 else if (c_kind == PRAGMA_CILK_CLAUSE_PRIVATE)
38646 /* Use the OpenMP 4.0 equivalent function. */
38647 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE, clauses);
38648 else if (c_kind == PRAGMA_CILK_CLAUSE_FIRSTPRIVATE)
38649 /* Use the OpenMP 4.0 equivalent function. */
38650 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
38651 clauses);
38652 else if (c_kind == PRAGMA_CILK_CLAUSE_LASTPRIVATE)
38653 /* Use the OMP 4.0 equivalent function. */
38654 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LASTPRIVATE,
38655 clauses);
38656 else if (c_kind == PRAGMA_CILK_CLAUSE_REDUCTION)
38657 /* Use the OMP 4.0 equivalent function. */
38658 clauses = cp_parser_omp_clause_reduction (parser, clauses);
38659 else
38661 clauses = error_mark_node;
38662 cp_parser_error (parser, "expected %<#pragma simd%> clause");
38663 break;
38667 cp_parser_skip_to_pragma_eol (parser, pragma_token);
38669 if (clauses == error_mark_node)
38670 return error_mark_node;
38671 else
38672 return finish_omp_clauses (clauses, C_ORT_CILK);
38675 /* Main entry-point for parsing Cilk Plus <#pragma simd> for loops. */
38677 static void
38678 cp_parser_cilk_simd (cp_parser *parser, cp_token *pragma_token, bool *if_p)
38680 tree clauses = cp_parser_cilk_simd_all_clauses (parser, pragma_token);
38682 if (clauses == error_mark_node)
38683 return;
38685 if (cp_lexer_next_token_is_not_keyword (parser->lexer, RID_FOR))
38687 error_at (cp_lexer_peek_token (parser->lexer)->location,
38688 "for statement expected");
38689 return;
38692 tree sb = begin_omp_structured_block ();
38693 int save = cp_parser_begin_omp_structured_block (parser);
38694 tree ret = cp_parser_omp_for_loop (parser, CILK_SIMD, clauses, NULL, if_p);
38695 if (ret)
38696 cpp_validate_cilk_plus_loop (OMP_FOR_BODY (ret));
38697 cp_parser_end_omp_structured_block (parser, save);
38698 add_stmt (finish_omp_structured_block (sb));
38701 /* Main entry-point for parsing Cilk Plus _Cilk_for
38702 loops. The return value is error_mark_node
38703 when errors happen and CILK_FOR tree on success. */
38705 static tree
38706 cp_parser_cilk_for (cp_parser *parser, tree grain, bool *if_p)
38708 if (cp_lexer_next_token_is_not_keyword (parser->lexer, RID_CILK_FOR))
38709 gcc_unreachable ();
38711 tree sb = begin_omp_structured_block ();
38712 int save = cp_parser_begin_omp_structured_block (parser);
38714 tree clauses = build_omp_clause (EXPR_LOCATION (grain), OMP_CLAUSE_SCHEDULE);
38715 OMP_CLAUSE_SCHEDULE_KIND (clauses) = OMP_CLAUSE_SCHEDULE_CILKFOR;
38716 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (clauses) = grain;
38717 clauses = finish_omp_clauses (clauses, C_ORT_CILK);
38719 tree ret = cp_parser_omp_for_loop (parser, CILK_FOR, clauses, NULL, if_p);
38720 if (ret)
38721 cpp_validate_cilk_plus_loop (ret);
38722 else
38723 ret = error_mark_node;
38725 cp_parser_end_omp_structured_block (parser, save);
38726 add_stmt (finish_omp_structured_block (sb));
38727 return ret;
38730 /* Create an identifier for a generic parameter type (a synthesized
38731 template parameter implied by `auto' or a concept identifier). */
38733 static GTY(()) int generic_parm_count;
38734 static tree
38735 make_generic_type_name ()
38737 char buf[32];
38738 sprintf (buf, "auto:%d", ++generic_parm_count);
38739 return get_identifier (buf);
38742 /* Predicate that behaves as is_auto_or_concept but matches the parent
38743 node of the generic type rather than the generic type itself. This
38744 allows for type transformation in add_implicit_template_parms. */
38746 static inline bool
38747 tree_type_is_auto_or_concept (const_tree t)
38749 return TREE_TYPE (t) && is_auto_or_concept (TREE_TYPE (t));
38752 /* Add an implicit template type parameter to the CURRENT_TEMPLATE_PARMS
38753 (creating a new template parameter list if necessary). Returns the newly
38754 created template type parm. */
38756 static tree
38757 synthesize_implicit_template_parm (cp_parser *parser, tree constr)
38759 gcc_assert (current_binding_level->kind == sk_function_parms);
38761 /* Before committing to modifying any scope, if we're in an
38762 implicit template scope, and we're trying to synthesize a
38763 constrained parameter, try to find a previous parameter with
38764 the same name. This is the same-type rule for abbreviated
38765 function templates.
38767 NOTE: We can generate implicit parameters when tentatively
38768 parsing a nested name specifier, only to reject that parse
38769 later. However, matching the same template-id as part of a
38770 direct-declarator should generate an identical template
38771 parameter, so this rule will merge them. */
38772 if (parser->implicit_template_scope && constr)
38774 tree t = parser->implicit_template_parms;
38775 while (t)
38777 if (equivalent_placeholder_constraints (TREE_TYPE (t), constr))
38779 tree d = TREE_VALUE (t);
38780 if (TREE_CODE (d) == PARM_DECL)
38781 /* Return the TEMPLATE_PARM_INDEX. */
38782 d = DECL_INITIAL (d);
38783 return d;
38785 t = TREE_CHAIN (t);
38789 /* We are either continuing a function template that already contains implicit
38790 template parameters, creating a new fully-implicit function template, or
38791 extending an existing explicit function template with implicit template
38792 parameters. */
38794 cp_binding_level *const entry_scope = current_binding_level;
38796 bool become_template = false;
38797 cp_binding_level *parent_scope = 0;
38799 if (parser->implicit_template_scope)
38801 gcc_assert (parser->implicit_template_parms);
38803 current_binding_level = parser->implicit_template_scope;
38805 else
38807 /* Roll back to the existing template parameter scope (in the case of
38808 extending an explicit function template) or introduce a new template
38809 parameter scope ahead of the function parameter scope (or class scope
38810 in the case of out-of-line member definitions). The function scope is
38811 added back after template parameter synthesis below. */
38813 cp_binding_level *scope = entry_scope;
38815 while (scope->kind == sk_function_parms)
38817 parent_scope = scope;
38818 scope = scope->level_chain;
38820 if (current_class_type && !LAMBDA_TYPE_P (current_class_type))
38822 /* If not defining a class, then any class scope is a scope level in
38823 an out-of-line member definition. In this case simply wind back
38824 beyond the first such scope to inject the template parameter list.
38825 Otherwise wind back to the class being defined. The latter can
38826 occur in class member friend declarations such as:
38828 class A {
38829 void foo (auto);
38831 class B {
38832 friend void A::foo (auto);
38835 The template parameter list synthesized for the friend declaration
38836 must be injected in the scope of 'B'. This can also occur in
38837 erroneous cases such as:
38839 struct A {
38840 struct B {
38841 void foo (auto);
38843 void B::foo (auto) {}
38846 Here the attempted definition of 'B::foo' within 'A' is ill-formed
38847 but, nevertheless, the template parameter list synthesized for the
38848 declarator should be injected into the scope of 'A' as if the
38849 ill-formed template was specified explicitly. */
38851 while (scope->kind == sk_class && !scope->defining_class_p)
38853 parent_scope = scope;
38854 scope = scope->level_chain;
38858 current_binding_level = scope;
38860 if (scope->kind != sk_template_parms
38861 || !function_being_declared_is_template_p (parser))
38863 /* Introduce a new template parameter list for implicit template
38864 parameters. */
38866 become_template = true;
38868 parser->implicit_template_scope
38869 = begin_scope (sk_template_parms, NULL);
38871 ++processing_template_decl;
38873 parser->fully_implicit_function_template_p = true;
38874 ++parser->num_template_parameter_lists;
38876 else
38878 /* Synthesize implicit template parameters at the end of the explicit
38879 template parameter list. */
38881 gcc_assert (current_template_parms);
38883 parser->implicit_template_scope = scope;
38885 tree v = INNERMOST_TEMPLATE_PARMS (current_template_parms);
38886 parser->implicit_template_parms
38887 = TREE_VEC_ELT (v, TREE_VEC_LENGTH (v) - 1);
38891 /* Synthesize a new template parameter and track the current template
38892 parameter chain with implicit_template_parms. */
38894 tree proto = constr ? DECL_INITIAL (constr) : NULL_TREE;
38895 tree synth_id = make_generic_type_name ();
38896 tree synth_tmpl_parm;
38897 bool non_type = false;
38899 if (proto == NULL_TREE || TREE_CODE (proto) == TYPE_DECL)
38900 synth_tmpl_parm
38901 = finish_template_type_parm (class_type_node, synth_id);
38902 else if (TREE_CODE (proto) == TEMPLATE_DECL)
38903 synth_tmpl_parm
38904 = finish_constrained_template_template_parm (proto, synth_id);
38905 else
38907 synth_tmpl_parm = copy_decl (proto);
38908 DECL_NAME (synth_tmpl_parm) = synth_id;
38909 non_type = true;
38912 // Attach the constraint to the parm before processing.
38913 tree node = build_tree_list (NULL_TREE, synth_tmpl_parm);
38914 TREE_TYPE (node) = constr;
38915 tree new_parm
38916 = process_template_parm (parser->implicit_template_parms,
38917 input_location,
38918 node,
38919 /*non_type=*/non_type,
38920 /*param_pack=*/false);
38922 // Chain the new parameter to the list of implicit parameters.
38923 if (parser->implicit_template_parms)
38924 parser->implicit_template_parms
38925 = TREE_CHAIN (parser->implicit_template_parms);
38926 else
38927 parser->implicit_template_parms = new_parm;
38929 tree new_decl = getdecls ();
38930 if (non_type)
38931 /* Return the TEMPLATE_PARM_INDEX, not the PARM_DECL. */
38932 new_decl = DECL_INITIAL (new_decl);
38934 /* If creating a fully implicit function template, start the new implicit
38935 template parameter list with this synthesized type, otherwise grow the
38936 current template parameter list. */
38938 if (become_template)
38940 parent_scope->level_chain = current_binding_level;
38942 tree new_parms = make_tree_vec (1);
38943 TREE_VEC_ELT (new_parms, 0) = parser->implicit_template_parms;
38944 current_template_parms = tree_cons (size_int (processing_template_decl),
38945 new_parms, current_template_parms);
38947 else
38949 tree& new_parms = INNERMOST_TEMPLATE_PARMS (current_template_parms);
38950 int new_parm_idx = TREE_VEC_LENGTH (new_parms);
38951 new_parms = grow_tree_vec (new_parms, new_parm_idx + 1);
38952 TREE_VEC_ELT (new_parms, new_parm_idx) = parser->implicit_template_parms;
38955 // If the new parameter was constrained, we need to add that to the
38956 // constraints in the template parameter list.
38957 if (tree req = TEMPLATE_PARM_CONSTRAINTS (tree_last (new_parm)))
38959 tree reqs = TEMPLATE_PARMS_CONSTRAINTS (current_template_parms);
38960 reqs = conjoin_constraints (reqs, req);
38961 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) = reqs;
38964 current_binding_level = entry_scope;
38966 return new_decl;
38969 /* Finish the declaration of a fully implicit function template. Such a
38970 template has no explicit template parameter list so has not been through the
38971 normal template head and tail processing. synthesize_implicit_template_parm
38972 tries to do the head; this tries to do the tail. MEMBER_DECL_OPT should be
38973 provided if the declaration is a class member such that its template
38974 declaration can be completed. If MEMBER_DECL_OPT is provided the finished
38975 form is returned. Otherwise NULL_TREE is returned. */
38977 static tree
38978 finish_fully_implicit_template (cp_parser *parser, tree member_decl_opt)
38980 gcc_assert (parser->fully_implicit_function_template_p);
38982 if (member_decl_opt && member_decl_opt != error_mark_node
38983 && DECL_VIRTUAL_P (member_decl_opt))
38985 error_at (DECL_SOURCE_LOCATION (member_decl_opt),
38986 "implicit templates may not be %<virtual%>");
38987 DECL_VIRTUAL_P (member_decl_opt) = false;
38990 if (member_decl_opt)
38991 member_decl_opt = finish_member_template_decl (member_decl_opt);
38992 end_template_decl ();
38994 parser->fully_implicit_function_template_p = false;
38995 --parser->num_template_parameter_lists;
38997 return member_decl_opt;
39000 #include "gt-cp-parser.h"