* config/i386/i386.c: Include intl.h.
[official-gcc.git] / gcc / cp / parser.c
blob50528e24335fde2c7fe7cc715bdbc37fab5a3e8c
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 (type_uses_auto (type)
8232 && (vec_safe_length (initializer) != 1
8233 || (BRACE_ENCLOSED_INITIALIZER_P ((*initializer)[0])
8234 && CONSTRUCTOR_NELTS ((*initializer)[0]) != 1)))
8236 error_at (token->location,
8237 "initialization of new-expression for type %<auto%> "
8238 "requires exactly one element");
8239 ret = error_mark_node;
8241 else
8243 /* Construct a location e.g.:
8244 ptr = new int[100]
8245 ^~~~~~~~~~~~
8246 with caret == start at the start of the "new" token, and the end
8247 at the end of the final token we consumed. */
8248 cp_token *end_tok = cp_lexer_previous_token (parser->lexer);
8249 location_t end_loc = get_finish (end_tok->location);
8250 location_t combined_loc = make_location (start_loc, start_loc, end_loc);
8252 /* Create a representation of the new-expression. */
8253 ret = build_new (&placement, type, nelts, &initializer, global_scope_p,
8254 tf_warning_or_error);
8255 protected_set_expr_location (ret, combined_loc);
8258 if (placement != NULL)
8259 release_tree_vector (placement);
8260 if (initializer != NULL)
8261 release_tree_vector (initializer);
8263 return ret;
8266 /* Parse a new-placement.
8268 new-placement:
8269 ( expression-list )
8271 Returns the same representation as for an expression-list. */
8273 static vec<tree, va_gc> *
8274 cp_parser_new_placement (cp_parser* parser)
8276 vec<tree, va_gc> *expression_list;
8278 /* Parse the expression-list. */
8279 expression_list = (cp_parser_parenthesized_expression_list
8280 (parser, non_attr, /*cast_p=*/false,
8281 /*allow_expansion_p=*/true,
8282 /*non_constant_p=*/NULL));
8284 if (expression_list && expression_list->is_empty ())
8285 error ("expected expression-list or type-id");
8287 return expression_list;
8290 /* Parse a new-type-id.
8292 new-type-id:
8293 type-specifier-seq new-declarator [opt]
8295 Returns the TYPE allocated. If the new-type-id indicates an array
8296 type, *NELTS is set to the number of elements in the last array
8297 bound; the TYPE will not include the last array bound. */
8299 static tree
8300 cp_parser_new_type_id (cp_parser* parser, tree *nelts)
8302 cp_decl_specifier_seq type_specifier_seq;
8303 cp_declarator *new_declarator;
8304 cp_declarator *declarator;
8305 cp_declarator *outer_declarator;
8306 const char *saved_message;
8308 /* The type-specifier sequence must not contain type definitions.
8309 (It cannot contain declarations of new types either, but if they
8310 are not definitions we will catch that because they are not
8311 complete.) */
8312 saved_message = parser->type_definition_forbidden_message;
8313 parser->type_definition_forbidden_message
8314 = G_("types may not be defined in a new-type-id");
8315 /* Parse the type-specifier-seq. */
8316 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
8317 /*is_trailing_return=*/false,
8318 &type_specifier_seq);
8319 /* Restore the old message. */
8320 parser->type_definition_forbidden_message = saved_message;
8322 if (type_specifier_seq.type == error_mark_node)
8323 return error_mark_node;
8325 /* Parse the new-declarator. */
8326 new_declarator = cp_parser_new_declarator_opt (parser);
8328 /* Determine the number of elements in the last array dimension, if
8329 any. */
8330 *nelts = NULL_TREE;
8331 /* Skip down to the last array dimension. */
8332 declarator = new_declarator;
8333 outer_declarator = NULL;
8334 while (declarator && (declarator->kind == cdk_pointer
8335 || declarator->kind == cdk_ptrmem))
8337 outer_declarator = declarator;
8338 declarator = declarator->declarator;
8340 while (declarator
8341 && declarator->kind == cdk_array
8342 && declarator->declarator
8343 && declarator->declarator->kind == cdk_array)
8345 outer_declarator = declarator;
8346 declarator = declarator->declarator;
8349 if (declarator && declarator->kind == cdk_array)
8351 *nelts = declarator->u.array.bounds;
8352 if (*nelts == error_mark_node)
8353 *nelts = integer_one_node;
8355 if (outer_declarator)
8356 outer_declarator->declarator = declarator->declarator;
8357 else
8358 new_declarator = NULL;
8361 return groktypename (&type_specifier_seq, new_declarator, false);
8364 /* Parse an (optional) new-declarator.
8366 new-declarator:
8367 ptr-operator new-declarator [opt]
8368 direct-new-declarator
8370 Returns the declarator. */
8372 static cp_declarator *
8373 cp_parser_new_declarator_opt (cp_parser* parser)
8375 enum tree_code code;
8376 tree type, std_attributes = NULL_TREE;
8377 cp_cv_quals cv_quals;
8379 /* We don't know if there's a ptr-operator next, or not. */
8380 cp_parser_parse_tentatively (parser);
8381 /* Look for a ptr-operator. */
8382 code = cp_parser_ptr_operator (parser, &type, &cv_quals, &std_attributes);
8383 /* If that worked, look for more new-declarators. */
8384 if (cp_parser_parse_definitely (parser))
8386 cp_declarator *declarator;
8388 /* Parse another optional declarator. */
8389 declarator = cp_parser_new_declarator_opt (parser);
8391 declarator = cp_parser_make_indirect_declarator
8392 (code, type, cv_quals, declarator, std_attributes);
8394 return declarator;
8397 /* If the next token is a `[', there is a direct-new-declarator. */
8398 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
8399 return cp_parser_direct_new_declarator (parser);
8401 return NULL;
8404 /* Parse a direct-new-declarator.
8406 direct-new-declarator:
8407 [ expression ]
8408 direct-new-declarator [constant-expression]
8412 static cp_declarator *
8413 cp_parser_direct_new_declarator (cp_parser* parser)
8415 cp_declarator *declarator = NULL;
8417 while (true)
8419 tree expression;
8420 cp_token *token;
8422 /* Look for the opening `['. */
8423 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
8425 token = cp_lexer_peek_token (parser->lexer);
8426 expression = cp_parser_expression (parser);
8427 /* The standard requires that the expression have integral
8428 type. DR 74 adds enumeration types. We believe that the
8429 real intent is that these expressions be handled like the
8430 expression in a `switch' condition, which also allows
8431 classes with a single conversion to integral or
8432 enumeration type. */
8433 if (!processing_template_decl)
8435 expression
8436 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
8437 expression,
8438 /*complain=*/true);
8439 if (!expression)
8441 error_at (token->location,
8442 "expression in new-declarator must have integral "
8443 "or enumeration type");
8444 expression = error_mark_node;
8448 /* Look for the closing `]'. */
8449 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
8451 /* Add this bound to the declarator. */
8452 declarator = make_array_declarator (declarator, expression);
8454 /* If the next token is not a `[', then there are no more
8455 bounds. */
8456 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
8457 break;
8460 return declarator;
8463 /* Parse a new-initializer.
8465 new-initializer:
8466 ( expression-list [opt] )
8467 braced-init-list
8469 Returns a representation of the expression-list. */
8471 static vec<tree, va_gc> *
8472 cp_parser_new_initializer (cp_parser* parser)
8474 vec<tree, va_gc> *expression_list;
8476 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
8478 tree t;
8479 bool expr_non_constant_p;
8480 cp_lexer_set_source_position (parser->lexer);
8481 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
8482 t = cp_parser_braced_list (parser, &expr_non_constant_p);
8483 CONSTRUCTOR_IS_DIRECT_INIT (t) = 1;
8484 expression_list = make_tree_vector_single (t);
8486 else
8487 expression_list = (cp_parser_parenthesized_expression_list
8488 (parser, non_attr, /*cast_p=*/false,
8489 /*allow_expansion_p=*/true,
8490 /*non_constant_p=*/NULL));
8492 return expression_list;
8495 /* Parse a delete-expression.
8497 delete-expression:
8498 :: [opt] delete cast-expression
8499 :: [opt] delete [ ] cast-expression
8501 Returns a representation of the expression. */
8503 static tree
8504 cp_parser_delete_expression (cp_parser* parser)
8506 bool global_scope_p;
8507 bool array_p;
8508 tree expression;
8510 /* Look for the optional `::' operator. */
8511 global_scope_p
8512 = (cp_parser_global_scope_opt (parser,
8513 /*current_scope_valid_p=*/false)
8514 != NULL_TREE);
8515 /* Look for the `delete' keyword. */
8516 cp_parser_require_keyword (parser, RID_DELETE, RT_DELETE);
8517 /* See if the array syntax is in use. */
8518 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
8520 /* Consume the `[' token. */
8521 cp_lexer_consume_token (parser->lexer);
8522 /* Look for the `]' token. */
8523 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
8524 /* Remember that this is the `[]' construct. */
8525 array_p = true;
8527 else
8528 array_p = false;
8530 /* Parse the cast-expression. */
8531 expression = cp_parser_simple_cast_expression (parser);
8533 /* A delete-expression may not appear in an integral constant
8534 expression. */
8535 if (cp_parser_non_integral_constant_expression (parser, NIC_DEL))
8536 return error_mark_node;
8538 return delete_sanity (expression, NULL_TREE, array_p, global_scope_p,
8539 tf_warning_or_error);
8542 /* Returns 1 if TOKEN may start a cast-expression and isn't '++', '--',
8543 neither '[' in C++11; -1 if TOKEN is '++', '--', or '[' in C++11;
8544 0 otherwise. */
8546 static int
8547 cp_parser_tokens_start_cast_expression (cp_parser *parser)
8549 cp_token *token = cp_lexer_peek_token (parser->lexer);
8550 switch (token->type)
8552 case CPP_COMMA:
8553 case CPP_SEMICOLON:
8554 case CPP_QUERY:
8555 case CPP_COLON:
8556 case CPP_CLOSE_SQUARE:
8557 case CPP_CLOSE_PAREN:
8558 case CPP_CLOSE_BRACE:
8559 case CPP_OPEN_BRACE:
8560 case CPP_DOT:
8561 case CPP_DOT_STAR:
8562 case CPP_DEREF:
8563 case CPP_DEREF_STAR:
8564 case CPP_DIV:
8565 case CPP_MOD:
8566 case CPP_LSHIFT:
8567 case CPP_RSHIFT:
8568 case CPP_LESS:
8569 case CPP_GREATER:
8570 case CPP_LESS_EQ:
8571 case CPP_GREATER_EQ:
8572 case CPP_EQ_EQ:
8573 case CPP_NOT_EQ:
8574 case CPP_EQ:
8575 case CPP_MULT_EQ:
8576 case CPP_DIV_EQ:
8577 case CPP_MOD_EQ:
8578 case CPP_PLUS_EQ:
8579 case CPP_MINUS_EQ:
8580 case CPP_RSHIFT_EQ:
8581 case CPP_LSHIFT_EQ:
8582 case CPP_AND_EQ:
8583 case CPP_XOR_EQ:
8584 case CPP_OR_EQ:
8585 case CPP_XOR:
8586 case CPP_OR:
8587 case CPP_OR_OR:
8588 case CPP_EOF:
8589 case CPP_ELLIPSIS:
8590 return 0;
8592 case CPP_OPEN_PAREN:
8593 /* In ((type ()) () the last () isn't a valid cast-expression,
8594 so the whole must be parsed as postfix-expression. */
8595 return cp_lexer_peek_nth_token (parser->lexer, 2)->type
8596 != CPP_CLOSE_PAREN;
8598 case CPP_OPEN_SQUARE:
8599 /* '[' may start a primary-expression in obj-c++ and in C++11,
8600 as a lambda-expression, eg, '(void)[]{}'. */
8601 if (cxx_dialect >= cxx11)
8602 return -1;
8603 return c_dialect_objc ();
8605 case CPP_PLUS_PLUS:
8606 case CPP_MINUS_MINUS:
8607 /* '++' and '--' may or may not start a cast-expression:
8609 struct T { void operator++(int); };
8610 void f() { (T())++; }
8614 int a;
8615 (int)++a; */
8616 return -1;
8618 default:
8619 return 1;
8623 /* Parse a cast-expression.
8625 cast-expression:
8626 unary-expression
8627 ( type-id ) cast-expression
8629 ADDRESS_P is true iff the unary-expression is appearing as the
8630 operand of the `&' operator. CAST_P is true if this expression is
8631 the target of a cast.
8633 Returns a representation of the expression. */
8635 static cp_expr
8636 cp_parser_cast_expression (cp_parser *parser, bool address_p, bool cast_p,
8637 bool decltype_p, cp_id_kind * pidk)
8639 /* If it's a `(', then we might be looking at a cast. */
8640 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
8642 tree type = NULL_TREE;
8643 cp_expr expr (NULL_TREE);
8644 int cast_expression = 0;
8645 const char *saved_message;
8647 /* There's no way to know yet whether or not this is a cast.
8648 For example, `(int (3))' is a unary-expression, while `(int)
8649 3' is a cast. So, we resort to parsing tentatively. */
8650 cp_parser_parse_tentatively (parser);
8651 /* Types may not be defined in a cast. */
8652 saved_message = parser->type_definition_forbidden_message;
8653 parser->type_definition_forbidden_message
8654 = G_("types may not be defined in casts");
8655 /* Consume the `('. */
8656 cp_token *open_paren = cp_lexer_consume_token (parser->lexer);
8657 location_t open_paren_loc = open_paren->location;
8659 /* A very tricky bit is that `(struct S) { 3 }' is a
8660 compound-literal (which we permit in C++ as an extension).
8661 But, that construct is not a cast-expression -- it is a
8662 postfix-expression. (The reason is that `(struct S) { 3 }.i'
8663 is legal; if the compound-literal were a cast-expression,
8664 you'd need an extra set of parentheses.) But, if we parse
8665 the type-id, and it happens to be a class-specifier, then we
8666 will commit to the parse at that point, because we cannot
8667 undo the action that is done when creating a new class. So,
8668 then we cannot back up and do a postfix-expression.
8670 Another tricky case is the following (c++/29234):
8672 struct S { void operator () (); };
8674 void foo ()
8676 ( S()() );
8679 As a type-id we parse the parenthesized S()() as a function
8680 returning a function, groktypename complains and we cannot
8681 back up in this case either.
8683 Therefore, we scan ahead to the closing `)', and check to see
8684 if the tokens after the `)' can start a cast-expression. Otherwise
8685 we are dealing with an unary-expression, a postfix-expression
8686 or something else.
8688 Yet another tricky case, in C++11, is the following (c++/54891):
8690 (void)[]{};
8692 The issue is that usually, besides the case of lambda-expressions,
8693 the parenthesized type-id cannot be followed by '[', and, eg, we
8694 want to parse '(C ())[2];' in parse/pr26997.C as unary-expression.
8695 Thus, if cp_parser_tokens_start_cast_expression returns -1, below
8696 we don't commit, we try a cast-expression, then an unary-expression.
8698 Save tokens so that we can put them back. */
8699 cp_lexer_save_tokens (parser->lexer);
8701 /* We may be looking at a cast-expression. */
8702 if (cp_parser_skip_to_closing_parenthesis (parser, false, false,
8703 /*consume_paren=*/true))
8704 cast_expression
8705 = cp_parser_tokens_start_cast_expression (parser);
8707 /* Roll back the tokens we skipped. */
8708 cp_lexer_rollback_tokens (parser->lexer);
8709 /* If we aren't looking at a cast-expression, simulate an error so
8710 that the call to cp_parser_error_occurred below returns true. */
8711 if (!cast_expression)
8712 cp_parser_simulate_error (parser);
8713 else
8715 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
8716 parser->in_type_id_in_expr_p = true;
8717 /* Look for the type-id. */
8718 type = cp_parser_type_id (parser);
8719 /* Look for the closing `)'. */
8720 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
8721 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
8724 /* Restore the saved message. */
8725 parser->type_definition_forbidden_message = saved_message;
8727 /* At this point this can only be either a cast or a
8728 parenthesized ctor such as `(T ())' that looks like a cast to
8729 function returning T. */
8730 if (!cp_parser_error_occurred (parser))
8732 /* Only commit if the cast-expression doesn't start with
8733 '++', '--', or '[' in C++11. */
8734 if (cast_expression > 0)
8735 cp_parser_commit_to_topmost_tentative_parse (parser);
8737 expr = cp_parser_cast_expression (parser,
8738 /*address_p=*/false,
8739 /*cast_p=*/true,
8740 /*decltype_p=*/false,
8741 pidk);
8743 if (cp_parser_parse_definitely (parser))
8745 /* Warn about old-style casts, if so requested. */
8746 if (warn_old_style_cast
8747 && !in_system_header_at (input_location)
8748 && !VOID_TYPE_P (type)
8749 && current_lang_name != lang_name_c)
8750 warning (OPT_Wold_style_cast, "use of old-style cast");
8752 /* Only type conversions to integral or enumeration types
8753 can be used in constant-expressions. */
8754 if (!cast_valid_in_integral_constant_expression_p (type)
8755 && cp_parser_non_integral_constant_expression (parser,
8756 NIC_CAST))
8757 return error_mark_node;
8759 /* Perform the cast. */
8760 /* Make a location:
8761 (TYPE) EXPR
8762 ^~~~~~~~~~~
8763 with start==caret at the open paren, extending to the
8764 end of "expr". */
8765 location_t cast_loc = make_location (open_paren_loc,
8766 open_paren_loc,
8767 expr.get_finish ());
8768 expr = build_c_cast (cast_loc, type, expr);
8769 return expr;
8772 else
8773 cp_parser_abort_tentative_parse (parser);
8776 /* If we get here, then it's not a cast, so it must be a
8777 unary-expression. */
8778 return cp_parser_unary_expression (parser, pidk, address_p,
8779 cast_p, decltype_p);
8782 /* Parse a binary expression of the general form:
8784 pm-expression:
8785 cast-expression
8786 pm-expression .* cast-expression
8787 pm-expression ->* cast-expression
8789 multiplicative-expression:
8790 pm-expression
8791 multiplicative-expression * pm-expression
8792 multiplicative-expression / pm-expression
8793 multiplicative-expression % pm-expression
8795 additive-expression:
8796 multiplicative-expression
8797 additive-expression + multiplicative-expression
8798 additive-expression - multiplicative-expression
8800 shift-expression:
8801 additive-expression
8802 shift-expression << additive-expression
8803 shift-expression >> additive-expression
8805 relational-expression:
8806 shift-expression
8807 relational-expression < shift-expression
8808 relational-expression > shift-expression
8809 relational-expression <= shift-expression
8810 relational-expression >= shift-expression
8812 GNU Extension:
8814 relational-expression:
8815 relational-expression <? shift-expression
8816 relational-expression >? shift-expression
8818 equality-expression:
8819 relational-expression
8820 equality-expression == relational-expression
8821 equality-expression != relational-expression
8823 and-expression:
8824 equality-expression
8825 and-expression & equality-expression
8827 exclusive-or-expression:
8828 and-expression
8829 exclusive-or-expression ^ and-expression
8831 inclusive-or-expression:
8832 exclusive-or-expression
8833 inclusive-or-expression | exclusive-or-expression
8835 logical-and-expression:
8836 inclusive-or-expression
8837 logical-and-expression && inclusive-or-expression
8839 logical-or-expression:
8840 logical-and-expression
8841 logical-or-expression || logical-and-expression
8843 All these are implemented with a single function like:
8845 binary-expression:
8846 simple-cast-expression
8847 binary-expression <token> binary-expression
8849 CAST_P is true if this expression is the target of a cast.
8851 The binops_by_token map is used to get the tree codes for each <token> type.
8852 binary-expressions are associated according to a precedence table. */
8854 #define TOKEN_PRECEDENCE(token) \
8855 (((token->type == CPP_GREATER \
8856 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT)) \
8857 && !parser->greater_than_is_operator_p) \
8858 ? PREC_NOT_OPERATOR \
8859 : binops_by_token[token->type].prec)
8861 static cp_expr
8862 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
8863 bool no_toplevel_fold_p,
8864 bool decltype_p,
8865 enum cp_parser_prec prec,
8866 cp_id_kind * pidk)
8868 cp_parser_expression_stack stack;
8869 cp_parser_expression_stack_entry *sp = &stack[0];
8870 cp_parser_expression_stack_entry current;
8871 cp_expr rhs;
8872 cp_token *token;
8873 enum tree_code rhs_type;
8874 enum cp_parser_prec new_prec, lookahead_prec;
8875 tree overload;
8877 /* Parse the first expression. */
8878 current.lhs_type = (cp_lexer_next_token_is (parser->lexer, CPP_NOT)
8879 ? TRUTH_NOT_EXPR : ERROR_MARK);
8880 current.lhs = cp_parser_cast_expression (parser, /*address_p=*/false,
8881 cast_p, decltype_p, pidk);
8882 current.prec = prec;
8884 if (cp_parser_error_occurred (parser))
8885 return error_mark_node;
8887 for (;;)
8889 /* Get an operator token. */
8890 token = cp_lexer_peek_token (parser->lexer);
8892 if (warn_cxx11_compat
8893 && token->type == CPP_RSHIFT
8894 && !parser->greater_than_is_operator_p)
8896 if (warning_at (token->location, OPT_Wc__11_compat,
8897 "%<>>%> operator is treated"
8898 " as two right angle brackets in C++11"))
8899 inform (token->location,
8900 "suggest parentheses around %<>>%> expression");
8903 new_prec = TOKEN_PRECEDENCE (token);
8904 if (new_prec != PREC_NOT_OPERATOR
8905 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS))
8906 /* This is a fold-expression; handle it later. */
8907 new_prec = PREC_NOT_OPERATOR;
8909 /* Popping an entry off the stack means we completed a subexpression:
8910 - either we found a token which is not an operator (`>' where it is not
8911 an operator, or prec == PREC_NOT_OPERATOR), in which case popping
8912 will happen repeatedly;
8913 - or, we found an operator which has lower priority. This is the case
8914 where the recursive descent *ascends*, as in `3 * 4 + 5' after
8915 parsing `3 * 4'. */
8916 if (new_prec <= current.prec)
8918 if (sp == stack)
8919 break;
8920 else
8921 goto pop;
8924 get_rhs:
8925 current.tree_type = binops_by_token[token->type].tree_type;
8926 current.loc = token->location;
8928 /* We used the operator token. */
8929 cp_lexer_consume_token (parser->lexer);
8931 /* For "false && x" or "true || x", x will never be executed;
8932 disable warnings while evaluating it. */
8933 if (current.tree_type == TRUTH_ANDIF_EXPR)
8934 c_inhibit_evaluation_warnings +=
8935 cp_fully_fold (current.lhs) == truthvalue_false_node;
8936 else if (current.tree_type == TRUTH_ORIF_EXPR)
8937 c_inhibit_evaluation_warnings +=
8938 cp_fully_fold (current.lhs) == truthvalue_true_node;
8940 /* Extract another operand. It may be the RHS of this expression
8941 or the LHS of a new, higher priority expression. */
8942 rhs_type = (cp_lexer_next_token_is (parser->lexer, CPP_NOT)
8943 ? TRUTH_NOT_EXPR : ERROR_MARK);
8944 rhs = cp_parser_simple_cast_expression (parser);
8946 /* Get another operator token. Look up its precedence to avoid
8947 building a useless (immediately popped) stack entry for common
8948 cases such as 3 + 4 + 5 or 3 * 4 + 5. */
8949 token = cp_lexer_peek_token (parser->lexer);
8950 lookahead_prec = TOKEN_PRECEDENCE (token);
8951 if (lookahead_prec != PREC_NOT_OPERATOR
8952 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS))
8953 lookahead_prec = PREC_NOT_OPERATOR;
8954 if (lookahead_prec > new_prec)
8956 /* ... and prepare to parse the RHS of the new, higher priority
8957 expression. Since precedence levels on the stack are
8958 monotonically increasing, we do not have to care about
8959 stack overflows. */
8960 *sp = current;
8961 ++sp;
8962 current.lhs = rhs;
8963 current.lhs_type = rhs_type;
8964 current.prec = new_prec;
8965 new_prec = lookahead_prec;
8966 goto get_rhs;
8968 pop:
8969 lookahead_prec = new_prec;
8970 /* If the stack is not empty, we have parsed into LHS the right side
8971 (`4' in the example above) of an expression we had suspended.
8972 We can use the information on the stack to recover the LHS (`3')
8973 from the stack together with the tree code (`MULT_EXPR'), and
8974 the precedence of the higher level subexpression
8975 (`PREC_ADDITIVE_EXPRESSION'). TOKEN is the CPP_PLUS token,
8976 which will be used to actually build the additive expression. */
8977 rhs = current.lhs;
8978 rhs_type = current.lhs_type;
8979 --sp;
8980 current = *sp;
8983 /* Undo the disabling of warnings done above. */
8984 if (current.tree_type == TRUTH_ANDIF_EXPR)
8985 c_inhibit_evaluation_warnings -=
8986 cp_fully_fold (current.lhs) == truthvalue_false_node;
8987 else if (current.tree_type == TRUTH_ORIF_EXPR)
8988 c_inhibit_evaluation_warnings -=
8989 cp_fully_fold (current.lhs) == truthvalue_true_node;
8991 if (warn_logical_not_paren
8992 && TREE_CODE_CLASS (current.tree_type) == tcc_comparison
8993 && current.lhs_type == TRUTH_NOT_EXPR
8994 /* Avoid warning for !!x == y. */
8995 && (TREE_CODE (current.lhs) != NE_EXPR
8996 || !integer_zerop (TREE_OPERAND (current.lhs, 1)))
8997 && (TREE_CODE (current.lhs) != TRUTH_NOT_EXPR
8998 || (TREE_CODE (TREE_OPERAND (current.lhs, 0)) != TRUTH_NOT_EXPR
8999 /* Avoid warning for !b == y where b is boolean. */
9000 && (TREE_TYPE (TREE_OPERAND (current.lhs, 0)) == NULL_TREE
9001 || (TREE_CODE (TREE_TYPE (TREE_OPERAND (current.lhs, 0)))
9002 != BOOLEAN_TYPE))))
9003 /* Avoid warning for !!b == y where b is boolean. */
9004 && (!DECL_P (current.lhs)
9005 || TREE_TYPE (current.lhs) == NULL_TREE
9006 || TREE_CODE (TREE_TYPE (current.lhs)) != BOOLEAN_TYPE))
9007 warn_logical_not_parentheses (current.loc, current.tree_type,
9008 current.lhs, maybe_constant_value (rhs));
9010 overload = NULL;
9012 location_t combined_loc = make_location (current.loc,
9013 current.lhs.get_start (),
9014 rhs.get_finish ());
9016 /* ??? Currently we pass lhs_type == ERROR_MARK and rhs_type ==
9017 ERROR_MARK for everything that is not a binary expression.
9018 This makes warn_about_parentheses miss some warnings that
9019 involve unary operators. For unary expressions we should
9020 pass the correct tree_code unless the unary expression was
9021 surrounded by parentheses.
9023 if (no_toplevel_fold_p
9024 && lookahead_prec <= current.prec
9025 && sp == stack)
9026 current.lhs = build2_loc (combined_loc,
9027 current.tree_type,
9028 TREE_CODE_CLASS (current.tree_type)
9029 == tcc_comparison
9030 ? boolean_type_node : TREE_TYPE (current.lhs),
9031 current.lhs, rhs);
9032 else
9034 current.lhs = build_x_binary_op (combined_loc, current.tree_type,
9035 current.lhs, current.lhs_type,
9036 rhs, rhs_type, &overload,
9037 complain_flags (decltype_p));
9038 /* TODO: build_x_binary_op doesn't always honor the location. */
9039 current.lhs.set_location (combined_loc);
9041 current.lhs_type = current.tree_type;
9043 /* If the binary operator required the use of an overloaded operator,
9044 then this expression cannot be an integral constant-expression.
9045 An overloaded operator can be used even if both operands are
9046 otherwise permissible in an integral constant-expression if at
9047 least one of the operands is of enumeration type. */
9049 if (overload
9050 && cp_parser_non_integral_constant_expression (parser,
9051 NIC_OVERLOADED))
9052 return error_mark_node;
9055 return current.lhs;
9058 static cp_expr
9059 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
9060 bool no_toplevel_fold_p,
9061 enum cp_parser_prec prec,
9062 cp_id_kind * pidk)
9064 return cp_parser_binary_expression (parser, cast_p, no_toplevel_fold_p,
9065 /*decltype*/false, prec, pidk);
9068 /* Parse the `? expression : assignment-expression' part of a
9069 conditional-expression. The LOGICAL_OR_EXPR is the
9070 logical-or-expression that started the conditional-expression.
9071 Returns a representation of the entire conditional-expression.
9073 This routine is used by cp_parser_assignment_expression.
9075 ? expression : assignment-expression
9077 GNU Extensions:
9079 ? : assignment-expression */
9081 static tree
9082 cp_parser_question_colon_clause (cp_parser* parser, cp_expr logical_or_expr)
9084 tree expr, folded_logical_or_expr = cp_fully_fold (logical_or_expr);
9085 cp_expr assignment_expr;
9086 struct cp_token *token;
9087 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
9089 /* Consume the `?' token. */
9090 cp_lexer_consume_token (parser->lexer);
9091 token = cp_lexer_peek_token (parser->lexer);
9092 if (cp_parser_allow_gnu_extensions_p (parser)
9093 && token->type == CPP_COLON)
9095 pedwarn (token->location, OPT_Wpedantic,
9096 "ISO C++ does not allow ?: with omitted middle operand");
9097 /* Implicit true clause. */
9098 expr = NULL_TREE;
9099 c_inhibit_evaluation_warnings +=
9100 folded_logical_or_expr == truthvalue_true_node;
9101 warn_for_omitted_condop (token->location, logical_or_expr);
9103 else
9105 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
9106 parser->colon_corrects_to_scope_p = false;
9107 /* Parse the expression. */
9108 c_inhibit_evaluation_warnings +=
9109 folded_logical_or_expr == truthvalue_false_node;
9110 expr = cp_parser_expression (parser);
9111 c_inhibit_evaluation_warnings +=
9112 ((folded_logical_or_expr == truthvalue_true_node)
9113 - (folded_logical_or_expr == truthvalue_false_node));
9114 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
9117 /* The next token should be a `:'. */
9118 cp_parser_require (parser, CPP_COLON, RT_COLON);
9119 /* Parse the assignment-expression. */
9120 assignment_expr = cp_parser_assignment_expression (parser);
9121 c_inhibit_evaluation_warnings -=
9122 folded_logical_or_expr == truthvalue_true_node;
9124 /* Make a location:
9125 LOGICAL_OR_EXPR ? EXPR : ASSIGNMENT_EXPR
9126 ~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
9127 with the caret at the "?", ranging from the start of
9128 the logical_or_expr to the end of the assignment_expr. */
9129 loc = make_location (loc,
9130 logical_or_expr.get_start (),
9131 assignment_expr.get_finish ());
9133 /* Build the conditional-expression. */
9134 return build_x_conditional_expr (loc, logical_or_expr,
9135 expr,
9136 assignment_expr,
9137 tf_warning_or_error);
9140 /* Parse an assignment-expression.
9142 assignment-expression:
9143 conditional-expression
9144 logical-or-expression assignment-operator assignment_expression
9145 throw-expression
9147 CAST_P is true if this expression is the target of a cast.
9148 DECLTYPE_P is true if this expression is the operand of decltype.
9150 Returns a representation for the expression. */
9152 static cp_expr
9153 cp_parser_assignment_expression (cp_parser* parser, cp_id_kind * pidk,
9154 bool cast_p, bool decltype_p)
9156 cp_expr expr;
9158 /* If the next token is the `throw' keyword, then we're looking at
9159 a throw-expression. */
9160 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
9161 expr = cp_parser_throw_expression (parser);
9162 /* Otherwise, it must be that we are looking at a
9163 logical-or-expression. */
9164 else
9166 /* Parse the binary expressions (logical-or-expression). */
9167 expr = cp_parser_binary_expression (parser, cast_p, false,
9168 decltype_p,
9169 PREC_NOT_OPERATOR, pidk);
9170 /* If the next token is a `?' then we're actually looking at a
9171 conditional-expression. */
9172 if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
9173 return cp_parser_question_colon_clause (parser, expr);
9174 else
9176 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
9178 /* If it's an assignment-operator, we're using the second
9179 production. */
9180 enum tree_code assignment_operator
9181 = cp_parser_assignment_operator_opt (parser);
9182 if (assignment_operator != ERROR_MARK)
9184 bool non_constant_p;
9186 /* Parse the right-hand side of the assignment. */
9187 cp_expr rhs = cp_parser_initializer_clause (parser,
9188 &non_constant_p);
9190 if (BRACE_ENCLOSED_INITIALIZER_P (rhs))
9191 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
9193 /* An assignment may not appear in a
9194 constant-expression. */
9195 if (cp_parser_non_integral_constant_expression (parser,
9196 NIC_ASSIGNMENT))
9197 return error_mark_node;
9198 /* Build the assignment expression. Its default
9199 location:
9200 LHS = RHS
9201 ~~~~^~~~~
9202 is the location of the '=' token as the
9203 caret, ranging from the start of the lhs to the
9204 end of the rhs. */
9205 loc = make_location (loc,
9206 expr.get_start (),
9207 rhs.get_finish ());
9208 expr = build_x_modify_expr (loc, expr,
9209 assignment_operator,
9210 rhs,
9211 complain_flags (decltype_p));
9212 /* TODO: build_x_modify_expr doesn't honor the location,
9213 so we must set it here. */
9214 expr.set_location (loc);
9219 return expr;
9222 /* Parse an (optional) assignment-operator.
9224 assignment-operator: one of
9225 = *= /= %= += -= >>= <<= &= ^= |=
9227 GNU Extension:
9229 assignment-operator: one of
9230 <?= >?=
9232 If the next token is an assignment operator, the corresponding tree
9233 code is returned, and the token is consumed. For example, for
9234 `+=', PLUS_EXPR is returned. For `=' itself, the code returned is
9235 NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%',
9236 TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment
9237 operator, ERROR_MARK is returned. */
9239 static enum tree_code
9240 cp_parser_assignment_operator_opt (cp_parser* parser)
9242 enum tree_code op;
9243 cp_token *token;
9245 /* Peek at the next token. */
9246 token = cp_lexer_peek_token (parser->lexer);
9248 switch (token->type)
9250 case CPP_EQ:
9251 op = NOP_EXPR;
9252 break;
9254 case CPP_MULT_EQ:
9255 op = MULT_EXPR;
9256 break;
9258 case CPP_DIV_EQ:
9259 op = TRUNC_DIV_EXPR;
9260 break;
9262 case CPP_MOD_EQ:
9263 op = TRUNC_MOD_EXPR;
9264 break;
9266 case CPP_PLUS_EQ:
9267 op = PLUS_EXPR;
9268 break;
9270 case CPP_MINUS_EQ:
9271 op = MINUS_EXPR;
9272 break;
9274 case CPP_RSHIFT_EQ:
9275 op = RSHIFT_EXPR;
9276 break;
9278 case CPP_LSHIFT_EQ:
9279 op = LSHIFT_EXPR;
9280 break;
9282 case CPP_AND_EQ:
9283 op = BIT_AND_EXPR;
9284 break;
9286 case CPP_XOR_EQ:
9287 op = BIT_XOR_EXPR;
9288 break;
9290 case CPP_OR_EQ:
9291 op = BIT_IOR_EXPR;
9292 break;
9294 default:
9295 /* Nothing else is an assignment operator. */
9296 op = ERROR_MARK;
9299 /* An operator followed by ... is a fold-expression, handled elsewhere. */
9300 if (op != ERROR_MARK
9301 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS))
9302 op = ERROR_MARK;
9304 /* If it was an assignment operator, consume it. */
9305 if (op != ERROR_MARK)
9306 cp_lexer_consume_token (parser->lexer);
9308 return op;
9311 /* Parse an expression.
9313 expression:
9314 assignment-expression
9315 expression , assignment-expression
9317 CAST_P is true if this expression is the target of a cast.
9318 DECLTYPE_P is true if this expression is the immediate operand of decltype,
9319 except possibly parenthesized or on the RHS of a comma (N3276).
9321 Returns a representation of the expression. */
9323 static cp_expr
9324 cp_parser_expression (cp_parser* parser, cp_id_kind * pidk,
9325 bool cast_p, bool decltype_p)
9327 cp_expr expression = NULL_TREE;
9328 location_t loc = UNKNOWN_LOCATION;
9330 while (true)
9332 cp_expr assignment_expression;
9334 /* Parse the next assignment-expression. */
9335 assignment_expression
9336 = cp_parser_assignment_expression (parser, pidk, cast_p, decltype_p);
9338 /* We don't create a temporary for a call that is the immediate operand
9339 of decltype or on the RHS of a comma. But when we see a comma, we
9340 need to create a temporary for a call on the LHS. */
9341 if (decltype_p && !processing_template_decl
9342 && TREE_CODE (assignment_expression) == CALL_EXPR
9343 && CLASS_TYPE_P (TREE_TYPE (assignment_expression))
9344 && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
9345 assignment_expression
9346 = build_cplus_new (TREE_TYPE (assignment_expression),
9347 assignment_expression, tf_warning_or_error);
9349 /* If this is the first assignment-expression, we can just
9350 save it away. */
9351 if (!expression)
9352 expression = assignment_expression;
9353 else
9355 /* Create a location with caret at the comma, ranging
9356 from the start of the LHS to the end of the RHS. */
9357 loc = make_location (loc,
9358 expression.get_start (),
9359 assignment_expression.get_finish ());
9360 expression = build_x_compound_expr (loc, expression,
9361 assignment_expression,
9362 complain_flags (decltype_p));
9363 expression.set_location (loc);
9365 /* If the next token is not a comma, or we're in a fold-expression, then
9366 we are done with the expression. */
9367 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA)
9368 || cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS))
9369 break;
9370 /* Consume the `,'. */
9371 loc = cp_lexer_peek_token (parser->lexer)->location;
9372 cp_lexer_consume_token (parser->lexer);
9373 /* A comma operator cannot appear in a constant-expression. */
9374 if (cp_parser_non_integral_constant_expression (parser, NIC_COMMA))
9375 expression = error_mark_node;
9378 return expression;
9381 /* Parse a constant-expression.
9383 constant-expression:
9384 conditional-expression
9386 If ALLOW_NON_CONSTANT_P a non-constant expression is silently
9387 accepted. If ALLOW_NON_CONSTANT_P is true and the expression is not
9388 constant, *NON_CONSTANT_P is set to TRUE. If ALLOW_NON_CONSTANT_P
9389 is false, NON_CONSTANT_P should be NULL. */
9391 static cp_expr
9392 cp_parser_constant_expression (cp_parser* parser,
9393 bool allow_non_constant_p,
9394 bool *non_constant_p)
9396 bool saved_integral_constant_expression_p;
9397 bool saved_allow_non_integral_constant_expression_p;
9398 bool saved_non_integral_constant_expression_p;
9399 cp_expr expression;
9401 /* It might seem that we could simply parse the
9402 conditional-expression, and then check to see if it were
9403 TREE_CONSTANT. However, an expression that is TREE_CONSTANT is
9404 one that the compiler can figure out is constant, possibly after
9405 doing some simplifications or optimizations. The standard has a
9406 precise definition of constant-expression, and we must honor
9407 that, even though it is somewhat more restrictive.
9409 For example:
9411 int i[(2, 3)];
9413 is not a legal declaration, because `(2, 3)' is not a
9414 constant-expression. The `,' operator is forbidden in a
9415 constant-expression. However, GCC's constant-folding machinery
9416 will fold this operation to an INTEGER_CST for `3'. */
9418 /* Save the old settings. */
9419 saved_integral_constant_expression_p = parser->integral_constant_expression_p;
9420 saved_allow_non_integral_constant_expression_p
9421 = parser->allow_non_integral_constant_expression_p;
9422 saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
9423 /* We are now parsing a constant-expression. */
9424 parser->integral_constant_expression_p = true;
9425 parser->allow_non_integral_constant_expression_p
9426 = (allow_non_constant_p || cxx_dialect >= cxx11);
9427 parser->non_integral_constant_expression_p = false;
9428 /* Although the grammar says "conditional-expression", we parse an
9429 "assignment-expression", which also permits "throw-expression"
9430 and the use of assignment operators. In the case that
9431 ALLOW_NON_CONSTANT_P is false, we get better errors than we would
9432 otherwise. In the case that ALLOW_NON_CONSTANT_P is true, it is
9433 actually essential that we look for an assignment-expression.
9434 For example, cp_parser_initializer_clauses uses this function to
9435 determine whether a particular assignment-expression is in fact
9436 constant. */
9437 expression = cp_parser_assignment_expression (parser);
9438 /* Restore the old settings. */
9439 parser->integral_constant_expression_p
9440 = saved_integral_constant_expression_p;
9441 parser->allow_non_integral_constant_expression_p
9442 = saved_allow_non_integral_constant_expression_p;
9443 if (cxx_dialect >= cxx11)
9445 /* Require an rvalue constant expression here; that's what our
9446 callers expect. Reference constant expressions are handled
9447 separately in e.g. cp_parser_template_argument. */
9448 bool is_const = potential_rvalue_constant_expression (expression);
9449 parser->non_integral_constant_expression_p = !is_const;
9450 if (!is_const && !allow_non_constant_p)
9451 require_potential_rvalue_constant_expression (expression);
9453 if (allow_non_constant_p)
9454 *non_constant_p = parser->non_integral_constant_expression_p;
9455 parser->non_integral_constant_expression_p
9456 = saved_non_integral_constant_expression_p;
9458 return expression;
9461 /* Parse __builtin_offsetof.
9463 offsetof-expression:
9464 "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
9466 offsetof-member-designator:
9467 id-expression
9468 | offsetof-member-designator "." id-expression
9469 | offsetof-member-designator "[" expression "]"
9470 | offsetof-member-designator "->" id-expression */
9472 static cp_expr
9473 cp_parser_builtin_offsetof (cp_parser *parser)
9475 int save_ice_p, save_non_ice_p;
9476 tree type;
9477 cp_expr expr;
9478 cp_id_kind dummy;
9479 cp_token *token;
9480 location_t finish_loc;
9482 /* We're about to accept non-integral-constant things, but will
9483 definitely yield an integral constant expression. Save and
9484 restore these values around our local parsing. */
9485 save_ice_p = parser->integral_constant_expression_p;
9486 save_non_ice_p = parser->non_integral_constant_expression_p;
9488 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
9490 /* Consume the "__builtin_offsetof" token. */
9491 cp_lexer_consume_token (parser->lexer);
9492 /* Consume the opening `('. */
9493 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
9494 /* Parse the type-id. */
9495 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
9496 type = cp_parser_type_id (parser);
9497 /* Look for the `,'. */
9498 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
9499 token = cp_lexer_peek_token (parser->lexer);
9501 /* Build the (type *)null that begins the traditional offsetof macro. */
9502 tree object_ptr
9503 = build_static_cast (build_pointer_type (type), null_pointer_node,
9504 tf_warning_or_error);
9506 /* Parse the offsetof-member-designator. We begin as if we saw "expr->". */
9507 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, object_ptr,
9508 true, &dummy, token->location);
9509 while (true)
9511 token = cp_lexer_peek_token (parser->lexer);
9512 switch (token->type)
9514 case CPP_OPEN_SQUARE:
9515 /* offsetof-member-designator "[" expression "]" */
9516 expr = cp_parser_postfix_open_square_expression (parser, expr,
9517 true, false);
9518 break;
9520 case CPP_DEREF:
9521 /* offsetof-member-designator "->" identifier */
9522 expr = grok_array_decl (token->location, expr,
9523 integer_zero_node, false);
9524 /* FALLTHRU */
9526 case CPP_DOT:
9527 /* offsetof-member-designator "." identifier */
9528 cp_lexer_consume_token (parser->lexer);
9529 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT,
9530 expr, true, &dummy,
9531 token->location);
9532 break;
9534 case CPP_CLOSE_PAREN:
9535 /* Consume the ")" token. */
9536 finish_loc = cp_lexer_peek_token (parser->lexer)->location;
9537 cp_lexer_consume_token (parser->lexer);
9538 goto success;
9540 default:
9541 /* Error. We know the following require will fail, but
9542 that gives the proper error message. */
9543 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
9544 cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
9545 expr = error_mark_node;
9546 goto failure;
9550 success:
9551 /* Make a location of the form:
9552 __builtin_offsetof (struct s, f)
9553 ~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~
9554 with caret at the type-id, ranging from the start of the
9555 "_builtin_offsetof" token to the close paren. */
9556 loc = make_location (loc, start_loc, finish_loc);
9557 /* The result will be an INTEGER_CST, so we need to explicitly
9558 preserve the location. */
9559 expr = cp_expr (finish_offsetof (object_ptr, expr, loc), loc);
9561 failure:
9562 parser->integral_constant_expression_p = save_ice_p;
9563 parser->non_integral_constant_expression_p = save_non_ice_p;
9565 return expr;
9568 /* Parse a trait expression.
9570 Returns a representation of the expression, the underlying type
9571 of the type at issue when KEYWORD is RID_UNDERLYING_TYPE. */
9573 static tree
9574 cp_parser_trait_expr (cp_parser* parser, enum rid keyword)
9576 cp_trait_kind kind;
9577 tree type1, type2 = NULL_TREE;
9578 bool binary = false;
9579 bool variadic = false;
9581 switch (keyword)
9583 case RID_HAS_NOTHROW_ASSIGN:
9584 kind = CPTK_HAS_NOTHROW_ASSIGN;
9585 break;
9586 case RID_HAS_NOTHROW_CONSTRUCTOR:
9587 kind = CPTK_HAS_NOTHROW_CONSTRUCTOR;
9588 break;
9589 case RID_HAS_NOTHROW_COPY:
9590 kind = CPTK_HAS_NOTHROW_COPY;
9591 break;
9592 case RID_HAS_TRIVIAL_ASSIGN:
9593 kind = CPTK_HAS_TRIVIAL_ASSIGN;
9594 break;
9595 case RID_HAS_TRIVIAL_CONSTRUCTOR:
9596 kind = CPTK_HAS_TRIVIAL_CONSTRUCTOR;
9597 break;
9598 case RID_HAS_TRIVIAL_COPY:
9599 kind = CPTK_HAS_TRIVIAL_COPY;
9600 break;
9601 case RID_HAS_TRIVIAL_DESTRUCTOR:
9602 kind = CPTK_HAS_TRIVIAL_DESTRUCTOR;
9603 break;
9604 case RID_HAS_UNIQUE_OBJ_REPRESENTATIONS:
9605 kind = CPTK_HAS_UNIQUE_OBJ_REPRESENTATIONS;
9606 break;
9607 case RID_HAS_VIRTUAL_DESTRUCTOR:
9608 kind = CPTK_HAS_VIRTUAL_DESTRUCTOR;
9609 break;
9610 case RID_IS_ABSTRACT:
9611 kind = CPTK_IS_ABSTRACT;
9612 break;
9613 case RID_IS_BASE_OF:
9614 kind = CPTK_IS_BASE_OF;
9615 binary = true;
9616 break;
9617 case RID_IS_CLASS:
9618 kind = CPTK_IS_CLASS;
9619 break;
9620 case RID_IS_EMPTY:
9621 kind = CPTK_IS_EMPTY;
9622 break;
9623 case RID_IS_ENUM:
9624 kind = CPTK_IS_ENUM;
9625 break;
9626 case RID_IS_FINAL:
9627 kind = CPTK_IS_FINAL;
9628 break;
9629 case RID_IS_LITERAL_TYPE:
9630 kind = CPTK_IS_LITERAL_TYPE;
9631 break;
9632 case RID_IS_POD:
9633 kind = CPTK_IS_POD;
9634 break;
9635 case RID_IS_POLYMORPHIC:
9636 kind = CPTK_IS_POLYMORPHIC;
9637 break;
9638 case RID_IS_SAME_AS:
9639 kind = CPTK_IS_SAME_AS;
9640 binary = true;
9641 break;
9642 case RID_IS_STD_LAYOUT:
9643 kind = CPTK_IS_STD_LAYOUT;
9644 break;
9645 case RID_IS_TRIVIAL:
9646 kind = CPTK_IS_TRIVIAL;
9647 break;
9648 case RID_IS_TRIVIALLY_ASSIGNABLE:
9649 kind = CPTK_IS_TRIVIALLY_ASSIGNABLE;
9650 binary = true;
9651 break;
9652 case RID_IS_TRIVIALLY_CONSTRUCTIBLE:
9653 kind = CPTK_IS_TRIVIALLY_CONSTRUCTIBLE;
9654 variadic = true;
9655 break;
9656 case RID_IS_TRIVIALLY_COPYABLE:
9657 kind = CPTK_IS_TRIVIALLY_COPYABLE;
9658 break;
9659 case RID_IS_UNION:
9660 kind = CPTK_IS_UNION;
9661 break;
9662 case RID_UNDERLYING_TYPE:
9663 kind = CPTK_UNDERLYING_TYPE;
9664 break;
9665 case RID_BASES:
9666 kind = CPTK_BASES;
9667 break;
9668 case RID_DIRECT_BASES:
9669 kind = CPTK_DIRECT_BASES;
9670 break;
9671 default:
9672 gcc_unreachable ();
9675 /* Consume the token. */
9676 cp_lexer_consume_token (parser->lexer);
9678 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
9681 type_id_in_expr_sentinel s (parser);
9682 type1 = cp_parser_type_id (parser);
9685 if (type1 == error_mark_node)
9686 return error_mark_node;
9688 if (binary)
9690 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
9693 type_id_in_expr_sentinel s (parser);
9694 type2 = cp_parser_type_id (parser);
9697 if (type2 == error_mark_node)
9698 return error_mark_node;
9700 else if (variadic)
9702 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
9704 cp_lexer_consume_token (parser->lexer);
9705 tree elt = cp_parser_type_id (parser);
9706 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
9708 cp_lexer_consume_token (parser->lexer);
9709 elt = make_pack_expansion (elt);
9711 if (elt == error_mark_node)
9712 return error_mark_node;
9713 type2 = tree_cons (NULL_TREE, elt, type2);
9717 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
9719 /* Complete the trait expression, which may mean either processing
9720 the trait expr now or saving it for template instantiation. */
9721 switch (kind)
9723 case CPTK_UNDERLYING_TYPE:
9724 return finish_underlying_type (type1);
9725 case CPTK_BASES:
9726 return finish_bases (type1, false);
9727 case CPTK_DIRECT_BASES:
9728 return finish_bases (type1, true);
9729 default:
9730 return finish_trait_expr (kind, type1, type2);
9734 /* Lambdas that appear in variable initializer or default argument scope
9735 get that in their mangling, so we need to record it. We might as well
9736 use the count for function and namespace scopes as well. */
9737 static GTY(()) tree lambda_scope;
9738 static GTY(()) int lambda_count;
9739 struct GTY(()) tree_int
9741 tree t;
9742 int i;
9744 static GTY(()) vec<tree_int, va_gc> *lambda_scope_stack;
9746 static void
9747 start_lambda_scope (tree decl)
9749 tree_int ti;
9750 gcc_assert (decl);
9751 /* Once we're inside a function, we ignore other scopes and just push
9752 the function again so that popping works properly. */
9753 if (current_function_decl && TREE_CODE (decl) != FUNCTION_DECL)
9754 decl = current_function_decl;
9755 ti.t = lambda_scope;
9756 ti.i = lambda_count;
9757 vec_safe_push (lambda_scope_stack, ti);
9758 if (lambda_scope != decl)
9760 /* Don't reset the count if we're still in the same function. */
9761 lambda_scope = decl;
9762 lambda_count = 0;
9766 static void
9767 record_lambda_scope (tree lambda)
9769 LAMBDA_EXPR_EXTRA_SCOPE (lambda) = lambda_scope;
9770 LAMBDA_EXPR_DISCRIMINATOR (lambda) = lambda_count++;
9773 static void
9774 finish_lambda_scope (void)
9776 tree_int *p = &lambda_scope_stack->last ();
9777 if (lambda_scope != p->t)
9779 lambda_scope = p->t;
9780 lambda_count = p->i;
9782 lambda_scope_stack->pop ();
9785 /* Parse a lambda expression.
9787 lambda-expression:
9788 lambda-introducer lambda-declarator [opt] compound-statement
9790 Returns a representation of the expression. */
9792 static cp_expr
9793 cp_parser_lambda_expression (cp_parser* parser)
9795 tree lambda_expr = build_lambda_expr ();
9796 tree type;
9797 bool ok = true;
9798 cp_token *token = cp_lexer_peek_token (parser->lexer);
9799 cp_token_position start = 0;
9801 LAMBDA_EXPR_LOCATION (lambda_expr) = token->location;
9803 if (cp_unevaluated_operand)
9805 if (!token->error_reported)
9807 error_at (LAMBDA_EXPR_LOCATION (lambda_expr),
9808 "lambda-expression in unevaluated context");
9809 token->error_reported = true;
9811 ok = false;
9813 else if (parser->in_template_argument_list_p)
9815 if (!token->error_reported)
9817 error_at (token->location, "lambda-expression in template-argument");
9818 token->error_reported = true;
9820 ok = false;
9823 /* We may be in the middle of deferred access check. Disable
9824 it now. */
9825 push_deferring_access_checks (dk_no_deferred);
9827 cp_parser_lambda_introducer (parser, lambda_expr);
9829 type = begin_lambda_type (lambda_expr);
9830 if (type == error_mark_node)
9831 return error_mark_node;
9833 record_lambda_scope (lambda_expr);
9835 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
9836 determine_visibility (TYPE_NAME (type));
9838 /* Now that we've started the type, add the capture fields for any
9839 explicit captures. */
9840 register_capture_members (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr));
9843 /* Inside the class, surrounding template-parameter-lists do not apply. */
9844 unsigned int saved_num_template_parameter_lists
9845 = parser->num_template_parameter_lists;
9846 unsigned char in_statement = parser->in_statement;
9847 bool in_switch_statement_p = parser->in_switch_statement_p;
9848 bool fully_implicit_function_template_p
9849 = parser->fully_implicit_function_template_p;
9850 tree implicit_template_parms = parser->implicit_template_parms;
9851 cp_binding_level* implicit_template_scope = parser->implicit_template_scope;
9852 bool auto_is_implicit_function_template_parm_p
9853 = parser->auto_is_implicit_function_template_parm_p;
9855 parser->num_template_parameter_lists = 0;
9856 parser->in_statement = 0;
9857 parser->in_switch_statement_p = false;
9858 parser->fully_implicit_function_template_p = false;
9859 parser->implicit_template_parms = 0;
9860 parser->implicit_template_scope = 0;
9861 parser->auto_is_implicit_function_template_parm_p = false;
9863 /* By virtue of defining a local class, a lambda expression has access to
9864 the private variables of enclosing classes. */
9866 ok &= cp_parser_lambda_declarator_opt (parser, lambda_expr);
9868 if (ok && cp_parser_error_occurred (parser))
9869 ok = false;
9871 if (ok)
9873 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
9874 && cp_parser_start_tentative_firewall (parser))
9875 start = token;
9876 cp_parser_lambda_body (parser, lambda_expr);
9878 else if (cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
9880 if (cp_parser_skip_to_closing_brace (parser))
9881 cp_lexer_consume_token (parser->lexer);
9884 /* The capture list was built up in reverse order; fix that now. */
9885 LAMBDA_EXPR_CAPTURE_LIST (lambda_expr)
9886 = nreverse (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr));
9888 if (ok)
9889 maybe_add_lambda_conv_op (type);
9891 type = finish_struct (type, /*attributes=*/NULL_TREE);
9893 parser->num_template_parameter_lists = saved_num_template_parameter_lists;
9894 parser->in_statement = in_statement;
9895 parser->in_switch_statement_p = in_switch_statement_p;
9896 parser->fully_implicit_function_template_p
9897 = fully_implicit_function_template_p;
9898 parser->implicit_template_parms = implicit_template_parms;
9899 parser->implicit_template_scope = implicit_template_scope;
9900 parser->auto_is_implicit_function_template_parm_p
9901 = auto_is_implicit_function_template_parm_p;
9904 /* This field is only used during parsing of the lambda. */
9905 LAMBDA_EXPR_THIS_CAPTURE (lambda_expr) = NULL_TREE;
9907 /* This lambda shouldn't have any proxies left at this point. */
9908 gcc_assert (LAMBDA_EXPR_PENDING_PROXIES (lambda_expr) == NULL);
9909 /* And now that we're done, push proxies for an enclosing lambda. */
9910 insert_pending_capture_proxies ();
9912 if (ok)
9913 lambda_expr = build_lambda_object (lambda_expr);
9914 else
9915 lambda_expr = error_mark_node;
9917 cp_parser_end_tentative_firewall (parser, start, lambda_expr);
9919 pop_deferring_access_checks ();
9921 return lambda_expr;
9924 /* Parse the beginning of a lambda expression.
9926 lambda-introducer:
9927 [ lambda-capture [opt] ]
9929 LAMBDA_EXPR is the current representation of the lambda expression. */
9931 static void
9932 cp_parser_lambda_introducer (cp_parser* parser, tree lambda_expr)
9934 /* Need commas after the first capture. */
9935 bool first = true;
9937 /* Eat the leading `['. */
9938 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
9940 /* Record default capture mode. "[&" "[=" "[&," "[=," */
9941 if (cp_lexer_next_token_is (parser->lexer, CPP_AND)
9942 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_NAME)
9943 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_REFERENCE;
9944 else if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
9945 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_COPY;
9947 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE)
9949 cp_lexer_consume_token (parser->lexer);
9950 first = false;
9953 while (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_SQUARE))
9955 cp_token* capture_token;
9956 tree capture_id;
9957 tree capture_init_expr;
9958 cp_id_kind idk = CP_ID_KIND_NONE;
9959 bool explicit_init_p = false;
9961 enum capture_kind_type
9963 BY_COPY,
9964 BY_REFERENCE
9966 enum capture_kind_type capture_kind = BY_COPY;
9968 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
9970 error ("expected end of capture-list");
9971 return;
9974 if (first)
9975 first = false;
9976 else
9977 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
9979 /* Possibly capture `this'. */
9980 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THIS))
9982 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
9983 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_COPY)
9984 pedwarn (loc, 0, "explicit by-copy capture of %<this%> redundant "
9985 "with by-copy capture default");
9986 cp_lexer_consume_token (parser->lexer);
9987 add_capture (lambda_expr,
9988 /*id=*/this_identifier,
9989 /*initializer=*/finish_this_expr (),
9990 /*by_reference_p=*/true,
9991 explicit_init_p);
9992 continue;
9995 /* Possibly capture `*this'. */
9996 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT)
9997 && cp_lexer_nth_token_is_keyword (parser->lexer, 2, RID_THIS))
9999 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
10000 if (cxx_dialect < cxx1z)
10001 pedwarn (loc, 0, "%<*this%> capture only available with "
10002 "-std=c++1z or -std=gnu++1z");
10003 cp_lexer_consume_token (parser->lexer);
10004 cp_lexer_consume_token (parser->lexer);
10005 add_capture (lambda_expr,
10006 /*id=*/this_identifier,
10007 /*initializer=*/finish_this_expr (),
10008 /*by_reference_p=*/false,
10009 explicit_init_p);
10010 continue;
10013 /* Remember whether we want to capture as a reference or not. */
10014 if (cp_lexer_next_token_is (parser->lexer, CPP_AND))
10016 capture_kind = BY_REFERENCE;
10017 cp_lexer_consume_token (parser->lexer);
10020 /* Get the identifier. */
10021 capture_token = cp_lexer_peek_token (parser->lexer);
10022 capture_id = cp_parser_identifier (parser);
10024 if (capture_id == error_mark_node)
10025 /* Would be nice to have a cp_parser_skip_to_closing_x for general
10026 delimiters, but I modified this to stop on unnested ']' as well. It
10027 was already changed to stop on unnested '}', so the
10028 "closing_parenthesis" name is no more misleading with my change. */
10030 cp_parser_skip_to_closing_parenthesis (parser,
10031 /*recovering=*/true,
10032 /*or_comma=*/true,
10033 /*consume_paren=*/true);
10034 break;
10037 /* Find the initializer for this capture. */
10038 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ)
10039 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
10040 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
10042 bool direct, non_constant;
10043 /* An explicit initializer exists. */
10044 if (cxx_dialect < cxx14)
10045 pedwarn (input_location, 0,
10046 "lambda capture initializers "
10047 "only available with -std=c++14 or -std=gnu++14");
10048 capture_init_expr = cp_parser_initializer (parser, &direct,
10049 &non_constant);
10050 explicit_init_p = true;
10051 if (capture_init_expr == NULL_TREE)
10053 error ("empty initializer for lambda init-capture");
10054 capture_init_expr = error_mark_node;
10057 else
10059 const char* error_msg;
10061 /* Turn the identifier into an id-expression. */
10062 capture_init_expr
10063 = cp_parser_lookup_name_simple (parser, capture_id,
10064 capture_token->location);
10066 if (capture_init_expr == error_mark_node)
10068 unqualified_name_lookup_error (capture_id);
10069 continue;
10071 else if (DECL_P (capture_init_expr)
10072 && (!VAR_P (capture_init_expr)
10073 && TREE_CODE (capture_init_expr) != PARM_DECL))
10075 error_at (capture_token->location,
10076 "capture of non-variable %qD ",
10077 capture_init_expr);
10078 inform (DECL_SOURCE_LOCATION (capture_init_expr),
10079 "%q#D declared here", capture_init_expr);
10080 continue;
10082 if (VAR_P (capture_init_expr)
10083 && decl_storage_duration (capture_init_expr) != dk_auto)
10085 if (pedwarn (capture_token->location, 0, "capture of variable "
10086 "%qD with non-automatic storage duration",
10087 capture_init_expr))
10088 inform (DECL_SOURCE_LOCATION (capture_init_expr),
10089 "%q#D declared here", capture_init_expr);
10090 continue;
10093 capture_init_expr
10094 = finish_id_expression
10095 (capture_id,
10096 capture_init_expr,
10097 parser->scope,
10098 &idk,
10099 /*integral_constant_expression_p=*/false,
10100 /*allow_non_integral_constant_expression_p=*/false,
10101 /*non_integral_constant_expression_p=*/NULL,
10102 /*template_p=*/false,
10103 /*done=*/true,
10104 /*address_p=*/false,
10105 /*template_arg_p=*/false,
10106 &error_msg,
10107 capture_token->location);
10109 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
10111 cp_lexer_consume_token (parser->lexer);
10112 capture_init_expr = make_pack_expansion (capture_init_expr);
10114 else
10115 check_for_bare_parameter_packs (capture_init_expr);
10118 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE
10119 && !explicit_init_p)
10121 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_COPY
10122 && capture_kind == BY_COPY)
10123 pedwarn (capture_token->location, 0, "explicit by-copy capture "
10124 "of %qD redundant with by-copy capture default",
10125 capture_id);
10126 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_REFERENCE
10127 && capture_kind == BY_REFERENCE)
10128 pedwarn (capture_token->location, 0, "explicit by-reference "
10129 "capture of %qD redundant with by-reference capture "
10130 "default", capture_id);
10133 add_capture (lambda_expr,
10134 capture_id,
10135 capture_init_expr,
10136 /*by_reference_p=*/capture_kind == BY_REFERENCE,
10137 explicit_init_p);
10140 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
10143 /* Parse the (optional) middle of a lambda expression.
10145 lambda-declarator:
10146 < template-parameter-list [opt] >
10147 ( parameter-declaration-clause [opt] )
10148 attribute-specifier [opt]
10149 decl-specifier-seq [opt]
10150 exception-specification [opt]
10151 lambda-return-type-clause [opt]
10153 LAMBDA_EXPR is the current representation of the lambda expression. */
10155 static bool
10156 cp_parser_lambda_declarator_opt (cp_parser* parser, tree lambda_expr)
10158 /* 5.1.1.4 of the standard says:
10159 If a lambda-expression does not include a lambda-declarator, it is as if
10160 the lambda-declarator were ().
10161 This means an empty parameter list, no attributes, and no exception
10162 specification. */
10163 tree param_list = void_list_node;
10164 tree attributes = NULL_TREE;
10165 tree exception_spec = NULL_TREE;
10166 tree template_param_list = NULL_TREE;
10167 tree tx_qual = NULL_TREE;
10168 cp_decl_specifier_seq lambda_specs;
10169 clear_decl_specs (&lambda_specs);
10171 /* The template-parameter-list is optional, but must begin with
10172 an opening angle if present. */
10173 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
10175 if (cxx_dialect < cxx14)
10176 pedwarn (parser->lexer->next_token->location, 0,
10177 "lambda templates are only available with "
10178 "-std=c++14 or -std=gnu++14");
10179 else
10180 pedwarn (parser->lexer->next_token->location, OPT_Wpedantic,
10181 "ISO C++ does not support lambda templates");
10183 cp_lexer_consume_token (parser->lexer);
10185 template_param_list = cp_parser_template_parameter_list (parser);
10187 cp_parser_skip_to_end_of_template_parameter_list (parser);
10189 /* We just processed one more parameter list. */
10190 ++parser->num_template_parameter_lists;
10193 /* The parameter-declaration-clause is optional (unless
10194 template-parameter-list was given), but must begin with an
10195 opening parenthesis if present. */
10196 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
10198 cp_lexer_consume_token (parser->lexer);
10200 begin_scope (sk_function_parms, /*entity=*/NULL_TREE);
10202 /* Parse parameters. */
10203 param_list = cp_parser_parameter_declaration_clause (parser);
10205 /* Default arguments shall not be specified in the
10206 parameter-declaration-clause of a lambda-declarator. */
10207 if (cxx_dialect < cxx14)
10208 for (tree t = param_list; t; t = TREE_CHAIN (t))
10209 if (TREE_PURPOSE (t) && DECL_P (TREE_VALUE (t)))
10210 pedwarn (DECL_SOURCE_LOCATION (TREE_VALUE (t)), OPT_Wpedantic,
10211 "default argument specified for lambda parameter");
10213 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
10215 attributes = cp_parser_attributes_opt (parser);
10217 /* In the decl-specifier-seq of the lambda-declarator, each
10218 decl-specifier shall either be mutable or constexpr. */
10219 int declares_class_or_enum;
10220 if (cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer))
10221 cp_parser_decl_specifier_seq (parser,
10222 CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR,
10223 &lambda_specs, &declares_class_or_enum);
10224 if (lambda_specs.storage_class == sc_mutable)
10226 LAMBDA_EXPR_MUTABLE_P (lambda_expr) = 1;
10227 if (lambda_specs.conflicting_specifiers_p)
10228 error_at (lambda_specs.locations[ds_storage_class],
10229 "duplicate %<mutable%>");
10232 tx_qual = cp_parser_tx_qualifier_opt (parser);
10234 /* Parse optional exception specification. */
10235 exception_spec = cp_parser_exception_specification_opt (parser);
10237 /* Parse optional trailing return type. */
10238 if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
10240 cp_lexer_consume_token (parser->lexer);
10241 LAMBDA_EXPR_RETURN_TYPE (lambda_expr)
10242 = cp_parser_trailing_type_id (parser);
10245 /* The function parameters must be in scope all the way until after the
10246 trailing-return-type in case of decltype. */
10247 pop_bindings_and_leave_scope ();
10249 else if (template_param_list != NULL_TREE) // generate diagnostic
10250 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
10252 /* Create the function call operator.
10254 Messing with declarators like this is no uglier than building up the
10255 FUNCTION_DECL by hand, and this is less likely to get out of sync with
10256 other code. */
10258 cp_decl_specifier_seq return_type_specs;
10259 cp_declarator* declarator;
10260 tree fco;
10261 int quals;
10262 void *p;
10264 clear_decl_specs (&return_type_specs);
10265 if (LAMBDA_EXPR_RETURN_TYPE (lambda_expr))
10266 return_type_specs.type = LAMBDA_EXPR_RETURN_TYPE (lambda_expr);
10267 else
10268 /* Maybe we will deduce the return type later. */
10269 return_type_specs.type = make_auto ();
10271 if (lambda_specs.locations[ds_constexpr])
10273 if (cxx_dialect >= cxx1z)
10274 return_type_specs.locations[ds_constexpr]
10275 = lambda_specs.locations[ds_constexpr];
10276 else
10277 error_at (lambda_specs.locations[ds_constexpr], "%<constexpr%> "
10278 "lambda only available with -std=c++1z or -std=gnu++1z");
10281 p = obstack_alloc (&declarator_obstack, 0);
10283 declarator = make_id_declarator (NULL_TREE, cp_operator_id (CALL_EXPR),
10284 sfk_none);
10286 quals = (LAMBDA_EXPR_MUTABLE_P (lambda_expr)
10287 ? TYPE_UNQUALIFIED : TYPE_QUAL_CONST);
10288 declarator = make_call_declarator (declarator, param_list, quals,
10289 VIRT_SPEC_UNSPECIFIED,
10290 REF_QUAL_NONE,
10291 tx_qual,
10292 exception_spec,
10293 /*late_return_type=*/NULL_TREE,
10294 /*requires_clause*/NULL_TREE);
10295 declarator->id_loc = LAMBDA_EXPR_LOCATION (lambda_expr);
10297 fco = grokmethod (&return_type_specs,
10298 declarator,
10299 attributes);
10300 if (fco != error_mark_node)
10302 DECL_INITIALIZED_IN_CLASS_P (fco) = 1;
10303 DECL_ARTIFICIAL (fco) = 1;
10304 /* Give the object parameter a different name. */
10305 DECL_NAME (DECL_ARGUMENTS (fco)) = get_identifier ("__closure");
10306 if (LAMBDA_EXPR_RETURN_TYPE (lambda_expr))
10307 TYPE_HAS_LATE_RETURN_TYPE (TREE_TYPE (fco)) = 1;
10309 if (template_param_list)
10311 fco = finish_member_template_decl (fco);
10312 finish_template_decl (template_param_list);
10313 --parser->num_template_parameter_lists;
10315 else if (parser->fully_implicit_function_template_p)
10316 fco = finish_fully_implicit_template (parser, fco);
10318 finish_member_declaration (fco);
10320 obstack_free (&declarator_obstack, p);
10322 return (fco != error_mark_node);
10326 /* Parse the body of a lambda expression, which is simply
10328 compound-statement
10330 but which requires special handling.
10331 LAMBDA_EXPR is the current representation of the lambda expression. */
10333 static void
10334 cp_parser_lambda_body (cp_parser* parser, tree lambda_expr)
10336 bool nested = (current_function_decl != NULL_TREE);
10337 bool local_variables_forbidden_p = parser->local_variables_forbidden_p;
10338 if (nested)
10339 push_function_context ();
10340 else
10341 /* Still increment function_depth so that we don't GC in the
10342 middle of an expression. */
10343 ++function_depth;
10344 vec<tree> omp_privatization_save;
10345 save_omp_privatization_clauses (omp_privatization_save);
10346 /* Clear this in case we're in the middle of a default argument. */
10347 parser->local_variables_forbidden_p = false;
10349 /* Finish the function call operator
10350 - class_specifier
10351 + late_parsing_for_member
10352 + function_definition_after_declarator
10353 + ctor_initializer_opt_and_function_body */
10355 tree fco = lambda_function (lambda_expr);
10356 tree body;
10357 bool done = false;
10358 tree compound_stmt;
10359 tree cap;
10361 /* Let the front end know that we are going to be defining this
10362 function. */
10363 start_preparsed_function (fco,
10364 NULL_TREE,
10365 SF_PRE_PARSED | SF_INCLASS_INLINE);
10367 start_lambda_scope (fco);
10368 body = begin_function_body ();
10370 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
10371 goto out;
10373 /* Push the proxies for any explicit captures. */
10374 for (cap = LAMBDA_EXPR_CAPTURE_LIST (lambda_expr); cap;
10375 cap = TREE_CHAIN (cap))
10376 build_capture_proxy (TREE_PURPOSE (cap));
10378 compound_stmt = begin_compound_stmt (0);
10380 /* 5.1.1.4 of the standard says:
10381 If a lambda-expression does not include a trailing-return-type, it
10382 is as if the trailing-return-type denotes the following type:
10383 * if the compound-statement is of the form
10384 { return attribute-specifier [opt] expression ; }
10385 the type of the returned expression after lvalue-to-rvalue
10386 conversion (_conv.lval_ 4.1), array-to-pointer conversion
10387 (_conv.array_ 4.2), and function-to-pointer conversion
10388 (_conv.func_ 4.3);
10389 * otherwise, void. */
10391 /* In a lambda that has neither a lambda-return-type-clause
10392 nor a deducible form, errors should be reported for return statements
10393 in the body. Since we used void as the placeholder return type, parsing
10394 the body as usual will give such desired behavior. */
10395 if (!LAMBDA_EXPR_RETURN_TYPE (lambda_expr)
10396 && cp_lexer_peek_nth_token (parser->lexer, 1)->keyword == RID_RETURN
10397 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SEMICOLON)
10399 tree expr = NULL_TREE;
10400 cp_id_kind idk = CP_ID_KIND_NONE;
10402 /* Parse tentatively in case there's more after the initial return
10403 statement. */
10404 cp_parser_parse_tentatively (parser);
10406 cp_parser_require_keyword (parser, RID_RETURN, RT_RETURN);
10408 expr = cp_parser_expression (parser, &idk);
10410 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10411 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
10413 if (cp_parser_parse_definitely (parser))
10415 if (!processing_template_decl)
10417 tree type = lambda_return_type (expr);
10418 apply_deduced_return_type (fco, type);
10419 if (type == error_mark_node)
10420 expr = error_mark_node;
10423 /* Will get error here if type not deduced yet. */
10424 finish_return_stmt (expr);
10426 done = true;
10430 if (!done)
10432 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
10433 cp_parser_label_declaration (parser);
10434 cp_parser_statement_seq_opt (parser, NULL_TREE);
10435 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
10438 finish_compound_stmt (compound_stmt);
10440 out:
10441 finish_function_body (body);
10442 finish_lambda_scope ();
10444 /* Finish the function and generate code for it if necessary. */
10445 tree fn = finish_function (/*inline*/2);
10447 /* Only expand if the call op is not a template. */
10448 if (!DECL_TEMPLATE_INFO (fco))
10449 expand_or_defer_fn (fn);
10452 restore_omp_privatization_clauses (omp_privatization_save);
10453 parser->local_variables_forbidden_p = local_variables_forbidden_p;
10454 if (nested)
10455 pop_function_context();
10456 else
10457 --function_depth;
10460 /* Statements [gram.stmt.stmt] */
10462 /* Parse a statement.
10464 statement:
10465 labeled-statement
10466 expression-statement
10467 compound-statement
10468 selection-statement
10469 iteration-statement
10470 jump-statement
10471 declaration-statement
10472 try-block
10474 C++11:
10476 statement:
10477 labeled-statement
10478 attribute-specifier-seq (opt) expression-statement
10479 attribute-specifier-seq (opt) compound-statement
10480 attribute-specifier-seq (opt) selection-statement
10481 attribute-specifier-seq (opt) iteration-statement
10482 attribute-specifier-seq (opt) jump-statement
10483 declaration-statement
10484 attribute-specifier-seq (opt) try-block
10486 init-statement:
10487 expression-statement
10488 simple-declaration
10490 TM Extension:
10492 statement:
10493 atomic-statement
10495 IN_COMPOUND is true when the statement is nested inside a
10496 cp_parser_compound_statement; this matters for certain pragmas.
10498 If IF_P is not NULL, *IF_P is set to indicate whether the statement
10499 is a (possibly labeled) if statement which is not enclosed in braces
10500 and has an else clause. This is used to implement -Wparentheses.
10502 CHAIN is a vector of if-else-if conditions. */
10504 static void
10505 cp_parser_statement (cp_parser* parser, tree in_statement_expr,
10506 bool in_compound, bool *if_p, vec<tree> *chain)
10508 tree statement, std_attrs = NULL_TREE;
10509 cp_token *token;
10510 location_t statement_location, attrs_location;
10512 restart:
10513 if (if_p != NULL)
10514 *if_p = false;
10515 /* There is no statement yet. */
10516 statement = NULL_TREE;
10518 saved_token_sentinel saved_tokens (parser->lexer);
10519 attrs_location = cp_lexer_peek_token (parser->lexer)->location;
10520 if (c_dialect_objc ())
10521 /* In obj-c++, seeing '[[' might be the either the beginning of
10522 c++11 attributes, or a nested objc-message-expression. So
10523 let's parse the c++11 attributes tentatively. */
10524 cp_parser_parse_tentatively (parser);
10525 std_attrs = cp_parser_std_attribute_spec_seq (parser);
10526 if (c_dialect_objc ())
10528 if (!cp_parser_parse_definitely (parser))
10529 std_attrs = NULL_TREE;
10532 /* Peek at the next token. */
10533 token = cp_lexer_peek_token (parser->lexer);
10534 /* Remember the location of the first token in the statement. */
10535 statement_location = token->location;
10536 /* If this is a keyword, then that will often determine what kind of
10537 statement we have. */
10538 if (token->type == CPP_KEYWORD)
10540 enum rid keyword = token->keyword;
10542 switch (keyword)
10544 case RID_CASE:
10545 case RID_DEFAULT:
10546 /* Looks like a labeled-statement with a case label.
10547 Parse the label, and then use tail recursion to parse
10548 the statement. */
10549 cp_parser_label_for_labeled_statement (parser, std_attrs);
10550 in_compound = false;
10551 goto restart;
10553 case RID_IF:
10554 case RID_SWITCH:
10555 statement = cp_parser_selection_statement (parser, if_p, chain);
10556 break;
10558 case RID_WHILE:
10559 case RID_DO:
10560 case RID_FOR:
10561 statement = cp_parser_iteration_statement (parser, if_p, false);
10562 break;
10564 case RID_CILK_FOR:
10565 if (!flag_cilkplus)
10567 error_at (cp_lexer_peek_token (parser->lexer)->location,
10568 "-fcilkplus must be enabled to use %<_Cilk_for%>");
10569 cp_lexer_consume_token (parser->lexer);
10570 statement = error_mark_node;
10572 else
10573 statement = cp_parser_cilk_for (parser, integer_zero_node, if_p);
10574 break;
10576 case RID_BREAK:
10577 case RID_CONTINUE:
10578 case RID_RETURN:
10579 case RID_GOTO:
10580 statement = cp_parser_jump_statement (parser);
10581 break;
10583 case RID_CILK_SYNC:
10584 cp_lexer_consume_token (parser->lexer);
10585 if (flag_cilkplus)
10587 tree sync_expr = build_cilk_sync ();
10588 SET_EXPR_LOCATION (sync_expr,
10589 token->location);
10590 statement = finish_expr_stmt (sync_expr);
10592 else
10594 error_at (token->location, "-fcilkplus must be enabled to use"
10595 " %<_Cilk_sync%>");
10596 statement = error_mark_node;
10598 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10599 break;
10601 /* Objective-C++ exception-handling constructs. */
10602 case RID_AT_TRY:
10603 case RID_AT_CATCH:
10604 case RID_AT_FINALLY:
10605 case RID_AT_SYNCHRONIZED:
10606 case RID_AT_THROW:
10607 statement = cp_parser_objc_statement (parser);
10608 break;
10610 case RID_TRY:
10611 statement = cp_parser_try_block (parser);
10612 break;
10614 case RID_NAMESPACE:
10615 /* This must be a namespace alias definition. */
10616 cp_parser_declaration_statement (parser);
10617 return;
10619 case RID_TRANSACTION_ATOMIC:
10620 case RID_TRANSACTION_RELAXED:
10621 case RID_SYNCHRONIZED:
10622 case RID_ATOMIC_NOEXCEPT:
10623 case RID_ATOMIC_CANCEL:
10624 statement = cp_parser_transaction (parser, token);
10625 break;
10626 case RID_TRANSACTION_CANCEL:
10627 statement = cp_parser_transaction_cancel (parser);
10628 break;
10630 default:
10631 /* It might be a keyword like `int' that can start a
10632 declaration-statement. */
10633 break;
10636 else if (token->type == CPP_NAME)
10638 /* If the next token is a `:', then we are looking at a
10639 labeled-statement. */
10640 token = cp_lexer_peek_nth_token (parser->lexer, 2);
10641 if (token->type == CPP_COLON)
10643 /* Looks like a labeled-statement with an ordinary label.
10644 Parse the label, and then use tail recursion to parse
10645 the statement. */
10647 cp_parser_label_for_labeled_statement (parser, std_attrs);
10648 in_compound = false;
10649 goto restart;
10652 /* Anything that starts with a `{' must be a compound-statement. */
10653 else if (token->type == CPP_OPEN_BRACE)
10654 statement = cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
10655 /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
10656 a statement all its own. */
10657 else if (token->type == CPP_PRAGMA)
10659 /* Only certain OpenMP pragmas are attached to statements, and thus
10660 are considered statements themselves. All others are not. In
10661 the context of a compound, accept the pragma as a "statement" and
10662 return so that we can check for a close brace. Otherwise we
10663 require a real statement and must go back and read one. */
10664 if (in_compound)
10665 cp_parser_pragma (parser, pragma_compound, if_p);
10666 else if (!cp_parser_pragma (parser, pragma_stmt, if_p))
10667 goto restart;
10668 return;
10670 else if (token->type == CPP_EOF)
10672 cp_parser_error (parser, "expected statement");
10673 return;
10676 /* Everything else must be a declaration-statement or an
10677 expression-statement. Try for the declaration-statement
10678 first, unless we are looking at a `;', in which case we know that
10679 we have an expression-statement. */
10680 if (!statement)
10682 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
10684 if (std_attrs != NULL_TREE)
10686 /* Attributes should be parsed as part of the the
10687 declaration, so let's un-parse them. */
10688 saved_tokens.rollback();
10689 std_attrs = NULL_TREE;
10692 cp_parser_parse_tentatively (parser);
10693 /* Try to parse the declaration-statement. */
10694 cp_parser_declaration_statement (parser);
10695 /* If that worked, we're done. */
10696 if (cp_parser_parse_definitely (parser))
10697 return;
10699 /* Look for an expression-statement instead. */
10700 statement = cp_parser_expression_statement (parser, in_statement_expr);
10702 /* Handle [[fallthrough]];. */
10703 if (attribute_fallthrough_p (std_attrs))
10705 /* The next token after the fallthrough attribute is ';'. */
10706 if (statement == NULL_TREE)
10708 /* Turn [[fallthrough]]; into FALLTHROUGH ();. */
10709 statement = build_call_expr_internal_loc (statement_location,
10710 IFN_FALLTHROUGH,
10711 void_type_node, 0);
10712 finish_expr_stmt (statement);
10714 else
10715 warning_at (statement_location, OPT_Wattributes,
10716 "%<fallthrough%> attribute not followed by %<;%>");
10717 std_attrs = NULL_TREE;
10721 /* Set the line number for the statement. */
10722 if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
10723 SET_EXPR_LOCATION (statement, statement_location);
10725 /* Allow "[[fallthrough]];", but warn otherwise. */
10726 if (std_attrs != NULL_TREE)
10727 warning_at (attrs_location,
10728 OPT_Wattributes,
10729 "attributes at the beginning of statement are ignored");
10732 /* Parse the label for a labeled-statement, i.e.
10734 identifier :
10735 case constant-expression :
10736 default :
10738 GNU Extension:
10739 case constant-expression ... constant-expression : statement
10741 When a label is parsed without errors, the label is added to the
10742 parse tree by the finish_* functions, so this function doesn't
10743 have to return the label. */
10745 static void
10746 cp_parser_label_for_labeled_statement (cp_parser* parser, tree attributes)
10748 cp_token *token;
10749 tree label = NULL_TREE;
10750 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
10752 /* The next token should be an identifier. */
10753 token = cp_lexer_peek_token (parser->lexer);
10754 if (token->type != CPP_NAME
10755 && token->type != CPP_KEYWORD)
10757 cp_parser_error (parser, "expected labeled-statement");
10758 return;
10761 /* Remember whether this case or a user-defined label is allowed to fall
10762 through to. */
10763 bool fallthrough_p = token->flags & PREV_FALLTHROUGH;
10765 parser->colon_corrects_to_scope_p = false;
10766 switch (token->keyword)
10768 case RID_CASE:
10770 tree expr, expr_hi;
10771 cp_token *ellipsis;
10773 /* Consume the `case' token. */
10774 cp_lexer_consume_token (parser->lexer);
10775 /* Parse the constant-expression. */
10776 expr = cp_parser_constant_expression (parser);
10777 if (check_for_bare_parameter_packs (expr))
10778 expr = error_mark_node;
10780 ellipsis = cp_lexer_peek_token (parser->lexer);
10781 if (ellipsis->type == CPP_ELLIPSIS)
10783 /* Consume the `...' token. */
10784 cp_lexer_consume_token (parser->lexer);
10785 expr_hi = cp_parser_constant_expression (parser);
10786 if (check_for_bare_parameter_packs (expr_hi))
10787 expr_hi = error_mark_node;
10789 /* We don't need to emit warnings here, as the common code
10790 will do this for us. */
10792 else
10793 expr_hi = NULL_TREE;
10795 if (parser->in_switch_statement_p)
10797 tree l = finish_case_label (token->location, expr, expr_hi);
10798 if (l && TREE_CODE (l) == CASE_LABEL_EXPR)
10799 FALLTHROUGH_LABEL_P (CASE_LABEL (l)) = fallthrough_p;
10801 else
10802 error_at (token->location,
10803 "case label %qE not within a switch statement",
10804 expr);
10806 break;
10808 case RID_DEFAULT:
10809 /* Consume the `default' token. */
10810 cp_lexer_consume_token (parser->lexer);
10812 if (parser->in_switch_statement_p)
10814 tree l = finish_case_label (token->location, NULL_TREE, NULL_TREE);
10815 if (l && TREE_CODE (l) == CASE_LABEL_EXPR)
10816 FALLTHROUGH_LABEL_P (CASE_LABEL (l)) = fallthrough_p;
10818 else
10819 error_at (token->location, "case label not within a switch statement");
10820 break;
10822 default:
10823 /* Anything else must be an ordinary label. */
10824 label = finish_label_stmt (cp_parser_identifier (parser));
10825 if (label && TREE_CODE (label) == LABEL_DECL)
10826 FALLTHROUGH_LABEL_P (label) = fallthrough_p;
10827 break;
10830 /* Require the `:' token. */
10831 cp_parser_require (parser, CPP_COLON, RT_COLON);
10833 /* An ordinary label may optionally be followed by attributes.
10834 However, this is only permitted if the attributes are then
10835 followed by a semicolon. This is because, for backward
10836 compatibility, when parsing
10837 lab: __attribute__ ((unused)) int i;
10838 we want the attribute to attach to "i", not "lab". */
10839 if (label != NULL_TREE
10840 && cp_next_tokens_can_be_gnu_attribute_p (parser))
10842 tree attrs;
10843 cp_parser_parse_tentatively (parser);
10844 attrs = cp_parser_gnu_attributes_opt (parser);
10845 if (attrs == NULL_TREE
10846 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
10847 cp_parser_abort_tentative_parse (parser);
10848 else if (!cp_parser_parse_definitely (parser))
10850 else
10851 attributes = chainon (attributes, attrs);
10854 if (attributes != NULL_TREE)
10855 cplus_decl_attributes (&label, attributes, 0);
10857 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
10860 /* Parse an expression-statement.
10862 expression-statement:
10863 expression [opt] ;
10865 Returns the new EXPR_STMT -- or NULL_TREE if the expression
10866 statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
10867 indicates whether this expression-statement is part of an
10868 expression statement. */
10870 static tree
10871 cp_parser_expression_statement (cp_parser* parser, tree in_statement_expr)
10873 tree statement = NULL_TREE;
10874 cp_token *token = cp_lexer_peek_token (parser->lexer);
10875 location_t loc = token->location;
10877 /* There might be attribute fallthrough. */
10878 tree attr = cp_parser_gnu_attributes_opt (parser);
10880 /* If the next token is a ';', then there is no expression
10881 statement. */
10882 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
10884 statement = cp_parser_expression (parser);
10885 if (statement == error_mark_node
10886 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
10888 cp_parser_skip_to_end_of_block_or_statement (parser);
10889 return error_mark_node;
10893 /* Handle [[fallthrough]];. */
10894 if (attribute_fallthrough_p (attr))
10896 /* The next token after the fallthrough attribute is ';'. */
10897 if (statement == NULL_TREE)
10898 /* Turn [[fallthrough]]; into FALLTHROUGH ();. */
10899 statement = build_call_expr_internal_loc (loc, IFN_FALLTHROUGH,
10900 void_type_node, 0);
10901 else
10902 warning_at (loc, OPT_Wattributes,
10903 "%<fallthrough%> attribute not followed by %<;%>");
10904 attr = NULL_TREE;
10907 /* Allow "[[fallthrough]];", but warn otherwise. */
10908 if (attr != NULL_TREE)
10909 warning_at (loc, OPT_Wattributes,
10910 "attributes at the beginning of statement are ignored");
10912 /* Give a helpful message for "A<T>::type t;" and the like. */
10913 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
10914 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
10916 if (TREE_CODE (statement) == SCOPE_REF)
10917 error_at (token->location, "need %<typename%> before %qE because "
10918 "%qT is a dependent scope",
10919 statement, TREE_OPERAND (statement, 0));
10920 else if (is_overloaded_fn (statement)
10921 && DECL_CONSTRUCTOR_P (get_first_fn (statement)))
10923 /* A::A a; */
10924 tree fn = get_first_fn (statement);
10925 error_at (token->location,
10926 "%<%T::%D%> names the constructor, not the type",
10927 DECL_CONTEXT (fn), DECL_NAME (fn));
10931 /* Consume the final `;'. */
10932 cp_parser_consume_semicolon_at_end_of_statement (parser);
10934 if (in_statement_expr
10935 && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
10936 /* This is the final expression statement of a statement
10937 expression. */
10938 statement = finish_stmt_expr_expr (statement, in_statement_expr);
10939 else if (statement)
10940 statement = finish_expr_stmt (statement);
10942 return statement;
10945 /* Parse a compound-statement.
10947 compound-statement:
10948 { statement-seq [opt] }
10950 GNU extension:
10952 compound-statement:
10953 { label-declaration-seq [opt] statement-seq [opt] }
10955 label-declaration-seq:
10956 label-declaration
10957 label-declaration-seq label-declaration
10959 Returns a tree representing the statement. */
10961 static tree
10962 cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr,
10963 int bcs_flags, bool function_body)
10965 tree compound_stmt;
10967 /* Consume the `{'. */
10968 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
10969 return error_mark_node;
10970 if (DECL_DECLARED_CONSTEXPR_P (current_function_decl)
10971 && !function_body && cxx_dialect < cxx14)
10972 pedwarn (input_location, OPT_Wpedantic,
10973 "compound-statement in constexpr function");
10974 /* Begin the compound-statement. */
10975 compound_stmt = begin_compound_stmt (bcs_flags);
10976 /* If the next keyword is `__label__' we have a label declaration. */
10977 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
10978 cp_parser_label_declaration (parser);
10979 /* Parse an (optional) statement-seq. */
10980 cp_parser_statement_seq_opt (parser, in_statement_expr);
10981 /* Finish the compound-statement. */
10982 finish_compound_stmt (compound_stmt);
10983 /* Consume the `}'. */
10984 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
10986 return compound_stmt;
10989 /* Parse an (optional) statement-seq.
10991 statement-seq:
10992 statement
10993 statement-seq [opt] statement */
10995 static void
10996 cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr)
10998 /* Scan statements until there aren't any more. */
10999 while (true)
11001 cp_token *token = cp_lexer_peek_token (parser->lexer);
11003 /* If we are looking at a `}', then we have run out of
11004 statements; the same is true if we have reached the end
11005 of file, or have stumbled upon a stray '@end'. */
11006 if (token->type == CPP_CLOSE_BRACE
11007 || token->type == CPP_EOF
11008 || token->type == CPP_PRAGMA_EOL
11009 || (token->type == CPP_KEYWORD && token->keyword == RID_AT_END))
11010 break;
11012 /* If we are in a compound statement and find 'else' then
11013 something went wrong. */
11014 else if (token->type == CPP_KEYWORD && token->keyword == RID_ELSE)
11016 if (parser->in_statement & IN_IF_STMT)
11017 break;
11018 else
11020 token = cp_lexer_consume_token (parser->lexer);
11021 error_at (token->location, "%<else%> without a previous %<if%>");
11025 /* Parse the statement. */
11026 cp_parser_statement (parser, in_statement_expr, true, NULL);
11030 /* Return true if we're looking at (init; cond), false otherwise. */
11032 static bool
11033 cp_parser_init_statement_p (cp_parser *parser)
11035 /* Save tokens so that we can put them back. */
11036 cp_lexer_save_tokens (parser->lexer);
11038 /* Look for ';' that is not nested in () or {}. */
11039 int ret = cp_parser_skip_to_closing_parenthesis_1 (parser,
11040 /*recovering=*/false,
11041 CPP_SEMICOLON,
11042 /*consume_paren=*/false);
11044 /* Roll back the tokens we skipped. */
11045 cp_lexer_rollback_tokens (parser->lexer);
11047 return ret == -1;
11050 /* Parse a selection-statement.
11052 selection-statement:
11053 if ( init-statement [opt] condition ) statement
11054 if ( init-statement [opt] condition ) statement else statement
11055 switch ( init-statement [opt] condition ) statement
11057 Returns the new IF_STMT or SWITCH_STMT.
11059 If IF_P is not NULL, *IF_P is set to indicate whether the statement
11060 is a (possibly labeled) if statement which is not enclosed in
11061 braces and has an else clause. This is used to implement
11062 -Wparentheses.
11064 CHAIN is a vector of if-else-if conditions. This is used to implement
11065 -Wduplicated-cond. */
11067 static tree
11068 cp_parser_selection_statement (cp_parser* parser, bool *if_p,
11069 vec<tree> *chain)
11071 cp_token *token;
11072 enum rid keyword;
11073 token_indent_info guard_tinfo;
11075 if (if_p != NULL)
11076 *if_p = false;
11078 /* Peek at the next token. */
11079 token = cp_parser_require (parser, CPP_KEYWORD, RT_SELECT);
11080 guard_tinfo = get_token_indent_info (token);
11082 /* See what kind of keyword it is. */
11083 keyword = token->keyword;
11084 switch (keyword)
11086 case RID_IF:
11087 case RID_SWITCH:
11089 tree statement;
11090 tree condition;
11092 bool cx = false;
11093 if (keyword == RID_IF
11094 && cp_lexer_next_token_is_keyword (parser->lexer,
11095 RID_CONSTEXPR))
11097 cx = true;
11098 cp_token *tok = cp_lexer_consume_token (parser->lexer);
11099 if (cxx_dialect < cxx1z && !in_system_header_at (tok->location))
11100 pedwarn (tok->location, 0, "%<if constexpr%> only available "
11101 "with -std=c++1z or -std=gnu++1z");
11104 /* Look for the `('. */
11105 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
11107 cp_parser_skip_to_end_of_statement (parser);
11108 return error_mark_node;
11111 /* Begin the selection-statement. */
11112 if (keyword == RID_IF)
11114 statement = begin_if_stmt ();
11115 IF_STMT_CONSTEXPR_P (statement) = cx;
11117 else
11118 statement = begin_switch_stmt ();
11120 /* Parse the optional init-statement. */
11121 if (cp_parser_init_statement_p (parser))
11123 tree decl;
11124 if (cxx_dialect < cxx1z)
11125 pedwarn (cp_lexer_peek_token (parser->lexer)->location, 0,
11126 "init-statement in selection statements only available "
11127 "with -std=c++1z or -std=gnu++1z");
11128 cp_parser_init_statement (parser, &decl);
11131 /* Parse the condition. */
11132 condition = cp_parser_condition (parser);
11133 /* Look for the `)'. */
11134 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
11135 cp_parser_skip_to_closing_parenthesis (parser, true, false,
11136 /*consume_paren=*/true);
11138 if (keyword == RID_IF)
11140 bool nested_if;
11141 unsigned char in_statement;
11143 /* Add the condition. */
11144 condition = finish_if_stmt_cond (condition, statement);
11146 if (warn_duplicated_cond)
11147 warn_duplicated_cond_add_or_warn (token->location, condition,
11148 &chain);
11150 /* Parse the then-clause. */
11151 in_statement = parser->in_statement;
11152 parser->in_statement |= IN_IF_STMT;
11154 /* Outside a template, the non-selected branch of a constexpr
11155 if is a 'discarded statement', i.e. unevaluated. */
11156 bool was_discarded = in_discarded_stmt;
11157 bool discard_then = (cx && !processing_template_decl
11158 && integer_zerop (condition));
11159 if (discard_then)
11161 in_discarded_stmt = true;
11162 ++c_inhibit_evaluation_warnings;
11165 cp_parser_implicitly_scoped_statement (parser, &nested_if,
11166 guard_tinfo);
11168 parser->in_statement = in_statement;
11170 finish_then_clause (statement);
11172 if (discard_then)
11174 THEN_CLAUSE (statement) = NULL_TREE;
11175 in_discarded_stmt = was_discarded;
11176 --c_inhibit_evaluation_warnings;
11179 /* If the next token is `else', parse the else-clause. */
11180 if (cp_lexer_next_token_is_keyword (parser->lexer,
11181 RID_ELSE))
11183 bool discard_else = (cx && !processing_template_decl
11184 && integer_nonzerop (condition));
11185 if (discard_else)
11187 in_discarded_stmt = true;
11188 ++c_inhibit_evaluation_warnings;
11191 guard_tinfo
11192 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
11193 /* Consume the `else' keyword. */
11194 cp_lexer_consume_token (parser->lexer);
11195 if (warn_duplicated_cond)
11197 if (cp_lexer_next_token_is_keyword (parser->lexer,
11198 RID_IF)
11199 && chain == NULL)
11201 /* We've got "if (COND) else if (COND2)". Start
11202 the condition chain and add COND as the first
11203 element. */
11204 chain = new vec<tree> ();
11205 if (!CONSTANT_CLASS_P (condition)
11206 && !TREE_SIDE_EFFECTS (condition))
11208 /* Wrap it in a NOP_EXPR so that we can set the
11209 location of the condition. */
11210 tree e = build1 (NOP_EXPR, TREE_TYPE (condition),
11211 condition);
11212 SET_EXPR_LOCATION (e, token->location);
11213 chain->safe_push (e);
11216 else if (!cp_lexer_next_token_is_keyword (parser->lexer,
11217 RID_IF))
11219 /* This is if-else without subsequent if. Zap the
11220 condition chain; we would have already warned at
11221 this point. */
11222 delete chain;
11223 chain = NULL;
11226 begin_else_clause (statement);
11227 /* Parse the else-clause. */
11228 cp_parser_implicitly_scoped_statement (parser, NULL,
11229 guard_tinfo, chain);
11231 finish_else_clause (statement);
11233 /* If we are currently parsing a then-clause, then
11234 IF_P will not be NULL. We set it to true to
11235 indicate that this if statement has an else clause.
11236 This may trigger the Wparentheses warning below
11237 when we get back up to the parent if statement. */
11238 if (if_p != NULL)
11239 *if_p = true;
11241 if (discard_else)
11243 ELSE_CLAUSE (statement) = NULL_TREE;
11244 in_discarded_stmt = was_discarded;
11245 --c_inhibit_evaluation_warnings;
11248 else
11250 /* This if statement does not have an else clause. If
11251 NESTED_IF is true, then the then-clause has an if
11252 statement which does have an else clause. We warn
11253 about the potential ambiguity. */
11254 if (nested_if)
11255 warning_at (EXPR_LOCATION (statement), OPT_Wdangling_else,
11256 "suggest explicit braces to avoid ambiguous"
11257 " %<else%>");
11258 if (warn_duplicated_cond)
11260 /* We don't need the condition chain anymore. */
11261 delete chain;
11262 chain = NULL;
11266 /* Now we're all done with the if-statement. */
11267 finish_if_stmt (statement);
11269 else
11271 bool in_switch_statement_p;
11272 unsigned char in_statement;
11274 /* Add the condition. */
11275 finish_switch_cond (condition, statement);
11277 /* Parse the body of the switch-statement. */
11278 in_switch_statement_p = parser->in_switch_statement_p;
11279 in_statement = parser->in_statement;
11280 parser->in_switch_statement_p = true;
11281 parser->in_statement |= IN_SWITCH_STMT;
11282 cp_parser_implicitly_scoped_statement (parser, if_p,
11283 guard_tinfo);
11284 parser->in_switch_statement_p = in_switch_statement_p;
11285 parser->in_statement = in_statement;
11287 /* Now we're all done with the switch-statement. */
11288 finish_switch_stmt (statement);
11291 return statement;
11293 break;
11295 default:
11296 cp_parser_error (parser, "expected selection-statement");
11297 return error_mark_node;
11301 /* Parse a condition.
11303 condition:
11304 expression
11305 type-specifier-seq declarator = initializer-clause
11306 type-specifier-seq declarator braced-init-list
11308 GNU Extension:
11310 condition:
11311 type-specifier-seq declarator asm-specification [opt]
11312 attributes [opt] = assignment-expression
11314 Returns the expression that should be tested. */
11316 static tree
11317 cp_parser_condition (cp_parser* parser)
11319 cp_decl_specifier_seq type_specifiers;
11320 const char *saved_message;
11321 int declares_class_or_enum;
11323 /* Try the declaration first. */
11324 cp_parser_parse_tentatively (parser);
11325 /* New types are not allowed in the type-specifier-seq for a
11326 condition. */
11327 saved_message = parser->type_definition_forbidden_message;
11328 parser->type_definition_forbidden_message
11329 = G_("types may not be defined in conditions");
11330 /* Parse the type-specifier-seq. */
11331 cp_parser_decl_specifier_seq (parser,
11332 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR,
11333 &type_specifiers,
11334 &declares_class_or_enum);
11335 /* Restore the saved message. */
11336 parser->type_definition_forbidden_message = saved_message;
11337 /* If all is well, we might be looking at a declaration. */
11338 if (!cp_parser_error_occurred (parser))
11340 tree decl;
11341 tree asm_specification;
11342 tree attributes;
11343 cp_declarator *declarator;
11344 tree initializer = NULL_TREE;
11346 /* Parse the declarator. */
11347 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
11348 /*ctor_dtor_or_conv_p=*/NULL,
11349 /*parenthesized_p=*/NULL,
11350 /*member_p=*/false,
11351 /*friend_p=*/false);
11352 /* Parse the attributes. */
11353 attributes = cp_parser_attributes_opt (parser);
11354 /* Parse the asm-specification. */
11355 asm_specification = cp_parser_asm_specification_opt (parser);
11356 /* If the next token is not an `=' or '{', then we might still be
11357 looking at an expression. For example:
11359 if (A(a).x)
11361 looks like a decl-specifier-seq and a declarator -- but then
11362 there is no `=', so this is an expression. */
11363 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
11364 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
11365 cp_parser_simulate_error (parser);
11367 /* If we did see an `=' or '{', then we are looking at a declaration
11368 for sure. */
11369 if (cp_parser_parse_definitely (parser))
11371 tree pushed_scope;
11372 bool non_constant_p;
11373 int flags = LOOKUP_ONLYCONVERTING;
11375 /* Create the declaration. */
11376 decl = start_decl (declarator, &type_specifiers,
11377 /*initialized_p=*/true,
11378 attributes, /*prefix_attributes=*/NULL_TREE,
11379 &pushed_scope);
11381 /* Parse the initializer. */
11382 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
11384 initializer = cp_parser_braced_list (parser, &non_constant_p);
11385 CONSTRUCTOR_IS_DIRECT_INIT (initializer) = 1;
11386 flags = 0;
11388 else
11390 /* Consume the `='. */
11391 cp_parser_require (parser, CPP_EQ, RT_EQ);
11392 initializer = cp_parser_initializer_clause (parser, &non_constant_p);
11394 if (BRACE_ENCLOSED_INITIALIZER_P (initializer))
11395 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
11397 /* Process the initializer. */
11398 cp_finish_decl (decl,
11399 initializer, !non_constant_p,
11400 asm_specification,
11401 flags);
11403 if (pushed_scope)
11404 pop_scope (pushed_scope);
11406 return convert_from_reference (decl);
11409 /* If we didn't even get past the declarator successfully, we are
11410 definitely not looking at a declaration. */
11411 else
11412 cp_parser_abort_tentative_parse (parser);
11414 /* Otherwise, we are looking at an expression. */
11415 return cp_parser_expression (parser);
11418 /* Parses a for-statement or range-for-statement until the closing ')',
11419 not included. */
11421 static tree
11422 cp_parser_for (cp_parser *parser, bool ivdep)
11424 tree init, scope, decl;
11425 bool is_range_for;
11427 /* Begin the for-statement. */
11428 scope = begin_for_scope (&init);
11430 /* Parse the initialization. */
11431 is_range_for = cp_parser_init_statement (parser, &decl);
11433 if (is_range_for)
11434 return cp_parser_range_for (parser, scope, init, decl, ivdep);
11435 else
11436 return cp_parser_c_for (parser, scope, init, ivdep);
11439 static tree
11440 cp_parser_c_for (cp_parser *parser, tree scope, tree init, bool ivdep)
11442 /* Normal for loop */
11443 tree condition = NULL_TREE;
11444 tree expression = NULL_TREE;
11445 tree stmt;
11447 stmt = begin_for_stmt (scope, init);
11448 /* The init-statement has already been parsed in
11449 cp_parser_init_statement, so no work is needed here. */
11450 finish_init_stmt (stmt);
11452 /* If there's a condition, process it. */
11453 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
11454 condition = cp_parser_condition (parser);
11455 else if (ivdep)
11457 cp_parser_error (parser, "missing loop condition in loop with "
11458 "%<GCC ivdep%> pragma");
11459 condition = error_mark_node;
11461 finish_for_cond (condition, stmt, ivdep);
11462 /* Look for the `;'. */
11463 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
11465 /* If there's an expression, process it. */
11466 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
11467 expression = cp_parser_expression (parser);
11468 finish_for_expr (expression, stmt);
11470 return stmt;
11473 /* Tries to parse a range-based for-statement:
11475 range-based-for:
11476 decl-specifier-seq declarator : expression
11478 The decl-specifier-seq declarator and the `:' are already parsed by
11479 cp_parser_init_statement. If processing_template_decl it returns a
11480 newly created RANGE_FOR_STMT; if not, it is converted to a
11481 regular FOR_STMT. */
11483 static tree
11484 cp_parser_range_for (cp_parser *parser, tree scope, tree init, tree range_decl,
11485 bool ivdep)
11487 tree stmt, range_expr;
11488 auto_vec <cxx_binding *, 16> bindings;
11489 auto_vec <tree, 16> names;
11490 tree decomp_first_name = NULL_TREE;
11491 unsigned int decomp_cnt = 0;
11493 /* Get the range declaration momentarily out of the way so that
11494 the range expression doesn't clash with it. */
11495 if (range_decl != error_mark_node)
11497 if (DECL_HAS_VALUE_EXPR_P (range_decl))
11499 tree v = DECL_VALUE_EXPR (range_decl);
11500 /* For decomposition declaration get all of the corresponding
11501 declarations out of the way. */
11502 if (TREE_CODE (v) == ARRAY_REF
11503 && VAR_P (TREE_OPERAND (v, 0))
11504 && DECL_DECOMPOSITION_P (TREE_OPERAND (v, 0)))
11506 tree d = range_decl;
11507 range_decl = TREE_OPERAND (v, 0);
11508 decomp_cnt = tree_to_uhwi (TREE_OPERAND (v, 1)) + 1;
11509 decomp_first_name = d;
11510 for (unsigned int i = 0; i < decomp_cnt; i++, d = DECL_CHAIN (d))
11512 tree name = DECL_NAME (d);
11513 names.safe_push (name);
11514 bindings.safe_push (IDENTIFIER_BINDING (name));
11515 IDENTIFIER_BINDING (name)
11516 = IDENTIFIER_BINDING (name)->previous;
11520 if (names.is_empty ())
11522 tree name = DECL_NAME (range_decl);
11523 names.safe_push (name);
11524 bindings.safe_push (IDENTIFIER_BINDING (name));
11525 IDENTIFIER_BINDING (name) = IDENTIFIER_BINDING (name)->previous;
11529 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
11531 bool expr_non_constant_p;
11532 range_expr = cp_parser_braced_list (parser, &expr_non_constant_p);
11534 else
11535 range_expr = cp_parser_expression (parser);
11537 /* Put the range declaration(s) back into scope. */
11538 for (unsigned int i = 0; i < names.length (); i++)
11540 cxx_binding *binding = bindings[i];
11541 binding->previous = IDENTIFIER_BINDING (names[i]);
11542 IDENTIFIER_BINDING (names[i]) = binding;
11545 /* If in template, STMT is converted to a normal for-statement
11546 at instantiation. If not, it is done just ahead. */
11547 if (processing_template_decl)
11549 if (check_for_bare_parameter_packs (range_expr))
11550 range_expr = error_mark_node;
11551 stmt = begin_range_for_stmt (scope, init);
11552 if (ivdep)
11553 RANGE_FOR_IVDEP (stmt) = 1;
11554 finish_range_for_decl (stmt, range_decl, range_expr);
11555 if (!type_dependent_expression_p (range_expr)
11556 /* do_auto_deduction doesn't mess with template init-lists. */
11557 && !BRACE_ENCLOSED_INITIALIZER_P (range_expr))
11558 do_range_for_auto_deduction (range_decl, range_expr);
11560 else
11562 stmt = begin_for_stmt (scope, init);
11563 stmt = cp_convert_range_for (stmt, range_decl, range_expr,
11564 decomp_first_name, decomp_cnt, ivdep);
11566 return stmt;
11569 /* Subroutine of cp_convert_range_for: given the initializer expression,
11570 builds up the range temporary. */
11572 static tree
11573 build_range_temp (tree range_expr)
11575 tree range_type, range_temp;
11577 /* Find out the type deduced by the declaration
11578 `auto &&__range = range_expr'. */
11579 range_type = cp_build_reference_type (make_auto (), true);
11580 range_type = do_auto_deduction (range_type, range_expr,
11581 type_uses_auto (range_type));
11583 /* Create the __range variable. */
11584 range_temp = build_decl (input_location, VAR_DECL,
11585 get_identifier ("__for_range"), range_type);
11586 TREE_USED (range_temp) = 1;
11587 DECL_ARTIFICIAL (range_temp) = 1;
11589 return range_temp;
11592 /* Used by cp_parser_range_for in template context: we aren't going to
11593 do a full conversion yet, but we still need to resolve auto in the
11594 type of the for-range-declaration if present. This is basically
11595 a shortcut version of cp_convert_range_for. */
11597 static void
11598 do_range_for_auto_deduction (tree decl, tree range_expr)
11600 tree auto_node = type_uses_auto (TREE_TYPE (decl));
11601 if (auto_node)
11603 tree begin_dummy, end_dummy, range_temp, iter_type, iter_decl;
11604 range_temp = convert_from_reference (build_range_temp (range_expr));
11605 iter_type = (cp_parser_perform_range_for_lookup
11606 (range_temp, &begin_dummy, &end_dummy));
11607 if (iter_type)
11609 iter_decl = build_decl (input_location, VAR_DECL, NULL_TREE,
11610 iter_type);
11611 iter_decl = build_x_indirect_ref (input_location, iter_decl, RO_NULL,
11612 tf_warning_or_error);
11613 TREE_TYPE (decl) = do_auto_deduction (TREE_TYPE (decl),
11614 iter_decl, auto_node);
11619 /* Converts a range-based for-statement into a normal
11620 for-statement, as per the definition.
11622 for (RANGE_DECL : RANGE_EXPR)
11623 BLOCK
11625 should be equivalent to:
11628 auto &&__range = RANGE_EXPR;
11629 for (auto __begin = BEGIN_EXPR, end = END_EXPR;
11630 __begin != __end;
11631 ++__begin)
11633 RANGE_DECL = *__begin;
11634 BLOCK
11638 If RANGE_EXPR is an array:
11639 BEGIN_EXPR = __range
11640 END_EXPR = __range + ARRAY_SIZE(__range)
11641 Else if RANGE_EXPR has a member 'begin' or 'end':
11642 BEGIN_EXPR = __range.begin()
11643 END_EXPR = __range.end()
11644 Else:
11645 BEGIN_EXPR = begin(__range)
11646 END_EXPR = end(__range);
11648 If __range has a member 'begin' but not 'end', or vice versa, we must
11649 still use the second alternative (it will surely fail, however).
11650 When calling begin()/end() in the third alternative we must use
11651 argument dependent lookup, but always considering 'std' as an associated
11652 namespace. */
11654 tree
11655 cp_convert_range_for (tree statement, tree range_decl, tree range_expr,
11656 tree decomp_first_name, unsigned int decomp_cnt,
11657 bool ivdep)
11659 tree begin, end;
11660 tree iter_type, begin_expr, end_expr;
11661 tree condition, expression;
11663 if (range_decl == error_mark_node || range_expr == error_mark_node)
11664 /* If an error happened previously do nothing or else a lot of
11665 unhelpful errors would be issued. */
11666 begin_expr = end_expr = iter_type = error_mark_node;
11667 else
11669 tree range_temp;
11671 if (VAR_P (range_expr)
11672 && array_of_runtime_bound_p (TREE_TYPE (range_expr)))
11673 /* Can't bind a reference to an array of runtime bound. */
11674 range_temp = range_expr;
11675 else
11677 range_temp = build_range_temp (range_expr);
11678 pushdecl (range_temp);
11679 cp_finish_decl (range_temp, range_expr,
11680 /*is_constant_init*/false, NULL_TREE,
11681 LOOKUP_ONLYCONVERTING);
11682 range_temp = convert_from_reference (range_temp);
11684 iter_type = cp_parser_perform_range_for_lookup (range_temp,
11685 &begin_expr, &end_expr);
11688 /* The new for initialization statement. */
11689 begin = build_decl (input_location, VAR_DECL,
11690 get_identifier ("__for_begin"), iter_type);
11691 TREE_USED (begin) = 1;
11692 DECL_ARTIFICIAL (begin) = 1;
11693 pushdecl (begin);
11694 cp_finish_decl (begin, begin_expr,
11695 /*is_constant_init*/false, NULL_TREE,
11696 LOOKUP_ONLYCONVERTING);
11698 if (cxx_dialect >= cxx1z)
11699 iter_type = cv_unqualified (TREE_TYPE (end_expr));
11700 end = build_decl (input_location, VAR_DECL,
11701 get_identifier ("__for_end"), iter_type);
11702 TREE_USED (end) = 1;
11703 DECL_ARTIFICIAL (end) = 1;
11704 pushdecl (end);
11705 cp_finish_decl (end, end_expr,
11706 /*is_constant_init*/false, NULL_TREE,
11707 LOOKUP_ONLYCONVERTING);
11709 finish_init_stmt (statement);
11711 /* The new for condition. */
11712 condition = build_x_binary_op (input_location, NE_EXPR,
11713 begin, ERROR_MARK,
11714 end, ERROR_MARK,
11715 NULL, tf_warning_or_error);
11716 finish_for_cond (condition, statement, ivdep);
11718 /* The new increment expression. */
11719 expression = finish_unary_op_expr (input_location,
11720 PREINCREMENT_EXPR, begin,
11721 tf_warning_or_error);
11722 finish_for_expr (expression, statement);
11724 /* The declaration is initialized with *__begin inside the loop body. */
11725 cp_finish_decl (range_decl,
11726 build_x_indirect_ref (input_location, begin, RO_NULL,
11727 tf_warning_or_error),
11728 /*is_constant_init*/false, NULL_TREE,
11729 LOOKUP_ONLYCONVERTING);
11730 if (VAR_P (range_decl) && DECL_DECOMPOSITION_P (range_decl))
11731 cp_finish_decomp (range_decl, decomp_first_name, decomp_cnt);
11733 return statement;
11736 /* Solves BEGIN_EXPR and END_EXPR as described in cp_convert_range_for.
11737 We need to solve both at the same time because the method used
11738 depends on the existence of members begin or end.
11739 Returns the type deduced for the iterator expression. */
11741 static tree
11742 cp_parser_perform_range_for_lookup (tree range, tree *begin, tree *end)
11744 if (error_operand_p (range))
11746 *begin = *end = error_mark_node;
11747 return error_mark_node;
11750 if (!COMPLETE_TYPE_P (complete_type (TREE_TYPE (range))))
11752 error ("range-based %<for%> expression of type %qT "
11753 "has incomplete type", TREE_TYPE (range));
11754 *begin = *end = error_mark_node;
11755 return error_mark_node;
11757 if (TREE_CODE (TREE_TYPE (range)) == ARRAY_TYPE)
11759 /* If RANGE is an array, we will use pointer arithmetic. */
11760 *begin = decay_conversion (range, tf_warning_or_error);
11761 *end = build_binary_op (input_location, PLUS_EXPR,
11762 range,
11763 array_type_nelts_top (TREE_TYPE (range)),
11765 return TREE_TYPE (*begin);
11767 else
11769 /* If it is not an array, we must do a bit of magic. */
11770 tree id_begin, id_end;
11771 tree member_begin, member_end;
11773 *begin = *end = error_mark_node;
11775 id_begin = get_identifier ("begin");
11776 id_end = get_identifier ("end");
11777 member_begin = lookup_member (TREE_TYPE (range), id_begin,
11778 /*protect=*/2, /*want_type=*/false,
11779 tf_warning_or_error);
11780 member_end = lookup_member (TREE_TYPE (range), id_end,
11781 /*protect=*/2, /*want_type=*/false,
11782 tf_warning_or_error);
11784 if (member_begin != NULL_TREE || member_end != NULL_TREE)
11786 /* Use the member functions. */
11787 if (member_begin != NULL_TREE)
11788 *begin = cp_parser_range_for_member_function (range, id_begin);
11789 else
11790 error ("range-based %<for%> expression of type %qT has an "
11791 "%<end%> member but not a %<begin%>", TREE_TYPE (range));
11793 if (member_end != NULL_TREE)
11794 *end = cp_parser_range_for_member_function (range, id_end);
11795 else
11796 error ("range-based %<for%> expression of type %qT has a "
11797 "%<begin%> member but not an %<end%>", TREE_TYPE (range));
11799 else
11801 /* Use global functions with ADL. */
11802 vec<tree, va_gc> *vec;
11803 vec = make_tree_vector ();
11805 vec_safe_push (vec, range);
11807 member_begin = perform_koenig_lookup (id_begin, vec,
11808 tf_warning_or_error);
11809 *begin = finish_call_expr (member_begin, &vec, false, true,
11810 tf_warning_or_error);
11811 member_end = perform_koenig_lookup (id_end, vec,
11812 tf_warning_or_error);
11813 *end = finish_call_expr (member_end, &vec, false, true,
11814 tf_warning_or_error);
11816 release_tree_vector (vec);
11819 /* Last common checks. */
11820 if (*begin == error_mark_node || *end == error_mark_node)
11822 /* If one of the expressions is an error do no more checks. */
11823 *begin = *end = error_mark_node;
11824 return error_mark_node;
11826 else if (type_dependent_expression_p (*begin)
11827 || type_dependent_expression_p (*end))
11828 /* Can happen, when, eg, in a template context, Koenig lookup
11829 can't resolve begin/end (c++/58503). */
11830 return NULL_TREE;
11831 else
11833 tree iter_type = cv_unqualified (TREE_TYPE (*begin));
11834 /* The unqualified type of the __begin and __end temporaries should
11835 be the same, as required by the multiple auto declaration. */
11836 if (!same_type_p (iter_type, cv_unqualified (TREE_TYPE (*end))))
11838 if (cxx_dialect >= cxx1z
11839 && (build_x_binary_op (input_location, NE_EXPR,
11840 *begin, ERROR_MARK,
11841 *end, ERROR_MARK,
11842 NULL, tf_none)
11843 != error_mark_node))
11844 /* P0184R0 allows __begin and __end to have different types,
11845 but make sure they are comparable so we can give a better
11846 diagnostic. */;
11847 else
11848 error ("inconsistent begin/end types in range-based %<for%> "
11849 "statement: %qT and %qT",
11850 TREE_TYPE (*begin), TREE_TYPE (*end));
11852 return iter_type;
11857 /* Helper function for cp_parser_perform_range_for_lookup.
11858 Builds a tree for RANGE.IDENTIFIER(). */
11860 static tree
11861 cp_parser_range_for_member_function (tree range, tree identifier)
11863 tree member, res;
11864 vec<tree, va_gc> *vec;
11866 member = finish_class_member_access_expr (range, identifier,
11867 false, tf_warning_or_error);
11868 if (member == error_mark_node)
11869 return error_mark_node;
11871 vec = make_tree_vector ();
11872 res = finish_call_expr (member, &vec,
11873 /*disallow_virtual=*/false,
11874 /*koenig_p=*/false,
11875 tf_warning_or_error);
11876 release_tree_vector (vec);
11877 return res;
11880 /* Parse an iteration-statement.
11882 iteration-statement:
11883 while ( condition ) statement
11884 do statement while ( expression ) ;
11885 for ( init-statement condition [opt] ; expression [opt] )
11886 statement
11888 Returns the new WHILE_STMT, DO_STMT, FOR_STMT or RANGE_FOR_STMT. */
11890 static tree
11891 cp_parser_iteration_statement (cp_parser* parser, bool *if_p, bool ivdep)
11893 cp_token *token;
11894 enum rid keyword;
11895 tree statement;
11896 unsigned char in_statement;
11897 token_indent_info guard_tinfo;
11899 /* Peek at the next token. */
11900 token = cp_parser_require (parser, CPP_KEYWORD, RT_INTERATION);
11901 if (!token)
11902 return error_mark_node;
11904 guard_tinfo = get_token_indent_info (token);
11906 /* Remember whether or not we are already within an iteration
11907 statement. */
11908 in_statement = parser->in_statement;
11910 /* See what kind of keyword it is. */
11911 keyword = token->keyword;
11912 switch (keyword)
11914 case RID_WHILE:
11916 tree condition;
11918 /* Begin the while-statement. */
11919 statement = begin_while_stmt ();
11920 /* Look for the `('. */
11921 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
11922 /* Parse the condition. */
11923 condition = cp_parser_condition (parser);
11924 finish_while_stmt_cond (condition, statement, ivdep);
11925 /* Look for the `)'. */
11926 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
11927 /* Parse the dependent statement. */
11928 parser->in_statement = IN_ITERATION_STMT;
11929 cp_parser_already_scoped_statement (parser, if_p, guard_tinfo);
11930 parser->in_statement = in_statement;
11931 /* We're done with the while-statement. */
11932 finish_while_stmt (statement);
11934 break;
11936 case RID_DO:
11938 tree expression;
11940 /* Begin the do-statement. */
11941 statement = begin_do_stmt ();
11942 /* Parse the body of the do-statement. */
11943 parser->in_statement = IN_ITERATION_STMT;
11944 cp_parser_implicitly_scoped_statement (parser, NULL, guard_tinfo);
11945 parser->in_statement = in_statement;
11946 finish_do_body (statement);
11947 /* Look for the `while' keyword. */
11948 cp_parser_require_keyword (parser, RID_WHILE, RT_WHILE);
11949 /* Look for the `('. */
11950 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
11951 /* Parse the expression. */
11952 expression = cp_parser_expression (parser);
11953 /* We're done with the do-statement. */
11954 finish_do_stmt (expression, statement, ivdep);
11955 /* Look for the `)'. */
11956 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
11957 /* Look for the `;'. */
11958 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
11960 break;
11962 case RID_FOR:
11964 /* Look for the `('. */
11965 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
11967 statement = cp_parser_for (parser, ivdep);
11969 /* Look for the `)'. */
11970 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
11972 /* Parse the body of the for-statement. */
11973 parser->in_statement = IN_ITERATION_STMT;
11974 cp_parser_already_scoped_statement (parser, if_p, guard_tinfo);
11975 parser->in_statement = in_statement;
11977 /* We're done with the for-statement. */
11978 finish_for_stmt (statement);
11980 break;
11982 default:
11983 cp_parser_error (parser, "expected iteration-statement");
11984 statement = error_mark_node;
11985 break;
11988 return statement;
11991 /* Parse a init-statement or the declarator of a range-based-for.
11992 Returns true if a range-based-for declaration is seen.
11994 init-statement:
11995 expression-statement
11996 simple-declaration */
11998 static bool
11999 cp_parser_init_statement (cp_parser* parser, tree *decl)
12001 /* If the next token is a `;', then we have an empty
12002 expression-statement. Grammatically, this is also a
12003 simple-declaration, but an invalid one, because it does not
12004 declare anything. Therefore, if we did not handle this case
12005 specially, we would issue an error message about an invalid
12006 declaration. */
12007 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
12009 bool is_range_for = false;
12010 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
12012 /* A colon is used in range-based for. */
12013 parser->colon_corrects_to_scope_p = false;
12015 /* We're going to speculatively look for a declaration, falling back
12016 to an expression, if necessary. */
12017 cp_parser_parse_tentatively (parser);
12018 /* Parse the declaration. */
12019 cp_parser_simple_declaration (parser,
12020 /*function_definition_allowed_p=*/false,
12021 decl);
12022 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
12023 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
12025 /* It is a range-for, consume the ':' */
12026 cp_lexer_consume_token (parser->lexer);
12027 is_range_for = true;
12028 if (cxx_dialect < cxx11)
12030 pedwarn (cp_lexer_peek_token (parser->lexer)->location, 0,
12031 "range-based %<for%> loops only available with "
12032 "-std=c++11 or -std=gnu++11");
12033 *decl = error_mark_node;
12036 else
12037 /* The ';' is not consumed yet because we told
12038 cp_parser_simple_declaration not to. */
12039 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12041 if (cp_parser_parse_definitely (parser))
12042 return is_range_for;
12043 /* If the tentative parse failed, then we shall need to look for an
12044 expression-statement. */
12046 /* If we are here, it is an expression-statement. */
12047 cp_parser_expression_statement (parser, NULL_TREE);
12048 return false;
12051 /* Parse a jump-statement.
12053 jump-statement:
12054 break ;
12055 continue ;
12056 return expression [opt] ;
12057 return braced-init-list ;
12058 goto identifier ;
12060 GNU extension:
12062 jump-statement:
12063 goto * expression ;
12065 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR. */
12067 static tree
12068 cp_parser_jump_statement (cp_parser* parser)
12070 tree statement = error_mark_node;
12071 cp_token *token;
12072 enum rid keyword;
12073 unsigned char in_statement;
12075 /* Peek at the next token. */
12076 token = cp_parser_require (parser, CPP_KEYWORD, RT_JUMP);
12077 if (!token)
12078 return error_mark_node;
12080 /* See what kind of keyword it is. */
12081 keyword = token->keyword;
12082 switch (keyword)
12084 case RID_BREAK:
12085 in_statement = parser->in_statement & ~IN_IF_STMT;
12086 switch (in_statement)
12088 case 0:
12089 error_at (token->location, "break statement not within loop or switch");
12090 break;
12091 default:
12092 gcc_assert ((in_statement & IN_SWITCH_STMT)
12093 || in_statement == IN_ITERATION_STMT);
12094 statement = finish_break_stmt ();
12095 if (in_statement == IN_ITERATION_STMT)
12096 break_maybe_infinite_loop ();
12097 break;
12098 case IN_OMP_BLOCK:
12099 error_at (token->location, "invalid exit from OpenMP structured block");
12100 break;
12101 case IN_OMP_FOR:
12102 error_at (token->location, "break statement used with OpenMP for loop");
12103 break;
12104 case IN_CILK_SIMD_FOR:
12105 error_at (token->location, "break statement used with Cilk Plus for loop");
12106 break;
12108 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12109 break;
12111 case RID_CONTINUE:
12112 switch (parser->in_statement & ~(IN_SWITCH_STMT | IN_IF_STMT))
12114 case 0:
12115 error_at (token->location, "continue statement not within a loop");
12116 break;
12117 case IN_CILK_SIMD_FOR:
12118 error_at (token->location,
12119 "continue statement within %<#pragma simd%> loop body");
12120 /* Fall through. */
12121 case IN_ITERATION_STMT:
12122 case IN_OMP_FOR:
12123 statement = finish_continue_stmt ();
12124 break;
12125 case IN_OMP_BLOCK:
12126 error_at (token->location, "invalid exit from OpenMP structured block");
12127 break;
12128 default:
12129 gcc_unreachable ();
12131 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12132 break;
12134 case RID_RETURN:
12136 tree expr;
12137 bool expr_non_constant_p;
12139 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12141 cp_lexer_set_source_position (parser->lexer);
12142 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
12143 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
12145 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
12146 expr = cp_parser_expression (parser);
12147 else
12148 /* If the next token is a `;', then there is no
12149 expression. */
12150 expr = NULL_TREE;
12151 /* Build the return-statement. */
12152 if (current_function_auto_return_pattern && in_discarded_stmt)
12153 /* Don't deduce from a discarded return statement. */;
12154 else
12155 statement = finish_return_stmt (expr);
12156 /* Look for the final `;'. */
12157 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12159 break;
12161 case RID_GOTO:
12162 if (parser->in_function_body
12163 && DECL_DECLARED_CONSTEXPR_P (current_function_decl))
12165 error ("%<goto%> in %<constexpr%> function");
12166 cp_function_chain->invalid_constexpr = true;
12169 /* Create the goto-statement. */
12170 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
12172 /* Issue a warning about this use of a GNU extension. */
12173 pedwarn (token->location, OPT_Wpedantic, "ISO C++ forbids computed gotos");
12174 /* Consume the '*' token. */
12175 cp_lexer_consume_token (parser->lexer);
12176 /* Parse the dependent expression. */
12177 finish_goto_stmt (cp_parser_expression (parser));
12179 else
12180 finish_goto_stmt (cp_parser_identifier (parser));
12181 /* Look for the final `;'. */
12182 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12183 break;
12185 default:
12186 cp_parser_error (parser, "expected jump-statement");
12187 break;
12190 return statement;
12193 /* Parse a declaration-statement.
12195 declaration-statement:
12196 block-declaration */
12198 static void
12199 cp_parser_declaration_statement (cp_parser* parser)
12201 void *p;
12203 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
12204 p = obstack_alloc (&declarator_obstack, 0);
12206 /* Parse the block-declaration. */
12207 cp_parser_block_declaration (parser, /*statement_p=*/true);
12209 /* Free any declarators allocated. */
12210 obstack_free (&declarator_obstack, p);
12213 /* Some dependent statements (like `if (cond) statement'), are
12214 implicitly in their own scope. In other words, if the statement is
12215 a single statement (as opposed to a compound-statement), it is
12216 none-the-less treated as if it were enclosed in braces. Any
12217 declarations appearing in the dependent statement are out of scope
12218 after control passes that point. This function parses a statement,
12219 but ensures that is in its own scope, even if it is not a
12220 compound-statement.
12222 If IF_P is not NULL, *IF_P is set to indicate whether the statement
12223 is a (possibly labeled) if statement which is not enclosed in
12224 braces and has an else clause. This is used to implement
12225 -Wparentheses.
12227 CHAIN is a vector of if-else-if conditions. This is used to implement
12228 -Wduplicated-cond.
12230 Returns the new statement. */
12232 static tree
12233 cp_parser_implicitly_scoped_statement (cp_parser* parser, bool *if_p,
12234 const token_indent_info &guard_tinfo,
12235 vec<tree> *chain)
12237 tree statement;
12238 location_t body_loc = cp_lexer_peek_token (parser->lexer)->location;
12239 token_indent_info body_tinfo
12240 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
12242 if (if_p != NULL)
12243 *if_p = false;
12245 /* Mark if () ; with a special NOP_EXPR. */
12246 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
12248 cp_lexer_consume_token (parser->lexer);
12249 statement = add_stmt (build_empty_stmt (body_loc));
12251 if (guard_tinfo.keyword == RID_IF
12252 && !cp_lexer_next_token_is_keyword (parser->lexer, RID_ELSE))
12253 warning_at (body_loc, OPT_Wempty_body,
12254 "suggest braces around empty body in an %<if%> statement");
12255 else if (guard_tinfo.keyword == RID_ELSE)
12256 warning_at (body_loc, OPT_Wempty_body,
12257 "suggest braces around empty body in an %<else%> statement");
12259 /* if a compound is opened, we simply parse the statement directly. */
12260 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12261 statement = cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
12262 /* If the token is not a `{', then we must take special action. */
12263 else
12265 /* Create a compound-statement. */
12266 statement = begin_compound_stmt (0);
12267 /* Parse the dependent-statement. */
12268 cp_parser_statement (parser, NULL_TREE, false, if_p, chain);
12269 /* Finish the dummy compound-statement. */
12270 finish_compound_stmt (statement);
12273 token_indent_info next_tinfo
12274 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
12275 warn_for_misleading_indentation (guard_tinfo, body_tinfo, next_tinfo);
12277 /* Return the statement. */
12278 return statement;
12281 /* For some dependent statements (like `while (cond) statement'), we
12282 have already created a scope. Therefore, even if the dependent
12283 statement is a compound-statement, we do not want to create another
12284 scope. */
12286 static void
12287 cp_parser_already_scoped_statement (cp_parser* parser, bool *if_p,
12288 const token_indent_info &guard_tinfo)
12290 /* If the token is a `{', then we must take special action. */
12291 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
12293 token_indent_info body_tinfo
12294 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
12296 cp_parser_statement (parser, NULL_TREE, false, if_p);
12297 token_indent_info next_tinfo
12298 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
12299 warn_for_misleading_indentation (guard_tinfo, body_tinfo, next_tinfo);
12301 else
12303 /* Avoid calling cp_parser_compound_statement, so that we
12304 don't create a new scope. Do everything else by hand. */
12305 cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
12306 /* If the next keyword is `__label__' we have a label declaration. */
12307 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
12308 cp_parser_label_declaration (parser);
12309 /* Parse an (optional) statement-seq. */
12310 cp_parser_statement_seq_opt (parser, NULL_TREE);
12311 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
12315 /* Declarations [gram.dcl.dcl] */
12317 /* Parse an optional declaration-sequence.
12319 declaration-seq:
12320 declaration
12321 declaration-seq declaration */
12323 static void
12324 cp_parser_declaration_seq_opt (cp_parser* parser)
12326 while (true)
12328 cp_token *token;
12330 token = cp_lexer_peek_token (parser->lexer);
12332 if (token->type == CPP_CLOSE_BRACE
12333 || token->type == CPP_EOF
12334 || token->type == CPP_PRAGMA_EOL)
12335 break;
12337 if (token->type == CPP_SEMICOLON)
12339 /* A declaration consisting of a single semicolon is
12340 invalid. Allow it unless we're being pedantic. */
12341 cp_lexer_consume_token (parser->lexer);
12342 if (!in_system_header_at (input_location))
12343 pedwarn (input_location, OPT_Wpedantic, "extra %<;%>");
12344 continue;
12347 /* If we're entering or exiting a region that's implicitly
12348 extern "C", modify the lang context appropriately. */
12349 if (!parser->implicit_extern_c && token->implicit_extern_c)
12351 push_lang_context (lang_name_c);
12352 parser->implicit_extern_c = true;
12354 else if (parser->implicit_extern_c && !token->implicit_extern_c)
12356 pop_lang_context ();
12357 parser->implicit_extern_c = false;
12360 if (token->type == CPP_PRAGMA)
12362 /* A top-level declaration can consist solely of a #pragma.
12363 A nested declaration cannot, so this is done here and not
12364 in cp_parser_declaration. (A #pragma at block scope is
12365 handled in cp_parser_statement.) */
12366 cp_parser_pragma (parser, pragma_external, NULL);
12367 continue;
12370 /* Parse the declaration itself. */
12371 cp_parser_declaration (parser);
12375 /* Parse a declaration.
12377 declaration:
12378 block-declaration
12379 function-definition
12380 template-declaration
12381 explicit-instantiation
12382 explicit-specialization
12383 linkage-specification
12384 namespace-definition
12386 C++17:
12387 deduction-guide
12389 GNU extension:
12391 declaration:
12392 __extension__ declaration */
12394 static void
12395 cp_parser_declaration (cp_parser* parser)
12397 cp_token token1;
12398 cp_token token2;
12399 int saved_pedantic;
12400 void *p;
12401 tree attributes = NULL_TREE;
12403 /* Check for the `__extension__' keyword. */
12404 if (cp_parser_extension_opt (parser, &saved_pedantic))
12406 /* Parse the qualified declaration. */
12407 cp_parser_declaration (parser);
12408 /* Restore the PEDANTIC flag. */
12409 pedantic = saved_pedantic;
12411 return;
12414 /* Try to figure out what kind of declaration is present. */
12415 token1 = *cp_lexer_peek_token (parser->lexer);
12417 if (token1.type != CPP_EOF)
12418 token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
12419 else
12421 token2.type = CPP_EOF;
12422 token2.keyword = RID_MAX;
12425 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
12426 p = obstack_alloc (&declarator_obstack, 0);
12428 /* If the next token is `extern' and the following token is a string
12429 literal, then we have a linkage specification. */
12430 if (token1.keyword == RID_EXTERN
12431 && cp_parser_is_pure_string_literal (&token2))
12432 cp_parser_linkage_specification (parser);
12433 /* If the next token is `template', then we have either a template
12434 declaration, an explicit instantiation, or an explicit
12435 specialization. */
12436 else if (token1.keyword == RID_TEMPLATE)
12438 /* `template <>' indicates a template specialization. */
12439 if (token2.type == CPP_LESS
12440 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
12441 cp_parser_explicit_specialization (parser);
12442 /* `template <' indicates a template declaration. */
12443 else if (token2.type == CPP_LESS)
12444 cp_parser_template_declaration (parser, /*member_p=*/false);
12445 /* Anything else must be an explicit instantiation. */
12446 else
12447 cp_parser_explicit_instantiation (parser);
12449 /* If the next token is `export', then we have a template
12450 declaration. */
12451 else if (token1.keyword == RID_EXPORT)
12452 cp_parser_template_declaration (parser, /*member_p=*/false);
12453 /* If the next token is `extern', 'static' or 'inline' and the one
12454 after that is `template', we have a GNU extended explicit
12455 instantiation directive. */
12456 else if (cp_parser_allow_gnu_extensions_p (parser)
12457 && (token1.keyword == RID_EXTERN
12458 || token1.keyword == RID_STATIC
12459 || token1.keyword == RID_INLINE)
12460 && token2.keyword == RID_TEMPLATE)
12461 cp_parser_explicit_instantiation (parser);
12462 /* If the next token is `namespace', check for a named or unnamed
12463 namespace definition. */
12464 else if (token1.keyword == RID_NAMESPACE
12465 && (/* A named namespace definition. */
12466 (token2.type == CPP_NAME
12467 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
12468 != CPP_EQ))
12469 || (token2.type == CPP_OPEN_SQUARE
12470 && cp_lexer_peek_nth_token (parser->lexer, 3)->type
12471 == CPP_OPEN_SQUARE)
12472 /* An unnamed namespace definition. */
12473 || token2.type == CPP_OPEN_BRACE
12474 || token2.keyword == RID_ATTRIBUTE))
12475 cp_parser_namespace_definition (parser);
12476 /* An inline (associated) namespace definition. */
12477 else if (token1.keyword == RID_INLINE
12478 && token2.keyword == RID_NAMESPACE)
12479 cp_parser_namespace_definition (parser);
12480 /* Objective-C++ declaration/definition. */
12481 else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1.keyword))
12482 cp_parser_objc_declaration (parser, NULL_TREE);
12483 else if (c_dialect_objc ()
12484 && token1.keyword == RID_ATTRIBUTE
12485 && cp_parser_objc_valid_prefix_attributes (parser, &attributes))
12486 cp_parser_objc_declaration (parser, attributes);
12487 /* At this point we may have a template declared by a concept
12488 introduction. */
12489 else if (flag_concepts
12490 && cp_parser_template_declaration_after_export (parser,
12491 /*member_p=*/false))
12492 /* We did. */;
12493 else
12494 /* Try to parse a block-declaration, or a function-definition. */
12495 cp_parser_block_declaration (parser, /*statement_p=*/false);
12497 /* Free any declarators allocated. */
12498 obstack_free (&declarator_obstack, p);
12501 /* Parse a block-declaration.
12503 block-declaration:
12504 simple-declaration
12505 asm-definition
12506 namespace-alias-definition
12507 using-declaration
12508 using-directive
12510 GNU Extension:
12512 block-declaration:
12513 __extension__ block-declaration
12515 C++0x Extension:
12517 block-declaration:
12518 static_assert-declaration
12520 If STATEMENT_P is TRUE, then this block-declaration is occurring as
12521 part of a declaration-statement. */
12523 static void
12524 cp_parser_block_declaration (cp_parser *parser,
12525 bool statement_p)
12527 cp_token *token1;
12528 int saved_pedantic;
12530 /* Check for the `__extension__' keyword. */
12531 if (cp_parser_extension_opt (parser, &saved_pedantic))
12533 /* Parse the qualified declaration. */
12534 cp_parser_block_declaration (parser, statement_p);
12535 /* Restore the PEDANTIC flag. */
12536 pedantic = saved_pedantic;
12538 return;
12541 /* Peek at the next token to figure out which kind of declaration is
12542 present. */
12543 token1 = cp_lexer_peek_token (parser->lexer);
12545 /* If the next keyword is `asm', we have an asm-definition. */
12546 if (token1->keyword == RID_ASM)
12548 if (statement_p)
12549 cp_parser_commit_to_tentative_parse (parser);
12550 cp_parser_asm_definition (parser);
12552 /* If the next keyword is `namespace', we have a
12553 namespace-alias-definition. */
12554 else if (token1->keyword == RID_NAMESPACE)
12555 cp_parser_namespace_alias_definition (parser);
12556 /* If the next keyword is `using', we have a
12557 using-declaration, a using-directive, or an alias-declaration. */
12558 else if (token1->keyword == RID_USING)
12560 cp_token *token2;
12562 if (statement_p)
12563 cp_parser_commit_to_tentative_parse (parser);
12564 /* If the token after `using' is `namespace', then we have a
12565 using-directive. */
12566 token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
12567 if (token2->keyword == RID_NAMESPACE)
12568 cp_parser_using_directive (parser);
12569 /* If the second token after 'using' is '=', then we have an
12570 alias-declaration. */
12571 else if (cxx_dialect >= cxx11
12572 && token2->type == CPP_NAME
12573 && ((cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_EQ)
12574 || (cp_nth_tokens_can_be_attribute_p (parser, 3))))
12575 cp_parser_alias_declaration (parser);
12576 /* Otherwise, it's a using-declaration. */
12577 else
12578 cp_parser_using_declaration (parser,
12579 /*access_declaration_p=*/false);
12581 /* If the next keyword is `__label__' we have a misplaced label
12582 declaration. */
12583 else if (token1->keyword == RID_LABEL)
12585 cp_lexer_consume_token (parser->lexer);
12586 error_at (token1->location, "%<__label__%> not at the beginning of a block");
12587 cp_parser_skip_to_end_of_statement (parser);
12588 /* If the next token is now a `;', consume it. */
12589 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
12590 cp_lexer_consume_token (parser->lexer);
12592 /* If the next token is `static_assert' we have a static assertion. */
12593 else if (token1->keyword == RID_STATIC_ASSERT)
12594 cp_parser_static_assert (parser, /*member_p=*/false);
12595 /* Anything else must be a simple-declaration. */
12596 else
12597 cp_parser_simple_declaration (parser, !statement_p,
12598 /*maybe_range_for_decl*/NULL);
12601 /* Parse a simple-declaration.
12603 simple-declaration:
12604 decl-specifier-seq [opt] init-declarator-list [opt] ;
12605 decl-specifier-seq ref-qualifier [opt] [ identifier-list ]
12606 brace-or-equal-initializer ;
12608 init-declarator-list:
12609 init-declarator
12610 init-declarator-list , init-declarator
12612 If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
12613 function-definition as a simple-declaration.
12615 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
12616 parsed declaration if it is an uninitialized single declarator not followed
12617 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
12618 if present, will not be consumed. */
12620 static void
12621 cp_parser_simple_declaration (cp_parser* parser,
12622 bool function_definition_allowed_p,
12623 tree *maybe_range_for_decl)
12625 cp_decl_specifier_seq decl_specifiers;
12626 int declares_class_or_enum;
12627 bool saw_declarator;
12628 location_t comma_loc = UNKNOWN_LOCATION;
12629 location_t init_loc = UNKNOWN_LOCATION;
12631 if (maybe_range_for_decl)
12632 *maybe_range_for_decl = NULL_TREE;
12634 /* Defer access checks until we know what is being declared; the
12635 checks for names appearing in the decl-specifier-seq should be
12636 done as if we were in the scope of the thing being declared. */
12637 push_deferring_access_checks (dk_deferred);
12639 /* Parse the decl-specifier-seq. We have to keep track of whether
12640 or not the decl-specifier-seq declares a named class or
12641 enumeration type, since that is the only case in which the
12642 init-declarator-list is allowed to be empty.
12644 [dcl.dcl]
12646 In a simple-declaration, the optional init-declarator-list can be
12647 omitted only when declaring a class or enumeration, that is when
12648 the decl-specifier-seq contains either a class-specifier, an
12649 elaborated-type-specifier, or an enum-specifier. */
12650 cp_parser_decl_specifier_seq (parser,
12651 CP_PARSER_FLAGS_OPTIONAL,
12652 &decl_specifiers,
12653 &declares_class_or_enum);
12654 /* We no longer need to defer access checks. */
12655 stop_deferring_access_checks ();
12657 /* In a block scope, a valid declaration must always have a
12658 decl-specifier-seq. By not trying to parse declarators, we can
12659 resolve the declaration/expression ambiguity more quickly. */
12660 if (!function_definition_allowed_p
12661 && !decl_specifiers.any_specifiers_p)
12663 cp_parser_error (parser, "expected declaration");
12664 goto done;
12667 /* If the next two tokens are both identifiers, the code is
12668 erroneous. The usual cause of this situation is code like:
12670 T t;
12672 where "T" should name a type -- but does not. */
12673 if (!decl_specifiers.any_type_specifiers_p
12674 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
12676 /* If parsing tentatively, we should commit; we really are
12677 looking at a declaration. */
12678 cp_parser_commit_to_tentative_parse (parser);
12679 /* Give up. */
12680 goto done;
12683 /* If we have seen at least one decl-specifier, and the next token
12684 is not a parenthesis, then we must be looking at a declaration.
12685 (After "int (" we might be looking at a functional cast.) */
12686 if (decl_specifiers.any_specifiers_p
12687 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN)
12688 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
12689 && !cp_parser_error_occurred (parser))
12690 cp_parser_commit_to_tentative_parse (parser);
12692 /* Look for C++17 decomposition declaration. */
12693 for (size_t n = 1; ; n++)
12694 if (cp_lexer_nth_token_is (parser->lexer, n, CPP_AND)
12695 || cp_lexer_nth_token_is (parser->lexer, n, CPP_AND_AND))
12696 continue;
12697 else if (cp_lexer_nth_token_is (parser->lexer, n, CPP_OPEN_SQUARE)
12698 && !cp_lexer_nth_token_is (parser->lexer, n + 1, CPP_OPEN_SQUARE)
12699 && decl_specifiers.any_specifiers_p)
12701 tree decl
12702 = cp_parser_decomposition_declaration (parser, &decl_specifiers,
12703 maybe_range_for_decl,
12704 &init_loc);
12706 /* The next token should be either a `,' or a `;'. */
12707 cp_token *token = cp_lexer_peek_token (parser->lexer);
12708 /* If it's a `;', we are done. */
12709 if (token->type == CPP_SEMICOLON || maybe_range_for_decl)
12710 goto finish;
12711 /* Anything else is an error. */
12712 else
12714 /* If we have already issued an error message we don't need
12715 to issue another one. */
12716 if ((decl != error_mark_node
12717 && DECL_INITIAL (decl) != error_mark_node)
12718 || cp_parser_uncommitted_to_tentative_parse_p (parser))
12719 cp_parser_error (parser, "expected %<,%> or %<;%>");
12720 /* Skip tokens until we reach the end of the statement. */
12721 cp_parser_skip_to_end_of_statement (parser);
12722 /* If the next token is now a `;', consume it. */
12723 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
12724 cp_lexer_consume_token (parser->lexer);
12725 goto done;
12728 else
12729 break;
12731 tree last_type;
12732 bool auto_specifier_p;
12733 /* NULL_TREE if both variable and function declaration are allowed,
12734 error_mark_node if function declaration are not allowed and
12735 a FUNCTION_DECL that should be diagnosed if it is followed by
12736 variable declarations. */
12737 tree auto_function_declaration;
12739 last_type = NULL_TREE;
12740 auto_specifier_p
12741 = decl_specifiers.type && type_uses_auto (decl_specifiers.type);
12742 auto_function_declaration = NULL_TREE;
12744 /* Keep going until we hit the `;' at the end of the simple
12745 declaration. */
12746 saw_declarator = false;
12747 while (cp_lexer_next_token_is_not (parser->lexer,
12748 CPP_SEMICOLON))
12750 cp_token *token;
12751 bool function_definition_p;
12752 tree decl;
12753 tree auto_result = NULL_TREE;
12755 if (saw_declarator)
12757 /* If we are processing next declarator, comma is expected */
12758 token = cp_lexer_peek_token (parser->lexer);
12759 gcc_assert (token->type == CPP_COMMA);
12760 cp_lexer_consume_token (parser->lexer);
12761 if (maybe_range_for_decl)
12763 *maybe_range_for_decl = error_mark_node;
12764 if (comma_loc == UNKNOWN_LOCATION)
12765 comma_loc = token->location;
12768 else
12769 saw_declarator = true;
12771 /* Parse the init-declarator. */
12772 decl = cp_parser_init_declarator (parser, &decl_specifiers,
12773 /*checks=*/NULL,
12774 function_definition_allowed_p,
12775 /*member_p=*/false,
12776 declares_class_or_enum,
12777 &function_definition_p,
12778 maybe_range_for_decl,
12779 &init_loc,
12780 &auto_result);
12781 /* If an error occurred while parsing tentatively, exit quickly.
12782 (That usually happens when in the body of a function; each
12783 statement is treated as a declaration-statement until proven
12784 otherwise.) */
12785 if (cp_parser_error_occurred (parser))
12786 goto done;
12788 if (auto_specifier_p && cxx_dialect >= cxx14)
12790 /* If the init-declarator-list contains more than one
12791 init-declarator, they shall all form declarations of
12792 variables. */
12793 if (auto_function_declaration == NULL_TREE)
12794 auto_function_declaration
12795 = TREE_CODE (decl) == FUNCTION_DECL ? decl : error_mark_node;
12796 else if (TREE_CODE (decl) == FUNCTION_DECL
12797 || auto_function_declaration != error_mark_node)
12799 error_at (decl_specifiers.locations[ds_type_spec],
12800 "non-variable %qD in declaration with more than one "
12801 "declarator with placeholder type",
12802 TREE_CODE (decl) == FUNCTION_DECL
12803 ? decl : auto_function_declaration);
12804 auto_function_declaration = error_mark_node;
12808 if (auto_result
12809 && (!processing_template_decl || !type_uses_auto (auto_result)))
12811 if (last_type
12812 && last_type != error_mark_node
12813 && !same_type_p (auto_result, last_type))
12815 /* If the list of declarators contains more than one declarator,
12816 the type of each declared variable is determined as described
12817 above. If the type deduced for the template parameter U is not
12818 the same in each deduction, the program is ill-formed. */
12819 error_at (decl_specifiers.locations[ds_type_spec],
12820 "inconsistent deduction for %qT: %qT and then %qT",
12821 decl_specifiers.type, last_type, auto_result);
12822 last_type = error_mark_node;
12824 else
12825 last_type = auto_result;
12828 /* Handle function definitions specially. */
12829 if (function_definition_p)
12831 /* If the next token is a `,', then we are probably
12832 processing something like:
12834 void f() {}, *p;
12836 which is erroneous. */
12837 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
12839 cp_token *token = cp_lexer_peek_token (parser->lexer);
12840 error_at (token->location,
12841 "mixing"
12842 " declarations and function-definitions is forbidden");
12844 /* Otherwise, we're done with the list of declarators. */
12845 else
12847 pop_deferring_access_checks ();
12848 return;
12851 if (maybe_range_for_decl && *maybe_range_for_decl == NULL_TREE)
12852 *maybe_range_for_decl = decl;
12853 /* The next token should be either a `,' or a `;'. */
12854 token = cp_lexer_peek_token (parser->lexer);
12855 /* If it's a `,', there are more declarators to come. */
12856 if (token->type == CPP_COMMA)
12857 /* will be consumed next time around */;
12858 /* If it's a `;', we are done. */
12859 else if (token->type == CPP_SEMICOLON)
12860 break;
12861 else if (maybe_range_for_decl)
12863 if ((declares_class_or_enum & 2) && token->type == CPP_COLON)
12864 permerror (decl_specifiers.locations[ds_type_spec],
12865 "types may not be defined in a for-range-declaration");
12866 break;
12868 /* Anything else is an error. */
12869 else
12871 /* If we have already issued an error message we don't need
12872 to issue another one. */
12873 if ((decl != error_mark_node
12874 && DECL_INITIAL (decl) != error_mark_node)
12875 || cp_parser_uncommitted_to_tentative_parse_p (parser))
12876 cp_parser_error (parser, "expected %<,%> or %<;%>");
12877 /* Skip tokens until we reach the end of the statement. */
12878 cp_parser_skip_to_end_of_statement (parser);
12879 /* If the next token is now a `;', consume it. */
12880 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
12881 cp_lexer_consume_token (parser->lexer);
12882 goto done;
12884 /* After the first time around, a function-definition is not
12885 allowed -- even if it was OK at first. For example:
12887 int i, f() {}
12889 is not valid. */
12890 function_definition_allowed_p = false;
12893 /* Issue an error message if no declarators are present, and the
12894 decl-specifier-seq does not itself declare a class or
12895 enumeration: [dcl.dcl]/3. */
12896 if (!saw_declarator)
12898 if (cp_parser_declares_only_class_p (parser))
12900 if (!declares_class_or_enum
12901 && decl_specifiers.type
12902 && OVERLOAD_TYPE_P (decl_specifiers.type))
12903 /* Ensure an error is issued anyway when finish_decltype_type,
12904 called via cp_parser_decl_specifier_seq, returns a class or
12905 an enumeration (c++/51786). */
12906 decl_specifiers.type = NULL_TREE;
12907 shadow_tag (&decl_specifiers);
12909 /* Perform any deferred access checks. */
12910 perform_deferred_access_checks (tf_warning_or_error);
12913 /* Consume the `;'. */
12914 finish:
12915 if (!maybe_range_for_decl)
12916 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12917 else if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
12919 if (init_loc != UNKNOWN_LOCATION)
12920 error_at (init_loc, "initializer in range-based %<for%> loop");
12921 if (comma_loc != UNKNOWN_LOCATION)
12922 error_at (comma_loc,
12923 "multiple declarations in range-based %<for%> loop");
12926 done:
12927 pop_deferring_access_checks ();
12930 /* Helper of cp_parser_simple_declaration, parse a decomposition declaration.
12931 decl-specifier-seq ref-qualifier [opt] [ identifier-list ]
12932 initializer ; */
12934 static tree
12935 cp_parser_decomposition_declaration (cp_parser *parser,
12936 cp_decl_specifier_seq *decl_specifiers,
12937 tree *maybe_range_for_decl,
12938 location_t *init_loc)
12940 cp_ref_qualifier ref_qual = cp_parser_ref_qualifier_opt (parser);
12941 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
12942 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
12944 /* Parse the identifier-list. */
12945 auto_vec<cp_expr, 10> v;
12946 if (!cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_SQUARE))
12947 while (true)
12949 cp_expr e = cp_parser_identifier (parser);
12950 if (e.get_value () == error_mark_node)
12951 break;
12952 v.safe_push (e);
12953 if (!cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
12954 break;
12955 cp_lexer_consume_token (parser->lexer);
12958 location_t end_loc = cp_lexer_peek_token (parser->lexer)->location;
12959 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
12961 end_loc = UNKNOWN_LOCATION;
12962 cp_parser_skip_to_closing_parenthesis_1 (parser, true, CPP_CLOSE_SQUARE,
12963 false);
12964 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_SQUARE))
12965 cp_lexer_consume_token (parser->lexer);
12966 else
12968 cp_parser_skip_to_end_of_statement (parser);
12969 return error_mark_node;
12973 if (cxx_dialect < cxx1z)
12974 pedwarn (loc, 0, "decomposition declaration only available with "
12975 "-std=c++1z or -std=gnu++1z");
12977 tree pushed_scope;
12978 cp_declarator *declarator = make_declarator (cdk_decomp);
12979 loc = end_loc == UNKNOWN_LOCATION ? loc : make_location (loc, loc, end_loc);
12980 declarator->id_loc = loc;
12981 if (ref_qual != REF_QUAL_NONE)
12982 declarator = make_reference_declarator (TYPE_UNQUALIFIED, declarator,
12983 ref_qual == REF_QUAL_RVALUE,
12984 NULL_TREE);
12985 tree decl = start_decl (declarator, decl_specifiers, SD_INITIALIZED,
12986 NULL_TREE, decl_specifiers->attributes,
12987 &pushed_scope);
12988 tree orig_decl = decl;
12990 unsigned int i;
12991 cp_expr e;
12992 cp_decl_specifier_seq decl_specs;
12993 clear_decl_specs (&decl_specs);
12994 decl_specs.type = make_auto ();
12995 tree prev = decl;
12996 FOR_EACH_VEC_ELT (v, i, e)
12998 if (i == 0)
12999 declarator = make_id_declarator (NULL_TREE, e.get_value (), sfk_none);
13000 else
13001 declarator->u.id.unqualified_name = e.get_value ();
13002 declarator->id_loc = e.get_location ();
13003 tree elt_pushed_scope;
13004 tree decl2 = start_decl (declarator, &decl_specs, SD_INITIALIZED,
13005 NULL_TREE, NULL_TREE, &elt_pushed_scope);
13006 if (decl2 == error_mark_node)
13007 decl = error_mark_node;
13008 else if (decl != error_mark_node && DECL_CHAIN (decl2) != prev)
13010 /* Ensure we've diagnosed redeclaration if we aren't creating
13011 a new VAR_DECL. */
13012 gcc_assert (errorcount);
13013 decl = error_mark_node;
13015 else
13016 prev = decl2;
13017 if (elt_pushed_scope)
13018 pop_scope (elt_pushed_scope);
13021 if (v.is_empty ())
13023 error_at (loc, "empty decomposition declaration");
13024 decl = error_mark_node;
13027 if (maybe_range_for_decl == NULL
13028 || cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
13030 bool non_constant_p = false, is_direct_init = false;
13031 *init_loc = cp_lexer_peek_token (parser->lexer)->location;
13032 tree initializer = cp_parser_initializer (parser, &is_direct_init,
13033 &non_constant_p);
13035 if (decl != error_mark_node)
13037 cp_finish_decl (decl, initializer, non_constant_p, NULL_TREE,
13038 is_direct_init ? LOOKUP_NORMAL : LOOKUP_IMPLICIT);
13039 cp_finish_decomp (decl, prev, v.length ());
13042 else if (decl != error_mark_node)
13044 *maybe_range_for_decl = prev;
13045 /* Ensure DECL_VALUE_EXPR is created for all the decls but
13046 the underlying DECL. */
13047 cp_finish_decomp (decl, prev, v.length ());
13050 if (pushed_scope)
13051 pop_scope (pushed_scope);
13053 if (decl == error_mark_node && DECL_P (orig_decl))
13055 if (DECL_NAMESPACE_SCOPE_P (orig_decl))
13056 SET_DECL_ASSEMBLER_NAME (orig_decl, get_identifier ("<decomp>"));
13059 return decl;
13062 /* Parse a decl-specifier-seq.
13064 decl-specifier-seq:
13065 decl-specifier-seq [opt] decl-specifier
13066 decl-specifier attribute-specifier-seq [opt] (C++11)
13068 decl-specifier:
13069 storage-class-specifier
13070 type-specifier
13071 function-specifier
13072 friend
13073 typedef
13075 GNU Extension:
13077 decl-specifier:
13078 attributes
13080 Concepts Extension:
13082 decl-specifier:
13083 concept
13085 Set *DECL_SPECS to a representation of the decl-specifier-seq.
13087 The parser flags FLAGS is used to control type-specifier parsing.
13089 *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
13090 flags:
13092 1: one of the decl-specifiers is an elaborated-type-specifier
13093 (i.e., a type declaration)
13094 2: one of the decl-specifiers is an enum-specifier or a
13095 class-specifier (i.e., a type definition)
13099 static void
13100 cp_parser_decl_specifier_seq (cp_parser* parser,
13101 cp_parser_flags flags,
13102 cp_decl_specifier_seq *decl_specs,
13103 int* declares_class_or_enum)
13105 bool constructor_possible_p = !parser->in_declarator_p;
13106 bool found_decl_spec = false;
13107 cp_token *start_token = NULL;
13108 cp_decl_spec ds;
13110 /* Clear DECL_SPECS. */
13111 clear_decl_specs (decl_specs);
13113 /* Assume no class or enumeration type is declared. */
13114 *declares_class_or_enum = 0;
13116 /* Keep reading specifiers until there are no more to read. */
13117 while (true)
13119 bool constructor_p;
13120 cp_token *token;
13121 ds = ds_last;
13123 /* Peek at the next token. */
13124 token = cp_lexer_peek_token (parser->lexer);
13126 /* Save the first token of the decl spec list for error
13127 reporting. */
13128 if (!start_token)
13129 start_token = token;
13130 /* Handle attributes. */
13131 if (cp_next_tokens_can_be_attribute_p (parser))
13133 /* Parse the attributes. */
13134 tree attrs = cp_parser_attributes_opt (parser);
13136 /* In a sequence of declaration specifiers, c++11 attributes
13137 appertain to the type that precede them. In that case
13138 [dcl.spec]/1 says:
13140 The attribute-specifier-seq affects the type only for
13141 the declaration it appears in, not other declarations
13142 involving the same type.
13144 But for now let's force the user to position the
13145 attribute either at the beginning of the declaration or
13146 after the declarator-id, which would clearly mean that it
13147 applies to the declarator. */
13148 if (cxx11_attribute_p (attrs))
13150 if (!found_decl_spec)
13151 /* The c++11 attribute is at the beginning of the
13152 declaration. It appertains to the entity being
13153 declared. */;
13154 else
13156 if (decl_specs->type && CLASS_TYPE_P (decl_specs->type))
13158 /* This is an attribute following a
13159 class-specifier. */
13160 if (decl_specs->type_definition_p)
13161 warn_misplaced_attr_for_class_type (token->location,
13162 decl_specs->type);
13163 attrs = NULL_TREE;
13165 else
13167 decl_specs->std_attributes
13168 = chainon (decl_specs->std_attributes,
13169 attrs);
13170 if (decl_specs->locations[ds_std_attribute] == 0)
13171 decl_specs->locations[ds_std_attribute] = token->location;
13173 continue;
13177 decl_specs->attributes
13178 = chainon (decl_specs->attributes,
13179 attrs);
13180 if (decl_specs->locations[ds_attribute] == 0)
13181 decl_specs->locations[ds_attribute] = token->location;
13182 continue;
13184 /* Assume we will find a decl-specifier keyword. */
13185 found_decl_spec = true;
13186 /* If the next token is an appropriate keyword, we can simply
13187 add it to the list. */
13188 switch (token->keyword)
13190 /* decl-specifier:
13191 friend
13192 constexpr */
13193 case RID_FRIEND:
13194 if (!at_class_scope_p ())
13196 error_at (token->location, "%<friend%> used outside of class");
13197 cp_lexer_purge_token (parser->lexer);
13199 else
13201 ds = ds_friend;
13202 /* Consume the token. */
13203 cp_lexer_consume_token (parser->lexer);
13205 break;
13207 case RID_CONSTEXPR:
13208 ds = ds_constexpr;
13209 cp_lexer_consume_token (parser->lexer);
13210 break;
13212 case RID_CONCEPT:
13213 ds = ds_concept;
13214 cp_lexer_consume_token (parser->lexer);
13215 break;
13217 /* function-specifier:
13218 inline
13219 virtual
13220 explicit */
13221 case RID_INLINE:
13222 case RID_VIRTUAL:
13223 case RID_EXPLICIT:
13224 cp_parser_function_specifier_opt (parser, decl_specs);
13225 break;
13227 /* decl-specifier:
13228 typedef */
13229 case RID_TYPEDEF:
13230 ds = ds_typedef;
13231 /* Consume the token. */
13232 cp_lexer_consume_token (parser->lexer);
13233 /* A constructor declarator cannot appear in a typedef. */
13234 constructor_possible_p = false;
13235 /* The "typedef" keyword can only occur in a declaration; we
13236 may as well commit at this point. */
13237 cp_parser_commit_to_tentative_parse (parser);
13239 if (decl_specs->storage_class != sc_none)
13240 decl_specs->conflicting_specifiers_p = true;
13241 break;
13243 /* storage-class-specifier:
13244 auto
13245 register
13246 static
13247 extern
13248 mutable
13250 GNU Extension:
13251 thread */
13252 case RID_AUTO:
13253 if (cxx_dialect == cxx98)
13255 /* Consume the token. */
13256 cp_lexer_consume_token (parser->lexer);
13258 /* Complain about `auto' as a storage specifier, if
13259 we're complaining about C++0x compatibility. */
13260 warning_at (token->location, OPT_Wc__11_compat, "%<auto%>"
13261 " changes meaning in C++11; please remove it");
13263 /* Set the storage class anyway. */
13264 cp_parser_set_storage_class (parser, decl_specs, RID_AUTO,
13265 token);
13267 else
13268 /* C++0x auto type-specifier. */
13269 found_decl_spec = false;
13270 break;
13272 case RID_REGISTER:
13273 case RID_STATIC:
13274 case RID_EXTERN:
13275 case RID_MUTABLE:
13276 /* Consume the token. */
13277 cp_lexer_consume_token (parser->lexer);
13278 cp_parser_set_storage_class (parser, decl_specs, token->keyword,
13279 token);
13280 break;
13281 case RID_THREAD:
13282 /* Consume the token. */
13283 ds = ds_thread;
13284 cp_lexer_consume_token (parser->lexer);
13285 break;
13287 default:
13288 /* We did not yet find a decl-specifier yet. */
13289 found_decl_spec = false;
13290 break;
13293 if (found_decl_spec
13294 && (flags & CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR)
13295 && token->keyword != RID_CONSTEXPR)
13296 error ("decl-specifier invalid in condition");
13298 if (found_decl_spec
13299 && (flags & CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR)
13300 && token->keyword != RID_MUTABLE
13301 && token->keyword != RID_CONSTEXPR)
13302 error_at (token->location, "%qD invalid in lambda",
13303 ridpointers[token->keyword]);
13305 if (ds != ds_last)
13306 set_and_check_decl_spec_loc (decl_specs, ds, token);
13308 /* Constructors are a special case. The `S' in `S()' is not a
13309 decl-specifier; it is the beginning of the declarator. */
13310 constructor_p
13311 = (!found_decl_spec
13312 && constructor_possible_p
13313 && (cp_parser_constructor_declarator_p
13314 (parser, decl_spec_seq_has_spec_p (decl_specs, ds_friend))));
13316 /* If we don't have a DECL_SPEC yet, then we must be looking at
13317 a type-specifier. */
13318 if (!found_decl_spec && !constructor_p)
13320 int decl_spec_declares_class_or_enum;
13321 bool is_cv_qualifier;
13322 tree type_spec;
13324 type_spec
13325 = cp_parser_type_specifier (parser, flags,
13326 decl_specs,
13327 /*is_declaration=*/true,
13328 &decl_spec_declares_class_or_enum,
13329 &is_cv_qualifier);
13330 *declares_class_or_enum |= decl_spec_declares_class_or_enum;
13332 /* If this type-specifier referenced a user-defined type
13333 (a typedef, class-name, etc.), then we can't allow any
13334 more such type-specifiers henceforth.
13336 [dcl.spec]
13338 The longest sequence of decl-specifiers that could
13339 possibly be a type name is taken as the
13340 decl-specifier-seq of a declaration. The sequence shall
13341 be self-consistent as described below.
13343 [dcl.type]
13345 As a general rule, at most one type-specifier is allowed
13346 in the complete decl-specifier-seq of a declaration. The
13347 only exceptions are the following:
13349 -- const or volatile can be combined with any other
13350 type-specifier.
13352 -- signed or unsigned can be combined with char, long,
13353 short, or int.
13355 -- ..
13357 Example:
13359 typedef char* Pc;
13360 void g (const int Pc);
13362 Here, Pc is *not* part of the decl-specifier seq; it's
13363 the declarator. Therefore, once we see a type-specifier
13364 (other than a cv-qualifier), we forbid any additional
13365 user-defined types. We *do* still allow things like `int
13366 int' to be considered a decl-specifier-seq, and issue the
13367 error message later. */
13368 if (type_spec && !is_cv_qualifier)
13369 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
13370 /* A constructor declarator cannot follow a type-specifier. */
13371 if (type_spec)
13373 constructor_possible_p = false;
13374 found_decl_spec = true;
13375 if (!is_cv_qualifier)
13376 decl_specs->any_type_specifiers_p = true;
13380 /* If we still do not have a DECL_SPEC, then there are no more
13381 decl-specifiers. */
13382 if (!found_decl_spec)
13383 break;
13385 decl_specs->any_specifiers_p = true;
13386 /* After we see one decl-specifier, further decl-specifiers are
13387 always optional. */
13388 flags |= CP_PARSER_FLAGS_OPTIONAL;
13391 /* Don't allow a friend specifier with a class definition. */
13392 if (decl_spec_seq_has_spec_p (decl_specs, ds_friend)
13393 && (*declares_class_or_enum & 2))
13394 error_at (decl_specs->locations[ds_friend],
13395 "class definition may not be declared a friend");
13398 /* Parse an (optional) storage-class-specifier.
13400 storage-class-specifier:
13401 auto
13402 register
13403 static
13404 extern
13405 mutable
13407 GNU Extension:
13409 storage-class-specifier:
13410 thread
13412 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
13414 static tree
13415 cp_parser_storage_class_specifier_opt (cp_parser* parser)
13417 switch (cp_lexer_peek_token (parser->lexer)->keyword)
13419 case RID_AUTO:
13420 if (cxx_dialect != cxx98)
13421 return NULL_TREE;
13422 /* Fall through for C++98. */
13423 gcc_fallthrough ();
13425 case RID_REGISTER:
13426 case RID_STATIC:
13427 case RID_EXTERN:
13428 case RID_MUTABLE:
13429 case RID_THREAD:
13430 /* Consume the token. */
13431 return cp_lexer_consume_token (parser->lexer)->u.value;
13433 default:
13434 return NULL_TREE;
13438 /* Parse an (optional) function-specifier.
13440 function-specifier:
13441 inline
13442 virtual
13443 explicit
13445 Returns an IDENTIFIER_NODE corresponding to the keyword used.
13446 Updates DECL_SPECS, if it is non-NULL. */
13448 static tree
13449 cp_parser_function_specifier_opt (cp_parser* parser,
13450 cp_decl_specifier_seq *decl_specs)
13452 cp_token *token = cp_lexer_peek_token (parser->lexer);
13453 switch (token->keyword)
13455 case RID_INLINE:
13456 set_and_check_decl_spec_loc (decl_specs, ds_inline, token);
13457 break;
13459 case RID_VIRTUAL:
13460 /* 14.5.2.3 [temp.mem]
13462 A member function template shall not be virtual. */
13463 if (PROCESSING_REAL_TEMPLATE_DECL_P ()
13464 && current_class_type)
13465 error_at (token->location, "templates may not be %<virtual%>");
13466 else
13467 set_and_check_decl_spec_loc (decl_specs, ds_virtual, token);
13468 break;
13470 case RID_EXPLICIT:
13471 set_and_check_decl_spec_loc (decl_specs, ds_explicit, token);
13472 break;
13474 default:
13475 return NULL_TREE;
13478 /* Consume the token. */
13479 return cp_lexer_consume_token (parser->lexer)->u.value;
13482 /* Parse a linkage-specification.
13484 linkage-specification:
13485 extern string-literal { declaration-seq [opt] }
13486 extern string-literal declaration */
13488 static void
13489 cp_parser_linkage_specification (cp_parser* parser)
13491 tree linkage;
13493 /* Look for the `extern' keyword. */
13494 cp_parser_require_keyword (parser, RID_EXTERN, RT_EXTERN);
13496 /* Look for the string-literal. */
13497 linkage = cp_parser_string_literal (parser, false, false);
13499 /* Transform the literal into an identifier. If the literal is a
13500 wide-character string, or contains embedded NULs, then we can't
13501 handle it as the user wants. */
13502 if (strlen (TREE_STRING_POINTER (linkage))
13503 != (size_t) (TREE_STRING_LENGTH (linkage) - 1))
13505 cp_parser_error (parser, "invalid linkage-specification");
13506 /* Assume C++ linkage. */
13507 linkage = lang_name_cplusplus;
13509 else
13510 linkage = get_identifier (TREE_STRING_POINTER (linkage));
13512 /* We're now using the new linkage. */
13513 push_lang_context (linkage);
13515 /* If the next token is a `{', then we're using the first
13516 production. */
13517 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
13519 cp_ensure_no_omp_declare_simd (parser);
13520 cp_ensure_no_oacc_routine (parser);
13522 /* Consume the `{' token. */
13523 cp_lexer_consume_token (parser->lexer);
13524 /* Parse the declarations. */
13525 cp_parser_declaration_seq_opt (parser);
13526 /* Look for the closing `}'. */
13527 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
13529 /* Otherwise, there's just one declaration. */
13530 else
13532 bool saved_in_unbraced_linkage_specification_p;
13534 saved_in_unbraced_linkage_specification_p
13535 = parser->in_unbraced_linkage_specification_p;
13536 parser->in_unbraced_linkage_specification_p = true;
13537 cp_parser_declaration (parser);
13538 parser->in_unbraced_linkage_specification_p
13539 = saved_in_unbraced_linkage_specification_p;
13542 /* We're done with the linkage-specification. */
13543 pop_lang_context ();
13546 /* Parse a static_assert-declaration.
13548 static_assert-declaration:
13549 static_assert ( constant-expression , string-literal ) ;
13550 static_assert ( constant-expression ) ; (C++1Z)
13552 If MEMBER_P, this static_assert is a class member. */
13554 static void
13555 cp_parser_static_assert(cp_parser *parser, bool member_p)
13557 tree condition;
13558 tree message;
13559 cp_token *token;
13560 location_t saved_loc;
13561 bool dummy;
13563 /* Peek at the `static_assert' token so we can keep track of exactly
13564 where the static assertion started. */
13565 token = cp_lexer_peek_token (parser->lexer);
13566 saved_loc = token->location;
13568 /* Look for the `static_assert' keyword. */
13569 if (!cp_parser_require_keyword (parser, RID_STATIC_ASSERT,
13570 RT_STATIC_ASSERT))
13571 return;
13573 /* We know we are in a static assertion; commit to any tentative
13574 parse. */
13575 if (cp_parser_parsing_tentatively (parser))
13576 cp_parser_commit_to_tentative_parse (parser);
13578 /* Parse the `(' starting the static assertion condition. */
13579 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
13581 /* Parse the constant-expression. Allow a non-constant expression
13582 here in order to give better diagnostics in finish_static_assert. */
13583 condition =
13584 cp_parser_constant_expression (parser,
13585 /*allow_non_constant_p=*/true,
13586 /*non_constant_p=*/&dummy);
13588 if (cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
13590 if (cxx_dialect < cxx1z)
13591 pedwarn (input_location, OPT_Wpedantic,
13592 "static_assert without a message "
13593 "only available with -std=c++1z or -std=gnu++1z");
13594 /* Eat the ')' */
13595 cp_lexer_consume_token (parser->lexer);
13596 message = build_string (1, "");
13597 TREE_TYPE (message) = char_array_type_node;
13598 fix_string_type (message);
13600 else
13602 /* Parse the separating `,'. */
13603 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
13605 /* Parse the string-literal message. */
13606 message = cp_parser_string_literal (parser,
13607 /*translate=*/false,
13608 /*wide_ok=*/true);
13610 /* A `)' completes the static assertion. */
13611 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
13612 cp_parser_skip_to_closing_parenthesis (parser,
13613 /*recovering=*/true,
13614 /*or_comma=*/false,
13615 /*consume_paren=*/true);
13618 /* A semicolon terminates the declaration. */
13619 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
13621 /* Complete the static assertion, which may mean either processing
13622 the static assert now or saving it for template instantiation. */
13623 finish_static_assert (condition, message, saved_loc, member_p);
13626 /* Parse the expression in decltype ( expression ). */
13628 static tree
13629 cp_parser_decltype_expr (cp_parser *parser,
13630 bool &id_expression_or_member_access_p)
13632 cp_token *id_expr_start_token;
13633 tree expr;
13635 /* Since we're going to preserve any side-effects from this parse, set up a
13636 firewall to protect our callers from cp_parser_commit_to_tentative_parse
13637 in the expression. */
13638 tentative_firewall firewall (parser);
13640 /* First, try parsing an id-expression. */
13641 id_expr_start_token = cp_lexer_peek_token (parser->lexer);
13642 cp_parser_parse_tentatively (parser);
13643 expr = cp_parser_id_expression (parser,
13644 /*template_keyword_p=*/false,
13645 /*check_dependency_p=*/true,
13646 /*template_p=*/NULL,
13647 /*declarator_p=*/false,
13648 /*optional_p=*/false);
13650 if (!cp_parser_error_occurred (parser) && expr != error_mark_node)
13652 bool non_integral_constant_expression_p = false;
13653 tree id_expression = expr;
13654 cp_id_kind idk;
13655 const char *error_msg;
13657 if (identifier_p (expr))
13658 /* Lookup the name we got back from the id-expression. */
13659 expr = cp_parser_lookup_name_simple (parser, expr,
13660 id_expr_start_token->location);
13662 if (expr
13663 && expr != error_mark_node
13664 && TREE_CODE (expr) != TYPE_DECL
13665 && (TREE_CODE (expr) != BIT_NOT_EXPR
13666 || !TYPE_P (TREE_OPERAND (expr, 0)))
13667 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
13669 /* Complete lookup of the id-expression. */
13670 expr = (finish_id_expression
13671 (id_expression, expr, parser->scope, &idk,
13672 /*integral_constant_expression_p=*/false,
13673 /*allow_non_integral_constant_expression_p=*/true,
13674 &non_integral_constant_expression_p,
13675 /*template_p=*/false,
13676 /*done=*/true,
13677 /*address_p=*/false,
13678 /*template_arg_p=*/false,
13679 &error_msg,
13680 id_expr_start_token->location));
13682 if (expr == error_mark_node)
13683 /* We found an id-expression, but it was something that we
13684 should not have found. This is an error, not something
13685 we can recover from, so note that we found an
13686 id-expression and we'll recover as gracefully as
13687 possible. */
13688 id_expression_or_member_access_p = true;
13691 if (expr
13692 && expr != error_mark_node
13693 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
13694 /* We have an id-expression. */
13695 id_expression_or_member_access_p = true;
13698 if (!id_expression_or_member_access_p)
13700 /* Abort the id-expression parse. */
13701 cp_parser_abort_tentative_parse (parser);
13703 /* Parsing tentatively, again. */
13704 cp_parser_parse_tentatively (parser);
13706 /* Parse a class member access. */
13707 expr = cp_parser_postfix_expression (parser, /*address_p=*/false,
13708 /*cast_p=*/false, /*decltype*/true,
13709 /*member_access_only_p=*/true, NULL);
13711 if (expr
13712 && expr != error_mark_node
13713 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
13714 /* We have an id-expression. */
13715 id_expression_or_member_access_p = true;
13718 if (id_expression_or_member_access_p)
13719 /* We have parsed the complete id-expression or member access. */
13720 cp_parser_parse_definitely (parser);
13721 else
13723 /* Abort our attempt to parse an id-expression or member access
13724 expression. */
13725 cp_parser_abort_tentative_parse (parser);
13727 /* Parse a full expression. */
13728 expr = cp_parser_expression (parser, /*pidk=*/NULL, /*cast_p=*/false,
13729 /*decltype_p=*/true);
13732 return expr;
13735 /* Parse a `decltype' type. Returns the type.
13737 simple-type-specifier:
13738 decltype ( expression )
13739 C++14 proposal:
13740 decltype ( auto ) */
13742 static tree
13743 cp_parser_decltype (cp_parser *parser)
13745 tree expr;
13746 bool id_expression_or_member_access_p = false;
13747 const char *saved_message;
13748 bool saved_integral_constant_expression_p;
13749 bool saved_non_integral_constant_expression_p;
13750 bool saved_greater_than_is_operator_p;
13751 cp_token *start_token = cp_lexer_peek_token (parser->lexer);
13753 if (start_token->type == CPP_DECLTYPE)
13755 /* Already parsed. */
13756 cp_lexer_consume_token (parser->lexer);
13757 return saved_checks_value (start_token->u.tree_check_value);
13760 /* Look for the `decltype' token. */
13761 if (!cp_parser_require_keyword (parser, RID_DECLTYPE, RT_DECLTYPE))
13762 return error_mark_node;
13764 /* Parse the opening `('. */
13765 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
13766 return error_mark_node;
13768 /* decltype (auto) */
13769 if (cxx_dialect >= cxx14
13770 && cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
13772 cp_lexer_consume_token (parser->lexer);
13773 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
13774 return error_mark_node;
13775 expr = make_decltype_auto ();
13776 AUTO_IS_DECLTYPE (expr) = true;
13777 goto rewrite;
13780 /* Types cannot be defined in a `decltype' expression. Save away the
13781 old message. */
13782 saved_message = parser->type_definition_forbidden_message;
13784 /* And create the new one. */
13785 parser->type_definition_forbidden_message
13786 = G_("types may not be defined in %<decltype%> expressions");
13788 /* The restrictions on constant-expressions do not apply inside
13789 decltype expressions. */
13790 saved_integral_constant_expression_p
13791 = parser->integral_constant_expression_p;
13792 saved_non_integral_constant_expression_p
13793 = parser->non_integral_constant_expression_p;
13794 parser->integral_constant_expression_p = false;
13796 /* Within a parenthesized expression, a `>' token is always
13797 the greater-than operator. */
13798 saved_greater_than_is_operator_p
13799 = parser->greater_than_is_operator_p;
13800 parser->greater_than_is_operator_p = true;
13802 /* Do not actually evaluate the expression. */
13803 ++cp_unevaluated_operand;
13805 /* Do not warn about problems with the expression. */
13806 ++c_inhibit_evaluation_warnings;
13808 expr = cp_parser_decltype_expr (parser, id_expression_or_member_access_p);
13810 /* Go back to evaluating expressions. */
13811 --cp_unevaluated_operand;
13812 --c_inhibit_evaluation_warnings;
13814 /* The `>' token might be the end of a template-id or
13815 template-parameter-list now. */
13816 parser->greater_than_is_operator_p
13817 = saved_greater_than_is_operator_p;
13819 /* Restore the old message and the integral constant expression
13820 flags. */
13821 parser->type_definition_forbidden_message = saved_message;
13822 parser->integral_constant_expression_p
13823 = saved_integral_constant_expression_p;
13824 parser->non_integral_constant_expression_p
13825 = saved_non_integral_constant_expression_p;
13827 /* Parse to the closing `)'. */
13828 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
13830 cp_parser_skip_to_closing_parenthesis (parser, true, false,
13831 /*consume_paren=*/true);
13832 return error_mark_node;
13835 expr = finish_decltype_type (expr, id_expression_or_member_access_p,
13836 tf_warning_or_error);
13838 rewrite:
13839 /* Replace the decltype with a CPP_DECLTYPE so we don't need to parse
13840 it again. */
13841 start_token->type = CPP_DECLTYPE;
13842 start_token->u.tree_check_value = ggc_cleared_alloc<struct tree_check> ();
13843 start_token->u.tree_check_value->value = expr;
13844 start_token->u.tree_check_value->checks = get_deferred_access_checks ();
13845 start_token->keyword = RID_MAX;
13846 cp_lexer_purge_tokens_after (parser->lexer, start_token);
13848 return expr;
13851 /* Special member functions [gram.special] */
13853 /* Parse a conversion-function-id.
13855 conversion-function-id:
13856 operator conversion-type-id
13858 Returns an IDENTIFIER_NODE representing the operator. */
13860 static tree
13861 cp_parser_conversion_function_id (cp_parser* parser)
13863 tree type;
13864 tree saved_scope;
13865 tree saved_qualifying_scope;
13866 tree saved_object_scope;
13867 tree pushed_scope = NULL_TREE;
13869 /* Look for the `operator' token. */
13870 if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
13871 return error_mark_node;
13872 /* When we parse the conversion-type-id, the current scope will be
13873 reset. However, we need that information in able to look up the
13874 conversion function later, so we save it here. */
13875 saved_scope = parser->scope;
13876 saved_qualifying_scope = parser->qualifying_scope;
13877 saved_object_scope = parser->object_scope;
13878 /* We must enter the scope of the class so that the names of
13879 entities declared within the class are available in the
13880 conversion-type-id. For example, consider:
13882 struct S {
13883 typedef int I;
13884 operator I();
13887 S::operator I() { ... }
13889 In order to see that `I' is a type-name in the definition, we
13890 must be in the scope of `S'. */
13891 if (saved_scope)
13892 pushed_scope = push_scope (saved_scope);
13893 /* Parse the conversion-type-id. */
13894 type = cp_parser_conversion_type_id (parser);
13895 /* Leave the scope of the class, if any. */
13896 if (pushed_scope)
13897 pop_scope (pushed_scope);
13898 /* Restore the saved scope. */
13899 parser->scope = saved_scope;
13900 parser->qualifying_scope = saved_qualifying_scope;
13901 parser->object_scope = saved_object_scope;
13902 /* If the TYPE is invalid, indicate failure. */
13903 if (type == error_mark_node)
13904 return error_mark_node;
13905 return mangle_conv_op_name_for_type (type);
13908 /* Parse a conversion-type-id:
13910 conversion-type-id:
13911 type-specifier-seq conversion-declarator [opt]
13913 Returns the TYPE specified. */
13915 static tree
13916 cp_parser_conversion_type_id (cp_parser* parser)
13918 tree attributes;
13919 cp_decl_specifier_seq type_specifiers;
13920 cp_declarator *declarator;
13921 tree type_specified;
13922 const char *saved_message;
13924 /* Parse the attributes. */
13925 attributes = cp_parser_attributes_opt (parser);
13927 saved_message = parser->type_definition_forbidden_message;
13928 parser->type_definition_forbidden_message
13929 = G_("types may not be defined in a conversion-type-id");
13931 /* Parse the type-specifiers. */
13932 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
13933 /*is_trailing_return=*/false,
13934 &type_specifiers);
13936 parser->type_definition_forbidden_message = saved_message;
13938 /* If that didn't work, stop. */
13939 if (type_specifiers.type == error_mark_node)
13940 return error_mark_node;
13941 /* Parse the conversion-declarator. */
13942 declarator = cp_parser_conversion_declarator_opt (parser);
13944 type_specified = grokdeclarator (declarator, &type_specifiers, TYPENAME,
13945 /*initialized=*/0, &attributes);
13946 if (attributes)
13947 cplus_decl_attributes (&type_specified, attributes, /*flags=*/0);
13949 /* Don't give this error when parsing tentatively. This happens to
13950 work because we always parse this definitively once. */
13951 if (! cp_parser_uncommitted_to_tentative_parse_p (parser)
13952 && type_uses_auto (type_specified))
13954 if (cxx_dialect < cxx14)
13956 error ("invalid use of %<auto%> in conversion operator");
13957 return error_mark_node;
13959 else if (template_parm_scope_p ())
13960 warning (0, "use of %<auto%> in member template "
13961 "conversion operator can never be deduced");
13964 return type_specified;
13967 /* Parse an (optional) conversion-declarator.
13969 conversion-declarator:
13970 ptr-operator conversion-declarator [opt]
13974 static cp_declarator *
13975 cp_parser_conversion_declarator_opt (cp_parser* parser)
13977 enum tree_code code;
13978 tree class_type, std_attributes = NULL_TREE;
13979 cp_cv_quals cv_quals;
13981 /* We don't know if there's a ptr-operator next, or not. */
13982 cp_parser_parse_tentatively (parser);
13983 /* Try the ptr-operator. */
13984 code = cp_parser_ptr_operator (parser, &class_type, &cv_quals,
13985 &std_attributes);
13986 /* If it worked, look for more conversion-declarators. */
13987 if (cp_parser_parse_definitely (parser))
13989 cp_declarator *declarator;
13991 /* Parse another optional declarator. */
13992 declarator = cp_parser_conversion_declarator_opt (parser);
13994 declarator = cp_parser_make_indirect_declarator
13995 (code, class_type, cv_quals, declarator, std_attributes);
13997 return declarator;
14000 return NULL;
14003 /* Parse an (optional) ctor-initializer.
14005 ctor-initializer:
14006 : mem-initializer-list
14008 Returns TRUE iff the ctor-initializer was actually present. */
14010 static bool
14011 cp_parser_ctor_initializer_opt (cp_parser* parser)
14013 /* If the next token is not a `:', then there is no
14014 ctor-initializer. */
14015 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
14017 /* Do default initialization of any bases and members. */
14018 if (DECL_CONSTRUCTOR_P (current_function_decl))
14019 finish_mem_initializers (NULL_TREE);
14021 return false;
14024 /* Consume the `:' token. */
14025 cp_lexer_consume_token (parser->lexer);
14026 /* And the mem-initializer-list. */
14027 cp_parser_mem_initializer_list (parser);
14029 return true;
14032 /* Parse a mem-initializer-list.
14034 mem-initializer-list:
14035 mem-initializer ... [opt]
14036 mem-initializer ... [opt] , mem-initializer-list */
14038 static void
14039 cp_parser_mem_initializer_list (cp_parser* parser)
14041 tree mem_initializer_list = NULL_TREE;
14042 tree target_ctor = error_mark_node;
14043 cp_token *token = cp_lexer_peek_token (parser->lexer);
14045 /* Let the semantic analysis code know that we are starting the
14046 mem-initializer-list. */
14047 if (!DECL_CONSTRUCTOR_P (current_function_decl))
14048 error_at (token->location,
14049 "only constructors take member initializers");
14051 /* Loop through the list. */
14052 while (true)
14054 tree mem_initializer;
14056 token = cp_lexer_peek_token (parser->lexer);
14057 /* Parse the mem-initializer. */
14058 mem_initializer = cp_parser_mem_initializer (parser);
14059 /* If the next token is a `...', we're expanding member initializers. */
14060 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
14062 /* Consume the `...'. */
14063 cp_lexer_consume_token (parser->lexer);
14065 /* The TREE_PURPOSE must be a _TYPE, because base-specifiers
14066 can be expanded but members cannot. */
14067 if (mem_initializer != error_mark_node
14068 && !TYPE_P (TREE_PURPOSE (mem_initializer)))
14070 error_at (token->location,
14071 "cannot expand initializer for member %<%D%>",
14072 TREE_PURPOSE (mem_initializer));
14073 mem_initializer = error_mark_node;
14076 /* Construct the pack expansion type. */
14077 if (mem_initializer != error_mark_node)
14078 mem_initializer = make_pack_expansion (mem_initializer);
14080 if (target_ctor != error_mark_node
14081 && mem_initializer != error_mark_node)
14083 error ("mem-initializer for %qD follows constructor delegation",
14084 TREE_PURPOSE (mem_initializer));
14085 mem_initializer = error_mark_node;
14087 /* Look for a target constructor. */
14088 if (mem_initializer != error_mark_node
14089 && CLASS_TYPE_P (TREE_PURPOSE (mem_initializer))
14090 && same_type_p (TREE_PURPOSE (mem_initializer), current_class_type))
14092 maybe_warn_cpp0x (CPP0X_DELEGATING_CTORS);
14093 if (mem_initializer_list)
14095 error ("constructor delegation follows mem-initializer for %qD",
14096 TREE_PURPOSE (mem_initializer_list));
14097 mem_initializer = error_mark_node;
14099 target_ctor = mem_initializer;
14101 /* Add it to the list, unless it was erroneous. */
14102 if (mem_initializer != error_mark_node)
14104 TREE_CHAIN (mem_initializer) = mem_initializer_list;
14105 mem_initializer_list = mem_initializer;
14107 /* If the next token is not a `,', we're done. */
14108 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
14109 break;
14110 /* Consume the `,' token. */
14111 cp_lexer_consume_token (parser->lexer);
14114 /* Perform semantic analysis. */
14115 if (DECL_CONSTRUCTOR_P (current_function_decl))
14116 finish_mem_initializers (mem_initializer_list);
14119 /* Parse a mem-initializer.
14121 mem-initializer:
14122 mem-initializer-id ( expression-list [opt] )
14123 mem-initializer-id braced-init-list
14125 GNU extension:
14127 mem-initializer:
14128 ( expression-list [opt] )
14130 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
14131 class) or FIELD_DECL (for a non-static data member) to initialize;
14132 the TREE_VALUE is the expression-list. An empty initialization
14133 list is represented by void_list_node. */
14135 static tree
14136 cp_parser_mem_initializer (cp_parser* parser)
14138 tree mem_initializer_id;
14139 tree expression_list;
14140 tree member;
14141 cp_token *token = cp_lexer_peek_token (parser->lexer);
14143 /* Find out what is being initialized. */
14144 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
14146 permerror (token->location,
14147 "anachronistic old-style base class initializer");
14148 mem_initializer_id = NULL_TREE;
14150 else
14152 mem_initializer_id = cp_parser_mem_initializer_id (parser);
14153 if (mem_initializer_id == error_mark_node)
14154 return mem_initializer_id;
14156 member = expand_member_init (mem_initializer_id);
14157 if (member && !DECL_P (member))
14158 in_base_initializer = 1;
14160 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
14162 bool expr_non_constant_p;
14163 cp_lexer_set_source_position (parser->lexer);
14164 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
14165 expression_list = cp_parser_braced_list (parser, &expr_non_constant_p);
14166 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
14167 expression_list = build_tree_list (NULL_TREE, expression_list);
14169 else
14171 vec<tree, va_gc> *vec;
14172 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
14173 /*cast_p=*/false,
14174 /*allow_expansion_p=*/true,
14175 /*non_constant_p=*/NULL);
14176 if (vec == NULL)
14177 return error_mark_node;
14178 expression_list = build_tree_list_vec (vec);
14179 release_tree_vector (vec);
14182 if (expression_list == error_mark_node)
14183 return error_mark_node;
14184 if (!expression_list)
14185 expression_list = void_type_node;
14187 in_base_initializer = 0;
14189 return member ? build_tree_list (member, expression_list) : error_mark_node;
14192 /* Parse a mem-initializer-id.
14194 mem-initializer-id:
14195 :: [opt] nested-name-specifier [opt] class-name
14196 decltype-specifier (C++11)
14197 identifier
14199 Returns a TYPE indicating the class to be initialized for the first
14200 production (and the second in C++11). Returns an IDENTIFIER_NODE
14201 indicating the data member to be initialized for the last production. */
14203 static tree
14204 cp_parser_mem_initializer_id (cp_parser* parser)
14206 bool global_scope_p;
14207 bool nested_name_specifier_p;
14208 bool template_p = false;
14209 tree id;
14211 cp_token *token = cp_lexer_peek_token (parser->lexer);
14213 /* `typename' is not allowed in this context ([temp.res]). */
14214 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
14216 error_at (token->location,
14217 "keyword %<typename%> not allowed in this context (a qualified "
14218 "member initializer is implicitly a type)");
14219 cp_lexer_consume_token (parser->lexer);
14221 /* Look for the optional `::' operator. */
14222 global_scope_p
14223 = (cp_parser_global_scope_opt (parser,
14224 /*current_scope_valid_p=*/false)
14225 != NULL_TREE);
14226 /* Look for the optional nested-name-specifier. The simplest way to
14227 implement:
14229 [temp.res]
14231 The keyword `typename' is not permitted in a base-specifier or
14232 mem-initializer; in these contexts a qualified name that
14233 depends on a template-parameter is implicitly assumed to be a
14234 type name.
14236 is to assume that we have seen the `typename' keyword at this
14237 point. */
14238 nested_name_specifier_p
14239 = (cp_parser_nested_name_specifier_opt (parser,
14240 /*typename_keyword_p=*/true,
14241 /*check_dependency_p=*/true,
14242 /*type_p=*/true,
14243 /*is_declaration=*/true)
14244 != NULL_TREE);
14245 if (nested_name_specifier_p)
14246 template_p = cp_parser_optional_template_keyword (parser);
14247 /* If there is a `::' operator or a nested-name-specifier, then we
14248 are definitely looking for a class-name. */
14249 if (global_scope_p || nested_name_specifier_p)
14250 return cp_parser_class_name (parser,
14251 /*typename_keyword_p=*/true,
14252 /*template_keyword_p=*/template_p,
14253 typename_type,
14254 /*check_dependency_p=*/true,
14255 /*class_head_p=*/false,
14256 /*is_declaration=*/true);
14257 /* Otherwise, we could also be looking for an ordinary identifier. */
14258 cp_parser_parse_tentatively (parser);
14259 if (cp_lexer_next_token_is_decltype (parser->lexer))
14260 /* Try a decltype-specifier. */
14261 id = cp_parser_decltype (parser);
14262 else
14263 /* Otherwise, try a class-name. */
14264 id = cp_parser_class_name (parser,
14265 /*typename_keyword_p=*/true,
14266 /*template_keyword_p=*/false,
14267 none_type,
14268 /*check_dependency_p=*/true,
14269 /*class_head_p=*/false,
14270 /*is_declaration=*/true);
14271 /* If we found one, we're done. */
14272 if (cp_parser_parse_definitely (parser))
14273 return id;
14274 /* Otherwise, look for an ordinary identifier. */
14275 return cp_parser_identifier (parser);
14278 /* Overloading [gram.over] */
14280 /* Parse an operator-function-id.
14282 operator-function-id:
14283 operator operator
14285 Returns an IDENTIFIER_NODE for the operator which is a
14286 human-readable spelling of the identifier, e.g., `operator +'. */
14288 static cp_expr
14289 cp_parser_operator_function_id (cp_parser* parser)
14291 /* Look for the `operator' keyword. */
14292 if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
14293 return error_mark_node;
14294 /* And then the name of the operator itself. */
14295 return cp_parser_operator (parser);
14298 /* Return an identifier node for a user-defined literal operator.
14299 The suffix identifier is chained to the operator name identifier. */
14301 tree
14302 cp_literal_operator_id (const char* name)
14304 tree identifier;
14305 char *buffer = XNEWVEC (char, strlen (UDLIT_OP_ANSI_PREFIX)
14306 + strlen (name) + 10);
14307 sprintf (buffer, UDLIT_OP_ANSI_FORMAT, name);
14308 identifier = get_identifier (buffer);
14310 return identifier;
14313 /* Parse an operator.
14315 operator:
14316 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
14317 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
14318 || ++ -- , ->* -> () []
14320 GNU Extensions:
14322 operator:
14323 <? >? <?= >?=
14325 Returns an IDENTIFIER_NODE for the operator which is a
14326 human-readable spelling of the identifier, e.g., `operator +'. */
14328 static cp_expr
14329 cp_parser_operator (cp_parser* parser)
14331 tree id = NULL_TREE;
14332 cp_token *token;
14333 bool utf8 = false;
14335 /* Peek at the next token. */
14336 token = cp_lexer_peek_token (parser->lexer);
14338 location_t start_loc = token->location;
14340 /* Figure out which operator we have. */
14341 switch (token->type)
14343 case CPP_KEYWORD:
14345 enum tree_code op;
14347 /* The keyword should be either `new' or `delete'. */
14348 if (token->keyword == RID_NEW)
14349 op = NEW_EXPR;
14350 else if (token->keyword == RID_DELETE)
14351 op = DELETE_EXPR;
14352 else
14353 break;
14355 /* Consume the `new' or `delete' token. */
14356 location_t end_loc = cp_lexer_consume_token (parser->lexer)->location;
14358 /* Peek at the next token. */
14359 token = cp_lexer_peek_token (parser->lexer);
14360 /* If it's a `[' token then this is the array variant of the
14361 operator. */
14362 if (token->type == CPP_OPEN_SQUARE)
14364 /* Consume the `[' token. */
14365 cp_lexer_consume_token (parser->lexer);
14366 /* Look for the `]' token. */
14367 if (cp_token *close_token
14368 = cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
14369 end_loc = close_token->location;
14370 id = cp_operator_id (op == NEW_EXPR
14371 ? VEC_NEW_EXPR : VEC_DELETE_EXPR);
14373 /* Otherwise, we have the non-array variant. */
14374 else
14375 id = cp_operator_id (op);
14377 location_t loc = make_location (start_loc, start_loc, end_loc);
14379 return cp_expr (id, loc);
14382 case CPP_PLUS:
14383 id = cp_operator_id (PLUS_EXPR);
14384 break;
14386 case CPP_MINUS:
14387 id = cp_operator_id (MINUS_EXPR);
14388 break;
14390 case CPP_MULT:
14391 id = cp_operator_id (MULT_EXPR);
14392 break;
14394 case CPP_DIV:
14395 id = cp_operator_id (TRUNC_DIV_EXPR);
14396 break;
14398 case CPP_MOD:
14399 id = cp_operator_id (TRUNC_MOD_EXPR);
14400 break;
14402 case CPP_XOR:
14403 id = cp_operator_id (BIT_XOR_EXPR);
14404 break;
14406 case CPP_AND:
14407 id = cp_operator_id (BIT_AND_EXPR);
14408 break;
14410 case CPP_OR:
14411 id = cp_operator_id (BIT_IOR_EXPR);
14412 break;
14414 case CPP_COMPL:
14415 id = cp_operator_id (BIT_NOT_EXPR);
14416 break;
14418 case CPP_NOT:
14419 id = cp_operator_id (TRUTH_NOT_EXPR);
14420 break;
14422 case CPP_EQ:
14423 id = cp_assignment_operator_id (NOP_EXPR);
14424 break;
14426 case CPP_LESS:
14427 id = cp_operator_id (LT_EXPR);
14428 break;
14430 case CPP_GREATER:
14431 id = cp_operator_id (GT_EXPR);
14432 break;
14434 case CPP_PLUS_EQ:
14435 id = cp_assignment_operator_id (PLUS_EXPR);
14436 break;
14438 case CPP_MINUS_EQ:
14439 id = cp_assignment_operator_id (MINUS_EXPR);
14440 break;
14442 case CPP_MULT_EQ:
14443 id = cp_assignment_operator_id (MULT_EXPR);
14444 break;
14446 case CPP_DIV_EQ:
14447 id = cp_assignment_operator_id (TRUNC_DIV_EXPR);
14448 break;
14450 case CPP_MOD_EQ:
14451 id = cp_assignment_operator_id (TRUNC_MOD_EXPR);
14452 break;
14454 case CPP_XOR_EQ:
14455 id = cp_assignment_operator_id (BIT_XOR_EXPR);
14456 break;
14458 case CPP_AND_EQ:
14459 id = cp_assignment_operator_id (BIT_AND_EXPR);
14460 break;
14462 case CPP_OR_EQ:
14463 id = cp_assignment_operator_id (BIT_IOR_EXPR);
14464 break;
14466 case CPP_LSHIFT:
14467 id = cp_operator_id (LSHIFT_EXPR);
14468 break;
14470 case CPP_RSHIFT:
14471 id = cp_operator_id (RSHIFT_EXPR);
14472 break;
14474 case CPP_LSHIFT_EQ:
14475 id = cp_assignment_operator_id (LSHIFT_EXPR);
14476 break;
14478 case CPP_RSHIFT_EQ:
14479 id = cp_assignment_operator_id (RSHIFT_EXPR);
14480 break;
14482 case CPP_EQ_EQ:
14483 id = cp_operator_id (EQ_EXPR);
14484 break;
14486 case CPP_NOT_EQ:
14487 id = cp_operator_id (NE_EXPR);
14488 break;
14490 case CPP_LESS_EQ:
14491 id = cp_operator_id (LE_EXPR);
14492 break;
14494 case CPP_GREATER_EQ:
14495 id = cp_operator_id (GE_EXPR);
14496 break;
14498 case CPP_AND_AND:
14499 id = cp_operator_id (TRUTH_ANDIF_EXPR);
14500 break;
14502 case CPP_OR_OR:
14503 id = cp_operator_id (TRUTH_ORIF_EXPR);
14504 break;
14506 case CPP_PLUS_PLUS:
14507 id = cp_operator_id (POSTINCREMENT_EXPR);
14508 break;
14510 case CPP_MINUS_MINUS:
14511 id = cp_operator_id (PREDECREMENT_EXPR);
14512 break;
14514 case CPP_COMMA:
14515 id = cp_operator_id (COMPOUND_EXPR);
14516 break;
14518 case CPP_DEREF_STAR:
14519 id = cp_operator_id (MEMBER_REF);
14520 break;
14522 case CPP_DEREF:
14523 id = cp_operator_id (COMPONENT_REF);
14524 break;
14526 case CPP_OPEN_PAREN:
14527 /* Consume the `('. */
14528 cp_lexer_consume_token (parser->lexer);
14529 /* Look for the matching `)'. */
14530 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
14531 return cp_operator_id (CALL_EXPR);
14533 case CPP_OPEN_SQUARE:
14534 /* Consume the `['. */
14535 cp_lexer_consume_token (parser->lexer);
14536 /* Look for the matching `]'. */
14537 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
14538 return cp_operator_id (ARRAY_REF);
14540 case CPP_UTF8STRING:
14541 case CPP_UTF8STRING_USERDEF:
14542 utf8 = true;
14543 /* FALLTHRU */
14544 case CPP_STRING:
14545 case CPP_WSTRING:
14546 case CPP_STRING16:
14547 case CPP_STRING32:
14548 case CPP_STRING_USERDEF:
14549 case CPP_WSTRING_USERDEF:
14550 case CPP_STRING16_USERDEF:
14551 case CPP_STRING32_USERDEF:
14553 tree str, string_tree;
14554 int sz, len;
14556 if (cxx_dialect == cxx98)
14557 maybe_warn_cpp0x (CPP0X_USER_DEFINED_LITERALS);
14559 /* Consume the string. */
14560 str = cp_parser_string_literal (parser, /*translate=*/true,
14561 /*wide_ok=*/true, /*lookup_udlit=*/false);
14562 if (str == error_mark_node)
14563 return error_mark_node;
14564 else if (TREE_CODE (str) == USERDEF_LITERAL)
14566 string_tree = USERDEF_LITERAL_VALUE (str);
14567 id = USERDEF_LITERAL_SUFFIX_ID (str);
14569 else
14571 string_tree = str;
14572 /* Look for the suffix identifier. */
14573 token = cp_lexer_peek_token (parser->lexer);
14574 if (token->type == CPP_NAME)
14575 id = cp_parser_identifier (parser);
14576 else if (token->type == CPP_KEYWORD)
14578 error ("unexpected keyword;"
14579 " remove space between quotes and suffix identifier");
14580 return error_mark_node;
14582 else
14584 error ("expected suffix identifier");
14585 return error_mark_node;
14588 sz = TREE_INT_CST_LOW (TYPE_SIZE_UNIT
14589 (TREE_TYPE (TREE_TYPE (string_tree))));
14590 len = TREE_STRING_LENGTH (string_tree) / sz - 1;
14591 if (len != 0)
14593 error ("expected empty string after %<operator%> keyword");
14594 return error_mark_node;
14596 if (utf8 || TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (string_tree)))
14597 != char_type_node)
14599 error ("invalid encoding prefix in literal operator");
14600 return error_mark_node;
14602 if (id != error_mark_node)
14604 const char *name = IDENTIFIER_POINTER (id);
14605 id = cp_literal_operator_id (name);
14607 return id;
14610 default:
14611 /* Anything else is an error. */
14612 break;
14615 /* If we have selected an identifier, we need to consume the
14616 operator token. */
14617 if (id)
14618 cp_lexer_consume_token (parser->lexer);
14619 /* Otherwise, no valid operator name was present. */
14620 else
14622 cp_parser_error (parser, "expected operator");
14623 id = error_mark_node;
14626 return cp_expr (id, start_loc);
14629 /* Parse a template-declaration.
14631 template-declaration:
14632 export [opt] template < template-parameter-list > declaration
14634 If MEMBER_P is TRUE, this template-declaration occurs within a
14635 class-specifier.
14637 The grammar rule given by the standard isn't correct. What
14638 is really meant is:
14640 template-declaration:
14641 export [opt] template-parameter-list-seq
14642 decl-specifier-seq [opt] init-declarator [opt] ;
14643 export [opt] template-parameter-list-seq
14644 function-definition
14646 template-parameter-list-seq:
14647 template-parameter-list-seq [opt]
14648 template < template-parameter-list >
14650 Concept Extensions:
14652 template-parameter-list-seq:
14653 template < template-parameter-list > requires-clause [opt]
14655 requires-clause:
14656 requires logical-or-expression */
14658 static void
14659 cp_parser_template_declaration (cp_parser* parser, bool member_p)
14661 /* Check for `export'. */
14662 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
14664 /* Consume the `export' token. */
14665 cp_lexer_consume_token (parser->lexer);
14666 /* Warn that we do not support `export'. */
14667 warning (0, "keyword %<export%> not implemented, and will be ignored");
14670 cp_parser_template_declaration_after_export (parser, member_p);
14673 /* Parse a template-parameter-list.
14675 template-parameter-list:
14676 template-parameter
14677 template-parameter-list , template-parameter
14679 Returns a TREE_LIST. Each node represents a template parameter.
14680 The nodes are connected via their TREE_CHAINs. */
14682 static tree
14683 cp_parser_template_parameter_list (cp_parser* parser)
14685 tree parameter_list = NULL_TREE;
14687 begin_template_parm_list ();
14689 /* The loop below parses the template parms. We first need to know
14690 the total number of template parms to be able to compute proper
14691 canonical types of each dependent type. So after the loop, when
14692 we know the total number of template parms,
14693 end_template_parm_list computes the proper canonical types and
14694 fixes up the dependent types accordingly. */
14695 while (true)
14697 tree parameter;
14698 bool is_non_type;
14699 bool is_parameter_pack;
14700 location_t parm_loc;
14702 /* Parse the template-parameter. */
14703 parm_loc = cp_lexer_peek_token (parser->lexer)->location;
14704 parameter = cp_parser_template_parameter (parser,
14705 &is_non_type,
14706 &is_parameter_pack);
14707 /* Add it to the list. */
14708 if (parameter != error_mark_node)
14709 parameter_list = process_template_parm (parameter_list,
14710 parm_loc,
14711 parameter,
14712 is_non_type,
14713 is_parameter_pack);
14714 else
14716 tree err_parm = build_tree_list (parameter, parameter);
14717 parameter_list = chainon (parameter_list, err_parm);
14720 /* If the next token is not a `,', we're done. */
14721 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
14722 break;
14723 /* Otherwise, consume the `,' token. */
14724 cp_lexer_consume_token (parser->lexer);
14727 return end_template_parm_list (parameter_list);
14730 /* Parse a introduction-list.
14732 introduction-list:
14733 introduced-parameter
14734 introduction-list , introduced-parameter
14736 introduced-parameter:
14737 ...[opt] identifier
14739 Returns a TREE_VEC of WILDCARD_DECLs. If the parameter is a pack
14740 then the introduced parm will have WILDCARD_PACK_P set. In addition, the
14741 WILDCARD_DECL will also have DECL_NAME set and token location in
14742 DECL_SOURCE_LOCATION. */
14744 static tree
14745 cp_parser_introduction_list (cp_parser *parser)
14747 vec<tree, va_gc> *introduction_vec = make_tree_vector ();
14749 while (true)
14751 bool is_pack = cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS);
14752 if (is_pack)
14753 cp_lexer_consume_token (parser->lexer);
14755 /* Build placeholder. */
14756 tree parm = build_nt (WILDCARD_DECL);
14757 DECL_SOURCE_LOCATION (parm)
14758 = cp_lexer_peek_token (parser->lexer)->location;
14759 DECL_NAME (parm) = cp_parser_identifier (parser);
14760 WILDCARD_PACK_P (parm) = is_pack;
14761 vec_safe_push (introduction_vec, parm);
14763 /* If the next token is not a `,', we're done. */
14764 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
14765 break;
14766 /* Otherwise, consume the `,' token. */
14767 cp_lexer_consume_token (parser->lexer);
14770 /* Convert the vec into a TREE_VEC. */
14771 tree introduction_list = make_tree_vec (introduction_vec->length ());
14772 unsigned int n;
14773 tree parm;
14774 FOR_EACH_VEC_ELT (*introduction_vec, n, parm)
14775 TREE_VEC_ELT (introduction_list, n) = parm;
14777 release_tree_vector (introduction_vec);
14778 return introduction_list;
14781 /* Given a declarator, get the declarator-id part, or NULL_TREE if this
14782 is an abstract declarator. */
14784 static inline cp_declarator*
14785 get_id_declarator (cp_declarator *declarator)
14787 cp_declarator *d = declarator;
14788 while (d && d->kind != cdk_id)
14789 d = d->declarator;
14790 return d;
14793 /* Get the unqualified-id from the DECLARATOR or NULL_TREE if this
14794 is an abstract declarator. */
14796 static inline tree
14797 get_unqualified_id (cp_declarator *declarator)
14799 declarator = get_id_declarator (declarator);
14800 if (declarator)
14801 return declarator->u.id.unqualified_name;
14802 else
14803 return NULL_TREE;
14806 /* Returns true if DECL represents a constrained-parameter. */
14808 static inline bool
14809 is_constrained_parameter (tree decl)
14811 return (decl
14812 && TREE_CODE (decl) == TYPE_DECL
14813 && CONSTRAINED_PARM_CONCEPT (decl)
14814 && DECL_P (CONSTRAINED_PARM_CONCEPT (decl)));
14817 /* Returns true if PARM declares a constrained-parameter. */
14819 static inline bool
14820 is_constrained_parameter (cp_parameter_declarator *parm)
14822 return is_constrained_parameter (parm->decl_specifiers.type);
14825 /* Check that the type parameter is only a declarator-id, and that its
14826 type is not cv-qualified. */
14828 bool
14829 cp_parser_check_constrained_type_parm (cp_parser *parser,
14830 cp_parameter_declarator *parm)
14832 if (!parm->declarator)
14833 return true;
14835 if (parm->declarator->kind != cdk_id)
14837 cp_parser_error (parser, "invalid constrained type parameter");
14838 return false;
14841 /* Don't allow cv-qualified type parameters. */
14842 if (decl_spec_seq_has_spec_p (&parm->decl_specifiers, ds_const)
14843 || decl_spec_seq_has_spec_p (&parm->decl_specifiers, ds_volatile))
14845 cp_parser_error (parser, "cv-qualified type parameter");
14846 return false;
14849 return true;
14852 /* Finish parsing/processing a template type parameter and checking
14853 various restrictions. */
14855 static inline tree
14856 cp_parser_constrained_type_template_parm (cp_parser *parser,
14857 tree id,
14858 cp_parameter_declarator* parmdecl)
14860 if (cp_parser_check_constrained_type_parm (parser, parmdecl))
14861 return finish_template_type_parm (class_type_node, id);
14862 else
14863 return error_mark_node;
14866 static tree
14867 finish_constrained_template_template_parm (tree proto, tree id)
14869 /* FIXME: This should probably be copied, and we may need to adjust
14870 the template parameter depths. */
14871 tree saved_parms = current_template_parms;
14872 begin_template_parm_list ();
14873 current_template_parms = DECL_TEMPLATE_PARMS (proto);
14874 end_template_parm_list ();
14876 tree parm = finish_template_template_parm (class_type_node, id);
14877 current_template_parms = saved_parms;
14879 return parm;
14882 /* Finish parsing/processing a template template parameter by borrowing
14883 the template parameter list from the prototype parameter. */
14885 static tree
14886 cp_parser_constrained_template_template_parm (cp_parser *parser,
14887 tree proto,
14888 tree id,
14889 cp_parameter_declarator *parmdecl)
14891 if (!cp_parser_check_constrained_type_parm (parser, parmdecl))
14892 return error_mark_node;
14893 return finish_constrained_template_template_parm (proto, id);
14896 /* Create a new non-type template parameter from the given PARM
14897 declarator. */
14899 static tree
14900 constrained_non_type_template_parm (bool *is_non_type,
14901 cp_parameter_declarator *parm)
14903 *is_non_type = true;
14904 cp_declarator *decl = parm->declarator;
14905 cp_decl_specifier_seq *specs = &parm->decl_specifiers;
14906 specs->type = TREE_TYPE (DECL_INITIAL (specs->type));
14907 return grokdeclarator (decl, specs, TPARM, 0, NULL);
14910 /* Build a constrained template parameter based on the PARMDECL
14911 declarator. The type of PARMDECL is the constrained type, which
14912 refers to the prototype template parameter that ultimately
14913 specifies the type of the declared parameter. */
14915 static tree
14916 finish_constrained_parameter (cp_parser *parser,
14917 cp_parameter_declarator *parmdecl,
14918 bool *is_non_type,
14919 bool *is_parameter_pack)
14921 tree decl = parmdecl->decl_specifiers.type;
14922 tree id = get_unqualified_id (parmdecl->declarator);
14923 tree def = parmdecl->default_argument;
14924 tree proto = DECL_INITIAL (decl);
14926 /* A template parameter constrained by a variadic concept shall also
14927 be declared as a template parameter pack. */
14928 bool is_variadic = template_parameter_pack_p (proto);
14929 if (is_variadic && !*is_parameter_pack)
14930 cp_parser_error (parser, "variadic constraint introduced without %<...%>");
14932 /* Build the parameter. Return an error if the declarator was invalid. */
14933 tree parm;
14934 if (TREE_CODE (proto) == TYPE_DECL)
14935 parm = cp_parser_constrained_type_template_parm (parser, id, parmdecl);
14936 else if (TREE_CODE (proto) == TEMPLATE_DECL)
14937 parm = cp_parser_constrained_template_template_parm (parser, proto, id,
14938 parmdecl);
14939 else
14940 parm = constrained_non_type_template_parm (is_non_type, parmdecl);
14941 if (parm == error_mark_node)
14942 return error_mark_node;
14944 /* Finish the parameter decl and create a node attaching the
14945 default argument and constraint. */
14946 parm = build_tree_list (def, parm);
14947 TEMPLATE_PARM_CONSTRAINTS (parm) = decl;
14949 return parm;
14952 /* Returns true if the parsed type actually represents the declaration
14953 of a type template-parameter. */
14955 static inline bool
14956 declares_constrained_type_template_parameter (tree type)
14958 return (is_constrained_parameter (type)
14959 && TREE_CODE (TREE_TYPE (type)) == TEMPLATE_TYPE_PARM);
14963 /* Returns true if the parsed type actually represents the declaration of
14964 a template template-parameter. */
14966 static bool
14967 declares_constrained_template_template_parameter (tree type)
14969 return (is_constrained_parameter (type)
14970 && TREE_CODE (TREE_TYPE (type)) == TEMPLATE_TEMPLATE_PARM);
14973 /* Parse a default argument for a type template-parameter.
14974 Note that diagnostics are handled in cp_parser_template_parameter. */
14976 static tree
14977 cp_parser_default_type_template_argument (cp_parser *parser)
14979 gcc_assert (cp_lexer_next_token_is (parser->lexer, CPP_EQ));
14981 /* Consume the `=' token. */
14982 cp_lexer_consume_token (parser->lexer);
14984 cp_token *token = cp_lexer_peek_token (parser->lexer);
14986 /* Parse the default-argument. */
14987 push_deferring_access_checks (dk_no_deferred);
14988 tree default_argument = cp_parser_type_id (parser);
14989 pop_deferring_access_checks ();
14991 if (flag_concepts && type_uses_auto (default_argument))
14993 error_at (token->location,
14994 "invalid use of %<auto%> in default template argument");
14995 return error_mark_node;
14998 return default_argument;
15001 /* Parse a default argument for a template template-parameter. */
15003 static tree
15004 cp_parser_default_template_template_argument (cp_parser *parser)
15006 gcc_assert (cp_lexer_next_token_is (parser->lexer, CPP_EQ));
15008 bool is_template;
15010 /* Consume the `='. */
15011 cp_lexer_consume_token (parser->lexer);
15012 /* Parse the id-expression. */
15013 push_deferring_access_checks (dk_no_deferred);
15014 /* save token before parsing the id-expression, for error
15015 reporting */
15016 const cp_token* token = cp_lexer_peek_token (parser->lexer);
15017 tree default_argument
15018 = cp_parser_id_expression (parser,
15019 /*template_keyword_p=*/false,
15020 /*check_dependency_p=*/true,
15021 /*template_p=*/&is_template,
15022 /*declarator_p=*/false,
15023 /*optional_p=*/false);
15024 if (TREE_CODE (default_argument) == TYPE_DECL)
15025 /* If the id-expression was a template-id that refers to
15026 a template-class, we already have the declaration here,
15027 so no further lookup is needed. */
15029 else
15030 /* Look up the name. */
15031 default_argument
15032 = cp_parser_lookup_name (parser, default_argument,
15033 none_type,
15034 /*is_template=*/is_template,
15035 /*is_namespace=*/false,
15036 /*check_dependency=*/true,
15037 /*ambiguous_decls=*/NULL,
15038 token->location);
15039 /* See if the default argument is valid. */
15040 default_argument = check_template_template_default_arg (default_argument);
15041 pop_deferring_access_checks ();
15042 return default_argument;
15045 /* Parse a template-parameter.
15047 template-parameter:
15048 type-parameter
15049 parameter-declaration
15051 If all goes well, returns a TREE_LIST. The TREE_VALUE represents
15052 the parameter. The TREE_PURPOSE is the default value, if any.
15053 Returns ERROR_MARK_NODE on failure. *IS_NON_TYPE is set to true
15054 iff this parameter is a non-type parameter. *IS_PARAMETER_PACK is
15055 set to true iff this parameter is a parameter pack. */
15057 static tree
15058 cp_parser_template_parameter (cp_parser* parser, bool *is_non_type,
15059 bool *is_parameter_pack)
15061 cp_token *token;
15062 cp_parameter_declarator *parameter_declarator;
15063 tree parm;
15065 /* Assume it is a type parameter or a template parameter. */
15066 *is_non_type = false;
15067 /* Assume it not a parameter pack. */
15068 *is_parameter_pack = false;
15069 /* Peek at the next token. */
15070 token = cp_lexer_peek_token (parser->lexer);
15071 /* If it is `class' or `template', we have a type-parameter. */
15072 if (token->keyword == RID_TEMPLATE)
15073 return cp_parser_type_parameter (parser, is_parameter_pack);
15074 /* If it is `class' or `typename' we do not know yet whether it is a
15075 type parameter or a non-type parameter. Consider:
15077 template <typename T, typename T::X X> ...
15081 template <class C, class D*> ...
15083 Here, the first parameter is a type parameter, and the second is
15084 a non-type parameter. We can tell by looking at the token after
15085 the identifier -- if it is a `,', `=', or `>' then we have a type
15086 parameter. */
15087 if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
15089 /* Peek at the token after `class' or `typename'. */
15090 token = cp_lexer_peek_nth_token (parser->lexer, 2);
15091 /* If it's an ellipsis, we have a template type parameter
15092 pack. */
15093 if (token->type == CPP_ELLIPSIS)
15094 return cp_parser_type_parameter (parser, is_parameter_pack);
15095 /* If it's an identifier, skip it. */
15096 if (token->type == CPP_NAME)
15097 token = cp_lexer_peek_nth_token (parser->lexer, 3);
15098 /* Now, see if the token looks like the end of a template
15099 parameter. */
15100 if (token->type == CPP_COMMA
15101 || token->type == CPP_EQ
15102 || token->type == CPP_GREATER)
15103 return cp_parser_type_parameter (parser, is_parameter_pack);
15106 /* Otherwise, it is a non-type parameter or a constrained parameter.
15108 [temp.param]
15110 When parsing a default template-argument for a non-type
15111 template-parameter, the first non-nested `>' is taken as the end
15112 of the template parameter-list rather than a greater-than
15113 operator. */
15114 parameter_declarator
15115 = cp_parser_parameter_declaration (parser, /*template_parm_p=*/true,
15116 /*parenthesized_p=*/NULL);
15118 if (!parameter_declarator)
15119 return error_mark_node;
15121 /* If the parameter declaration is marked as a parameter pack, set
15122 *IS_PARAMETER_PACK to notify the caller. */
15123 if (parameter_declarator->template_parameter_pack_p)
15124 *is_parameter_pack = true;
15126 if (parameter_declarator->default_argument)
15128 /* Can happen in some cases of erroneous input (c++/34892). */
15129 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
15130 /* Consume the `...' for better error recovery. */
15131 cp_lexer_consume_token (parser->lexer);
15134 // The parameter may have been constrained.
15135 if (is_constrained_parameter (parameter_declarator))
15136 return finish_constrained_parameter (parser,
15137 parameter_declarator,
15138 is_non_type,
15139 is_parameter_pack);
15141 // Now we're sure that the parameter is a non-type parameter.
15142 *is_non_type = true;
15144 parm = grokdeclarator (parameter_declarator->declarator,
15145 &parameter_declarator->decl_specifiers,
15146 TPARM, /*initialized=*/0,
15147 /*attrlist=*/NULL);
15148 if (parm == error_mark_node)
15149 return error_mark_node;
15151 return build_tree_list (parameter_declarator->default_argument, parm);
15154 /* Parse a type-parameter.
15156 type-parameter:
15157 class identifier [opt]
15158 class identifier [opt] = type-id
15159 typename identifier [opt]
15160 typename identifier [opt] = type-id
15161 template < template-parameter-list > class identifier [opt]
15162 template < template-parameter-list > class identifier [opt]
15163 = id-expression
15165 GNU Extension (variadic templates):
15167 type-parameter:
15168 class ... identifier [opt]
15169 typename ... identifier [opt]
15171 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
15172 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
15173 the declaration of the parameter.
15175 Sets *IS_PARAMETER_PACK if this is a template parameter pack. */
15177 static tree
15178 cp_parser_type_parameter (cp_parser* parser, bool *is_parameter_pack)
15180 cp_token *token;
15181 tree parameter;
15183 /* Look for a keyword to tell us what kind of parameter this is. */
15184 token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_TYPENAME_TEMPLATE);
15185 if (!token)
15186 return error_mark_node;
15188 switch (token->keyword)
15190 case RID_CLASS:
15191 case RID_TYPENAME:
15193 tree identifier;
15194 tree default_argument;
15196 /* If the next token is an ellipsis, we have a template
15197 argument pack. */
15198 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
15200 /* Consume the `...' token. */
15201 cp_lexer_consume_token (parser->lexer);
15202 maybe_warn_variadic_templates ();
15204 *is_parameter_pack = true;
15207 /* If the next token is an identifier, then it names the
15208 parameter. */
15209 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
15210 identifier = cp_parser_identifier (parser);
15211 else
15212 identifier = NULL_TREE;
15214 /* Create the parameter. */
15215 parameter = finish_template_type_parm (class_type_node, identifier);
15217 /* If the next token is an `=', we have a default argument. */
15218 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
15220 default_argument
15221 = cp_parser_default_type_template_argument (parser);
15223 /* Template parameter packs cannot have default
15224 arguments. */
15225 if (*is_parameter_pack)
15227 if (identifier)
15228 error_at (token->location,
15229 "template parameter pack %qD cannot have a "
15230 "default argument", identifier);
15231 else
15232 error_at (token->location,
15233 "template parameter packs cannot have "
15234 "default arguments");
15235 default_argument = NULL_TREE;
15237 else if (check_for_bare_parameter_packs (default_argument))
15238 default_argument = error_mark_node;
15240 else
15241 default_argument = NULL_TREE;
15243 /* Create the combined representation of the parameter and the
15244 default argument. */
15245 parameter = build_tree_list (default_argument, parameter);
15247 break;
15249 case RID_TEMPLATE:
15251 tree identifier;
15252 tree default_argument;
15254 /* Look for the `<'. */
15255 cp_parser_require (parser, CPP_LESS, RT_LESS);
15256 /* Parse the template-parameter-list. */
15257 cp_parser_template_parameter_list (parser);
15258 /* Look for the `>'. */
15259 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
15261 // If template requirements are present, parse them.
15262 if (flag_concepts)
15264 tree reqs = get_shorthand_constraints (current_template_parms);
15265 if (tree r = cp_parser_requires_clause_opt (parser))
15266 reqs = conjoin_constraints (reqs, normalize_expression (r));
15267 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) = reqs;
15270 /* Look for the `class' or 'typename' keywords. */
15271 cp_parser_type_parameter_key (parser);
15272 /* If the next token is an ellipsis, we have a template
15273 argument pack. */
15274 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
15276 /* Consume the `...' token. */
15277 cp_lexer_consume_token (parser->lexer);
15278 maybe_warn_variadic_templates ();
15280 *is_parameter_pack = true;
15282 /* If the next token is an `=', then there is a
15283 default-argument. If the next token is a `>', we are at
15284 the end of the parameter-list. If the next token is a `,',
15285 then we are at the end of this parameter. */
15286 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
15287 && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
15288 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
15290 identifier = cp_parser_identifier (parser);
15291 /* Treat invalid names as if the parameter were nameless. */
15292 if (identifier == error_mark_node)
15293 identifier = NULL_TREE;
15295 else
15296 identifier = NULL_TREE;
15298 /* Create the template parameter. */
15299 parameter = finish_template_template_parm (class_type_node,
15300 identifier);
15302 /* If the next token is an `=', then there is a
15303 default-argument. */
15304 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
15306 default_argument
15307 = cp_parser_default_template_template_argument (parser);
15309 /* Template parameter packs cannot have default
15310 arguments. */
15311 if (*is_parameter_pack)
15313 if (identifier)
15314 error_at (token->location,
15315 "template parameter pack %qD cannot "
15316 "have a default argument",
15317 identifier);
15318 else
15319 error_at (token->location, "template parameter packs cannot "
15320 "have default arguments");
15321 default_argument = NULL_TREE;
15324 else
15325 default_argument = NULL_TREE;
15327 /* Create the combined representation of the parameter and the
15328 default argument. */
15329 parameter = build_tree_list (default_argument, parameter);
15331 break;
15333 default:
15334 gcc_unreachable ();
15335 break;
15338 return parameter;
15341 /* Parse a template-id.
15343 template-id:
15344 template-name < template-argument-list [opt] >
15346 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
15347 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
15348 returned. Otherwise, if the template-name names a function, or set
15349 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
15350 names a class, returns a TYPE_DECL for the specialization.
15352 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
15353 uninstantiated templates. */
15355 static tree
15356 cp_parser_template_id (cp_parser *parser,
15357 bool template_keyword_p,
15358 bool check_dependency_p,
15359 enum tag_types tag_type,
15360 bool is_declaration)
15362 tree templ;
15363 tree arguments;
15364 tree template_id;
15365 cp_token_position start_of_id = 0;
15366 cp_token *next_token = NULL, *next_token_2 = NULL;
15367 bool is_identifier;
15369 /* If the next token corresponds to a template-id, there is no need
15370 to reparse it. */
15371 next_token = cp_lexer_peek_token (parser->lexer);
15372 if (next_token->type == CPP_TEMPLATE_ID)
15374 cp_lexer_consume_token (parser->lexer);
15375 return saved_checks_value (next_token->u.tree_check_value);
15378 /* Avoid performing name lookup if there is no possibility of
15379 finding a template-id. */
15380 if ((next_token->type != CPP_NAME && next_token->keyword != RID_OPERATOR)
15381 || (next_token->type == CPP_NAME
15382 && !cp_parser_nth_token_starts_template_argument_list_p
15383 (parser, 2)))
15385 cp_parser_error (parser, "expected template-id");
15386 return error_mark_node;
15389 /* Remember where the template-id starts. */
15390 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
15391 start_of_id = cp_lexer_token_position (parser->lexer, false);
15393 push_deferring_access_checks (dk_deferred);
15395 /* Parse the template-name. */
15396 is_identifier = false;
15397 templ = cp_parser_template_name (parser, template_keyword_p,
15398 check_dependency_p,
15399 is_declaration,
15400 tag_type,
15401 &is_identifier);
15402 if (templ == error_mark_node || is_identifier)
15404 pop_deferring_access_checks ();
15405 return templ;
15408 /* Since we're going to preserve any side-effects from this parse, set up a
15409 firewall to protect our callers from cp_parser_commit_to_tentative_parse
15410 in the template arguments. */
15411 tentative_firewall firewall (parser);
15413 /* If we find the sequence `[:' after a template-name, it's probably
15414 a digraph-typo for `< ::'. Substitute the tokens and check if we can
15415 parse correctly the argument list. */
15416 if (((next_token = cp_lexer_peek_token (parser->lexer))->type
15417 == CPP_OPEN_SQUARE)
15418 && next_token->flags & DIGRAPH
15419 && ((next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2))->type
15420 == CPP_COLON)
15421 && !(next_token_2->flags & PREV_WHITE))
15423 cp_parser_parse_tentatively (parser);
15424 /* Change `:' into `::'. */
15425 next_token_2->type = CPP_SCOPE;
15426 /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
15427 CPP_LESS. */
15428 cp_lexer_consume_token (parser->lexer);
15430 /* Parse the arguments. */
15431 arguments = cp_parser_enclosed_template_argument_list (parser);
15432 if (!cp_parser_parse_definitely (parser))
15434 /* If we couldn't parse an argument list, then we revert our changes
15435 and return simply an error. Maybe this is not a template-id
15436 after all. */
15437 next_token_2->type = CPP_COLON;
15438 cp_parser_error (parser, "expected %<<%>");
15439 pop_deferring_access_checks ();
15440 return error_mark_node;
15442 /* Otherwise, emit an error about the invalid digraph, but continue
15443 parsing because we got our argument list. */
15444 if (permerror (next_token->location,
15445 "%<<::%> cannot begin a template-argument list"))
15447 static bool hint = false;
15448 inform (next_token->location,
15449 "%<<:%> is an alternate spelling for %<[%>."
15450 " Insert whitespace between %<<%> and %<::%>");
15451 if (!hint && !flag_permissive)
15453 inform (next_token->location, "(if you use %<-fpermissive%> "
15454 "or %<-std=c++11%>, or %<-std=gnu++11%> G++ will "
15455 "accept your code)");
15456 hint = true;
15460 else
15462 /* Look for the `<' that starts the template-argument-list. */
15463 if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
15465 pop_deferring_access_checks ();
15466 return error_mark_node;
15468 /* Parse the arguments. */
15469 arguments = cp_parser_enclosed_template_argument_list (parser);
15472 /* Build a representation of the specialization. */
15473 if (identifier_p (templ))
15474 template_id = build_min_nt_loc (next_token->location,
15475 TEMPLATE_ID_EXPR,
15476 templ, arguments);
15477 else if (DECL_TYPE_TEMPLATE_P (templ)
15478 || DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
15480 bool entering_scope;
15481 /* In "template <typename T> ... A<T>::", A<T> is the abstract A
15482 template (rather than some instantiation thereof) only if
15483 is not nested within some other construct. For example, in
15484 "template <typename T> void f(T) { A<T>::", A<T> is just an
15485 instantiation of A. */
15486 entering_scope = (template_parm_scope_p ()
15487 && cp_lexer_next_token_is (parser->lexer,
15488 CPP_SCOPE));
15489 template_id
15490 = finish_template_type (templ, arguments, entering_scope);
15492 /* A template-like identifier may be a partial concept id. */
15493 else if (flag_concepts
15494 && (template_id = (cp_parser_maybe_partial_concept_id
15495 (parser, templ, arguments))))
15496 return template_id;
15497 else if (variable_template_p (templ))
15499 template_id = lookup_template_variable (templ, arguments);
15500 if (TREE_CODE (template_id) == TEMPLATE_ID_EXPR)
15501 SET_EXPR_LOCATION (template_id, next_token->location);
15503 else
15505 /* If it's not a class-template or a template-template, it should be
15506 a function-template. */
15507 gcc_assert ((DECL_FUNCTION_TEMPLATE_P (templ)
15508 || TREE_CODE (templ) == OVERLOAD
15509 || BASELINK_P (templ)));
15511 template_id = lookup_template_function (templ, arguments);
15512 if (TREE_CODE (template_id) == TEMPLATE_ID_EXPR)
15513 SET_EXPR_LOCATION (template_id, next_token->location);
15516 /* If parsing tentatively, replace the sequence of tokens that makes
15517 up the template-id with a CPP_TEMPLATE_ID token. That way,
15518 should we re-parse the token stream, we will not have to repeat
15519 the effort required to do the parse, nor will we issue duplicate
15520 error messages about problems during instantiation of the
15521 template. */
15522 if (start_of_id
15523 /* Don't do this if we had a parse error in a declarator; re-parsing
15524 might succeed if a name changes meaning (60361). */
15525 && !(cp_parser_error_occurred (parser)
15526 && cp_parser_parsing_tentatively (parser)
15527 && parser->in_declarator_p))
15529 cp_token *token = cp_lexer_token_at (parser->lexer, start_of_id);
15531 /* Reset the contents of the START_OF_ID token. */
15532 token->type = CPP_TEMPLATE_ID;
15534 /* Update the location to be of the form:
15535 template-name < template-argument-list [opt] >
15536 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
15537 with caret == start at the start of the template-name,
15538 ranging until the closing '>'. */
15539 location_t finish_loc
15540 = get_finish (cp_lexer_previous_token (parser->lexer)->location);
15541 location_t combined_loc
15542 = make_location (token->location, token->location, finish_loc);
15543 token->location = combined_loc;
15545 /* Retrieve any deferred checks. Do not pop this access checks yet
15546 so the memory will not be reclaimed during token replacing below. */
15547 token->u.tree_check_value = ggc_cleared_alloc<struct tree_check> ();
15548 token->u.tree_check_value->value = template_id;
15549 token->u.tree_check_value->checks = get_deferred_access_checks ();
15550 token->keyword = RID_MAX;
15552 /* Purge all subsequent tokens. */
15553 cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
15555 /* ??? Can we actually assume that, if template_id ==
15556 error_mark_node, we will have issued a diagnostic to the
15557 user, as opposed to simply marking the tentative parse as
15558 failed? */
15559 if (cp_parser_error_occurred (parser) && template_id != error_mark_node)
15560 error_at (token->location, "parse error in template argument list");
15563 pop_to_parent_deferring_access_checks ();
15564 return template_id;
15567 /* Parse a template-name.
15569 template-name:
15570 identifier
15572 The standard should actually say:
15574 template-name:
15575 identifier
15576 operator-function-id
15578 A defect report has been filed about this issue.
15580 A conversion-function-id cannot be a template name because they cannot
15581 be part of a template-id. In fact, looking at this code:
15583 a.operator K<int>()
15585 the conversion-function-id is "operator K<int>", and K<int> is a type-id.
15586 It is impossible to call a templated conversion-function-id with an
15587 explicit argument list, since the only allowed template parameter is
15588 the type to which it is converting.
15590 If TEMPLATE_KEYWORD_P is true, then we have just seen the
15591 `template' keyword, in a construction like:
15593 T::template f<3>()
15595 In that case `f' is taken to be a template-name, even though there
15596 is no way of knowing for sure.
15598 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
15599 name refers to a set of overloaded functions, at least one of which
15600 is a template, or an IDENTIFIER_NODE with the name of the template,
15601 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
15602 names are looked up inside uninstantiated templates. */
15604 static tree
15605 cp_parser_template_name (cp_parser* parser,
15606 bool template_keyword_p,
15607 bool check_dependency_p,
15608 bool is_declaration,
15609 enum tag_types tag_type,
15610 bool *is_identifier)
15612 tree identifier;
15613 tree decl;
15614 tree fns;
15615 cp_token *token = cp_lexer_peek_token (parser->lexer);
15617 /* If the next token is `operator', then we have either an
15618 operator-function-id or a conversion-function-id. */
15619 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
15621 /* We don't know whether we're looking at an
15622 operator-function-id or a conversion-function-id. */
15623 cp_parser_parse_tentatively (parser);
15624 /* Try an operator-function-id. */
15625 identifier = cp_parser_operator_function_id (parser);
15626 /* If that didn't work, try a conversion-function-id. */
15627 if (!cp_parser_parse_definitely (parser))
15629 cp_parser_error (parser, "expected template-name");
15630 return error_mark_node;
15633 /* Look for the identifier. */
15634 else
15635 identifier = cp_parser_identifier (parser);
15637 /* If we didn't find an identifier, we don't have a template-id. */
15638 if (identifier == error_mark_node)
15639 return error_mark_node;
15641 /* If the name immediately followed the `template' keyword, then it
15642 is a template-name. However, if the next token is not `<', then
15643 we do not treat it as a template-name, since it is not being used
15644 as part of a template-id. This enables us to handle constructs
15645 like:
15647 template <typename T> struct S { S(); };
15648 template <typename T> S<T>::S();
15650 correctly. We would treat `S' as a template -- if it were `S<T>'
15651 -- but we do not if there is no `<'. */
15653 if (processing_template_decl
15654 && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
15656 /* In a declaration, in a dependent context, we pretend that the
15657 "template" keyword was present in order to improve error
15658 recovery. For example, given:
15660 template <typename T> void f(T::X<int>);
15662 we want to treat "X<int>" as a template-id. */
15663 if (is_declaration
15664 && !template_keyword_p
15665 && parser->scope && TYPE_P (parser->scope)
15666 && check_dependency_p
15667 && dependent_scope_p (parser->scope)
15668 /* Do not do this for dtors (or ctors), since they never
15669 need the template keyword before their name. */
15670 && !constructor_name_p (identifier, parser->scope))
15672 cp_token_position start = 0;
15674 /* Explain what went wrong. */
15675 error_at (token->location, "non-template %qD used as template",
15676 identifier);
15677 inform (token->location, "use %<%T::template %D%> to indicate that it is a template",
15678 parser->scope, identifier);
15679 /* If parsing tentatively, find the location of the "<" token. */
15680 if (cp_parser_simulate_error (parser))
15681 start = cp_lexer_token_position (parser->lexer, true);
15682 /* Parse the template arguments so that we can issue error
15683 messages about them. */
15684 cp_lexer_consume_token (parser->lexer);
15685 cp_parser_enclosed_template_argument_list (parser);
15686 /* Skip tokens until we find a good place from which to
15687 continue parsing. */
15688 cp_parser_skip_to_closing_parenthesis (parser,
15689 /*recovering=*/true,
15690 /*or_comma=*/true,
15691 /*consume_paren=*/false);
15692 /* If parsing tentatively, permanently remove the
15693 template argument list. That will prevent duplicate
15694 error messages from being issued about the missing
15695 "template" keyword. */
15696 if (start)
15697 cp_lexer_purge_tokens_after (parser->lexer, start);
15698 if (is_identifier)
15699 *is_identifier = true;
15700 parser->context->object_type = NULL_TREE;
15701 return identifier;
15704 /* If the "template" keyword is present, then there is generally
15705 no point in doing name-lookup, so we just return IDENTIFIER.
15706 But, if the qualifying scope is non-dependent then we can
15707 (and must) do name-lookup normally. */
15708 if (template_keyword_p
15709 && (!parser->scope
15710 || (TYPE_P (parser->scope)
15711 && dependent_type_p (parser->scope))))
15713 /* We're optimizing away the call to cp_parser_lookup_name, but we
15714 still need to do this. */
15715 parser->context->object_type = NULL_TREE;
15716 return identifier;
15720 /* Look up the name. */
15721 decl = cp_parser_lookup_name (parser, identifier,
15722 tag_type,
15723 /*is_template=*/true,
15724 /*is_namespace=*/false,
15725 check_dependency_p,
15726 /*ambiguous_decls=*/NULL,
15727 token->location);
15729 decl = strip_using_decl (decl);
15731 /* If DECL is a template, then the name was a template-name. */
15732 if (TREE_CODE (decl) == TEMPLATE_DECL)
15734 if (TREE_DEPRECATED (decl)
15735 && deprecated_state != DEPRECATED_SUPPRESS)
15736 warn_deprecated_use (decl, NULL_TREE);
15738 else
15740 tree fn = NULL_TREE;
15742 /* The standard does not explicitly indicate whether a name that
15743 names a set of overloaded declarations, some of which are
15744 templates, is a template-name. However, such a name should
15745 be a template-name; otherwise, there is no way to form a
15746 template-id for the overloaded templates. */
15747 fns = BASELINK_P (decl) ? BASELINK_FUNCTIONS (decl) : decl;
15748 if (TREE_CODE (fns) == OVERLOAD)
15749 for (fn = fns; fn; fn = OVL_NEXT (fn))
15750 if (TREE_CODE (OVL_CURRENT (fn)) == TEMPLATE_DECL)
15751 break;
15753 if (!fn)
15755 /* The name does not name a template. */
15756 cp_parser_error (parser, "expected template-name");
15757 return error_mark_node;
15761 /* If DECL is dependent, and refers to a function, then just return
15762 its name; we will look it up again during template instantiation. */
15763 if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
15765 tree scope = ovl_scope (decl);
15766 if (TYPE_P (scope) && dependent_type_p (scope))
15767 return identifier;
15770 return decl;
15773 /* Parse a template-argument-list.
15775 template-argument-list:
15776 template-argument ... [opt]
15777 template-argument-list , template-argument ... [opt]
15779 Returns a TREE_VEC containing the arguments. */
15781 static tree
15782 cp_parser_template_argument_list (cp_parser* parser)
15784 tree fixed_args[10];
15785 unsigned n_args = 0;
15786 unsigned alloced = 10;
15787 tree *arg_ary = fixed_args;
15788 tree vec;
15789 bool saved_in_template_argument_list_p;
15790 bool saved_ice_p;
15791 bool saved_non_ice_p;
15793 saved_in_template_argument_list_p = parser->in_template_argument_list_p;
15794 parser->in_template_argument_list_p = true;
15795 /* Even if the template-id appears in an integral
15796 constant-expression, the contents of the argument list do
15797 not. */
15798 saved_ice_p = parser->integral_constant_expression_p;
15799 parser->integral_constant_expression_p = false;
15800 saved_non_ice_p = parser->non_integral_constant_expression_p;
15801 parser->non_integral_constant_expression_p = false;
15803 /* Parse the arguments. */
15806 tree argument;
15808 if (n_args)
15809 /* Consume the comma. */
15810 cp_lexer_consume_token (parser->lexer);
15812 /* Parse the template-argument. */
15813 argument = cp_parser_template_argument (parser);
15815 /* If the next token is an ellipsis, we're expanding a template
15816 argument pack. */
15817 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
15819 if (argument == error_mark_node)
15821 cp_token *token = cp_lexer_peek_token (parser->lexer);
15822 error_at (token->location,
15823 "expected parameter pack before %<...%>");
15825 /* Consume the `...' token. */
15826 cp_lexer_consume_token (parser->lexer);
15828 /* Make the argument into a TYPE_PACK_EXPANSION or
15829 EXPR_PACK_EXPANSION. */
15830 argument = make_pack_expansion (argument);
15833 if (n_args == alloced)
15835 alloced *= 2;
15837 if (arg_ary == fixed_args)
15839 arg_ary = XNEWVEC (tree, alloced);
15840 memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
15842 else
15843 arg_ary = XRESIZEVEC (tree, arg_ary, alloced);
15845 arg_ary[n_args++] = argument;
15847 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
15849 vec = make_tree_vec (n_args);
15851 while (n_args--)
15852 TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
15854 if (arg_ary != fixed_args)
15855 free (arg_ary);
15856 parser->non_integral_constant_expression_p = saved_non_ice_p;
15857 parser->integral_constant_expression_p = saved_ice_p;
15858 parser->in_template_argument_list_p = saved_in_template_argument_list_p;
15859 if (CHECKING_P)
15860 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec, TREE_VEC_LENGTH (vec));
15861 return vec;
15864 /* Parse a template-argument.
15866 template-argument:
15867 assignment-expression
15868 type-id
15869 id-expression
15871 The representation is that of an assignment-expression, type-id, or
15872 id-expression -- except that the qualified id-expression is
15873 evaluated, so that the value returned is either a DECL or an
15874 OVERLOAD.
15876 Although the standard says "assignment-expression", it forbids
15877 throw-expressions or assignments in the template argument.
15878 Therefore, we use "conditional-expression" instead. */
15880 static tree
15881 cp_parser_template_argument (cp_parser* parser)
15883 tree argument;
15884 bool template_p;
15885 bool address_p;
15886 bool maybe_type_id = false;
15887 cp_token *token = NULL, *argument_start_token = NULL;
15888 location_t loc = 0;
15889 cp_id_kind idk;
15891 /* There's really no way to know what we're looking at, so we just
15892 try each alternative in order.
15894 [temp.arg]
15896 In a template-argument, an ambiguity between a type-id and an
15897 expression is resolved to a type-id, regardless of the form of
15898 the corresponding template-parameter.
15900 Therefore, we try a type-id first. */
15901 cp_parser_parse_tentatively (parser);
15902 argument = cp_parser_template_type_arg (parser);
15903 /* If there was no error parsing the type-id but the next token is a
15904 '>>', our behavior depends on which dialect of C++ we're
15905 parsing. In C++98, we probably found a typo for '> >'. But there
15906 are type-id which are also valid expressions. For instance:
15908 struct X { int operator >> (int); };
15909 template <int V> struct Foo {};
15910 Foo<X () >> 5> r;
15912 Here 'X()' is a valid type-id of a function type, but the user just
15913 wanted to write the expression "X() >> 5". Thus, we remember that we
15914 found a valid type-id, but we still try to parse the argument as an
15915 expression to see what happens.
15917 In C++0x, the '>>' will be considered two separate '>'
15918 tokens. */
15919 if (!cp_parser_error_occurred (parser)
15920 && cxx_dialect == cxx98
15921 && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
15923 maybe_type_id = true;
15924 cp_parser_abort_tentative_parse (parser);
15926 else
15928 /* If the next token isn't a `,' or a `>', then this argument wasn't
15929 really finished. This means that the argument is not a valid
15930 type-id. */
15931 if (!cp_parser_next_token_ends_template_argument_p (parser))
15932 cp_parser_error (parser, "expected template-argument");
15933 /* If that worked, we're done. */
15934 if (cp_parser_parse_definitely (parser))
15935 return argument;
15937 /* We're still not sure what the argument will be. */
15938 cp_parser_parse_tentatively (parser);
15939 /* Try a template. */
15940 argument_start_token = cp_lexer_peek_token (parser->lexer);
15941 argument = cp_parser_id_expression (parser,
15942 /*template_keyword_p=*/false,
15943 /*check_dependency_p=*/true,
15944 &template_p,
15945 /*declarator_p=*/false,
15946 /*optional_p=*/false);
15947 /* If the next token isn't a `,' or a `>', then this argument wasn't
15948 really finished. */
15949 if (!cp_parser_next_token_ends_template_argument_p (parser))
15950 cp_parser_error (parser, "expected template-argument");
15951 if (!cp_parser_error_occurred (parser))
15953 /* Figure out what is being referred to. If the id-expression
15954 was for a class template specialization, then we will have a
15955 TYPE_DECL at this point. There is no need to do name lookup
15956 at this point in that case. */
15957 if (TREE_CODE (argument) != TYPE_DECL)
15958 argument = cp_parser_lookup_name (parser, argument,
15959 none_type,
15960 /*is_template=*/template_p,
15961 /*is_namespace=*/false,
15962 /*check_dependency=*/true,
15963 /*ambiguous_decls=*/NULL,
15964 argument_start_token->location);
15965 /* Handle a constrained-type-specifier for a non-type template
15966 parameter. */
15967 if (tree decl = cp_parser_maybe_concept_name (parser, argument))
15968 argument = decl;
15969 else if (TREE_CODE (argument) != TEMPLATE_DECL
15970 && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
15971 cp_parser_error (parser, "expected template-name");
15973 if (cp_parser_parse_definitely (parser))
15975 if (TREE_DEPRECATED (argument))
15976 warn_deprecated_use (argument, NULL_TREE);
15977 return argument;
15979 /* It must be a non-type argument. In C++17 any constant-expression is
15980 allowed. */
15981 if (cxx_dialect > cxx14)
15982 goto general_expr;
15984 /* Otherwise, the permitted cases are given in [temp.arg.nontype]:
15986 -- an integral constant-expression of integral or enumeration
15987 type; or
15989 -- the name of a non-type template-parameter; or
15991 -- the name of an object or function with external linkage...
15993 -- the address of an object or function with external linkage...
15995 -- a pointer to member... */
15996 /* Look for a non-type template parameter. */
15997 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
15999 cp_parser_parse_tentatively (parser);
16000 argument = cp_parser_primary_expression (parser,
16001 /*address_p=*/false,
16002 /*cast_p=*/false,
16003 /*template_arg_p=*/true,
16004 &idk);
16005 if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
16006 || !cp_parser_next_token_ends_template_argument_p (parser))
16007 cp_parser_simulate_error (parser);
16008 if (cp_parser_parse_definitely (parser))
16009 return argument;
16012 /* If the next token is "&", the argument must be the address of an
16013 object or function with external linkage. */
16014 address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
16015 if (address_p)
16017 loc = cp_lexer_peek_token (parser->lexer)->location;
16018 cp_lexer_consume_token (parser->lexer);
16020 /* See if we might have an id-expression. */
16021 token = cp_lexer_peek_token (parser->lexer);
16022 if (token->type == CPP_NAME
16023 || token->keyword == RID_OPERATOR
16024 || token->type == CPP_SCOPE
16025 || token->type == CPP_TEMPLATE_ID
16026 || token->type == CPP_NESTED_NAME_SPECIFIER)
16028 cp_parser_parse_tentatively (parser);
16029 argument = cp_parser_primary_expression (parser,
16030 address_p,
16031 /*cast_p=*/false,
16032 /*template_arg_p=*/true,
16033 &idk);
16034 if (cp_parser_error_occurred (parser)
16035 || !cp_parser_next_token_ends_template_argument_p (parser))
16036 cp_parser_abort_tentative_parse (parser);
16037 else
16039 tree probe;
16041 if (INDIRECT_REF_P (argument))
16043 /* Strip the dereference temporarily. */
16044 gcc_assert (REFERENCE_REF_P (argument));
16045 argument = TREE_OPERAND (argument, 0);
16048 /* If we're in a template, we represent a qualified-id referring
16049 to a static data member as a SCOPE_REF even if the scope isn't
16050 dependent so that we can check access control later. */
16051 probe = argument;
16052 if (TREE_CODE (probe) == SCOPE_REF)
16053 probe = TREE_OPERAND (probe, 1);
16054 if (VAR_P (probe))
16056 /* A variable without external linkage might still be a
16057 valid constant-expression, so no error is issued here
16058 if the external-linkage check fails. */
16059 if (!address_p && !DECL_EXTERNAL_LINKAGE_P (probe))
16060 cp_parser_simulate_error (parser);
16062 else if (is_overloaded_fn (argument))
16063 /* All overloaded functions are allowed; if the external
16064 linkage test does not pass, an error will be issued
16065 later. */
16067 else if (address_p
16068 && (TREE_CODE (argument) == OFFSET_REF
16069 || TREE_CODE (argument) == SCOPE_REF))
16070 /* A pointer-to-member. */
16072 else if (TREE_CODE (argument) == TEMPLATE_PARM_INDEX)
16074 else
16075 cp_parser_simulate_error (parser);
16077 if (cp_parser_parse_definitely (parser))
16079 if (address_p)
16080 argument = build_x_unary_op (loc, ADDR_EXPR, argument,
16081 tf_warning_or_error);
16082 else
16083 argument = convert_from_reference (argument);
16084 return argument;
16088 /* If the argument started with "&", there are no other valid
16089 alternatives at this point. */
16090 if (address_p)
16092 cp_parser_error (parser, "invalid non-type template argument");
16093 return error_mark_node;
16096 general_expr:
16097 /* If the argument wasn't successfully parsed as a type-id followed
16098 by '>>', the argument can only be a constant expression now.
16099 Otherwise, we try parsing the constant-expression tentatively,
16100 because the argument could really be a type-id. */
16101 if (maybe_type_id)
16102 cp_parser_parse_tentatively (parser);
16104 if (cxx_dialect <= cxx14)
16105 argument = cp_parser_constant_expression (parser);
16106 else
16108 /* With C++17 generalized non-type template arguments we need to handle
16109 lvalue constant expressions, too. */
16110 argument = cp_parser_assignment_expression (parser);
16111 require_potential_constant_expression (argument);
16114 if (!maybe_type_id)
16115 return argument;
16116 if (!cp_parser_next_token_ends_template_argument_p (parser))
16117 cp_parser_error (parser, "expected template-argument");
16118 if (cp_parser_parse_definitely (parser))
16119 return argument;
16120 /* We did our best to parse the argument as a non type-id, but that
16121 was the only alternative that matched (albeit with a '>' after
16122 it). We can assume it's just a typo from the user, and a
16123 diagnostic will then be issued. */
16124 return cp_parser_template_type_arg (parser);
16127 /* Parse an explicit-instantiation.
16129 explicit-instantiation:
16130 template declaration
16132 Although the standard says `declaration', what it really means is:
16134 explicit-instantiation:
16135 template decl-specifier-seq [opt] declarator [opt] ;
16137 Things like `template int S<int>::i = 5, int S<double>::j;' are not
16138 supposed to be allowed. A defect report has been filed about this
16139 issue.
16141 GNU Extension:
16143 explicit-instantiation:
16144 storage-class-specifier template
16145 decl-specifier-seq [opt] declarator [opt] ;
16146 function-specifier template
16147 decl-specifier-seq [opt] declarator [opt] ; */
16149 static void
16150 cp_parser_explicit_instantiation (cp_parser* parser)
16152 int declares_class_or_enum;
16153 cp_decl_specifier_seq decl_specifiers;
16154 tree extension_specifier = NULL_TREE;
16156 timevar_push (TV_TEMPLATE_INST);
16158 /* Look for an (optional) storage-class-specifier or
16159 function-specifier. */
16160 if (cp_parser_allow_gnu_extensions_p (parser))
16162 extension_specifier
16163 = cp_parser_storage_class_specifier_opt (parser);
16164 if (!extension_specifier)
16165 extension_specifier
16166 = cp_parser_function_specifier_opt (parser,
16167 /*decl_specs=*/NULL);
16170 /* Look for the `template' keyword. */
16171 cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
16172 /* Let the front end know that we are processing an explicit
16173 instantiation. */
16174 begin_explicit_instantiation ();
16175 /* [temp.explicit] says that we are supposed to ignore access
16176 control while processing explicit instantiation directives. */
16177 push_deferring_access_checks (dk_no_check);
16178 /* Parse a decl-specifier-seq. */
16179 cp_parser_decl_specifier_seq (parser,
16180 CP_PARSER_FLAGS_OPTIONAL,
16181 &decl_specifiers,
16182 &declares_class_or_enum);
16183 /* If there was exactly one decl-specifier, and it declared a class,
16184 and there's no declarator, then we have an explicit type
16185 instantiation. */
16186 if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
16188 tree type;
16190 type = check_tag_decl (&decl_specifiers,
16191 /*explicit_type_instantiation_p=*/true);
16192 /* Turn access control back on for names used during
16193 template instantiation. */
16194 pop_deferring_access_checks ();
16195 if (type)
16196 do_type_instantiation (type, extension_specifier,
16197 /*complain=*/tf_error);
16199 else
16201 cp_declarator *declarator;
16202 tree decl;
16204 /* Parse the declarator. */
16205 declarator
16206 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
16207 /*ctor_dtor_or_conv_p=*/NULL,
16208 /*parenthesized_p=*/NULL,
16209 /*member_p=*/false,
16210 /*friend_p=*/false);
16211 if (declares_class_or_enum & 2)
16212 cp_parser_check_for_definition_in_return_type (declarator,
16213 decl_specifiers.type,
16214 decl_specifiers.locations[ds_type_spec]);
16215 if (declarator != cp_error_declarator)
16217 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_inline))
16218 permerror (decl_specifiers.locations[ds_inline],
16219 "explicit instantiation shall not use"
16220 " %<inline%> specifier");
16221 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_constexpr))
16222 permerror (decl_specifiers.locations[ds_constexpr],
16223 "explicit instantiation shall not use"
16224 " %<constexpr%> specifier");
16226 decl = grokdeclarator (declarator, &decl_specifiers,
16227 NORMAL, 0, &decl_specifiers.attributes);
16228 /* Turn access control back on for names used during
16229 template instantiation. */
16230 pop_deferring_access_checks ();
16231 /* Do the explicit instantiation. */
16232 do_decl_instantiation (decl, extension_specifier);
16234 else
16236 pop_deferring_access_checks ();
16237 /* Skip the body of the explicit instantiation. */
16238 cp_parser_skip_to_end_of_statement (parser);
16241 /* We're done with the instantiation. */
16242 end_explicit_instantiation ();
16244 cp_parser_consume_semicolon_at_end_of_statement (parser);
16246 timevar_pop (TV_TEMPLATE_INST);
16249 /* Parse an explicit-specialization.
16251 explicit-specialization:
16252 template < > declaration
16254 Although the standard says `declaration', what it really means is:
16256 explicit-specialization:
16257 template <> decl-specifier [opt] init-declarator [opt] ;
16258 template <> function-definition
16259 template <> explicit-specialization
16260 template <> template-declaration */
16262 static void
16263 cp_parser_explicit_specialization (cp_parser* parser)
16265 bool need_lang_pop;
16266 cp_token *token = cp_lexer_peek_token (parser->lexer);
16268 /* Look for the `template' keyword. */
16269 cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
16270 /* Look for the `<'. */
16271 cp_parser_require (parser, CPP_LESS, RT_LESS);
16272 /* Look for the `>'. */
16273 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
16274 /* We have processed another parameter list. */
16275 ++parser->num_template_parameter_lists;
16276 /* [temp]
16278 A template ... explicit specialization ... shall not have C
16279 linkage. */
16280 if (current_lang_name == lang_name_c)
16282 error_at (token->location, "template specialization with C linkage");
16283 /* Give it C++ linkage to avoid confusing other parts of the
16284 front end. */
16285 push_lang_context (lang_name_cplusplus);
16286 need_lang_pop = true;
16288 else
16289 need_lang_pop = false;
16290 /* Let the front end know that we are beginning a specialization. */
16291 if (!begin_specialization ())
16293 end_specialization ();
16294 return;
16297 /* If the next keyword is `template', we need to figure out whether
16298 or not we're looking a template-declaration. */
16299 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
16301 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
16302 && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
16303 cp_parser_template_declaration_after_export (parser,
16304 /*member_p=*/false);
16305 else
16306 cp_parser_explicit_specialization (parser);
16308 else
16309 /* Parse the dependent declaration. */
16310 cp_parser_single_declaration (parser,
16311 /*checks=*/NULL,
16312 /*member_p=*/false,
16313 /*explicit_specialization_p=*/true,
16314 /*friend_p=*/NULL);
16315 /* We're done with the specialization. */
16316 end_specialization ();
16317 /* For the erroneous case of a template with C linkage, we pushed an
16318 implicit C++ linkage scope; exit that scope now. */
16319 if (need_lang_pop)
16320 pop_lang_context ();
16321 /* We're done with this parameter list. */
16322 --parser->num_template_parameter_lists;
16325 /* Parse a type-specifier.
16327 type-specifier:
16328 simple-type-specifier
16329 class-specifier
16330 enum-specifier
16331 elaborated-type-specifier
16332 cv-qualifier
16334 GNU Extension:
16336 type-specifier:
16337 __complex__
16339 Returns a representation of the type-specifier. For a
16340 class-specifier, enum-specifier, or elaborated-type-specifier, a
16341 TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
16343 The parser flags FLAGS is used to control type-specifier parsing.
16345 If IS_DECLARATION is TRUE, then this type-specifier is appearing
16346 in a decl-specifier-seq.
16348 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
16349 class-specifier, enum-specifier, or elaborated-type-specifier, then
16350 *DECLARES_CLASS_OR_ENUM is set to a nonzero value. The value is 1
16351 if a type is declared; 2 if it is defined. Otherwise, it is set to
16352 zero.
16354 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
16355 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
16356 is set to FALSE. */
16358 static tree
16359 cp_parser_type_specifier (cp_parser* parser,
16360 cp_parser_flags flags,
16361 cp_decl_specifier_seq *decl_specs,
16362 bool is_declaration,
16363 int* declares_class_or_enum,
16364 bool* is_cv_qualifier)
16366 tree type_spec = NULL_TREE;
16367 cp_token *token;
16368 enum rid keyword;
16369 cp_decl_spec ds = ds_last;
16371 /* Assume this type-specifier does not declare a new type. */
16372 if (declares_class_or_enum)
16373 *declares_class_or_enum = 0;
16374 /* And that it does not specify a cv-qualifier. */
16375 if (is_cv_qualifier)
16376 *is_cv_qualifier = false;
16377 /* Peek at the next token. */
16378 token = cp_lexer_peek_token (parser->lexer);
16380 /* If we're looking at a keyword, we can use that to guide the
16381 production we choose. */
16382 keyword = token->keyword;
16383 switch (keyword)
16385 case RID_ENUM:
16386 if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
16387 goto elaborated_type_specifier;
16389 /* Look for the enum-specifier. */
16390 type_spec = cp_parser_enum_specifier (parser);
16391 /* If that worked, we're done. */
16392 if (type_spec)
16394 if (declares_class_or_enum)
16395 *declares_class_or_enum = 2;
16396 if (decl_specs)
16397 cp_parser_set_decl_spec_type (decl_specs,
16398 type_spec,
16399 token,
16400 /*type_definition_p=*/true);
16401 return type_spec;
16403 else
16404 goto elaborated_type_specifier;
16406 /* Any of these indicate either a class-specifier, or an
16407 elaborated-type-specifier. */
16408 case RID_CLASS:
16409 case RID_STRUCT:
16410 case RID_UNION:
16411 if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
16412 goto elaborated_type_specifier;
16414 /* Parse tentatively so that we can back up if we don't find a
16415 class-specifier. */
16416 cp_parser_parse_tentatively (parser);
16417 /* Look for the class-specifier. */
16418 type_spec = cp_parser_class_specifier (parser);
16419 invoke_plugin_callbacks (PLUGIN_FINISH_TYPE, type_spec);
16420 /* If that worked, we're done. */
16421 if (cp_parser_parse_definitely (parser))
16423 if (declares_class_or_enum)
16424 *declares_class_or_enum = 2;
16425 if (decl_specs)
16426 cp_parser_set_decl_spec_type (decl_specs,
16427 type_spec,
16428 token,
16429 /*type_definition_p=*/true);
16430 return type_spec;
16433 /* Fall through. */
16434 elaborated_type_specifier:
16435 /* We're declaring (not defining) a class or enum. */
16436 if (declares_class_or_enum)
16437 *declares_class_or_enum = 1;
16439 /* Fall through. */
16440 case RID_TYPENAME:
16441 /* Look for an elaborated-type-specifier. */
16442 type_spec
16443 = (cp_parser_elaborated_type_specifier
16444 (parser,
16445 decl_spec_seq_has_spec_p (decl_specs, ds_friend),
16446 is_declaration));
16447 if (decl_specs)
16448 cp_parser_set_decl_spec_type (decl_specs,
16449 type_spec,
16450 token,
16451 /*type_definition_p=*/false);
16452 return type_spec;
16454 case RID_CONST:
16455 ds = ds_const;
16456 if (is_cv_qualifier)
16457 *is_cv_qualifier = true;
16458 break;
16460 case RID_VOLATILE:
16461 ds = ds_volatile;
16462 if (is_cv_qualifier)
16463 *is_cv_qualifier = true;
16464 break;
16466 case RID_RESTRICT:
16467 ds = ds_restrict;
16468 if (is_cv_qualifier)
16469 *is_cv_qualifier = true;
16470 break;
16472 case RID_COMPLEX:
16473 /* The `__complex__' keyword is a GNU extension. */
16474 ds = ds_complex;
16475 break;
16477 default:
16478 break;
16481 /* Handle simple keywords. */
16482 if (ds != ds_last)
16484 if (decl_specs)
16486 set_and_check_decl_spec_loc (decl_specs, ds, token);
16487 decl_specs->any_specifiers_p = true;
16489 return cp_lexer_consume_token (parser->lexer)->u.value;
16492 /* If we do not already have a type-specifier, assume we are looking
16493 at a simple-type-specifier. */
16494 type_spec = cp_parser_simple_type_specifier (parser,
16495 decl_specs,
16496 flags);
16498 /* If we didn't find a type-specifier, and a type-specifier was not
16499 optional in this context, issue an error message. */
16500 if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
16502 cp_parser_error (parser, "expected type specifier");
16503 return error_mark_node;
16506 return type_spec;
16509 /* Parse a simple-type-specifier.
16511 simple-type-specifier:
16512 :: [opt] nested-name-specifier [opt] type-name
16513 :: [opt] nested-name-specifier template template-id
16514 char
16515 wchar_t
16516 bool
16517 short
16519 long
16520 signed
16521 unsigned
16522 float
16523 double
16524 void
16526 C++11 Extension:
16528 simple-type-specifier:
16529 auto
16530 decltype ( expression )
16531 char16_t
16532 char32_t
16533 __underlying_type ( type-id )
16535 C++17 extension:
16537 nested-name-specifier(opt) template-name
16539 GNU Extension:
16541 simple-type-specifier:
16542 __int128
16543 __typeof__ unary-expression
16544 __typeof__ ( type-id )
16545 __typeof__ ( type-id ) { initializer-list , [opt] }
16547 Concepts Extension:
16549 simple-type-specifier:
16550 constrained-type-specifier
16552 Returns the indicated TYPE_DECL. If DECL_SPECS is not NULL, it is
16553 appropriately updated. */
16555 static tree
16556 cp_parser_simple_type_specifier (cp_parser* parser,
16557 cp_decl_specifier_seq *decl_specs,
16558 cp_parser_flags flags)
16560 tree type = NULL_TREE;
16561 cp_token *token;
16562 int idx;
16564 /* Peek at the next token. */
16565 token = cp_lexer_peek_token (parser->lexer);
16567 /* If we're looking at a keyword, things are easy. */
16568 switch (token->keyword)
16570 case RID_CHAR:
16571 if (decl_specs)
16572 decl_specs->explicit_char_p = true;
16573 type = char_type_node;
16574 break;
16575 case RID_CHAR16:
16576 type = char16_type_node;
16577 break;
16578 case RID_CHAR32:
16579 type = char32_type_node;
16580 break;
16581 case RID_WCHAR:
16582 type = wchar_type_node;
16583 break;
16584 case RID_BOOL:
16585 type = boolean_type_node;
16586 break;
16587 case RID_SHORT:
16588 set_and_check_decl_spec_loc (decl_specs, ds_short, token);
16589 type = short_integer_type_node;
16590 break;
16591 case RID_INT:
16592 if (decl_specs)
16593 decl_specs->explicit_int_p = true;
16594 type = integer_type_node;
16595 break;
16596 case RID_INT_N_0:
16597 case RID_INT_N_1:
16598 case RID_INT_N_2:
16599 case RID_INT_N_3:
16600 idx = token->keyword - RID_INT_N_0;
16601 if (! int_n_enabled_p [idx])
16602 break;
16603 if (decl_specs)
16605 decl_specs->explicit_intN_p = true;
16606 decl_specs->int_n_idx = idx;
16608 type = int_n_trees [idx].signed_type;
16609 break;
16610 case RID_LONG:
16611 if (decl_specs)
16612 set_and_check_decl_spec_loc (decl_specs, ds_long, token);
16613 type = long_integer_type_node;
16614 break;
16615 case RID_SIGNED:
16616 set_and_check_decl_spec_loc (decl_specs, ds_signed, token);
16617 type = integer_type_node;
16618 break;
16619 case RID_UNSIGNED:
16620 set_and_check_decl_spec_loc (decl_specs, ds_unsigned, token);
16621 type = unsigned_type_node;
16622 break;
16623 case RID_FLOAT:
16624 type = float_type_node;
16625 break;
16626 case RID_DOUBLE:
16627 type = double_type_node;
16628 break;
16629 case RID_VOID:
16630 type = void_type_node;
16631 break;
16633 case RID_AUTO:
16634 maybe_warn_cpp0x (CPP0X_AUTO);
16635 if (parser->auto_is_implicit_function_template_parm_p)
16637 /* The 'auto' might be the placeholder return type for a function decl
16638 with trailing return type. */
16639 bool have_trailing_return_fn_decl = false;
16641 cp_parser_parse_tentatively (parser);
16642 cp_lexer_consume_token (parser->lexer);
16643 while (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
16644 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA)
16645 && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
16646 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
16648 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
16650 cp_lexer_consume_token (parser->lexer);
16651 cp_parser_skip_to_closing_parenthesis (parser,
16652 /*recovering*/false,
16653 /*or_comma*/false,
16654 /*consume_paren*/true);
16655 continue;
16658 if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
16660 have_trailing_return_fn_decl = true;
16661 break;
16664 cp_lexer_consume_token (parser->lexer);
16666 cp_parser_abort_tentative_parse (parser);
16668 if (have_trailing_return_fn_decl)
16670 type = make_auto ();
16671 break;
16674 if (cxx_dialect >= cxx14)
16676 type = synthesize_implicit_template_parm (parser, NULL_TREE);
16677 type = TREE_TYPE (type);
16679 else
16680 type = error_mark_node;
16682 if (current_class_type && LAMBDA_TYPE_P (current_class_type))
16684 if (cxx_dialect < cxx14)
16685 error_at (token->location,
16686 "use of %<auto%> in lambda parameter declaration "
16687 "only available with "
16688 "-std=c++14 or -std=gnu++14");
16690 else if (cxx_dialect < cxx14)
16691 error_at (token->location,
16692 "use of %<auto%> in parameter declaration "
16693 "only available with "
16694 "-std=c++14 or -std=gnu++14");
16695 else if (!flag_concepts)
16696 pedwarn (token->location, OPT_Wpedantic,
16697 "ISO C++ forbids use of %<auto%> in parameter "
16698 "declaration");
16700 else
16701 type = make_auto ();
16702 break;
16704 case RID_DECLTYPE:
16705 /* Since DR 743, decltype can either be a simple-type-specifier by
16706 itself or begin a nested-name-specifier. Parsing it will replace
16707 it with a CPP_DECLTYPE, so just rewind and let the CPP_DECLTYPE
16708 handling below decide what to do. */
16709 cp_parser_decltype (parser);
16710 cp_lexer_set_token_position (parser->lexer, token);
16711 break;
16713 case RID_TYPEOF:
16714 /* Consume the `typeof' token. */
16715 cp_lexer_consume_token (parser->lexer);
16716 /* Parse the operand to `typeof'. */
16717 type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
16718 /* If it is not already a TYPE, take its type. */
16719 if (!TYPE_P (type))
16720 type = finish_typeof (type);
16722 if (decl_specs)
16723 cp_parser_set_decl_spec_type (decl_specs, type,
16724 token,
16725 /*type_definition_p=*/false);
16727 return type;
16729 case RID_UNDERLYING_TYPE:
16730 type = cp_parser_trait_expr (parser, RID_UNDERLYING_TYPE);
16731 if (decl_specs)
16732 cp_parser_set_decl_spec_type (decl_specs, type,
16733 token,
16734 /*type_definition_p=*/false);
16736 return type;
16738 case RID_BASES:
16739 case RID_DIRECT_BASES:
16740 type = cp_parser_trait_expr (parser, token->keyword);
16741 if (decl_specs)
16742 cp_parser_set_decl_spec_type (decl_specs, type,
16743 token,
16744 /*type_definition_p=*/false);
16745 return type;
16746 default:
16747 break;
16750 /* If token is an already-parsed decltype not followed by ::,
16751 it's a simple-type-specifier. */
16752 if (token->type == CPP_DECLTYPE
16753 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
16755 type = saved_checks_value (token->u.tree_check_value);
16756 if (decl_specs)
16758 cp_parser_set_decl_spec_type (decl_specs, type,
16759 token,
16760 /*type_definition_p=*/false);
16761 /* Remember that we are handling a decltype in order to
16762 implement the resolution of DR 1510 when the argument
16763 isn't instantiation dependent. */
16764 decl_specs->decltype_p = true;
16766 cp_lexer_consume_token (parser->lexer);
16767 return type;
16770 /* If the type-specifier was for a built-in type, we're done. */
16771 if (type)
16773 /* Record the type. */
16774 if (decl_specs
16775 && (token->keyword != RID_SIGNED
16776 && token->keyword != RID_UNSIGNED
16777 && token->keyword != RID_SHORT
16778 && token->keyword != RID_LONG))
16779 cp_parser_set_decl_spec_type (decl_specs,
16780 type,
16781 token,
16782 /*type_definition_p=*/false);
16783 if (decl_specs)
16784 decl_specs->any_specifiers_p = true;
16786 /* Consume the token. */
16787 cp_lexer_consume_token (parser->lexer);
16789 if (type == error_mark_node)
16790 return error_mark_node;
16792 /* There is no valid C++ program where a non-template type is
16793 followed by a "<". That usually indicates that the user thought
16794 that the type was a template. */
16795 cp_parser_check_for_invalid_template_id (parser, type, none_type,
16796 token->location);
16798 return TYPE_NAME (type);
16801 /* The type-specifier must be a user-defined type. */
16802 if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
16804 bool qualified_p;
16805 bool global_p;
16807 /* Don't gobble tokens or issue error messages if this is an
16808 optional type-specifier. */
16809 if ((flags & CP_PARSER_FLAGS_OPTIONAL) || cxx_dialect >= cxx1z)
16810 cp_parser_parse_tentatively (parser);
16812 token = cp_lexer_peek_token (parser->lexer);
16814 /* Look for the optional `::' operator. */
16815 global_p
16816 = (cp_parser_global_scope_opt (parser,
16817 /*current_scope_valid_p=*/false)
16818 != NULL_TREE);
16819 /* Look for the nested-name specifier. */
16820 qualified_p
16821 = (cp_parser_nested_name_specifier_opt (parser,
16822 /*typename_keyword_p=*/false,
16823 /*check_dependency_p=*/true,
16824 /*type_p=*/false,
16825 /*is_declaration=*/false)
16826 != NULL_TREE);
16827 /* If we have seen a nested-name-specifier, and the next token
16828 is `template', then we are using the template-id production. */
16829 if (parser->scope
16830 && cp_parser_optional_template_keyword (parser))
16832 /* Look for the template-id. */
16833 type = cp_parser_template_id (parser,
16834 /*template_keyword_p=*/true,
16835 /*check_dependency_p=*/true,
16836 none_type,
16837 /*is_declaration=*/false);
16838 /* If the template-id did not name a type, we are out of
16839 luck. */
16840 if (TREE_CODE (type) != TYPE_DECL)
16842 cp_parser_error (parser, "expected template-id for type");
16843 type = NULL_TREE;
16846 /* Otherwise, look for a type-name. */
16847 else
16848 type = cp_parser_type_name (parser);
16849 /* Keep track of all name-lookups performed in class scopes. */
16850 if (type
16851 && !global_p
16852 && !qualified_p
16853 && TREE_CODE (type) == TYPE_DECL
16854 && identifier_p (DECL_NAME (type)))
16855 maybe_note_name_used_in_class (DECL_NAME (type), type);
16856 /* If it didn't work out, we don't have a TYPE. */
16857 if (((flags & CP_PARSER_FLAGS_OPTIONAL) || cxx_dialect >= cxx1z)
16858 && !cp_parser_parse_definitely (parser))
16859 type = NULL_TREE;
16860 if (!type && cxx_dialect >= cxx1z)
16862 if (flags & CP_PARSER_FLAGS_OPTIONAL)
16863 cp_parser_parse_tentatively (parser);
16865 cp_parser_global_scope_opt (parser,
16866 /*current_scope_valid_p=*/false);
16867 cp_parser_nested_name_specifier_opt (parser,
16868 /*typename_keyword_p=*/false,
16869 /*check_dependency_p=*/true,
16870 /*type_p=*/false,
16871 /*is_declaration=*/false);
16872 tree name = cp_parser_identifier (parser);
16873 if (name && TREE_CODE (name) == IDENTIFIER_NODE
16874 && parser->scope != error_mark_node)
16876 tree tmpl = cp_parser_lookup_name (parser, name,
16877 none_type,
16878 /*is_template=*/false,
16879 /*is_namespace=*/false,
16880 /*check_dependency=*/true,
16881 /*ambiguous_decls=*/NULL,
16882 token->location);
16883 if (tmpl && tmpl != error_mark_node
16884 && (DECL_CLASS_TEMPLATE_P (tmpl)
16885 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl)))
16886 type = make_template_placeholder (tmpl);
16887 else
16889 type = error_mark_node;
16890 if (!cp_parser_simulate_error (parser))
16891 cp_parser_name_lookup_error (parser, name, tmpl,
16892 NLE_TYPE, token->location);
16895 else
16896 type = error_mark_node;
16898 if ((flags & CP_PARSER_FLAGS_OPTIONAL)
16899 && !cp_parser_parse_definitely (parser))
16900 type = NULL_TREE;
16902 if (type && decl_specs)
16903 cp_parser_set_decl_spec_type (decl_specs, type,
16904 token,
16905 /*type_definition_p=*/false);
16908 /* If we didn't get a type-name, issue an error message. */
16909 if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
16911 cp_parser_error (parser, "expected type-name");
16912 return error_mark_node;
16915 if (type && type != error_mark_node)
16917 /* See if TYPE is an Objective-C type, and if so, parse and
16918 accept any protocol references following it. Do this before
16919 the cp_parser_check_for_invalid_template_id() call, because
16920 Objective-C types can be followed by '<...>' which would
16921 enclose protocol names rather than template arguments, and so
16922 everything is fine. */
16923 if (c_dialect_objc () && !parser->scope
16924 && (objc_is_id (type) || objc_is_class_name (type)))
16926 tree protos = cp_parser_objc_protocol_refs_opt (parser);
16927 tree qual_type = objc_get_protocol_qualified_type (type, protos);
16929 /* Clobber the "unqualified" type previously entered into
16930 DECL_SPECS with the new, improved protocol-qualified version. */
16931 if (decl_specs)
16932 decl_specs->type = qual_type;
16934 return qual_type;
16937 /* There is no valid C++ program where a non-template type is
16938 followed by a "<". That usually indicates that the user
16939 thought that the type was a template. */
16940 cp_parser_check_for_invalid_template_id (parser, TREE_TYPE (type),
16941 none_type,
16942 token->location);
16945 return type;
16948 /* Parse a type-name.
16950 type-name:
16951 class-name
16952 enum-name
16953 typedef-name
16954 simple-template-id [in c++0x]
16956 enum-name:
16957 identifier
16959 typedef-name:
16960 identifier
16962 Concepts:
16964 type-name:
16965 concept-name
16966 partial-concept-id
16968 concept-name:
16969 identifier
16971 Returns a TYPE_DECL for the type. */
16973 static tree
16974 cp_parser_type_name (cp_parser* parser)
16976 return cp_parser_type_name (parser, /*typename_keyword_p=*/false);
16979 /* See above. */
16980 static tree
16981 cp_parser_type_name (cp_parser* parser, bool typename_keyword_p)
16983 tree type_decl;
16985 /* We can't know yet whether it is a class-name or not. */
16986 cp_parser_parse_tentatively (parser);
16987 /* Try a class-name. */
16988 type_decl = cp_parser_class_name (parser,
16989 typename_keyword_p,
16990 /*template_keyword_p=*/false,
16991 none_type,
16992 /*check_dependency_p=*/true,
16993 /*class_head_p=*/false,
16994 /*is_declaration=*/false);
16995 /* If it's not a class-name, keep looking. */
16996 if (!cp_parser_parse_definitely (parser))
16998 if (cxx_dialect < cxx11)
16999 /* It must be a typedef-name or an enum-name. */
17000 return cp_parser_nonclass_name (parser);
17002 cp_parser_parse_tentatively (parser);
17003 /* It is either a simple-template-id representing an
17004 instantiation of an alias template... */
17005 type_decl = cp_parser_template_id (parser,
17006 /*template_keyword_p=*/false,
17007 /*check_dependency_p=*/true,
17008 none_type,
17009 /*is_declaration=*/false);
17010 /* Note that this must be an instantiation of an alias template
17011 because [temp.names]/6 says:
17013 A template-id that names an alias template specialization
17014 is a type-name.
17016 Whereas [temp.names]/7 says:
17018 A simple-template-id that names a class template
17019 specialization is a class-name.
17021 With concepts, this could also be a partial-concept-id that
17022 declares a non-type template parameter. */
17023 if (type_decl != NULL_TREE
17024 && TREE_CODE (type_decl) == TYPE_DECL
17025 && TYPE_DECL_ALIAS_P (type_decl))
17026 gcc_assert (DECL_TEMPLATE_INSTANTIATION (type_decl));
17027 else if (is_constrained_parameter (type_decl))
17028 /* Don't do anything. */ ;
17029 else
17030 cp_parser_simulate_error (parser);
17032 if (!cp_parser_parse_definitely (parser))
17033 /* ... Or a typedef-name or an enum-name. */
17034 return cp_parser_nonclass_name (parser);
17037 return type_decl;
17040 /* Check if DECL and ARGS can form a constrained-type-specifier.
17041 If ARGS is non-null, we try to form a concept check of the
17042 form DECL<?, ARGS> where ? is a wildcard that matches any
17043 kind of template argument. If ARGS is NULL, then we try to
17044 form a concept check of the form DECL<?>. */
17046 static tree
17047 cp_parser_maybe_constrained_type_specifier (cp_parser *parser,
17048 tree decl, tree args)
17050 gcc_assert (args ? TREE_CODE (args) == TREE_VEC : true);
17052 /* If we a constrained-type-specifier cannot be deduced. */
17053 if (parser->prevent_constrained_type_specifiers)
17054 return NULL_TREE;
17056 /* A constrained type specifier can only be found in an
17057 overload set or as a reference to a template declaration.
17059 FIXME: This might be masking a bug. It's possible that
17060 that the deduction below is causing template specializations
17061 to be formed with the wildcard as an argument. */
17062 if (TREE_CODE (decl) != OVERLOAD && TREE_CODE (decl) != TEMPLATE_DECL)
17063 return NULL_TREE;
17065 /* Try to build a call expression that evaluates the
17066 concept. This can fail if the overload set refers
17067 only to non-templates. */
17068 tree placeholder = build_nt (WILDCARD_DECL);
17069 tree check = build_concept_check (decl, placeholder, args);
17070 if (check == error_mark_node)
17071 return NULL_TREE;
17073 /* Deduce the checked constraint and the prototype parameter.
17075 FIXME: In certain cases, failure to deduce should be a
17076 diagnosable error. */
17077 tree conc;
17078 tree proto;
17079 if (!deduce_constrained_parameter (check, conc, proto))
17080 return NULL_TREE;
17082 /* In template parameter scope, this results in a constrained
17083 parameter. Return a descriptor of that parm. */
17084 if (processing_template_parmlist)
17085 return build_constrained_parameter (conc, proto, args);
17087 /* In a parameter-declaration-clause, constrained-type
17088 specifiers result in invented template parameters. */
17089 if (parser->auto_is_implicit_function_template_parm_p)
17091 tree x = build_constrained_parameter (conc, proto, args);
17092 return synthesize_implicit_template_parm (parser, x);
17094 else
17096 /* Otherwise, we're in a context where the constrained
17097 type name is deduced and the constraint applies
17098 after deduction. */
17099 return make_constrained_auto (conc, args);
17102 return NULL_TREE;
17105 /* If DECL refers to a concept, return a TYPE_DECL representing
17106 the result of using the constrained type specifier in the
17107 current context. DECL refers to a concept if
17109 - it is an overload set containing a function concept taking a single
17110 type argument, or
17112 - it is a variable concept taking a single type argument. */
17114 static tree
17115 cp_parser_maybe_concept_name (cp_parser* parser, tree decl)
17117 if (flag_concepts
17118 && (TREE_CODE (decl) == OVERLOAD
17119 || BASELINK_P (decl)
17120 || variable_concept_p (decl)))
17121 return cp_parser_maybe_constrained_type_specifier (parser, decl, NULL_TREE);
17122 else
17123 return NULL_TREE;
17126 /* Check if DECL and ARGS form a partial-concept-id. If so,
17127 assign ID to the resulting constrained placeholder.
17129 Returns true if the partial-concept-id designates a placeholder
17130 and false otherwise. Note that *id is set to NULL_TREE in
17131 this case. */
17133 static tree
17134 cp_parser_maybe_partial_concept_id (cp_parser *parser, tree decl, tree args)
17136 return cp_parser_maybe_constrained_type_specifier (parser, decl, args);
17139 /* Parse a non-class type-name, that is, either an enum-name, a typedef-name,
17140 or a concept-name.
17142 enum-name:
17143 identifier
17145 typedef-name:
17146 identifier
17148 concept-name:
17149 identifier
17151 Returns a TYPE_DECL for the type. */
17153 static tree
17154 cp_parser_nonclass_name (cp_parser* parser)
17156 tree type_decl;
17157 tree identifier;
17159 cp_token *token = cp_lexer_peek_token (parser->lexer);
17160 identifier = cp_parser_identifier (parser);
17161 if (identifier == error_mark_node)
17162 return error_mark_node;
17164 /* Look up the type-name. */
17165 type_decl = cp_parser_lookup_name_simple (parser, identifier, token->location);
17167 type_decl = strip_using_decl (type_decl);
17169 /* If we found an overload set, then it may refer to a concept-name. */
17170 if (tree decl = cp_parser_maybe_concept_name (parser, type_decl))
17171 type_decl = decl;
17173 if (TREE_CODE (type_decl) != TYPE_DECL
17174 && (objc_is_id (identifier) || objc_is_class_name (identifier)))
17176 /* See if this is an Objective-C type. */
17177 tree protos = cp_parser_objc_protocol_refs_opt (parser);
17178 tree type = objc_get_protocol_qualified_type (identifier, protos);
17179 if (type)
17180 type_decl = TYPE_NAME (type);
17183 /* Issue an error if we did not find a type-name. */
17184 if (TREE_CODE (type_decl) != TYPE_DECL
17185 /* In Objective-C, we have the complication that class names are
17186 normally type names and start declarations (eg, the
17187 "NSObject" in "NSObject *object;"), but can be used in an
17188 Objective-C 2.0 dot-syntax (as in "NSObject.version") which
17189 is an expression. So, a classname followed by a dot is not a
17190 valid type-name. */
17191 || (objc_is_class_name (TREE_TYPE (type_decl))
17192 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT))
17194 if (!cp_parser_simulate_error (parser))
17195 cp_parser_name_lookup_error (parser, identifier, type_decl,
17196 NLE_TYPE, token->location);
17197 return error_mark_node;
17199 /* Remember that the name was used in the definition of the
17200 current class so that we can check later to see if the
17201 meaning would have been different after the class was
17202 entirely defined. */
17203 else if (type_decl != error_mark_node
17204 && !parser->scope)
17205 maybe_note_name_used_in_class (identifier, type_decl);
17207 return type_decl;
17210 /* Parse an elaborated-type-specifier. Note that the grammar given
17211 here incorporates the resolution to DR68.
17213 elaborated-type-specifier:
17214 class-key :: [opt] nested-name-specifier [opt] identifier
17215 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
17216 enum-key :: [opt] nested-name-specifier [opt] identifier
17217 typename :: [opt] nested-name-specifier identifier
17218 typename :: [opt] nested-name-specifier template [opt]
17219 template-id
17221 GNU extension:
17223 elaborated-type-specifier:
17224 class-key attributes :: [opt] nested-name-specifier [opt] identifier
17225 class-key attributes :: [opt] nested-name-specifier [opt]
17226 template [opt] template-id
17227 enum attributes :: [opt] nested-name-specifier [opt] identifier
17229 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
17230 declared `friend'. If IS_DECLARATION is TRUE, then this
17231 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
17232 something is being declared.
17234 Returns the TYPE specified. */
17236 static tree
17237 cp_parser_elaborated_type_specifier (cp_parser* parser,
17238 bool is_friend,
17239 bool is_declaration)
17241 enum tag_types tag_type;
17242 tree identifier;
17243 tree type = NULL_TREE;
17244 tree attributes = NULL_TREE;
17245 tree globalscope;
17246 cp_token *token = NULL;
17248 /* See if we're looking at the `enum' keyword. */
17249 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
17251 /* Consume the `enum' token. */
17252 cp_lexer_consume_token (parser->lexer);
17253 /* Remember that it's an enumeration type. */
17254 tag_type = enum_type;
17255 /* Issue a warning if the `struct' or `class' key (for C++0x scoped
17256 enums) is used here. */
17257 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
17258 || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
17260 pedwarn (input_location, 0, "elaborated-type-specifier "
17261 "for a scoped enum must not use the %<%D%> keyword",
17262 cp_lexer_peek_token (parser->lexer)->u.value);
17263 /* Consume the `struct' or `class' and parse it anyway. */
17264 cp_lexer_consume_token (parser->lexer);
17266 /* Parse the attributes. */
17267 attributes = cp_parser_attributes_opt (parser);
17269 /* Or, it might be `typename'. */
17270 else if (cp_lexer_next_token_is_keyword (parser->lexer,
17271 RID_TYPENAME))
17273 /* Consume the `typename' token. */
17274 cp_lexer_consume_token (parser->lexer);
17275 /* Remember that it's a `typename' type. */
17276 tag_type = typename_type;
17278 /* Otherwise it must be a class-key. */
17279 else
17281 tag_type = cp_parser_class_key (parser);
17282 if (tag_type == none_type)
17283 return error_mark_node;
17284 /* Parse the attributes. */
17285 attributes = cp_parser_attributes_opt (parser);
17288 /* Look for the `::' operator. */
17289 globalscope = cp_parser_global_scope_opt (parser,
17290 /*current_scope_valid_p=*/false);
17291 /* Look for the nested-name-specifier. */
17292 tree nested_name_specifier;
17293 if (tag_type == typename_type && !globalscope)
17295 nested_name_specifier
17296 = cp_parser_nested_name_specifier (parser,
17297 /*typename_keyword_p=*/true,
17298 /*check_dependency_p=*/true,
17299 /*type_p=*/true,
17300 is_declaration);
17301 if (!nested_name_specifier)
17302 return error_mark_node;
17304 else
17305 /* Even though `typename' is not present, the proposed resolution
17306 to Core Issue 180 says that in `class A<T>::B', `B' should be
17307 considered a type-name, even if `A<T>' is dependent. */
17308 nested_name_specifier
17309 = cp_parser_nested_name_specifier_opt (parser,
17310 /*typename_keyword_p=*/true,
17311 /*check_dependency_p=*/true,
17312 /*type_p=*/true,
17313 is_declaration);
17314 /* For everything but enumeration types, consider a template-id.
17315 For an enumeration type, consider only a plain identifier. */
17316 if (tag_type != enum_type)
17318 bool template_p = false;
17319 tree decl;
17321 /* Allow the `template' keyword. */
17322 template_p = cp_parser_optional_template_keyword (parser);
17323 /* If we didn't see `template', we don't know if there's a
17324 template-id or not. */
17325 if (!template_p)
17326 cp_parser_parse_tentatively (parser);
17327 /* Parse the template-id. */
17328 token = cp_lexer_peek_token (parser->lexer);
17329 decl = cp_parser_template_id (parser, template_p,
17330 /*check_dependency_p=*/true,
17331 tag_type,
17332 is_declaration);
17333 /* If we didn't find a template-id, look for an ordinary
17334 identifier. */
17335 if (!template_p && !cp_parser_parse_definitely (parser))
17337 /* We can get here when cp_parser_template_id, called by
17338 cp_parser_class_name with tag_type == none_type, succeeds
17339 and caches a BASELINK. Then, when called again here,
17340 instead of failing and returning an error_mark_node
17341 returns it (see template/typename17.C in C++11).
17342 ??? Could we diagnose this earlier? */
17343 else if (tag_type == typename_type && BASELINK_P (decl))
17345 cp_parser_diagnose_invalid_type_name (parser, decl, token->location);
17346 type = error_mark_node;
17348 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
17349 in effect, then we must assume that, upon instantiation, the
17350 template will correspond to a class. */
17351 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
17352 && tag_type == typename_type)
17353 type = make_typename_type (parser->scope, decl,
17354 typename_type,
17355 /*complain=*/tf_error);
17356 /* If the `typename' keyword is in effect and DECL is not a type
17357 decl, then type is non existent. */
17358 else if (tag_type == typename_type && TREE_CODE (decl) != TYPE_DECL)
17360 else if (TREE_CODE (decl) == TYPE_DECL)
17362 type = check_elaborated_type_specifier (tag_type, decl,
17363 /*allow_template_p=*/true);
17365 /* If the next token is a semicolon, this must be a specialization,
17366 instantiation, or friend declaration. Check the scope while we
17367 still know whether or not we had a nested-name-specifier. */
17368 if (type != error_mark_node
17369 && !nested_name_specifier && !is_friend
17370 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
17371 check_unqualified_spec_or_inst (type, token->location);
17373 else if (decl == error_mark_node)
17374 type = error_mark_node;
17377 if (!type)
17379 token = cp_lexer_peek_token (parser->lexer);
17380 identifier = cp_parser_identifier (parser);
17382 if (identifier == error_mark_node)
17384 parser->scope = NULL_TREE;
17385 return error_mark_node;
17388 /* For a `typename', we needn't call xref_tag. */
17389 if (tag_type == typename_type
17390 && TREE_CODE (parser->scope) != NAMESPACE_DECL)
17391 return cp_parser_make_typename_type (parser, identifier,
17392 token->location);
17394 /* Template parameter lists apply only if we are not within a
17395 function parameter list. */
17396 bool template_parm_lists_apply
17397 = parser->num_template_parameter_lists;
17398 if (template_parm_lists_apply)
17399 for (cp_binding_level *s = current_binding_level;
17400 s && s->kind != sk_template_parms;
17401 s = s->level_chain)
17402 if (s->kind == sk_function_parms)
17403 template_parm_lists_apply = false;
17405 /* Look up a qualified name in the usual way. */
17406 if (parser->scope)
17408 tree decl;
17409 tree ambiguous_decls;
17411 decl = cp_parser_lookup_name (parser, identifier,
17412 tag_type,
17413 /*is_template=*/false,
17414 /*is_namespace=*/false,
17415 /*check_dependency=*/true,
17416 &ambiguous_decls,
17417 token->location);
17419 /* If the lookup was ambiguous, an error will already have been
17420 issued. */
17421 if (ambiguous_decls)
17422 return error_mark_node;
17424 /* If we are parsing friend declaration, DECL may be a
17425 TEMPLATE_DECL tree node here. However, we need to check
17426 whether this TEMPLATE_DECL results in valid code. Consider
17427 the following example:
17429 namespace N {
17430 template <class T> class C {};
17432 class X {
17433 template <class T> friend class N::C; // #1, valid code
17435 template <class T> class Y {
17436 friend class N::C; // #2, invalid code
17439 For both case #1 and #2, we arrive at a TEMPLATE_DECL after
17440 name lookup of `N::C'. We see that friend declaration must
17441 be template for the code to be valid. Note that
17442 processing_template_decl does not work here since it is
17443 always 1 for the above two cases. */
17445 decl = (cp_parser_maybe_treat_template_as_class
17446 (decl, /*tag_name_p=*/is_friend
17447 && template_parm_lists_apply));
17449 if (TREE_CODE (decl) != TYPE_DECL)
17451 cp_parser_diagnose_invalid_type_name (parser,
17452 identifier,
17453 token->location);
17454 return error_mark_node;
17457 if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
17459 bool allow_template = (template_parm_lists_apply
17460 || DECL_SELF_REFERENCE_P (decl));
17461 type = check_elaborated_type_specifier (tag_type, decl,
17462 allow_template);
17464 if (type == error_mark_node)
17465 return error_mark_node;
17468 /* Forward declarations of nested types, such as
17470 class C1::C2;
17471 class C1::C2::C3;
17473 are invalid unless all components preceding the final '::'
17474 are complete. If all enclosing types are complete, these
17475 declarations become merely pointless.
17477 Invalid forward declarations of nested types are errors
17478 caught elsewhere in parsing. Those that are pointless arrive
17479 here. */
17481 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
17482 && !is_friend && !processing_explicit_instantiation)
17483 warning (0, "declaration %qD does not declare anything", decl);
17485 type = TREE_TYPE (decl);
17487 else
17489 /* An elaborated-type-specifier sometimes introduces a new type and
17490 sometimes names an existing type. Normally, the rule is that it
17491 introduces a new type only if there is not an existing type of
17492 the same name already in scope. For example, given:
17494 struct S {};
17495 void f() { struct S s; }
17497 the `struct S' in the body of `f' is the same `struct S' as in
17498 the global scope; the existing definition is used. However, if
17499 there were no global declaration, this would introduce a new
17500 local class named `S'.
17502 An exception to this rule applies to the following code:
17504 namespace N { struct S; }
17506 Here, the elaborated-type-specifier names a new type
17507 unconditionally; even if there is already an `S' in the
17508 containing scope this declaration names a new type.
17509 This exception only applies if the elaborated-type-specifier
17510 forms the complete declaration:
17512 [class.name]
17514 A declaration consisting solely of `class-key identifier ;' is
17515 either a redeclaration of the name in the current scope or a
17516 forward declaration of the identifier as a class name. It
17517 introduces the name into the current scope.
17519 We are in this situation precisely when the next token is a `;'.
17521 An exception to the exception is that a `friend' declaration does
17522 *not* name a new type; i.e., given:
17524 struct S { friend struct T; };
17526 `T' is not a new type in the scope of `S'.
17528 Also, `new struct S' or `sizeof (struct S)' never results in the
17529 definition of a new type; a new type can only be declared in a
17530 declaration context. */
17532 tag_scope ts;
17533 bool template_p;
17535 if (is_friend)
17536 /* Friends have special name lookup rules. */
17537 ts = ts_within_enclosing_non_class;
17538 else if (is_declaration
17539 && cp_lexer_next_token_is (parser->lexer,
17540 CPP_SEMICOLON))
17541 /* This is a `class-key identifier ;' */
17542 ts = ts_current;
17543 else
17544 ts = ts_global;
17546 template_p =
17547 (template_parm_lists_apply
17548 && (cp_parser_next_token_starts_class_definition_p (parser)
17549 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)));
17550 /* An unqualified name was used to reference this type, so
17551 there were no qualifying templates. */
17552 if (template_parm_lists_apply
17553 && !cp_parser_check_template_parameters (parser,
17554 /*num_templates=*/0,
17555 token->location,
17556 /*declarator=*/NULL))
17557 return error_mark_node;
17558 type = xref_tag (tag_type, identifier, ts, template_p);
17562 if (type == error_mark_node)
17563 return error_mark_node;
17565 /* Allow attributes on forward declarations of classes. */
17566 if (attributes)
17568 if (TREE_CODE (type) == TYPENAME_TYPE)
17569 warning (OPT_Wattributes,
17570 "attributes ignored on uninstantiated type");
17571 else if (tag_type != enum_type && CLASSTYPE_TEMPLATE_INSTANTIATION (type)
17572 && ! processing_explicit_instantiation)
17573 warning (OPT_Wattributes,
17574 "attributes ignored on template instantiation");
17575 else if (is_declaration && cp_parser_declares_only_class_p (parser))
17576 cplus_decl_attributes (&type, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
17577 else
17578 warning (OPT_Wattributes,
17579 "attributes ignored on elaborated-type-specifier that is not a forward declaration");
17582 if (tag_type != enum_type)
17584 /* Indicate whether this class was declared as a `class' or as a
17585 `struct'. */
17586 if (CLASS_TYPE_P (type))
17587 CLASSTYPE_DECLARED_CLASS (type) = (tag_type == class_type);
17588 cp_parser_check_class_key (tag_type, type);
17591 /* A "<" cannot follow an elaborated type specifier. If that
17592 happens, the user was probably trying to form a template-id. */
17593 cp_parser_check_for_invalid_template_id (parser, type, tag_type,
17594 token->location);
17596 return type;
17599 /* Parse an enum-specifier.
17601 enum-specifier:
17602 enum-head { enumerator-list [opt] }
17603 enum-head { enumerator-list , } [C++0x]
17605 enum-head:
17606 enum-key identifier [opt] enum-base [opt]
17607 enum-key nested-name-specifier identifier enum-base [opt]
17609 enum-key:
17610 enum
17611 enum class [C++0x]
17612 enum struct [C++0x]
17614 enum-base: [C++0x]
17615 : type-specifier-seq
17617 opaque-enum-specifier:
17618 enum-key identifier enum-base [opt] ;
17620 GNU Extensions:
17621 enum-key attributes[opt] identifier [opt] enum-base [opt]
17622 { enumerator-list [opt] }attributes[opt]
17623 enum-key attributes[opt] identifier [opt] enum-base [opt]
17624 { enumerator-list, }attributes[opt] [C++0x]
17626 Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
17627 if the token stream isn't an enum-specifier after all. */
17629 static tree
17630 cp_parser_enum_specifier (cp_parser* parser)
17632 tree identifier;
17633 tree type = NULL_TREE;
17634 tree prev_scope;
17635 tree nested_name_specifier = NULL_TREE;
17636 tree attributes;
17637 bool scoped_enum_p = false;
17638 bool has_underlying_type = false;
17639 bool nested_being_defined = false;
17640 bool new_value_list = false;
17641 bool is_new_type = false;
17642 bool is_unnamed = false;
17643 tree underlying_type = NULL_TREE;
17644 cp_token *type_start_token = NULL;
17645 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
17647 parser->colon_corrects_to_scope_p = false;
17649 /* Parse tentatively so that we can back up if we don't find a
17650 enum-specifier. */
17651 cp_parser_parse_tentatively (parser);
17653 /* Caller guarantees that the current token is 'enum', an identifier
17654 possibly follows, and the token after that is an opening brace.
17655 If we don't have an identifier, fabricate an anonymous name for
17656 the enumeration being defined. */
17657 cp_lexer_consume_token (parser->lexer);
17659 /* Parse the "class" or "struct", which indicates a scoped
17660 enumeration type in C++0x. */
17661 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
17662 || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
17664 if (cxx_dialect < cxx11)
17665 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
17667 /* Consume the `struct' or `class' token. */
17668 cp_lexer_consume_token (parser->lexer);
17670 scoped_enum_p = true;
17673 attributes = cp_parser_attributes_opt (parser);
17675 /* Clear the qualification. */
17676 parser->scope = NULL_TREE;
17677 parser->qualifying_scope = NULL_TREE;
17678 parser->object_scope = NULL_TREE;
17680 /* Figure out in what scope the declaration is being placed. */
17681 prev_scope = current_scope ();
17683 type_start_token = cp_lexer_peek_token (parser->lexer);
17685 push_deferring_access_checks (dk_no_check);
17686 nested_name_specifier
17687 = cp_parser_nested_name_specifier_opt (parser,
17688 /*typename_keyword_p=*/true,
17689 /*check_dependency_p=*/false,
17690 /*type_p=*/false,
17691 /*is_declaration=*/false);
17693 if (nested_name_specifier)
17695 tree name;
17697 identifier = cp_parser_identifier (parser);
17698 name = cp_parser_lookup_name (parser, identifier,
17699 enum_type,
17700 /*is_template=*/false,
17701 /*is_namespace=*/false,
17702 /*check_dependency=*/true,
17703 /*ambiguous_decls=*/NULL,
17704 input_location);
17705 if (name && name != error_mark_node)
17707 type = TREE_TYPE (name);
17708 if (TREE_CODE (type) == TYPENAME_TYPE)
17710 /* Are template enums allowed in ISO? */
17711 if (template_parm_scope_p ())
17712 pedwarn (type_start_token->location, OPT_Wpedantic,
17713 "%qD is an enumeration template", name);
17714 /* ignore a typename reference, for it will be solved by name
17715 in start_enum. */
17716 type = NULL_TREE;
17719 else if (nested_name_specifier == error_mark_node)
17720 /* We already issued an error. */;
17721 else
17723 error_at (type_start_token->location,
17724 "%qD does not name an enumeration in %qT",
17725 identifier, nested_name_specifier);
17726 nested_name_specifier = error_mark_node;
17729 else
17731 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
17732 identifier = cp_parser_identifier (parser);
17733 else
17735 identifier = make_anon_name ();
17736 is_unnamed = true;
17737 if (scoped_enum_p)
17738 error_at (type_start_token->location,
17739 "unnamed scoped enum is not allowed");
17742 pop_deferring_access_checks ();
17744 /* Check for the `:' that denotes a specified underlying type in C++0x.
17745 Note that a ':' could also indicate a bitfield width, however. */
17746 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
17748 cp_decl_specifier_seq type_specifiers;
17750 /* Consume the `:'. */
17751 cp_lexer_consume_token (parser->lexer);
17753 /* Parse the type-specifier-seq. */
17754 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
17755 /*is_trailing_return=*/false,
17756 &type_specifiers);
17758 /* At this point this is surely not elaborated type specifier. */
17759 if (!cp_parser_parse_definitely (parser))
17760 return NULL_TREE;
17762 if (cxx_dialect < cxx11)
17763 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
17765 has_underlying_type = true;
17767 /* If that didn't work, stop. */
17768 if (type_specifiers.type != error_mark_node)
17770 underlying_type = grokdeclarator (NULL, &type_specifiers, TYPENAME,
17771 /*initialized=*/0, NULL);
17772 if (underlying_type == error_mark_node
17773 || check_for_bare_parameter_packs (underlying_type))
17774 underlying_type = NULL_TREE;
17778 /* Look for the `{' but don't consume it yet. */
17779 if (!cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
17781 if (cxx_dialect < cxx11 || (!scoped_enum_p && !underlying_type))
17783 cp_parser_error (parser, "expected %<{%>");
17784 if (has_underlying_type)
17786 type = NULL_TREE;
17787 goto out;
17790 /* An opaque-enum-specifier must have a ';' here. */
17791 if ((scoped_enum_p || underlying_type)
17792 && cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
17794 cp_parser_error (parser, "expected %<;%> or %<{%>");
17795 if (has_underlying_type)
17797 type = NULL_TREE;
17798 goto out;
17803 if (!has_underlying_type && !cp_parser_parse_definitely (parser))
17804 return NULL_TREE;
17806 if (nested_name_specifier)
17808 if (CLASS_TYPE_P (nested_name_specifier))
17810 nested_being_defined = TYPE_BEING_DEFINED (nested_name_specifier);
17811 TYPE_BEING_DEFINED (nested_name_specifier) = 1;
17812 push_scope (nested_name_specifier);
17814 else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
17816 push_nested_namespace (nested_name_specifier);
17820 /* Issue an error message if type-definitions are forbidden here. */
17821 if (!cp_parser_check_type_definition (parser))
17822 type = error_mark_node;
17823 else
17824 /* Create the new type. We do this before consuming the opening
17825 brace so the enum will be recorded as being on the line of its
17826 tag (or the 'enum' keyword, if there is no tag). */
17827 type = start_enum (identifier, type, underlying_type,
17828 attributes, scoped_enum_p, &is_new_type);
17830 /* If the next token is not '{' it is an opaque-enum-specifier or an
17831 elaborated-type-specifier. */
17832 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
17834 timevar_push (TV_PARSE_ENUM);
17835 if (nested_name_specifier
17836 && nested_name_specifier != error_mark_node)
17838 /* The following catches invalid code such as:
17839 enum class S<int>::E { A, B, C }; */
17840 if (!processing_specialization
17841 && CLASS_TYPE_P (nested_name_specifier)
17842 && CLASSTYPE_USE_TEMPLATE (nested_name_specifier))
17843 error_at (type_start_token->location, "cannot add an enumerator "
17844 "list to a template instantiation");
17846 if (TREE_CODE (nested_name_specifier) == TYPENAME_TYPE)
17848 error_at (type_start_token->location,
17849 "%<%T::%E%> has not been declared",
17850 TYPE_CONTEXT (nested_name_specifier),
17851 nested_name_specifier);
17852 type = error_mark_node;
17854 else if (TREE_CODE (nested_name_specifier) != NAMESPACE_DECL
17855 && !CLASS_TYPE_P (nested_name_specifier))
17857 error_at (type_start_token->location, "nested name specifier "
17858 "%qT for enum declaration does not name a class "
17859 "or namespace", nested_name_specifier);
17860 type = error_mark_node;
17862 /* If that scope does not contain the scope in which the
17863 class was originally declared, the program is invalid. */
17864 else if (prev_scope && !is_ancestor (prev_scope,
17865 nested_name_specifier))
17867 if (at_namespace_scope_p ())
17868 error_at (type_start_token->location,
17869 "declaration of %qD in namespace %qD which does not "
17870 "enclose %qD",
17871 type, prev_scope, nested_name_specifier);
17872 else
17873 error_at (type_start_token->location,
17874 "declaration of %qD in %qD which does not "
17875 "enclose %qD",
17876 type, prev_scope, nested_name_specifier);
17877 type = error_mark_node;
17879 /* If that scope is the scope where the declaration is being placed
17880 the program is invalid. */
17881 else if (CLASS_TYPE_P (nested_name_specifier)
17882 && CLASS_TYPE_P (prev_scope)
17883 && same_type_p (nested_name_specifier, prev_scope))
17885 permerror (type_start_token->location,
17886 "extra qualification not allowed");
17887 nested_name_specifier = NULL_TREE;
17891 if (scoped_enum_p)
17892 begin_scope (sk_scoped_enum, type);
17894 /* Consume the opening brace. */
17895 cp_lexer_consume_token (parser->lexer);
17897 if (type == error_mark_node)
17898 ; /* Nothing to add */
17899 else if (OPAQUE_ENUM_P (type)
17900 || (cxx_dialect > cxx98 && processing_specialization))
17902 new_value_list = true;
17903 SET_OPAQUE_ENUM_P (type, false);
17904 DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
17906 else
17908 error_at (type_start_token->location,
17909 "multiple definition of %q#T", type);
17910 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)),
17911 "previous definition here");
17912 type = error_mark_node;
17915 if (type == error_mark_node)
17916 cp_parser_skip_to_end_of_block_or_statement (parser);
17917 /* If the next token is not '}', then there are some enumerators. */
17918 else if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
17920 if (is_unnamed && !scoped_enum_p)
17921 pedwarn (type_start_token->location, OPT_Wpedantic,
17922 "ISO C++ forbids empty unnamed enum");
17924 else
17925 cp_parser_enumerator_list (parser, type);
17927 /* Consume the final '}'. */
17928 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
17930 if (scoped_enum_p)
17931 finish_scope ();
17932 timevar_pop (TV_PARSE_ENUM);
17934 else
17936 /* If a ';' follows, then it is an opaque-enum-specifier
17937 and additional restrictions apply. */
17938 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
17940 if (is_unnamed)
17941 error_at (type_start_token->location,
17942 "opaque-enum-specifier without name");
17943 else if (nested_name_specifier)
17944 error_at (type_start_token->location,
17945 "opaque-enum-specifier must use a simple identifier");
17949 /* Look for trailing attributes to apply to this enumeration, and
17950 apply them if appropriate. */
17951 if (cp_parser_allow_gnu_extensions_p (parser))
17953 tree trailing_attr = cp_parser_gnu_attributes_opt (parser);
17954 cplus_decl_attributes (&type,
17955 trailing_attr,
17956 (int) ATTR_FLAG_TYPE_IN_PLACE);
17959 /* Finish up the enumeration. */
17960 if (type != error_mark_node)
17962 if (new_value_list)
17963 finish_enum_value_list (type);
17964 if (is_new_type)
17965 finish_enum (type);
17968 if (nested_name_specifier)
17970 if (CLASS_TYPE_P (nested_name_specifier))
17972 TYPE_BEING_DEFINED (nested_name_specifier) = nested_being_defined;
17973 pop_scope (nested_name_specifier);
17975 else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
17977 pop_nested_namespace (nested_name_specifier);
17980 out:
17981 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
17982 return type;
17985 /* Parse an enumerator-list. The enumerators all have the indicated
17986 TYPE.
17988 enumerator-list:
17989 enumerator-definition
17990 enumerator-list , enumerator-definition */
17992 static void
17993 cp_parser_enumerator_list (cp_parser* parser, tree type)
17995 while (true)
17997 /* Parse an enumerator-definition. */
17998 cp_parser_enumerator_definition (parser, type);
18000 /* If the next token is not a ',', we've reached the end of
18001 the list. */
18002 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
18003 break;
18004 /* Otherwise, consume the `,' and keep going. */
18005 cp_lexer_consume_token (parser->lexer);
18006 /* If the next token is a `}', there is a trailing comma. */
18007 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
18009 if (cxx_dialect < cxx11 && !in_system_header_at (input_location))
18010 pedwarn (input_location, OPT_Wpedantic,
18011 "comma at end of enumerator list");
18012 break;
18017 /* Parse an enumerator-definition. The enumerator has the indicated
18018 TYPE.
18020 enumerator-definition:
18021 enumerator
18022 enumerator = constant-expression
18024 enumerator:
18025 identifier
18027 GNU Extensions:
18029 enumerator-definition:
18030 enumerator attributes [opt]
18031 enumerator attributes [opt] = constant-expression */
18033 static void
18034 cp_parser_enumerator_definition (cp_parser* parser, tree type)
18036 tree identifier;
18037 tree value;
18038 location_t loc;
18040 /* Save the input location because we are interested in the location
18041 of the identifier and not the location of the explicit value. */
18042 loc = cp_lexer_peek_token (parser->lexer)->location;
18044 /* Look for the identifier. */
18045 identifier = cp_parser_identifier (parser);
18046 if (identifier == error_mark_node)
18047 return;
18049 /* Parse any specified attributes. */
18050 tree attrs = cp_parser_attributes_opt (parser);
18052 /* If the next token is an '=', then there is an explicit value. */
18053 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
18055 /* Consume the `=' token. */
18056 cp_lexer_consume_token (parser->lexer);
18057 /* Parse the value. */
18058 value = cp_parser_constant_expression (parser);
18060 else
18061 value = NULL_TREE;
18063 /* If we are processing a template, make sure the initializer of the
18064 enumerator doesn't contain any bare template parameter pack. */
18065 if (check_for_bare_parameter_packs (value))
18066 value = error_mark_node;
18068 /* Create the enumerator. */
18069 build_enumerator (identifier, value, type, attrs, loc);
18072 /* Parse a namespace-name.
18074 namespace-name:
18075 original-namespace-name
18076 namespace-alias
18078 Returns the NAMESPACE_DECL for the namespace. */
18080 static tree
18081 cp_parser_namespace_name (cp_parser* parser)
18083 tree identifier;
18084 tree namespace_decl;
18086 cp_token *token = cp_lexer_peek_token (parser->lexer);
18088 /* Get the name of the namespace. */
18089 identifier = cp_parser_identifier (parser);
18090 if (identifier == error_mark_node)
18091 return error_mark_node;
18093 /* Look up the identifier in the currently active scope. Look only
18094 for namespaces, due to:
18096 [basic.lookup.udir]
18098 When looking up a namespace-name in a using-directive or alias
18099 definition, only namespace names are considered.
18101 And:
18103 [basic.lookup.qual]
18105 During the lookup of a name preceding the :: scope resolution
18106 operator, object, function, and enumerator names are ignored.
18108 (Note that cp_parser_qualifying_entity only calls this
18109 function if the token after the name is the scope resolution
18110 operator.) */
18111 namespace_decl = cp_parser_lookup_name (parser, identifier,
18112 none_type,
18113 /*is_template=*/false,
18114 /*is_namespace=*/true,
18115 /*check_dependency=*/true,
18116 /*ambiguous_decls=*/NULL,
18117 token->location);
18118 /* If it's not a namespace, issue an error. */
18119 if (namespace_decl == error_mark_node
18120 || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
18122 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
18123 error_at (token->location, "%qD is not a namespace-name", identifier);
18124 cp_parser_error (parser, "expected namespace-name");
18125 namespace_decl = error_mark_node;
18128 return namespace_decl;
18131 /* Parse a namespace-definition.
18133 namespace-definition:
18134 named-namespace-definition
18135 unnamed-namespace-definition
18137 named-namespace-definition:
18138 original-namespace-definition
18139 extension-namespace-definition
18141 original-namespace-definition:
18142 namespace identifier { namespace-body }
18144 extension-namespace-definition:
18145 namespace original-namespace-name { namespace-body }
18147 unnamed-namespace-definition:
18148 namespace { namespace-body } */
18150 static void
18151 cp_parser_namespace_definition (cp_parser* parser)
18153 tree identifier, attribs;
18154 bool has_visibility;
18155 bool is_inline;
18156 cp_token* token;
18157 int nested_definition_count = 0;
18159 cp_ensure_no_omp_declare_simd (parser);
18160 cp_ensure_no_oacc_routine (parser);
18161 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_INLINE))
18163 maybe_warn_cpp0x (CPP0X_INLINE_NAMESPACES);
18164 is_inline = true;
18165 cp_lexer_consume_token (parser->lexer);
18167 else
18168 is_inline = false;
18170 /* Look for the `namespace' keyword. */
18171 token = cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
18173 /* Parse any specified attributes before the identifier. */
18174 attribs = cp_parser_attributes_opt (parser);
18176 /* Get the name of the namespace. We do not attempt to distinguish
18177 between an original-namespace-definition and an
18178 extension-namespace-definition at this point. The semantic
18179 analysis routines are responsible for that. */
18180 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
18181 identifier = cp_parser_identifier (parser);
18182 else
18183 identifier = NULL_TREE;
18185 /* Parse any specified attributes after the identifier. */
18186 tree post_ident_attribs = cp_parser_attributes_opt (parser);
18187 if (post_ident_attribs)
18189 if (attribs)
18190 attribs = chainon (attribs, post_ident_attribs);
18191 else
18192 attribs = post_ident_attribs;
18195 /* Start the namespace. */
18196 bool ok = push_namespace (identifier);
18198 /* Parse any nested namespace definition. */
18199 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
18201 if (attribs)
18202 error_at (token->location, "a nested namespace definition cannot have attributes");
18203 if (cxx_dialect < cxx1z)
18204 pedwarn (input_location, OPT_Wpedantic,
18205 "nested namespace definitions only available with "
18206 "-std=c++1z or -std=gnu++1z");
18207 if (is_inline)
18208 error_at (token->location, "a nested namespace definition cannot be inline");
18209 while (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
18211 cp_lexer_consume_token (parser->lexer);
18212 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
18213 identifier = cp_parser_identifier (parser);
18214 else
18216 cp_parser_error (parser, "nested identifier required");
18217 break;
18219 if (push_namespace (identifier))
18220 ++nested_definition_count;
18224 /* Look for the `{' to validate starting the namespace. */
18225 cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
18227 /* "inline namespace" is equivalent to a stub namespace definition
18228 followed by a strong using directive. */
18229 if (is_inline && ok)
18231 tree name_space = current_namespace;
18232 /* Set up namespace association. */
18233 DECL_NAMESPACE_ASSOCIATIONS (name_space)
18234 = tree_cons (CP_DECL_CONTEXT (name_space), NULL_TREE,
18235 DECL_NAMESPACE_ASSOCIATIONS (name_space));
18236 /* Import the contents of the inline namespace. */
18237 pop_namespace ();
18238 do_using_directive (name_space);
18239 push_namespace (identifier);
18242 has_visibility = handle_namespace_attrs (current_namespace, attribs);
18244 warning (OPT_Wnamespaces, "namespace %qD entered", current_namespace);
18246 /* Parse the body of the namespace. */
18247 cp_parser_namespace_body (parser);
18249 if (has_visibility)
18250 pop_visibility (1);
18252 /* Finish the nested namespace definitions. */
18253 while (nested_definition_count--)
18254 pop_namespace ();
18256 /* Finish the namespace. */
18257 if (ok)
18258 pop_namespace ();
18259 /* Look for the final `}'. */
18260 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
18263 /* Parse a namespace-body.
18265 namespace-body:
18266 declaration-seq [opt] */
18268 static void
18269 cp_parser_namespace_body (cp_parser* parser)
18271 cp_parser_declaration_seq_opt (parser);
18274 /* Parse a namespace-alias-definition.
18276 namespace-alias-definition:
18277 namespace identifier = qualified-namespace-specifier ; */
18279 static void
18280 cp_parser_namespace_alias_definition (cp_parser* parser)
18282 tree identifier;
18283 tree namespace_specifier;
18285 cp_token *token = cp_lexer_peek_token (parser->lexer);
18287 /* Look for the `namespace' keyword. */
18288 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
18289 /* Look for the identifier. */
18290 identifier = cp_parser_identifier (parser);
18291 if (identifier == error_mark_node)
18292 return;
18293 /* Look for the `=' token. */
18294 if (!cp_parser_uncommitted_to_tentative_parse_p (parser)
18295 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
18297 error_at (token->location, "%<namespace%> definition is not allowed here");
18298 /* Skip the definition. */
18299 cp_lexer_consume_token (parser->lexer);
18300 if (cp_parser_skip_to_closing_brace (parser))
18301 cp_lexer_consume_token (parser->lexer);
18302 return;
18304 cp_parser_require (parser, CPP_EQ, RT_EQ);
18305 /* Look for the qualified-namespace-specifier. */
18306 namespace_specifier
18307 = cp_parser_qualified_namespace_specifier (parser);
18308 /* Look for the `;' token. */
18309 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
18311 /* Register the alias in the symbol table. */
18312 do_namespace_alias (identifier, namespace_specifier);
18315 /* Parse a qualified-namespace-specifier.
18317 qualified-namespace-specifier:
18318 :: [opt] nested-name-specifier [opt] namespace-name
18320 Returns a NAMESPACE_DECL corresponding to the specified
18321 namespace. */
18323 static tree
18324 cp_parser_qualified_namespace_specifier (cp_parser* parser)
18326 /* Look for the optional `::'. */
18327 cp_parser_global_scope_opt (parser,
18328 /*current_scope_valid_p=*/false);
18330 /* Look for the optional nested-name-specifier. */
18331 cp_parser_nested_name_specifier_opt (parser,
18332 /*typename_keyword_p=*/false,
18333 /*check_dependency_p=*/true,
18334 /*type_p=*/false,
18335 /*is_declaration=*/true);
18337 return cp_parser_namespace_name (parser);
18340 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
18341 access declaration.
18343 using-declaration:
18344 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
18345 using :: unqualified-id ;
18347 access-declaration:
18348 qualified-id ;
18352 static bool
18353 cp_parser_using_declaration (cp_parser* parser,
18354 bool access_declaration_p)
18356 cp_token *token;
18357 bool typename_p = false;
18358 bool global_scope_p;
18359 tree decl;
18360 tree identifier;
18361 tree qscope;
18362 int oldcount = errorcount;
18363 cp_token *diag_token = NULL;
18365 if (access_declaration_p)
18367 diag_token = cp_lexer_peek_token (parser->lexer);
18368 cp_parser_parse_tentatively (parser);
18370 else
18372 /* Look for the `using' keyword. */
18373 cp_parser_require_keyword (parser, RID_USING, RT_USING);
18375 again:
18376 /* Peek at the next token. */
18377 token = cp_lexer_peek_token (parser->lexer);
18378 /* See if it's `typename'. */
18379 if (token->keyword == RID_TYPENAME)
18381 /* Remember that we've seen it. */
18382 typename_p = true;
18383 /* Consume the `typename' token. */
18384 cp_lexer_consume_token (parser->lexer);
18388 /* Look for the optional global scope qualification. */
18389 global_scope_p
18390 = (cp_parser_global_scope_opt (parser,
18391 /*current_scope_valid_p=*/false)
18392 != NULL_TREE);
18394 /* If we saw `typename', or didn't see `::', then there must be a
18395 nested-name-specifier present. */
18396 if (typename_p || !global_scope_p)
18398 qscope = cp_parser_nested_name_specifier (parser, typename_p,
18399 /*check_dependency_p=*/true,
18400 /*type_p=*/false,
18401 /*is_declaration=*/true);
18402 if (!qscope && !cp_parser_uncommitted_to_tentative_parse_p (parser))
18404 cp_parser_skip_to_end_of_block_or_statement (parser);
18405 return false;
18408 /* Otherwise, we could be in either of the two productions. In that
18409 case, treat the nested-name-specifier as optional. */
18410 else
18411 qscope = cp_parser_nested_name_specifier_opt (parser,
18412 /*typename_keyword_p=*/false,
18413 /*check_dependency_p=*/true,
18414 /*type_p=*/false,
18415 /*is_declaration=*/true);
18416 if (!qscope)
18417 qscope = global_namespace;
18418 else if (UNSCOPED_ENUM_P (qscope))
18419 qscope = CP_TYPE_CONTEXT (qscope);
18421 if (access_declaration_p && cp_parser_error_occurred (parser))
18422 /* Something has already gone wrong; there's no need to parse
18423 further. Since an error has occurred, the return value of
18424 cp_parser_parse_definitely will be false, as required. */
18425 return cp_parser_parse_definitely (parser);
18427 token = cp_lexer_peek_token (parser->lexer);
18428 /* Parse the unqualified-id. */
18429 identifier = cp_parser_unqualified_id (parser,
18430 /*template_keyword_p=*/false,
18431 /*check_dependency_p=*/true,
18432 /*declarator_p=*/true,
18433 /*optional_p=*/false);
18435 if (access_declaration_p)
18437 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
18438 cp_parser_simulate_error (parser);
18439 if (!cp_parser_parse_definitely (parser))
18440 return false;
18442 else if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
18444 cp_token *ell = cp_lexer_consume_token (parser->lexer);
18445 if (cxx_dialect < cxx1z
18446 && !in_system_header_at (ell->location))
18447 pedwarn (ell->location, 0,
18448 "pack expansion in using-declaration only available "
18449 "with -std=c++1z or -std=gnu++1z");
18450 qscope = make_pack_expansion (qscope);
18453 /* The function we call to handle a using-declaration is different
18454 depending on what scope we are in. */
18455 if (qscope == error_mark_node || identifier == error_mark_node)
18457 else if (!identifier_p (identifier)
18458 && TREE_CODE (identifier) != BIT_NOT_EXPR)
18459 /* [namespace.udecl]
18461 A using declaration shall not name a template-id. */
18462 error_at (token->location,
18463 "a template-id may not appear in a using-declaration");
18464 else
18466 if (at_class_scope_p ())
18468 /* Create the USING_DECL. */
18469 decl = do_class_using_decl (qscope, identifier);
18471 if (decl && typename_p)
18472 USING_DECL_TYPENAME_P (decl) = 1;
18474 if (check_for_bare_parameter_packs (decl))
18476 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
18477 return false;
18479 else
18480 /* Add it to the list of members in this class. */
18481 finish_member_declaration (decl);
18483 else
18485 decl = cp_parser_lookup_name_simple (parser,
18486 identifier,
18487 token->location);
18488 if (decl == error_mark_node)
18489 cp_parser_name_lookup_error (parser, identifier,
18490 decl, NLE_NULL,
18491 token->location);
18492 else if (check_for_bare_parameter_packs (decl))
18494 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
18495 return false;
18497 else if (!at_namespace_scope_p ())
18498 do_local_using_decl (decl, qscope, identifier);
18499 else
18500 do_toplevel_using_decl (decl, qscope, identifier);
18504 if (!access_declaration_p
18505 && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
18507 cp_token *comma = cp_lexer_consume_token (parser->lexer);
18508 if (cxx_dialect < cxx1z)
18509 pedwarn (comma->location, 0,
18510 "comma-separated list in using-declaration only available "
18511 "with -std=c++1z or -std=gnu++1z");
18512 goto again;
18515 /* Look for the final `;'. */
18516 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
18518 if (access_declaration_p && errorcount == oldcount)
18519 warning_at (diag_token->location, OPT_Wdeprecated,
18520 "access declarations are deprecated "
18521 "in favour of using-declarations; "
18522 "suggestion: add the %<using%> keyword");
18524 return true;
18527 /* Parse an alias-declaration.
18529 alias-declaration:
18530 using identifier attribute-specifier-seq [opt] = type-id */
18532 static tree
18533 cp_parser_alias_declaration (cp_parser* parser)
18535 tree id, type, decl, pushed_scope = NULL_TREE, attributes;
18536 location_t id_location;
18537 cp_declarator *declarator;
18538 cp_decl_specifier_seq decl_specs;
18539 bool member_p;
18540 const char *saved_message = NULL;
18542 /* Look for the `using' keyword. */
18543 cp_token *using_token
18544 = cp_parser_require_keyword (parser, RID_USING, RT_USING);
18545 if (using_token == NULL)
18546 return error_mark_node;
18548 id_location = cp_lexer_peek_token (parser->lexer)->location;
18549 id = cp_parser_identifier (parser);
18550 if (id == error_mark_node)
18551 return error_mark_node;
18553 cp_token *attrs_token = cp_lexer_peek_token (parser->lexer);
18554 attributes = cp_parser_attributes_opt (parser);
18555 if (attributes == error_mark_node)
18556 return error_mark_node;
18558 cp_parser_require (parser, CPP_EQ, RT_EQ);
18560 if (cp_parser_error_occurred (parser))
18561 return error_mark_node;
18563 cp_parser_commit_to_tentative_parse (parser);
18565 /* Now we are going to parse the type-id of the declaration. */
18568 [dcl.type]/3 says:
18570 "A type-specifier-seq shall not define a class or enumeration
18571 unless it appears in the type-id of an alias-declaration (7.1.3) that
18572 is not the declaration of a template-declaration."
18574 In other words, if we currently are in an alias template, the
18575 type-id should not define a type.
18577 So let's set parser->type_definition_forbidden_message in that
18578 case; cp_parser_check_type_definition (called by
18579 cp_parser_class_specifier) will then emit an error if a type is
18580 defined in the type-id. */
18581 if (parser->num_template_parameter_lists)
18583 saved_message = parser->type_definition_forbidden_message;
18584 parser->type_definition_forbidden_message =
18585 G_("types may not be defined in alias template declarations");
18588 type = cp_parser_type_id (parser);
18590 /* Restore the error message if need be. */
18591 if (parser->num_template_parameter_lists)
18592 parser->type_definition_forbidden_message = saved_message;
18594 if (type == error_mark_node
18595 || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
18597 cp_parser_skip_to_end_of_block_or_statement (parser);
18598 return error_mark_node;
18601 /* A typedef-name can also be introduced by an alias-declaration. The
18602 identifier following the using keyword becomes a typedef-name. It has
18603 the same semantics as if it were introduced by the typedef
18604 specifier. In particular, it does not define a new type and it shall
18605 not appear in the type-id. */
18607 clear_decl_specs (&decl_specs);
18608 decl_specs.type = type;
18609 if (attributes != NULL_TREE)
18611 decl_specs.attributes = attributes;
18612 set_and_check_decl_spec_loc (&decl_specs,
18613 ds_attribute,
18614 attrs_token);
18616 set_and_check_decl_spec_loc (&decl_specs,
18617 ds_typedef,
18618 using_token);
18619 set_and_check_decl_spec_loc (&decl_specs,
18620 ds_alias,
18621 using_token);
18623 declarator = make_id_declarator (NULL_TREE, id, sfk_none);
18624 declarator->id_loc = id_location;
18626 member_p = at_class_scope_p ();
18627 if (member_p)
18628 decl = grokfield (declarator, &decl_specs, NULL_TREE, false,
18629 NULL_TREE, attributes);
18630 else
18631 decl = start_decl (declarator, &decl_specs, 0,
18632 attributes, NULL_TREE, &pushed_scope);
18633 if (decl == error_mark_node)
18634 return decl;
18636 // Attach constraints to the alias declaration.
18637 if (flag_concepts && current_template_parms)
18639 tree reqs = TEMPLATE_PARMS_CONSTRAINTS (current_template_parms);
18640 tree constr = build_constraints (reqs, NULL_TREE);
18641 set_constraints (decl, constr);
18644 cp_finish_decl (decl, NULL_TREE, 0, NULL_TREE, 0);
18646 if (pushed_scope)
18647 pop_scope (pushed_scope);
18649 /* If decl is a template, return its TEMPLATE_DECL so that it gets
18650 added into the symbol table; otherwise, return the TYPE_DECL. */
18651 if (DECL_LANG_SPECIFIC (decl)
18652 && DECL_TEMPLATE_INFO (decl)
18653 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl)))
18655 decl = DECL_TI_TEMPLATE (decl);
18656 if (member_p)
18657 check_member_template (decl);
18660 return decl;
18663 /* Parse a using-directive.
18665 using-directive:
18666 using namespace :: [opt] nested-name-specifier [opt]
18667 namespace-name ; */
18669 static void
18670 cp_parser_using_directive (cp_parser* parser)
18672 tree namespace_decl;
18673 tree attribs;
18675 /* Look for the `using' keyword. */
18676 cp_parser_require_keyword (parser, RID_USING, RT_USING);
18677 /* And the `namespace' keyword. */
18678 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
18679 /* Look for the optional `::' operator. */
18680 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
18681 /* And the optional nested-name-specifier. */
18682 cp_parser_nested_name_specifier_opt (parser,
18683 /*typename_keyword_p=*/false,
18684 /*check_dependency_p=*/true,
18685 /*type_p=*/false,
18686 /*is_declaration=*/true);
18687 /* Get the namespace being used. */
18688 namespace_decl = cp_parser_namespace_name (parser);
18689 /* And any specified attributes. */
18690 attribs = cp_parser_attributes_opt (parser);
18691 /* Update the symbol table. */
18692 parse_using_directive (namespace_decl, attribs);
18693 /* Look for the final `;'. */
18694 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
18697 /* Parse an asm-definition.
18699 asm-definition:
18700 asm ( string-literal ) ;
18702 GNU Extension:
18704 asm-definition:
18705 asm volatile [opt] ( string-literal ) ;
18706 asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
18707 asm volatile [opt] ( string-literal : asm-operand-list [opt]
18708 : asm-operand-list [opt] ) ;
18709 asm volatile [opt] ( string-literal : asm-operand-list [opt]
18710 : asm-operand-list [opt]
18711 : asm-clobber-list [opt] ) ;
18712 asm volatile [opt] goto ( string-literal : : asm-operand-list [opt]
18713 : asm-clobber-list [opt]
18714 : asm-goto-list ) ; */
18716 static void
18717 cp_parser_asm_definition (cp_parser* parser)
18719 tree string;
18720 tree outputs = NULL_TREE;
18721 tree inputs = NULL_TREE;
18722 tree clobbers = NULL_TREE;
18723 tree labels = NULL_TREE;
18724 tree asm_stmt;
18725 bool volatile_p = false;
18726 bool extended_p = false;
18727 bool invalid_inputs_p = false;
18728 bool invalid_outputs_p = false;
18729 bool goto_p = false;
18730 required_token missing = RT_NONE;
18732 /* Look for the `asm' keyword. */
18733 cp_parser_require_keyword (parser, RID_ASM, RT_ASM);
18735 if (parser->in_function_body
18736 && DECL_DECLARED_CONSTEXPR_P (current_function_decl))
18738 error ("%<asm%> in %<constexpr%> function");
18739 cp_function_chain->invalid_constexpr = true;
18742 /* See if the next token is `volatile'. */
18743 if (cp_parser_allow_gnu_extensions_p (parser)
18744 && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
18746 /* Remember that we saw the `volatile' keyword. */
18747 volatile_p = true;
18748 /* Consume the token. */
18749 cp_lexer_consume_token (parser->lexer);
18751 if (cp_parser_allow_gnu_extensions_p (parser)
18752 && parser->in_function_body
18753 && cp_lexer_next_token_is_keyword (parser->lexer, RID_GOTO))
18755 /* Remember that we saw the `goto' keyword. */
18756 goto_p = true;
18757 /* Consume the token. */
18758 cp_lexer_consume_token (parser->lexer);
18760 /* Look for the opening `('. */
18761 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
18762 return;
18763 /* Look for the string. */
18764 string = cp_parser_string_literal (parser, false, false);
18765 if (string == error_mark_node)
18767 cp_parser_skip_to_closing_parenthesis (parser, true, false,
18768 /*consume_paren=*/true);
18769 return;
18772 /* If we're allowing GNU extensions, check for the extended assembly
18773 syntax. Unfortunately, the `:' tokens need not be separated by
18774 a space in C, and so, for compatibility, we tolerate that here
18775 too. Doing that means that we have to treat the `::' operator as
18776 two `:' tokens. */
18777 if (cp_parser_allow_gnu_extensions_p (parser)
18778 && parser->in_function_body
18779 && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
18780 || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
18782 bool inputs_p = false;
18783 bool clobbers_p = false;
18784 bool labels_p = false;
18786 /* The extended syntax was used. */
18787 extended_p = true;
18789 /* Look for outputs. */
18790 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
18792 /* Consume the `:'. */
18793 cp_lexer_consume_token (parser->lexer);
18794 /* Parse the output-operands. */
18795 if (cp_lexer_next_token_is_not (parser->lexer,
18796 CPP_COLON)
18797 && cp_lexer_next_token_is_not (parser->lexer,
18798 CPP_SCOPE)
18799 && cp_lexer_next_token_is_not (parser->lexer,
18800 CPP_CLOSE_PAREN)
18801 && !goto_p)
18803 outputs = cp_parser_asm_operand_list (parser);
18804 if (outputs == error_mark_node)
18805 invalid_outputs_p = true;
18808 /* If the next token is `::', there are no outputs, and the
18809 next token is the beginning of the inputs. */
18810 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
18811 /* The inputs are coming next. */
18812 inputs_p = true;
18814 /* Look for inputs. */
18815 if (inputs_p
18816 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
18818 /* Consume the `:' or `::'. */
18819 cp_lexer_consume_token (parser->lexer);
18820 /* Parse the output-operands. */
18821 if (cp_lexer_next_token_is_not (parser->lexer,
18822 CPP_COLON)
18823 && cp_lexer_next_token_is_not (parser->lexer,
18824 CPP_SCOPE)
18825 && cp_lexer_next_token_is_not (parser->lexer,
18826 CPP_CLOSE_PAREN))
18828 inputs = cp_parser_asm_operand_list (parser);
18829 if (inputs == error_mark_node)
18830 invalid_inputs_p = true;
18833 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
18834 /* The clobbers are coming next. */
18835 clobbers_p = true;
18837 /* Look for clobbers. */
18838 if (clobbers_p
18839 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
18841 clobbers_p = true;
18842 /* Consume the `:' or `::'. */
18843 cp_lexer_consume_token (parser->lexer);
18844 /* Parse the clobbers. */
18845 if (cp_lexer_next_token_is_not (parser->lexer,
18846 CPP_COLON)
18847 && cp_lexer_next_token_is_not (parser->lexer,
18848 CPP_CLOSE_PAREN))
18849 clobbers = cp_parser_asm_clobber_list (parser);
18851 else if (goto_p
18852 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
18853 /* The labels are coming next. */
18854 labels_p = true;
18856 /* Look for labels. */
18857 if (labels_p
18858 || (goto_p && cp_lexer_next_token_is (parser->lexer, CPP_COLON)))
18860 labels_p = true;
18861 /* Consume the `:' or `::'. */
18862 cp_lexer_consume_token (parser->lexer);
18863 /* Parse the labels. */
18864 labels = cp_parser_asm_label_list (parser);
18867 if (goto_p && !labels_p)
18868 missing = clobbers_p ? RT_COLON : RT_COLON_SCOPE;
18870 else if (goto_p)
18871 missing = RT_COLON_SCOPE;
18873 /* Look for the closing `)'. */
18874 if (!cp_parser_require (parser, missing ? CPP_COLON : CPP_CLOSE_PAREN,
18875 missing ? missing : RT_CLOSE_PAREN))
18876 cp_parser_skip_to_closing_parenthesis (parser, true, false,
18877 /*consume_paren=*/true);
18878 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
18880 if (!invalid_inputs_p && !invalid_outputs_p)
18882 /* Create the ASM_EXPR. */
18883 if (parser->in_function_body)
18885 asm_stmt = finish_asm_stmt (volatile_p, string, outputs,
18886 inputs, clobbers, labels);
18887 /* If the extended syntax was not used, mark the ASM_EXPR. */
18888 if (!extended_p)
18890 tree temp = asm_stmt;
18891 if (TREE_CODE (temp) == CLEANUP_POINT_EXPR)
18892 temp = TREE_OPERAND (temp, 0);
18894 ASM_INPUT_P (temp) = 1;
18897 else
18898 symtab->finalize_toplevel_asm (string);
18902 /* Given the type TYPE of a declaration with declarator DECLARATOR, return the
18903 type that comes from the decl-specifier-seq. */
18905 static tree
18906 strip_declarator_types (tree type, cp_declarator *declarator)
18908 for (cp_declarator *d = declarator; d;)
18909 switch (d->kind)
18911 case cdk_id:
18912 case cdk_decomp:
18913 case cdk_error:
18914 d = NULL;
18915 break;
18917 default:
18918 if (TYPE_PTRMEMFUNC_P (type))
18919 type = TYPE_PTRMEMFUNC_FN_TYPE (type);
18920 type = TREE_TYPE (type);
18921 d = d->declarator;
18922 break;
18925 return type;
18928 /* Declarators [gram.dcl.decl] */
18930 /* Parse an init-declarator.
18932 init-declarator:
18933 declarator initializer [opt]
18935 GNU Extension:
18937 init-declarator:
18938 declarator asm-specification [opt] attributes [opt] initializer [opt]
18940 function-definition:
18941 decl-specifier-seq [opt] declarator ctor-initializer [opt]
18942 function-body
18943 decl-specifier-seq [opt] declarator function-try-block
18945 GNU Extension:
18947 function-definition:
18948 __extension__ function-definition
18950 TM Extension:
18952 function-definition:
18953 decl-specifier-seq [opt] declarator function-transaction-block
18955 The DECL_SPECIFIERS apply to this declarator. Returns a
18956 representation of the entity declared. If MEMBER_P is TRUE, then
18957 this declarator appears in a class scope. The new DECL created by
18958 this declarator is returned.
18960 The CHECKS are access checks that should be performed once we know
18961 what entity is being declared (and, therefore, what classes have
18962 befriended it).
18964 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
18965 for a function-definition here as well. If the declarator is a
18966 declarator for a function-definition, *FUNCTION_DEFINITION_P will
18967 be TRUE upon return. By that point, the function-definition will
18968 have been completely parsed.
18970 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
18971 is FALSE.
18973 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
18974 parsed declaration if it is an uninitialized single declarator not followed
18975 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
18976 if present, will not be consumed. If returned, this declarator will be
18977 created with SD_INITIALIZED but will not call cp_finish_decl.
18979 If INIT_LOC is not NULL, and *INIT_LOC is equal to UNKNOWN_LOCATION,
18980 and there is an initializer, the pointed location_t is set to the
18981 location of the '=' or `(', or '{' in C++11 token introducing the
18982 initializer. */
18984 static tree
18985 cp_parser_init_declarator (cp_parser* parser,
18986 cp_decl_specifier_seq *decl_specifiers,
18987 vec<deferred_access_check, va_gc> *checks,
18988 bool function_definition_allowed_p,
18989 bool member_p,
18990 int declares_class_or_enum,
18991 bool* function_definition_p,
18992 tree* maybe_range_for_decl,
18993 location_t* init_loc,
18994 tree* auto_result)
18996 cp_token *token = NULL, *asm_spec_start_token = NULL,
18997 *attributes_start_token = NULL;
18998 cp_declarator *declarator;
18999 tree prefix_attributes;
19000 tree attributes = NULL;
19001 tree asm_specification;
19002 tree initializer;
19003 tree decl = NULL_TREE;
19004 tree scope;
19005 int is_initialized;
19006 /* Only valid if IS_INITIALIZED is true. In that case, CPP_EQ if
19007 initialized with "= ..", CPP_OPEN_PAREN if initialized with
19008 "(...)". */
19009 enum cpp_ttype initialization_kind;
19010 bool is_direct_init = false;
19011 bool is_non_constant_init;
19012 int ctor_dtor_or_conv_p;
19013 bool friend_p = cp_parser_friend_p (decl_specifiers);
19014 tree pushed_scope = NULL_TREE;
19015 bool range_for_decl_p = false;
19016 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
19017 location_t tmp_init_loc = UNKNOWN_LOCATION;
19019 /* Gather the attributes that were provided with the
19020 decl-specifiers. */
19021 prefix_attributes = decl_specifiers->attributes;
19023 /* Assume that this is not the declarator for a function
19024 definition. */
19025 if (function_definition_p)
19026 *function_definition_p = false;
19028 /* Default arguments are only permitted for function parameters. */
19029 if (decl_spec_seq_has_spec_p (decl_specifiers, ds_typedef))
19030 parser->default_arg_ok_p = false;
19032 /* Defer access checks while parsing the declarator; we cannot know
19033 what names are accessible until we know what is being
19034 declared. */
19035 resume_deferring_access_checks ();
19037 token = cp_lexer_peek_token (parser->lexer);
19039 /* Parse the declarator. */
19040 declarator
19041 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
19042 &ctor_dtor_or_conv_p,
19043 /*parenthesized_p=*/NULL,
19044 member_p, friend_p);
19045 /* Gather up the deferred checks. */
19046 stop_deferring_access_checks ();
19048 parser->default_arg_ok_p = saved_default_arg_ok_p;
19050 /* If the DECLARATOR was erroneous, there's no need to go
19051 further. */
19052 if (declarator == cp_error_declarator)
19053 return error_mark_node;
19055 /* Check that the number of template-parameter-lists is OK. */
19056 if (!cp_parser_check_declarator_template_parameters (parser, declarator,
19057 token->location))
19058 return error_mark_node;
19060 if (declares_class_or_enum & 2)
19061 cp_parser_check_for_definition_in_return_type (declarator,
19062 decl_specifiers->type,
19063 decl_specifiers->locations[ds_type_spec]);
19065 /* Figure out what scope the entity declared by the DECLARATOR is
19066 located in. `grokdeclarator' sometimes changes the scope, so
19067 we compute it now. */
19068 scope = get_scope_of_declarator (declarator);
19070 /* Perform any lookups in the declared type which were thought to be
19071 dependent, but are not in the scope of the declarator. */
19072 decl_specifiers->type
19073 = maybe_update_decl_type (decl_specifiers->type, scope);
19075 /* If we're allowing GNU extensions, look for an
19076 asm-specification. */
19077 if (cp_parser_allow_gnu_extensions_p (parser))
19079 /* Look for an asm-specification. */
19080 asm_spec_start_token = cp_lexer_peek_token (parser->lexer);
19081 asm_specification = cp_parser_asm_specification_opt (parser);
19083 else
19084 asm_specification = NULL_TREE;
19086 /* Look for attributes. */
19087 attributes_start_token = cp_lexer_peek_token (parser->lexer);
19088 attributes = cp_parser_attributes_opt (parser);
19090 /* Peek at the next token. */
19091 token = cp_lexer_peek_token (parser->lexer);
19093 bool bogus_implicit_tmpl = false;
19095 if (function_declarator_p (declarator))
19097 /* Handle C++17 deduction guides. */
19098 if (!decl_specifiers->type
19099 && ctor_dtor_or_conv_p <= 0
19100 && cxx_dialect >= cxx1z)
19102 cp_declarator *id = get_id_declarator (declarator);
19103 tree name = id->u.id.unqualified_name;
19104 parser->scope = id->u.id.qualifying_scope;
19105 tree tmpl = cp_parser_lookup_name_simple (parser, name, id->id_loc);
19106 if (tmpl
19107 && (DECL_CLASS_TEMPLATE_P (tmpl)
19108 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl)))
19110 id->u.id.unqualified_name = dguide_name (tmpl);
19111 id->u.id.sfk = sfk_deduction_guide;
19112 ctor_dtor_or_conv_p = 1;
19116 /* Check to see if the token indicates the start of a
19117 function-definition. */
19118 if (cp_parser_token_starts_function_definition_p (token))
19120 if (!function_definition_allowed_p)
19122 /* If a function-definition should not appear here, issue an
19123 error message. */
19124 cp_parser_error (parser,
19125 "a function-definition is not allowed here");
19126 return error_mark_node;
19129 location_t func_brace_location
19130 = cp_lexer_peek_token (parser->lexer)->location;
19132 /* Neither attributes nor an asm-specification are allowed
19133 on a function-definition. */
19134 if (asm_specification)
19135 error_at (asm_spec_start_token->location,
19136 "an asm-specification is not allowed "
19137 "on a function-definition");
19138 if (attributes)
19139 error_at (attributes_start_token->location,
19140 "attributes are not allowed "
19141 "on a function-definition");
19142 /* This is a function-definition. */
19143 *function_definition_p = true;
19145 /* Parse the function definition. */
19146 if (member_p)
19147 decl = cp_parser_save_member_function_body (parser,
19148 decl_specifiers,
19149 declarator,
19150 prefix_attributes);
19151 else
19152 decl =
19153 (cp_parser_function_definition_from_specifiers_and_declarator
19154 (parser, decl_specifiers, prefix_attributes, declarator));
19156 if (decl != error_mark_node && DECL_STRUCT_FUNCTION (decl))
19158 /* This is where the prologue starts... */
19159 DECL_STRUCT_FUNCTION (decl)->function_start_locus
19160 = func_brace_location;
19163 return decl;
19166 else if (parser->fully_implicit_function_template_p)
19168 /* A non-template declaration involving a function parameter list
19169 containing an implicit template parameter will be made into a
19170 template. If the resulting declaration is not going to be an
19171 actual function then finish the template scope here to prevent it.
19172 An error message will be issued once we have a decl to talk about.
19174 FIXME probably we should do type deduction rather than create an
19175 implicit template, but the standard currently doesn't allow it. */
19176 bogus_implicit_tmpl = true;
19177 finish_fully_implicit_template (parser, NULL_TREE);
19180 /* [dcl.dcl]
19182 Only in function declarations for constructors, destructors, type
19183 conversions, and deduction guides can the decl-specifier-seq be omitted.
19185 We explicitly postpone this check past the point where we handle
19186 function-definitions because we tolerate function-definitions
19187 that are missing their return types in some modes. */
19188 if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
19190 cp_parser_error (parser,
19191 "expected constructor, destructor, or type conversion");
19192 return error_mark_node;
19195 /* An `=' or an `(', or an '{' in C++0x, indicates an initializer. */
19196 if (token->type == CPP_EQ
19197 || token->type == CPP_OPEN_PAREN
19198 || token->type == CPP_OPEN_BRACE)
19200 is_initialized = SD_INITIALIZED;
19201 initialization_kind = token->type;
19202 if (maybe_range_for_decl)
19203 *maybe_range_for_decl = error_mark_node;
19204 tmp_init_loc = token->location;
19205 if (init_loc && *init_loc == UNKNOWN_LOCATION)
19206 *init_loc = tmp_init_loc;
19208 if (token->type == CPP_EQ
19209 && function_declarator_p (declarator))
19211 cp_token *t2 = cp_lexer_peek_nth_token (parser->lexer, 2);
19212 if (t2->keyword == RID_DEFAULT)
19213 is_initialized = SD_DEFAULTED;
19214 else if (t2->keyword == RID_DELETE)
19215 is_initialized = SD_DELETED;
19218 else
19220 /* If the init-declarator isn't initialized and isn't followed by a
19221 `,' or `;', it's not a valid init-declarator. */
19222 if (token->type != CPP_COMMA
19223 && token->type != CPP_SEMICOLON)
19225 if (maybe_range_for_decl && *maybe_range_for_decl != error_mark_node)
19226 range_for_decl_p = true;
19227 else
19229 if (!maybe_range_for_decl)
19230 cp_parser_error (parser, "expected initializer");
19231 return error_mark_node;
19234 is_initialized = SD_UNINITIALIZED;
19235 initialization_kind = CPP_EOF;
19238 /* Because start_decl has side-effects, we should only call it if we
19239 know we're going ahead. By this point, we know that we cannot
19240 possibly be looking at any other construct. */
19241 cp_parser_commit_to_tentative_parse (parser);
19243 /* Enter the newly declared entry in the symbol table. If we're
19244 processing a declaration in a class-specifier, we wait until
19245 after processing the initializer. */
19246 if (!member_p)
19248 if (parser->in_unbraced_linkage_specification_p)
19249 decl_specifiers->storage_class = sc_extern;
19250 decl = start_decl (declarator, decl_specifiers,
19251 range_for_decl_p? SD_INITIALIZED : is_initialized,
19252 attributes, prefix_attributes, &pushed_scope);
19253 cp_finalize_omp_declare_simd (parser, decl);
19254 cp_finalize_oacc_routine (parser, decl, false);
19255 /* Adjust location of decl if declarator->id_loc is more appropriate:
19256 set, and decl wasn't merged with another decl, in which case its
19257 location would be different from input_location, and more accurate. */
19258 if (DECL_P (decl)
19259 && declarator->id_loc != UNKNOWN_LOCATION
19260 && DECL_SOURCE_LOCATION (decl) == input_location)
19261 DECL_SOURCE_LOCATION (decl) = declarator->id_loc;
19263 else if (scope)
19264 /* Enter the SCOPE. That way unqualified names appearing in the
19265 initializer will be looked up in SCOPE. */
19266 pushed_scope = push_scope (scope);
19268 /* Perform deferred access control checks, now that we know in which
19269 SCOPE the declared entity resides. */
19270 if (!member_p && decl)
19272 tree saved_current_function_decl = NULL_TREE;
19274 /* If the entity being declared is a function, pretend that we
19275 are in its scope. If it is a `friend', it may have access to
19276 things that would not otherwise be accessible. */
19277 if (TREE_CODE (decl) == FUNCTION_DECL)
19279 saved_current_function_decl = current_function_decl;
19280 current_function_decl = decl;
19283 /* Perform access checks for template parameters. */
19284 cp_parser_perform_template_parameter_access_checks (checks);
19286 /* Perform the access control checks for the declarator and the
19287 decl-specifiers. */
19288 perform_deferred_access_checks (tf_warning_or_error);
19290 /* Restore the saved value. */
19291 if (TREE_CODE (decl) == FUNCTION_DECL)
19292 current_function_decl = saved_current_function_decl;
19295 /* Parse the initializer. */
19296 initializer = NULL_TREE;
19297 is_direct_init = false;
19298 is_non_constant_init = true;
19299 if (is_initialized)
19301 if (function_declarator_p (declarator))
19303 if (initialization_kind == CPP_EQ)
19304 initializer = cp_parser_pure_specifier (parser);
19305 else
19307 /* If the declaration was erroneous, we don't really
19308 know what the user intended, so just silently
19309 consume the initializer. */
19310 if (decl != error_mark_node)
19311 error_at (tmp_init_loc, "initializer provided for function");
19312 cp_parser_skip_to_closing_parenthesis (parser,
19313 /*recovering=*/true,
19314 /*or_comma=*/false,
19315 /*consume_paren=*/true);
19318 else
19320 /* We want to record the extra mangling scope for in-class
19321 initializers of class members and initializers of static data
19322 member templates. The former involves deferring
19323 parsing of the initializer until end of class as with default
19324 arguments. So right here we only handle the latter. */
19325 if (!member_p && processing_template_decl)
19326 start_lambda_scope (decl);
19327 initializer = cp_parser_initializer (parser,
19328 &is_direct_init,
19329 &is_non_constant_init);
19330 if (!member_p && processing_template_decl)
19331 finish_lambda_scope ();
19332 if (initializer == error_mark_node)
19333 cp_parser_skip_to_end_of_statement (parser);
19337 /* The old parser allows attributes to appear after a parenthesized
19338 initializer. Mark Mitchell proposed removing this functionality
19339 on the GCC mailing lists on 2002-08-13. This parser accepts the
19340 attributes -- but ignores them. */
19341 if (cp_parser_allow_gnu_extensions_p (parser)
19342 && initialization_kind == CPP_OPEN_PAREN)
19343 if (cp_parser_attributes_opt (parser))
19344 warning (OPT_Wattributes,
19345 "attributes after parenthesized initializer ignored");
19347 /* And now complain about a non-function implicit template. */
19348 if (bogus_implicit_tmpl && decl != error_mark_node)
19349 error_at (DECL_SOURCE_LOCATION (decl),
19350 "non-function %qD declared as implicit template", decl);
19352 /* For an in-class declaration, use `grokfield' to create the
19353 declaration. */
19354 if (member_p)
19356 if (pushed_scope)
19358 pop_scope (pushed_scope);
19359 pushed_scope = NULL_TREE;
19361 decl = grokfield (declarator, decl_specifiers,
19362 initializer, !is_non_constant_init,
19363 /*asmspec=*/NULL_TREE,
19364 chainon (attributes, prefix_attributes));
19365 if (decl && TREE_CODE (decl) == FUNCTION_DECL)
19366 cp_parser_save_default_args (parser, decl);
19367 cp_finalize_omp_declare_simd (parser, decl);
19368 cp_finalize_oacc_routine (parser, decl, false);
19371 /* Finish processing the declaration. But, skip member
19372 declarations. */
19373 if (!member_p && decl && decl != error_mark_node && !range_for_decl_p)
19375 cp_finish_decl (decl,
19376 initializer, !is_non_constant_init,
19377 asm_specification,
19378 /* If the initializer is in parentheses, then this is
19379 a direct-initialization, which means that an
19380 `explicit' constructor is OK. Otherwise, an
19381 `explicit' constructor cannot be used. */
19382 ((is_direct_init || !is_initialized)
19383 ? LOOKUP_NORMAL : LOOKUP_IMPLICIT));
19385 else if ((cxx_dialect != cxx98) && friend_p
19386 && decl && TREE_CODE (decl) == FUNCTION_DECL)
19387 /* Core issue #226 (C++0x only): A default template-argument
19388 shall not be specified in a friend class template
19389 declaration. */
19390 check_default_tmpl_args (decl, current_template_parms, /*is_primary=*/true,
19391 /*is_partial=*/false, /*is_friend_decl=*/1);
19393 if (!friend_p && pushed_scope)
19394 pop_scope (pushed_scope);
19396 if (function_declarator_p (declarator)
19397 && parser->fully_implicit_function_template_p)
19399 if (member_p)
19400 decl = finish_fully_implicit_template (parser, decl);
19401 else
19402 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
19405 if (auto_result && is_initialized && decl_specifiers->type
19406 && type_uses_auto (decl_specifiers->type))
19407 *auto_result = strip_declarator_types (TREE_TYPE (decl), declarator);
19409 return decl;
19412 /* Parse a declarator.
19414 declarator:
19415 direct-declarator
19416 ptr-operator declarator
19418 abstract-declarator:
19419 ptr-operator abstract-declarator [opt]
19420 direct-abstract-declarator
19422 GNU Extensions:
19424 declarator:
19425 attributes [opt] direct-declarator
19426 attributes [opt] ptr-operator declarator
19428 abstract-declarator:
19429 attributes [opt] ptr-operator abstract-declarator [opt]
19430 attributes [opt] direct-abstract-declarator
19432 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
19433 detect constructors, destructors, deduction guides, or conversion operators.
19434 It is set to -1 if the declarator is a name, and +1 if it is a
19435 function. Otherwise it is set to zero. Usually you just want to
19436 test for >0, but internally the negative value is used.
19438 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
19439 a decl-specifier-seq unless it declares a constructor, destructor,
19440 or conversion. It might seem that we could check this condition in
19441 semantic analysis, rather than parsing, but that makes it difficult
19442 to handle something like `f()'. We want to notice that there are
19443 no decl-specifiers, and therefore realize that this is an
19444 expression, not a declaration.)
19446 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
19447 the declarator is a direct-declarator of the form "(...)".
19449 MEMBER_P is true iff this declarator is a member-declarator.
19451 FRIEND_P is true iff this declarator is a friend. */
19453 static cp_declarator *
19454 cp_parser_declarator (cp_parser* parser,
19455 cp_parser_declarator_kind dcl_kind,
19456 int* ctor_dtor_or_conv_p,
19457 bool* parenthesized_p,
19458 bool member_p, bool friend_p)
19460 cp_declarator *declarator;
19461 enum tree_code code;
19462 cp_cv_quals cv_quals;
19463 tree class_type;
19464 tree gnu_attributes = NULL_TREE, std_attributes = NULL_TREE;
19466 /* Assume this is not a constructor, destructor, or type-conversion
19467 operator. */
19468 if (ctor_dtor_or_conv_p)
19469 *ctor_dtor_or_conv_p = 0;
19471 if (cp_parser_allow_gnu_extensions_p (parser))
19472 gnu_attributes = cp_parser_gnu_attributes_opt (parser);
19474 /* Check for the ptr-operator production. */
19475 cp_parser_parse_tentatively (parser);
19476 /* Parse the ptr-operator. */
19477 code = cp_parser_ptr_operator (parser,
19478 &class_type,
19479 &cv_quals,
19480 &std_attributes);
19482 /* If that worked, then we have a ptr-operator. */
19483 if (cp_parser_parse_definitely (parser))
19485 /* If a ptr-operator was found, then this declarator was not
19486 parenthesized. */
19487 if (parenthesized_p)
19488 *parenthesized_p = true;
19489 /* The dependent declarator is optional if we are parsing an
19490 abstract-declarator. */
19491 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
19492 cp_parser_parse_tentatively (parser);
19494 /* Parse the dependent declarator. */
19495 declarator = cp_parser_declarator (parser, dcl_kind,
19496 /*ctor_dtor_or_conv_p=*/NULL,
19497 /*parenthesized_p=*/NULL,
19498 /*member_p=*/false,
19499 friend_p);
19501 /* If we are parsing an abstract-declarator, we must handle the
19502 case where the dependent declarator is absent. */
19503 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
19504 && !cp_parser_parse_definitely (parser))
19505 declarator = NULL;
19507 declarator = cp_parser_make_indirect_declarator
19508 (code, class_type, cv_quals, declarator, std_attributes);
19510 /* Everything else is a direct-declarator. */
19511 else
19513 if (parenthesized_p)
19514 *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
19515 CPP_OPEN_PAREN);
19516 declarator = cp_parser_direct_declarator (parser, dcl_kind,
19517 ctor_dtor_or_conv_p,
19518 member_p, friend_p);
19521 if (gnu_attributes && declarator && declarator != cp_error_declarator)
19522 declarator->attributes = gnu_attributes;
19523 return declarator;
19526 /* Parse a direct-declarator or direct-abstract-declarator.
19528 direct-declarator:
19529 declarator-id
19530 direct-declarator ( parameter-declaration-clause )
19531 cv-qualifier-seq [opt]
19532 ref-qualifier [opt]
19533 exception-specification [opt]
19534 direct-declarator [ constant-expression [opt] ]
19535 ( declarator )
19537 direct-abstract-declarator:
19538 direct-abstract-declarator [opt]
19539 ( parameter-declaration-clause )
19540 cv-qualifier-seq [opt]
19541 ref-qualifier [opt]
19542 exception-specification [opt]
19543 direct-abstract-declarator [opt] [ constant-expression [opt] ]
19544 ( abstract-declarator )
19546 Returns a representation of the declarator. DCL_KIND is
19547 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
19548 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
19549 we are parsing a direct-declarator. It is
19550 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
19551 of ambiguity we prefer an abstract declarator, as per
19552 [dcl.ambig.res]. CTOR_DTOR_OR_CONV_P, MEMBER_P, and FRIEND_P are
19553 as for cp_parser_declarator. */
19555 static cp_declarator *
19556 cp_parser_direct_declarator (cp_parser* parser,
19557 cp_parser_declarator_kind dcl_kind,
19558 int* ctor_dtor_or_conv_p,
19559 bool member_p, bool friend_p)
19561 cp_token *token;
19562 cp_declarator *declarator = NULL;
19563 tree scope = NULL_TREE;
19564 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
19565 bool saved_in_declarator_p = parser->in_declarator_p;
19566 bool first = true;
19567 tree pushed_scope = NULL_TREE;
19569 while (true)
19571 /* Peek at the next token. */
19572 token = cp_lexer_peek_token (parser->lexer);
19573 if (token->type == CPP_OPEN_PAREN)
19575 /* This is either a parameter-declaration-clause, or a
19576 parenthesized declarator. When we know we are parsing a
19577 named declarator, it must be a parenthesized declarator
19578 if FIRST is true. For instance, `(int)' is a
19579 parameter-declaration-clause, with an omitted
19580 direct-abstract-declarator. But `((*))', is a
19581 parenthesized abstract declarator. Finally, when T is a
19582 template parameter `(T)' is a
19583 parameter-declaration-clause, and not a parenthesized
19584 named declarator.
19586 We first try and parse a parameter-declaration-clause,
19587 and then try a nested declarator (if FIRST is true).
19589 It is not an error for it not to be a
19590 parameter-declaration-clause, even when FIRST is
19591 false. Consider,
19593 int i (int);
19594 int i (3);
19596 The first is the declaration of a function while the
19597 second is the definition of a variable, including its
19598 initializer.
19600 Having seen only the parenthesis, we cannot know which of
19601 these two alternatives should be selected. Even more
19602 complex are examples like:
19604 int i (int (a));
19605 int i (int (3));
19607 The former is a function-declaration; the latter is a
19608 variable initialization.
19610 Thus again, we try a parameter-declaration-clause, and if
19611 that fails, we back out and return. */
19613 if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
19615 tree params;
19616 bool is_declarator = false;
19618 /* In a member-declarator, the only valid interpretation
19619 of a parenthesis is the start of a
19620 parameter-declaration-clause. (It is invalid to
19621 initialize a static data member with a parenthesized
19622 initializer; only the "=" form of initialization is
19623 permitted.) */
19624 if (!member_p)
19625 cp_parser_parse_tentatively (parser);
19627 /* Consume the `('. */
19628 cp_lexer_consume_token (parser->lexer);
19629 if (first)
19631 /* If this is going to be an abstract declarator, we're
19632 in a declarator and we can't have default args. */
19633 parser->default_arg_ok_p = false;
19634 parser->in_declarator_p = true;
19637 begin_scope (sk_function_parms, NULL_TREE);
19639 /* Parse the parameter-declaration-clause. */
19640 params = cp_parser_parameter_declaration_clause (parser);
19642 /* Consume the `)'. */
19643 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
19645 /* If all went well, parse the cv-qualifier-seq,
19646 ref-qualifier and the exception-specification. */
19647 if (member_p || cp_parser_parse_definitely (parser))
19649 cp_cv_quals cv_quals;
19650 cp_virt_specifiers virt_specifiers;
19651 cp_ref_qualifier ref_qual;
19652 tree exception_specification;
19653 tree late_return;
19654 tree attrs;
19655 bool memfn = (member_p || (pushed_scope
19656 && CLASS_TYPE_P (pushed_scope)));
19658 is_declarator = true;
19660 if (ctor_dtor_or_conv_p)
19661 *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
19662 first = false;
19664 /* Parse the cv-qualifier-seq. */
19665 cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
19666 /* Parse the ref-qualifier. */
19667 ref_qual = cp_parser_ref_qualifier_opt (parser);
19668 /* Parse the tx-qualifier. */
19669 tree tx_qual = cp_parser_tx_qualifier_opt (parser);
19670 /* And the exception-specification. */
19671 exception_specification
19672 = cp_parser_exception_specification_opt (parser);
19674 attrs = cp_parser_std_attribute_spec_seq (parser);
19676 /* In here, we handle cases where attribute is used after
19677 the function declaration. For example:
19678 void func (int x) __attribute__((vector(..))); */
19679 tree gnu_attrs = NULL_TREE;
19680 if (flag_cilkplus
19681 && cp_next_tokens_can_be_gnu_attribute_p (parser))
19683 cp_parser_parse_tentatively (parser);
19684 tree attr = cp_parser_gnu_attributes_opt (parser);
19685 if (cp_lexer_next_token_is_not (parser->lexer,
19686 CPP_SEMICOLON)
19687 && cp_lexer_next_token_is_not (parser->lexer,
19688 CPP_OPEN_BRACE))
19689 cp_parser_abort_tentative_parse (parser);
19690 else if (!cp_parser_parse_definitely (parser))
19692 else
19693 gnu_attrs = attr;
19695 tree requires_clause = NULL_TREE;
19696 late_return = (cp_parser_late_return_type_opt
19697 (parser, declarator, requires_clause,
19698 memfn ? cv_quals : -1));
19700 /* Parse the virt-specifier-seq. */
19701 virt_specifiers = cp_parser_virt_specifier_seq_opt (parser);
19703 /* Create the function-declarator. */
19704 declarator = make_call_declarator (declarator,
19705 params,
19706 cv_quals,
19707 virt_specifiers,
19708 ref_qual,
19709 tx_qual,
19710 exception_specification,
19711 late_return,
19712 requires_clause);
19713 declarator->std_attributes = attrs;
19714 declarator->attributes = gnu_attrs;
19715 /* Any subsequent parameter lists are to do with
19716 return type, so are not those of the declared
19717 function. */
19718 parser->default_arg_ok_p = false;
19721 /* Remove the function parms from scope. */
19722 pop_bindings_and_leave_scope ();
19724 if (is_declarator)
19725 /* Repeat the main loop. */
19726 continue;
19729 /* If this is the first, we can try a parenthesized
19730 declarator. */
19731 if (first)
19733 bool saved_in_type_id_in_expr_p;
19735 parser->default_arg_ok_p = saved_default_arg_ok_p;
19736 parser->in_declarator_p = saved_in_declarator_p;
19738 /* Consume the `('. */
19739 cp_lexer_consume_token (parser->lexer);
19740 /* Parse the nested declarator. */
19741 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
19742 parser->in_type_id_in_expr_p = true;
19743 declarator
19744 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
19745 /*parenthesized_p=*/NULL,
19746 member_p, friend_p);
19747 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
19748 first = false;
19749 /* Expect a `)'. */
19750 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
19751 declarator = cp_error_declarator;
19752 if (declarator == cp_error_declarator)
19753 break;
19755 goto handle_declarator;
19757 /* Otherwise, we must be done. */
19758 else
19759 break;
19761 else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
19762 && token->type == CPP_OPEN_SQUARE
19763 && !cp_next_tokens_can_be_attribute_p (parser))
19765 /* Parse an array-declarator. */
19766 tree bounds, attrs;
19768 if (ctor_dtor_or_conv_p)
19769 *ctor_dtor_or_conv_p = 0;
19771 first = false;
19772 parser->default_arg_ok_p = false;
19773 parser->in_declarator_p = true;
19774 /* Consume the `['. */
19775 cp_lexer_consume_token (parser->lexer);
19776 /* Peek at the next token. */
19777 token = cp_lexer_peek_token (parser->lexer);
19778 /* If the next token is `]', then there is no
19779 constant-expression. */
19780 if (token->type != CPP_CLOSE_SQUARE)
19782 bool non_constant_p;
19783 bounds
19784 = cp_parser_constant_expression (parser,
19785 /*allow_non_constant=*/true,
19786 &non_constant_p);
19787 if (!non_constant_p)
19788 /* OK */;
19789 else if (error_operand_p (bounds))
19790 /* Already gave an error. */;
19791 else if (!parser->in_function_body
19792 || current_binding_level->kind == sk_function_parms)
19794 /* Normally, the array bound must be an integral constant
19795 expression. However, as an extension, we allow VLAs
19796 in function scopes as long as they aren't part of a
19797 parameter declaration. */
19798 cp_parser_error (parser,
19799 "array bound is not an integer constant");
19800 bounds = error_mark_node;
19802 else if (processing_template_decl
19803 && !type_dependent_expression_p (bounds))
19805 /* Remember this wasn't a constant-expression. */
19806 bounds = build_nop (TREE_TYPE (bounds), bounds);
19807 TREE_SIDE_EFFECTS (bounds) = 1;
19810 else
19811 bounds = NULL_TREE;
19812 /* Look for the closing `]'. */
19813 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
19815 declarator = cp_error_declarator;
19816 break;
19819 attrs = cp_parser_std_attribute_spec_seq (parser);
19820 declarator = make_array_declarator (declarator, bounds);
19821 declarator->std_attributes = attrs;
19823 else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
19826 tree qualifying_scope;
19827 tree unqualified_name;
19828 tree attrs;
19829 special_function_kind sfk;
19830 bool abstract_ok;
19831 bool pack_expansion_p = false;
19832 cp_token *declarator_id_start_token;
19834 /* Parse a declarator-id */
19835 abstract_ok = (dcl_kind == CP_PARSER_DECLARATOR_EITHER);
19836 if (abstract_ok)
19838 cp_parser_parse_tentatively (parser);
19840 /* If we see an ellipsis, we should be looking at a
19841 parameter pack. */
19842 if (token->type == CPP_ELLIPSIS)
19844 /* Consume the `...' */
19845 cp_lexer_consume_token (parser->lexer);
19847 pack_expansion_p = true;
19851 declarator_id_start_token = cp_lexer_peek_token (parser->lexer);
19852 unqualified_name
19853 = cp_parser_declarator_id (parser, /*optional_p=*/abstract_ok);
19854 qualifying_scope = parser->scope;
19855 if (abstract_ok)
19857 bool okay = false;
19859 if (!unqualified_name && pack_expansion_p)
19861 /* Check whether an error occurred. */
19862 okay = !cp_parser_error_occurred (parser);
19864 /* We already consumed the ellipsis to mark a
19865 parameter pack, but we have no way to report it,
19866 so abort the tentative parse. We will be exiting
19867 immediately anyway. */
19868 cp_parser_abort_tentative_parse (parser);
19870 else
19871 okay = cp_parser_parse_definitely (parser);
19873 if (!okay)
19874 unqualified_name = error_mark_node;
19875 else if (unqualified_name
19876 && (qualifying_scope
19877 || (!identifier_p (unqualified_name))))
19879 cp_parser_error (parser, "expected unqualified-id");
19880 unqualified_name = error_mark_node;
19884 if (!unqualified_name)
19885 return NULL;
19886 if (unqualified_name == error_mark_node)
19888 declarator = cp_error_declarator;
19889 pack_expansion_p = false;
19890 declarator->parameter_pack_p = false;
19891 break;
19894 attrs = cp_parser_std_attribute_spec_seq (parser);
19896 if (qualifying_scope && at_namespace_scope_p ()
19897 && TREE_CODE (qualifying_scope) == TYPENAME_TYPE)
19899 /* In the declaration of a member of a template class
19900 outside of the class itself, the SCOPE will sometimes
19901 be a TYPENAME_TYPE. For example, given:
19903 template <typename T>
19904 int S<T>::R::i = 3;
19906 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
19907 this context, we must resolve S<T>::R to an ordinary
19908 type, rather than a typename type.
19910 The reason we normally avoid resolving TYPENAME_TYPEs
19911 is that a specialization of `S' might render
19912 `S<T>::R' not a type. However, if `S' is
19913 specialized, then this `i' will not be used, so there
19914 is no harm in resolving the types here. */
19915 tree type;
19917 /* Resolve the TYPENAME_TYPE. */
19918 type = resolve_typename_type (qualifying_scope,
19919 /*only_current_p=*/false);
19920 /* If that failed, the declarator is invalid. */
19921 if (TREE_CODE (type) == TYPENAME_TYPE)
19923 if (typedef_variant_p (type))
19924 error_at (declarator_id_start_token->location,
19925 "cannot define member of dependent typedef "
19926 "%qT", type);
19927 else
19928 error_at (declarator_id_start_token->location,
19929 "%<%T::%E%> is not a type",
19930 TYPE_CONTEXT (qualifying_scope),
19931 TYPE_IDENTIFIER (qualifying_scope));
19933 qualifying_scope = type;
19936 sfk = sfk_none;
19938 if (unqualified_name)
19940 tree class_type;
19942 if (qualifying_scope
19943 && CLASS_TYPE_P (qualifying_scope))
19944 class_type = qualifying_scope;
19945 else
19946 class_type = current_class_type;
19948 if (TREE_CODE (unqualified_name) == TYPE_DECL)
19950 tree name_type = TREE_TYPE (unqualified_name);
19951 if (class_type && same_type_p (name_type, class_type))
19953 if (qualifying_scope
19954 && CLASSTYPE_USE_TEMPLATE (name_type))
19956 error_at (declarator_id_start_token->location,
19957 "invalid use of constructor as a template");
19958 inform (declarator_id_start_token->location,
19959 "use %<%T::%D%> instead of %<%T::%D%> to "
19960 "name the constructor in a qualified name",
19961 class_type,
19962 DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
19963 class_type, name_type);
19964 declarator = cp_error_declarator;
19965 break;
19967 else
19968 unqualified_name = constructor_name (class_type);
19970 else
19972 /* We do not attempt to print the declarator
19973 here because we do not have enough
19974 information about its original syntactic
19975 form. */
19976 cp_parser_error (parser, "invalid declarator");
19977 declarator = cp_error_declarator;
19978 break;
19982 if (class_type)
19984 if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
19985 sfk = sfk_destructor;
19986 else if (IDENTIFIER_TYPENAME_P (unqualified_name))
19987 sfk = sfk_conversion;
19988 else if (/* There's no way to declare a constructor
19989 for an unnamed type, even if the type
19990 got a name for linkage purposes. */
19991 !TYPE_WAS_UNNAMED (class_type)
19992 /* Handle correctly (c++/19200):
19994 struct S {
19995 struct T{};
19996 friend void S(T);
19999 and also:
20001 namespace N {
20002 void S();
20005 struct S {
20006 friend void N::S();
20007 }; */
20008 && !(friend_p
20009 && class_type != qualifying_scope)
20010 && constructor_name_p (unqualified_name,
20011 class_type))
20013 unqualified_name = constructor_name (class_type);
20014 sfk = sfk_constructor;
20016 else if (is_overloaded_fn (unqualified_name)
20017 && DECL_CONSTRUCTOR_P (get_first_fn
20018 (unqualified_name)))
20019 sfk = sfk_constructor;
20021 if (ctor_dtor_or_conv_p && sfk != sfk_none)
20022 *ctor_dtor_or_conv_p = -1;
20025 declarator = make_id_declarator (qualifying_scope,
20026 unqualified_name,
20027 sfk);
20028 declarator->std_attributes = attrs;
20029 declarator->id_loc = token->location;
20030 declarator->parameter_pack_p = pack_expansion_p;
20032 if (pack_expansion_p)
20033 maybe_warn_variadic_templates ();
20036 handle_declarator:;
20037 scope = get_scope_of_declarator (declarator);
20038 if (scope)
20040 /* Any names that appear after the declarator-id for a
20041 member are looked up in the containing scope. */
20042 if (at_function_scope_p ())
20044 /* But declarations with qualified-ids can't appear in a
20045 function. */
20046 cp_parser_error (parser, "qualified-id in declaration");
20047 declarator = cp_error_declarator;
20048 break;
20050 pushed_scope = push_scope (scope);
20052 parser->in_declarator_p = true;
20053 if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
20054 || (declarator && declarator->kind == cdk_id))
20055 /* Default args are only allowed on function
20056 declarations. */
20057 parser->default_arg_ok_p = saved_default_arg_ok_p;
20058 else
20059 parser->default_arg_ok_p = false;
20061 first = false;
20063 /* We're done. */
20064 else
20065 break;
20068 /* For an abstract declarator, we might wind up with nothing at this
20069 point. That's an error; the declarator is not optional. */
20070 if (!declarator)
20071 cp_parser_error (parser, "expected declarator");
20073 /* If we entered a scope, we must exit it now. */
20074 if (pushed_scope)
20075 pop_scope (pushed_scope);
20077 parser->default_arg_ok_p = saved_default_arg_ok_p;
20078 parser->in_declarator_p = saved_in_declarator_p;
20080 return declarator;
20083 /* Parse a ptr-operator.
20085 ptr-operator:
20086 * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
20087 * cv-qualifier-seq [opt]
20089 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
20090 nested-name-specifier * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
20092 GNU Extension:
20094 ptr-operator:
20095 & cv-qualifier-seq [opt]
20097 Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
20098 Returns ADDR_EXPR if a reference was used, or NON_LVALUE_EXPR for
20099 an rvalue reference. In the case of a pointer-to-member, *TYPE is
20100 filled in with the TYPE containing the member. *CV_QUALS is
20101 filled in with the cv-qualifier-seq, or TYPE_UNQUALIFIED, if there
20102 are no cv-qualifiers. Returns ERROR_MARK if an error occurred.
20103 Note that the tree codes returned by this function have nothing
20104 to do with the types of trees that will be eventually be created
20105 to represent the pointer or reference type being parsed. They are
20106 just constants with suggestive names. */
20107 static enum tree_code
20108 cp_parser_ptr_operator (cp_parser* parser,
20109 tree* type,
20110 cp_cv_quals *cv_quals,
20111 tree *attributes)
20113 enum tree_code code = ERROR_MARK;
20114 cp_token *token;
20115 tree attrs = NULL_TREE;
20117 /* Assume that it's not a pointer-to-member. */
20118 *type = NULL_TREE;
20119 /* And that there are no cv-qualifiers. */
20120 *cv_quals = TYPE_UNQUALIFIED;
20122 /* Peek at the next token. */
20123 token = cp_lexer_peek_token (parser->lexer);
20125 /* If it's a `*', `&' or `&&' we have a pointer or reference. */
20126 if (token->type == CPP_MULT)
20127 code = INDIRECT_REF;
20128 else if (token->type == CPP_AND)
20129 code = ADDR_EXPR;
20130 else if ((cxx_dialect != cxx98) &&
20131 token->type == CPP_AND_AND) /* C++0x only */
20132 code = NON_LVALUE_EXPR;
20134 if (code != ERROR_MARK)
20136 /* Consume the `*', `&' or `&&'. */
20137 cp_lexer_consume_token (parser->lexer);
20139 /* A `*' can be followed by a cv-qualifier-seq, and so can a
20140 `&', if we are allowing GNU extensions. (The only qualifier
20141 that can legally appear after `&' is `restrict', but that is
20142 enforced during semantic analysis. */
20143 if (code == INDIRECT_REF
20144 || cp_parser_allow_gnu_extensions_p (parser))
20145 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
20147 attrs = cp_parser_std_attribute_spec_seq (parser);
20148 if (attributes != NULL)
20149 *attributes = attrs;
20151 else
20153 /* Try the pointer-to-member case. */
20154 cp_parser_parse_tentatively (parser);
20155 /* Look for the optional `::' operator. */
20156 cp_parser_global_scope_opt (parser,
20157 /*current_scope_valid_p=*/false);
20158 /* Look for the nested-name specifier. */
20159 token = cp_lexer_peek_token (parser->lexer);
20160 cp_parser_nested_name_specifier (parser,
20161 /*typename_keyword_p=*/false,
20162 /*check_dependency_p=*/true,
20163 /*type_p=*/false,
20164 /*is_declaration=*/false);
20165 /* If we found it, and the next token is a `*', then we are
20166 indeed looking at a pointer-to-member operator. */
20167 if (!cp_parser_error_occurred (parser)
20168 && cp_parser_require (parser, CPP_MULT, RT_MULT))
20170 /* Indicate that the `*' operator was used. */
20171 code = INDIRECT_REF;
20173 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
20174 error_at (token->location, "%qD is a namespace", parser->scope);
20175 else if (TREE_CODE (parser->scope) == ENUMERAL_TYPE)
20176 error_at (token->location, "cannot form pointer to member of "
20177 "non-class %q#T", parser->scope);
20178 else
20180 /* The type of which the member is a member is given by the
20181 current SCOPE. */
20182 *type = parser->scope;
20183 /* The next name will not be qualified. */
20184 parser->scope = NULL_TREE;
20185 parser->qualifying_scope = NULL_TREE;
20186 parser->object_scope = NULL_TREE;
20187 /* Look for optional c++11 attributes. */
20188 attrs = cp_parser_std_attribute_spec_seq (parser);
20189 if (attributes != NULL)
20190 *attributes = attrs;
20191 /* Look for the optional cv-qualifier-seq. */
20192 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
20195 /* If that didn't work we don't have a ptr-operator. */
20196 if (!cp_parser_parse_definitely (parser))
20197 cp_parser_error (parser, "expected ptr-operator");
20200 return code;
20203 /* Parse an (optional) cv-qualifier-seq.
20205 cv-qualifier-seq:
20206 cv-qualifier cv-qualifier-seq [opt]
20208 cv-qualifier:
20209 const
20210 volatile
20212 GNU Extension:
20214 cv-qualifier:
20215 __restrict__
20217 Returns a bitmask representing the cv-qualifiers. */
20219 static cp_cv_quals
20220 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
20222 cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
20224 while (true)
20226 cp_token *token;
20227 cp_cv_quals cv_qualifier;
20229 /* Peek at the next token. */
20230 token = cp_lexer_peek_token (parser->lexer);
20231 /* See if it's a cv-qualifier. */
20232 switch (token->keyword)
20234 case RID_CONST:
20235 cv_qualifier = TYPE_QUAL_CONST;
20236 break;
20238 case RID_VOLATILE:
20239 cv_qualifier = TYPE_QUAL_VOLATILE;
20240 break;
20242 case RID_RESTRICT:
20243 cv_qualifier = TYPE_QUAL_RESTRICT;
20244 break;
20246 default:
20247 cv_qualifier = TYPE_UNQUALIFIED;
20248 break;
20251 if (!cv_qualifier)
20252 break;
20254 if (cv_quals & cv_qualifier)
20256 error_at (token->location, "duplicate cv-qualifier");
20257 cp_lexer_purge_token (parser->lexer);
20259 else
20261 cp_lexer_consume_token (parser->lexer);
20262 cv_quals |= cv_qualifier;
20266 return cv_quals;
20269 /* Parse an (optional) ref-qualifier
20271 ref-qualifier:
20275 Returns cp_ref_qualifier representing ref-qualifier. */
20277 static cp_ref_qualifier
20278 cp_parser_ref_qualifier_opt (cp_parser* parser)
20280 cp_ref_qualifier ref_qual = REF_QUAL_NONE;
20282 /* Don't try to parse bitwise '&' as a ref-qualifier (c++/57532). */
20283 if (cxx_dialect < cxx11 && cp_parser_parsing_tentatively (parser))
20284 return ref_qual;
20286 while (true)
20288 cp_ref_qualifier curr_ref_qual = REF_QUAL_NONE;
20289 cp_token *token = cp_lexer_peek_token (parser->lexer);
20291 switch (token->type)
20293 case CPP_AND:
20294 curr_ref_qual = REF_QUAL_LVALUE;
20295 break;
20297 case CPP_AND_AND:
20298 curr_ref_qual = REF_QUAL_RVALUE;
20299 break;
20301 default:
20302 curr_ref_qual = REF_QUAL_NONE;
20303 break;
20306 if (!curr_ref_qual)
20307 break;
20308 else if (ref_qual)
20310 error_at (token->location, "multiple ref-qualifiers");
20311 cp_lexer_purge_token (parser->lexer);
20313 else
20315 ref_qual = curr_ref_qual;
20316 cp_lexer_consume_token (parser->lexer);
20320 return ref_qual;
20323 /* Parse an optional tx-qualifier.
20325 tx-qualifier:
20326 transaction_safe
20327 transaction_safe_dynamic */
20329 static tree
20330 cp_parser_tx_qualifier_opt (cp_parser *parser)
20332 cp_token *token = cp_lexer_peek_token (parser->lexer);
20333 if (token->type == CPP_NAME)
20335 tree name = token->u.value;
20336 const char *p = IDENTIFIER_POINTER (name);
20337 const int len = strlen ("transaction_safe");
20338 if (!strncmp (p, "transaction_safe", len))
20340 p += len;
20341 if (*p == '\0'
20342 || !strcmp (p, "_dynamic"))
20344 cp_lexer_consume_token (parser->lexer);
20345 if (!flag_tm)
20347 error ("%E requires %<-fgnu-tm%>", name);
20348 return NULL_TREE;
20350 else
20351 return name;
20355 return NULL_TREE;
20358 /* Parse an (optional) virt-specifier-seq.
20360 virt-specifier-seq:
20361 virt-specifier virt-specifier-seq [opt]
20363 virt-specifier:
20364 override
20365 final
20367 Returns a bitmask representing the virt-specifiers. */
20369 static cp_virt_specifiers
20370 cp_parser_virt_specifier_seq_opt (cp_parser* parser)
20372 cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
20374 while (true)
20376 cp_token *token;
20377 cp_virt_specifiers virt_specifier;
20379 /* Peek at the next token. */
20380 token = cp_lexer_peek_token (parser->lexer);
20381 /* See if it's a virt-specifier-qualifier. */
20382 if (token->type != CPP_NAME)
20383 break;
20384 if (!strcmp (IDENTIFIER_POINTER(token->u.value), "override"))
20386 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS);
20387 virt_specifier = VIRT_SPEC_OVERRIDE;
20389 else if (!strcmp (IDENTIFIER_POINTER(token->u.value), "final"))
20391 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS);
20392 virt_specifier = VIRT_SPEC_FINAL;
20394 else if (!strcmp (IDENTIFIER_POINTER(token->u.value), "__final"))
20396 virt_specifier = VIRT_SPEC_FINAL;
20398 else
20399 break;
20401 if (virt_specifiers & virt_specifier)
20403 error_at (token->location, "duplicate virt-specifier");
20404 cp_lexer_purge_token (parser->lexer);
20406 else
20408 cp_lexer_consume_token (parser->lexer);
20409 virt_specifiers |= virt_specifier;
20412 return virt_specifiers;
20415 /* Used by handling of trailing-return-types and NSDMI, in which 'this'
20416 is in scope even though it isn't real. */
20418 void
20419 inject_this_parameter (tree ctype, cp_cv_quals quals)
20421 tree this_parm;
20423 if (current_class_ptr)
20425 /* We don't clear this between NSDMIs. Is it already what we want? */
20426 tree type = TREE_TYPE (TREE_TYPE (current_class_ptr));
20427 if (same_type_ignoring_top_level_qualifiers_p (ctype, type)
20428 && cp_type_quals (type) == quals)
20429 return;
20432 this_parm = build_this_parm (ctype, quals);
20433 /* Clear this first to avoid shortcut in cp_build_indirect_ref. */
20434 current_class_ptr = NULL_TREE;
20435 current_class_ref
20436 = cp_build_indirect_ref (this_parm, RO_NULL, tf_warning_or_error);
20437 current_class_ptr = this_parm;
20440 /* Return true iff our current scope is a non-static data member
20441 initializer. */
20443 bool
20444 parsing_nsdmi (void)
20446 /* We recognize NSDMI context by the context-less 'this' pointer set up
20447 by the function above. */
20448 if (current_class_ptr
20449 && TREE_CODE (current_class_ptr) == PARM_DECL
20450 && DECL_CONTEXT (current_class_ptr) == NULL_TREE)
20451 return true;
20452 return false;
20455 /* Return true iff our current scope is a default capturing generic lambda
20456 defined within a template. FIXME: This is part of a workaround (see
20457 semantics.c) to handle building lambda closure types correctly in templates
20458 which we ultimately want to defer to instantiation time. */
20460 bool
20461 parsing_default_capturing_generic_lambda_in_template (void)
20463 if (!processing_template_decl || !current_class_type)
20464 return false;
20466 tree lam = CLASSTYPE_LAMBDA_EXPR (current_class_type);
20467 if (!lam || LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lam) == CPLD_NONE)
20468 return false;
20470 tree callop = lambda_function (lam);
20471 if (!callop)
20472 return false;
20474 return (DECL_TEMPLATE_INFO (callop)
20475 && (DECL_TEMPLATE_RESULT (DECL_TI_TEMPLATE (callop)) == callop)
20476 && ((current_nonlambda_class_type ()
20477 && CLASSTYPE_TEMPLATE_INFO (current_nonlambda_class_type ()))
20478 || ((current_nonlambda_function ()
20479 && DECL_TEMPLATE_INFO (current_nonlambda_function ())))));
20482 /* Parse a late-specified return type, if any. This is not a separate
20483 non-terminal, but part of a function declarator, which looks like
20485 -> trailing-type-specifier-seq abstract-declarator(opt)
20487 Returns the type indicated by the type-id.
20489 In addition to this, parse any queued up #pragma omp declare simd
20490 clauses, Cilk Plus SIMD-enabled functions' vector attributes, and
20491 #pragma acc routine clauses.
20493 QUALS is either a bitmask of cv_qualifiers or -1 for a non-member
20494 function. */
20496 static tree
20497 cp_parser_late_return_type_opt (cp_parser* parser, cp_declarator *declarator,
20498 tree& requires_clause, cp_cv_quals quals)
20500 cp_token *token;
20501 tree type = NULL_TREE;
20502 bool declare_simd_p = (parser->omp_declare_simd
20503 && declarator
20504 && declarator->kind == cdk_id);
20506 bool cilk_simd_fn_vector_p = (parser->cilk_simd_fn_info
20507 && declarator && declarator->kind == cdk_id);
20509 bool oacc_routine_p = (parser->oacc_routine
20510 && declarator
20511 && declarator->kind == cdk_id);
20513 /* Peek at the next token. */
20514 token = cp_lexer_peek_token (parser->lexer);
20515 /* A late-specified return type is indicated by an initial '->'. */
20516 if (token->type != CPP_DEREF
20517 && token->keyword != RID_REQUIRES
20518 && !(token->type == CPP_NAME
20519 && token->u.value == ridpointers[RID_REQUIRES])
20520 && !(declare_simd_p || cilk_simd_fn_vector_p || oacc_routine_p))
20521 return NULL_TREE;
20523 tree save_ccp = current_class_ptr;
20524 tree save_ccr = current_class_ref;
20525 if (quals >= 0)
20527 /* DR 1207: 'this' is in scope in the trailing return type. */
20528 inject_this_parameter (current_class_type, quals);
20531 if (token->type == CPP_DEREF)
20533 /* Consume the ->. */
20534 cp_lexer_consume_token (parser->lexer);
20536 type = cp_parser_trailing_type_id (parser);
20539 /* Function declarations may be followed by a trailing
20540 requires-clause. */
20541 requires_clause = cp_parser_requires_clause_opt (parser);
20543 if (cilk_simd_fn_vector_p)
20544 declarator->attributes
20545 = cp_parser_late_parsing_cilk_simd_fn_info (parser,
20546 declarator->attributes);
20547 if (declare_simd_p)
20548 declarator->attributes
20549 = cp_parser_late_parsing_omp_declare_simd (parser,
20550 declarator->attributes);
20551 if (oacc_routine_p)
20552 declarator->attributes
20553 = cp_parser_late_parsing_oacc_routine (parser,
20554 declarator->attributes);
20556 if (quals >= 0)
20558 current_class_ptr = save_ccp;
20559 current_class_ref = save_ccr;
20562 return type;
20565 /* Parse a declarator-id.
20567 declarator-id:
20568 id-expression
20569 :: [opt] nested-name-specifier [opt] type-name
20571 In the `id-expression' case, the value returned is as for
20572 cp_parser_id_expression if the id-expression was an unqualified-id.
20573 If the id-expression was a qualified-id, then a SCOPE_REF is
20574 returned. The first operand is the scope (either a NAMESPACE_DECL
20575 or TREE_TYPE), but the second is still just a representation of an
20576 unqualified-id. */
20578 static tree
20579 cp_parser_declarator_id (cp_parser* parser, bool optional_p)
20581 tree id;
20582 /* The expression must be an id-expression. Assume that qualified
20583 names are the names of types so that:
20585 template <class T>
20586 int S<T>::R::i = 3;
20588 will work; we must treat `S<T>::R' as the name of a type.
20589 Similarly, assume that qualified names are templates, where
20590 required, so that:
20592 template <class T>
20593 int S<T>::R<T>::i = 3;
20595 will work, too. */
20596 id = cp_parser_id_expression (parser,
20597 /*template_keyword_p=*/false,
20598 /*check_dependency_p=*/false,
20599 /*template_p=*/NULL,
20600 /*declarator_p=*/true,
20601 optional_p);
20602 if (id && BASELINK_P (id))
20603 id = BASELINK_FUNCTIONS (id);
20604 return id;
20607 /* Parse a type-id.
20609 type-id:
20610 type-specifier-seq abstract-declarator [opt]
20612 Returns the TYPE specified. */
20614 static tree
20615 cp_parser_type_id_1 (cp_parser* parser, bool is_template_arg,
20616 bool is_trailing_return)
20618 cp_decl_specifier_seq type_specifier_seq;
20619 cp_declarator *abstract_declarator;
20621 /* Parse the type-specifier-seq. */
20622 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
20623 is_trailing_return,
20624 &type_specifier_seq);
20625 if (is_template_arg && type_specifier_seq.type
20626 && TREE_CODE (type_specifier_seq.type) == TEMPLATE_TYPE_PARM
20627 && CLASS_PLACEHOLDER_TEMPLATE (type_specifier_seq.type))
20628 /* A bare template name as a template argument is a template template
20629 argument, not a placeholder, so fail parsing it as a type argument. */
20631 gcc_assert (cp_parser_uncommitted_to_tentative_parse_p (parser));
20632 cp_parser_simulate_error (parser);
20633 return error_mark_node;
20635 if (type_specifier_seq.type == error_mark_node)
20636 return error_mark_node;
20638 /* There might or might not be an abstract declarator. */
20639 cp_parser_parse_tentatively (parser);
20640 /* Look for the declarator. */
20641 abstract_declarator
20642 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
20643 /*parenthesized_p=*/NULL,
20644 /*member_p=*/false,
20645 /*friend_p=*/false);
20646 /* Check to see if there really was a declarator. */
20647 if (!cp_parser_parse_definitely (parser))
20648 abstract_declarator = NULL;
20650 if (type_specifier_seq.type
20651 /* The concepts TS allows 'auto' as a type-id. */
20652 && (!flag_concepts || parser->in_type_id_in_expr_p)
20653 /* None of the valid uses of 'auto' in C++14 involve the type-id
20654 nonterminal, but it is valid in a trailing-return-type. */
20655 && !(cxx_dialect >= cxx14 && is_trailing_return))
20656 if (tree auto_node = type_uses_auto (type_specifier_seq.type))
20658 /* A type-id with type 'auto' is only ok if the abstract declarator
20659 is a function declarator with a late-specified return type.
20661 A type-id with 'auto' is also valid in a trailing-return-type
20662 in a compound-requirement. */
20663 if (abstract_declarator
20664 && abstract_declarator->kind == cdk_function
20665 && abstract_declarator->u.function.late_return_type)
20666 /* OK */;
20667 else if (parser->in_result_type_constraint_p)
20668 /* OK */;
20669 else
20671 location_t loc = type_specifier_seq.locations[ds_type_spec];
20672 if (tree tmpl = CLASS_PLACEHOLDER_TEMPLATE (auto_node))
20674 error_at (loc, "missing template arguments after %qT",
20675 auto_node);
20676 inform (DECL_SOURCE_LOCATION (tmpl), "%qD declared here",
20677 tmpl);
20679 else
20680 error_at (loc, "invalid use of %qT", auto_node);
20681 return error_mark_node;
20685 return groktypename (&type_specifier_seq, abstract_declarator,
20686 is_template_arg);
20689 static tree
20690 cp_parser_type_id (cp_parser *parser)
20692 return cp_parser_type_id_1 (parser, false, false);
20695 static tree
20696 cp_parser_template_type_arg (cp_parser *parser)
20698 tree r;
20699 const char *saved_message = parser->type_definition_forbidden_message;
20700 parser->type_definition_forbidden_message
20701 = G_("types may not be defined in template arguments");
20702 r = cp_parser_type_id_1 (parser, true, false);
20703 parser->type_definition_forbidden_message = saved_message;
20704 if (cxx_dialect >= cxx14 && !flag_concepts && type_uses_auto (r))
20706 error ("invalid use of %<auto%> in template argument");
20707 r = error_mark_node;
20709 return r;
20712 static tree
20713 cp_parser_trailing_type_id (cp_parser *parser)
20715 return cp_parser_type_id_1 (parser, false, true);
20718 /* Parse a type-specifier-seq.
20720 type-specifier-seq:
20721 type-specifier type-specifier-seq [opt]
20723 GNU extension:
20725 type-specifier-seq:
20726 attributes type-specifier-seq [opt]
20728 If IS_DECLARATION is true, we are at the start of a "condition" or
20729 exception-declaration, so we might be followed by a declarator-id.
20731 If IS_TRAILING_RETURN is true, we are in a trailing-return-type,
20732 i.e. we've just seen "->".
20734 Sets *TYPE_SPECIFIER_SEQ to represent the sequence. */
20736 static void
20737 cp_parser_type_specifier_seq (cp_parser* parser,
20738 bool is_declaration,
20739 bool is_trailing_return,
20740 cp_decl_specifier_seq *type_specifier_seq)
20742 bool seen_type_specifier = false;
20743 cp_parser_flags flags = CP_PARSER_FLAGS_OPTIONAL;
20744 cp_token *start_token = NULL;
20746 /* Clear the TYPE_SPECIFIER_SEQ. */
20747 clear_decl_specs (type_specifier_seq);
20749 /* In the context of a trailing return type, enum E { } is an
20750 elaborated-type-specifier followed by a function-body, not an
20751 enum-specifier. */
20752 if (is_trailing_return)
20753 flags |= CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS;
20755 /* Parse the type-specifiers and attributes. */
20756 while (true)
20758 tree type_specifier;
20759 bool is_cv_qualifier;
20761 /* Check for attributes first. */
20762 if (cp_next_tokens_can_be_attribute_p (parser))
20764 type_specifier_seq->attributes =
20765 chainon (type_specifier_seq->attributes,
20766 cp_parser_attributes_opt (parser));
20767 continue;
20770 /* record the token of the beginning of the type specifier seq,
20771 for error reporting purposes*/
20772 if (!start_token)
20773 start_token = cp_lexer_peek_token (parser->lexer);
20775 /* Look for the type-specifier. */
20776 type_specifier = cp_parser_type_specifier (parser,
20777 flags,
20778 type_specifier_seq,
20779 /*is_declaration=*/false,
20780 NULL,
20781 &is_cv_qualifier);
20782 if (!type_specifier)
20784 /* If the first type-specifier could not be found, this is not a
20785 type-specifier-seq at all. */
20786 if (!seen_type_specifier)
20788 /* Set in_declarator_p to avoid skipping to the semicolon. */
20789 int in_decl = parser->in_declarator_p;
20790 parser->in_declarator_p = true;
20792 if (cp_parser_uncommitted_to_tentative_parse_p (parser)
20793 || !cp_parser_parse_and_diagnose_invalid_type_name (parser))
20794 cp_parser_error (parser, "expected type-specifier");
20796 parser->in_declarator_p = in_decl;
20798 type_specifier_seq->type = error_mark_node;
20799 return;
20801 /* If subsequent type-specifiers could not be found, the
20802 type-specifier-seq is complete. */
20803 break;
20806 seen_type_specifier = true;
20807 /* The standard says that a condition can be:
20809 type-specifier-seq declarator = assignment-expression
20811 However, given:
20813 struct S {};
20814 if (int S = ...)
20816 we should treat the "S" as a declarator, not as a
20817 type-specifier. The standard doesn't say that explicitly for
20818 type-specifier-seq, but it does say that for
20819 decl-specifier-seq in an ordinary declaration. Perhaps it
20820 would be clearer just to allow a decl-specifier-seq here, and
20821 then add a semantic restriction that if any decl-specifiers
20822 that are not type-specifiers appear, the program is invalid. */
20823 if (is_declaration && !is_cv_qualifier)
20824 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
20828 /* Return whether the function currently being declared has an associated
20829 template parameter list. */
20831 static bool
20832 function_being_declared_is_template_p (cp_parser* parser)
20834 if (!current_template_parms || processing_template_parmlist)
20835 return false;
20837 if (parser->implicit_template_scope)
20838 return true;
20840 if (at_class_scope_p ()
20841 && TYPE_BEING_DEFINED (current_class_type))
20842 return parser->num_template_parameter_lists != 0;
20844 return ((int) parser->num_template_parameter_lists > template_class_depth
20845 (current_class_type));
20848 /* Parse a parameter-declaration-clause.
20850 parameter-declaration-clause:
20851 parameter-declaration-list [opt] ... [opt]
20852 parameter-declaration-list , ...
20854 Returns a representation for the parameter declarations. A return
20855 value of NULL indicates a parameter-declaration-clause consisting
20856 only of an ellipsis. */
20858 static tree
20859 cp_parser_parameter_declaration_clause (cp_parser* parser)
20861 tree parameters;
20862 cp_token *token;
20863 bool ellipsis_p;
20864 bool is_error;
20866 struct cleanup {
20867 cp_parser* parser;
20868 int auto_is_implicit_function_template_parm_p;
20869 ~cleanup() {
20870 parser->auto_is_implicit_function_template_parm_p
20871 = auto_is_implicit_function_template_parm_p;
20873 } cleanup = { parser, parser->auto_is_implicit_function_template_parm_p };
20875 (void) cleanup;
20877 if (!processing_specialization
20878 && !processing_template_parmlist
20879 && !processing_explicit_instantiation)
20880 if (!current_function_decl
20881 || (current_class_type && LAMBDA_TYPE_P (current_class_type)))
20882 parser->auto_is_implicit_function_template_parm_p = true;
20884 /* Peek at the next token. */
20885 token = cp_lexer_peek_token (parser->lexer);
20886 /* Check for trivial parameter-declaration-clauses. */
20887 if (token->type == CPP_ELLIPSIS)
20889 /* Consume the `...' token. */
20890 cp_lexer_consume_token (parser->lexer);
20891 return NULL_TREE;
20893 else if (token->type == CPP_CLOSE_PAREN)
20894 /* There are no parameters. */
20896 #ifndef NO_IMPLICIT_EXTERN_C
20897 if (in_system_header_at (input_location)
20898 && current_class_type == NULL
20899 && current_lang_name == lang_name_c)
20900 return NULL_TREE;
20901 else
20902 #endif
20903 return void_list_node;
20905 /* Check for `(void)', too, which is a special case. */
20906 else if (token->keyword == RID_VOID
20907 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
20908 == CPP_CLOSE_PAREN))
20910 /* Consume the `void' token. */
20911 cp_lexer_consume_token (parser->lexer);
20912 /* There are no parameters. */
20913 return void_list_node;
20916 /* Parse the parameter-declaration-list. */
20917 parameters = cp_parser_parameter_declaration_list (parser, &is_error);
20918 /* If a parse error occurred while parsing the
20919 parameter-declaration-list, then the entire
20920 parameter-declaration-clause is erroneous. */
20921 if (is_error)
20922 return NULL;
20924 /* Peek at the next token. */
20925 token = cp_lexer_peek_token (parser->lexer);
20926 /* If it's a `,', the clause should terminate with an ellipsis. */
20927 if (token->type == CPP_COMMA)
20929 /* Consume the `,'. */
20930 cp_lexer_consume_token (parser->lexer);
20931 /* Expect an ellipsis. */
20932 ellipsis_p
20933 = (cp_parser_require (parser, CPP_ELLIPSIS, RT_ELLIPSIS) != NULL);
20935 /* It might also be `...' if the optional trailing `,' was
20936 omitted. */
20937 else if (token->type == CPP_ELLIPSIS)
20939 /* Consume the `...' token. */
20940 cp_lexer_consume_token (parser->lexer);
20941 /* And remember that we saw it. */
20942 ellipsis_p = true;
20944 else
20945 ellipsis_p = false;
20947 /* Finish the parameter list. */
20948 if (!ellipsis_p)
20949 parameters = chainon (parameters, void_list_node);
20951 return parameters;
20954 /* Parse a parameter-declaration-list.
20956 parameter-declaration-list:
20957 parameter-declaration
20958 parameter-declaration-list , parameter-declaration
20960 Returns a representation of the parameter-declaration-list, as for
20961 cp_parser_parameter_declaration_clause. However, the
20962 `void_list_node' is never appended to the list. Upon return,
20963 *IS_ERROR will be true iff an error occurred. */
20965 static tree
20966 cp_parser_parameter_declaration_list (cp_parser* parser, bool *is_error)
20968 tree parameters = NULL_TREE;
20969 tree *tail = &parameters;
20970 bool saved_in_unbraced_linkage_specification_p;
20971 int index = 0;
20973 /* Assume all will go well. */
20974 *is_error = false;
20975 /* The special considerations that apply to a function within an
20976 unbraced linkage specifications do not apply to the parameters
20977 to the function. */
20978 saved_in_unbraced_linkage_specification_p
20979 = parser->in_unbraced_linkage_specification_p;
20980 parser->in_unbraced_linkage_specification_p = false;
20982 /* Look for more parameters. */
20983 while (true)
20985 cp_parameter_declarator *parameter;
20986 tree decl = error_mark_node;
20987 bool parenthesized_p = false;
20988 int template_parm_idx = (function_being_declared_is_template_p (parser)?
20989 TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS
20990 (current_template_parms)) : 0);
20992 /* Parse the parameter. */
20993 parameter
20994 = cp_parser_parameter_declaration (parser,
20995 /*template_parm_p=*/false,
20996 &parenthesized_p);
20998 /* We don't know yet if the enclosing context is deprecated, so wait
20999 and warn in grokparms if appropriate. */
21000 deprecated_state = DEPRECATED_SUPPRESS;
21002 if (parameter)
21004 /* If a function parameter pack was specified and an implicit template
21005 parameter was introduced during cp_parser_parameter_declaration,
21006 change any implicit parameters introduced into packs. */
21007 if (parser->implicit_template_parms
21008 && parameter->declarator
21009 && parameter->declarator->parameter_pack_p)
21011 int latest_template_parm_idx = TREE_VEC_LENGTH
21012 (INNERMOST_TEMPLATE_PARMS (current_template_parms));
21014 if (latest_template_parm_idx != template_parm_idx)
21015 parameter->decl_specifiers.type = convert_generic_types_to_packs
21016 (parameter->decl_specifiers.type,
21017 template_parm_idx, latest_template_parm_idx);
21020 decl = grokdeclarator (parameter->declarator,
21021 &parameter->decl_specifiers,
21022 PARM,
21023 parameter->default_argument != NULL_TREE,
21024 &parameter->decl_specifiers.attributes);
21027 deprecated_state = DEPRECATED_NORMAL;
21029 /* If a parse error occurred parsing the parameter declaration,
21030 then the entire parameter-declaration-list is erroneous. */
21031 if (decl == error_mark_node)
21033 *is_error = true;
21034 parameters = error_mark_node;
21035 break;
21038 if (parameter->decl_specifiers.attributes)
21039 cplus_decl_attributes (&decl,
21040 parameter->decl_specifiers.attributes,
21042 if (DECL_NAME (decl))
21043 decl = pushdecl (decl);
21045 if (decl != error_mark_node)
21047 retrofit_lang_decl (decl);
21048 DECL_PARM_INDEX (decl) = ++index;
21049 DECL_PARM_LEVEL (decl) = function_parm_depth ();
21052 /* Add the new parameter to the list. */
21053 *tail = build_tree_list (parameter->default_argument, decl);
21054 tail = &TREE_CHAIN (*tail);
21056 /* Peek at the next token. */
21057 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
21058 || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
21059 /* These are for Objective-C++ */
21060 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
21061 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
21062 /* The parameter-declaration-list is complete. */
21063 break;
21064 else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
21066 cp_token *token;
21068 /* Peek at the next token. */
21069 token = cp_lexer_peek_nth_token (parser->lexer, 2);
21070 /* If it's an ellipsis, then the list is complete. */
21071 if (token->type == CPP_ELLIPSIS)
21072 break;
21073 /* Otherwise, there must be more parameters. Consume the
21074 `,'. */
21075 cp_lexer_consume_token (parser->lexer);
21076 /* When parsing something like:
21078 int i(float f, double d)
21080 we can tell after seeing the declaration for "f" that we
21081 are not looking at an initialization of a variable "i",
21082 but rather at the declaration of a function "i".
21084 Due to the fact that the parsing of template arguments
21085 (as specified to a template-id) requires backtracking we
21086 cannot use this technique when inside a template argument
21087 list. */
21088 if (!parser->in_template_argument_list_p
21089 && !parser->in_type_id_in_expr_p
21090 && cp_parser_uncommitted_to_tentative_parse_p (parser)
21091 /* However, a parameter-declaration of the form
21092 "float(f)" (which is a valid declaration of a
21093 parameter "f") can also be interpreted as an
21094 expression (the conversion of "f" to "float"). */
21095 && !parenthesized_p)
21096 cp_parser_commit_to_tentative_parse (parser);
21098 else
21100 cp_parser_error (parser, "expected %<,%> or %<...%>");
21101 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
21102 cp_parser_skip_to_closing_parenthesis (parser,
21103 /*recovering=*/true,
21104 /*or_comma=*/false,
21105 /*consume_paren=*/false);
21106 break;
21110 parser->in_unbraced_linkage_specification_p
21111 = saved_in_unbraced_linkage_specification_p;
21113 /* Reset implicit_template_scope if we are about to leave the function
21114 parameter list that introduced it. Note that for out-of-line member
21115 definitions, there will be one or more class scopes before we get to
21116 the template parameter scope. */
21118 if (cp_binding_level *its = parser->implicit_template_scope)
21119 if (cp_binding_level *maybe_its = current_binding_level->level_chain)
21121 while (maybe_its->kind == sk_class)
21122 maybe_its = maybe_its->level_chain;
21123 if (maybe_its == its)
21125 parser->implicit_template_parms = 0;
21126 parser->implicit_template_scope = 0;
21130 return parameters;
21133 /* Parse a parameter declaration.
21135 parameter-declaration:
21136 decl-specifier-seq ... [opt] declarator
21137 decl-specifier-seq declarator = assignment-expression
21138 decl-specifier-seq ... [opt] abstract-declarator [opt]
21139 decl-specifier-seq abstract-declarator [opt] = assignment-expression
21141 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
21142 declares a template parameter. (In that case, a non-nested `>'
21143 token encountered during the parsing of the assignment-expression
21144 is not interpreted as a greater-than operator.)
21146 Returns a representation of the parameter, or NULL if an error
21147 occurs. If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
21148 true iff the declarator is of the form "(p)". */
21150 static cp_parameter_declarator *
21151 cp_parser_parameter_declaration (cp_parser *parser,
21152 bool template_parm_p,
21153 bool *parenthesized_p)
21155 int declares_class_or_enum;
21156 cp_decl_specifier_seq decl_specifiers;
21157 cp_declarator *declarator;
21158 tree default_argument;
21159 cp_token *token = NULL, *declarator_token_start = NULL;
21160 const char *saved_message;
21161 bool template_parameter_pack_p = false;
21163 /* In a template parameter, `>' is not an operator.
21165 [temp.param]
21167 When parsing a default template-argument for a non-type
21168 template-parameter, the first non-nested `>' is taken as the end
21169 of the template parameter-list rather than a greater-than
21170 operator. */
21172 /* Type definitions may not appear in parameter types. */
21173 saved_message = parser->type_definition_forbidden_message;
21174 parser->type_definition_forbidden_message
21175 = G_("types may not be defined in parameter types");
21177 /* Parse the declaration-specifiers. */
21178 cp_parser_decl_specifier_seq (parser,
21179 CP_PARSER_FLAGS_NONE,
21180 &decl_specifiers,
21181 &declares_class_or_enum);
21183 /* Complain about missing 'typename' or other invalid type names. */
21184 if (!decl_specifiers.any_type_specifiers_p
21185 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
21186 decl_specifiers.type = error_mark_node;
21188 /* If an error occurred, there's no reason to attempt to parse the
21189 rest of the declaration. */
21190 if (cp_parser_error_occurred (parser))
21192 parser->type_definition_forbidden_message = saved_message;
21193 return NULL;
21196 /* Peek at the next token. */
21197 token = cp_lexer_peek_token (parser->lexer);
21199 /* If the next token is a `)', `,', `=', `>', or `...', then there
21200 is no declarator. However, when variadic templates are enabled,
21201 there may be a declarator following `...'. */
21202 if (token->type == CPP_CLOSE_PAREN
21203 || token->type == CPP_COMMA
21204 || token->type == CPP_EQ
21205 || token->type == CPP_GREATER)
21207 declarator = NULL;
21208 if (parenthesized_p)
21209 *parenthesized_p = false;
21211 /* Otherwise, there should be a declarator. */
21212 else
21214 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
21215 parser->default_arg_ok_p = false;
21217 /* After seeing a decl-specifier-seq, if the next token is not a
21218 "(", there is no possibility that the code is a valid
21219 expression. Therefore, if parsing tentatively, we commit at
21220 this point. */
21221 if (!parser->in_template_argument_list_p
21222 /* In an expression context, having seen:
21224 (int((char ...
21226 we cannot be sure whether we are looking at a
21227 function-type (taking a "char" as a parameter) or a cast
21228 of some object of type "char" to "int". */
21229 && !parser->in_type_id_in_expr_p
21230 && cp_parser_uncommitted_to_tentative_parse_p (parser)
21231 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
21232 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
21233 cp_parser_commit_to_tentative_parse (parser);
21234 /* Parse the declarator. */
21235 declarator_token_start = token;
21236 declarator = cp_parser_declarator (parser,
21237 CP_PARSER_DECLARATOR_EITHER,
21238 /*ctor_dtor_or_conv_p=*/NULL,
21239 parenthesized_p,
21240 /*member_p=*/false,
21241 /*friend_p=*/false);
21242 parser->default_arg_ok_p = saved_default_arg_ok_p;
21243 /* After the declarator, allow more attributes. */
21244 decl_specifiers.attributes
21245 = chainon (decl_specifiers.attributes,
21246 cp_parser_attributes_opt (parser));
21248 /* If the declarator is a template parameter pack, remember that and
21249 clear the flag in the declarator itself so we don't get errors
21250 from grokdeclarator. */
21251 if (template_parm_p && declarator && declarator->parameter_pack_p)
21253 declarator->parameter_pack_p = false;
21254 template_parameter_pack_p = true;
21258 /* If the next token is an ellipsis, and we have not seen a declarator
21259 name, and if either the type of the declarator contains parameter
21260 packs but it is not a TYPE_PACK_EXPANSION or is null (this happens
21261 for, eg, abbreviated integral type names), then we actually have a
21262 parameter pack expansion expression. Otherwise, leave the ellipsis
21263 for a C-style variadic function. */
21264 token = cp_lexer_peek_token (parser->lexer);
21265 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
21267 tree type = decl_specifiers.type;
21269 if (type && DECL_P (type))
21270 type = TREE_TYPE (type);
21272 if (((type
21273 && TREE_CODE (type) != TYPE_PACK_EXPANSION
21274 && (template_parm_p || uses_parameter_packs (type)))
21275 || (!type && template_parm_p))
21276 && declarator_can_be_parameter_pack (declarator))
21278 /* Consume the `...'. */
21279 cp_lexer_consume_token (parser->lexer);
21280 maybe_warn_variadic_templates ();
21282 /* Build a pack expansion type */
21283 if (template_parm_p)
21284 template_parameter_pack_p = true;
21285 else if (declarator)
21286 declarator->parameter_pack_p = true;
21287 else
21288 decl_specifiers.type = make_pack_expansion (type);
21292 /* The restriction on defining new types applies only to the type
21293 of the parameter, not to the default argument. */
21294 parser->type_definition_forbidden_message = saved_message;
21296 /* If the next token is `=', then process a default argument. */
21297 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
21299 tree type = decl_specifiers.type;
21300 token = cp_lexer_peek_token (parser->lexer);
21301 /* If we are defining a class, then the tokens that make up the
21302 default argument must be saved and processed later. */
21303 if (!template_parm_p && at_class_scope_p ()
21304 && TYPE_BEING_DEFINED (current_class_type)
21305 && !LAMBDA_TYPE_P (current_class_type))
21306 default_argument = cp_parser_cache_defarg (parser, /*nsdmi=*/false);
21308 // A constrained-type-specifier may declare a type template-parameter.
21309 else if (declares_constrained_type_template_parameter (type))
21310 default_argument
21311 = cp_parser_default_type_template_argument (parser);
21313 // A constrained-type-specifier may declare a template-template-parameter.
21314 else if (declares_constrained_template_template_parameter (type))
21315 default_argument
21316 = cp_parser_default_template_template_argument (parser);
21318 /* Outside of a class definition, we can just parse the
21319 assignment-expression. */
21320 else
21321 default_argument
21322 = cp_parser_default_argument (parser, template_parm_p);
21324 if (!parser->default_arg_ok_p)
21326 permerror (token->location,
21327 "default arguments are only "
21328 "permitted for function parameters");
21330 else if ((declarator && declarator->parameter_pack_p)
21331 || template_parameter_pack_p
21332 || (decl_specifiers.type
21333 && PACK_EXPANSION_P (decl_specifiers.type)))
21335 /* Find the name of the parameter pack. */
21336 cp_declarator *id_declarator = declarator;
21337 while (id_declarator && id_declarator->kind != cdk_id)
21338 id_declarator = id_declarator->declarator;
21340 if (id_declarator && id_declarator->kind == cdk_id)
21341 error_at (declarator_token_start->location,
21342 template_parm_p
21343 ? G_("template parameter pack %qD "
21344 "cannot have a default argument")
21345 : G_("parameter pack %qD cannot have "
21346 "a default argument"),
21347 id_declarator->u.id.unqualified_name);
21348 else
21349 error_at (declarator_token_start->location,
21350 template_parm_p
21351 ? G_("template parameter pack cannot have "
21352 "a default argument")
21353 : G_("parameter pack cannot have a "
21354 "default argument"));
21356 default_argument = NULL_TREE;
21359 else
21360 default_argument = NULL_TREE;
21362 return make_parameter_declarator (&decl_specifiers,
21363 declarator,
21364 default_argument,
21365 template_parameter_pack_p);
21368 /* Parse a default argument and return it.
21370 TEMPLATE_PARM_P is true if this is a default argument for a
21371 non-type template parameter. */
21372 static tree
21373 cp_parser_default_argument (cp_parser *parser, bool template_parm_p)
21375 tree default_argument = NULL_TREE;
21376 bool saved_greater_than_is_operator_p;
21377 bool saved_local_variables_forbidden_p;
21378 bool non_constant_p, is_direct_init;
21380 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
21381 set correctly. */
21382 saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
21383 parser->greater_than_is_operator_p = !template_parm_p;
21384 /* Local variable names (and the `this' keyword) may not
21385 appear in a default argument. */
21386 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
21387 parser->local_variables_forbidden_p = true;
21388 /* Parse the assignment-expression. */
21389 if (template_parm_p)
21390 push_deferring_access_checks (dk_no_deferred);
21391 tree saved_class_ptr = NULL_TREE;
21392 tree saved_class_ref = NULL_TREE;
21393 /* The "this" pointer is not valid in a default argument. */
21394 if (cfun)
21396 saved_class_ptr = current_class_ptr;
21397 cp_function_chain->x_current_class_ptr = NULL_TREE;
21398 saved_class_ref = current_class_ref;
21399 cp_function_chain->x_current_class_ref = NULL_TREE;
21401 default_argument
21402 = cp_parser_initializer (parser, &is_direct_init, &non_constant_p);
21403 /* Restore the "this" pointer. */
21404 if (cfun)
21406 cp_function_chain->x_current_class_ptr = saved_class_ptr;
21407 cp_function_chain->x_current_class_ref = saved_class_ref;
21409 if (BRACE_ENCLOSED_INITIALIZER_P (default_argument))
21410 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
21411 if (template_parm_p)
21412 pop_deferring_access_checks ();
21413 parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
21414 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
21416 return default_argument;
21419 /* Parse a function-body.
21421 function-body:
21422 compound_statement */
21424 static void
21425 cp_parser_function_body (cp_parser *parser, bool in_function_try_block)
21427 cp_parser_compound_statement (parser, NULL, (in_function_try_block
21428 ? BCS_TRY_BLOCK : BCS_NORMAL),
21429 true);
21432 /* Parse a ctor-initializer-opt followed by a function-body. Return
21433 true if a ctor-initializer was present. When IN_FUNCTION_TRY_BLOCK
21434 is true we are parsing a function-try-block. */
21436 static bool
21437 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser,
21438 bool in_function_try_block)
21440 tree body, list;
21441 bool ctor_initializer_p;
21442 const bool check_body_p =
21443 DECL_CONSTRUCTOR_P (current_function_decl)
21444 && DECL_DECLARED_CONSTEXPR_P (current_function_decl);
21445 tree last = NULL;
21447 /* Begin the function body. */
21448 body = begin_function_body ();
21449 /* Parse the optional ctor-initializer. */
21450 ctor_initializer_p = cp_parser_ctor_initializer_opt (parser);
21452 /* If we're parsing a constexpr constructor definition, we need
21453 to check that the constructor body is indeed empty. However,
21454 before we get to cp_parser_function_body lot of junk has been
21455 generated, so we can't just check that we have an empty block.
21456 Rather we take a snapshot of the outermost block, and check whether
21457 cp_parser_function_body changed its state. */
21458 if (check_body_p)
21460 list = cur_stmt_list;
21461 if (STATEMENT_LIST_TAIL (list))
21462 last = STATEMENT_LIST_TAIL (list)->stmt;
21464 /* Parse the function-body. */
21465 cp_parser_function_body (parser, in_function_try_block);
21466 if (check_body_p)
21467 check_constexpr_ctor_body (last, list, /*complain=*/true);
21468 /* Finish the function body. */
21469 finish_function_body (body);
21471 return ctor_initializer_p;
21474 /* Parse an initializer.
21476 initializer:
21477 = initializer-clause
21478 ( expression-list )
21480 Returns an expression representing the initializer. If no
21481 initializer is present, NULL_TREE is returned.
21483 *IS_DIRECT_INIT is set to FALSE if the `= initializer-clause'
21484 production is used, and TRUE otherwise. *IS_DIRECT_INIT is
21485 set to TRUE if there is no initializer present. If there is an
21486 initializer, and it is not a constant-expression, *NON_CONSTANT_P
21487 is set to true; otherwise it is set to false. */
21489 static tree
21490 cp_parser_initializer (cp_parser* parser, bool* is_direct_init,
21491 bool* non_constant_p)
21493 cp_token *token;
21494 tree init;
21496 /* Peek at the next token. */
21497 token = cp_lexer_peek_token (parser->lexer);
21499 /* Let our caller know whether or not this initializer was
21500 parenthesized. */
21501 *is_direct_init = (token->type != CPP_EQ);
21502 /* Assume that the initializer is constant. */
21503 *non_constant_p = false;
21505 if (token->type == CPP_EQ)
21507 /* Consume the `='. */
21508 cp_lexer_consume_token (parser->lexer);
21509 /* Parse the initializer-clause. */
21510 init = cp_parser_initializer_clause (parser, non_constant_p);
21512 else if (token->type == CPP_OPEN_PAREN)
21514 vec<tree, va_gc> *vec;
21515 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
21516 /*cast_p=*/false,
21517 /*allow_expansion_p=*/true,
21518 non_constant_p);
21519 if (vec == NULL)
21520 return error_mark_node;
21521 init = build_tree_list_vec (vec);
21522 release_tree_vector (vec);
21524 else if (token->type == CPP_OPEN_BRACE)
21526 cp_lexer_set_source_position (parser->lexer);
21527 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
21528 init = cp_parser_braced_list (parser, non_constant_p);
21529 CONSTRUCTOR_IS_DIRECT_INIT (init) = 1;
21531 else
21533 /* Anything else is an error. */
21534 cp_parser_error (parser, "expected initializer");
21535 init = error_mark_node;
21538 if (check_for_bare_parameter_packs (init))
21539 init = error_mark_node;
21541 return init;
21544 /* Parse an initializer-clause.
21546 initializer-clause:
21547 assignment-expression
21548 braced-init-list
21550 Returns an expression representing the initializer.
21552 If the `assignment-expression' production is used the value
21553 returned is simply a representation for the expression.
21555 Otherwise, calls cp_parser_braced_list. */
21557 static cp_expr
21558 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
21560 cp_expr initializer;
21562 /* Assume the expression is constant. */
21563 *non_constant_p = false;
21565 /* If it is not a `{', then we are looking at an
21566 assignment-expression. */
21567 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
21569 initializer
21570 = cp_parser_constant_expression (parser,
21571 /*allow_non_constant_p=*/true,
21572 non_constant_p);
21574 else
21575 initializer = cp_parser_braced_list (parser, non_constant_p);
21577 return initializer;
21580 /* Parse a brace-enclosed initializer list.
21582 braced-init-list:
21583 { initializer-list , [opt] }
21586 Returns a CONSTRUCTOR. The CONSTRUCTOR_ELTS will be
21587 the elements of the initializer-list (or NULL, if the last
21588 production is used). The TREE_TYPE for the CONSTRUCTOR will be
21589 NULL_TREE. There is no way to detect whether or not the optional
21590 trailing `,' was provided. NON_CONSTANT_P is as for
21591 cp_parser_initializer. */
21593 static cp_expr
21594 cp_parser_braced_list (cp_parser* parser, bool* non_constant_p)
21596 tree initializer;
21597 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
21599 /* Consume the `{' token. */
21600 cp_lexer_consume_token (parser->lexer);
21601 /* Create a CONSTRUCTOR to represent the braced-initializer. */
21602 initializer = make_node (CONSTRUCTOR);
21603 /* If it's not a `}', then there is a non-trivial initializer. */
21604 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
21606 /* Parse the initializer list. */
21607 CONSTRUCTOR_ELTS (initializer)
21608 = cp_parser_initializer_list (parser, non_constant_p);
21609 /* A trailing `,' token is allowed. */
21610 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
21611 cp_lexer_consume_token (parser->lexer);
21613 else
21614 *non_constant_p = false;
21615 /* Now, there should be a trailing `}'. */
21616 location_t finish_loc = cp_lexer_peek_token (parser->lexer)->location;
21617 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
21618 TREE_TYPE (initializer) = init_list_type_node;
21620 cp_expr result (initializer);
21621 /* Build a location of the form:
21622 { ... }
21623 ^~~~~~~
21624 with caret==start at the open brace, finish at the close brace. */
21625 location_t combined_loc = make_location (start_loc, start_loc, finish_loc);
21626 result.set_location (combined_loc);
21627 return result;
21630 /* Consume tokens up to, and including, the next non-nested closing `]'.
21631 Returns true iff we found a closing `]'. */
21633 static bool
21634 cp_parser_skip_to_closing_square_bracket (cp_parser *parser)
21636 unsigned square_depth = 0;
21638 while (true)
21640 cp_token * token = cp_lexer_peek_token (parser->lexer);
21642 switch (token->type)
21644 case CPP_EOF:
21645 case CPP_PRAGMA_EOL:
21646 /* If we've run out of tokens, then there is no closing `]'. */
21647 return false;
21649 case CPP_OPEN_SQUARE:
21650 ++square_depth;
21651 break;
21653 case CPP_CLOSE_SQUARE:
21654 if (!square_depth--)
21656 cp_lexer_consume_token (parser->lexer);
21657 return true;
21659 break;
21661 default:
21662 break;
21665 /* Consume the token. */
21666 cp_lexer_consume_token (parser->lexer);
21670 /* Return true if we are looking at an array-designator, false otherwise. */
21672 static bool
21673 cp_parser_array_designator_p (cp_parser *parser)
21675 /* Consume the `['. */
21676 cp_lexer_consume_token (parser->lexer);
21678 cp_lexer_save_tokens (parser->lexer);
21680 /* Skip tokens until the next token is a closing square bracket.
21681 If we find the closing `]', and the next token is a `=', then
21682 we are looking at an array designator. */
21683 bool array_designator_p
21684 = (cp_parser_skip_to_closing_square_bracket (parser)
21685 && cp_lexer_next_token_is (parser->lexer, CPP_EQ));
21687 /* Roll back the tokens we skipped. */
21688 cp_lexer_rollback_tokens (parser->lexer);
21690 return array_designator_p;
21693 /* Parse an initializer-list.
21695 initializer-list:
21696 initializer-clause ... [opt]
21697 initializer-list , initializer-clause ... [opt]
21699 GNU Extension:
21701 initializer-list:
21702 designation initializer-clause ...[opt]
21703 initializer-list , designation initializer-clause ...[opt]
21705 designation:
21706 . identifier =
21707 identifier :
21708 [ constant-expression ] =
21710 Returns a vec of constructor_elt. The VALUE of each elt is an expression
21711 for the initializer. If the INDEX of the elt is non-NULL, it is the
21712 IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is
21713 as for cp_parser_initializer. */
21715 static vec<constructor_elt, va_gc> *
21716 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
21718 vec<constructor_elt, va_gc> *v = NULL;
21720 /* Assume all of the expressions are constant. */
21721 *non_constant_p = false;
21723 /* Parse the rest of the list. */
21724 while (true)
21726 cp_token *token;
21727 tree designator;
21728 tree initializer;
21729 bool clause_non_constant_p;
21731 /* If the next token is an identifier and the following one is a
21732 colon, we are looking at the GNU designated-initializer
21733 syntax. */
21734 if (cp_parser_allow_gnu_extensions_p (parser)
21735 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
21736 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
21738 /* Warn the user that they are using an extension. */
21739 pedwarn (input_location, OPT_Wpedantic,
21740 "ISO C++ does not allow designated initializers");
21741 /* Consume the identifier. */
21742 designator = cp_lexer_consume_token (parser->lexer)->u.value;
21743 /* Consume the `:'. */
21744 cp_lexer_consume_token (parser->lexer);
21746 /* Also handle the C99 syntax, '. id ='. */
21747 else if (cp_parser_allow_gnu_extensions_p (parser)
21748 && cp_lexer_next_token_is (parser->lexer, CPP_DOT)
21749 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME
21750 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_EQ)
21752 /* Warn the user that they are using an extension. */
21753 pedwarn (input_location, OPT_Wpedantic,
21754 "ISO C++ does not allow C99 designated initializers");
21755 /* Consume the `.'. */
21756 cp_lexer_consume_token (parser->lexer);
21757 /* Consume the identifier. */
21758 designator = cp_lexer_consume_token (parser->lexer)->u.value;
21759 /* Consume the `='. */
21760 cp_lexer_consume_token (parser->lexer);
21762 /* Also handle C99 array designators, '[ const ] ='. */
21763 else if (cp_parser_allow_gnu_extensions_p (parser)
21764 && !c_dialect_objc ()
21765 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
21767 /* In C++11, [ could start a lambda-introducer. */
21768 bool non_const = false;
21770 cp_parser_parse_tentatively (parser);
21772 if (!cp_parser_array_designator_p (parser))
21774 cp_parser_simulate_error (parser);
21775 designator = NULL_TREE;
21777 else
21779 designator = cp_parser_constant_expression (parser, true,
21780 &non_const);
21781 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
21782 cp_parser_require (parser, CPP_EQ, RT_EQ);
21785 if (!cp_parser_parse_definitely (parser))
21786 designator = NULL_TREE;
21787 else if (non_const)
21788 require_potential_rvalue_constant_expression (designator);
21790 else
21791 designator = NULL_TREE;
21793 /* Parse the initializer. */
21794 initializer = cp_parser_initializer_clause (parser,
21795 &clause_non_constant_p);
21796 /* If any clause is non-constant, so is the entire initializer. */
21797 if (clause_non_constant_p)
21798 *non_constant_p = true;
21800 /* If we have an ellipsis, this is an initializer pack
21801 expansion. */
21802 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
21804 /* Consume the `...'. */
21805 cp_lexer_consume_token (parser->lexer);
21807 /* Turn the initializer into an initializer expansion. */
21808 initializer = make_pack_expansion (initializer);
21811 /* Add it to the vector. */
21812 CONSTRUCTOR_APPEND_ELT (v, designator, initializer);
21814 /* If the next token is not a comma, we have reached the end of
21815 the list. */
21816 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
21817 break;
21819 /* Peek at the next token. */
21820 token = cp_lexer_peek_nth_token (parser->lexer, 2);
21821 /* If the next token is a `}', then we're still done. An
21822 initializer-clause can have a trailing `,' after the
21823 initializer-list and before the closing `}'. */
21824 if (token->type == CPP_CLOSE_BRACE)
21825 break;
21827 /* Consume the `,' token. */
21828 cp_lexer_consume_token (parser->lexer);
21831 return v;
21834 /* Classes [gram.class] */
21836 /* Parse a class-name.
21838 class-name:
21839 identifier
21840 template-id
21842 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
21843 to indicate that names looked up in dependent types should be
21844 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
21845 keyword has been used to indicate that the name that appears next
21846 is a template. TAG_TYPE indicates the explicit tag given before
21847 the type name, if any. If CHECK_DEPENDENCY_P is FALSE, names are
21848 looked up in dependent scopes. If CLASS_HEAD_P is TRUE, this class
21849 is the class being defined in a class-head. If ENUM_OK is TRUE,
21850 enum-names are also accepted.
21852 Returns the TYPE_DECL representing the class. */
21854 static tree
21855 cp_parser_class_name (cp_parser *parser,
21856 bool typename_keyword_p,
21857 bool template_keyword_p,
21858 enum tag_types tag_type,
21859 bool check_dependency_p,
21860 bool class_head_p,
21861 bool is_declaration,
21862 bool enum_ok)
21864 tree decl;
21865 tree scope;
21866 bool typename_p;
21867 cp_token *token;
21868 tree identifier = NULL_TREE;
21870 /* All class-names start with an identifier. */
21871 token = cp_lexer_peek_token (parser->lexer);
21872 if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
21874 cp_parser_error (parser, "expected class-name");
21875 return error_mark_node;
21878 /* PARSER->SCOPE can be cleared when parsing the template-arguments
21879 to a template-id, so we save it here. */
21880 scope = parser->scope;
21881 if (scope == error_mark_node)
21882 return error_mark_node;
21884 /* Any name names a type if we're following the `typename' keyword
21885 in a qualified name where the enclosing scope is type-dependent. */
21886 typename_p = (typename_keyword_p && scope && TYPE_P (scope)
21887 && dependent_type_p (scope));
21888 /* Handle the common case (an identifier, but not a template-id)
21889 efficiently. */
21890 if (token->type == CPP_NAME
21891 && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
21893 cp_token *identifier_token;
21894 bool ambiguous_p;
21896 /* Look for the identifier. */
21897 identifier_token = cp_lexer_peek_token (parser->lexer);
21898 ambiguous_p = identifier_token->error_reported;
21899 identifier = cp_parser_identifier (parser);
21900 /* If the next token isn't an identifier, we are certainly not
21901 looking at a class-name. */
21902 if (identifier == error_mark_node)
21903 decl = error_mark_node;
21904 /* If we know this is a type-name, there's no need to look it
21905 up. */
21906 else if (typename_p)
21907 decl = identifier;
21908 else
21910 tree ambiguous_decls;
21911 /* If we already know that this lookup is ambiguous, then
21912 we've already issued an error message; there's no reason
21913 to check again. */
21914 if (ambiguous_p)
21916 cp_parser_simulate_error (parser);
21917 return error_mark_node;
21919 /* If the next token is a `::', then the name must be a type
21920 name.
21922 [basic.lookup.qual]
21924 During the lookup for a name preceding the :: scope
21925 resolution operator, object, function, and enumerator
21926 names are ignored. */
21927 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
21928 tag_type = scope_type;
21929 /* Look up the name. */
21930 decl = cp_parser_lookup_name (parser, identifier,
21931 tag_type,
21932 /*is_template=*/false,
21933 /*is_namespace=*/false,
21934 check_dependency_p,
21935 &ambiguous_decls,
21936 identifier_token->location);
21937 if (ambiguous_decls)
21939 if (cp_parser_parsing_tentatively (parser))
21940 cp_parser_simulate_error (parser);
21941 return error_mark_node;
21945 else
21947 /* Try a template-id. */
21948 decl = cp_parser_template_id (parser, template_keyword_p,
21949 check_dependency_p,
21950 tag_type,
21951 is_declaration);
21952 if (decl == error_mark_node)
21953 return error_mark_node;
21956 decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
21958 /* If this is a typename, create a TYPENAME_TYPE. */
21959 if (typename_p && decl != error_mark_node)
21961 decl = make_typename_type (scope, decl, typename_type,
21962 /*complain=*/tf_error);
21963 if (decl != error_mark_node)
21964 decl = TYPE_NAME (decl);
21967 decl = strip_using_decl (decl);
21969 /* Check to see that it is really the name of a class. */
21970 if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
21971 && identifier_p (TREE_OPERAND (decl, 0))
21972 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
21973 /* Situations like this:
21975 template <typename T> struct A {
21976 typename T::template X<int>::I i;
21979 are problematic. Is `T::template X<int>' a class-name? The
21980 standard does not seem to be definitive, but there is no other
21981 valid interpretation of the following `::'. Therefore, those
21982 names are considered class-names. */
21984 decl = make_typename_type (scope, decl, tag_type, tf_error);
21985 if (decl != error_mark_node)
21986 decl = TYPE_NAME (decl);
21988 else if (TREE_CODE (decl) != TYPE_DECL
21989 || TREE_TYPE (decl) == error_mark_node
21990 || !(MAYBE_CLASS_TYPE_P (TREE_TYPE (decl))
21991 || (enum_ok && TREE_CODE (TREE_TYPE (decl)) == ENUMERAL_TYPE))
21992 /* In Objective-C 2.0, a classname followed by '.' starts a
21993 dot-syntax expression, and it's not a type-name. */
21994 || (c_dialect_objc ()
21995 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT
21996 && objc_is_class_name (decl)))
21997 decl = error_mark_node;
21999 if (decl == error_mark_node)
22000 cp_parser_error (parser, "expected class-name");
22001 else if (identifier && !parser->scope)
22002 maybe_note_name_used_in_class (identifier, decl);
22004 return decl;
22007 /* Parse a class-specifier.
22009 class-specifier:
22010 class-head { member-specification [opt] }
22012 Returns the TREE_TYPE representing the class. */
22014 static tree
22015 cp_parser_class_specifier_1 (cp_parser* parser)
22017 tree type;
22018 tree attributes = NULL_TREE;
22019 bool nested_name_specifier_p;
22020 unsigned saved_num_template_parameter_lists;
22021 bool saved_in_function_body;
22022 unsigned char in_statement;
22023 bool in_switch_statement_p;
22024 bool saved_in_unbraced_linkage_specification_p;
22025 tree old_scope = NULL_TREE;
22026 tree scope = NULL_TREE;
22027 cp_token *closing_brace;
22029 push_deferring_access_checks (dk_no_deferred);
22031 /* Parse the class-head. */
22032 type = cp_parser_class_head (parser,
22033 &nested_name_specifier_p);
22034 /* If the class-head was a semantic disaster, skip the entire body
22035 of the class. */
22036 if (!type)
22038 cp_parser_skip_to_end_of_block_or_statement (parser);
22039 pop_deferring_access_checks ();
22040 return error_mark_node;
22043 /* Look for the `{'. */
22044 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
22046 pop_deferring_access_checks ();
22047 return error_mark_node;
22050 cp_ensure_no_omp_declare_simd (parser);
22051 cp_ensure_no_oacc_routine (parser);
22053 /* Issue an error message if type-definitions are forbidden here. */
22054 cp_parser_check_type_definition (parser);
22055 /* Remember that we are defining one more class. */
22056 ++parser->num_classes_being_defined;
22057 /* Inside the class, surrounding template-parameter-lists do not
22058 apply. */
22059 saved_num_template_parameter_lists
22060 = parser->num_template_parameter_lists;
22061 parser->num_template_parameter_lists = 0;
22062 /* We are not in a function body. */
22063 saved_in_function_body = parser->in_function_body;
22064 parser->in_function_body = false;
22065 /* Or in a loop. */
22066 in_statement = parser->in_statement;
22067 parser->in_statement = 0;
22068 /* Or in a switch. */
22069 in_switch_statement_p = parser->in_switch_statement_p;
22070 parser->in_switch_statement_p = false;
22071 /* We are not immediately inside an extern "lang" block. */
22072 saved_in_unbraced_linkage_specification_p
22073 = parser->in_unbraced_linkage_specification_p;
22074 parser->in_unbraced_linkage_specification_p = false;
22076 // Associate constraints with the type.
22077 if (flag_concepts)
22078 type = associate_classtype_constraints (type);
22080 /* Start the class. */
22081 if (nested_name_specifier_p)
22083 scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
22084 old_scope = push_inner_scope (scope);
22086 type = begin_class_definition (type);
22088 if (type == error_mark_node)
22089 /* If the type is erroneous, skip the entire body of the class. */
22090 cp_parser_skip_to_closing_brace (parser);
22091 else
22092 /* Parse the member-specification. */
22093 cp_parser_member_specification_opt (parser);
22095 /* Look for the trailing `}'. */
22096 closing_brace = cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
22097 /* Look for trailing attributes to apply to this class. */
22098 if (cp_parser_allow_gnu_extensions_p (parser))
22099 attributes = cp_parser_gnu_attributes_opt (parser);
22100 if (type != error_mark_node)
22101 type = finish_struct (type, attributes);
22102 if (nested_name_specifier_p)
22103 pop_inner_scope (old_scope, scope);
22105 /* We've finished a type definition. Check for the common syntax
22106 error of forgetting a semicolon after the definition. We need to
22107 be careful, as we can't just check for not-a-semicolon and be done
22108 with it; the user might have typed:
22110 class X { } c = ...;
22111 class X { } *p = ...;
22113 and so forth. Instead, enumerate all the possible tokens that
22114 might follow this production; if we don't see one of them, then
22115 complain and silently insert the semicolon. */
22117 cp_token *token = cp_lexer_peek_token (parser->lexer);
22118 bool want_semicolon = true;
22120 if (cp_next_tokens_can_be_std_attribute_p (parser))
22121 /* Don't try to parse c++11 attributes here. As per the
22122 grammar, that should be a task for
22123 cp_parser_decl_specifier_seq. */
22124 want_semicolon = false;
22126 switch (token->type)
22128 case CPP_NAME:
22129 case CPP_SEMICOLON:
22130 case CPP_MULT:
22131 case CPP_AND:
22132 case CPP_OPEN_PAREN:
22133 case CPP_CLOSE_PAREN:
22134 case CPP_COMMA:
22135 want_semicolon = false;
22136 break;
22138 /* While it's legal for type qualifiers and storage class
22139 specifiers to follow type definitions in the grammar, only
22140 compiler testsuites contain code like that. Assume that if
22141 we see such code, then what we're really seeing is a case
22142 like:
22144 class X { }
22145 const <type> var = ...;
22149 class Y { }
22150 static <type> func (...) ...
22152 i.e. the qualifier or specifier applies to the next
22153 declaration. To do so, however, we need to look ahead one
22154 more token to see if *that* token is a type specifier.
22156 This code could be improved to handle:
22158 class Z { }
22159 static const <type> var = ...; */
22160 case CPP_KEYWORD:
22161 if (keyword_is_decl_specifier (token->keyword))
22163 cp_token *lookahead = cp_lexer_peek_nth_token (parser->lexer, 2);
22165 /* Handling user-defined types here would be nice, but very
22166 tricky. */
22167 want_semicolon
22168 = (lookahead->type == CPP_KEYWORD
22169 && keyword_begins_type_specifier (lookahead->keyword));
22171 break;
22172 default:
22173 break;
22176 /* If we don't have a type, then something is very wrong and we
22177 shouldn't try to do anything clever. Likewise for not seeing the
22178 closing brace. */
22179 if (closing_brace && TYPE_P (type) && want_semicolon)
22181 /* Locate the closing brace. */
22182 cp_token_position prev
22183 = cp_lexer_previous_token_position (parser->lexer);
22184 cp_token *prev_token = cp_lexer_token_at (parser->lexer, prev);
22185 location_t loc = prev_token->location;
22187 /* We want to suggest insertion of a ';' immediately *after* the
22188 closing brace, so, if we can, offset the location by 1 column. */
22189 location_t next_loc = loc;
22190 if (!linemap_location_from_macro_expansion_p (line_table, loc))
22191 next_loc = linemap_position_for_loc_and_offset (line_table, loc, 1);
22193 rich_location richloc (line_table, next_loc);
22195 /* If we successfully offset the location, suggest the fix-it. */
22196 if (next_loc != loc)
22197 richloc.add_fixit_insert_before (next_loc, ";");
22199 if (CLASSTYPE_DECLARED_CLASS (type))
22200 error_at_rich_loc (&richloc,
22201 "expected %<;%> after class definition");
22202 else if (TREE_CODE (type) == RECORD_TYPE)
22203 error_at_rich_loc (&richloc,
22204 "expected %<;%> after struct definition");
22205 else if (TREE_CODE (type) == UNION_TYPE)
22206 error_at_rich_loc (&richloc,
22207 "expected %<;%> after union definition");
22208 else
22209 gcc_unreachable ();
22211 /* Unget one token and smash it to look as though we encountered
22212 a semicolon in the input stream. */
22213 cp_lexer_set_token_position (parser->lexer, prev);
22214 token = cp_lexer_peek_token (parser->lexer);
22215 token->type = CPP_SEMICOLON;
22216 token->keyword = RID_MAX;
22220 /* If this class is not itself within the scope of another class,
22221 then we need to parse the bodies of all of the queued function
22222 definitions. Note that the queued functions defined in a class
22223 are not always processed immediately following the
22224 class-specifier for that class. Consider:
22226 struct A {
22227 struct B { void f() { sizeof (A); } };
22230 If `f' were processed before the processing of `A' were
22231 completed, there would be no way to compute the size of `A'.
22232 Note that the nesting we are interested in here is lexical --
22233 not the semantic nesting given by TYPE_CONTEXT. In particular,
22234 for:
22236 struct A { struct B; };
22237 struct A::B { void f() { } };
22239 there is no need to delay the parsing of `A::B::f'. */
22240 if (--parser->num_classes_being_defined == 0)
22242 tree decl;
22243 tree class_type = NULL_TREE;
22244 tree pushed_scope = NULL_TREE;
22245 unsigned ix;
22246 cp_default_arg_entry *e;
22247 tree save_ccp, save_ccr;
22249 /* In a first pass, parse default arguments to the functions.
22250 Then, in a second pass, parse the bodies of the functions.
22251 This two-phased approach handles cases like:
22253 struct S {
22254 void f() { g(); }
22255 void g(int i = 3);
22259 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_default_args, ix, e)
22261 decl = e->decl;
22262 /* If there are default arguments that have not yet been processed,
22263 take care of them now. */
22264 if (class_type != e->class_type)
22266 if (pushed_scope)
22267 pop_scope (pushed_scope);
22268 class_type = e->class_type;
22269 pushed_scope = push_scope (class_type);
22271 /* Make sure that any template parameters are in scope. */
22272 maybe_begin_member_template_processing (decl);
22273 /* Parse the default argument expressions. */
22274 cp_parser_late_parsing_default_args (parser, decl);
22275 /* Remove any template parameters from the symbol table. */
22276 maybe_end_member_template_processing ();
22278 vec_safe_truncate (unparsed_funs_with_default_args, 0);
22279 /* Now parse any NSDMIs. */
22280 save_ccp = current_class_ptr;
22281 save_ccr = current_class_ref;
22282 FOR_EACH_VEC_SAFE_ELT (unparsed_nsdmis, ix, decl)
22284 if (class_type != DECL_CONTEXT (decl))
22286 if (pushed_scope)
22287 pop_scope (pushed_scope);
22288 class_type = DECL_CONTEXT (decl);
22289 pushed_scope = push_scope (class_type);
22291 inject_this_parameter (class_type, TYPE_UNQUALIFIED);
22292 cp_parser_late_parsing_nsdmi (parser, decl);
22294 vec_safe_truncate (unparsed_nsdmis, 0);
22295 current_class_ptr = save_ccp;
22296 current_class_ref = save_ccr;
22297 if (pushed_scope)
22298 pop_scope (pushed_scope);
22300 /* Now do some post-NSDMI bookkeeping. */
22301 FOR_EACH_VEC_SAFE_ELT (unparsed_classes, ix, class_type)
22302 after_nsdmi_defaulted_late_checks (class_type);
22303 vec_safe_truncate (unparsed_classes, 0);
22304 after_nsdmi_defaulted_late_checks (type);
22306 /* Now parse the body of the functions. */
22307 if (flag_openmp)
22309 /* OpenMP UDRs need to be parsed before all other functions. */
22310 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
22311 if (DECL_OMP_DECLARE_REDUCTION_P (decl))
22312 cp_parser_late_parsing_for_member (parser, decl);
22313 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
22314 if (!DECL_OMP_DECLARE_REDUCTION_P (decl))
22315 cp_parser_late_parsing_for_member (parser, decl);
22317 else
22318 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
22319 cp_parser_late_parsing_for_member (parser, decl);
22320 vec_safe_truncate (unparsed_funs_with_definitions, 0);
22322 else
22323 vec_safe_push (unparsed_classes, type);
22325 /* Put back any saved access checks. */
22326 pop_deferring_access_checks ();
22328 /* Restore saved state. */
22329 parser->in_switch_statement_p = in_switch_statement_p;
22330 parser->in_statement = in_statement;
22331 parser->in_function_body = saved_in_function_body;
22332 parser->num_template_parameter_lists
22333 = saved_num_template_parameter_lists;
22334 parser->in_unbraced_linkage_specification_p
22335 = saved_in_unbraced_linkage_specification_p;
22337 return type;
22340 static tree
22341 cp_parser_class_specifier (cp_parser* parser)
22343 tree ret;
22344 timevar_push (TV_PARSE_STRUCT);
22345 ret = cp_parser_class_specifier_1 (parser);
22346 timevar_pop (TV_PARSE_STRUCT);
22347 return ret;
22350 /* Parse a class-head.
22352 class-head:
22353 class-key identifier [opt] base-clause [opt]
22354 class-key nested-name-specifier identifier class-virt-specifier [opt] base-clause [opt]
22355 class-key nested-name-specifier [opt] template-id
22356 base-clause [opt]
22358 class-virt-specifier:
22359 final
22361 GNU Extensions:
22362 class-key attributes identifier [opt] base-clause [opt]
22363 class-key attributes nested-name-specifier identifier base-clause [opt]
22364 class-key attributes nested-name-specifier [opt] template-id
22365 base-clause [opt]
22367 Upon return BASES is initialized to the list of base classes (or
22368 NULL, if there are none) in the same form returned by
22369 cp_parser_base_clause.
22371 Returns the TYPE of the indicated class. Sets
22372 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
22373 involving a nested-name-specifier was used, and FALSE otherwise.
22375 Returns error_mark_node if this is not a class-head.
22377 Returns NULL_TREE if the class-head is syntactically valid, but
22378 semantically invalid in a way that means we should skip the entire
22379 body of the class. */
22381 static tree
22382 cp_parser_class_head (cp_parser* parser,
22383 bool* nested_name_specifier_p)
22385 tree nested_name_specifier;
22386 enum tag_types class_key;
22387 tree id = NULL_TREE;
22388 tree type = NULL_TREE;
22389 tree attributes;
22390 tree bases;
22391 cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
22392 bool template_id_p = false;
22393 bool qualified_p = false;
22394 bool invalid_nested_name_p = false;
22395 bool invalid_explicit_specialization_p = false;
22396 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
22397 tree pushed_scope = NULL_TREE;
22398 unsigned num_templates;
22399 cp_token *type_start_token = NULL, *nested_name_specifier_token_start = NULL;
22400 /* Assume no nested-name-specifier will be present. */
22401 *nested_name_specifier_p = false;
22402 /* Assume no template parameter lists will be used in defining the
22403 type. */
22404 num_templates = 0;
22405 parser->colon_corrects_to_scope_p = false;
22407 /* Look for the class-key. */
22408 class_key = cp_parser_class_key (parser);
22409 if (class_key == none_type)
22410 return error_mark_node;
22412 location_t class_head_start_location = input_location;
22414 /* Parse the attributes. */
22415 attributes = cp_parser_attributes_opt (parser);
22417 /* If the next token is `::', that is invalid -- but sometimes
22418 people do try to write:
22420 struct ::S {};
22422 Handle this gracefully by accepting the extra qualifier, and then
22423 issuing an error about it later if this really is a
22424 class-head. If it turns out just to be an elaborated type
22425 specifier, remain silent. */
22426 if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
22427 qualified_p = true;
22429 push_deferring_access_checks (dk_no_check);
22431 /* Determine the name of the class. Begin by looking for an
22432 optional nested-name-specifier. */
22433 nested_name_specifier_token_start = cp_lexer_peek_token (parser->lexer);
22434 nested_name_specifier
22435 = cp_parser_nested_name_specifier_opt (parser,
22436 /*typename_keyword_p=*/false,
22437 /*check_dependency_p=*/false,
22438 /*type_p=*/true,
22439 /*is_declaration=*/false);
22440 /* If there was a nested-name-specifier, then there *must* be an
22441 identifier. */
22442 if (nested_name_specifier)
22444 type_start_token = cp_lexer_peek_token (parser->lexer);
22445 /* Although the grammar says `identifier', it really means
22446 `class-name' or `template-name'. You are only allowed to
22447 define a class that has already been declared with this
22448 syntax.
22450 The proposed resolution for Core Issue 180 says that wherever
22451 you see `class T::X' you should treat `X' as a type-name.
22453 It is OK to define an inaccessible class; for example:
22455 class A { class B; };
22456 class A::B {};
22458 We do not know if we will see a class-name, or a
22459 template-name. We look for a class-name first, in case the
22460 class-name is a template-id; if we looked for the
22461 template-name first we would stop after the template-name. */
22462 cp_parser_parse_tentatively (parser);
22463 type = cp_parser_class_name (parser,
22464 /*typename_keyword_p=*/false,
22465 /*template_keyword_p=*/false,
22466 class_type,
22467 /*check_dependency_p=*/false,
22468 /*class_head_p=*/true,
22469 /*is_declaration=*/false);
22470 /* If that didn't work, ignore the nested-name-specifier. */
22471 if (!cp_parser_parse_definitely (parser))
22473 invalid_nested_name_p = true;
22474 type_start_token = cp_lexer_peek_token (parser->lexer);
22475 id = cp_parser_identifier (parser);
22476 if (id == error_mark_node)
22477 id = NULL_TREE;
22479 /* If we could not find a corresponding TYPE, treat this
22480 declaration like an unqualified declaration. */
22481 if (type == error_mark_node)
22482 nested_name_specifier = NULL_TREE;
22483 /* Otherwise, count the number of templates used in TYPE and its
22484 containing scopes. */
22485 else
22487 tree scope;
22489 for (scope = TREE_TYPE (type);
22490 scope && TREE_CODE (scope) != NAMESPACE_DECL;
22491 scope = get_containing_scope (scope))
22492 if (TYPE_P (scope)
22493 && CLASS_TYPE_P (scope)
22494 && CLASSTYPE_TEMPLATE_INFO (scope)
22495 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
22496 && (!CLASSTYPE_TEMPLATE_SPECIALIZATION (scope)
22497 || uses_template_parms (CLASSTYPE_TI_ARGS (scope))))
22498 ++num_templates;
22501 /* Otherwise, the identifier is optional. */
22502 else
22504 /* We don't know whether what comes next is a template-id,
22505 an identifier, or nothing at all. */
22506 cp_parser_parse_tentatively (parser);
22507 /* Check for a template-id. */
22508 type_start_token = cp_lexer_peek_token (parser->lexer);
22509 id = cp_parser_template_id (parser,
22510 /*template_keyword_p=*/false,
22511 /*check_dependency_p=*/true,
22512 class_key,
22513 /*is_declaration=*/true);
22514 /* If that didn't work, it could still be an identifier. */
22515 if (!cp_parser_parse_definitely (parser))
22517 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
22519 type_start_token = cp_lexer_peek_token (parser->lexer);
22520 id = cp_parser_identifier (parser);
22522 else
22523 id = NULL_TREE;
22525 else
22527 template_id_p = true;
22528 ++num_templates;
22532 pop_deferring_access_checks ();
22534 if (id)
22536 cp_parser_check_for_invalid_template_id (parser, id,
22537 class_key,
22538 type_start_token->location);
22540 virt_specifiers = cp_parser_virt_specifier_seq_opt (parser);
22542 /* If it's not a `:' or a `{' then we can't really be looking at a
22543 class-head, since a class-head only appears as part of a
22544 class-specifier. We have to detect this situation before calling
22545 xref_tag, since that has irreversible side-effects. */
22546 if (!cp_parser_next_token_starts_class_definition_p (parser))
22548 cp_parser_error (parser, "expected %<{%> or %<:%>");
22549 type = error_mark_node;
22550 goto out;
22553 /* At this point, we're going ahead with the class-specifier, even
22554 if some other problem occurs. */
22555 cp_parser_commit_to_tentative_parse (parser);
22556 if (virt_specifiers & VIRT_SPEC_OVERRIDE)
22558 cp_parser_error (parser,
22559 "cannot specify %<override%> for a class");
22560 type = error_mark_node;
22561 goto out;
22563 /* Issue the error about the overly-qualified name now. */
22564 if (qualified_p)
22566 cp_parser_error (parser,
22567 "global qualification of class name is invalid");
22568 type = error_mark_node;
22569 goto out;
22571 else if (invalid_nested_name_p)
22573 cp_parser_error (parser,
22574 "qualified name does not name a class");
22575 type = error_mark_node;
22576 goto out;
22578 else if (nested_name_specifier)
22580 tree scope;
22582 /* Reject typedef-names in class heads. */
22583 if (!DECL_IMPLICIT_TYPEDEF_P (type))
22585 error_at (type_start_token->location,
22586 "invalid class name in declaration of %qD",
22587 type);
22588 type = NULL_TREE;
22589 goto done;
22592 /* Figure out in what scope the declaration is being placed. */
22593 scope = current_scope ();
22594 /* If that scope does not contain the scope in which the
22595 class was originally declared, the program is invalid. */
22596 if (scope && !is_ancestor (scope, nested_name_specifier))
22598 if (at_namespace_scope_p ())
22599 error_at (type_start_token->location,
22600 "declaration of %qD in namespace %qD which does not "
22601 "enclose %qD",
22602 type, scope, nested_name_specifier);
22603 else
22604 error_at (type_start_token->location,
22605 "declaration of %qD in %qD which does not enclose %qD",
22606 type, scope, nested_name_specifier);
22607 type = NULL_TREE;
22608 goto done;
22610 /* [dcl.meaning]
22612 A declarator-id shall not be qualified except for the
22613 definition of a ... nested class outside of its class
22614 ... [or] the definition or explicit instantiation of a
22615 class member of a namespace outside of its namespace. */
22616 if (scope == nested_name_specifier)
22618 permerror (nested_name_specifier_token_start->location,
22619 "extra qualification not allowed");
22620 nested_name_specifier = NULL_TREE;
22621 num_templates = 0;
22624 /* An explicit-specialization must be preceded by "template <>". If
22625 it is not, try to recover gracefully. */
22626 if (at_namespace_scope_p ()
22627 && parser->num_template_parameter_lists == 0
22628 && !processing_template_parmlist
22629 && template_id_p)
22631 /* Build a location of this form:
22632 struct typename <ARGS>
22633 ^~~~~~~~~~~~~~~~~~~~~~
22634 with caret==start at the start token, and
22635 finishing at the end of the type. */
22636 location_t reported_loc
22637 = make_location (class_head_start_location,
22638 class_head_start_location,
22639 get_finish (type_start_token->location));
22640 rich_location richloc (line_table, reported_loc);
22641 richloc.add_fixit_insert_before (class_head_start_location,
22642 "template <> ");
22643 error_at_rich_loc
22644 (&richloc,
22645 "an explicit specialization must be preceded by %<template <>%>");
22646 invalid_explicit_specialization_p = true;
22647 /* Take the same action that would have been taken by
22648 cp_parser_explicit_specialization. */
22649 ++parser->num_template_parameter_lists;
22650 begin_specialization ();
22652 /* There must be no "return" statements between this point and the
22653 end of this function; set "type "to the correct return value and
22654 use "goto done;" to return. */
22655 /* Make sure that the right number of template parameters were
22656 present. */
22657 if (!cp_parser_check_template_parameters (parser, num_templates,
22658 type_start_token->location,
22659 /*declarator=*/NULL))
22661 /* If something went wrong, there is no point in even trying to
22662 process the class-definition. */
22663 type = NULL_TREE;
22664 goto done;
22667 /* Look up the type. */
22668 if (template_id_p)
22670 if (TREE_CODE (id) == TEMPLATE_ID_EXPR
22671 && (DECL_FUNCTION_TEMPLATE_P (TREE_OPERAND (id, 0))
22672 || TREE_CODE (TREE_OPERAND (id, 0)) == OVERLOAD))
22674 error_at (type_start_token->location,
22675 "function template %qD redeclared as a class template", id);
22676 type = error_mark_node;
22678 else
22680 type = TREE_TYPE (id);
22681 type = maybe_process_partial_specialization (type);
22683 /* Check the scope while we still know whether or not we had a
22684 nested-name-specifier. */
22685 if (type != error_mark_node)
22686 check_unqualified_spec_or_inst (type, type_start_token->location);
22688 if (nested_name_specifier)
22689 pushed_scope = push_scope (nested_name_specifier);
22691 else if (nested_name_specifier)
22693 tree class_type;
22695 /* Given:
22697 template <typename T> struct S { struct T };
22698 template <typename T> struct S<T>::T { };
22700 we will get a TYPENAME_TYPE when processing the definition of
22701 `S::T'. We need to resolve it to the actual type before we
22702 try to define it. */
22703 if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
22705 class_type = resolve_typename_type (TREE_TYPE (type),
22706 /*only_current_p=*/false);
22707 if (TREE_CODE (class_type) != TYPENAME_TYPE)
22708 type = TYPE_NAME (class_type);
22709 else
22711 cp_parser_error (parser, "could not resolve typename type");
22712 type = error_mark_node;
22716 if (maybe_process_partial_specialization (TREE_TYPE (type))
22717 == error_mark_node)
22719 type = NULL_TREE;
22720 goto done;
22723 class_type = current_class_type;
22724 /* Enter the scope indicated by the nested-name-specifier. */
22725 pushed_scope = push_scope (nested_name_specifier);
22726 /* Get the canonical version of this type. */
22727 type = TYPE_MAIN_DECL (TREE_TYPE (type));
22728 /* Call push_template_decl if it seems like we should be defining a
22729 template either from the template headers or the type we're
22730 defining, so that we diagnose both extra and missing headers. */
22731 if ((PROCESSING_REAL_TEMPLATE_DECL_P ()
22732 || CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (type)))
22733 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
22735 type = push_template_decl (type);
22736 if (type == error_mark_node)
22738 type = NULL_TREE;
22739 goto done;
22743 type = TREE_TYPE (type);
22744 *nested_name_specifier_p = true;
22746 else /* The name is not a nested name. */
22748 /* If the class was unnamed, create a dummy name. */
22749 if (!id)
22750 id = make_anon_name ();
22751 tag_scope tag_scope = (parser->in_type_id_in_expr_p
22752 ? ts_within_enclosing_non_class
22753 : ts_current);
22754 type = xref_tag (class_key, id, tag_scope,
22755 parser->num_template_parameter_lists);
22758 /* Indicate whether this class was declared as a `class' or as a
22759 `struct'. */
22760 if (TREE_CODE (type) == RECORD_TYPE)
22761 CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
22762 cp_parser_check_class_key (class_key, type);
22764 /* If this type was already complete, and we see another definition,
22765 that's an error. */
22766 if (type != error_mark_node && COMPLETE_TYPE_P (type))
22768 error_at (type_start_token->location, "redefinition of %q#T",
22769 type);
22770 inform (location_of (type), "previous definition of %q#T",
22771 type);
22772 type = NULL_TREE;
22773 goto done;
22775 else if (type == error_mark_node)
22776 type = NULL_TREE;
22778 if (type)
22780 /* Apply attributes now, before any use of the class as a template
22781 argument in its base list. */
22782 cplus_decl_attributes (&type, attributes, (int)ATTR_FLAG_TYPE_IN_PLACE);
22783 fixup_attribute_variants (type);
22786 /* We will have entered the scope containing the class; the names of
22787 base classes should be looked up in that context. For example:
22789 struct A { struct B {}; struct C; };
22790 struct A::C : B {};
22792 is valid. */
22794 /* Get the list of base-classes, if there is one. */
22795 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
22797 /* PR59482: enter the class scope so that base-specifiers are looked
22798 up correctly. */
22799 if (type)
22800 pushclass (type);
22801 bases = cp_parser_base_clause (parser);
22802 /* PR59482: get out of the previously pushed class scope so that the
22803 subsequent pops pop the right thing. */
22804 if (type)
22805 popclass ();
22807 else
22808 bases = NULL_TREE;
22810 /* If we're really defining a class, process the base classes.
22811 If they're invalid, fail. */
22812 if (type && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
22813 xref_basetypes (type, bases);
22815 done:
22816 /* Leave the scope given by the nested-name-specifier. We will
22817 enter the class scope itself while processing the members. */
22818 if (pushed_scope)
22819 pop_scope (pushed_scope);
22821 if (invalid_explicit_specialization_p)
22823 end_specialization ();
22824 --parser->num_template_parameter_lists;
22827 if (type)
22828 DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
22829 if (type && (virt_specifiers & VIRT_SPEC_FINAL))
22830 CLASSTYPE_FINAL (type) = 1;
22831 out:
22832 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
22833 return type;
22836 /* Parse a class-key.
22838 class-key:
22839 class
22840 struct
22841 union
22843 Returns the kind of class-key specified, or none_type to indicate
22844 error. */
22846 static enum tag_types
22847 cp_parser_class_key (cp_parser* parser)
22849 cp_token *token;
22850 enum tag_types tag_type;
22852 /* Look for the class-key. */
22853 token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_KEY);
22854 if (!token)
22855 return none_type;
22857 /* Check to see if the TOKEN is a class-key. */
22858 tag_type = cp_parser_token_is_class_key (token);
22859 if (!tag_type)
22860 cp_parser_error (parser, "expected class-key");
22861 return tag_type;
22864 /* Parse a type-parameter-key.
22866 type-parameter-key:
22867 class
22868 typename
22871 static void
22872 cp_parser_type_parameter_key (cp_parser* parser)
22874 /* Look for the type-parameter-key. */
22875 enum tag_types tag_type = none_type;
22876 cp_token *token = cp_lexer_peek_token (parser->lexer);
22877 if ((tag_type = cp_parser_token_is_type_parameter_key (token)) != none_type)
22879 cp_lexer_consume_token (parser->lexer);
22880 if (pedantic && tag_type == typename_type && cxx_dialect < cxx1z)
22881 /* typename is not allowed in a template template parameter
22882 by the standard until C++1Z. */
22883 pedwarn (token->location, OPT_Wpedantic,
22884 "ISO C++ forbids typename key in template template parameter;"
22885 " use -std=c++1z or -std=gnu++1z");
22887 else
22888 cp_parser_error (parser, "expected %<class%> or %<typename%>");
22890 return;
22893 /* Parse an (optional) member-specification.
22895 member-specification:
22896 member-declaration member-specification [opt]
22897 access-specifier : member-specification [opt] */
22899 static void
22900 cp_parser_member_specification_opt (cp_parser* parser)
22902 while (true)
22904 cp_token *token;
22905 enum rid keyword;
22907 /* Peek at the next token. */
22908 token = cp_lexer_peek_token (parser->lexer);
22909 /* If it's a `}', or EOF then we've seen all the members. */
22910 if (token->type == CPP_CLOSE_BRACE
22911 || token->type == CPP_EOF
22912 || token->type == CPP_PRAGMA_EOL)
22913 break;
22915 /* See if this token is a keyword. */
22916 keyword = token->keyword;
22917 switch (keyword)
22919 case RID_PUBLIC:
22920 case RID_PROTECTED:
22921 case RID_PRIVATE:
22922 /* Consume the access-specifier. */
22923 cp_lexer_consume_token (parser->lexer);
22924 /* Remember which access-specifier is active. */
22925 current_access_specifier = token->u.value;
22926 /* Look for the `:'. */
22927 cp_parser_require (parser, CPP_COLON, RT_COLON);
22928 break;
22930 default:
22931 /* Accept #pragmas at class scope. */
22932 if (token->type == CPP_PRAGMA)
22934 cp_parser_pragma (parser, pragma_member, NULL);
22935 break;
22938 /* Otherwise, the next construction must be a
22939 member-declaration. */
22940 cp_parser_member_declaration (parser);
22945 /* Parse a member-declaration.
22947 member-declaration:
22948 decl-specifier-seq [opt] member-declarator-list [opt] ;
22949 function-definition ; [opt]
22950 :: [opt] nested-name-specifier template [opt] unqualified-id ;
22951 using-declaration
22952 template-declaration
22953 alias-declaration
22955 member-declarator-list:
22956 member-declarator
22957 member-declarator-list , member-declarator
22959 member-declarator:
22960 declarator pure-specifier [opt]
22961 declarator constant-initializer [opt]
22962 identifier [opt] : constant-expression
22964 GNU Extensions:
22966 member-declaration:
22967 __extension__ member-declaration
22969 member-declarator:
22970 declarator attributes [opt] pure-specifier [opt]
22971 declarator attributes [opt] constant-initializer [opt]
22972 identifier [opt] attributes [opt] : constant-expression
22974 C++0x Extensions:
22976 member-declaration:
22977 static_assert-declaration */
22979 static void
22980 cp_parser_member_declaration (cp_parser* parser)
22982 cp_decl_specifier_seq decl_specifiers;
22983 tree prefix_attributes;
22984 tree decl;
22985 int declares_class_or_enum;
22986 bool friend_p;
22987 cp_token *token = NULL;
22988 cp_token *decl_spec_token_start = NULL;
22989 cp_token *initializer_token_start = NULL;
22990 int saved_pedantic;
22991 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
22993 /* Check for the `__extension__' keyword. */
22994 if (cp_parser_extension_opt (parser, &saved_pedantic))
22996 /* Recurse. */
22997 cp_parser_member_declaration (parser);
22998 /* Restore the old value of the PEDANTIC flag. */
22999 pedantic = saved_pedantic;
23001 return;
23004 /* Check for a template-declaration. */
23005 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
23007 /* An explicit specialization here is an error condition, and we
23008 expect the specialization handler to detect and report this. */
23009 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
23010 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
23011 cp_parser_explicit_specialization (parser);
23012 else
23013 cp_parser_template_declaration (parser, /*member_p=*/true);
23015 return;
23017 /* Check for a template introduction. */
23018 else if (cp_parser_template_declaration_after_export (parser, true))
23019 return;
23021 /* Check for a using-declaration. */
23022 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
23024 if (cxx_dialect < cxx11)
23026 /* Parse the using-declaration. */
23027 cp_parser_using_declaration (parser,
23028 /*access_declaration_p=*/false);
23029 return;
23031 else
23033 tree decl;
23034 bool alias_decl_expected;
23035 cp_parser_parse_tentatively (parser);
23036 decl = cp_parser_alias_declaration (parser);
23037 /* Note that if we actually see the '=' token after the
23038 identifier, cp_parser_alias_declaration commits the
23039 tentative parse. In that case, we really expect an
23040 alias-declaration. Otherwise, we expect a using
23041 declaration. */
23042 alias_decl_expected =
23043 !cp_parser_uncommitted_to_tentative_parse_p (parser);
23044 cp_parser_parse_definitely (parser);
23046 if (alias_decl_expected)
23047 finish_member_declaration (decl);
23048 else
23049 cp_parser_using_declaration (parser,
23050 /*access_declaration_p=*/false);
23051 return;
23055 /* Check for @defs. */
23056 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_DEFS))
23058 tree ivar, member;
23059 tree ivar_chains = cp_parser_objc_defs_expression (parser);
23060 ivar = ivar_chains;
23061 while (ivar)
23063 member = ivar;
23064 ivar = TREE_CHAIN (member);
23065 TREE_CHAIN (member) = NULL_TREE;
23066 finish_member_declaration (member);
23068 return;
23071 /* If the next token is `static_assert' we have a static assertion. */
23072 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC_ASSERT))
23074 cp_parser_static_assert (parser, /*member_p=*/true);
23075 return;
23078 parser->colon_corrects_to_scope_p = false;
23080 if (cp_parser_using_declaration (parser, /*access_declaration=*/true))
23081 goto out;
23083 /* Parse the decl-specifier-seq. */
23084 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
23085 cp_parser_decl_specifier_seq (parser,
23086 CP_PARSER_FLAGS_OPTIONAL,
23087 &decl_specifiers,
23088 &declares_class_or_enum);
23089 /* Check for an invalid type-name. */
23090 if (!decl_specifiers.any_type_specifiers_p
23091 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
23092 goto out;
23093 /* If there is no declarator, then the decl-specifier-seq should
23094 specify a type. */
23095 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
23097 /* If there was no decl-specifier-seq, and the next token is a
23098 `;', then we have something like:
23100 struct S { ; };
23102 [class.mem]
23104 Each member-declaration shall declare at least one member
23105 name of the class. */
23106 if (!decl_specifiers.any_specifiers_p)
23108 cp_token *token = cp_lexer_peek_token (parser->lexer);
23109 if (!in_system_header_at (token->location))
23110 pedwarn (token->location, OPT_Wpedantic, "extra %<;%>");
23112 else
23114 tree type;
23116 /* See if this declaration is a friend. */
23117 friend_p = cp_parser_friend_p (&decl_specifiers);
23118 /* If there were decl-specifiers, check to see if there was
23119 a class-declaration. */
23120 type = check_tag_decl (&decl_specifiers,
23121 /*explicit_type_instantiation_p=*/false);
23122 /* Nested classes have already been added to the class, but
23123 a `friend' needs to be explicitly registered. */
23124 if (friend_p)
23126 /* If the `friend' keyword was present, the friend must
23127 be introduced with a class-key. */
23128 if (!declares_class_or_enum && cxx_dialect < cxx11)
23129 pedwarn (decl_spec_token_start->location, OPT_Wpedantic,
23130 "in C++03 a class-key must be used "
23131 "when declaring a friend");
23132 /* In this case:
23134 template <typename T> struct A {
23135 friend struct A<T>::B;
23138 A<T>::B will be represented by a TYPENAME_TYPE, and
23139 therefore not recognized by check_tag_decl. */
23140 if (!type)
23142 type = decl_specifiers.type;
23143 if (type && TREE_CODE (type) == TYPE_DECL)
23144 type = TREE_TYPE (type);
23146 if (!type || !TYPE_P (type))
23147 error_at (decl_spec_token_start->location,
23148 "friend declaration does not name a class or "
23149 "function");
23150 else
23151 make_friend_class (current_class_type, type,
23152 /*complain=*/true);
23154 /* If there is no TYPE, an error message will already have
23155 been issued. */
23156 else if (!type || type == error_mark_node)
23158 /* An anonymous aggregate has to be handled specially; such
23159 a declaration really declares a data member (with a
23160 particular type), as opposed to a nested class. */
23161 else if (ANON_AGGR_TYPE_P (type))
23163 /* C++11 9.5/6. */
23164 if (decl_specifiers.storage_class != sc_none)
23165 error_at (decl_spec_token_start->location,
23166 "a storage class on an anonymous aggregate "
23167 "in class scope is not allowed");
23169 /* Remove constructors and such from TYPE, now that we
23170 know it is an anonymous aggregate. */
23171 fixup_anonymous_aggr (type);
23172 /* And make the corresponding data member. */
23173 decl = build_decl (decl_spec_token_start->location,
23174 FIELD_DECL, NULL_TREE, type);
23175 /* Add it to the class. */
23176 finish_member_declaration (decl);
23178 else
23179 cp_parser_check_access_in_redeclaration
23180 (TYPE_NAME (type),
23181 decl_spec_token_start->location);
23184 else
23186 bool assume_semicolon = false;
23188 /* Clear attributes from the decl_specifiers but keep them
23189 around as prefix attributes that apply them to the entity
23190 being declared. */
23191 prefix_attributes = decl_specifiers.attributes;
23192 decl_specifiers.attributes = NULL_TREE;
23194 /* See if these declarations will be friends. */
23195 friend_p = cp_parser_friend_p (&decl_specifiers);
23197 /* Keep going until we hit the `;' at the end of the
23198 declaration. */
23199 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
23201 tree attributes = NULL_TREE;
23202 tree first_attribute;
23204 /* Peek at the next token. */
23205 token = cp_lexer_peek_token (parser->lexer);
23207 /* Check for a bitfield declaration. */
23208 if (token->type == CPP_COLON
23209 || (token->type == CPP_NAME
23210 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
23211 == CPP_COLON))
23213 tree identifier;
23214 tree width;
23216 /* Get the name of the bitfield. Note that we cannot just
23217 check TOKEN here because it may have been invalidated by
23218 the call to cp_lexer_peek_nth_token above. */
23219 if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
23220 identifier = cp_parser_identifier (parser);
23221 else
23222 identifier = NULL_TREE;
23224 /* Consume the `:' token. */
23225 cp_lexer_consume_token (parser->lexer);
23226 /* Get the width of the bitfield. */
23227 width
23228 = cp_parser_constant_expression (parser);
23230 /* Look for attributes that apply to the bitfield. */
23231 attributes = cp_parser_attributes_opt (parser);
23232 /* Remember which attributes are prefix attributes and
23233 which are not. */
23234 first_attribute = attributes;
23235 /* Combine the attributes. */
23236 attributes = chainon (prefix_attributes, attributes);
23238 /* Create the bitfield declaration. */
23239 decl = grokbitfield (identifier
23240 ? make_id_declarator (NULL_TREE,
23241 identifier,
23242 sfk_none)
23243 : NULL,
23244 &decl_specifiers,
23245 width,
23246 attributes);
23248 else
23250 cp_declarator *declarator;
23251 tree initializer;
23252 tree asm_specification;
23253 int ctor_dtor_or_conv_p;
23255 /* Parse the declarator. */
23256 declarator
23257 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
23258 &ctor_dtor_or_conv_p,
23259 /*parenthesized_p=*/NULL,
23260 /*member_p=*/true,
23261 friend_p);
23263 /* If something went wrong parsing the declarator, make sure
23264 that we at least consume some tokens. */
23265 if (declarator == cp_error_declarator)
23267 /* Skip to the end of the statement. */
23268 cp_parser_skip_to_end_of_statement (parser);
23269 /* If the next token is not a semicolon, that is
23270 probably because we just skipped over the body of
23271 a function. So, we consume a semicolon if
23272 present, but do not issue an error message if it
23273 is not present. */
23274 if (cp_lexer_next_token_is (parser->lexer,
23275 CPP_SEMICOLON))
23276 cp_lexer_consume_token (parser->lexer);
23277 goto out;
23280 if (declares_class_or_enum & 2)
23281 cp_parser_check_for_definition_in_return_type
23282 (declarator, decl_specifiers.type,
23283 decl_specifiers.locations[ds_type_spec]);
23285 /* Look for an asm-specification. */
23286 asm_specification = cp_parser_asm_specification_opt (parser);
23287 /* Look for attributes that apply to the declaration. */
23288 attributes = cp_parser_attributes_opt (parser);
23289 /* Remember which attributes are prefix attributes and
23290 which are not. */
23291 first_attribute = attributes;
23292 /* Combine the attributes. */
23293 attributes = chainon (prefix_attributes, attributes);
23295 /* If it's an `=', then we have a constant-initializer or a
23296 pure-specifier. It is not correct to parse the
23297 initializer before registering the member declaration
23298 since the member declaration should be in scope while
23299 its initializer is processed. However, the rest of the
23300 front end does not yet provide an interface that allows
23301 us to handle this correctly. */
23302 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
23304 /* In [class.mem]:
23306 A pure-specifier shall be used only in the declaration of
23307 a virtual function.
23309 A member-declarator can contain a constant-initializer
23310 only if it declares a static member of integral or
23311 enumeration type.
23313 Therefore, if the DECLARATOR is for a function, we look
23314 for a pure-specifier; otherwise, we look for a
23315 constant-initializer. When we call `grokfield', it will
23316 perform more stringent semantics checks. */
23317 initializer_token_start = cp_lexer_peek_token (parser->lexer);
23318 if (function_declarator_p (declarator)
23319 || (decl_specifiers.type
23320 && TREE_CODE (decl_specifiers.type) == TYPE_DECL
23321 && declarator->kind == cdk_id
23322 && (TREE_CODE (TREE_TYPE (decl_specifiers.type))
23323 == FUNCTION_TYPE)))
23324 initializer = cp_parser_pure_specifier (parser);
23325 else if (decl_specifiers.storage_class != sc_static)
23326 initializer = cp_parser_save_nsdmi (parser);
23327 else if (cxx_dialect >= cxx11)
23329 bool nonconst;
23330 /* Don't require a constant rvalue in C++11, since we
23331 might want a reference constant. We'll enforce
23332 constancy later. */
23333 cp_lexer_consume_token (parser->lexer);
23334 /* Parse the initializer. */
23335 initializer = cp_parser_initializer_clause (parser,
23336 &nonconst);
23338 else
23339 /* Parse the initializer. */
23340 initializer = cp_parser_constant_initializer (parser);
23342 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
23343 && !function_declarator_p (declarator))
23345 bool x;
23346 if (decl_specifiers.storage_class != sc_static)
23347 initializer = cp_parser_save_nsdmi (parser);
23348 else
23349 initializer = cp_parser_initializer (parser, &x, &x);
23351 /* Otherwise, there is no initializer. */
23352 else
23353 initializer = NULL_TREE;
23355 /* See if we are probably looking at a function
23356 definition. We are certainly not looking at a
23357 member-declarator. Calling `grokfield' has
23358 side-effects, so we must not do it unless we are sure
23359 that we are looking at a member-declarator. */
23360 if (cp_parser_token_starts_function_definition_p
23361 (cp_lexer_peek_token (parser->lexer)))
23363 /* The grammar does not allow a pure-specifier to be
23364 used when a member function is defined. (It is
23365 possible that this fact is an oversight in the
23366 standard, since a pure function may be defined
23367 outside of the class-specifier. */
23368 if (initializer && initializer_token_start)
23369 error_at (initializer_token_start->location,
23370 "pure-specifier on function-definition");
23371 decl = cp_parser_save_member_function_body (parser,
23372 &decl_specifiers,
23373 declarator,
23374 attributes);
23375 if (parser->fully_implicit_function_template_p)
23376 decl = finish_fully_implicit_template (parser, decl);
23377 /* If the member was not a friend, declare it here. */
23378 if (!friend_p)
23379 finish_member_declaration (decl);
23380 /* Peek at the next token. */
23381 token = cp_lexer_peek_token (parser->lexer);
23382 /* If the next token is a semicolon, consume it. */
23383 if (token->type == CPP_SEMICOLON)
23384 cp_lexer_consume_token (parser->lexer);
23385 goto out;
23387 else
23388 if (declarator->kind == cdk_function)
23389 declarator->id_loc = token->location;
23390 /* Create the declaration. */
23391 decl = grokfield (declarator, &decl_specifiers,
23392 initializer, /*init_const_expr_p=*/true,
23393 asm_specification, attributes);
23394 if (parser->fully_implicit_function_template_p)
23396 if (friend_p)
23397 finish_fully_implicit_template (parser, 0);
23398 else
23399 decl = finish_fully_implicit_template (parser, decl);
23403 cp_finalize_omp_declare_simd (parser, decl);
23404 cp_finalize_oacc_routine (parser, decl, false);
23406 /* Reset PREFIX_ATTRIBUTES. */
23407 while (attributes && TREE_CHAIN (attributes) != first_attribute)
23408 attributes = TREE_CHAIN (attributes);
23409 if (attributes)
23410 TREE_CHAIN (attributes) = NULL_TREE;
23412 /* If there is any qualification still in effect, clear it
23413 now; we will be starting fresh with the next declarator. */
23414 parser->scope = NULL_TREE;
23415 parser->qualifying_scope = NULL_TREE;
23416 parser->object_scope = NULL_TREE;
23417 /* If it's a `,', then there are more declarators. */
23418 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
23420 cp_lexer_consume_token (parser->lexer);
23421 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
23423 cp_token *token = cp_lexer_previous_token (parser->lexer);
23424 error_at (token->location,
23425 "stray %<,%> at end of member declaration");
23428 /* If the next token isn't a `;', then we have a parse error. */
23429 else if (cp_lexer_next_token_is_not (parser->lexer,
23430 CPP_SEMICOLON))
23432 /* The next token might be a ways away from where the
23433 actual semicolon is missing. Find the previous token
23434 and use that for our error position. */
23435 cp_token *token = cp_lexer_previous_token (parser->lexer);
23436 error_at (token->location,
23437 "expected %<;%> at end of member declaration");
23439 /* Assume that the user meant to provide a semicolon. If
23440 we were to cp_parser_skip_to_end_of_statement, we might
23441 skip to a semicolon inside a member function definition
23442 and issue nonsensical error messages. */
23443 assume_semicolon = true;
23446 if (decl)
23448 /* Add DECL to the list of members. */
23449 if (!friend_p
23450 /* Explicitly include, eg, NSDMIs, for better error
23451 recovery (c++/58650). */
23452 || !DECL_DECLARES_FUNCTION_P (decl))
23453 finish_member_declaration (decl);
23455 if (TREE_CODE (decl) == FUNCTION_DECL)
23456 cp_parser_save_default_args (parser, decl);
23457 else if (TREE_CODE (decl) == FIELD_DECL
23458 && !DECL_C_BIT_FIELD (decl)
23459 && DECL_INITIAL (decl))
23460 /* Add DECL to the queue of NSDMI to be parsed later. */
23461 vec_safe_push (unparsed_nsdmis, decl);
23464 if (assume_semicolon)
23465 goto out;
23469 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
23470 out:
23471 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
23474 /* Parse a pure-specifier.
23476 pure-specifier:
23479 Returns INTEGER_ZERO_NODE if a pure specifier is found.
23480 Otherwise, ERROR_MARK_NODE is returned. */
23482 static tree
23483 cp_parser_pure_specifier (cp_parser* parser)
23485 cp_token *token;
23487 /* Look for the `=' token. */
23488 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
23489 return error_mark_node;
23490 /* Look for the `0' token. */
23491 token = cp_lexer_peek_token (parser->lexer);
23493 if (token->type == CPP_EOF
23494 || token->type == CPP_PRAGMA_EOL)
23495 return error_mark_node;
23497 cp_lexer_consume_token (parser->lexer);
23499 /* Accept = default or = delete in c++0x mode. */
23500 if (token->keyword == RID_DEFAULT
23501 || token->keyword == RID_DELETE)
23503 maybe_warn_cpp0x (CPP0X_DEFAULTED_DELETED);
23504 return token->u.value;
23507 /* c_lex_with_flags marks a single digit '0' with PURE_ZERO. */
23508 if (token->type != CPP_NUMBER || !(token->flags & PURE_ZERO))
23510 cp_parser_error (parser,
23511 "invalid pure specifier (only %<= 0%> is allowed)");
23512 cp_parser_skip_to_end_of_statement (parser);
23513 return error_mark_node;
23515 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
23517 error_at (token->location, "templates may not be %<virtual%>");
23518 return error_mark_node;
23521 return integer_zero_node;
23524 /* Parse a constant-initializer.
23526 constant-initializer:
23527 = constant-expression
23529 Returns a representation of the constant-expression. */
23531 static tree
23532 cp_parser_constant_initializer (cp_parser* parser)
23534 /* Look for the `=' token. */
23535 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
23536 return error_mark_node;
23538 /* It is invalid to write:
23540 struct S { static const int i = { 7 }; };
23543 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
23545 cp_parser_error (parser,
23546 "a brace-enclosed initializer is not allowed here");
23547 /* Consume the opening brace. */
23548 cp_lexer_consume_token (parser->lexer);
23549 /* Skip the initializer. */
23550 cp_parser_skip_to_closing_brace (parser);
23551 /* Look for the trailing `}'. */
23552 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
23554 return error_mark_node;
23557 return cp_parser_constant_expression (parser);
23560 /* Derived classes [gram.class.derived] */
23562 /* Parse a base-clause.
23564 base-clause:
23565 : base-specifier-list
23567 base-specifier-list:
23568 base-specifier ... [opt]
23569 base-specifier-list , base-specifier ... [opt]
23571 Returns a TREE_LIST representing the base-classes, in the order in
23572 which they were declared. The representation of each node is as
23573 described by cp_parser_base_specifier.
23575 In the case that no bases are specified, this function will return
23576 NULL_TREE, not ERROR_MARK_NODE. */
23578 static tree
23579 cp_parser_base_clause (cp_parser* parser)
23581 tree bases = NULL_TREE;
23583 /* Look for the `:' that begins the list. */
23584 cp_parser_require (parser, CPP_COLON, RT_COLON);
23586 /* Scan the base-specifier-list. */
23587 while (true)
23589 cp_token *token;
23590 tree base;
23591 bool pack_expansion_p = false;
23593 /* Look for the base-specifier. */
23594 base = cp_parser_base_specifier (parser);
23595 /* Look for the (optional) ellipsis. */
23596 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
23598 /* Consume the `...'. */
23599 cp_lexer_consume_token (parser->lexer);
23601 pack_expansion_p = true;
23604 /* Add BASE to the front of the list. */
23605 if (base && base != error_mark_node)
23607 if (pack_expansion_p)
23608 /* Make this a pack expansion type. */
23609 TREE_VALUE (base) = make_pack_expansion (TREE_VALUE (base));
23611 if (!check_for_bare_parameter_packs (TREE_VALUE (base)))
23613 TREE_CHAIN (base) = bases;
23614 bases = base;
23617 /* Peek at the next token. */
23618 token = cp_lexer_peek_token (parser->lexer);
23619 /* If it's not a comma, then the list is complete. */
23620 if (token->type != CPP_COMMA)
23621 break;
23622 /* Consume the `,'. */
23623 cp_lexer_consume_token (parser->lexer);
23626 /* PARSER->SCOPE may still be non-NULL at this point, if the last
23627 base class had a qualified name. However, the next name that
23628 appears is certainly not qualified. */
23629 parser->scope = NULL_TREE;
23630 parser->qualifying_scope = NULL_TREE;
23631 parser->object_scope = NULL_TREE;
23633 return nreverse (bases);
23636 /* Parse a base-specifier.
23638 base-specifier:
23639 :: [opt] nested-name-specifier [opt] class-name
23640 virtual access-specifier [opt] :: [opt] nested-name-specifier
23641 [opt] class-name
23642 access-specifier virtual [opt] :: [opt] nested-name-specifier
23643 [opt] class-name
23645 Returns a TREE_LIST. The TREE_PURPOSE will be one of
23646 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
23647 indicate the specifiers provided. The TREE_VALUE will be a TYPE
23648 (or the ERROR_MARK_NODE) indicating the type that was specified. */
23650 static tree
23651 cp_parser_base_specifier (cp_parser* parser)
23653 cp_token *token;
23654 bool done = false;
23655 bool virtual_p = false;
23656 bool duplicate_virtual_error_issued_p = false;
23657 bool duplicate_access_error_issued_p = false;
23658 bool class_scope_p, template_p;
23659 tree access = access_default_node;
23660 tree type;
23662 /* Process the optional `virtual' and `access-specifier'. */
23663 while (!done)
23665 /* Peek at the next token. */
23666 token = cp_lexer_peek_token (parser->lexer);
23667 /* Process `virtual'. */
23668 switch (token->keyword)
23670 case RID_VIRTUAL:
23671 /* If `virtual' appears more than once, issue an error. */
23672 if (virtual_p && !duplicate_virtual_error_issued_p)
23674 cp_parser_error (parser,
23675 "%<virtual%> specified more than once in base-specified");
23676 duplicate_virtual_error_issued_p = true;
23679 virtual_p = true;
23681 /* Consume the `virtual' token. */
23682 cp_lexer_consume_token (parser->lexer);
23684 break;
23686 case RID_PUBLIC:
23687 case RID_PROTECTED:
23688 case RID_PRIVATE:
23689 /* If more than one access specifier appears, issue an
23690 error. */
23691 if (access != access_default_node
23692 && !duplicate_access_error_issued_p)
23694 cp_parser_error (parser,
23695 "more than one access specifier in base-specified");
23696 duplicate_access_error_issued_p = true;
23699 access = ridpointers[(int) token->keyword];
23701 /* Consume the access-specifier. */
23702 cp_lexer_consume_token (parser->lexer);
23704 break;
23706 default:
23707 done = true;
23708 break;
23711 /* It is not uncommon to see programs mechanically, erroneously, use
23712 the 'typename' keyword to denote (dependent) qualified types
23713 as base classes. */
23714 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
23716 token = cp_lexer_peek_token (parser->lexer);
23717 if (!processing_template_decl)
23718 error_at (token->location,
23719 "keyword %<typename%> not allowed outside of templates");
23720 else
23721 error_at (token->location,
23722 "keyword %<typename%> not allowed in this context "
23723 "(the base class is implicitly a type)");
23724 cp_lexer_consume_token (parser->lexer);
23727 /* Look for the optional `::' operator. */
23728 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
23729 /* Look for the nested-name-specifier. The simplest way to
23730 implement:
23732 [temp.res]
23734 The keyword `typename' is not permitted in a base-specifier or
23735 mem-initializer; in these contexts a qualified name that
23736 depends on a template-parameter is implicitly assumed to be a
23737 type name.
23739 is to pretend that we have seen the `typename' keyword at this
23740 point. */
23741 cp_parser_nested_name_specifier_opt (parser,
23742 /*typename_keyword_p=*/true,
23743 /*check_dependency_p=*/true,
23744 /*type_p=*/true,
23745 /*is_declaration=*/true);
23746 /* If the base class is given by a qualified name, assume that names
23747 we see are type names or templates, as appropriate. */
23748 class_scope_p = (parser->scope && TYPE_P (parser->scope));
23749 template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
23751 if (!parser->scope
23752 && cp_lexer_next_token_is_decltype (parser->lexer))
23753 /* DR 950 allows decltype as a base-specifier. */
23754 type = cp_parser_decltype (parser);
23755 else
23757 /* Otherwise, look for the class-name. */
23758 type = cp_parser_class_name (parser,
23759 class_scope_p,
23760 template_p,
23761 typename_type,
23762 /*check_dependency_p=*/true,
23763 /*class_head_p=*/false,
23764 /*is_declaration=*/true);
23765 type = TREE_TYPE (type);
23768 if (type == error_mark_node)
23769 return error_mark_node;
23771 return finish_base_specifier (type, access, virtual_p);
23774 /* Exception handling [gram.exception] */
23776 /* Parse an (optional) noexcept-specification.
23778 noexcept-specification:
23779 noexcept ( constant-expression ) [opt]
23781 If no noexcept-specification is present, returns NULL_TREE.
23782 Otherwise, if REQUIRE_CONSTEXPR is false, then either parse and return any
23783 expression if parentheses follow noexcept, or return BOOLEAN_TRUE_NODE if
23784 there are no parentheses. CONSUMED_EXPR will be set accordingly.
23785 Otherwise, returns a noexcept specification unless RETURN_COND is true,
23786 in which case a boolean condition is returned instead. */
23788 static tree
23789 cp_parser_noexcept_specification_opt (cp_parser* parser,
23790 bool require_constexpr,
23791 bool* consumed_expr,
23792 bool return_cond)
23794 cp_token *token;
23795 const char *saved_message;
23797 /* Peek at the next token. */
23798 token = cp_lexer_peek_token (parser->lexer);
23800 /* Is it a noexcept-specification? */
23801 if (cp_parser_is_keyword (token, RID_NOEXCEPT))
23803 tree expr;
23804 cp_lexer_consume_token (parser->lexer);
23806 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
23808 cp_lexer_consume_token (parser->lexer);
23810 if (require_constexpr)
23812 /* Types may not be defined in an exception-specification. */
23813 saved_message = parser->type_definition_forbidden_message;
23814 parser->type_definition_forbidden_message
23815 = G_("types may not be defined in an exception-specification");
23817 expr = cp_parser_constant_expression (parser);
23819 /* Restore the saved message. */
23820 parser->type_definition_forbidden_message = saved_message;
23822 else
23824 expr = cp_parser_expression (parser);
23825 *consumed_expr = true;
23828 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
23830 else
23832 expr = boolean_true_node;
23833 if (!require_constexpr)
23834 *consumed_expr = false;
23837 /* We cannot build a noexcept-spec right away because this will check
23838 that expr is a constexpr. */
23839 if (!return_cond)
23840 return build_noexcept_spec (expr, tf_warning_or_error);
23841 else
23842 return expr;
23844 else
23845 return NULL_TREE;
23848 /* Parse an (optional) exception-specification.
23850 exception-specification:
23851 throw ( type-id-list [opt] )
23853 Returns a TREE_LIST representing the exception-specification. The
23854 TREE_VALUE of each node is a type. */
23856 static tree
23857 cp_parser_exception_specification_opt (cp_parser* parser)
23859 cp_token *token;
23860 tree type_id_list;
23861 const char *saved_message;
23863 /* Peek at the next token. */
23864 token = cp_lexer_peek_token (parser->lexer);
23866 /* Is it a noexcept-specification? */
23867 type_id_list = cp_parser_noexcept_specification_opt (parser, true, NULL,
23868 false);
23869 if (type_id_list != NULL_TREE)
23870 return type_id_list;
23872 /* If it's not `throw', then there's no exception-specification. */
23873 if (!cp_parser_is_keyword (token, RID_THROW))
23874 return NULL_TREE;
23876 location_t loc = token->location;
23878 /* Consume the `throw'. */
23879 cp_lexer_consume_token (parser->lexer);
23881 /* Look for the `('. */
23882 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
23884 /* Peek at the next token. */
23885 token = cp_lexer_peek_token (parser->lexer);
23886 /* If it's not a `)', then there is a type-id-list. */
23887 if (token->type != CPP_CLOSE_PAREN)
23889 /* Types may not be defined in an exception-specification. */
23890 saved_message = parser->type_definition_forbidden_message;
23891 parser->type_definition_forbidden_message
23892 = G_("types may not be defined in an exception-specification");
23893 /* Parse the type-id-list. */
23894 type_id_list = cp_parser_type_id_list (parser);
23895 /* Restore the saved message. */
23896 parser->type_definition_forbidden_message = saved_message;
23898 if (cxx_dialect >= cxx1z)
23900 error_at (loc, "ISO C++1z does not allow dynamic exception "
23901 "specifications");
23902 type_id_list = NULL_TREE;
23904 else if (cxx_dialect >= cxx11 && !in_system_header_at (loc))
23905 warning_at (loc, OPT_Wdeprecated,
23906 "dynamic exception specifications are deprecated in "
23907 "C++11");
23909 /* In C++17, throw() is equivalent to noexcept (true). throw()
23910 is deprecated in C++11 and above as well, but is still widely used,
23911 so don't warn about it yet. */
23912 else if (cxx_dialect >= cxx1z)
23913 type_id_list = noexcept_true_spec;
23914 else
23915 type_id_list = empty_except_spec;
23917 /* Look for the `)'. */
23918 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
23920 return type_id_list;
23923 /* Parse an (optional) type-id-list.
23925 type-id-list:
23926 type-id ... [opt]
23927 type-id-list , type-id ... [opt]
23929 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
23930 in the order that the types were presented. */
23932 static tree
23933 cp_parser_type_id_list (cp_parser* parser)
23935 tree types = NULL_TREE;
23937 while (true)
23939 cp_token *token;
23940 tree type;
23942 token = cp_lexer_peek_token (parser->lexer);
23944 /* Get the next type-id. */
23945 type = cp_parser_type_id (parser);
23946 /* Check for invalid 'auto'. */
23947 if (flag_concepts && type_uses_auto (type))
23949 error_at (token->location,
23950 "invalid use of %<auto%> in exception-specification");
23951 type = error_mark_node;
23953 /* Parse the optional ellipsis. */
23954 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
23956 /* Consume the `...'. */
23957 cp_lexer_consume_token (parser->lexer);
23959 /* Turn the type into a pack expansion expression. */
23960 type = make_pack_expansion (type);
23962 /* Add it to the list. */
23963 types = add_exception_specifier (types, type, /*complain=*/1);
23964 /* Peek at the next token. */
23965 token = cp_lexer_peek_token (parser->lexer);
23966 /* If it is not a `,', we are done. */
23967 if (token->type != CPP_COMMA)
23968 break;
23969 /* Consume the `,'. */
23970 cp_lexer_consume_token (parser->lexer);
23973 return nreverse (types);
23976 /* Parse a try-block.
23978 try-block:
23979 try compound-statement handler-seq */
23981 static tree
23982 cp_parser_try_block (cp_parser* parser)
23984 tree try_block;
23986 cp_parser_require_keyword (parser, RID_TRY, RT_TRY);
23987 if (parser->in_function_body
23988 && DECL_DECLARED_CONSTEXPR_P (current_function_decl))
23989 error ("%<try%> in %<constexpr%> function");
23991 try_block = begin_try_block ();
23992 cp_parser_compound_statement (parser, NULL, BCS_TRY_BLOCK, false);
23993 finish_try_block (try_block);
23994 cp_parser_handler_seq (parser);
23995 finish_handler_sequence (try_block);
23997 return try_block;
24000 /* Parse a function-try-block.
24002 function-try-block:
24003 try ctor-initializer [opt] function-body handler-seq */
24005 static bool
24006 cp_parser_function_try_block (cp_parser* parser)
24008 tree compound_stmt;
24009 tree try_block;
24010 bool ctor_initializer_p;
24012 /* Look for the `try' keyword. */
24013 if (!cp_parser_require_keyword (parser, RID_TRY, RT_TRY))
24014 return false;
24015 /* Let the rest of the front end know where we are. */
24016 try_block = begin_function_try_block (&compound_stmt);
24017 /* Parse the function-body. */
24018 ctor_initializer_p = cp_parser_ctor_initializer_opt_and_function_body
24019 (parser, /*in_function_try_block=*/true);
24020 /* We're done with the `try' part. */
24021 finish_function_try_block (try_block);
24022 /* Parse the handlers. */
24023 cp_parser_handler_seq (parser);
24024 /* We're done with the handlers. */
24025 finish_function_handler_sequence (try_block, compound_stmt);
24027 return ctor_initializer_p;
24030 /* Parse a handler-seq.
24032 handler-seq:
24033 handler handler-seq [opt] */
24035 static void
24036 cp_parser_handler_seq (cp_parser* parser)
24038 while (true)
24040 cp_token *token;
24042 /* Parse the handler. */
24043 cp_parser_handler (parser);
24044 /* Peek at the next token. */
24045 token = cp_lexer_peek_token (parser->lexer);
24046 /* If it's not `catch' then there are no more handlers. */
24047 if (!cp_parser_is_keyword (token, RID_CATCH))
24048 break;
24052 /* Parse a handler.
24054 handler:
24055 catch ( exception-declaration ) compound-statement */
24057 static void
24058 cp_parser_handler (cp_parser* parser)
24060 tree handler;
24061 tree declaration;
24063 cp_parser_require_keyword (parser, RID_CATCH, RT_CATCH);
24064 handler = begin_handler ();
24065 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
24066 declaration = cp_parser_exception_declaration (parser);
24067 finish_handler_parms (declaration, handler);
24068 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
24069 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
24070 finish_handler (handler);
24073 /* Parse an exception-declaration.
24075 exception-declaration:
24076 type-specifier-seq declarator
24077 type-specifier-seq abstract-declarator
24078 type-specifier-seq
24081 Returns a VAR_DECL for the declaration, or NULL_TREE if the
24082 ellipsis variant is used. */
24084 static tree
24085 cp_parser_exception_declaration (cp_parser* parser)
24087 cp_decl_specifier_seq type_specifiers;
24088 cp_declarator *declarator;
24089 const char *saved_message;
24091 /* If it's an ellipsis, it's easy to handle. */
24092 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
24094 /* Consume the `...' token. */
24095 cp_lexer_consume_token (parser->lexer);
24096 return NULL_TREE;
24099 /* Types may not be defined in exception-declarations. */
24100 saved_message = parser->type_definition_forbidden_message;
24101 parser->type_definition_forbidden_message
24102 = G_("types may not be defined in exception-declarations");
24104 /* Parse the type-specifier-seq. */
24105 cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
24106 /*is_trailing_return=*/false,
24107 &type_specifiers);
24108 /* If it's a `)', then there is no declarator. */
24109 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
24110 declarator = NULL;
24111 else
24112 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
24113 /*ctor_dtor_or_conv_p=*/NULL,
24114 /*parenthesized_p=*/NULL,
24115 /*member_p=*/false,
24116 /*friend_p=*/false);
24118 /* Restore the saved message. */
24119 parser->type_definition_forbidden_message = saved_message;
24121 if (!type_specifiers.any_specifiers_p)
24122 return error_mark_node;
24124 return grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
24127 /* Parse a throw-expression.
24129 throw-expression:
24130 throw assignment-expression [opt]
24132 Returns a THROW_EXPR representing the throw-expression. */
24134 static tree
24135 cp_parser_throw_expression (cp_parser* parser)
24137 tree expression;
24138 cp_token* token;
24140 cp_parser_require_keyword (parser, RID_THROW, RT_THROW);
24141 token = cp_lexer_peek_token (parser->lexer);
24142 /* Figure out whether or not there is an assignment-expression
24143 following the "throw" keyword. */
24144 if (token->type == CPP_COMMA
24145 || token->type == CPP_SEMICOLON
24146 || token->type == CPP_CLOSE_PAREN
24147 || token->type == CPP_CLOSE_SQUARE
24148 || token->type == CPP_CLOSE_BRACE
24149 || token->type == CPP_COLON)
24150 expression = NULL_TREE;
24151 else
24152 expression = cp_parser_assignment_expression (parser);
24154 return build_throw (expression);
24157 /* GNU Extensions */
24159 /* Parse an (optional) asm-specification.
24161 asm-specification:
24162 asm ( string-literal )
24164 If the asm-specification is present, returns a STRING_CST
24165 corresponding to the string-literal. Otherwise, returns
24166 NULL_TREE. */
24168 static tree
24169 cp_parser_asm_specification_opt (cp_parser* parser)
24171 cp_token *token;
24172 tree asm_specification;
24174 /* Peek at the next token. */
24175 token = cp_lexer_peek_token (parser->lexer);
24176 /* If the next token isn't the `asm' keyword, then there's no
24177 asm-specification. */
24178 if (!cp_parser_is_keyword (token, RID_ASM))
24179 return NULL_TREE;
24181 /* Consume the `asm' token. */
24182 cp_lexer_consume_token (parser->lexer);
24183 /* Look for the `('. */
24184 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
24186 /* Look for the string-literal. */
24187 asm_specification = cp_parser_string_literal (parser, false, false);
24189 /* Look for the `)'. */
24190 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
24192 return asm_specification;
24195 /* Parse an asm-operand-list.
24197 asm-operand-list:
24198 asm-operand
24199 asm-operand-list , asm-operand
24201 asm-operand:
24202 string-literal ( expression )
24203 [ string-literal ] string-literal ( expression )
24205 Returns a TREE_LIST representing the operands. The TREE_VALUE of
24206 each node is the expression. The TREE_PURPOSE is itself a
24207 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
24208 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
24209 is a STRING_CST for the string literal before the parenthesis. Returns
24210 ERROR_MARK_NODE if any of the operands are invalid. */
24212 static tree
24213 cp_parser_asm_operand_list (cp_parser* parser)
24215 tree asm_operands = NULL_TREE;
24216 bool invalid_operands = false;
24218 while (true)
24220 tree string_literal;
24221 tree expression;
24222 tree name;
24224 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
24226 /* Consume the `[' token. */
24227 cp_lexer_consume_token (parser->lexer);
24228 /* Read the operand name. */
24229 name = cp_parser_identifier (parser);
24230 if (name != error_mark_node)
24231 name = build_string (IDENTIFIER_LENGTH (name),
24232 IDENTIFIER_POINTER (name));
24233 /* Look for the closing `]'. */
24234 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
24236 else
24237 name = NULL_TREE;
24238 /* Look for the string-literal. */
24239 string_literal = cp_parser_string_literal (parser, false, false);
24241 /* Look for the `('. */
24242 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
24243 /* Parse the expression. */
24244 expression = cp_parser_expression (parser);
24245 /* Look for the `)'. */
24246 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
24248 if (name == error_mark_node
24249 || string_literal == error_mark_node
24250 || expression == error_mark_node)
24251 invalid_operands = true;
24253 /* Add this operand to the list. */
24254 asm_operands = tree_cons (build_tree_list (name, string_literal),
24255 expression,
24256 asm_operands);
24257 /* If the next token is not a `,', there are no more
24258 operands. */
24259 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
24260 break;
24261 /* Consume the `,'. */
24262 cp_lexer_consume_token (parser->lexer);
24265 return invalid_operands ? error_mark_node : nreverse (asm_operands);
24268 /* Parse an asm-clobber-list.
24270 asm-clobber-list:
24271 string-literal
24272 asm-clobber-list , string-literal
24274 Returns a TREE_LIST, indicating the clobbers in the order that they
24275 appeared. The TREE_VALUE of each node is a STRING_CST. */
24277 static tree
24278 cp_parser_asm_clobber_list (cp_parser* parser)
24280 tree clobbers = NULL_TREE;
24282 while (true)
24284 tree string_literal;
24286 /* Look for the string literal. */
24287 string_literal = cp_parser_string_literal (parser, false, false);
24288 /* Add it to the list. */
24289 clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
24290 /* If the next token is not a `,', then the list is
24291 complete. */
24292 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
24293 break;
24294 /* Consume the `,' token. */
24295 cp_lexer_consume_token (parser->lexer);
24298 return clobbers;
24301 /* Parse an asm-label-list.
24303 asm-label-list:
24304 identifier
24305 asm-label-list , identifier
24307 Returns a TREE_LIST, indicating the labels in the order that they
24308 appeared. The TREE_VALUE of each node is a label. */
24310 static tree
24311 cp_parser_asm_label_list (cp_parser* parser)
24313 tree labels = NULL_TREE;
24315 while (true)
24317 tree identifier, label, name;
24319 /* Look for the identifier. */
24320 identifier = cp_parser_identifier (parser);
24321 if (!error_operand_p (identifier))
24323 label = lookup_label (identifier);
24324 if (TREE_CODE (label) == LABEL_DECL)
24326 TREE_USED (label) = 1;
24327 check_goto (label);
24328 name = build_string (IDENTIFIER_LENGTH (identifier),
24329 IDENTIFIER_POINTER (identifier));
24330 labels = tree_cons (name, label, labels);
24333 /* If the next token is not a `,', then the list is
24334 complete. */
24335 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
24336 break;
24337 /* Consume the `,' token. */
24338 cp_lexer_consume_token (parser->lexer);
24341 return nreverse (labels);
24344 /* Return TRUE iff the next tokens in the stream are possibly the
24345 beginning of a GNU extension attribute. */
24347 static bool
24348 cp_next_tokens_can_be_gnu_attribute_p (cp_parser *parser)
24350 return cp_nth_tokens_can_be_gnu_attribute_p (parser, 1);
24353 /* Return TRUE iff the next tokens in the stream are possibly the
24354 beginning of a standard C++-11 attribute specifier. */
24356 static bool
24357 cp_next_tokens_can_be_std_attribute_p (cp_parser *parser)
24359 return cp_nth_tokens_can_be_std_attribute_p (parser, 1);
24362 /* Return TRUE iff the next Nth tokens in the stream are possibly the
24363 beginning of a standard C++-11 attribute specifier. */
24365 static bool
24366 cp_nth_tokens_can_be_std_attribute_p (cp_parser *parser, size_t n)
24368 cp_token *token = cp_lexer_peek_nth_token (parser->lexer, n);
24370 return (cxx_dialect >= cxx11
24371 && ((token->type == CPP_KEYWORD && token->keyword == RID_ALIGNAS)
24372 || (token->type == CPP_OPEN_SQUARE
24373 && (token = cp_lexer_peek_nth_token (parser->lexer, n + 1))
24374 && token->type == CPP_OPEN_SQUARE)));
24377 /* Return TRUE iff the next Nth tokens in the stream are possibly the
24378 beginning of a GNU extension attribute. */
24380 static bool
24381 cp_nth_tokens_can_be_gnu_attribute_p (cp_parser *parser, size_t n)
24383 cp_token *token = cp_lexer_peek_nth_token (parser->lexer, n);
24385 return token->type == CPP_KEYWORD && token->keyword == RID_ATTRIBUTE;
24388 /* Return true iff the next tokens can be the beginning of either a
24389 GNU attribute list, or a standard C++11 attribute sequence. */
24391 static bool
24392 cp_next_tokens_can_be_attribute_p (cp_parser *parser)
24394 return (cp_next_tokens_can_be_gnu_attribute_p (parser)
24395 || cp_next_tokens_can_be_std_attribute_p (parser));
24398 /* Return true iff the next Nth tokens can be the beginning of either
24399 a GNU attribute list, or a standard C++11 attribute sequence. */
24401 static bool
24402 cp_nth_tokens_can_be_attribute_p (cp_parser *parser, size_t n)
24404 return (cp_nth_tokens_can_be_gnu_attribute_p (parser, n)
24405 || cp_nth_tokens_can_be_std_attribute_p (parser, n));
24408 /* Parse either a standard C++-11 attribute-specifier-seq, or a series
24409 of GNU attributes, or return NULL. */
24411 static tree
24412 cp_parser_attributes_opt (cp_parser *parser)
24414 if (cp_next_tokens_can_be_gnu_attribute_p (parser))
24415 return cp_parser_gnu_attributes_opt (parser);
24416 return cp_parser_std_attribute_spec_seq (parser);
24419 #define CILK_SIMD_FN_CLAUSE_MASK \
24420 ((OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_VECTORLENGTH) \
24421 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_LINEAR) \
24422 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_UNIFORM) \
24423 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_MASK) \
24424 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_NOMASK))
24426 /* Parses the Cilk Plus SIMD-enabled function's attribute. Syntax:
24427 vector [(<clauses>)] */
24429 static void
24430 cp_parser_cilk_simd_fn_vector_attrs (cp_parser *parser, cp_token *v_token)
24432 bool first_p = parser->cilk_simd_fn_info == NULL;
24433 cp_token *token = v_token;
24434 if (first_p)
24436 parser->cilk_simd_fn_info = XNEW (cp_omp_declare_simd_data);
24437 parser->cilk_simd_fn_info->error_seen = false;
24438 parser->cilk_simd_fn_info->fndecl_seen = false;
24439 parser->cilk_simd_fn_info->tokens = vNULL;
24440 parser->cilk_simd_fn_info->clauses = NULL_TREE;
24442 int paren_scope = 0;
24443 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
24445 cp_lexer_consume_token (parser->lexer);
24446 v_token = cp_lexer_peek_token (parser->lexer);
24447 paren_scope++;
24449 while (paren_scope > 0)
24451 token = cp_lexer_peek_token (parser->lexer);
24452 if (token->type == CPP_OPEN_PAREN)
24453 paren_scope++;
24454 else if (token->type == CPP_CLOSE_PAREN)
24455 paren_scope--;
24456 /* Do not push the last ')' */
24457 if (!(token->type == CPP_CLOSE_PAREN && paren_scope == 0))
24458 cp_lexer_consume_token (parser->lexer);
24461 token->type = CPP_PRAGMA_EOL;
24462 parser->lexer->next_token = token;
24463 cp_lexer_consume_token (parser->lexer);
24465 struct cp_token_cache *cp
24466 = cp_token_cache_new (v_token, cp_lexer_peek_token (parser->lexer));
24467 parser->cilk_simd_fn_info->tokens.safe_push (cp);
24470 /* Parse an (optional) series of attributes.
24472 attributes:
24473 attributes attribute
24475 attribute:
24476 __attribute__ (( attribute-list [opt] ))
24478 The return value is as for cp_parser_gnu_attribute_list. */
24480 static tree
24481 cp_parser_gnu_attributes_opt (cp_parser* parser)
24483 tree attributes = NULL_TREE;
24485 while (true)
24487 cp_token *token;
24488 tree attribute_list;
24489 bool ok = true;
24491 /* Peek at the next token. */
24492 token = cp_lexer_peek_token (parser->lexer);
24493 /* If it's not `__attribute__', then we're done. */
24494 if (token->keyword != RID_ATTRIBUTE)
24495 break;
24497 /* Consume the `__attribute__' keyword. */
24498 cp_lexer_consume_token (parser->lexer);
24499 /* Look for the two `(' tokens. */
24500 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
24501 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
24503 /* Peek at the next token. */
24504 token = cp_lexer_peek_token (parser->lexer);
24505 if (token->type != CPP_CLOSE_PAREN)
24506 /* Parse the attribute-list. */
24507 attribute_list = cp_parser_gnu_attribute_list (parser);
24508 else
24509 /* If the next token is a `)', then there is no attribute
24510 list. */
24511 attribute_list = NULL;
24513 /* Look for the two `)' tokens. */
24514 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
24515 ok = false;
24516 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
24517 ok = false;
24518 if (!ok)
24519 cp_parser_skip_to_end_of_statement (parser);
24521 /* Add these new attributes to the list. */
24522 attributes = chainon (attributes, attribute_list);
24525 return attributes;
24528 /* Parse a GNU attribute-list.
24530 attribute-list:
24531 attribute
24532 attribute-list , attribute
24534 attribute:
24535 identifier
24536 identifier ( identifier )
24537 identifier ( identifier , expression-list )
24538 identifier ( expression-list )
24540 Returns a TREE_LIST, or NULL_TREE on error. Each node corresponds
24541 to an attribute. The TREE_PURPOSE of each node is the identifier
24542 indicating which attribute is in use. The TREE_VALUE represents
24543 the arguments, if any. */
24545 static tree
24546 cp_parser_gnu_attribute_list (cp_parser* parser)
24548 tree attribute_list = NULL_TREE;
24549 bool save_translate_strings_p = parser->translate_strings_p;
24551 parser->translate_strings_p = false;
24552 while (true)
24554 cp_token *token;
24555 tree identifier;
24556 tree attribute;
24558 /* Look for the identifier. We also allow keywords here; for
24559 example `__attribute__ ((const))' is legal. */
24560 token = cp_lexer_peek_token (parser->lexer);
24561 if (token->type == CPP_NAME
24562 || token->type == CPP_KEYWORD)
24564 tree arguments = NULL_TREE;
24566 /* Consume the token, but save it since we need it for the
24567 SIMD enabled function parsing. */
24568 cp_token *id_token = cp_lexer_consume_token (parser->lexer);
24570 /* Save away the identifier that indicates which attribute
24571 this is. */
24572 identifier = (token->type == CPP_KEYWORD)
24573 /* For keywords, use the canonical spelling, not the
24574 parsed identifier. */
24575 ? ridpointers[(int) token->keyword]
24576 : id_token->u.value;
24578 attribute = build_tree_list (identifier, NULL_TREE);
24580 /* Peek at the next token. */
24581 token = cp_lexer_peek_token (parser->lexer);
24582 /* If it's an `(', then parse the attribute arguments. */
24583 if (token->type == CPP_OPEN_PAREN)
24585 vec<tree, va_gc> *vec;
24586 int attr_flag = (attribute_takes_identifier_p (identifier)
24587 ? id_attr : normal_attr);
24588 if (is_cilkplus_vector_p (identifier))
24590 cp_parser_cilk_simd_fn_vector_attrs (parser, id_token);
24591 continue;
24593 else
24594 vec = cp_parser_parenthesized_expression_list
24595 (parser, attr_flag, /*cast_p=*/false,
24596 /*allow_expansion_p=*/false,
24597 /*non_constant_p=*/NULL);
24598 if (vec == NULL)
24599 arguments = error_mark_node;
24600 else
24602 arguments = build_tree_list_vec (vec);
24603 release_tree_vector (vec);
24605 /* Save the arguments away. */
24606 TREE_VALUE (attribute) = arguments;
24608 else if (is_cilkplus_vector_p (identifier))
24610 cp_parser_cilk_simd_fn_vector_attrs (parser, id_token);
24611 continue;
24614 if (arguments != error_mark_node)
24616 /* Add this attribute to the list. */
24617 TREE_CHAIN (attribute) = attribute_list;
24618 attribute_list = attribute;
24621 token = cp_lexer_peek_token (parser->lexer);
24623 /* Now, look for more attributes. If the next token isn't a
24624 `,', we're done. */
24625 if (token->type != CPP_COMMA)
24626 break;
24628 /* Consume the comma and keep going. */
24629 cp_lexer_consume_token (parser->lexer);
24631 parser->translate_strings_p = save_translate_strings_p;
24633 /* We built up the list in reverse order. */
24634 return nreverse (attribute_list);
24637 /* Parse a standard C++11 attribute.
24639 The returned representation is a TREE_LIST which TREE_PURPOSE is
24640 the scoped name of the attribute, and the TREE_VALUE is its
24641 arguments list.
24643 Note that the scoped name of the attribute is itself a TREE_LIST
24644 which TREE_PURPOSE is the namespace of the attribute, and
24645 TREE_VALUE its name. This is unlike a GNU attribute -- as parsed
24646 by cp_parser_gnu_attribute_list -- that doesn't have any namespace
24647 and which TREE_PURPOSE is directly the attribute name.
24649 Clients of the attribute code should use get_attribute_namespace
24650 and get_attribute_name to get the actual namespace and name of
24651 attributes, regardless of their being GNU or C++11 attributes.
24653 attribute:
24654 attribute-token attribute-argument-clause [opt]
24656 attribute-token:
24657 identifier
24658 attribute-scoped-token
24660 attribute-scoped-token:
24661 attribute-namespace :: identifier
24663 attribute-namespace:
24664 identifier
24666 attribute-argument-clause:
24667 ( balanced-token-seq )
24669 balanced-token-seq:
24670 balanced-token [opt]
24671 balanced-token-seq balanced-token
24673 balanced-token:
24674 ( balanced-token-seq )
24675 [ balanced-token-seq ]
24676 { balanced-token-seq }. */
24678 static tree
24679 cp_parser_std_attribute (cp_parser *parser, tree attr_ns)
24681 tree attribute, attr_id = NULL_TREE, arguments;
24682 cp_token *token;
24684 /* First, parse name of the attribute, a.k.a attribute-token. */
24686 token = cp_lexer_peek_token (parser->lexer);
24687 if (token->type == CPP_NAME)
24688 attr_id = token->u.value;
24689 else if (token->type == CPP_KEYWORD)
24690 attr_id = ridpointers[(int) token->keyword];
24691 else if (token->flags & NAMED_OP)
24692 attr_id = get_identifier (cpp_type2name (token->type, token->flags));
24694 if (attr_id == NULL_TREE)
24695 return NULL_TREE;
24697 cp_lexer_consume_token (parser->lexer);
24699 token = cp_lexer_peek_token (parser->lexer);
24700 if (token->type == CPP_SCOPE)
24702 /* We are seeing a scoped attribute token. */
24704 cp_lexer_consume_token (parser->lexer);
24705 if (attr_ns)
24706 error_at (token->location, "attribute using prefix used together "
24707 "with scoped attribute token");
24708 attr_ns = attr_id;
24710 token = cp_lexer_consume_token (parser->lexer);
24711 if (token->type == CPP_NAME)
24712 attr_id = token->u.value;
24713 else if (token->type == CPP_KEYWORD)
24714 attr_id = ridpointers[(int) token->keyword];
24715 else if (token->flags & NAMED_OP)
24716 attr_id = get_identifier (cpp_type2name (token->type, token->flags));
24717 else
24719 error_at (token->location,
24720 "expected an identifier for the attribute name");
24721 return error_mark_node;
24723 attribute = build_tree_list (build_tree_list (attr_ns, attr_id),
24724 NULL_TREE);
24725 token = cp_lexer_peek_token (parser->lexer);
24727 else if (attr_ns)
24728 attribute = build_tree_list (build_tree_list (attr_ns, attr_id),
24729 NULL_TREE);
24730 else
24732 attribute = build_tree_list (build_tree_list (NULL_TREE, attr_id),
24733 NULL_TREE);
24734 /* C++11 noreturn attribute is equivalent to GNU's. */
24735 if (is_attribute_p ("noreturn", attr_id))
24736 TREE_PURPOSE (TREE_PURPOSE (attribute)) = get_identifier ("gnu");
24737 /* C++14 deprecated attribute is equivalent to GNU's. */
24738 else if (is_attribute_p ("deprecated", attr_id))
24739 TREE_PURPOSE (TREE_PURPOSE (attribute)) = get_identifier ("gnu");
24740 /* C++17 fallthrough attribute is equivalent to GNU's. */
24741 else if (is_attribute_p ("fallthrough", attr_id))
24742 TREE_PURPOSE (TREE_PURPOSE (attribute)) = get_identifier ("gnu");
24743 /* Transactional Memory TS optimize_for_synchronized attribute is
24744 equivalent to GNU transaction_callable. */
24745 else if (is_attribute_p ("optimize_for_synchronized", attr_id))
24746 TREE_PURPOSE (attribute)
24747 = get_identifier ("transaction_callable");
24748 /* Transactional Memory attributes are GNU attributes. */
24749 else if (tm_attr_to_mask (attr_id))
24750 TREE_PURPOSE (attribute) = attr_id;
24753 /* Now parse the optional argument clause of the attribute. */
24755 if (token->type != CPP_OPEN_PAREN)
24756 return attribute;
24759 vec<tree, va_gc> *vec;
24760 int attr_flag = normal_attr;
24762 if (attr_ns == get_identifier ("gnu")
24763 && attribute_takes_identifier_p (attr_id))
24764 /* A GNU attribute that takes an identifier in parameter. */
24765 attr_flag = id_attr;
24767 vec = cp_parser_parenthesized_expression_list
24768 (parser, attr_flag, /*cast_p=*/false,
24769 /*allow_expansion_p=*/true,
24770 /*non_constant_p=*/NULL);
24771 if (vec == NULL)
24772 arguments = error_mark_node;
24773 else
24775 arguments = build_tree_list_vec (vec);
24776 release_tree_vector (vec);
24779 if (arguments == error_mark_node)
24780 attribute = error_mark_node;
24781 else
24782 TREE_VALUE (attribute) = arguments;
24785 return attribute;
24788 /* Check that the attribute ATTRIBUTE appears at most once in the
24789 attribute-list ATTRIBUTES. This is enforced for noreturn (7.6.3)
24790 and deprecated (7.6.5). Note that carries_dependency (7.6.4)
24791 isn't implemented yet in GCC. */
24793 static void
24794 cp_parser_check_std_attribute (tree attributes, tree attribute)
24796 if (attributes)
24798 tree name = get_attribute_name (attribute);
24799 if (is_attribute_p ("noreturn", name)
24800 && lookup_attribute ("noreturn", attributes))
24801 error ("attribute %<noreturn%> can appear at most once "
24802 "in an attribute-list");
24803 else if (is_attribute_p ("deprecated", name)
24804 && lookup_attribute ("deprecated", attributes))
24805 error ("attribute %<deprecated%> can appear at most once "
24806 "in an attribute-list");
24810 /* Parse a list of standard C++-11 attributes.
24812 attribute-list:
24813 attribute [opt]
24814 attribute-list , attribute[opt]
24815 attribute ...
24816 attribute-list , attribute ...
24819 static tree
24820 cp_parser_std_attribute_list (cp_parser *parser, tree attr_ns)
24822 tree attributes = NULL_TREE, attribute = NULL_TREE;
24823 cp_token *token = NULL;
24825 while (true)
24827 attribute = cp_parser_std_attribute (parser, attr_ns);
24828 if (attribute == error_mark_node)
24829 break;
24830 if (attribute != NULL_TREE)
24832 cp_parser_check_std_attribute (attributes, attribute);
24833 TREE_CHAIN (attribute) = attributes;
24834 attributes = attribute;
24836 token = cp_lexer_peek_token (parser->lexer);
24837 if (token->type == CPP_ELLIPSIS)
24839 cp_lexer_consume_token (parser->lexer);
24840 if (attribute == NULL_TREE)
24841 error_at (token->location,
24842 "expected attribute before %<...%>");
24843 else
24844 TREE_VALUE (attribute)
24845 = make_pack_expansion (TREE_VALUE (attribute));
24846 token = cp_lexer_peek_token (parser->lexer);
24848 if (token->type != CPP_COMMA)
24849 break;
24850 cp_lexer_consume_token (parser->lexer);
24852 attributes = nreverse (attributes);
24853 return attributes;
24856 /* Parse a standard C++-11 attribute specifier.
24858 attribute-specifier:
24859 [ [ attribute-using-prefix [opt] attribute-list ] ]
24860 alignment-specifier
24862 attribute-using-prefix:
24863 using attribute-namespace :
24865 alignment-specifier:
24866 alignas ( type-id ... [opt] )
24867 alignas ( alignment-expression ... [opt] ). */
24869 static tree
24870 cp_parser_std_attribute_spec (cp_parser *parser)
24872 tree attributes = NULL_TREE;
24873 cp_token *token = cp_lexer_peek_token (parser->lexer);
24875 if (token->type == CPP_OPEN_SQUARE
24876 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_OPEN_SQUARE)
24878 tree attr_ns = NULL_TREE;
24880 cp_lexer_consume_token (parser->lexer);
24881 cp_lexer_consume_token (parser->lexer);
24883 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
24885 token = cp_lexer_peek_nth_token (parser->lexer, 2);
24886 if (token->type == CPP_NAME)
24887 attr_ns = token->u.value;
24888 else if (token->type == CPP_KEYWORD)
24889 attr_ns = ridpointers[(int) token->keyword];
24890 else if (token->flags & NAMED_OP)
24891 attr_ns = get_identifier (cpp_type2name (token->type,
24892 token->flags));
24893 if (attr_ns
24894 && cp_lexer_nth_token_is (parser->lexer, 3, CPP_COLON))
24896 if (cxx_dialect < cxx1z
24897 && !in_system_header_at (input_location))
24898 pedwarn (input_location, 0,
24899 "attribute using prefix only available "
24900 "with -std=c++1z or -std=gnu++1z");
24902 cp_lexer_consume_token (parser->lexer);
24903 cp_lexer_consume_token (parser->lexer);
24904 cp_lexer_consume_token (parser->lexer);
24906 else
24907 attr_ns = NULL_TREE;
24910 attributes = cp_parser_std_attribute_list (parser, attr_ns);
24912 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE)
24913 || !cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
24914 cp_parser_skip_to_end_of_statement (parser);
24915 else
24916 /* Warn about parsing c++11 attribute in non-c++1 mode, only
24917 when we are sure that we have actually parsed them. */
24918 maybe_warn_cpp0x (CPP0X_ATTRIBUTES);
24920 else
24922 tree alignas_expr;
24924 /* Look for an alignment-specifier. */
24926 token = cp_lexer_peek_token (parser->lexer);
24928 if (token->type != CPP_KEYWORD
24929 || token->keyword != RID_ALIGNAS)
24930 return NULL_TREE;
24932 cp_lexer_consume_token (parser->lexer);
24933 maybe_warn_cpp0x (CPP0X_ATTRIBUTES);
24935 if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN) == NULL)
24937 cp_parser_error (parser, "expected %<(%>");
24938 return error_mark_node;
24941 cp_parser_parse_tentatively (parser);
24942 alignas_expr = cp_parser_type_id (parser);
24944 if (!cp_parser_parse_definitely (parser))
24946 alignas_expr = cp_parser_assignment_expression (parser);
24947 if (alignas_expr == error_mark_node)
24948 cp_parser_skip_to_end_of_statement (parser);
24949 if (alignas_expr == NULL_TREE
24950 || alignas_expr == error_mark_node)
24951 return alignas_expr;
24954 alignas_expr = cxx_alignas_expr (alignas_expr);
24955 alignas_expr = build_tree_list (NULL_TREE, alignas_expr);
24957 /* Handle alignas (pack...). */
24958 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
24960 cp_lexer_consume_token (parser->lexer);
24961 alignas_expr = make_pack_expansion (alignas_expr);
24964 /* Something went wrong, so don't build the attribute. */
24965 if (alignas_expr == error_mark_node)
24966 return error_mark_node;
24968 if (cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN) == NULL)
24970 cp_parser_error (parser, "expected %<)%>");
24971 return error_mark_node;
24974 /* Build the C++-11 representation of an 'aligned'
24975 attribute. */
24976 attributes =
24977 build_tree_list (build_tree_list (get_identifier ("gnu"),
24978 get_identifier ("aligned")),
24979 alignas_expr);
24982 return attributes;
24985 /* Parse a standard C++-11 attribute-specifier-seq.
24987 attribute-specifier-seq:
24988 attribute-specifier-seq [opt] attribute-specifier
24991 static tree
24992 cp_parser_std_attribute_spec_seq (cp_parser *parser)
24994 tree attr_specs = NULL_TREE;
24995 tree attr_last = NULL_TREE;
24997 while (true)
24999 tree attr_spec = cp_parser_std_attribute_spec (parser);
25000 if (attr_spec == NULL_TREE)
25001 break;
25002 if (attr_spec == error_mark_node)
25003 return error_mark_node;
25005 if (attr_last)
25006 TREE_CHAIN (attr_last) = attr_spec;
25007 else
25008 attr_specs = attr_last = attr_spec;
25009 attr_last = tree_last (attr_last);
25012 return attr_specs;
25015 /* Parse an optional `__extension__' keyword. Returns TRUE if it is
25016 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
25017 current value of the PEDANTIC flag, regardless of whether or not
25018 the `__extension__' keyword is present. The caller is responsible
25019 for restoring the value of the PEDANTIC flag. */
25021 static bool
25022 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
25024 /* Save the old value of the PEDANTIC flag. */
25025 *saved_pedantic = pedantic;
25027 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
25029 /* Consume the `__extension__' token. */
25030 cp_lexer_consume_token (parser->lexer);
25031 /* We're not being pedantic while the `__extension__' keyword is
25032 in effect. */
25033 pedantic = 0;
25035 return true;
25038 return false;
25041 /* Parse a label declaration.
25043 label-declaration:
25044 __label__ label-declarator-seq ;
25046 label-declarator-seq:
25047 identifier , label-declarator-seq
25048 identifier */
25050 static void
25051 cp_parser_label_declaration (cp_parser* parser)
25053 /* Look for the `__label__' keyword. */
25054 cp_parser_require_keyword (parser, RID_LABEL, RT_LABEL);
25056 while (true)
25058 tree identifier;
25060 /* Look for an identifier. */
25061 identifier = cp_parser_identifier (parser);
25062 /* If we failed, stop. */
25063 if (identifier == error_mark_node)
25064 break;
25065 /* Declare it as a label. */
25066 finish_label_decl (identifier);
25067 /* If the next token is a `;', stop. */
25068 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
25069 break;
25070 /* Look for the `,' separating the label declarations. */
25071 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
25074 /* Look for the final `;'. */
25075 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
25078 // -------------------------------------------------------------------------- //
25079 // Requires Clause
25081 // Parse a requires clause.
25083 // requires-clause:
25084 // 'requires' logical-or-expression
25086 // The required logical-or-expression must be a constant expression. Note
25087 // that we don't check that the expression is constepxr here. We defer until
25088 // we analyze constraints and then, we only check atomic constraints.
25089 static tree
25090 cp_parser_requires_clause (cp_parser *parser)
25092 // Parse the requires clause so that it is not automatically folded.
25093 ++processing_template_decl;
25094 tree expr = cp_parser_binary_expression (parser, false, false,
25095 PREC_NOT_OPERATOR, NULL);
25096 if (check_for_bare_parameter_packs (expr))
25097 expr = error_mark_node;
25098 --processing_template_decl;
25099 return expr;
25102 // Optionally parse a requires clause:
25103 static tree
25104 cp_parser_requires_clause_opt (cp_parser *parser)
25106 cp_token *tok = cp_lexer_peek_token (parser->lexer);
25107 if (tok->keyword != RID_REQUIRES)
25109 if (!flag_concepts && tok->type == CPP_NAME
25110 && tok->u.value == ridpointers[RID_REQUIRES])
25112 error_at (cp_lexer_peek_token (parser->lexer)->location,
25113 "%<requires%> only available with -fconcepts");
25114 /* Parse and discard the requires-clause. */
25115 cp_lexer_consume_token (parser->lexer);
25116 cp_parser_requires_clause (parser);
25118 return NULL_TREE;
25120 cp_lexer_consume_token (parser->lexer);
25121 return cp_parser_requires_clause (parser);
25125 /*---------------------------------------------------------------------------
25126 Requires expressions
25127 ---------------------------------------------------------------------------*/
25129 /* Parse a requires expression
25131 requirement-expression:
25132 'requires' requirement-parameter-list [opt] requirement-body */
25133 static tree
25134 cp_parser_requires_expression (cp_parser *parser)
25136 gcc_assert (cp_lexer_next_token_is_keyword (parser->lexer, RID_REQUIRES));
25137 location_t loc = cp_lexer_consume_token (parser->lexer)->location;
25139 /* A requires-expression shall appear only within a concept
25140 definition or a requires-clause.
25142 TODO: Implement this diagnostic correctly. */
25143 if (!processing_template_decl)
25145 error_at (loc, "a requires expression cannot appear outside a template");
25146 cp_parser_skip_to_end_of_statement (parser);
25147 return error_mark_node;
25150 tree parms, reqs;
25152 /* Local parameters are delared as variables within the scope
25153 of the expression. They are not visible past the end of
25154 the expression. Expressions within the requires-expression
25155 are unevaluated. */
25156 struct scope_sentinel
25158 scope_sentinel ()
25160 ++cp_unevaluated_operand;
25161 begin_scope (sk_block, NULL_TREE);
25164 ~scope_sentinel ()
25166 pop_bindings_and_leave_scope ();
25167 --cp_unevaluated_operand;
25169 } s;
25171 /* Parse the optional parameter list. */
25172 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
25174 parms = cp_parser_requirement_parameter_list (parser);
25175 if (parms == error_mark_node)
25176 return error_mark_node;
25178 else
25179 parms = NULL_TREE;
25181 /* Parse the requirement body. */
25182 reqs = cp_parser_requirement_body (parser);
25183 if (reqs == error_mark_node)
25184 return error_mark_node;
25187 /* This needs to happen after pop_bindings_and_leave_scope, as it reverses
25188 the parm chain. */
25189 grokparms (parms, &parms);
25190 return finish_requires_expr (parms, reqs);
25193 /* Parse a parameterized requirement.
25195 requirement-parameter-list:
25196 '(' parameter-declaration-clause ')' */
25197 static tree
25198 cp_parser_requirement_parameter_list (cp_parser *parser)
25200 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
25201 return error_mark_node;
25203 tree parms = cp_parser_parameter_declaration_clause (parser);
25205 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
25206 return error_mark_node;
25208 return parms;
25211 /* Parse the body of a requirement.
25213 requirement-body:
25214 '{' requirement-list '}' */
25215 static tree
25216 cp_parser_requirement_body (cp_parser *parser)
25218 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
25219 return error_mark_node;
25221 tree reqs = cp_parser_requirement_list (parser);
25223 if (!cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE))
25224 return error_mark_node;
25226 return reqs;
25229 /* Parse a list of requirements.
25231 requirement-list:
25232 requirement
25233 requirement-list ';' requirement[opt] */
25234 static tree
25235 cp_parser_requirement_list (cp_parser *parser)
25237 tree result = NULL_TREE;
25238 while (true)
25240 tree req = cp_parser_requirement (parser);
25241 if (req == error_mark_node)
25242 return error_mark_node;
25244 result = tree_cons (NULL_TREE, req, result);
25246 /* If we see a semi-colon, consume it. */
25247 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
25248 cp_lexer_consume_token (parser->lexer);
25250 /* Stop processing at the end of the list. */
25251 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
25252 break;
25255 /* Reverse the order of requirements so they are analyzed in
25256 declaration order. */
25257 return nreverse (result);
25260 /* Parse a syntactic requirement or type requirement.
25262 requirement:
25263 simple-requirement
25264 compound-requirement
25265 type-requirement
25266 nested-requirement */
25267 static tree
25268 cp_parser_requirement (cp_parser *parser)
25270 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
25271 return cp_parser_compound_requirement (parser);
25272 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
25273 return cp_parser_type_requirement (parser);
25274 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_REQUIRES))
25275 return cp_parser_nested_requirement (parser);
25276 else
25277 return cp_parser_simple_requirement (parser);
25280 /* Parse a simple requirement.
25282 simple-requirement:
25283 expression ';' */
25284 static tree
25285 cp_parser_simple_requirement (cp_parser *parser)
25287 tree expr = cp_parser_expression (parser, NULL, false, false);
25288 if (!expr || expr == error_mark_node)
25289 return error_mark_node;
25291 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
25292 return error_mark_node;
25294 return finish_simple_requirement (expr);
25297 /* Parse a type requirement
25299 type-requirement
25300 nested-name-specifier [opt] required-type-name ';'
25302 required-type-name:
25303 type-name
25304 'template' [opt] simple-template-id */
25305 static tree
25306 cp_parser_type_requirement (cp_parser *parser)
25308 cp_lexer_consume_token (parser->lexer);
25310 // Save the scope before parsing name specifiers.
25311 tree saved_scope = parser->scope;
25312 tree saved_object_scope = parser->object_scope;
25313 tree saved_qualifying_scope = parser->qualifying_scope;
25314 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
25315 cp_parser_nested_name_specifier_opt (parser,
25316 /*typename_keyword_p=*/true,
25317 /*check_dependency_p=*/false,
25318 /*type_p=*/true,
25319 /*is_declaration=*/false);
25321 tree type;
25322 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
25324 cp_lexer_consume_token (parser->lexer);
25325 type = cp_parser_template_id (parser,
25326 /*template_keyword_p=*/true,
25327 /*check_dependency=*/false,
25328 /*tag_type=*/none_type,
25329 /*is_declaration=*/false);
25330 type = make_typename_type (parser->scope, type, typename_type,
25331 /*complain=*/tf_error);
25333 else
25334 type = cp_parser_type_name (parser, /*typename_keyword_p=*/true);
25336 if (TREE_CODE (type) == TYPE_DECL)
25337 type = TREE_TYPE (type);
25339 parser->scope = saved_scope;
25340 parser->object_scope = saved_object_scope;
25341 parser->qualifying_scope = saved_qualifying_scope;
25343 if (type == error_mark_node)
25344 cp_parser_skip_to_end_of_statement (parser);
25346 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
25347 return error_mark_node;
25348 if (type == error_mark_node)
25349 return error_mark_node;
25351 return finish_type_requirement (type);
25354 /* Parse a compound requirement
25356 compound-requirement:
25357 '{' expression '}' 'noexcept' [opt] trailing-return-type [opt] ';' */
25358 static tree
25359 cp_parser_compound_requirement (cp_parser *parser)
25361 /* Parse an expression enclosed in '{ }'s. */
25362 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
25363 return error_mark_node;
25365 tree expr = cp_parser_expression (parser, NULL, false, false);
25366 if (!expr || expr == error_mark_node)
25367 return error_mark_node;
25369 if (!cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE))
25370 return error_mark_node;
25372 /* Parse the optional noexcept. */
25373 bool noexcept_p = false;
25374 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_NOEXCEPT))
25376 cp_lexer_consume_token (parser->lexer);
25377 noexcept_p = true;
25380 /* Parse the optional trailing return type. */
25381 tree type = NULL_TREE;
25382 if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
25384 cp_lexer_consume_token (parser->lexer);
25385 bool saved_result_type_constraint_p = parser->in_result_type_constraint_p;
25386 parser->in_result_type_constraint_p = true;
25387 type = cp_parser_trailing_type_id (parser);
25388 parser->in_result_type_constraint_p = saved_result_type_constraint_p;
25389 if (type == error_mark_node)
25390 return error_mark_node;
25393 return finish_compound_requirement (expr, type, noexcept_p);
25396 /* Parse a nested requirement. This is the same as a requires clause.
25398 nested-requirement:
25399 requires-clause */
25400 static tree
25401 cp_parser_nested_requirement (cp_parser *parser)
25403 cp_lexer_consume_token (parser->lexer);
25404 tree req = cp_parser_requires_clause (parser);
25405 if (req == error_mark_node)
25406 return error_mark_node;
25407 return finish_nested_requirement (req);
25410 /* Support Functions */
25412 /* Return the appropriate prefer_type argument for lookup_name_real based on
25413 tag_type and template_mem_access. */
25415 static inline int
25416 prefer_type_arg (tag_types tag_type, bool template_mem_access = false)
25418 /* DR 141: When looking in the current enclosing context for a template-name
25419 after -> or ., only consider class templates. */
25420 if (template_mem_access)
25421 return 2;
25422 switch (tag_type)
25424 case none_type: return 0; // No preference.
25425 case scope_type: return 1; // Type or namespace.
25426 default: return 2; // Type only.
25430 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
25431 NAME should have one of the representations used for an
25432 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
25433 is returned. If PARSER->SCOPE is a dependent type, then a
25434 SCOPE_REF is returned.
25436 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
25437 returned; the name was already resolved when the TEMPLATE_ID_EXPR
25438 was formed. Abstractly, such entities should not be passed to this
25439 function, because they do not need to be looked up, but it is
25440 simpler to check for this special case here, rather than at the
25441 call-sites.
25443 In cases not explicitly covered above, this function returns a
25444 DECL, OVERLOAD, or baselink representing the result of the lookup.
25445 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
25446 is returned.
25448 If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
25449 (e.g., "struct") that was used. In that case bindings that do not
25450 refer to types are ignored.
25452 If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
25453 ignored.
25455 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
25456 are ignored.
25458 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
25459 types.
25461 If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
25462 TREE_LIST of candidates if name-lookup results in an ambiguity, and
25463 NULL_TREE otherwise. */
25465 static cp_expr
25466 cp_parser_lookup_name (cp_parser *parser, tree name,
25467 enum tag_types tag_type,
25468 bool is_template,
25469 bool is_namespace,
25470 bool check_dependency,
25471 tree *ambiguous_decls,
25472 location_t name_location)
25474 tree decl;
25475 tree object_type = parser->context->object_type;
25477 /* Assume that the lookup will be unambiguous. */
25478 if (ambiguous_decls)
25479 *ambiguous_decls = NULL_TREE;
25481 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
25482 no longer valid. Note that if we are parsing tentatively, and
25483 the parse fails, OBJECT_TYPE will be automatically restored. */
25484 parser->context->object_type = NULL_TREE;
25486 if (name == error_mark_node)
25487 return error_mark_node;
25489 /* A template-id has already been resolved; there is no lookup to
25490 do. */
25491 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
25492 return name;
25493 if (BASELINK_P (name))
25495 gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
25496 == TEMPLATE_ID_EXPR);
25497 return name;
25500 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
25501 it should already have been checked to make sure that the name
25502 used matches the type being destroyed. */
25503 if (TREE_CODE (name) == BIT_NOT_EXPR)
25505 tree type;
25507 /* Figure out to which type this destructor applies. */
25508 if (parser->scope)
25509 type = parser->scope;
25510 else if (object_type)
25511 type = object_type;
25512 else
25513 type = current_class_type;
25514 /* If that's not a class type, there is no destructor. */
25515 if (!type || !CLASS_TYPE_P (type))
25516 return error_mark_node;
25517 if (CLASSTYPE_LAZY_DESTRUCTOR (type))
25518 lazily_declare_fn (sfk_destructor, type);
25519 if (!CLASSTYPE_DESTRUCTORS (type))
25520 return error_mark_node;
25521 /* If it was a class type, return the destructor. */
25522 return CLASSTYPE_DESTRUCTORS (type);
25525 /* By this point, the NAME should be an ordinary identifier. If
25526 the id-expression was a qualified name, the qualifying scope is
25527 stored in PARSER->SCOPE at this point. */
25528 gcc_assert (identifier_p (name));
25530 /* Perform the lookup. */
25531 if (parser->scope)
25533 bool dependent_p;
25535 if (parser->scope == error_mark_node)
25536 return error_mark_node;
25538 /* If the SCOPE is dependent, the lookup must be deferred until
25539 the template is instantiated -- unless we are explicitly
25540 looking up names in uninstantiated templates. Even then, we
25541 cannot look up the name if the scope is not a class type; it
25542 might, for example, be a template type parameter. */
25543 dependent_p = (TYPE_P (parser->scope)
25544 && dependent_scope_p (parser->scope));
25545 if ((check_dependency || !CLASS_TYPE_P (parser->scope))
25546 && dependent_p)
25547 /* Defer lookup. */
25548 decl = error_mark_node;
25549 else
25551 tree pushed_scope = NULL_TREE;
25553 /* If PARSER->SCOPE is a dependent type, then it must be a
25554 class type, and we must not be checking dependencies;
25555 otherwise, we would have processed this lookup above. So
25556 that PARSER->SCOPE is not considered a dependent base by
25557 lookup_member, we must enter the scope here. */
25558 if (dependent_p)
25559 pushed_scope = push_scope (parser->scope);
25561 /* If the PARSER->SCOPE is a template specialization, it
25562 may be instantiated during name lookup. In that case,
25563 errors may be issued. Even if we rollback the current
25564 tentative parse, those errors are valid. */
25565 decl = lookup_qualified_name (parser->scope, name,
25566 prefer_type_arg (tag_type),
25567 /*complain=*/true);
25569 /* 3.4.3.1: In a lookup in which the constructor is an acceptable
25570 lookup result and the nested-name-specifier nominates a class C:
25571 * if the name specified after the nested-name-specifier, when
25572 looked up in C, is the injected-class-name of C (Clause 9), or
25573 * if the name specified after the nested-name-specifier is the
25574 same as the identifier or the simple-template-id's template-
25575 name in the last component of the nested-name-specifier,
25576 the name is instead considered to name the constructor of
25577 class C. [ Note: for example, the constructor is not an
25578 acceptable lookup result in an elaborated-type-specifier so
25579 the constructor would not be used in place of the
25580 injected-class-name. --end note ] Such a constructor name
25581 shall be used only in the declarator-id of a declaration that
25582 names a constructor or in a using-declaration. */
25583 if (tag_type == none_type
25584 && DECL_SELF_REFERENCE_P (decl)
25585 && same_type_p (DECL_CONTEXT (decl), parser->scope))
25586 decl = lookup_qualified_name (parser->scope, ctor_identifier,
25587 prefer_type_arg (tag_type),
25588 /*complain=*/true);
25590 /* If we have a single function from a using decl, pull it out. */
25591 if (TREE_CODE (decl) == OVERLOAD
25592 && !really_overloaded_fn (decl))
25593 decl = OVL_FUNCTION (decl);
25595 if (pushed_scope)
25596 pop_scope (pushed_scope);
25599 /* If the scope is a dependent type and either we deferred lookup or
25600 we did lookup but didn't find the name, rememeber the name. */
25601 if (decl == error_mark_node && TYPE_P (parser->scope)
25602 && dependent_type_p (parser->scope))
25604 if (tag_type)
25606 tree type;
25608 /* The resolution to Core Issue 180 says that `struct
25609 A::B' should be considered a type-name, even if `A'
25610 is dependent. */
25611 type = make_typename_type (parser->scope, name, tag_type,
25612 /*complain=*/tf_error);
25613 if (type != error_mark_node)
25614 decl = TYPE_NAME (type);
25616 else if (is_template
25617 && (cp_parser_next_token_ends_template_argument_p (parser)
25618 || cp_lexer_next_token_is (parser->lexer,
25619 CPP_CLOSE_PAREN)))
25620 decl = make_unbound_class_template (parser->scope,
25621 name, NULL_TREE,
25622 /*complain=*/tf_error);
25623 else
25624 decl = build_qualified_name (/*type=*/NULL_TREE,
25625 parser->scope, name,
25626 is_template);
25628 parser->qualifying_scope = parser->scope;
25629 parser->object_scope = NULL_TREE;
25631 else if (object_type)
25633 /* Look up the name in the scope of the OBJECT_TYPE, unless the
25634 OBJECT_TYPE is not a class. */
25635 if (CLASS_TYPE_P (object_type))
25636 /* If the OBJECT_TYPE is a template specialization, it may
25637 be instantiated during name lookup. In that case, errors
25638 may be issued. Even if we rollback the current tentative
25639 parse, those errors are valid. */
25640 decl = lookup_member (object_type,
25641 name,
25642 /*protect=*/0,
25643 prefer_type_arg (tag_type),
25644 tf_warning_or_error);
25645 else
25646 decl = NULL_TREE;
25648 if (!decl)
25649 /* Look it up in the enclosing context. DR 141: When looking for a
25650 template-name after -> or ., only consider class templates. */
25651 decl = lookup_name_real (name, prefer_type_arg (tag_type, is_template),
25652 /*nonclass=*/0,
25653 /*block_p=*/true, is_namespace, 0);
25654 if (object_type == unknown_type_node)
25655 /* The object is type-dependent, so we can't look anything up; we used
25656 this to get the DR 141 behavior. */
25657 object_type = NULL_TREE;
25658 parser->object_scope = object_type;
25659 parser->qualifying_scope = NULL_TREE;
25661 else
25663 decl = lookup_name_real (name, prefer_type_arg (tag_type),
25664 /*nonclass=*/0,
25665 /*block_p=*/true, is_namespace, 0);
25666 parser->qualifying_scope = NULL_TREE;
25667 parser->object_scope = NULL_TREE;
25670 /* If the lookup failed, let our caller know. */
25671 if (!decl || decl == error_mark_node)
25672 return error_mark_node;
25674 /* Pull out the template from an injected-class-name (or multiple). */
25675 if (is_template)
25676 decl = maybe_get_template_decl_from_type_decl (decl);
25678 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
25679 if (TREE_CODE (decl) == TREE_LIST)
25681 if (ambiguous_decls)
25682 *ambiguous_decls = decl;
25683 /* The error message we have to print is too complicated for
25684 cp_parser_error, so we incorporate its actions directly. */
25685 if (!cp_parser_simulate_error (parser))
25687 error_at (name_location, "reference to %qD is ambiguous",
25688 name);
25689 print_candidates (decl);
25691 return error_mark_node;
25694 gcc_assert (DECL_P (decl)
25695 || TREE_CODE (decl) == OVERLOAD
25696 || TREE_CODE (decl) == SCOPE_REF
25697 || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
25698 || BASELINK_P (decl));
25700 /* If we have resolved the name of a member declaration, check to
25701 see if the declaration is accessible. When the name resolves to
25702 set of overloaded functions, accessibility is checked when
25703 overload resolution is done.
25705 During an explicit instantiation, access is not checked at all,
25706 as per [temp.explicit]. */
25707 if (DECL_P (decl))
25708 check_accessibility_of_qualified_id (decl, object_type, parser->scope);
25710 maybe_record_typedef_use (decl);
25712 return cp_expr (decl, name_location);
25715 /* Like cp_parser_lookup_name, but for use in the typical case where
25716 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
25717 IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE. */
25719 static tree
25720 cp_parser_lookup_name_simple (cp_parser* parser, tree name, location_t location)
25722 return cp_parser_lookup_name (parser, name,
25723 none_type,
25724 /*is_template=*/false,
25725 /*is_namespace=*/false,
25726 /*check_dependency=*/true,
25727 /*ambiguous_decls=*/NULL,
25728 location);
25731 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
25732 the current context, return the TYPE_DECL. If TAG_NAME_P is
25733 true, the DECL indicates the class being defined in a class-head,
25734 or declared in an elaborated-type-specifier.
25736 Otherwise, return DECL. */
25738 static tree
25739 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
25741 /* If the TEMPLATE_DECL is being declared as part of a class-head,
25742 the translation from TEMPLATE_DECL to TYPE_DECL occurs:
25744 struct A {
25745 template <typename T> struct B;
25748 template <typename T> struct A::B {};
25750 Similarly, in an elaborated-type-specifier:
25752 namespace N { struct X{}; }
25754 struct A {
25755 template <typename T> friend struct N::X;
25758 However, if the DECL refers to a class type, and we are in
25759 the scope of the class, then the name lookup automatically
25760 finds the TYPE_DECL created by build_self_reference rather
25761 than a TEMPLATE_DECL. For example, in:
25763 template <class T> struct S {
25764 S s;
25767 there is no need to handle such case. */
25769 if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
25770 return DECL_TEMPLATE_RESULT (decl);
25772 return decl;
25775 /* If too many, or too few, template-parameter lists apply to the
25776 declarator, issue an error message. Returns TRUE if all went well,
25777 and FALSE otherwise. */
25779 static bool
25780 cp_parser_check_declarator_template_parameters (cp_parser* parser,
25781 cp_declarator *declarator,
25782 location_t declarator_location)
25784 switch (declarator->kind)
25786 case cdk_id:
25788 unsigned num_templates = 0;
25789 tree scope = declarator->u.id.qualifying_scope;
25791 if (scope)
25792 num_templates = num_template_headers_for_class (scope);
25793 else if (TREE_CODE (declarator->u.id.unqualified_name)
25794 == TEMPLATE_ID_EXPR)
25795 /* If the DECLARATOR has the form `X<y>' then it uses one
25796 additional level of template parameters. */
25797 ++num_templates;
25799 return cp_parser_check_template_parameters
25800 (parser, num_templates, declarator_location, declarator);
25803 case cdk_function:
25804 case cdk_array:
25805 case cdk_pointer:
25806 case cdk_reference:
25807 case cdk_ptrmem:
25808 return (cp_parser_check_declarator_template_parameters
25809 (parser, declarator->declarator, declarator_location));
25811 case cdk_decomp:
25812 case cdk_error:
25813 return true;
25815 default:
25816 gcc_unreachable ();
25818 return false;
25821 /* NUM_TEMPLATES were used in the current declaration. If that is
25822 invalid, return FALSE and issue an error messages. Otherwise,
25823 return TRUE. If DECLARATOR is non-NULL, then we are checking a
25824 declarator and we can print more accurate diagnostics. */
25826 static bool
25827 cp_parser_check_template_parameters (cp_parser* parser,
25828 unsigned num_templates,
25829 location_t location,
25830 cp_declarator *declarator)
25832 /* If there are the same number of template classes and parameter
25833 lists, that's OK. */
25834 if (parser->num_template_parameter_lists == num_templates)
25835 return true;
25836 /* If there are more, but only one more, then we are referring to a
25837 member template. That's OK too. */
25838 if (parser->num_template_parameter_lists == num_templates + 1)
25839 return true;
25840 /* If there are more template classes than parameter lists, we have
25841 something like:
25843 template <class T> void S<T>::R<T>::f (); */
25844 if (parser->num_template_parameter_lists < num_templates)
25846 if (declarator && !current_function_decl)
25847 error_at (location, "specializing member %<%T::%E%> "
25848 "requires %<template<>%> syntax",
25849 declarator->u.id.qualifying_scope,
25850 declarator->u.id.unqualified_name);
25851 else if (declarator)
25852 error_at (location, "invalid declaration of %<%T::%E%>",
25853 declarator->u.id.qualifying_scope,
25854 declarator->u.id.unqualified_name);
25855 else
25856 error_at (location, "too few template-parameter-lists");
25857 return false;
25859 /* Otherwise, there are too many template parameter lists. We have
25860 something like:
25862 template <class T> template <class U> void S::f(); */
25863 error_at (location, "too many template-parameter-lists");
25864 return false;
25867 /* Parse an optional `::' token indicating that the following name is
25868 from the global namespace. If so, PARSER->SCOPE is set to the
25869 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
25870 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
25871 Returns the new value of PARSER->SCOPE, if the `::' token is
25872 present, and NULL_TREE otherwise. */
25874 static tree
25875 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
25877 cp_token *token;
25879 /* Peek at the next token. */
25880 token = cp_lexer_peek_token (parser->lexer);
25881 /* If we're looking at a `::' token then we're starting from the
25882 global namespace, not our current location. */
25883 if (token->type == CPP_SCOPE)
25885 /* Consume the `::' token. */
25886 cp_lexer_consume_token (parser->lexer);
25887 /* Set the SCOPE so that we know where to start the lookup. */
25888 parser->scope = global_namespace;
25889 parser->qualifying_scope = global_namespace;
25890 parser->object_scope = NULL_TREE;
25892 return parser->scope;
25894 else if (!current_scope_valid_p)
25896 parser->scope = NULL_TREE;
25897 parser->qualifying_scope = NULL_TREE;
25898 parser->object_scope = NULL_TREE;
25901 return NULL_TREE;
25904 /* Returns TRUE if the upcoming token sequence is the start of a
25905 constructor declarator or C++17 deduction guide. If FRIEND_P is true, the
25906 declarator is preceded by the `friend' specifier. */
25908 static bool
25909 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
25911 bool constructor_p;
25912 bool outside_class_specifier_p;
25913 tree nested_name_specifier;
25914 cp_token *next_token;
25916 /* The common case is that this is not a constructor declarator, so
25917 try to avoid doing lots of work if at all possible. It's not
25918 valid declare a constructor at function scope. */
25919 if (parser->in_function_body)
25920 return false;
25921 /* And only certain tokens can begin a constructor declarator. */
25922 next_token = cp_lexer_peek_token (parser->lexer);
25923 if (next_token->type != CPP_NAME
25924 && next_token->type != CPP_SCOPE
25925 && next_token->type != CPP_NESTED_NAME_SPECIFIER
25926 && next_token->type != CPP_TEMPLATE_ID)
25927 return false;
25929 /* Parse tentatively; we are going to roll back all of the tokens
25930 consumed here. */
25931 cp_parser_parse_tentatively (parser);
25932 /* Assume that we are looking at a constructor declarator. */
25933 constructor_p = true;
25935 /* Look for the optional `::' operator. */
25936 cp_parser_global_scope_opt (parser,
25937 /*current_scope_valid_p=*/false);
25938 /* Look for the nested-name-specifier. */
25939 nested_name_specifier
25940 = (cp_parser_nested_name_specifier_opt (parser,
25941 /*typename_keyword_p=*/false,
25942 /*check_dependency_p=*/false,
25943 /*type_p=*/false,
25944 /*is_declaration=*/false));
25946 outside_class_specifier_p = (!at_class_scope_p ()
25947 || !TYPE_BEING_DEFINED (current_class_type)
25948 || friend_p);
25950 /* Outside of a class-specifier, there must be a
25951 nested-name-specifier. Except in C++17 mode, where we
25952 might be declaring a guiding declaration. */
25953 if (!nested_name_specifier && outside_class_specifier_p
25954 && cxx_dialect < cxx1z)
25955 constructor_p = false;
25956 else if (nested_name_specifier == error_mark_node)
25957 constructor_p = false;
25959 /* If we have a class scope, this is easy; DR 147 says that S::S always
25960 names the constructor, and no other qualified name could. */
25961 if (constructor_p && nested_name_specifier
25962 && CLASS_TYPE_P (nested_name_specifier))
25964 tree id = cp_parser_unqualified_id (parser,
25965 /*template_keyword_p=*/false,
25966 /*check_dependency_p=*/false,
25967 /*declarator_p=*/true,
25968 /*optional_p=*/false);
25969 if (is_overloaded_fn (id))
25970 id = DECL_NAME (get_first_fn (id));
25971 if (!constructor_name_p (id, nested_name_specifier))
25972 constructor_p = false;
25974 /* If we still think that this might be a constructor-declarator,
25975 look for a class-name. */
25976 else if (constructor_p)
25978 /* If we have:
25980 template <typename T> struct S {
25981 S();
25984 we must recognize that the nested `S' names a class. */
25985 if (cxx_dialect >= cxx1z)
25986 cp_parser_parse_tentatively (parser);
25988 tree type_decl;
25989 type_decl = cp_parser_class_name (parser,
25990 /*typename_keyword_p=*/false,
25991 /*template_keyword_p=*/false,
25992 none_type,
25993 /*check_dependency_p=*/false,
25994 /*class_head_p=*/false,
25995 /*is_declaration=*/false);
25997 if (cxx_dialect >= cxx1z
25998 && !cp_parser_parse_definitely (parser))
26000 type_decl = NULL_TREE;
26001 tree tmpl = cp_parser_template_name (parser,
26002 /*template_keyword*/false,
26003 /*check_dependency_p*/false,
26004 /*is_declaration*/false,
26005 none_type,
26006 /*is_identifier*/NULL);
26007 if (DECL_CLASS_TEMPLATE_P (tmpl)
26008 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl))
26009 /* It's a deduction guide, return true. */;
26010 else
26011 cp_parser_simulate_error (parser);
26014 /* If there was no class-name, then this is not a constructor.
26015 Otherwise, if we are in a class-specifier and we aren't
26016 handling a friend declaration, check that its type matches
26017 current_class_type (c++/38313). Note: error_mark_node
26018 is left alone for error recovery purposes. */
26019 constructor_p = (!cp_parser_error_occurred (parser)
26020 && (outside_class_specifier_p
26021 || type_decl == NULL_TREE
26022 || type_decl == error_mark_node
26023 || same_type_p (current_class_type,
26024 TREE_TYPE (type_decl))));
26026 /* If we're still considering a constructor, we have to see a `(',
26027 to begin the parameter-declaration-clause, followed by either a
26028 `)', an `...', or a decl-specifier. We need to check for a
26029 type-specifier to avoid being fooled into thinking that:
26031 S (f) (int);
26033 is a constructor. (It is actually a function named `f' that
26034 takes one parameter (of type `int') and returns a value of type
26035 `S'. */
26036 if (constructor_p
26037 && !cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
26038 constructor_p = false;
26040 if (constructor_p
26041 && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
26042 && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
26043 /* A parameter declaration begins with a decl-specifier,
26044 which is either the "attribute" keyword, a storage class
26045 specifier, or (usually) a type-specifier. */
26046 && !cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer))
26048 tree type;
26049 tree pushed_scope = NULL_TREE;
26050 unsigned saved_num_template_parameter_lists;
26052 /* Names appearing in the type-specifier should be looked up
26053 in the scope of the class. */
26054 if (current_class_type)
26055 type = NULL_TREE;
26056 else if (type_decl)
26058 type = TREE_TYPE (type_decl);
26059 if (TREE_CODE (type) == TYPENAME_TYPE)
26061 type = resolve_typename_type (type,
26062 /*only_current_p=*/false);
26063 if (TREE_CODE (type) == TYPENAME_TYPE)
26065 cp_parser_abort_tentative_parse (parser);
26066 return false;
26069 pushed_scope = push_scope (type);
26072 /* Inside the constructor parameter list, surrounding
26073 template-parameter-lists do not apply. */
26074 saved_num_template_parameter_lists
26075 = parser->num_template_parameter_lists;
26076 parser->num_template_parameter_lists = 0;
26078 /* Look for the type-specifier. */
26079 cp_parser_type_specifier (parser,
26080 CP_PARSER_FLAGS_NONE,
26081 /*decl_specs=*/NULL,
26082 /*is_declarator=*/true,
26083 /*declares_class_or_enum=*/NULL,
26084 /*is_cv_qualifier=*/NULL);
26086 parser->num_template_parameter_lists
26087 = saved_num_template_parameter_lists;
26089 /* Leave the scope of the class. */
26090 if (pushed_scope)
26091 pop_scope (pushed_scope);
26093 constructor_p = !cp_parser_error_occurred (parser);
26097 /* We did not really want to consume any tokens. */
26098 cp_parser_abort_tentative_parse (parser);
26100 return constructor_p;
26103 /* Parse the definition of the function given by the DECL_SPECIFIERS,
26104 ATTRIBUTES, and DECLARATOR. The access checks have been deferred;
26105 they must be performed once we are in the scope of the function.
26107 Returns the function defined. */
26109 static tree
26110 cp_parser_function_definition_from_specifiers_and_declarator
26111 (cp_parser* parser,
26112 cp_decl_specifier_seq *decl_specifiers,
26113 tree attributes,
26114 const cp_declarator *declarator)
26116 tree fn;
26117 bool success_p;
26119 /* Begin the function-definition. */
26120 success_p = start_function (decl_specifiers, declarator, attributes);
26122 /* The things we're about to see are not directly qualified by any
26123 template headers we've seen thus far. */
26124 reset_specialization ();
26126 /* If there were names looked up in the decl-specifier-seq that we
26127 did not check, check them now. We must wait until we are in the
26128 scope of the function to perform the checks, since the function
26129 might be a friend. */
26130 perform_deferred_access_checks (tf_warning_or_error);
26132 if (success_p)
26134 cp_finalize_omp_declare_simd (parser, current_function_decl);
26135 parser->omp_declare_simd = NULL;
26136 cp_finalize_oacc_routine (parser, current_function_decl, true);
26137 parser->oacc_routine = NULL;
26140 if (!success_p)
26142 /* Skip the entire function. */
26143 cp_parser_skip_to_end_of_block_or_statement (parser);
26144 fn = error_mark_node;
26146 else if (DECL_INITIAL (current_function_decl) != error_mark_node)
26148 /* Seen already, skip it. An error message has already been output. */
26149 cp_parser_skip_to_end_of_block_or_statement (parser);
26150 fn = current_function_decl;
26151 current_function_decl = NULL_TREE;
26152 /* If this is a function from a class, pop the nested class. */
26153 if (current_class_name)
26154 pop_nested_class ();
26156 else
26158 timevar_id_t tv;
26159 if (DECL_DECLARED_INLINE_P (current_function_decl))
26160 tv = TV_PARSE_INLINE;
26161 else
26162 tv = TV_PARSE_FUNC;
26163 timevar_push (tv);
26164 fn = cp_parser_function_definition_after_declarator (parser,
26165 /*inline_p=*/false);
26166 timevar_pop (tv);
26169 return fn;
26172 /* Parse the part of a function-definition that follows the
26173 declarator. INLINE_P is TRUE iff this function is an inline
26174 function defined within a class-specifier.
26176 Returns the function defined. */
26178 static tree
26179 cp_parser_function_definition_after_declarator (cp_parser* parser,
26180 bool inline_p)
26182 tree fn;
26183 bool ctor_initializer_p = false;
26184 bool saved_in_unbraced_linkage_specification_p;
26185 bool saved_in_function_body;
26186 unsigned saved_num_template_parameter_lists;
26187 cp_token *token;
26188 bool fully_implicit_function_template_p
26189 = parser->fully_implicit_function_template_p;
26190 parser->fully_implicit_function_template_p = false;
26191 tree implicit_template_parms
26192 = parser->implicit_template_parms;
26193 parser->implicit_template_parms = 0;
26194 cp_binding_level* implicit_template_scope
26195 = parser->implicit_template_scope;
26196 parser->implicit_template_scope = 0;
26198 saved_in_function_body = parser->in_function_body;
26199 parser->in_function_body = true;
26200 /* If the next token is `return', then the code may be trying to
26201 make use of the "named return value" extension that G++ used to
26202 support. */
26203 token = cp_lexer_peek_token (parser->lexer);
26204 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
26206 /* Consume the `return' keyword. */
26207 cp_lexer_consume_token (parser->lexer);
26208 /* Look for the identifier that indicates what value is to be
26209 returned. */
26210 cp_parser_identifier (parser);
26211 /* Issue an error message. */
26212 error_at (token->location,
26213 "named return values are no longer supported");
26214 /* Skip tokens until we reach the start of the function body. */
26215 while (true)
26217 cp_token *token = cp_lexer_peek_token (parser->lexer);
26218 if (token->type == CPP_OPEN_BRACE
26219 || token->type == CPP_EOF
26220 || token->type == CPP_PRAGMA_EOL)
26221 break;
26222 cp_lexer_consume_token (parser->lexer);
26225 /* The `extern' in `extern "C" void f () { ... }' does not apply to
26226 anything declared inside `f'. */
26227 saved_in_unbraced_linkage_specification_p
26228 = parser->in_unbraced_linkage_specification_p;
26229 parser->in_unbraced_linkage_specification_p = false;
26230 /* Inside the function, surrounding template-parameter-lists do not
26231 apply. */
26232 saved_num_template_parameter_lists
26233 = parser->num_template_parameter_lists;
26234 parser->num_template_parameter_lists = 0;
26236 start_lambda_scope (current_function_decl);
26238 /* If the next token is `try', `__transaction_atomic', or
26239 `__transaction_relaxed`, then we are looking at either function-try-block
26240 or function-transaction-block. Note that all of these include the
26241 function-body. */
26242 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRANSACTION_ATOMIC))
26243 ctor_initializer_p = cp_parser_function_transaction (parser,
26244 RID_TRANSACTION_ATOMIC);
26245 else if (cp_lexer_next_token_is_keyword (parser->lexer,
26246 RID_TRANSACTION_RELAXED))
26247 ctor_initializer_p = cp_parser_function_transaction (parser,
26248 RID_TRANSACTION_RELAXED);
26249 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
26250 ctor_initializer_p = cp_parser_function_try_block (parser);
26251 else
26252 ctor_initializer_p = cp_parser_ctor_initializer_opt_and_function_body
26253 (parser, /*in_function_try_block=*/false);
26255 finish_lambda_scope ();
26257 /* Finish the function. */
26258 fn = finish_function ((ctor_initializer_p ? 1 : 0) |
26259 (inline_p ? 2 : 0));
26260 /* Generate code for it, if necessary. */
26261 expand_or_defer_fn (fn);
26262 /* Restore the saved values. */
26263 parser->in_unbraced_linkage_specification_p
26264 = saved_in_unbraced_linkage_specification_p;
26265 parser->num_template_parameter_lists
26266 = saved_num_template_parameter_lists;
26267 parser->in_function_body = saved_in_function_body;
26269 parser->fully_implicit_function_template_p
26270 = fully_implicit_function_template_p;
26271 parser->implicit_template_parms
26272 = implicit_template_parms;
26273 parser->implicit_template_scope
26274 = implicit_template_scope;
26276 if (parser->fully_implicit_function_template_p)
26277 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
26279 return fn;
26282 /* Parse a template-declaration body (following argument list). */
26284 static void
26285 cp_parser_template_declaration_after_parameters (cp_parser* parser,
26286 tree parameter_list,
26287 bool member_p)
26289 tree decl = NULL_TREE;
26290 bool friend_p = false;
26292 /* We just processed one more parameter list. */
26293 ++parser->num_template_parameter_lists;
26295 /* Get the deferred access checks from the parameter list. These
26296 will be checked once we know what is being declared, as for a
26297 member template the checks must be performed in the scope of the
26298 class containing the member. */
26299 vec<deferred_access_check, va_gc> *checks = get_deferred_access_checks ();
26301 /* Tentatively parse for a new template parameter list, which can either be
26302 the template keyword or a template introduction. */
26303 if (cp_parser_template_declaration_after_export (parser, member_p))
26304 /* OK */;
26305 else if (cxx_dialect >= cxx11
26306 && cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
26307 decl = cp_parser_alias_declaration (parser);
26308 else
26310 /* There are no access checks when parsing a template, as we do not
26311 know if a specialization will be a friend. */
26312 push_deferring_access_checks (dk_no_check);
26313 cp_token *token = cp_lexer_peek_token (parser->lexer);
26314 decl = cp_parser_single_declaration (parser,
26315 checks,
26316 member_p,
26317 /*explicit_specialization_p=*/false,
26318 &friend_p);
26319 pop_deferring_access_checks ();
26321 /* If this is a member template declaration, let the front
26322 end know. */
26323 if (member_p && !friend_p && decl)
26325 if (TREE_CODE (decl) == TYPE_DECL)
26326 cp_parser_check_access_in_redeclaration (decl, token->location);
26328 decl = finish_member_template_decl (decl);
26330 else if (friend_p && decl
26331 && DECL_DECLARES_TYPE_P (decl))
26332 make_friend_class (current_class_type, TREE_TYPE (decl),
26333 /*complain=*/true);
26335 /* We are done with the current parameter list. */
26336 --parser->num_template_parameter_lists;
26338 pop_deferring_access_checks ();
26340 /* Finish up. */
26341 finish_template_decl (parameter_list);
26343 /* Check the template arguments for a literal operator template. */
26344 if (decl
26345 && DECL_DECLARES_FUNCTION_P (decl)
26346 && UDLIT_OPER_P (DECL_NAME (decl)))
26348 bool ok = true;
26349 if (parameter_list == NULL_TREE)
26350 ok = false;
26351 else
26353 int num_parms = TREE_VEC_LENGTH (parameter_list);
26354 if (num_parms == 1)
26356 tree parm_list = TREE_VEC_ELT (parameter_list, 0);
26357 tree parm = INNERMOST_TEMPLATE_PARMS (parm_list);
26358 if (TREE_TYPE (parm) != char_type_node
26359 || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
26360 ok = false;
26362 else if (num_parms == 2 && cxx_dialect >= cxx14)
26364 tree parm_type = TREE_VEC_ELT (parameter_list, 0);
26365 tree type = INNERMOST_TEMPLATE_PARMS (parm_type);
26366 tree parm_list = TREE_VEC_ELT (parameter_list, 1);
26367 tree parm = INNERMOST_TEMPLATE_PARMS (parm_list);
26368 if (parm == error_mark_node
26369 || TREE_TYPE (parm) != TREE_TYPE (type)
26370 || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
26371 ok = false;
26373 else
26374 ok = false;
26376 if (!ok)
26378 if (cxx_dialect >= cxx14)
26379 error ("literal operator template %qD has invalid parameter list."
26380 " Expected non-type template argument pack <char...>"
26381 " or <typename CharT, CharT...>",
26382 decl);
26383 else
26384 error ("literal operator template %qD has invalid parameter list."
26385 " Expected non-type template argument pack <char...>",
26386 decl);
26390 /* Register member declarations. */
26391 if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
26392 finish_member_declaration (decl);
26393 /* If DECL is a function template, we must return to parse it later.
26394 (Even though there is no definition, there might be default
26395 arguments that need handling.) */
26396 if (member_p && decl
26397 && DECL_DECLARES_FUNCTION_P (decl))
26398 vec_safe_push (unparsed_funs_with_definitions, decl);
26401 /* Parse a template introduction header for a template-declaration. Returns
26402 false if tentative parse fails. */
26404 static bool
26405 cp_parser_template_introduction (cp_parser* parser, bool member_p)
26407 cp_parser_parse_tentatively (parser);
26409 tree saved_scope = parser->scope;
26410 tree saved_object_scope = parser->object_scope;
26411 tree saved_qualifying_scope = parser->qualifying_scope;
26413 /* Look for the optional `::' operator. */
26414 cp_parser_global_scope_opt (parser,
26415 /*current_scope_valid_p=*/false);
26416 /* Look for the nested-name-specifier. */
26417 cp_parser_nested_name_specifier_opt (parser,
26418 /*typename_keyword_p=*/false,
26419 /*check_dependency_p=*/true,
26420 /*type_p=*/false,
26421 /*is_declaration=*/false);
26423 cp_token *token = cp_lexer_peek_token (parser->lexer);
26424 tree concept_name = cp_parser_identifier (parser);
26426 /* Look up the concept for which we will be matching
26427 template parameters. */
26428 tree tmpl_decl = cp_parser_lookup_name_simple (parser, concept_name,
26429 token->location);
26430 parser->scope = saved_scope;
26431 parser->object_scope = saved_object_scope;
26432 parser->qualifying_scope = saved_qualifying_scope;
26434 if (concept_name == error_mark_node)
26435 cp_parser_simulate_error (parser);
26437 /* Look for opening brace for introduction. */
26438 cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
26440 if (!cp_parser_parse_definitely (parser))
26441 return false;
26443 push_deferring_access_checks (dk_deferred);
26445 /* Build vector of placeholder parameters and grab
26446 matching identifiers. */
26447 tree introduction_list = cp_parser_introduction_list (parser);
26449 /* The introduction-list shall not be empty. */
26450 int nargs = TREE_VEC_LENGTH (introduction_list);
26451 if (nargs == 0)
26453 error ("empty introduction-list");
26454 return true;
26457 /* Look for closing brace for introduction. */
26458 if (!cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE))
26459 return true;
26461 if (tmpl_decl == error_mark_node)
26463 cp_parser_name_lookup_error (parser, concept_name, tmpl_decl, NLE_NULL,
26464 token->location);
26465 return true;
26468 /* Build and associate the constraint. */
26469 tree parms = finish_template_introduction (tmpl_decl, introduction_list);
26470 if (parms && parms != error_mark_node)
26472 cp_parser_template_declaration_after_parameters (parser, parms,
26473 member_p);
26474 return true;
26477 error_at (token->location, "no matching concept for template-introduction");
26478 return true;
26481 /* Parse a normal template-declaration following the template keyword. */
26483 static void
26484 cp_parser_explicit_template_declaration (cp_parser* parser, bool member_p)
26486 tree parameter_list;
26487 bool need_lang_pop;
26488 location_t location = input_location;
26490 /* Look for the `<' token. */
26491 if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
26492 return;
26493 if (at_class_scope_p () && current_function_decl)
26495 /* 14.5.2.2 [temp.mem]
26497 A local class shall not have member templates. */
26498 error_at (location,
26499 "invalid declaration of member template in local class");
26500 cp_parser_skip_to_end_of_block_or_statement (parser);
26501 return;
26503 /* [temp]
26505 A template ... shall not have C linkage. */
26506 if (current_lang_name == lang_name_c)
26508 error_at (location, "template with C linkage");
26509 /* Give it C++ linkage to avoid confusing other parts of the
26510 front end. */
26511 push_lang_context (lang_name_cplusplus);
26512 need_lang_pop = true;
26514 else
26515 need_lang_pop = false;
26517 /* We cannot perform access checks on the template parameter
26518 declarations until we know what is being declared, just as we
26519 cannot check the decl-specifier list. */
26520 push_deferring_access_checks (dk_deferred);
26522 /* If the next token is `>', then we have an invalid
26523 specialization. Rather than complain about an invalid template
26524 parameter, issue an error message here. */
26525 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
26527 cp_parser_error (parser, "invalid explicit specialization");
26528 begin_specialization ();
26529 parameter_list = NULL_TREE;
26531 else
26533 /* Parse the template parameters. */
26534 parameter_list = cp_parser_template_parameter_list (parser);
26537 /* Look for the `>'. */
26538 cp_parser_skip_to_end_of_template_parameter_list (parser);
26540 /* Manage template requirements */
26541 if (flag_concepts)
26543 tree reqs = get_shorthand_constraints (current_template_parms);
26544 if (tree r = cp_parser_requires_clause_opt (parser))
26545 reqs = conjoin_constraints (reqs, normalize_expression (r));
26546 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) = reqs;
26549 cp_parser_template_declaration_after_parameters (parser, parameter_list,
26550 member_p);
26552 /* For the erroneous case of a template with C linkage, we pushed an
26553 implicit C++ linkage scope; exit that scope now. */
26554 if (need_lang_pop)
26555 pop_lang_context ();
26558 /* Parse a template-declaration, assuming that the `export' (and
26559 `extern') keywords, if present, has already been scanned. MEMBER_P
26560 is as for cp_parser_template_declaration. */
26562 static bool
26563 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
26565 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
26567 cp_lexer_consume_token (parser->lexer);
26568 cp_parser_explicit_template_declaration (parser, member_p);
26569 return true;
26571 else if (flag_concepts)
26572 return cp_parser_template_introduction (parser, member_p);
26574 return false;
26577 /* Perform the deferred access checks from a template-parameter-list.
26578 CHECKS is a TREE_LIST of access checks, as returned by
26579 get_deferred_access_checks. */
26581 static void
26582 cp_parser_perform_template_parameter_access_checks (vec<deferred_access_check, va_gc> *checks)
26584 ++processing_template_parmlist;
26585 perform_access_checks (checks, tf_warning_or_error);
26586 --processing_template_parmlist;
26589 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
26590 `function-definition' sequence that follows a template header.
26591 If MEMBER_P is true, this declaration appears in a class scope.
26593 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
26594 *FRIEND_P is set to TRUE iff the declaration is a friend. */
26596 static tree
26597 cp_parser_single_declaration (cp_parser* parser,
26598 vec<deferred_access_check, va_gc> *checks,
26599 bool member_p,
26600 bool explicit_specialization_p,
26601 bool* friend_p)
26603 int declares_class_or_enum;
26604 tree decl = NULL_TREE;
26605 cp_decl_specifier_seq decl_specifiers;
26606 bool function_definition_p = false;
26607 cp_token *decl_spec_token_start;
26609 /* This function is only used when processing a template
26610 declaration. */
26611 gcc_assert (innermost_scope_kind () == sk_template_parms
26612 || innermost_scope_kind () == sk_template_spec);
26614 /* Defer access checks until we know what is being declared. */
26615 push_deferring_access_checks (dk_deferred);
26617 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
26618 alternative. */
26619 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
26620 cp_parser_decl_specifier_seq (parser,
26621 CP_PARSER_FLAGS_OPTIONAL,
26622 &decl_specifiers,
26623 &declares_class_or_enum);
26624 if (friend_p)
26625 *friend_p = cp_parser_friend_p (&decl_specifiers);
26627 /* There are no template typedefs. */
26628 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_typedef))
26630 error_at (decl_spec_token_start->location,
26631 "template declaration of %<typedef%>");
26632 decl = error_mark_node;
26635 /* Gather up the access checks that occurred the
26636 decl-specifier-seq. */
26637 stop_deferring_access_checks ();
26639 /* Check for the declaration of a template class. */
26640 if (declares_class_or_enum)
26642 if (cp_parser_declares_only_class_p (parser)
26643 || (declares_class_or_enum & 2))
26645 // If this is a declaration, but not a definition, associate
26646 // any constraints with the type declaration. Constraints
26647 // are associated with definitions in cp_parser_class_specifier.
26648 if (declares_class_or_enum == 1)
26649 associate_classtype_constraints (decl_specifiers.type);
26651 decl = shadow_tag (&decl_specifiers);
26653 /* In this case:
26655 struct C {
26656 friend template <typename T> struct A<T>::B;
26659 A<T>::B will be represented by a TYPENAME_TYPE, and
26660 therefore not recognized by shadow_tag. */
26661 if (friend_p && *friend_p
26662 && !decl
26663 && decl_specifiers.type
26664 && TYPE_P (decl_specifiers.type))
26665 decl = decl_specifiers.type;
26667 if (decl && decl != error_mark_node)
26668 decl = TYPE_NAME (decl);
26669 else
26670 decl = error_mark_node;
26672 /* Perform access checks for template parameters. */
26673 cp_parser_perform_template_parameter_access_checks (checks);
26675 /* Give a helpful diagnostic for
26676 template <class T> struct A { } a;
26677 if we aren't already recovering from an error. */
26678 if (!cp_parser_declares_only_class_p (parser)
26679 && !seen_error ())
26681 error_at (cp_lexer_peek_token (parser->lexer)->location,
26682 "a class template declaration must not declare "
26683 "anything else");
26684 cp_parser_skip_to_end_of_block_or_statement (parser);
26685 goto out;
26690 /* Complain about missing 'typename' or other invalid type names. */
26691 if (!decl_specifiers.any_type_specifiers_p
26692 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
26694 /* cp_parser_parse_and_diagnose_invalid_type_name calls
26695 cp_parser_skip_to_end_of_block_or_statement, so don't try to parse
26696 the rest of this declaration. */
26697 decl = error_mark_node;
26698 goto out;
26701 /* If it's not a template class, try for a template function. If
26702 the next token is a `;', then this declaration does not declare
26703 anything. But, if there were errors in the decl-specifiers, then
26704 the error might well have come from an attempted class-specifier.
26705 In that case, there's no need to warn about a missing declarator. */
26706 if (!decl
26707 && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
26708 || decl_specifiers.type != error_mark_node))
26710 decl = cp_parser_init_declarator (parser,
26711 &decl_specifiers,
26712 checks,
26713 /*function_definition_allowed_p=*/true,
26714 member_p,
26715 declares_class_or_enum,
26716 &function_definition_p,
26717 NULL, NULL, NULL);
26719 /* 7.1.1-1 [dcl.stc]
26721 A storage-class-specifier shall not be specified in an explicit
26722 specialization... */
26723 if (decl
26724 && explicit_specialization_p
26725 && decl_specifiers.storage_class != sc_none)
26727 error_at (decl_spec_token_start->location,
26728 "explicit template specialization cannot have a storage class");
26729 decl = error_mark_node;
26732 if (decl && VAR_P (decl))
26733 check_template_variable (decl);
26736 /* Look for a trailing `;' after the declaration. */
26737 if (!function_definition_p
26738 && (decl == error_mark_node
26739 || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON)))
26740 cp_parser_skip_to_end_of_block_or_statement (parser);
26742 out:
26743 pop_deferring_access_checks ();
26745 /* Clear any current qualification; whatever comes next is the start
26746 of something new. */
26747 parser->scope = NULL_TREE;
26748 parser->qualifying_scope = NULL_TREE;
26749 parser->object_scope = NULL_TREE;
26751 return decl;
26754 /* Parse a cast-expression that is not the operand of a unary "&". */
26756 static cp_expr
26757 cp_parser_simple_cast_expression (cp_parser *parser)
26759 return cp_parser_cast_expression (parser, /*address_p=*/false,
26760 /*cast_p=*/false, /*decltype*/false, NULL);
26763 /* Parse a functional cast to TYPE. Returns an expression
26764 representing the cast. */
26766 static cp_expr
26767 cp_parser_functional_cast (cp_parser* parser, tree type)
26769 vec<tree, va_gc> *vec;
26770 tree expression_list;
26771 cp_expr cast;
26772 bool nonconst_p;
26774 location_t start_loc = input_location;
26776 if (!type)
26777 type = error_mark_node;
26779 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
26781 cp_lexer_set_source_position (parser->lexer);
26782 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
26783 expression_list = cp_parser_braced_list (parser, &nonconst_p);
26784 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
26785 if (TREE_CODE (type) == TYPE_DECL)
26786 type = TREE_TYPE (type);
26788 cast = finish_compound_literal (type, expression_list,
26789 tf_warning_or_error);
26790 /* Create a location of the form:
26791 type_name{i, f}
26792 ^~~~~~~~~~~~~~~
26793 with caret == start at the start of the type name,
26794 finishing at the closing brace. */
26795 location_t finish_loc
26796 = get_finish (cp_lexer_previous_token (parser->lexer)->location);
26797 location_t combined_loc = make_location (start_loc, start_loc,
26798 finish_loc);
26799 cast.set_location (combined_loc);
26800 return cast;
26804 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
26805 /*cast_p=*/true,
26806 /*allow_expansion_p=*/true,
26807 /*non_constant_p=*/NULL);
26808 if (vec == NULL)
26809 expression_list = error_mark_node;
26810 else
26812 expression_list = build_tree_list_vec (vec);
26813 release_tree_vector (vec);
26816 cast = build_functional_cast (type, expression_list,
26817 tf_warning_or_error);
26818 /* [expr.const]/1: In an integral constant expression "only type
26819 conversions to integral or enumeration type can be used". */
26820 if (TREE_CODE (type) == TYPE_DECL)
26821 type = TREE_TYPE (type);
26822 if (cast != error_mark_node
26823 && !cast_valid_in_integral_constant_expression_p (type)
26824 && cp_parser_non_integral_constant_expression (parser,
26825 NIC_CONSTRUCTOR))
26826 return error_mark_node;
26828 /* Create a location of the form:
26829 float(i)
26830 ^~~~~~~~
26831 with caret == start at the start of the type name,
26832 finishing at the closing paren. */
26833 location_t finish_loc
26834 = get_finish (cp_lexer_previous_token (parser->lexer)->location);
26835 location_t combined_loc = make_location (start_loc, start_loc, finish_loc);
26836 cast.set_location (combined_loc);
26837 return cast;
26840 /* Save the tokens that make up the body of a member function defined
26841 in a class-specifier. The DECL_SPECIFIERS and DECLARATOR have
26842 already been parsed. The ATTRIBUTES are any GNU "__attribute__"
26843 specifiers applied to the declaration. Returns the FUNCTION_DECL
26844 for the member function. */
26846 static tree
26847 cp_parser_save_member_function_body (cp_parser* parser,
26848 cp_decl_specifier_seq *decl_specifiers,
26849 cp_declarator *declarator,
26850 tree attributes)
26852 cp_token *first;
26853 cp_token *last;
26854 tree fn;
26855 bool function_try_block = false;
26857 /* Create the FUNCTION_DECL. */
26858 fn = grokmethod (decl_specifiers, declarator, attributes);
26859 cp_finalize_omp_declare_simd (parser, fn);
26860 cp_finalize_oacc_routine (parser, fn, true);
26861 /* If something went badly wrong, bail out now. */
26862 if (fn == error_mark_node)
26864 /* If there's a function-body, skip it. */
26865 if (cp_parser_token_starts_function_definition_p
26866 (cp_lexer_peek_token (parser->lexer)))
26867 cp_parser_skip_to_end_of_block_or_statement (parser);
26868 return error_mark_node;
26871 /* Remember it, if there default args to post process. */
26872 cp_parser_save_default_args (parser, fn);
26874 /* Save away the tokens that make up the body of the
26875 function. */
26876 first = parser->lexer->next_token;
26878 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRANSACTION_RELAXED))
26879 cp_lexer_consume_token (parser->lexer);
26880 else if (cp_lexer_next_token_is_keyword (parser->lexer,
26881 RID_TRANSACTION_ATOMIC))
26883 cp_lexer_consume_token (parser->lexer);
26884 /* Match cp_parser_txn_attribute_opt [[ identifier ]]. */
26885 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE)
26886 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_SQUARE)
26887 && (cp_lexer_nth_token_is (parser->lexer, 3, CPP_NAME)
26888 || cp_lexer_nth_token_is (parser->lexer, 3, CPP_KEYWORD))
26889 && cp_lexer_nth_token_is (parser->lexer, 4, CPP_CLOSE_SQUARE)
26890 && cp_lexer_nth_token_is (parser->lexer, 5, CPP_CLOSE_SQUARE))
26892 cp_lexer_consume_token (parser->lexer);
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);
26898 else
26899 while (cp_next_tokens_can_be_gnu_attribute_p (parser)
26900 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_PAREN))
26902 cp_lexer_consume_token (parser->lexer);
26903 if (cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0))
26904 break;
26908 /* Handle function try blocks. */
26909 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
26911 cp_lexer_consume_token (parser->lexer);
26912 function_try_block = true;
26914 /* We can have braced-init-list mem-initializers before the fn body. */
26915 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
26917 cp_lexer_consume_token (parser->lexer);
26918 while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
26920 /* cache_group will stop after an un-nested { } pair, too. */
26921 if (cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0))
26922 break;
26924 /* variadic mem-inits have ... after the ')'. */
26925 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
26926 cp_lexer_consume_token (parser->lexer);
26929 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
26930 /* Handle function try blocks. */
26931 if (function_try_block)
26932 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
26933 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
26934 last = parser->lexer->next_token;
26936 /* Save away the inline definition; we will process it when the
26937 class is complete. */
26938 DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
26939 DECL_PENDING_INLINE_P (fn) = 1;
26941 /* We need to know that this was defined in the class, so that
26942 friend templates are handled correctly. */
26943 DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
26945 /* Add FN to the queue of functions to be parsed later. */
26946 vec_safe_push (unparsed_funs_with_definitions, fn);
26948 return fn;
26951 /* Save the tokens that make up the in-class initializer for a non-static
26952 data member. Returns a DEFAULT_ARG. */
26954 static tree
26955 cp_parser_save_nsdmi (cp_parser* parser)
26957 return cp_parser_cache_defarg (parser, /*nsdmi=*/true);
26960 /* Parse a template-argument-list, as well as the trailing ">" (but
26961 not the opening "<"). See cp_parser_template_argument_list for the
26962 return value. */
26964 static tree
26965 cp_parser_enclosed_template_argument_list (cp_parser* parser)
26967 tree arguments;
26968 tree saved_scope;
26969 tree saved_qualifying_scope;
26970 tree saved_object_scope;
26971 bool saved_greater_than_is_operator_p;
26972 int saved_unevaluated_operand;
26973 int saved_inhibit_evaluation_warnings;
26975 /* [temp.names]
26977 When parsing a template-id, the first non-nested `>' is taken as
26978 the end of the template-argument-list rather than a greater-than
26979 operator. */
26980 saved_greater_than_is_operator_p
26981 = parser->greater_than_is_operator_p;
26982 parser->greater_than_is_operator_p = false;
26983 /* Parsing the argument list may modify SCOPE, so we save it
26984 here. */
26985 saved_scope = parser->scope;
26986 saved_qualifying_scope = parser->qualifying_scope;
26987 saved_object_scope = parser->object_scope;
26988 /* We need to evaluate the template arguments, even though this
26989 template-id may be nested within a "sizeof". */
26990 saved_unevaluated_operand = cp_unevaluated_operand;
26991 cp_unevaluated_operand = 0;
26992 saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
26993 c_inhibit_evaluation_warnings = 0;
26994 /* Parse the template-argument-list itself. */
26995 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER)
26996 || cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
26997 arguments = NULL_TREE;
26998 else
26999 arguments = cp_parser_template_argument_list (parser);
27000 /* Look for the `>' that ends the template-argument-list. If we find
27001 a '>>' instead, it's probably just a typo. */
27002 if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
27004 if (cxx_dialect != cxx98)
27006 /* In C++0x, a `>>' in a template argument list or cast
27007 expression is considered to be two separate `>'
27008 tokens. So, change the current token to a `>', but don't
27009 consume it: it will be consumed later when the outer
27010 template argument list (or cast expression) is parsed.
27011 Note that this replacement of `>' for `>>' is necessary
27012 even if we are parsing tentatively: in the tentative
27013 case, after calling
27014 cp_parser_enclosed_template_argument_list we will always
27015 throw away all of the template arguments and the first
27016 closing `>', either because the template argument list
27017 was erroneous or because we are replacing those tokens
27018 with a CPP_TEMPLATE_ID token. The second `>' (which will
27019 not have been thrown away) is needed either to close an
27020 outer template argument list or to complete a new-style
27021 cast. */
27022 cp_token *token = cp_lexer_peek_token (parser->lexer);
27023 token->type = CPP_GREATER;
27025 else if (!saved_greater_than_is_operator_p)
27027 /* If we're in a nested template argument list, the '>>' has
27028 to be a typo for '> >'. We emit the error message, but we
27029 continue parsing and we push a '>' as next token, so that
27030 the argument list will be parsed correctly. Note that the
27031 global source location is still on the token before the
27032 '>>', so we need to say explicitly where we want it. */
27033 cp_token *token = cp_lexer_peek_token (parser->lexer);
27034 gcc_rich_location richloc (token->location);
27035 richloc.add_fixit_replace ("> >");
27036 error_at_rich_loc (&richloc, "%<>>%> should be %<> >%> "
27037 "within a nested template argument list");
27039 token->type = CPP_GREATER;
27041 else
27043 /* If this is not a nested template argument list, the '>>'
27044 is a typo for '>'. Emit an error message and continue.
27045 Same deal about the token location, but here we can get it
27046 right by consuming the '>>' before issuing the diagnostic. */
27047 cp_token *token = cp_lexer_consume_token (parser->lexer);
27048 error_at (token->location,
27049 "spurious %<>>%>, use %<>%> to terminate "
27050 "a template argument list");
27053 else
27054 cp_parser_skip_to_end_of_template_parameter_list (parser);
27055 /* The `>' token might be a greater-than operator again now. */
27056 parser->greater_than_is_operator_p
27057 = saved_greater_than_is_operator_p;
27058 /* Restore the SAVED_SCOPE. */
27059 parser->scope = saved_scope;
27060 parser->qualifying_scope = saved_qualifying_scope;
27061 parser->object_scope = saved_object_scope;
27062 cp_unevaluated_operand = saved_unevaluated_operand;
27063 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
27065 return arguments;
27068 /* MEMBER_FUNCTION is a member function, or a friend. If default
27069 arguments, or the body of the function have not yet been parsed,
27070 parse them now. */
27072 static void
27073 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
27075 timevar_push (TV_PARSE_INMETH);
27076 /* If this member is a template, get the underlying
27077 FUNCTION_DECL. */
27078 if (DECL_FUNCTION_TEMPLATE_P (member_function))
27079 member_function = DECL_TEMPLATE_RESULT (member_function);
27081 /* There should not be any class definitions in progress at this
27082 point; the bodies of members are only parsed outside of all class
27083 definitions. */
27084 gcc_assert (parser->num_classes_being_defined == 0);
27085 /* While we're parsing the member functions we might encounter more
27086 classes. We want to handle them right away, but we don't want
27087 them getting mixed up with functions that are currently in the
27088 queue. */
27089 push_unparsed_function_queues (parser);
27091 /* Make sure that any template parameters are in scope. */
27092 maybe_begin_member_template_processing (member_function);
27094 /* If the body of the function has not yet been parsed, parse it
27095 now. */
27096 if (DECL_PENDING_INLINE_P (member_function))
27098 tree function_scope;
27099 cp_token_cache *tokens;
27101 /* The function is no longer pending; we are processing it. */
27102 tokens = DECL_PENDING_INLINE_INFO (member_function);
27103 DECL_PENDING_INLINE_INFO (member_function) = NULL;
27104 DECL_PENDING_INLINE_P (member_function) = 0;
27106 /* If this is a local class, enter the scope of the containing
27107 function. */
27108 function_scope = current_function_decl;
27109 if (function_scope)
27110 push_function_context ();
27112 /* Push the body of the function onto the lexer stack. */
27113 cp_parser_push_lexer_for_tokens (parser, tokens);
27115 /* Let the front end know that we going to be defining this
27116 function. */
27117 start_preparsed_function (member_function, NULL_TREE,
27118 SF_PRE_PARSED | SF_INCLASS_INLINE);
27120 /* Don't do access checking if it is a templated function. */
27121 if (processing_template_decl)
27122 push_deferring_access_checks (dk_no_check);
27124 /* #pragma omp declare reduction needs special parsing. */
27125 if (DECL_OMP_DECLARE_REDUCTION_P (member_function))
27127 parser->lexer->in_pragma = true;
27128 cp_parser_omp_declare_reduction_exprs (member_function, parser);
27129 finish_function (/*inline*/2);
27130 cp_check_omp_declare_reduction (member_function);
27132 else
27133 /* Now, parse the body of the function. */
27134 cp_parser_function_definition_after_declarator (parser,
27135 /*inline_p=*/true);
27137 if (processing_template_decl)
27138 pop_deferring_access_checks ();
27140 /* Leave the scope of the containing function. */
27141 if (function_scope)
27142 pop_function_context ();
27143 cp_parser_pop_lexer (parser);
27146 /* Remove any template parameters from the symbol table. */
27147 maybe_end_member_template_processing ();
27149 /* Restore the queue. */
27150 pop_unparsed_function_queues (parser);
27151 timevar_pop (TV_PARSE_INMETH);
27154 /* If DECL contains any default args, remember it on the unparsed
27155 functions queue. */
27157 static void
27158 cp_parser_save_default_args (cp_parser* parser, tree decl)
27160 tree probe;
27162 for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
27163 probe;
27164 probe = TREE_CHAIN (probe))
27165 if (TREE_PURPOSE (probe))
27167 cp_default_arg_entry entry = {current_class_type, decl};
27168 vec_safe_push (unparsed_funs_with_default_args, entry);
27169 break;
27173 /* DEFAULT_ARG contains the saved tokens for the initializer of DECL,
27174 which is either a FIELD_DECL or PARM_DECL. Parse it and return
27175 the result. For a PARM_DECL, PARMTYPE is the corresponding type
27176 from the parameter-type-list. */
27178 static tree
27179 cp_parser_late_parse_one_default_arg (cp_parser *parser, tree decl,
27180 tree default_arg, tree parmtype)
27182 cp_token_cache *tokens;
27183 tree parsed_arg;
27184 bool dummy;
27186 if (default_arg == error_mark_node)
27187 return error_mark_node;
27189 /* Push the saved tokens for the default argument onto the parser's
27190 lexer stack. */
27191 tokens = DEFARG_TOKENS (default_arg);
27192 cp_parser_push_lexer_for_tokens (parser, tokens);
27194 start_lambda_scope (decl);
27196 /* Parse the default argument. */
27197 parsed_arg = cp_parser_initializer (parser, &dummy, &dummy);
27198 if (BRACE_ENCLOSED_INITIALIZER_P (parsed_arg))
27199 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
27201 finish_lambda_scope ();
27203 if (parsed_arg == error_mark_node)
27204 cp_parser_skip_to_end_of_statement (parser);
27206 if (!processing_template_decl)
27208 /* In a non-template class, check conversions now. In a template,
27209 we'll wait and instantiate these as needed. */
27210 if (TREE_CODE (decl) == PARM_DECL)
27211 parsed_arg = check_default_argument (parmtype, parsed_arg,
27212 tf_warning_or_error);
27213 else if (maybe_reject_flexarray_init (decl, parsed_arg))
27214 parsed_arg = error_mark_node;
27215 else
27216 parsed_arg = digest_nsdmi_init (decl, parsed_arg);
27219 /* If the token stream has not been completely used up, then
27220 there was extra junk after the end of the default
27221 argument. */
27222 if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
27224 if (TREE_CODE (decl) == PARM_DECL)
27225 cp_parser_error (parser, "expected %<,%>");
27226 else
27227 cp_parser_error (parser, "expected %<;%>");
27230 /* Revert to the main lexer. */
27231 cp_parser_pop_lexer (parser);
27233 return parsed_arg;
27236 /* FIELD is a non-static data member with an initializer which we saved for
27237 later; parse it now. */
27239 static void
27240 cp_parser_late_parsing_nsdmi (cp_parser *parser, tree field)
27242 tree def;
27244 maybe_begin_member_template_processing (field);
27246 push_unparsed_function_queues (parser);
27247 def = cp_parser_late_parse_one_default_arg (parser, field,
27248 DECL_INITIAL (field),
27249 NULL_TREE);
27250 pop_unparsed_function_queues (parser);
27252 maybe_end_member_template_processing ();
27254 DECL_INITIAL (field) = def;
27257 /* FN is a FUNCTION_DECL which may contains a parameter with an
27258 unparsed DEFAULT_ARG. Parse the default args now. This function
27259 assumes that the current scope is the scope in which the default
27260 argument should be processed. */
27262 static void
27263 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
27265 bool saved_local_variables_forbidden_p;
27266 tree parm, parmdecl;
27268 /* While we're parsing the default args, we might (due to the
27269 statement expression extension) encounter more classes. We want
27270 to handle them right away, but we don't want them getting mixed
27271 up with default args that are currently in the queue. */
27272 push_unparsed_function_queues (parser);
27274 /* Local variable names (and the `this' keyword) may not appear
27275 in a default argument. */
27276 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
27277 parser->local_variables_forbidden_p = true;
27279 push_defarg_context (fn);
27281 for (parm = TYPE_ARG_TYPES (TREE_TYPE (fn)),
27282 parmdecl = DECL_ARGUMENTS (fn);
27283 parm && parm != void_list_node;
27284 parm = TREE_CHAIN (parm),
27285 parmdecl = DECL_CHAIN (parmdecl))
27287 tree default_arg = TREE_PURPOSE (parm);
27288 tree parsed_arg;
27289 vec<tree, va_gc> *insts;
27290 tree copy;
27291 unsigned ix;
27293 if (!default_arg)
27294 continue;
27296 if (TREE_CODE (default_arg) != DEFAULT_ARG)
27297 /* This can happen for a friend declaration for a function
27298 already declared with default arguments. */
27299 continue;
27301 parsed_arg
27302 = cp_parser_late_parse_one_default_arg (parser, parmdecl,
27303 default_arg,
27304 TREE_VALUE (parm));
27305 if (parsed_arg == error_mark_node)
27307 continue;
27310 TREE_PURPOSE (parm) = parsed_arg;
27312 /* Update any instantiations we've already created. */
27313 for (insts = DEFARG_INSTANTIATIONS (default_arg), ix = 0;
27314 vec_safe_iterate (insts, ix, &copy); ix++)
27315 TREE_PURPOSE (copy) = parsed_arg;
27318 pop_defarg_context ();
27320 /* Make sure no default arg is missing. */
27321 check_default_args (fn);
27323 /* Restore the state of local_variables_forbidden_p. */
27324 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
27326 /* Restore the queue. */
27327 pop_unparsed_function_queues (parser);
27330 /* Subroutine of cp_parser_sizeof_operand, for handling C++11
27332 sizeof ... ( identifier )
27334 where the 'sizeof' token has already been consumed. */
27336 static tree
27337 cp_parser_sizeof_pack (cp_parser *parser)
27339 /* Consume the `...'. */
27340 cp_lexer_consume_token (parser->lexer);
27341 maybe_warn_variadic_templates ();
27343 bool paren = cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN);
27344 if (paren)
27345 cp_lexer_consume_token (parser->lexer);
27346 else
27347 permerror (cp_lexer_peek_token (parser->lexer)->location,
27348 "%<sizeof...%> argument must be surrounded by parentheses");
27350 cp_token *token = cp_lexer_peek_token (parser->lexer);
27351 tree name = cp_parser_identifier (parser);
27352 if (name == error_mark_node)
27353 return error_mark_node;
27354 /* The name is not qualified. */
27355 parser->scope = NULL_TREE;
27356 parser->qualifying_scope = NULL_TREE;
27357 parser->object_scope = NULL_TREE;
27358 tree expr = cp_parser_lookup_name_simple (parser, name, token->location);
27359 if (expr == error_mark_node)
27360 cp_parser_name_lookup_error (parser, name, expr, NLE_NULL,
27361 token->location);
27362 if (TREE_CODE (expr) == TYPE_DECL || TREE_CODE (expr) == TEMPLATE_DECL)
27363 expr = TREE_TYPE (expr);
27364 else if (TREE_CODE (expr) == CONST_DECL)
27365 expr = DECL_INITIAL (expr);
27366 expr = make_pack_expansion (expr);
27367 PACK_EXPANSION_SIZEOF_P (expr) = true;
27369 if (paren)
27370 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
27372 return expr;
27375 /* Parse the operand of `sizeof' (or a similar operator). Returns
27376 either a TYPE or an expression, depending on the form of the
27377 input. The KEYWORD indicates which kind of expression we have
27378 encountered. */
27380 static tree
27381 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
27383 tree expr = NULL_TREE;
27384 const char *saved_message;
27385 char *tmp;
27386 bool saved_integral_constant_expression_p;
27387 bool saved_non_integral_constant_expression_p;
27389 /* If it's a `...', then we are computing the length of a parameter
27390 pack. */
27391 if (keyword == RID_SIZEOF
27392 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
27393 return cp_parser_sizeof_pack (parser);
27395 /* Types cannot be defined in a `sizeof' expression. Save away the
27396 old message. */
27397 saved_message = parser->type_definition_forbidden_message;
27398 /* And create the new one. */
27399 tmp = concat ("types may not be defined in %<",
27400 IDENTIFIER_POINTER (ridpointers[keyword]),
27401 "%> expressions", NULL);
27402 parser->type_definition_forbidden_message = tmp;
27404 /* The restrictions on constant-expressions do not apply inside
27405 sizeof expressions. */
27406 saved_integral_constant_expression_p
27407 = parser->integral_constant_expression_p;
27408 saved_non_integral_constant_expression_p
27409 = parser->non_integral_constant_expression_p;
27410 parser->integral_constant_expression_p = false;
27412 /* Do not actually evaluate the expression. */
27413 ++cp_unevaluated_operand;
27414 ++c_inhibit_evaluation_warnings;
27415 /* If it's a `(', then we might be looking at the type-id
27416 construction. */
27417 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
27419 tree type = NULL_TREE;
27421 /* We can't be sure yet whether we're looking at a type-id or an
27422 expression. */
27423 cp_parser_parse_tentatively (parser);
27424 /* Note: as a GNU Extension, compound literals are considered
27425 postfix-expressions as they are in C99, so they are valid
27426 arguments to sizeof. See comment in cp_parser_cast_expression
27427 for details. */
27428 if (cp_parser_compound_literal_p (parser))
27429 cp_parser_simulate_error (parser);
27430 else
27432 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
27433 parser->in_type_id_in_expr_p = true;
27434 /* Look for the type-id. */
27435 type = cp_parser_type_id (parser);
27436 /* Look for the closing `)'. */
27437 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
27438 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
27441 /* If all went well, then we're done. */
27442 if (cp_parser_parse_definitely (parser))
27444 cp_decl_specifier_seq decl_specs;
27446 /* Build a trivial decl-specifier-seq. */
27447 clear_decl_specs (&decl_specs);
27448 decl_specs.type = type;
27450 /* Call grokdeclarator to figure out what type this is. */
27451 expr = grokdeclarator (NULL,
27452 &decl_specs,
27453 TYPENAME,
27454 /*initialized=*/0,
27455 /*attrlist=*/NULL);
27459 /* If the type-id production did not work out, then we must be
27460 looking at the unary-expression production. */
27461 if (!expr)
27462 expr = cp_parser_unary_expression (parser);
27464 /* Go back to evaluating expressions. */
27465 --cp_unevaluated_operand;
27466 --c_inhibit_evaluation_warnings;
27468 /* Free the message we created. */
27469 free (tmp);
27470 /* And restore the old one. */
27471 parser->type_definition_forbidden_message = saved_message;
27472 parser->integral_constant_expression_p
27473 = saved_integral_constant_expression_p;
27474 parser->non_integral_constant_expression_p
27475 = saved_non_integral_constant_expression_p;
27477 return expr;
27480 /* If the current declaration has no declarator, return true. */
27482 static bool
27483 cp_parser_declares_only_class_p (cp_parser *parser)
27485 /* If the next token is a `;' or a `,' then there is no
27486 declarator. */
27487 return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
27488 || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
27491 /* Update the DECL_SPECS to reflect the storage class indicated by
27492 KEYWORD. */
27494 static void
27495 cp_parser_set_storage_class (cp_parser *parser,
27496 cp_decl_specifier_seq *decl_specs,
27497 enum rid keyword,
27498 cp_token *token)
27500 cp_storage_class storage_class;
27502 if (parser->in_unbraced_linkage_specification_p)
27504 error_at (token->location, "invalid use of %qD in linkage specification",
27505 ridpointers[keyword]);
27506 return;
27508 else if (decl_specs->storage_class != sc_none)
27510 decl_specs->conflicting_specifiers_p = true;
27511 return;
27514 if ((keyword == RID_EXTERN || keyword == RID_STATIC)
27515 && decl_spec_seq_has_spec_p (decl_specs, ds_thread)
27516 && decl_specs->gnu_thread_keyword_p)
27518 pedwarn (decl_specs->locations[ds_thread], 0,
27519 "%<__thread%> before %qD", ridpointers[keyword]);
27522 switch (keyword)
27524 case RID_AUTO:
27525 storage_class = sc_auto;
27526 break;
27527 case RID_REGISTER:
27528 storage_class = sc_register;
27529 break;
27530 case RID_STATIC:
27531 storage_class = sc_static;
27532 break;
27533 case RID_EXTERN:
27534 storage_class = sc_extern;
27535 break;
27536 case RID_MUTABLE:
27537 storage_class = sc_mutable;
27538 break;
27539 default:
27540 gcc_unreachable ();
27542 decl_specs->storage_class = storage_class;
27543 set_and_check_decl_spec_loc (decl_specs, ds_storage_class, token);
27545 /* A storage class specifier cannot be applied alongside a typedef
27546 specifier. If there is a typedef specifier present then set
27547 conflicting_specifiers_p which will trigger an error later
27548 on in grokdeclarator. */
27549 if (decl_spec_seq_has_spec_p (decl_specs, ds_typedef))
27550 decl_specs->conflicting_specifiers_p = true;
27553 /* Update the DECL_SPECS to reflect the TYPE_SPEC. If TYPE_DEFINITION_P
27554 is true, the type is a class or enum definition. */
27556 static void
27557 cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
27558 tree type_spec,
27559 cp_token *token,
27560 bool type_definition_p)
27562 decl_specs->any_specifiers_p = true;
27564 /* If the user tries to redeclare bool, char16_t, char32_t, or wchar_t
27565 (with, for example, in "typedef int wchar_t;") we remember that
27566 this is what happened. In system headers, we ignore these
27567 declarations so that G++ can work with system headers that are not
27568 C++-safe. */
27569 if (decl_spec_seq_has_spec_p (decl_specs, ds_typedef)
27570 && !type_definition_p
27571 && (type_spec == boolean_type_node
27572 || type_spec == char16_type_node
27573 || type_spec == char32_type_node
27574 || type_spec == wchar_type_node)
27575 && (decl_specs->type
27576 || decl_spec_seq_has_spec_p (decl_specs, ds_long)
27577 || decl_spec_seq_has_spec_p (decl_specs, ds_short)
27578 || decl_spec_seq_has_spec_p (decl_specs, ds_unsigned)
27579 || decl_spec_seq_has_spec_p (decl_specs, ds_signed)))
27581 decl_specs->redefined_builtin_type = type_spec;
27582 set_and_check_decl_spec_loc (decl_specs,
27583 ds_redefined_builtin_type_spec,
27584 token);
27585 if (!decl_specs->type)
27587 decl_specs->type = type_spec;
27588 decl_specs->type_definition_p = false;
27589 set_and_check_decl_spec_loc (decl_specs,ds_type_spec, token);
27592 else if (decl_specs->type)
27593 decl_specs->multiple_types_p = true;
27594 else
27596 decl_specs->type = type_spec;
27597 decl_specs->type_definition_p = type_definition_p;
27598 decl_specs->redefined_builtin_type = NULL_TREE;
27599 set_and_check_decl_spec_loc (decl_specs, ds_type_spec, token);
27603 /* True iff TOKEN is the GNU keyword __thread. */
27605 static bool
27606 token_is__thread (cp_token *token)
27608 gcc_assert (token->keyword == RID_THREAD);
27609 return !strcmp (IDENTIFIER_POINTER (token->u.value), "__thread");
27612 /* Set the location for a declarator specifier and check if it is
27613 duplicated.
27615 DECL_SPECS is the sequence of declarator specifiers onto which to
27616 set the location.
27618 DS is the single declarator specifier to set which location is to
27619 be set onto the existing sequence of declarators.
27621 LOCATION is the location for the declarator specifier to
27622 consider. */
27624 static void
27625 set_and_check_decl_spec_loc (cp_decl_specifier_seq *decl_specs,
27626 cp_decl_spec ds, cp_token *token)
27628 gcc_assert (ds < ds_last);
27630 if (decl_specs == NULL)
27631 return;
27633 source_location location = token->location;
27635 if (decl_specs->locations[ds] == 0)
27637 decl_specs->locations[ds] = location;
27638 if (ds == ds_thread)
27639 decl_specs->gnu_thread_keyword_p = token_is__thread (token);
27641 else
27643 if (ds == ds_long)
27645 if (decl_specs->locations[ds_long_long] != 0)
27646 error_at (location,
27647 "%<long long long%> is too long for GCC");
27648 else
27650 decl_specs->locations[ds_long_long] = location;
27651 pedwarn_cxx98 (location,
27652 OPT_Wlong_long,
27653 "ISO C++ 1998 does not support %<long long%>");
27656 else if (ds == ds_thread)
27658 bool gnu = token_is__thread (token);
27659 if (gnu != decl_specs->gnu_thread_keyword_p)
27660 error_at (location,
27661 "both %<__thread%> and %<thread_local%> specified");
27662 else
27663 error_at (location, "duplicate %qD", token->u.value);
27665 else
27667 static const char *const decl_spec_names[] = {
27668 "signed",
27669 "unsigned",
27670 "short",
27671 "long",
27672 "const",
27673 "volatile",
27674 "restrict",
27675 "inline",
27676 "virtual",
27677 "explicit",
27678 "friend",
27679 "typedef",
27680 "using",
27681 "constexpr",
27682 "__complex"
27684 error_at (location,
27685 "duplicate %qs", decl_spec_names[ds]);
27690 /* Return true iff the declarator specifier DS is present in the
27691 sequence of declarator specifiers DECL_SPECS. */
27693 bool
27694 decl_spec_seq_has_spec_p (const cp_decl_specifier_seq * decl_specs,
27695 cp_decl_spec ds)
27697 gcc_assert (ds < ds_last);
27699 if (decl_specs == NULL)
27700 return false;
27702 return decl_specs->locations[ds] != 0;
27705 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
27706 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
27708 static bool
27709 cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
27711 return decl_spec_seq_has_spec_p (decl_specifiers, ds_friend);
27714 /* Issue an error message indicating that TOKEN_DESC was expected.
27715 If KEYWORD is true, it indicated this function is called by
27716 cp_parser_require_keword and the required token can only be
27717 a indicated keyword. */
27719 static void
27720 cp_parser_required_error (cp_parser *parser,
27721 required_token token_desc,
27722 bool keyword)
27724 switch (token_desc)
27726 case RT_NEW:
27727 cp_parser_error (parser, "expected %<new%>");
27728 return;
27729 case RT_DELETE:
27730 cp_parser_error (parser, "expected %<delete%>");
27731 return;
27732 case RT_RETURN:
27733 cp_parser_error (parser, "expected %<return%>");
27734 return;
27735 case RT_WHILE:
27736 cp_parser_error (parser, "expected %<while%>");
27737 return;
27738 case RT_EXTERN:
27739 cp_parser_error (parser, "expected %<extern%>");
27740 return;
27741 case RT_STATIC_ASSERT:
27742 cp_parser_error (parser, "expected %<static_assert%>");
27743 return;
27744 case RT_DECLTYPE:
27745 cp_parser_error (parser, "expected %<decltype%>");
27746 return;
27747 case RT_OPERATOR:
27748 cp_parser_error (parser, "expected %<operator%>");
27749 return;
27750 case RT_CLASS:
27751 cp_parser_error (parser, "expected %<class%>");
27752 return;
27753 case RT_TEMPLATE:
27754 cp_parser_error (parser, "expected %<template%>");
27755 return;
27756 case RT_NAMESPACE:
27757 cp_parser_error (parser, "expected %<namespace%>");
27758 return;
27759 case RT_USING:
27760 cp_parser_error (parser, "expected %<using%>");
27761 return;
27762 case RT_ASM:
27763 cp_parser_error (parser, "expected %<asm%>");
27764 return;
27765 case RT_TRY:
27766 cp_parser_error (parser, "expected %<try%>");
27767 return;
27768 case RT_CATCH:
27769 cp_parser_error (parser, "expected %<catch%>");
27770 return;
27771 case RT_THROW:
27772 cp_parser_error (parser, "expected %<throw%>");
27773 return;
27774 case RT_LABEL:
27775 cp_parser_error (parser, "expected %<__label__%>");
27776 return;
27777 case RT_AT_TRY:
27778 cp_parser_error (parser, "expected %<@try%>");
27779 return;
27780 case RT_AT_SYNCHRONIZED:
27781 cp_parser_error (parser, "expected %<@synchronized%>");
27782 return;
27783 case RT_AT_THROW:
27784 cp_parser_error (parser, "expected %<@throw%>");
27785 return;
27786 case RT_TRANSACTION_ATOMIC:
27787 cp_parser_error (parser, "expected %<__transaction_atomic%>");
27788 return;
27789 case RT_TRANSACTION_RELAXED:
27790 cp_parser_error (parser, "expected %<__transaction_relaxed%>");
27791 return;
27792 default:
27793 break;
27795 if (!keyword)
27797 switch (token_desc)
27799 case RT_SEMICOLON:
27800 cp_parser_error (parser, "expected %<;%>");
27801 return;
27802 case RT_OPEN_PAREN:
27803 cp_parser_error (parser, "expected %<(%>");
27804 return;
27805 case RT_CLOSE_BRACE:
27806 cp_parser_error (parser, "expected %<}%>");
27807 return;
27808 case RT_OPEN_BRACE:
27809 cp_parser_error (parser, "expected %<{%>");
27810 return;
27811 case RT_CLOSE_SQUARE:
27812 cp_parser_error (parser, "expected %<]%>");
27813 return;
27814 case RT_OPEN_SQUARE:
27815 cp_parser_error (parser, "expected %<[%>");
27816 return;
27817 case RT_COMMA:
27818 cp_parser_error (parser, "expected %<,%>");
27819 return;
27820 case RT_SCOPE:
27821 cp_parser_error (parser, "expected %<::%>");
27822 return;
27823 case RT_LESS:
27824 cp_parser_error (parser, "expected %<<%>");
27825 return;
27826 case RT_GREATER:
27827 cp_parser_error (parser, "expected %<>%>");
27828 return;
27829 case RT_EQ:
27830 cp_parser_error (parser, "expected %<=%>");
27831 return;
27832 case RT_ELLIPSIS:
27833 cp_parser_error (parser, "expected %<...%>");
27834 return;
27835 case RT_MULT:
27836 cp_parser_error (parser, "expected %<*%>");
27837 return;
27838 case RT_COMPL:
27839 cp_parser_error (parser, "expected %<~%>");
27840 return;
27841 case RT_COLON:
27842 cp_parser_error (parser, "expected %<:%>");
27843 return;
27844 case RT_COLON_SCOPE:
27845 cp_parser_error (parser, "expected %<:%> or %<::%>");
27846 return;
27847 case RT_CLOSE_PAREN:
27848 cp_parser_error (parser, "expected %<)%>");
27849 return;
27850 case RT_COMMA_CLOSE_PAREN:
27851 cp_parser_error (parser, "expected %<,%> or %<)%>");
27852 return;
27853 case RT_PRAGMA_EOL:
27854 cp_parser_error (parser, "expected end of line");
27855 return;
27856 case RT_NAME:
27857 cp_parser_error (parser, "expected identifier");
27858 return;
27859 case RT_SELECT:
27860 cp_parser_error (parser, "expected selection-statement");
27861 return;
27862 case RT_INTERATION:
27863 cp_parser_error (parser, "expected iteration-statement");
27864 return;
27865 case RT_JUMP:
27866 cp_parser_error (parser, "expected jump-statement");
27867 return;
27868 case RT_CLASS_KEY:
27869 cp_parser_error (parser, "expected class-key");
27870 return;
27871 case RT_CLASS_TYPENAME_TEMPLATE:
27872 cp_parser_error (parser,
27873 "expected %<class%>, %<typename%>, or %<template%>");
27874 return;
27875 default:
27876 gcc_unreachable ();
27879 else
27880 gcc_unreachable ();
27885 /* If the next token is of the indicated TYPE, consume it. Otherwise,
27886 issue an error message indicating that TOKEN_DESC was expected.
27888 Returns the token consumed, if the token had the appropriate type.
27889 Otherwise, returns NULL. */
27891 static cp_token *
27892 cp_parser_require (cp_parser* parser,
27893 enum cpp_ttype type,
27894 required_token token_desc)
27896 if (cp_lexer_next_token_is (parser->lexer, type))
27897 return cp_lexer_consume_token (parser->lexer);
27898 else
27900 /* Output the MESSAGE -- unless we're parsing tentatively. */
27901 if (!cp_parser_simulate_error (parser))
27902 cp_parser_required_error (parser, token_desc, /*keyword=*/false);
27903 return NULL;
27907 /* An error message is produced if the next token is not '>'.
27908 All further tokens are skipped until the desired token is
27909 found or '{', '}', ';' or an unbalanced ')' or ']'. */
27911 static void
27912 cp_parser_skip_to_end_of_template_parameter_list (cp_parser* parser)
27914 /* Current level of '< ... >'. */
27915 unsigned level = 0;
27916 /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'. */
27917 unsigned nesting_depth = 0;
27919 /* Are we ready, yet? If not, issue error message. */
27920 if (cp_parser_require (parser, CPP_GREATER, RT_GREATER))
27921 return;
27923 /* Skip tokens until the desired token is found. */
27924 while (true)
27926 /* Peek at the next token. */
27927 switch (cp_lexer_peek_token (parser->lexer)->type)
27929 case CPP_LESS:
27930 if (!nesting_depth)
27931 ++level;
27932 break;
27934 case CPP_RSHIFT:
27935 if (cxx_dialect == cxx98)
27936 /* C++0x views the `>>' operator as two `>' tokens, but
27937 C++98 does not. */
27938 break;
27939 else if (!nesting_depth && level-- == 0)
27941 /* We've hit a `>>' where the first `>' closes the
27942 template argument list, and the second `>' is
27943 spurious. Just consume the `>>' and stop; we've
27944 already produced at least one error. */
27945 cp_lexer_consume_token (parser->lexer);
27946 return;
27948 /* Fall through for C++0x, so we handle the second `>' in
27949 the `>>'. */
27950 gcc_fallthrough ();
27952 case CPP_GREATER:
27953 if (!nesting_depth && level-- == 0)
27955 /* We've reached the token we want, consume it and stop. */
27956 cp_lexer_consume_token (parser->lexer);
27957 return;
27959 break;
27961 case CPP_OPEN_PAREN:
27962 case CPP_OPEN_SQUARE:
27963 ++nesting_depth;
27964 break;
27966 case CPP_CLOSE_PAREN:
27967 case CPP_CLOSE_SQUARE:
27968 if (nesting_depth-- == 0)
27969 return;
27970 break;
27972 case CPP_EOF:
27973 case CPP_PRAGMA_EOL:
27974 case CPP_SEMICOLON:
27975 case CPP_OPEN_BRACE:
27976 case CPP_CLOSE_BRACE:
27977 /* The '>' was probably forgotten, don't look further. */
27978 return;
27980 default:
27981 break;
27984 /* Consume this token. */
27985 cp_lexer_consume_token (parser->lexer);
27989 /* If the next token is the indicated keyword, consume it. Otherwise,
27990 issue an error message indicating that TOKEN_DESC was expected.
27992 Returns the token consumed, if the token had the appropriate type.
27993 Otherwise, returns NULL. */
27995 static cp_token *
27996 cp_parser_require_keyword (cp_parser* parser,
27997 enum rid keyword,
27998 required_token token_desc)
28000 cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
28002 if (token && token->keyword != keyword)
28004 cp_parser_required_error (parser, token_desc, /*keyword=*/true);
28005 return NULL;
28008 return token;
28011 /* Returns TRUE iff TOKEN is a token that can begin the body of a
28012 function-definition. */
28014 static bool
28015 cp_parser_token_starts_function_definition_p (cp_token* token)
28017 return (/* An ordinary function-body begins with an `{'. */
28018 token->type == CPP_OPEN_BRACE
28019 /* A ctor-initializer begins with a `:'. */
28020 || token->type == CPP_COLON
28021 /* A function-try-block begins with `try'. */
28022 || token->keyword == RID_TRY
28023 /* A function-transaction-block begins with `__transaction_atomic'
28024 or `__transaction_relaxed'. */
28025 || token->keyword == RID_TRANSACTION_ATOMIC
28026 || token->keyword == RID_TRANSACTION_RELAXED
28027 /* The named return value extension begins with `return'. */
28028 || token->keyword == RID_RETURN);
28031 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
28032 definition. */
28034 static bool
28035 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
28037 cp_token *token;
28039 token = cp_lexer_peek_token (parser->lexer);
28040 return (token->type == CPP_OPEN_BRACE
28041 || (token->type == CPP_COLON
28042 && !parser->colon_doesnt_start_class_def_p));
28045 /* Returns TRUE iff the next token is the "," or ">" (or `>>', in
28046 C++0x) ending a template-argument. */
28048 static bool
28049 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
28051 cp_token *token;
28053 token = cp_lexer_peek_token (parser->lexer);
28054 return (token->type == CPP_COMMA
28055 || token->type == CPP_GREATER
28056 || token->type == CPP_ELLIPSIS
28057 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT));
28060 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
28061 (n+1)-th is a ":" (which is a possible digraph typo for "< ::"). */
28063 static bool
28064 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
28065 size_t n)
28067 cp_token *token;
28069 token = cp_lexer_peek_nth_token (parser->lexer, n);
28070 if (token->type == CPP_LESS)
28071 return true;
28072 /* Check for the sequence `<::' in the original code. It would be lexed as
28073 `[:', where `[' is a digraph, and there is no whitespace before
28074 `:'. */
28075 if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
28077 cp_token *token2;
28078 token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
28079 if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
28080 return true;
28082 return false;
28085 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
28086 or none_type otherwise. */
28088 static enum tag_types
28089 cp_parser_token_is_class_key (cp_token* token)
28091 switch (token->keyword)
28093 case RID_CLASS:
28094 return class_type;
28095 case RID_STRUCT:
28096 return record_type;
28097 case RID_UNION:
28098 return union_type;
28100 default:
28101 return none_type;
28105 /* Returns the kind of tag indicated by TOKEN, if it is a type-parameter-key,
28106 or none_type otherwise or if the token is null. */
28108 static enum tag_types
28109 cp_parser_token_is_type_parameter_key (cp_token* token)
28111 if (!token)
28112 return none_type;
28114 switch (token->keyword)
28116 case RID_CLASS:
28117 return class_type;
28118 case RID_TYPENAME:
28119 return typename_type;
28121 default:
28122 return none_type;
28126 /* Issue an error message if the CLASS_KEY does not match the TYPE. */
28128 static void
28129 cp_parser_check_class_key (enum tag_types class_key, tree type)
28131 if (type == error_mark_node)
28132 return;
28133 if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
28135 if (permerror (input_location, "%qs tag used in naming %q#T",
28136 class_key == union_type ? "union"
28137 : class_key == record_type ? "struct" : "class",
28138 type))
28139 inform (DECL_SOURCE_LOCATION (TYPE_NAME (type)),
28140 "%q#T was previously declared here", type);
28144 /* Issue an error message if DECL is redeclared with different
28145 access than its original declaration [class.access.spec/3].
28146 This applies to nested classes, nested class templates and
28147 enumerations [class.mem/1]. */
28149 static void
28150 cp_parser_check_access_in_redeclaration (tree decl, location_t location)
28152 if (!decl
28153 || (!CLASS_TYPE_P (TREE_TYPE (decl))
28154 && TREE_CODE (TREE_TYPE (decl)) != ENUMERAL_TYPE))
28155 return;
28157 if ((TREE_PRIVATE (decl)
28158 != (current_access_specifier == access_private_node))
28159 || (TREE_PROTECTED (decl)
28160 != (current_access_specifier == access_protected_node)))
28161 error_at (location, "%qD redeclared with different access", decl);
28164 /* Look for the `template' keyword, as a syntactic disambiguator.
28165 Return TRUE iff it is present, in which case it will be
28166 consumed. */
28168 static bool
28169 cp_parser_optional_template_keyword (cp_parser *parser)
28171 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
28173 /* In C++98 the `template' keyword can only be used within templates;
28174 outside templates the parser can always figure out what is a
28175 template and what is not. In C++11, per the resolution of DR 468,
28176 `template' is allowed in cases where it is not strictly necessary. */
28177 if (!processing_template_decl
28178 && pedantic && cxx_dialect == cxx98)
28180 cp_token *token = cp_lexer_peek_token (parser->lexer);
28181 pedwarn (token->location, OPT_Wpedantic,
28182 "in C++98 %<template%> (as a disambiguator) is only "
28183 "allowed within templates");
28184 /* If this part of the token stream is rescanned, the same
28185 error message would be generated. So, we purge the token
28186 from the stream. */
28187 cp_lexer_purge_token (parser->lexer);
28188 return false;
28190 else
28192 /* Consume the `template' keyword. */
28193 cp_lexer_consume_token (parser->lexer);
28194 return true;
28197 return false;
28200 /* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
28201 set PARSER->SCOPE, and perform other related actions. */
28203 static void
28204 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
28206 struct tree_check *check_value;
28208 /* Get the stored value. */
28209 check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
28210 /* Set the scope from the stored value. */
28211 parser->scope = saved_checks_value (check_value);
28212 parser->qualifying_scope = check_value->qualifying_scope;
28213 parser->object_scope = NULL_TREE;
28216 /* Consume tokens up through a non-nested END token. Returns TRUE if we
28217 encounter the end of a block before what we were looking for. */
28219 static bool
28220 cp_parser_cache_group (cp_parser *parser,
28221 enum cpp_ttype end,
28222 unsigned depth)
28224 while (true)
28226 cp_token *token = cp_lexer_peek_token (parser->lexer);
28228 /* Abort a parenthesized expression if we encounter a semicolon. */
28229 if ((end == CPP_CLOSE_PAREN || depth == 0)
28230 && token->type == CPP_SEMICOLON)
28231 return true;
28232 /* If we've reached the end of the file, stop. */
28233 if (token->type == CPP_EOF
28234 || (end != CPP_PRAGMA_EOL
28235 && token->type == CPP_PRAGMA_EOL))
28236 return true;
28237 if (token->type == CPP_CLOSE_BRACE && depth == 0)
28238 /* We've hit the end of an enclosing block, so there's been some
28239 kind of syntax error. */
28240 return true;
28242 /* Consume the token. */
28243 cp_lexer_consume_token (parser->lexer);
28244 /* See if it starts a new group. */
28245 if (token->type == CPP_OPEN_BRACE)
28247 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
28248 /* In theory this should probably check end == '}', but
28249 cp_parser_save_member_function_body needs it to exit
28250 after either '}' or ')' when called with ')'. */
28251 if (depth == 0)
28252 return false;
28254 else if (token->type == CPP_OPEN_PAREN)
28256 cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
28257 if (depth == 0 && end == CPP_CLOSE_PAREN)
28258 return false;
28260 else if (token->type == CPP_PRAGMA)
28261 cp_parser_cache_group (parser, CPP_PRAGMA_EOL, depth + 1);
28262 else if (token->type == end)
28263 return false;
28267 /* Like above, for caching a default argument or NSDMI. Both of these are
28268 terminated by a non-nested comma, but it can be unclear whether or not a
28269 comma is nested in a template argument list unless we do more parsing.
28270 In order to handle this ambiguity, when we encounter a ',' after a '<'
28271 we try to parse what follows as a parameter-declaration-list (in the
28272 case of a default argument) or a member-declarator (in the case of an
28273 NSDMI). If that succeeds, then we stop caching. */
28275 static tree
28276 cp_parser_cache_defarg (cp_parser *parser, bool nsdmi)
28278 unsigned depth = 0;
28279 int maybe_template_id = 0;
28280 cp_token *first_token;
28281 cp_token *token;
28282 tree default_argument;
28284 /* Add tokens until we have processed the entire default
28285 argument. We add the range [first_token, token). */
28286 first_token = cp_lexer_peek_token (parser->lexer);
28287 if (first_token->type == CPP_OPEN_BRACE)
28289 /* For list-initialization, this is straightforward. */
28290 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
28291 token = cp_lexer_peek_token (parser->lexer);
28293 else while (true)
28295 bool done = false;
28297 /* Peek at the next token. */
28298 token = cp_lexer_peek_token (parser->lexer);
28299 /* What we do depends on what token we have. */
28300 switch (token->type)
28302 /* In valid code, a default argument must be
28303 immediately followed by a `,' `)', or `...'. */
28304 case CPP_COMMA:
28305 if (depth == 0 && maybe_template_id)
28307 /* If we've seen a '<', we might be in a
28308 template-argument-list. Until Core issue 325 is
28309 resolved, we don't know how this situation ought
28310 to be handled, so try to DTRT. We check whether
28311 what comes after the comma is a valid parameter
28312 declaration list. If it is, then the comma ends
28313 the default argument; otherwise the default
28314 argument continues. */
28315 bool error = false;
28316 cp_token *peek;
28318 /* Set ITALP so cp_parser_parameter_declaration_list
28319 doesn't decide to commit to this parse. */
28320 bool saved_italp = parser->in_template_argument_list_p;
28321 parser->in_template_argument_list_p = true;
28323 cp_parser_parse_tentatively (parser);
28325 if (nsdmi)
28327 /* Parse declarators until we reach a non-comma or
28328 somthing that cannot be an initializer.
28329 Just checking whether we're looking at a single
28330 declarator is insufficient. Consider:
28331 int var = tuple<T,U>::x;
28332 The template parameter 'U' looks exactly like a
28333 declarator. */
28336 int ctor_dtor_or_conv_p;
28337 cp_lexer_consume_token (parser->lexer);
28338 cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
28339 &ctor_dtor_or_conv_p,
28340 /*parenthesized_p=*/NULL,
28341 /*member_p=*/true,
28342 /*friend_p=*/false);
28343 peek = cp_lexer_peek_token (parser->lexer);
28344 if (cp_parser_error_occurred (parser))
28345 break;
28347 while (peek->type == CPP_COMMA);
28348 /* If we met an '=' or ';' then the original comma
28349 was the end of the NSDMI. Otherwise assume
28350 we're still in the NSDMI. */
28351 error = (peek->type != CPP_EQ
28352 && peek->type != CPP_SEMICOLON);
28354 else
28356 cp_lexer_consume_token (parser->lexer);
28357 begin_scope (sk_function_parms, NULL_TREE);
28358 cp_parser_parameter_declaration_list (parser, &error);
28359 pop_bindings_and_leave_scope ();
28361 if (!cp_parser_error_occurred (parser) && !error)
28362 done = true;
28363 cp_parser_abort_tentative_parse (parser);
28365 parser->in_template_argument_list_p = saved_italp;
28366 break;
28368 /* FALLTHRU */
28369 case CPP_CLOSE_PAREN:
28370 case CPP_ELLIPSIS:
28371 /* If we run into a non-nested `;', `}', or `]',
28372 then the code is invalid -- but the default
28373 argument is certainly over. */
28374 case CPP_SEMICOLON:
28375 case CPP_CLOSE_BRACE:
28376 case CPP_CLOSE_SQUARE:
28377 if (depth == 0
28378 /* Handle correctly int n = sizeof ... ( p ); */
28379 && token->type != CPP_ELLIPSIS)
28380 done = true;
28381 /* Update DEPTH, if necessary. */
28382 else if (token->type == CPP_CLOSE_PAREN
28383 || token->type == CPP_CLOSE_BRACE
28384 || token->type == CPP_CLOSE_SQUARE)
28385 --depth;
28386 break;
28388 case CPP_OPEN_PAREN:
28389 case CPP_OPEN_SQUARE:
28390 case CPP_OPEN_BRACE:
28391 ++depth;
28392 break;
28394 case CPP_LESS:
28395 if (depth == 0)
28396 /* This might be the comparison operator, or it might
28397 start a template argument list. */
28398 ++maybe_template_id;
28399 break;
28401 case CPP_RSHIFT:
28402 if (cxx_dialect == cxx98)
28403 break;
28404 /* Fall through for C++0x, which treats the `>>'
28405 operator like two `>' tokens in certain
28406 cases. */
28407 gcc_fallthrough ();
28409 case CPP_GREATER:
28410 if (depth == 0)
28412 /* This might be an operator, or it might close a
28413 template argument list. But if a previous '<'
28414 started a template argument list, this will have
28415 closed it, so we can't be in one anymore. */
28416 maybe_template_id -= 1 + (token->type == CPP_RSHIFT);
28417 if (maybe_template_id < 0)
28418 maybe_template_id = 0;
28420 break;
28422 /* If we run out of tokens, issue an error message. */
28423 case CPP_EOF:
28424 case CPP_PRAGMA_EOL:
28425 error_at (token->location, "file ends in default argument");
28426 return error_mark_node;
28428 case CPP_NAME:
28429 case CPP_SCOPE:
28430 /* In these cases, we should look for template-ids.
28431 For example, if the default argument is
28432 `X<int, double>()', we need to do name lookup to
28433 figure out whether or not `X' is a template; if
28434 so, the `,' does not end the default argument.
28436 That is not yet done. */
28437 break;
28439 default:
28440 break;
28443 /* If we've reached the end, stop. */
28444 if (done)
28445 break;
28447 /* Add the token to the token block. */
28448 token = cp_lexer_consume_token (parser->lexer);
28451 /* Create a DEFAULT_ARG to represent the unparsed default
28452 argument. */
28453 default_argument = make_node (DEFAULT_ARG);
28454 DEFARG_TOKENS (default_argument)
28455 = cp_token_cache_new (first_token, token);
28456 DEFARG_INSTANTIATIONS (default_argument) = NULL;
28458 return default_argument;
28461 /* Begin parsing tentatively. We always save tokens while parsing
28462 tentatively so that if the tentative parsing fails we can restore the
28463 tokens. */
28465 static void
28466 cp_parser_parse_tentatively (cp_parser* parser)
28468 /* Enter a new parsing context. */
28469 parser->context = cp_parser_context_new (parser->context);
28470 /* Begin saving tokens. */
28471 cp_lexer_save_tokens (parser->lexer);
28472 /* In order to avoid repetitive access control error messages,
28473 access checks are queued up until we are no longer parsing
28474 tentatively. */
28475 push_deferring_access_checks (dk_deferred);
28478 /* Commit to the currently active tentative parse. */
28480 static void
28481 cp_parser_commit_to_tentative_parse (cp_parser* parser)
28483 cp_parser_context *context;
28484 cp_lexer *lexer;
28486 /* Mark all of the levels as committed. */
28487 lexer = parser->lexer;
28488 for (context = parser->context; context->next; context = context->next)
28490 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
28491 break;
28492 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
28493 while (!cp_lexer_saving_tokens (lexer))
28494 lexer = lexer->next;
28495 cp_lexer_commit_tokens (lexer);
28499 /* Commit to the topmost currently active tentative parse.
28501 Note that this function shouldn't be called when there are
28502 irreversible side-effects while in a tentative state. For
28503 example, we shouldn't create a permanent entry in the symbol
28504 table, or issue an error message that might not apply if the
28505 tentative parse is aborted. */
28507 static void
28508 cp_parser_commit_to_topmost_tentative_parse (cp_parser* parser)
28510 cp_parser_context *context = parser->context;
28511 cp_lexer *lexer = parser->lexer;
28513 if (context)
28515 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
28516 return;
28517 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
28519 while (!cp_lexer_saving_tokens (lexer))
28520 lexer = lexer->next;
28521 cp_lexer_commit_tokens (lexer);
28525 /* Abort the currently active tentative parse. All consumed tokens
28526 will be rolled back, and no diagnostics will be issued. */
28528 static void
28529 cp_parser_abort_tentative_parse (cp_parser* parser)
28531 gcc_assert (parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED
28532 || errorcount > 0);
28533 cp_parser_simulate_error (parser);
28534 /* Now, pretend that we want to see if the construct was
28535 successfully parsed. */
28536 cp_parser_parse_definitely (parser);
28539 /* Stop parsing tentatively. If a parse error has occurred, restore the
28540 token stream. Otherwise, commit to the tokens we have consumed.
28541 Returns true if no error occurred; false otherwise. */
28543 static bool
28544 cp_parser_parse_definitely (cp_parser* parser)
28546 bool error_occurred;
28547 cp_parser_context *context;
28549 /* Remember whether or not an error occurred, since we are about to
28550 destroy that information. */
28551 error_occurred = cp_parser_error_occurred (parser);
28552 /* Remove the topmost context from the stack. */
28553 context = parser->context;
28554 parser->context = context->next;
28555 /* If no parse errors occurred, commit to the tentative parse. */
28556 if (!error_occurred)
28558 /* Commit to the tokens read tentatively, unless that was
28559 already done. */
28560 if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
28561 cp_lexer_commit_tokens (parser->lexer);
28563 pop_to_parent_deferring_access_checks ();
28565 /* Otherwise, if errors occurred, roll back our state so that things
28566 are just as they were before we began the tentative parse. */
28567 else
28569 cp_lexer_rollback_tokens (parser->lexer);
28570 pop_deferring_access_checks ();
28572 /* Add the context to the front of the free list. */
28573 context->next = cp_parser_context_free_list;
28574 cp_parser_context_free_list = context;
28576 return !error_occurred;
28579 /* Returns true if we are parsing tentatively and are not committed to
28580 this tentative parse. */
28582 static bool
28583 cp_parser_uncommitted_to_tentative_parse_p (cp_parser* parser)
28585 return (cp_parser_parsing_tentatively (parser)
28586 && parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED);
28589 /* Returns nonzero iff an error has occurred during the most recent
28590 tentative parse. */
28592 static bool
28593 cp_parser_error_occurred (cp_parser* parser)
28595 return (cp_parser_parsing_tentatively (parser)
28596 && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
28599 /* Returns nonzero if GNU extensions are allowed. */
28601 static bool
28602 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
28604 return parser->allow_gnu_extensions_p;
28607 /* Objective-C++ Productions */
28610 /* Parse an Objective-C expression, which feeds into a primary-expression
28611 above.
28613 objc-expression:
28614 objc-message-expression
28615 objc-string-literal
28616 objc-encode-expression
28617 objc-protocol-expression
28618 objc-selector-expression
28620 Returns a tree representation of the expression. */
28622 static cp_expr
28623 cp_parser_objc_expression (cp_parser* parser)
28625 /* Try to figure out what kind of declaration is present. */
28626 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
28628 switch (kwd->type)
28630 case CPP_OPEN_SQUARE:
28631 return cp_parser_objc_message_expression (parser);
28633 case CPP_OBJC_STRING:
28634 kwd = cp_lexer_consume_token (parser->lexer);
28635 return objc_build_string_object (kwd->u.value);
28637 case CPP_KEYWORD:
28638 switch (kwd->keyword)
28640 case RID_AT_ENCODE:
28641 return cp_parser_objc_encode_expression (parser);
28643 case RID_AT_PROTOCOL:
28644 return cp_parser_objc_protocol_expression (parser);
28646 case RID_AT_SELECTOR:
28647 return cp_parser_objc_selector_expression (parser);
28649 default:
28650 break;
28652 default:
28653 error_at (kwd->location,
28654 "misplaced %<@%D%> Objective-C++ construct",
28655 kwd->u.value);
28656 cp_parser_skip_to_end_of_block_or_statement (parser);
28659 return error_mark_node;
28662 /* Parse an Objective-C message expression.
28664 objc-message-expression:
28665 [ objc-message-receiver objc-message-args ]
28667 Returns a representation of an Objective-C message. */
28669 static tree
28670 cp_parser_objc_message_expression (cp_parser* parser)
28672 tree receiver, messageargs;
28674 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
28675 cp_lexer_consume_token (parser->lexer); /* Eat '['. */
28676 receiver = cp_parser_objc_message_receiver (parser);
28677 messageargs = cp_parser_objc_message_args (parser);
28678 location_t end_loc = cp_lexer_peek_token (parser->lexer)->location;
28679 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
28681 tree result = objc_build_message_expr (receiver, messageargs);
28683 /* Construct a location e.g.
28684 [self func1:5]
28685 ^~~~~~~~~~~~~~
28686 ranging from the '[' to the ']', with the caret at the start. */
28687 location_t combined_loc = make_location (start_loc, start_loc, end_loc);
28688 protected_set_expr_location (result, combined_loc);
28690 return result;
28693 /* Parse an objc-message-receiver.
28695 objc-message-receiver:
28696 expression
28697 simple-type-specifier
28699 Returns a representation of the type or expression. */
28701 static tree
28702 cp_parser_objc_message_receiver (cp_parser* parser)
28704 tree rcv;
28706 /* An Objective-C message receiver may be either (1) a type
28707 or (2) an expression. */
28708 cp_parser_parse_tentatively (parser);
28709 rcv = cp_parser_expression (parser);
28711 /* If that worked out, fine. */
28712 if (cp_parser_parse_definitely (parser))
28713 return rcv;
28715 cp_parser_parse_tentatively (parser);
28716 rcv = cp_parser_simple_type_specifier (parser,
28717 /*decl_specs=*/NULL,
28718 CP_PARSER_FLAGS_NONE);
28720 if (cp_parser_parse_definitely (parser))
28721 return objc_get_class_reference (rcv);
28723 cp_parser_error (parser, "objective-c++ message receiver expected");
28724 return error_mark_node;
28727 /* Parse the arguments and selectors comprising an Objective-C message.
28729 objc-message-args:
28730 objc-selector
28731 objc-selector-args
28732 objc-selector-args , objc-comma-args
28734 objc-selector-args:
28735 objc-selector [opt] : assignment-expression
28736 objc-selector-args objc-selector [opt] : assignment-expression
28738 objc-comma-args:
28739 assignment-expression
28740 objc-comma-args , assignment-expression
28742 Returns a TREE_LIST, with TREE_PURPOSE containing a list of
28743 selector arguments and TREE_VALUE containing a list of comma
28744 arguments. */
28746 static tree
28747 cp_parser_objc_message_args (cp_parser* parser)
28749 tree sel_args = NULL_TREE, addl_args = NULL_TREE;
28750 bool maybe_unary_selector_p = true;
28751 cp_token *token = cp_lexer_peek_token (parser->lexer);
28753 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
28755 tree selector = NULL_TREE, arg;
28757 if (token->type != CPP_COLON)
28758 selector = cp_parser_objc_selector (parser);
28760 /* Detect if we have a unary selector. */
28761 if (maybe_unary_selector_p
28762 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
28763 return build_tree_list (selector, NULL_TREE);
28765 maybe_unary_selector_p = false;
28766 cp_parser_require (parser, CPP_COLON, RT_COLON);
28767 arg = cp_parser_assignment_expression (parser);
28769 sel_args
28770 = chainon (sel_args,
28771 build_tree_list (selector, arg));
28773 token = cp_lexer_peek_token (parser->lexer);
28776 /* Handle non-selector arguments, if any. */
28777 while (token->type == CPP_COMMA)
28779 tree arg;
28781 cp_lexer_consume_token (parser->lexer);
28782 arg = cp_parser_assignment_expression (parser);
28784 addl_args
28785 = chainon (addl_args,
28786 build_tree_list (NULL_TREE, arg));
28788 token = cp_lexer_peek_token (parser->lexer);
28791 if (sel_args == NULL_TREE && addl_args == NULL_TREE)
28793 cp_parser_error (parser, "objective-c++ message argument(s) are expected");
28794 return build_tree_list (error_mark_node, error_mark_node);
28797 return build_tree_list (sel_args, addl_args);
28800 /* Parse an Objective-C encode expression.
28802 objc-encode-expression:
28803 @encode objc-typename
28805 Returns an encoded representation of the type argument. */
28807 static cp_expr
28808 cp_parser_objc_encode_expression (cp_parser* parser)
28810 tree type;
28811 cp_token *token;
28812 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
28814 cp_lexer_consume_token (parser->lexer); /* Eat '@encode'. */
28815 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
28816 token = cp_lexer_peek_token (parser->lexer);
28817 type = complete_type (cp_parser_type_id (parser));
28818 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
28820 if (!type)
28822 error_at (token->location,
28823 "%<@encode%> must specify a type as an argument");
28824 return error_mark_node;
28827 /* This happens if we find @encode(T) (where T is a template
28828 typename or something dependent on a template typename) when
28829 parsing a template. In that case, we can't compile it
28830 immediately, but we rather create an AT_ENCODE_EXPR which will
28831 need to be instantiated when the template is used.
28833 if (dependent_type_p (type))
28835 tree value = build_min (AT_ENCODE_EXPR, size_type_node, type);
28836 TREE_READONLY (value) = 1;
28837 return value;
28841 /* Build a location of the form:
28842 @encode(int)
28843 ^~~~~~~~~~~~
28844 with caret==start at the @ token, finishing at the close paren. */
28845 location_t combined_loc
28846 = make_location (start_loc, start_loc,
28847 cp_lexer_previous_token (parser->lexer)->location);
28849 return cp_expr (objc_build_encode_expr (type), combined_loc);
28852 /* Parse an Objective-C @defs expression. */
28854 static tree
28855 cp_parser_objc_defs_expression (cp_parser *parser)
28857 tree name;
28859 cp_lexer_consume_token (parser->lexer); /* Eat '@defs'. */
28860 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
28861 name = cp_parser_identifier (parser);
28862 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
28864 return objc_get_class_ivars (name);
28867 /* Parse an Objective-C protocol expression.
28869 objc-protocol-expression:
28870 @protocol ( identifier )
28872 Returns a representation of the protocol expression. */
28874 static tree
28875 cp_parser_objc_protocol_expression (cp_parser* parser)
28877 tree proto;
28878 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
28880 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
28881 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
28882 proto = cp_parser_identifier (parser);
28883 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
28885 /* Build a location of the form:
28886 @protocol(prot)
28887 ^~~~~~~~~~~~~~~
28888 with caret==start at the @ token, finishing at the close paren. */
28889 location_t combined_loc
28890 = make_location (start_loc, start_loc,
28891 cp_lexer_previous_token (parser->lexer)->location);
28892 tree result = objc_build_protocol_expr (proto);
28893 protected_set_expr_location (result, combined_loc);
28894 return result;
28897 /* Parse an Objective-C selector expression.
28899 objc-selector-expression:
28900 @selector ( objc-method-signature )
28902 objc-method-signature:
28903 objc-selector
28904 objc-selector-seq
28906 objc-selector-seq:
28907 objc-selector :
28908 objc-selector-seq objc-selector :
28910 Returns a representation of the method selector. */
28912 static tree
28913 cp_parser_objc_selector_expression (cp_parser* parser)
28915 tree sel_seq = NULL_TREE;
28916 bool maybe_unary_selector_p = true;
28917 cp_token *token;
28918 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
28920 cp_lexer_consume_token (parser->lexer); /* Eat '@selector'. */
28921 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
28922 token = cp_lexer_peek_token (parser->lexer);
28924 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON
28925 || token->type == CPP_SCOPE)
28927 tree selector = NULL_TREE;
28929 if (token->type != CPP_COLON
28930 || token->type == CPP_SCOPE)
28931 selector = cp_parser_objc_selector (parser);
28933 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON)
28934 && cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
28936 /* Detect if we have a unary selector. */
28937 if (maybe_unary_selector_p)
28939 sel_seq = selector;
28940 goto finish_selector;
28942 else
28944 cp_parser_error (parser, "expected %<:%>");
28947 maybe_unary_selector_p = false;
28948 token = cp_lexer_consume_token (parser->lexer);
28950 if (token->type == CPP_SCOPE)
28952 sel_seq
28953 = chainon (sel_seq,
28954 build_tree_list (selector, NULL_TREE));
28955 sel_seq
28956 = chainon (sel_seq,
28957 build_tree_list (NULL_TREE, NULL_TREE));
28959 else
28960 sel_seq
28961 = chainon (sel_seq,
28962 build_tree_list (selector, NULL_TREE));
28964 token = cp_lexer_peek_token (parser->lexer);
28967 finish_selector:
28968 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
28971 /* Build a location of the form:
28972 @selector(func)
28973 ^~~~~~~~~~~~~~~
28974 with caret==start at the @ token, finishing at the close paren. */
28975 location_t combined_loc
28976 = make_location (loc, loc,
28977 cp_lexer_previous_token (parser->lexer)->location);
28978 tree result = objc_build_selector_expr (combined_loc, sel_seq);
28979 /* TODO: objc_build_selector_expr doesn't always honor the location. */
28980 protected_set_expr_location (result, combined_loc);
28981 return result;
28984 /* Parse a list of identifiers.
28986 objc-identifier-list:
28987 identifier
28988 objc-identifier-list , identifier
28990 Returns a TREE_LIST of identifier nodes. */
28992 static tree
28993 cp_parser_objc_identifier_list (cp_parser* parser)
28995 tree identifier;
28996 tree list;
28997 cp_token *sep;
28999 identifier = cp_parser_identifier (parser);
29000 if (identifier == error_mark_node)
29001 return error_mark_node;
29003 list = build_tree_list (NULL_TREE, identifier);
29004 sep = cp_lexer_peek_token (parser->lexer);
29006 while (sep->type == CPP_COMMA)
29008 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
29009 identifier = cp_parser_identifier (parser);
29010 if (identifier == error_mark_node)
29011 return list;
29013 list = chainon (list, build_tree_list (NULL_TREE,
29014 identifier));
29015 sep = cp_lexer_peek_token (parser->lexer);
29018 return list;
29021 /* Parse an Objective-C alias declaration.
29023 objc-alias-declaration:
29024 @compatibility_alias identifier identifier ;
29026 This function registers the alias mapping with the Objective-C front end.
29027 It returns nothing. */
29029 static void
29030 cp_parser_objc_alias_declaration (cp_parser* parser)
29032 tree alias, orig;
29034 cp_lexer_consume_token (parser->lexer); /* Eat '@compatibility_alias'. */
29035 alias = cp_parser_identifier (parser);
29036 orig = cp_parser_identifier (parser);
29037 objc_declare_alias (alias, orig);
29038 cp_parser_consume_semicolon_at_end_of_statement (parser);
29041 /* Parse an Objective-C class forward-declaration.
29043 objc-class-declaration:
29044 @class objc-identifier-list ;
29046 The function registers the forward declarations with the Objective-C
29047 front end. It returns nothing. */
29049 static void
29050 cp_parser_objc_class_declaration (cp_parser* parser)
29052 cp_lexer_consume_token (parser->lexer); /* Eat '@class'. */
29053 while (true)
29055 tree id;
29057 id = cp_parser_identifier (parser);
29058 if (id == error_mark_node)
29059 break;
29061 objc_declare_class (id);
29063 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
29064 cp_lexer_consume_token (parser->lexer);
29065 else
29066 break;
29068 cp_parser_consume_semicolon_at_end_of_statement (parser);
29071 /* Parse a list of Objective-C protocol references.
29073 objc-protocol-refs-opt:
29074 objc-protocol-refs [opt]
29076 objc-protocol-refs:
29077 < objc-identifier-list >
29079 Returns a TREE_LIST of identifiers, if any. */
29081 static tree
29082 cp_parser_objc_protocol_refs_opt (cp_parser* parser)
29084 tree protorefs = NULL_TREE;
29086 if(cp_lexer_next_token_is (parser->lexer, CPP_LESS))
29088 cp_lexer_consume_token (parser->lexer); /* Eat '<'. */
29089 protorefs = cp_parser_objc_identifier_list (parser);
29090 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
29093 return protorefs;
29096 /* Parse a Objective-C visibility specification. */
29098 static void
29099 cp_parser_objc_visibility_spec (cp_parser* parser)
29101 cp_token *vis = cp_lexer_peek_token (parser->lexer);
29103 switch (vis->keyword)
29105 case RID_AT_PRIVATE:
29106 objc_set_visibility (OBJC_IVAR_VIS_PRIVATE);
29107 break;
29108 case RID_AT_PROTECTED:
29109 objc_set_visibility (OBJC_IVAR_VIS_PROTECTED);
29110 break;
29111 case RID_AT_PUBLIC:
29112 objc_set_visibility (OBJC_IVAR_VIS_PUBLIC);
29113 break;
29114 case RID_AT_PACKAGE:
29115 objc_set_visibility (OBJC_IVAR_VIS_PACKAGE);
29116 break;
29117 default:
29118 return;
29121 /* Eat '@private'/'@protected'/'@public'. */
29122 cp_lexer_consume_token (parser->lexer);
29125 /* Parse an Objective-C method type. Return 'true' if it is a class
29126 (+) method, and 'false' if it is an instance (-) method. */
29128 static inline bool
29129 cp_parser_objc_method_type (cp_parser* parser)
29131 if (cp_lexer_consume_token (parser->lexer)->type == CPP_PLUS)
29132 return true;
29133 else
29134 return false;
29137 /* Parse an Objective-C protocol qualifier. */
29139 static tree
29140 cp_parser_objc_protocol_qualifiers (cp_parser* parser)
29142 tree quals = NULL_TREE, node;
29143 cp_token *token = cp_lexer_peek_token (parser->lexer);
29145 node = token->u.value;
29147 while (node && identifier_p (node)
29148 && (node == ridpointers [(int) RID_IN]
29149 || node == ridpointers [(int) RID_OUT]
29150 || node == ridpointers [(int) RID_INOUT]
29151 || node == ridpointers [(int) RID_BYCOPY]
29152 || node == ridpointers [(int) RID_BYREF]
29153 || node == ridpointers [(int) RID_ONEWAY]))
29155 quals = tree_cons (NULL_TREE, node, quals);
29156 cp_lexer_consume_token (parser->lexer);
29157 token = cp_lexer_peek_token (parser->lexer);
29158 node = token->u.value;
29161 return quals;
29164 /* Parse an Objective-C typename. */
29166 static tree
29167 cp_parser_objc_typename (cp_parser* parser)
29169 tree type_name = NULL_TREE;
29171 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
29173 tree proto_quals, cp_type = NULL_TREE;
29175 cp_lexer_consume_token (parser->lexer); /* Eat '('. */
29176 proto_quals = cp_parser_objc_protocol_qualifiers (parser);
29178 /* An ObjC type name may consist of just protocol qualifiers, in which
29179 case the type shall default to 'id'. */
29180 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
29182 cp_type = cp_parser_type_id (parser);
29184 /* If the type could not be parsed, an error has already
29185 been produced. For error recovery, behave as if it had
29186 not been specified, which will use the default type
29187 'id'. */
29188 if (cp_type == error_mark_node)
29190 cp_type = NULL_TREE;
29191 /* We need to skip to the closing parenthesis as
29192 cp_parser_type_id() does not seem to do it for
29193 us. */
29194 cp_parser_skip_to_closing_parenthesis (parser,
29195 /*recovering=*/true,
29196 /*or_comma=*/false,
29197 /*consume_paren=*/false);
29201 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
29202 type_name = build_tree_list (proto_quals, cp_type);
29205 return type_name;
29208 /* Check to see if TYPE refers to an Objective-C selector name. */
29210 static bool
29211 cp_parser_objc_selector_p (enum cpp_ttype type)
29213 return (type == CPP_NAME || type == CPP_KEYWORD
29214 || type == CPP_AND_AND || type == CPP_AND_EQ || type == CPP_AND
29215 || type == CPP_OR || type == CPP_COMPL || type == CPP_NOT
29216 || type == CPP_NOT_EQ || type == CPP_OR_OR || type == CPP_OR_EQ
29217 || type == CPP_XOR || type == CPP_XOR_EQ);
29220 /* Parse an Objective-C selector. */
29222 static tree
29223 cp_parser_objc_selector (cp_parser* parser)
29225 cp_token *token = cp_lexer_consume_token (parser->lexer);
29227 if (!cp_parser_objc_selector_p (token->type))
29229 error_at (token->location, "invalid Objective-C++ selector name");
29230 return error_mark_node;
29233 /* C++ operator names are allowed to appear in ObjC selectors. */
29234 switch (token->type)
29236 case CPP_AND_AND: return get_identifier ("and");
29237 case CPP_AND_EQ: return get_identifier ("and_eq");
29238 case CPP_AND: return get_identifier ("bitand");
29239 case CPP_OR: return get_identifier ("bitor");
29240 case CPP_COMPL: return get_identifier ("compl");
29241 case CPP_NOT: return get_identifier ("not");
29242 case CPP_NOT_EQ: return get_identifier ("not_eq");
29243 case CPP_OR_OR: return get_identifier ("or");
29244 case CPP_OR_EQ: return get_identifier ("or_eq");
29245 case CPP_XOR: return get_identifier ("xor");
29246 case CPP_XOR_EQ: return get_identifier ("xor_eq");
29247 default: return token->u.value;
29251 /* Parse an Objective-C params list. */
29253 static tree
29254 cp_parser_objc_method_keyword_params (cp_parser* parser, tree* attributes)
29256 tree params = NULL_TREE;
29257 bool maybe_unary_selector_p = true;
29258 cp_token *token = cp_lexer_peek_token (parser->lexer);
29260 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
29262 tree selector = NULL_TREE, type_name, identifier;
29263 tree parm_attr = NULL_TREE;
29265 if (token->keyword == RID_ATTRIBUTE)
29266 break;
29268 if (token->type != CPP_COLON)
29269 selector = cp_parser_objc_selector (parser);
29271 /* Detect if we have a unary selector. */
29272 if (maybe_unary_selector_p
29273 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
29275 params = selector; /* Might be followed by attributes. */
29276 break;
29279 maybe_unary_selector_p = false;
29280 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
29282 /* Something went quite wrong. There should be a colon
29283 here, but there is not. Stop parsing parameters. */
29284 break;
29286 type_name = cp_parser_objc_typename (parser);
29287 /* New ObjC allows attributes on parameters too. */
29288 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
29289 parm_attr = cp_parser_attributes_opt (parser);
29290 identifier = cp_parser_identifier (parser);
29292 params
29293 = chainon (params,
29294 objc_build_keyword_decl (selector,
29295 type_name,
29296 identifier,
29297 parm_attr));
29299 token = cp_lexer_peek_token (parser->lexer);
29302 if (params == NULL_TREE)
29304 cp_parser_error (parser, "objective-c++ method declaration is expected");
29305 return error_mark_node;
29308 /* We allow tail attributes for the method. */
29309 if (token->keyword == RID_ATTRIBUTE)
29311 *attributes = cp_parser_attributes_opt (parser);
29312 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
29313 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
29314 return params;
29315 cp_parser_error (parser,
29316 "method attributes must be specified at the end");
29317 return error_mark_node;
29320 if (params == NULL_TREE)
29322 cp_parser_error (parser, "objective-c++ method declaration is expected");
29323 return error_mark_node;
29325 return params;
29328 /* Parse the non-keyword Objective-C params. */
29330 static tree
29331 cp_parser_objc_method_tail_params_opt (cp_parser* parser, bool *ellipsisp,
29332 tree* attributes)
29334 tree params = make_node (TREE_LIST);
29335 cp_token *token = cp_lexer_peek_token (parser->lexer);
29336 *ellipsisp = false; /* Initially, assume no ellipsis. */
29338 while (token->type == CPP_COMMA)
29340 cp_parameter_declarator *parmdecl;
29341 tree parm;
29343 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
29344 token = cp_lexer_peek_token (parser->lexer);
29346 if (token->type == CPP_ELLIPSIS)
29348 cp_lexer_consume_token (parser->lexer); /* Eat '...'. */
29349 *ellipsisp = true;
29350 token = cp_lexer_peek_token (parser->lexer);
29351 break;
29354 /* TODO: parse attributes for tail parameters. */
29355 parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
29356 parm = grokdeclarator (parmdecl->declarator,
29357 &parmdecl->decl_specifiers,
29358 PARM, /*initialized=*/0,
29359 /*attrlist=*/NULL);
29361 chainon (params, build_tree_list (NULL_TREE, parm));
29362 token = cp_lexer_peek_token (parser->lexer);
29365 /* We allow tail attributes for the method. */
29366 if (token->keyword == RID_ATTRIBUTE)
29368 if (*attributes == NULL_TREE)
29370 *attributes = cp_parser_attributes_opt (parser);
29371 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
29372 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
29373 return params;
29375 else
29376 /* We have an error, but parse the attributes, so that we can
29377 carry on. */
29378 *attributes = cp_parser_attributes_opt (parser);
29380 cp_parser_error (parser,
29381 "method attributes must be specified at the end");
29382 return error_mark_node;
29385 return params;
29388 /* Parse a linkage specification, a pragma, an extra semicolon or a block. */
29390 static void
29391 cp_parser_objc_interstitial_code (cp_parser* parser)
29393 cp_token *token = cp_lexer_peek_token (parser->lexer);
29395 /* If the next token is `extern' and the following token is a string
29396 literal, then we have a linkage specification. */
29397 if (token->keyword == RID_EXTERN
29398 && cp_parser_is_pure_string_literal
29399 (cp_lexer_peek_nth_token (parser->lexer, 2)))
29400 cp_parser_linkage_specification (parser);
29401 /* Handle #pragma, if any. */
29402 else if (token->type == CPP_PRAGMA)
29403 cp_parser_pragma (parser, pragma_objc_icode, NULL);
29404 /* Allow stray semicolons. */
29405 else if (token->type == CPP_SEMICOLON)
29406 cp_lexer_consume_token (parser->lexer);
29407 /* Mark methods as optional or required, when building protocols. */
29408 else if (token->keyword == RID_AT_OPTIONAL)
29410 cp_lexer_consume_token (parser->lexer);
29411 objc_set_method_opt (true);
29413 else if (token->keyword == RID_AT_REQUIRED)
29415 cp_lexer_consume_token (parser->lexer);
29416 objc_set_method_opt (false);
29418 else if (token->keyword == RID_NAMESPACE)
29419 cp_parser_namespace_definition (parser);
29420 /* Other stray characters must generate errors. */
29421 else if (token->type == CPP_OPEN_BRACE || token->type == CPP_CLOSE_BRACE)
29423 cp_lexer_consume_token (parser->lexer);
29424 error ("stray %qs between Objective-C++ methods",
29425 token->type == CPP_OPEN_BRACE ? "{" : "}");
29427 /* Finally, try to parse a block-declaration, or a function-definition. */
29428 else
29429 cp_parser_block_declaration (parser, /*statement_p=*/false);
29432 /* Parse a method signature. */
29434 static tree
29435 cp_parser_objc_method_signature (cp_parser* parser, tree* attributes)
29437 tree rettype, kwdparms, optparms;
29438 bool ellipsis = false;
29439 bool is_class_method;
29441 is_class_method = cp_parser_objc_method_type (parser);
29442 rettype = cp_parser_objc_typename (parser);
29443 *attributes = NULL_TREE;
29444 kwdparms = cp_parser_objc_method_keyword_params (parser, attributes);
29445 if (kwdparms == error_mark_node)
29446 return error_mark_node;
29447 optparms = cp_parser_objc_method_tail_params_opt (parser, &ellipsis, attributes);
29448 if (optparms == error_mark_node)
29449 return error_mark_node;
29451 return objc_build_method_signature (is_class_method, rettype, kwdparms, optparms, ellipsis);
29454 static bool
29455 cp_parser_objc_method_maybe_bad_prefix_attributes (cp_parser* parser)
29457 tree tattr;
29458 cp_lexer_save_tokens (parser->lexer);
29459 tattr = cp_parser_attributes_opt (parser);
29460 gcc_assert (tattr) ;
29462 /* If the attributes are followed by a method introducer, this is not allowed.
29463 Dump the attributes and flag the situation. */
29464 if (cp_lexer_next_token_is (parser->lexer, CPP_PLUS)
29465 || cp_lexer_next_token_is (parser->lexer, CPP_MINUS))
29466 return true;
29468 /* Otherwise, the attributes introduce some interstitial code, possibly so
29469 rewind to allow that check. */
29470 cp_lexer_rollback_tokens (parser->lexer);
29471 return false;
29474 /* Parse an Objective-C method prototype list. */
29476 static void
29477 cp_parser_objc_method_prototype_list (cp_parser* parser)
29479 cp_token *token = cp_lexer_peek_token (parser->lexer);
29481 while (token->keyword != RID_AT_END && token->type != CPP_EOF)
29483 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
29485 tree attributes, sig;
29486 bool is_class_method;
29487 if (token->type == CPP_PLUS)
29488 is_class_method = true;
29489 else
29490 is_class_method = false;
29491 sig = cp_parser_objc_method_signature (parser, &attributes);
29492 if (sig == error_mark_node)
29494 cp_parser_skip_to_end_of_block_or_statement (parser);
29495 token = cp_lexer_peek_token (parser->lexer);
29496 continue;
29498 objc_add_method_declaration (is_class_method, sig, attributes);
29499 cp_parser_consume_semicolon_at_end_of_statement (parser);
29501 else if (token->keyword == RID_AT_PROPERTY)
29502 cp_parser_objc_at_property_declaration (parser);
29503 else if (token->keyword == RID_ATTRIBUTE
29504 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
29505 warning_at (cp_lexer_peek_token (parser->lexer)->location,
29506 OPT_Wattributes,
29507 "prefix attributes are ignored for methods");
29508 else
29509 /* Allow for interspersed non-ObjC++ code. */
29510 cp_parser_objc_interstitial_code (parser);
29512 token = cp_lexer_peek_token (parser->lexer);
29515 if (token->type != CPP_EOF)
29516 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
29517 else
29518 cp_parser_error (parser, "expected %<@end%>");
29520 objc_finish_interface ();
29523 /* Parse an Objective-C method definition list. */
29525 static void
29526 cp_parser_objc_method_definition_list (cp_parser* parser)
29528 cp_token *token = cp_lexer_peek_token (parser->lexer);
29530 while (token->keyword != RID_AT_END && token->type != CPP_EOF)
29532 tree meth;
29534 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
29536 cp_token *ptk;
29537 tree sig, attribute;
29538 bool is_class_method;
29539 if (token->type == CPP_PLUS)
29540 is_class_method = true;
29541 else
29542 is_class_method = false;
29543 push_deferring_access_checks (dk_deferred);
29544 sig = cp_parser_objc_method_signature (parser, &attribute);
29545 if (sig == error_mark_node)
29547 cp_parser_skip_to_end_of_block_or_statement (parser);
29548 token = cp_lexer_peek_token (parser->lexer);
29549 continue;
29551 objc_start_method_definition (is_class_method, sig, attribute,
29552 NULL_TREE);
29554 /* For historical reasons, we accept an optional semicolon. */
29555 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
29556 cp_lexer_consume_token (parser->lexer);
29558 ptk = cp_lexer_peek_token (parser->lexer);
29559 if (!(ptk->type == CPP_PLUS || ptk->type == CPP_MINUS
29560 || ptk->type == CPP_EOF || ptk->keyword == RID_AT_END))
29562 perform_deferred_access_checks (tf_warning_or_error);
29563 stop_deferring_access_checks ();
29564 meth = cp_parser_function_definition_after_declarator (parser,
29565 false);
29566 pop_deferring_access_checks ();
29567 objc_finish_method_definition (meth);
29570 /* The following case will be removed once @synthesize is
29571 completely implemented. */
29572 else if (token->keyword == RID_AT_PROPERTY)
29573 cp_parser_objc_at_property_declaration (parser);
29574 else if (token->keyword == RID_AT_SYNTHESIZE)
29575 cp_parser_objc_at_synthesize_declaration (parser);
29576 else if (token->keyword == RID_AT_DYNAMIC)
29577 cp_parser_objc_at_dynamic_declaration (parser);
29578 else if (token->keyword == RID_ATTRIBUTE
29579 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
29580 warning_at (token->location, OPT_Wattributes,
29581 "prefix attributes are ignored for methods");
29582 else
29583 /* Allow for interspersed non-ObjC++ code. */
29584 cp_parser_objc_interstitial_code (parser);
29586 token = cp_lexer_peek_token (parser->lexer);
29589 if (token->type != CPP_EOF)
29590 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
29591 else
29592 cp_parser_error (parser, "expected %<@end%>");
29594 objc_finish_implementation ();
29597 /* Parse Objective-C ivars. */
29599 static void
29600 cp_parser_objc_class_ivars (cp_parser* parser)
29602 cp_token *token = cp_lexer_peek_token (parser->lexer);
29604 if (token->type != CPP_OPEN_BRACE)
29605 return; /* No ivars specified. */
29607 cp_lexer_consume_token (parser->lexer); /* Eat '{'. */
29608 token = cp_lexer_peek_token (parser->lexer);
29610 while (token->type != CPP_CLOSE_BRACE
29611 && token->keyword != RID_AT_END && token->type != CPP_EOF)
29613 cp_decl_specifier_seq declspecs;
29614 int decl_class_or_enum_p;
29615 tree prefix_attributes;
29617 cp_parser_objc_visibility_spec (parser);
29619 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
29620 break;
29622 cp_parser_decl_specifier_seq (parser,
29623 CP_PARSER_FLAGS_OPTIONAL,
29624 &declspecs,
29625 &decl_class_or_enum_p);
29627 /* auto, register, static, extern, mutable. */
29628 if (declspecs.storage_class != sc_none)
29630 cp_parser_error (parser, "invalid type for instance variable");
29631 declspecs.storage_class = sc_none;
29634 /* thread_local. */
29635 if (decl_spec_seq_has_spec_p (&declspecs, ds_thread))
29637 cp_parser_error (parser, "invalid type for instance variable");
29638 declspecs.locations[ds_thread] = 0;
29641 /* typedef. */
29642 if (decl_spec_seq_has_spec_p (&declspecs, ds_typedef))
29644 cp_parser_error (parser, "invalid type for instance variable");
29645 declspecs.locations[ds_typedef] = 0;
29648 prefix_attributes = declspecs.attributes;
29649 declspecs.attributes = NULL_TREE;
29651 /* Keep going until we hit the `;' at the end of the
29652 declaration. */
29653 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
29655 tree width = NULL_TREE, attributes, first_attribute, decl;
29656 cp_declarator *declarator = NULL;
29657 int ctor_dtor_or_conv_p;
29659 /* Check for a (possibly unnamed) bitfield declaration. */
29660 token = cp_lexer_peek_token (parser->lexer);
29661 if (token->type == CPP_COLON)
29662 goto eat_colon;
29664 if (token->type == CPP_NAME
29665 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
29666 == CPP_COLON))
29668 /* Get the name of the bitfield. */
29669 declarator = make_id_declarator (NULL_TREE,
29670 cp_parser_identifier (parser),
29671 sfk_none);
29673 eat_colon:
29674 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
29675 /* Get the width of the bitfield. */
29676 width
29677 = cp_parser_constant_expression (parser);
29679 else
29681 /* Parse the declarator. */
29682 declarator
29683 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
29684 &ctor_dtor_or_conv_p,
29685 /*parenthesized_p=*/NULL,
29686 /*member_p=*/false,
29687 /*friend_p=*/false);
29690 /* Look for attributes that apply to the ivar. */
29691 attributes = cp_parser_attributes_opt (parser);
29692 /* Remember which attributes are prefix attributes and
29693 which are not. */
29694 first_attribute = attributes;
29695 /* Combine the attributes. */
29696 attributes = chainon (prefix_attributes, attributes);
29698 if (width)
29699 /* Create the bitfield declaration. */
29700 decl = grokbitfield (declarator, &declspecs,
29701 width,
29702 attributes);
29703 else
29704 decl = grokfield (declarator, &declspecs,
29705 NULL_TREE, /*init_const_expr_p=*/false,
29706 NULL_TREE, attributes);
29708 /* Add the instance variable. */
29709 if (decl != error_mark_node && decl != NULL_TREE)
29710 objc_add_instance_variable (decl);
29712 /* Reset PREFIX_ATTRIBUTES. */
29713 while (attributes && TREE_CHAIN (attributes) != first_attribute)
29714 attributes = TREE_CHAIN (attributes);
29715 if (attributes)
29716 TREE_CHAIN (attributes) = NULL_TREE;
29718 token = cp_lexer_peek_token (parser->lexer);
29720 if (token->type == CPP_COMMA)
29722 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
29723 continue;
29725 break;
29728 cp_parser_consume_semicolon_at_end_of_statement (parser);
29729 token = cp_lexer_peek_token (parser->lexer);
29732 if (token->keyword == RID_AT_END)
29733 cp_parser_error (parser, "expected %<}%>");
29735 /* Do not consume the RID_AT_END, so it will be read again as terminating
29736 the @interface of @implementation. */
29737 if (token->keyword != RID_AT_END && token->type != CPP_EOF)
29738 cp_lexer_consume_token (parser->lexer); /* Eat '}'. */
29740 /* For historical reasons, we accept an optional semicolon. */
29741 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
29742 cp_lexer_consume_token (parser->lexer);
29745 /* Parse an Objective-C protocol declaration. */
29747 static void
29748 cp_parser_objc_protocol_declaration (cp_parser* parser, tree attributes)
29750 tree proto, protorefs;
29751 cp_token *tok;
29753 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
29754 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
29756 tok = cp_lexer_peek_token (parser->lexer);
29757 error_at (tok->location, "identifier expected after %<@protocol%>");
29758 cp_parser_consume_semicolon_at_end_of_statement (parser);
29759 return;
29762 /* See if we have a forward declaration or a definition. */
29763 tok = cp_lexer_peek_nth_token (parser->lexer, 2);
29765 /* Try a forward declaration first. */
29766 if (tok->type == CPP_COMMA || tok->type == CPP_SEMICOLON)
29768 while (true)
29770 tree id;
29772 id = cp_parser_identifier (parser);
29773 if (id == error_mark_node)
29774 break;
29776 objc_declare_protocol (id, attributes);
29778 if(cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
29779 cp_lexer_consume_token (parser->lexer);
29780 else
29781 break;
29783 cp_parser_consume_semicolon_at_end_of_statement (parser);
29786 /* Ok, we got a full-fledged definition (or at least should). */
29787 else
29789 proto = cp_parser_identifier (parser);
29790 protorefs = cp_parser_objc_protocol_refs_opt (parser);
29791 objc_start_protocol (proto, protorefs, attributes);
29792 cp_parser_objc_method_prototype_list (parser);
29796 /* Parse an Objective-C superclass or category. */
29798 static void
29799 cp_parser_objc_superclass_or_category (cp_parser *parser,
29800 bool iface_p,
29801 tree *super,
29802 tree *categ, bool *is_class_extension)
29804 cp_token *next = cp_lexer_peek_token (parser->lexer);
29806 *super = *categ = NULL_TREE;
29807 *is_class_extension = false;
29808 if (next->type == CPP_COLON)
29810 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
29811 *super = cp_parser_identifier (parser);
29813 else if (next->type == CPP_OPEN_PAREN)
29815 cp_lexer_consume_token (parser->lexer); /* Eat '('. */
29817 /* If there is no category name, and this is an @interface, we
29818 have a class extension. */
29819 if (iface_p && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
29821 *categ = NULL_TREE;
29822 *is_class_extension = true;
29824 else
29825 *categ = cp_parser_identifier (parser);
29827 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
29831 /* Parse an Objective-C class interface. */
29833 static void
29834 cp_parser_objc_class_interface (cp_parser* parser, tree attributes)
29836 tree name, super, categ, protos;
29837 bool is_class_extension;
29839 cp_lexer_consume_token (parser->lexer); /* Eat '@interface'. */
29840 name = cp_parser_identifier (parser);
29841 if (name == error_mark_node)
29843 /* It's hard to recover because even if valid @interface stuff
29844 is to follow, we can't compile it (or validate it) if we
29845 don't even know which class it refers to. Let's assume this
29846 was a stray '@interface' token in the stream and skip it.
29848 return;
29850 cp_parser_objc_superclass_or_category (parser, true, &super, &categ,
29851 &is_class_extension);
29852 protos = cp_parser_objc_protocol_refs_opt (parser);
29854 /* We have either a class or a category on our hands. */
29855 if (categ || is_class_extension)
29856 objc_start_category_interface (name, categ, protos, attributes);
29857 else
29859 objc_start_class_interface (name, super, protos, attributes);
29860 /* Handle instance variable declarations, if any. */
29861 cp_parser_objc_class_ivars (parser);
29862 objc_continue_interface ();
29865 cp_parser_objc_method_prototype_list (parser);
29868 /* Parse an Objective-C class implementation. */
29870 static void
29871 cp_parser_objc_class_implementation (cp_parser* parser)
29873 tree name, super, categ;
29874 bool is_class_extension;
29876 cp_lexer_consume_token (parser->lexer); /* Eat '@implementation'. */
29877 name = cp_parser_identifier (parser);
29878 if (name == error_mark_node)
29880 /* It's hard to recover because even if valid @implementation
29881 stuff is to follow, we can't compile it (or validate it) if
29882 we don't even know which class it refers to. Let's assume
29883 this was a stray '@implementation' token in the stream and
29884 skip it.
29886 return;
29888 cp_parser_objc_superclass_or_category (parser, false, &super, &categ,
29889 &is_class_extension);
29891 /* We have either a class or a category on our hands. */
29892 if (categ)
29893 objc_start_category_implementation (name, categ);
29894 else
29896 objc_start_class_implementation (name, super);
29897 /* Handle instance variable declarations, if any. */
29898 cp_parser_objc_class_ivars (parser);
29899 objc_continue_implementation ();
29902 cp_parser_objc_method_definition_list (parser);
29905 /* Consume the @end token and finish off the implementation. */
29907 static void
29908 cp_parser_objc_end_implementation (cp_parser* parser)
29910 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
29911 objc_finish_implementation ();
29914 /* Parse an Objective-C declaration. */
29916 static void
29917 cp_parser_objc_declaration (cp_parser* parser, tree attributes)
29919 /* Try to figure out what kind of declaration is present. */
29920 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
29922 if (attributes)
29923 switch (kwd->keyword)
29925 case RID_AT_ALIAS:
29926 case RID_AT_CLASS:
29927 case RID_AT_END:
29928 error_at (kwd->location, "attributes may not be specified before"
29929 " the %<@%D%> Objective-C++ keyword",
29930 kwd->u.value);
29931 attributes = NULL;
29932 break;
29933 case RID_AT_IMPLEMENTATION:
29934 warning_at (kwd->location, OPT_Wattributes,
29935 "prefix attributes are ignored before %<@%D%>",
29936 kwd->u.value);
29937 attributes = NULL;
29938 default:
29939 break;
29942 switch (kwd->keyword)
29944 case RID_AT_ALIAS:
29945 cp_parser_objc_alias_declaration (parser);
29946 break;
29947 case RID_AT_CLASS:
29948 cp_parser_objc_class_declaration (parser);
29949 break;
29950 case RID_AT_PROTOCOL:
29951 cp_parser_objc_protocol_declaration (parser, attributes);
29952 break;
29953 case RID_AT_INTERFACE:
29954 cp_parser_objc_class_interface (parser, attributes);
29955 break;
29956 case RID_AT_IMPLEMENTATION:
29957 cp_parser_objc_class_implementation (parser);
29958 break;
29959 case RID_AT_END:
29960 cp_parser_objc_end_implementation (parser);
29961 break;
29962 default:
29963 error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
29964 kwd->u.value);
29965 cp_parser_skip_to_end_of_block_or_statement (parser);
29969 /* Parse an Objective-C try-catch-finally statement.
29971 objc-try-catch-finally-stmt:
29972 @try compound-statement objc-catch-clause-seq [opt]
29973 objc-finally-clause [opt]
29975 objc-catch-clause-seq:
29976 objc-catch-clause objc-catch-clause-seq [opt]
29978 objc-catch-clause:
29979 @catch ( objc-exception-declaration ) compound-statement
29981 objc-finally-clause:
29982 @finally compound-statement
29984 objc-exception-declaration:
29985 parameter-declaration
29986 '...'
29988 where '...' is to be interpreted literally, that is, it means CPP_ELLIPSIS.
29990 Returns NULL_TREE.
29992 PS: This function is identical to c_parser_objc_try_catch_finally_statement
29993 for C. Keep them in sync. */
29995 static tree
29996 cp_parser_objc_try_catch_finally_statement (cp_parser *parser)
29998 location_t location;
29999 tree stmt;
30001 cp_parser_require_keyword (parser, RID_AT_TRY, RT_AT_TRY);
30002 location = cp_lexer_peek_token (parser->lexer)->location;
30003 objc_maybe_warn_exceptions (location);
30004 /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
30005 node, lest it get absorbed into the surrounding block. */
30006 stmt = push_stmt_list ();
30007 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
30008 objc_begin_try_stmt (location, pop_stmt_list (stmt));
30010 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_CATCH))
30012 cp_parameter_declarator *parm;
30013 tree parameter_declaration = error_mark_node;
30014 bool seen_open_paren = false;
30016 cp_lexer_consume_token (parser->lexer);
30017 if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
30018 seen_open_paren = true;
30019 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
30021 /* We have "@catch (...)" (where the '...' are literally
30022 what is in the code). Skip the '...'.
30023 parameter_declaration is set to NULL_TREE, and
30024 objc_being_catch_clauses() knows that that means
30025 '...'. */
30026 cp_lexer_consume_token (parser->lexer);
30027 parameter_declaration = NULL_TREE;
30029 else
30031 /* We have "@catch (NSException *exception)" or something
30032 like that. Parse the parameter declaration. */
30033 parm = cp_parser_parameter_declaration (parser, false, NULL);
30034 if (parm == NULL)
30035 parameter_declaration = error_mark_node;
30036 else
30037 parameter_declaration = grokdeclarator (parm->declarator,
30038 &parm->decl_specifiers,
30039 PARM, /*initialized=*/0,
30040 /*attrlist=*/NULL);
30042 if (seen_open_paren)
30043 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
30044 else
30046 /* If there was no open parenthesis, we are recovering from
30047 an error, and we are trying to figure out what mistake
30048 the user has made. */
30050 /* If there is an immediate closing parenthesis, the user
30051 probably forgot the opening one (ie, they typed "@catch
30052 NSException *e)". Parse the closing parenthesis and keep
30053 going. */
30054 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
30055 cp_lexer_consume_token (parser->lexer);
30057 /* If these is no immediate closing parenthesis, the user
30058 probably doesn't know that parenthesis are required at
30059 all (ie, they typed "@catch NSException *e"). So, just
30060 forget about the closing parenthesis and keep going. */
30062 objc_begin_catch_clause (parameter_declaration);
30063 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
30064 objc_finish_catch_clause ();
30066 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_FINALLY))
30068 cp_lexer_consume_token (parser->lexer);
30069 location = cp_lexer_peek_token (parser->lexer)->location;
30070 /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
30071 node, lest it get absorbed into the surrounding block. */
30072 stmt = push_stmt_list ();
30073 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
30074 objc_build_finally_clause (location, pop_stmt_list (stmt));
30077 return objc_finish_try_stmt ();
30080 /* Parse an Objective-C synchronized statement.
30082 objc-synchronized-stmt:
30083 @synchronized ( expression ) compound-statement
30085 Returns NULL_TREE. */
30087 static tree
30088 cp_parser_objc_synchronized_statement (cp_parser *parser)
30090 location_t location;
30091 tree lock, stmt;
30093 cp_parser_require_keyword (parser, RID_AT_SYNCHRONIZED, RT_AT_SYNCHRONIZED);
30095 location = cp_lexer_peek_token (parser->lexer)->location;
30096 objc_maybe_warn_exceptions (location);
30097 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
30098 lock = cp_parser_expression (parser);
30099 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
30101 /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
30102 node, lest it get absorbed into the surrounding block. */
30103 stmt = push_stmt_list ();
30104 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
30106 return objc_build_synchronized (location, lock, pop_stmt_list (stmt));
30109 /* Parse an Objective-C throw statement.
30111 objc-throw-stmt:
30112 @throw assignment-expression [opt] ;
30114 Returns a constructed '@throw' statement. */
30116 static tree
30117 cp_parser_objc_throw_statement (cp_parser *parser)
30119 tree expr = NULL_TREE;
30120 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
30122 cp_parser_require_keyword (parser, RID_AT_THROW, RT_AT_THROW);
30124 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
30125 expr = cp_parser_expression (parser);
30127 cp_parser_consume_semicolon_at_end_of_statement (parser);
30129 return objc_build_throw_stmt (loc, expr);
30132 /* Parse an Objective-C statement. */
30134 static tree
30135 cp_parser_objc_statement (cp_parser * parser)
30137 /* Try to figure out what kind of declaration is present. */
30138 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
30140 switch (kwd->keyword)
30142 case RID_AT_TRY:
30143 return cp_parser_objc_try_catch_finally_statement (parser);
30144 case RID_AT_SYNCHRONIZED:
30145 return cp_parser_objc_synchronized_statement (parser);
30146 case RID_AT_THROW:
30147 return cp_parser_objc_throw_statement (parser);
30148 default:
30149 error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
30150 kwd->u.value);
30151 cp_parser_skip_to_end_of_block_or_statement (parser);
30154 return error_mark_node;
30157 /* If we are compiling ObjC++ and we see an __attribute__ we neeed to
30158 look ahead to see if an objc keyword follows the attributes. This
30159 is to detect the use of prefix attributes on ObjC @interface and
30160 @protocol. */
30162 static bool
30163 cp_parser_objc_valid_prefix_attributes (cp_parser* parser, tree *attrib)
30165 cp_lexer_save_tokens (parser->lexer);
30166 *attrib = cp_parser_attributes_opt (parser);
30167 gcc_assert (*attrib);
30168 if (OBJC_IS_AT_KEYWORD (cp_lexer_peek_token (parser->lexer)->keyword))
30170 cp_lexer_commit_tokens (parser->lexer);
30171 return true;
30173 cp_lexer_rollback_tokens (parser->lexer);
30174 return false;
30177 /* This routine is a minimal replacement for
30178 c_parser_struct_declaration () used when parsing the list of
30179 types/names or ObjC++ properties. For example, when parsing the
30180 code
30182 @property (readonly) int a, b, c;
30184 this function is responsible for parsing "int a, int b, int c" and
30185 returning the declarations as CHAIN of DECLs.
30187 TODO: Share this code with cp_parser_objc_class_ivars. It's very
30188 similar parsing. */
30189 static tree
30190 cp_parser_objc_struct_declaration (cp_parser *parser)
30192 tree decls = NULL_TREE;
30193 cp_decl_specifier_seq declspecs;
30194 int decl_class_or_enum_p;
30195 tree prefix_attributes;
30197 cp_parser_decl_specifier_seq (parser,
30198 CP_PARSER_FLAGS_NONE,
30199 &declspecs,
30200 &decl_class_or_enum_p);
30202 if (declspecs.type == error_mark_node)
30203 return error_mark_node;
30205 /* auto, register, static, extern, mutable. */
30206 if (declspecs.storage_class != sc_none)
30208 cp_parser_error (parser, "invalid type for property");
30209 declspecs.storage_class = sc_none;
30212 /* thread_local. */
30213 if (decl_spec_seq_has_spec_p (&declspecs, ds_thread))
30215 cp_parser_error (parser, "invalid type for property");
30216 declspecs.locations[ds_thread] = 0;
30219 /* typedef. */
30220 if (decl_spec_seq_has_spec_p (&declspecs, ds_typedef))
30222 cp_parser_error (parser, "invalid type for property");
30223 declspecs.locations[ds_typedef] = 0;
30226 prefix_attributes = declspecs.attributes;
30227 declspecs.attributes = NULL_TREE;
30229 /* Keep going until we hit the `;' at the end of the declaration. */
30230 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
30232 tree attributes, first_attribute, decl;
30233 cp_declarator *declarator;
30234 cp_token *token;
30236 /* Parse the declarator. */
30237 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
30238 NULL, NULL, false, false);
30240 /* Look for attributes that apply to the ivar. */
30241 attributes = cp_parser_attributes_opt (parser);
30242 /* Remember which attributes are prefix attributes and
30243 which are not. */
30244 first_attribute = attributes;
30245 /* Combine the attributes. */
30246 attributes = chainon (prefix_attributes, attributes);
30248 decl = grokfield (declarator, &declspecs,
30249 NULL_TREE, /*init_const_expr_p=*/false,
30250 NULL_TREE, attributes);
30252 if (decl == error_mark_node || decl == NULL_TREE)
30253 return error_mark_node;
30255 /* Reset PREFIX_ATTRIBUTES. */
30256 while (attributes && TREE_CHAIN (attributes) != first_attribute)
30257 attributes = TREE_CHAIN (attributes);
30258 if (attributes)
30259 TREE_CHAIN (attributes) = NULL_TREE;
30261 DECL_CHAIN (decl) = decls;
30262 decls = decl;
30264 token = cp_lexer_peek_token (parser->lexer);
30265 if (token->type == CPP_COMMA)
30267 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
30268 continue;
30270 else
30271 break;
30273 return decls;
30276 /* Parse an Objective-C @property declaration. The syntax is:
30278 objc-property-declaration:
30279 '@property' objc-property-attributes[opt] struct-declaration ;
30281 objc-property-attributes:
30282 '(' objc-property-attribute-list ')'
30284 objc-property-attribute-list:
30285 objc-property-attribute
30286 objc-property-attribute-list, objc-property-attribute
30288 objc-property-attribute
30289 'getter' = identifier
30290 'setter' = identifier
30291 'readonly'
30292 'readwrite'
30293 'assign'
30294 'retain'
30295 'copy'
30296 'nonatomic'
30298 For example:
30299 @property NSString *name;
30300 @property (readonly) id object;
30301 @property (retain, nonatomic, getter=getTheName) id name;
30302 @property int a, b, c;
30304 PS: This function is identical to
30305 c_parser_objc_at_property_declaration for C. Keep them in sync. */
30306 static void
30307 cp_parser_objc_at_property_declaration (cp_parser *parser)
30309 /* The following variables hold the attributes of the properties as
30310 parsed. They are 'false' or 'NULL_TREE' if the attribute was not
30311 seen. When we see an attribute, we set them to 'true' (if they
30312 are boolean properties) or to the identifier (if they have an
30313 argument, ie, for getter and setter). Note that here we only
30314 parse the list of attributes, check the syntax and accumulate the
30315 attributes that we find. objc_add_property_declaration() will
30316 then process the information. */
30317 bool property_assign = false;
30318 bool property_copy = false;
30319 tree property_getter_ident = NULL_TREE;
30320 bool property_nonatomic = false;
30321 bool property_readonly = false;
30322 bool property_readwrite = false;
30323 bool property_retain = false;
30324 tree property_setter_ident = NULL_TREE;
30326 /* 'properties' is the list of properties that we read. Usually a
30327 single one, but maybe more (eg, in "@property int a, b, c;" there
30328 are three). */
30329 tree properties;
30330 location_t loc;
30332 loc = cp_lexer_peek_token (parser->lexer)->location;
30334 cp_lexer_consume_token (parser->lexer); /* Eat '@property'. */
30336 /* Parse the optional attribute list... */
30337 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
30339 /* Eat the '('. */
30340 cp_lexer_consume_token (parser->lexer);
30342 while (true)
30344 bool syntax_error = false;
30345 cp_token *token = cp_lexer_peek_token (parser->lexer);
30346 enum rid keyword;
30348 if (token->type != CPP_NAME)
30350 cp_parser_error (parser, "expected identifier");
30351 break;
30353 keyword = C_RID_CODE (token->u.value);
30354 cp_lexer_consume_token (parser->lexer);
30355 switch (keyword)
30357 case RID_ASSIGN: property_assign = true; break;
30358 case RID_COPY: property_copy = true; break;
30359 case RID_NONATOMIC: property_nonatomic = true; break;
30360 case RID_READONLY: property_readonly = true; break;
30361 case RID_READWRITE: property_readwrite = true; break;
30362 case RID_RETAIN: property_retain = true; break;
30364 case RID_GETTER:
30365 case RID_SETTER:
30366 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
30368 if (keyword == RID_GETTER)
30369 cp_parser_error (parser,
30370 "missing %<=%> (after %<getter%> attribute)");
30371 else
30372 cp_parser_error (parser,
30373 "missing %<=%> (after %<setter%> attribute)");
30374 syntax_error = true;
30375 break;
30377 cp_lexer_consume_token (parser->lexer); /* eat the = */
30378 if (!cp_parser_objc_selector_p (cp_lexer_peek_token (parser->lexer)->type))
30380 cp_parser_error (parser, "expected identifier");
30381 syntax_error = true;
30382 break;
30384 if (keyword == RID_SETTER)
30386 if (property_setter_ident != NULL_TREE)
30388 cp_parser_error (parser, "the %<setter%> attribute may only be specified once");
30389 cp_lexer_consume_token (parser->lexer);
30391 else
30392 property_setter_ident = cp_parser_objc_selector (parser);
30393 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
30394 cp_parser_error (parser, "setter name must terminate with %<:%>");
30395 else
30396 cp_lexer_consume_token (parser->lexer);
30398 else
30400 if (property_getter_ident != NULL_TREE)
30402 cp_parser_error (parser, "the %<getter%> attribute may only be specified once");
30403 cp_lexer_consume_token (parser->lexer);
30405 else
30406 property_getter_ident = cp_parser_objc_selector (parser);
30408 break;
30409 default:
30410 cp_parser_error (parser, "unknown property attribute");
30411 syntax_error = true;
30412 break;
30415 if (syntax_error)
30416 break;
30418 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
30419 cp_lexer_consume_token (parser->lexer);
30420 else
30421 break;
30424 /* FIXME: "@property (setter, assign);" will generate a spurious
30425 "error: expected ‘)’ before ‘,’ token". This is because
30426 cp_parser_require, unlike the C counterpart, will produce an
30427 error even if we are in error recovery. */
30428 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
30430 cp_parser_skip_to_closing_parenthesis (parser,
30431 /*recovering=*/true,
30432 /*or_comma=*/false,
30433 /*consume_paren=*/true);
30437 /* ... and the property declaration(s). */
30438 properties = cp_parser_objc_struct_declaration (parser);
30440 if (properties == error_mark_node)
30442 cp_parser_skip_to_end_of_statement (parser);
30443 /* If the next token is now a `;', consume it. */
30444 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
30445 cp_lexer_consume_token (parser->lexer);
30446 return;
30449 if (properties == NULL_TREE)
30450 cp_parser_error (parser, "expected identifier");
30451 else
30453 /* Comma-separated properties are chained together in
30454 reverse order; add them one by one. */
30455 properties = nreverse (properties);
30457 for (; properties; properties = TREE_CHAIN (properties))
30458 objc_add_property_declaration (loc, copy_node (properties),
30459 property_readonly, property_readwrite,
30460 property_assign, property_retain,
30461 property_copy, property_nonatomic,
30462 property_getter_ident, property_setter_ident);
30465 cp_parser_consume_semicolon_at_end_of_statement (parser);
30468 /* Parse an Objective-C++ @synthesize declaration. The syntax is:
30470 objc-synthesize-declaration:
30471 @synthesize objc-synthesize-identifier-list ;
30473 objc-synthesize-identifier-list:
30474 objc-synthesize-identifier
30475 objc-synthesize-identifier-list, objc-synthesize-identifier
30477 objc-synthesize-identifier
30478 identifier
30479 identifier = identifier
30481 For example:
30482 @synthesize MyProperty;
30483 @synthesize OneProperty, AnotherProperty=MyIvar, YetAnotherProperty;
30485 PS: This function is identical to c_parser_objc_at_synthesize_declaration
30486 for C. Keep them in sync.
30488 static void
30489 cp_parser_objc_at_synthesize_declaration (cp_parser *parser)
30491 tree list = NULL_TREE;
30492 location_t loc;
30493 loc = cp_lexer_peek_token (parser->lexer)->location;
30495 cp_lexer_consume_token (parser->lexer); /* Eat '@synthesize'. */
30496 while (true)
30498 tree property, ivar;
30499 property = cp_parser_identifier (parser);
30500 if (property == error_mark_node)
30502 cp_parser_consume_semicolon_at_end_of_statement (parser);
30503 return;
30505 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
30507 cp_lexer_consume_token (parser->lexer);
30508 ivar = cp_parser_identifier (parser);
30509 if (ivar == error_mark_node)
30511 cp_parser_consume_semicolon_at_end_of_statement (parser);
30512 return;
30515 else
30516 ivar = NULL_TREE;
30517 list = chainon (list, build_tree_list (ivar, property));
30518 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
30519 cp_lexer_consume_token (parser->lexer);
30520 else
30521 break;
30523 cp_parser_consume_semicolon_at_end_of_statement (parser);
30524 objc_add_synthesize_declaration (loc, list);
30527 /* Parse an Objective-C++ @dynamic declaration. The syntax is:
30529 objc-dynamic-declaration:
30530 @dynamic identifier-list ;
30532 For example:
30533 @dynamic MyProperty;
30534 @dynamic MyProperty, AnotherProperty;
30536 PS: This function is identical to c_parser_objc_at_dynamic_declaration
30537 for C. Keep them in sync.
30539 static void
30540 cp_parser_objc_at_dynamic_declaration (cp_parser *parser)
30542 tree list = NULL_TREE;
30543 location_t loc;
30544 loc = cp_lexer_peek_token (parser->lexer)->location;
30546 cp_lexer_consume_token (parser->lexer); /* Eat '@dynamic'. */
30547 while (true)
30549 tree property;
30550 property = cp_parser_identifier (parser);
30551 if (property == error_mark_node)
30553 cp_parser_consume_semicolon_at_end_of_statement (parser);
30554 return;
30556 list = chainon (list, build_tree_list (NULL, property));
30557 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
30558 cp_lexer_consume_token (parser->lexer);
30559 else
30560 break;
30562 cp_parser_consume_semicolon_at_end_of_statement (parser);
30563 objc_add_dynamic_declaration (loc, list);
30567 /* OpenMP 2.5 / 3.0 / 3.1 / 4.0 parsing routines. */
30569 /* Returns name of the next clause.
30570 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
30571 the token is not consumed. Otherwise appropriate pragma_omp_clause is
30572 returned and the token is consumed. */
30574 static pragma_omp_clause
30575 cp_parser_omp_clause_name (cp_parser *parser)
30577 pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
30579 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
30580 result = PRAGMA_OACC_CLAUSE_AUTO;
30581 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
30582 result = PRAGMA_OMP_CLAUSE_IF;
30583 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT))
30584 result = PRAGMA_OMP_CLAUSE_DEFAULT;
30585 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DELETE))
30586 result = PRAGMA_OACC_CLAUSE_DELETE;
30587 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_PRIVATE))
30588 result = PRAGMA_OMP_CLAUSE_PRIVATE;
30589 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
30590 result = PRAGMA_OMP_CLAUSE_FOR;
30591 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
30593 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
30594 const char *p = IDENTIFIER_POINTER (id);
30596 switch (p[0])
30598 case 'a':
30599 if (!strcmp ("aligned", p))
30600 result = PRAGMA_OMP_CLAUSE_ALIGNED;
30601 else if (!strcmp ("async", p))
30602 result = PRAGMA_OACC_CLAUSE_ASYNC;
30603 break;
30604 case 'c':
30605 if (!strcmp ("collapse", p))
30606 result = PRAGMA_OMP_CLAUSE_COLLAPSE;
30607 else if (!strcmp ("copy", p))
30608 result = PRAGMA_OACC_CLAUSE_COPY;
30609 else if (!strcmp ("copyin", p))
30610 result = PRAGMA_OMP_CLAUSE_COPYIN;
30611 else if (!strcmp ("copyout", p))
30612 result = PRAGMA_OACC_CLAUSE_COPYOUT;
30613 else if (!strcmp ("copyprivate", p))
30614 result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
30615 else if (!strcmp ("create", p))
30616 result = PRAGMA_OACC_CLAUSE_CREATE;
30617 break;
30618 case 'd':
30619 if (!strcmp ("defaultmap", p))
30620 result = PRAGMA_OMP_CLAUSE_DEFAULTMAP;
30621 else if (!strcmp ("depend", p))
30622 result = PRAGMA_OMP_CLAUSE_DEPEND;
30623 else if (!strcmp ("device", p))
30624 result = PRAGMA_OMP_CLAUSE_DEVICE;
30625 else if (!strcmp ("deviceptr", p))
30626 result = PRAGMA_OACC_CLAUSE_DEVICEPTR;
30627 else if (!strcmp ("device_resident", p))
30628 result = PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT;
30629 else if (!strcmp ("dist_schedule", p))
30630 result = PRAGMA_OMP_CLAUSE_DIST_SCHEDULE;
30631 break;
30632 case 'f':
30633 if (!strcmp ("final", p))
30634 result = PRAGMA_OMP_CLAUSE_FINAL;
30635 else if (!strcmp ("firstprivate", p))
30636 result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
30637 else if (!strcmp ("from", p))
30638 result = PRAGMA_OMP_CLAUSE_FROM;
30639 break;
30640 case 'g':
30641 if (!strcmp ("gang", p))
30642 result = PRAGMA_OACC_CLAUSE_GANG;
30643 else if (!strcmp ("grainsize", p))
30644 result = PRAGMA_OMP_CLAUSE_GRAINSIZE;
30645 break;
30646 case 'h':
30647 if (!strcmp ("hint", p))
30648 result = PRAGMA_OMP_CLAUSE_HINT;
30649 else if (!strcmp ("host", p))
30650 result = PRAGMA_OACC_CLAUSE_HOST;
30651 break;
30652 case 'i':
30653 if (!strcmp ("inbranch", p))
30654 result = PRAGMA_OMP_CLAUSE_INBRANCH;
30655 else if (!strcmp ("independent", p))
30656 result = PRAGMA_OACC_CLAUSE_INDEPENDENT;
30657 else if (!strcmp ("is_device_ptr", p))
30658 result = PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR;
30659 break;
30660 case 'l':
30661 if (!strcmp ("lastprivate", p))
30662 result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
30663 else if (!strcmp ("linear", p))
30664 result = PRAGMA_OMP_CLAUSE_LINEAR;
30665 else if (!strcmp ("link", p))
30666 result = PRAGMA_OMP_CLAUSE_LINK;
30667 break;
30668 case 'm':
30669 if (!strcmp ("map", p))
30670 result = PRAGMA_OMP_CLAUSE_MAP;
30671 else if (!strcmp ("mergeable", p))
30672 result = PRAGMA_OMP_CLAUSE_MERGEABLE;
30673 else if (flag_cilkplus && !strcmp ("mask", p))
30674 result = PRAGMA_CILK_CLAUSE_MASK;
30675 break;
30676 case 'n':
30677 if (!strcmp ("nogroup", p))
30678 result = PRAGMA_OMP_CLAUSE_NOGROUP;
30679 else if (!strcmp ("notinbranch", p))
30680 result = PRAGMA_OMP_CLAUSE_NOTINBRANCH;
30681 else if (!strcmp ("nowait", p))
30682 result = PRAGMA_OMP_CLAUSE_NOWAIT;
30683 else if (flag_cilkplus && !strcmp ("nomask", p))
30684 result = PRAGMA_CILK_CLAUSE_NOMASK;
30685 else if (!strcmp ("num_gangs", p))
30686 result = PRAGMA_OACC_CLAUSE_NUM_GANGS;
30687 else if (!strcmp ("num_tasks", p))
30688 result = PRAGMA_OMP_CLAUSE_NUM_TASKS;
30689 else if (!strcmp ("num_teams", p))
30690 result = PRAGMA_OMP_CLAUSE_NUM_TEAMS;
30691 else if (!strcmp ("num_threads", p))
30692 result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
30693 else if (!strcmp ("num_workers", p))
30694 result = PRAGMA_OACC_CLAUSE_NUM_WORKERS;
30695 break;
30696 case 'o':
30697 if (!strcmp ("ordered", p))
30698 result = PRAGMA_OMP_CLAUSE_ORDERED;
30699 break;
30700 case 'p':
30701 if (!strcmp ("parallel", p))
30702 result = PRAGMA_OMP_CLAUSE_PARALLEL;
30703 else if (!strcmp ("present", p))
30704 result = PRAGMA_OACC_CLAUSE_PRESENT;
30705 else if (!strcmp ("present_or_copy", p)
30706 || !strcmp ("pcopy", p))
30707 result = PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY;
30708 else if (!strcmp ("present_or_copyin", p)
30709 || !strcmp ("pcopyin", p))
30710 result = PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN;
30711 else if (!strcmp ("present_or_copyout", p)
30712 || !strcmp ("pcopyout", p))
30713 result = PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT;
30714 else if (!strcmp ("present_or_create", p)
30715 || !strcmp ("pcreate", p))
30716 result = PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE;
30717 else if (!strcmp ("priority", p))
30718 result = PRAGMA_OMP_CLAUSE_PRIORITY;
30719 else if (!strcmp ("proc_bind", p))
30720 result = PRAGMA_OMP_CLAUSE_PROC_BIND;
30721 break;
30722 case 'r':
30723 if (!strcmp ("reduction", p))
30724 result = PRAGMA_OMP_CLAUSE_REDUCTION;
30725 break;
30726 case 's':
30727 if (!strcmp ("safelen", p))
30728 result = PRAGMA_OMP_CLAUSE_SAFELEN;
30729 else if (!strcmp ("schedule", p))
30730 result = PRAGMA_OMP_CLAUSE_SCHEDULE;
30731 else if (!strcmp ("sections", p))
30732 result = PRAGMA_OMP_CLAUSE_SECTIONS;
30733 else if (!strcmp ("self", p))
30734 result = PRAGMA_OACC_CLAUSE_SELF;
30735 else if (!strcmp ("seq", p))
30736 result = PRAGMA_OACC_CLAUSE_SEQ;
30737 else if (!strcmp ("shared", p))
30738 result = PRAGMA_OMP_CLAUSE_SHARED;
30739 else if (!strcmp ("simd", p))
30740 result = PRAGMA_OMP_CLAUSE_SIMD;
30741 else if (!strcmp ("simdlen", p))
30742 result = PRAGMA_OMP_CLAUSE_SIMDLEN;
30743 break;
30744 case 't':
30745 if (!strcmp ("taskgroup", p))
30746 result = PRAGMA_OMP_CLAUSE_TASKGROUP;
30747 else if (!strcmp ("thread_limit", p))
30748 result = PRAGMA_OMP_CLAUSE_THREAD_LIMIT;
30749 else if (!strcmp ("threads", p))
30750 result = PRAGMA_OMP_CLAUSE_THREADS;
30751 else if (!strcmp ("tile", p))
30752 result = PRAGMA_OACC_CLAUSE_TILE;
30753 else if (!strcmp ("to", p))
30754 result = PRAGMA_OMP_CLAUSE_TO;
30755 break;
30756 case 'u':
30757 if (!strcmp ("uniform", p))
30758 result = PRAGMA_OMP_CLAUSE_UNIFORM;
30759 else if (!strcmp ("untied", p))
30760 result = PRAGMA_OMP_CLAUSE_UNTIED;
30761 else if (!strcmp ("use_device", p))
30762 result = PRAGMA_OACC_CLAUSE_USE_DEVICE;
30763 else if (!strcmp ("use_device_ptr", p))
30764 result = PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR;
30765 break;
30766 case 'v':
30767 if (!strcmp ("vector", p))
30768 result = PRAGMA_OACC_CLAUSE_VECTOR;
30769 else if (!strcmp ("vector_length", p))
30770 result = PRAGMA_OACC_CLAUSE_VECTOR_LENGTH;
30771 else if (flag_cilkplus && !strcmp ("vectorlength", p))
30772 result = PRAGMA_CILK_CLAUSE_VECTORLENGTH;
30773 break;
30774 case 'w':
30775 if (!strcmp ("wait", p))
30776 result = PRAGMA_OACC_CLAUSE_WAIT;
30777 else if (!strcmp ("worker", p))
30778 result = PRAGMA_OACC_CLAUSE_WORKER;
30779 break;
30783 if (result != PRAGMA_OMP_CLAUSE_NONE)
30784 cp_lexer_consume_token (parser->lexer);
30786 return result;
30789 /* Validate that a clause of the given type does not already exist. */
30791 static void
30792 check_no_duplicate_clause (tree clauses, enum omp_clause_code code,
30793 const char *name, location_t location)
30795 tree c;
30797 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
30798 if (OMP_CLAUSE_CODE (c) == code)
30800 error_at (location, "too many %qs clauses", name);
30801 break;
30805 /* OpenMP 2.5:
30806 variable-list:
30807 identifier
30808 variable-list , identifier
30810 In addition, we match a closing parenthesis (or, if COLON is non-NULL,
30811 colon). An opening parenthesis will have been consumed by the caller.
30813 If KIND is nonzero, create the appropriate node and install the decl
30814 in OMP_CLAUSE_DECL and add the node to the head of the list.
30816 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
30817 return the list created.
30819 COLON can be NULL if only closing parenthesis should end the list,
30820 or pointer to bool which will receive false if the list is terminated
30821 by closing parenthesis or true if the list is terminated by colon. */
30823 static tree
30824 cp_parser_omp_var_list_no_open (cp_parser *parser, enum omp_clause_code kind,
30825 tree list, bool *colon)
30827 cp_token *token;
30828 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
30829 if (colon)
30831 parser->colon_corrects_to_scope_p = false;
30832 *colon = false;
30834 while (1)
30836 tree name, decl;
30838 token = cp_lexer_peek_token (parser->lexer);
30839 if (kind != 0
30840 && current_class_ptr
30841 && cp_parser_is_keyword (token, RID_THIS))
30843 decl = finish_this_expr ();
30844 if (TREE_CODE (decl) == NON_LVALUE_EXPR
30845 || CONVERT_EXPR_P (decl))
30846 decl = TREE_OPERAND (decl, 0);
30847 cp_lexer_consume_token (parser->lexer);
30849 else
30851 name = cp_parser_id_expression (parser, /*template_p=*/false,
30852 /*check_dependency_p=*/true,
30853 /*template_p=*/NULL,
30854 /*declarator_p=*/false,
30855 /*optional_p=*/false);
30856 if (name == error_mark_node)
30857 goto skip_comma;
30859 decl = cp_parser_lookup_name_simple (parser, name, token->location);
30860 if (decl == error_mark_node)
30861 cp_parser_name_lookup_error (parser, name, decl, NLE_NULL,
30862 token->location);
30864 if (decl == error_mark_node)
30866 else if (kind != 0)
30868 switch (kind)
30870 case OMP_CLAUSE__CACHE_:
30871 /* The OpenACC cache directive explicitly only allows "array
30872 elements or subarrays". */
30873 if (cp_lexer_peek_token (parser->lexer)->type != CPP_OPEN_SQUARE)
30875 error_at (token->location, "expected %<[%>");
30876 decl = error_mark_node;
30877 break;
30879 /* FALLTHROUGH. */
30880 case OMP_CLAUSE_MAP:
30881 case OMP_CLAUSE_FROM:
30882 case OMP_CLAUSE_TO:
30883 while (cp_lexer_next_token_is (parser->lexer, CPP_DOT))
30885 location_t loc
30886 = cp_lexer_peek_token (parser->lexer)->location;
30887 cp_id_kind idk = CP_ID_KIND_NONE;
30888 cp_lexer_consume_token (parser->lexer);
30889 decl = convert_from_reference (decl);
30890 decl
30891 = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT,
30892 decl, false,
30893 &idk, loc);
30895 /* FALLTHROUGH. */
30896 case OMP_CLAUSE_DEPEND:
30897 case OMP_CLAUSE_REDUCTION:
30898 while (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
30900 tree low_bound = NULL_TREE, length = NULL_TREE;
30902 parser->colon_corrects_to_scope_p = false;
30903 cp_lexer_consume_token (parser->lexer);
30904 if (!cp_lexer_next_token_is (parser->lexer, CPP_COLON))
30905 low_bound = cp_parser_expression (parser);
30906 if (!colon)
30907 parser->colon_corrects_to_scope_p
30908 = saved_colon_corrects_to_scope_p;
30909 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_SQUARE))
30910 length = integer_one_node;
30911 else
30913 /* Look for `:'. */
30914 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
30915 goto skip_comma;
30916 if (!cp_lexer_next_token_is (parser->lexer,
30917 CPP_CLOSE_SQUARE))
30918 length = cp_parser_expression (parser);
30920 /* Look for the closing `]'. */
30921 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE,
30922 RT_CLOSE_SQUARE))
30923 goto skip_comma;
30925 decl = tree_cons (low_bound, length, decl);
30927 break;
30928 default:
30929 break;
30932 tree u = build_omp_clause (token->location, kind);
30933 OMP_CLAUSE_DECL (u) = decl;
30934 OMP_CLAUSE_CHAIN (u) = list;
30935 list = u;
30937 else
30938 list = tree_cons (decl, NULL_TREE, list);
30940 get_comma:
30941 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
30942 break;
30943 cp_lexer_consume_token (parser->lexer);
30946 if (colon)
30947 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
30949 if (colon != NULL && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
30951 *colon = true;
30952 cp_parser_require (parser, CPP_COLON, RT_COLON);
30953 return list;
30956 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
30958 int ending;
30960 /* Try to resync to an unnested comma. Copied from
30961 cp_parser_parenthesized_expression_list. */
30962 skip_comma:
30963 if (colon)
30964 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
30965 ending = cp_parser_skip_to_closing_parenthesis (parser,
30966 /*recovering=*/true,
30967 /*or_comma=*/true,
30968 /*consume_paren=*/true);
30969 if (ending < 0)
30970 goto get_comma;
30973 return list;
30976 /* Similarly, but expect leading and trailing parenthesis. This is a very
30977 common case for omp clauses. */
30979 static tree
30980 cp_parser_omp_var_list (cp_parser *parser, enum omp_clause_code kind, tree list)
30982 if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
30983 return cp_parser_omp_var_list_no_open (parser, kind, list, NULL);
30984 return list;
30987 /* OpenACC 2.0:
30988 copy ( variable-list )
30989 copyin ( variable-list )
30990 copyout ( variable-list )
30991 create ( variable-list )
30992 delete ( variable-list )
30993 present ( variable-list )
30994 present_or_copy ( variable-list )
30995 pcopy ( variable-list )
30996 present_or_copyin ( variable-list )
30997 pcopyin ( variable-list )
30998 present_or_copyout ( variable-list )
30999 pcopyout ( variable-list )
31000 present_or_create ( variable-list )
31001 pcreate ( variable-list ) */
31003 static tree
31004 cp_parser_oacc_data_clause (cp_parser *parser, pragma_omp_clause c_kind,
31005 tree list)
31007 enum gomp_map_kind kind;
31008 switch (c_kind)
31010 case PRAGMA_OACC_CLAUSE_COPY:
31011 kind = GOMP_MAP_FORCE_TOFROM;
31012 break;
31013 case PRAGMA_OACC_CLAUSE_COPYIN:
31014 kind = GOMP_MAP_FORCE_TO;
31015 break;
31016 case PRAGMA_OACC_CLAUSE_COPYOUT:
31017 kind = GOMP_MAP_FORCE_FROM;
31018 break;
31019 case PRAGMA_OACC_CLAUSE_CREATE:
31020 kind = GOMP_MAP_FORCE_ALLOC;
31021 break;
31022 case PRAGMA_OACC_CLAUSE_DELETE:
31023 kind = GOMP_MAP_DELETE;
31024 break;
31025 case PRAGMA_OACC_CLAUSE_DEVICE:
31026 kind = GOMP_MAP_FORCE_TO;
31027 break;
31028 case PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT:
31029 kind = GOMP_MAP_DEVICE_RESIDENT;
31030 break;
31031 case PRAGMA_OACC_CLAUSE_HOST:
31032 case PRAGMA_OACC_CLAUSE_SELF:
31033 kind = GOMP_MAP_FORCE_FROM;
31034 break;
31035 case PRAGMA_OACC_CLAUSE_LINK:
31036 kind = GOMP_MAP_LINK;
31037 break;
31038 case PRAGMA_OACC_CLAUSE_PRESENT:
31039 kind = GOMP_MAP_FORCE_PRESENT;
31040 break;
31041 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY:
31042 kind = GOMP_MAP_TOFROM;
31043 break;
31044 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN:
31045 kind = GOMP_MAP_TO;
31046 break;
31047 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT:
31048 kind = GOMP_MAP_FROM;
31049 break;
31050 case PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE:
31051 kind = GOMP_MAP_ALLOC;
31052 break;
31053 default:
31054 gcc_unreachable ();
31056 tree nl, c;
31057 nl = cp_parser_omp_var_list (parser, OMP_CLAUSE_MAP, list);
31059 for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
31060 OMP_CLAUSE_SET_MAP_KIND (c, kind);
31062 return nl;
31065 /* OpenACC 2.0:
31066 deviceptr ( variable-list ) */
31068 static tree
31069 cp_parser_oacc_data_clause_deviceptr (cp_parser *parser, tree list)
31071 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
31072 tree vars, t;
31074 /* Can't use OMP_CLAUSE_MAP here (that is, can't use the generic
31075 cp_parser_oacc_data_clause), as for PRAGMA_OACC_CLAUSE_DEVICEPTR,
31076 variable-list must only allow for pointer variables. */
31077 vars = cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
31078 for (t = vars; t; t = TREE_CHAIN (t))
31080 tree v = TREE_PURPOSE (t);
31081 tree u = build_omp_clause (loc, OMP_CLAUSE_MAP);
31082 OMP_CLAUSE_SET_MAP_KIND (u, GOMP_MAP_FORCE_DEVICEPTR);
31083 OMP_CLAUSE_DECL (u) = v;
31084 OMP_CLAUSE_CHAIN (u) = list;
31085 list = u;
31088 return list;
31091 /* OpenACC 2.0:
31092 auto
31093 independent
31094 nohost
31095 seq */
31097 static tree
31098 cp_parser_oacc_simple_clause (cp_parser * /* parser */,
31099 enum omp_clause_code code,
31100 tree list, location_t location)
31102 check_no_duplicate_clause (list, code, omp_clause_code_name[code], location);
31103 tree c = build_omp_clause (location, code);
31104 OMP_CLAUSE_CHAIN (c) = list;
31105 return c;
31108 /* OpenACC:
31109 num_gangs ( expression )
31110 num_workers ( expression )
31111 vector_length ( expression ) */
31113 static tree
31114 cp_parser_oacc_single_int_clause (cp_parser *parser, omp_clause_code code,
31115 const char *str, tree list)
31117 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
31119 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31120 return list;
31122 tree t = cp_parser_assignment_expression (parser, NULL, false, false);
31124 if (t == error_mark_node
31125 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31127 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31128 /*or_comma=*/false,
31129 /*consume_paren=*/true);
31130 return list;
31133 check_no_duplicate_clause (list, code, str, loc);
31135 tree c = build_omp_clause (loc, code);
31136 OMP_CLAUSE_OPERAND (c, 0) = t;
31137 OMP_CLAUSE_CHAIN (c) = list;
31138 return c;
31141 /* OpenACC:
31143 gang [( gang-arg-list )]
31144 worker [( [num:] int-expr )]
31145 vector [( [length:] int-expr )]
31147 where gang-arg is one of:
31149 [num:] int-expr
31150 static: size-expr
31152 and size-expr may be:
31155 int-expr
31158 static tree
31159 cp_parser_oacc_shape_clause (cp_parser *parser, omp_clause_code kind,
31160 const char *str, tree list)
31162 const char *id = "num";
31163 cp_lexer *lexer = parser->lexer;
31164 tree ops[2] = { NULL_TREE, NULL_TREE }, c;
31165 location_t loc = cp_lexer_peek_token (lexer)->location;
31167 if (kind == OMP_CLAUSE_VECTOR)
31168 id = "length";
31170 if (cp_lexer_next_token_is (lexer, CPP_OPEN_PAREN))
31172 cp_lexer_consume_token (lexer);
31176 cp_token *next = cp_lexer_peek_token (lexer);
31177 int idx = 0;
31179 /* Gang static argument. */
31180 if (kind == OMP_CLAUSE_GANG
31181 && cp_lexer_next_token_is_keyword (lexer, RID_STATIC))
31183 cp_lexer_consume_token (lexer);
31185 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
31186 goto cleanup_error;
31188 idx = 1;
31189 if (ops[idx] != NULL)
31191 cp_parser_error (parser, "too many %<static%> arguments");
31192 goto cleanup_error;
31195 /* Check for the '*' argument. */
31196 if (cp_lexer_next_token_is (lexer, CPP_MULT)
31197 && (cp_lexer_nth_token_is (parser->lexer, 2, CPP_COMMA)
31198 || cp_lexer_nth_token_is (parser->lexer, 2,
31199 CPP_CLOSE_PAREN)))
31201 cp_lexer_consume_token (lexer);
31202 ops[idx] = integer_minus_one_node;
31204 if (cp_lexer_next_token_is (lexer, CPP_COMMA))
31206 cp_lexer_consume_token (lexer);
31207 continue;
31209 else break;
31212 /* Worker num: argument and vector length: arguments. */
31213 else if (cp_lexer_next_token_is (lexer, CPP_NAME)
31214 && strcmp (id, IDENTIFIER_POINTER (next->u.value)) == 0
31215 && cp_lexer_nth_token_is (lexer, 2, CPP_COLON))
31217 cp_lexer_consume_token (lexer); /* id */
31218 cp_lexer_consume_token (lexer); /* ':' */
31221 /* Now collect the actual argument. */
31222 if (ops[idx] != NULL_TREE)
31224 cp_parser_error (parser, "unexpected argument");
31225 goto cleanup_error;
31228 tree expr = cp_parser_assignment_expression (parser, NULL, false,
31229 false);
31230 if (expr == error_mark_node)
31231 goto cleanup_error;
31233 mark_exp_read (expr);
31234 ops[idx] = expr;
31236 if (kind == OMP_CLAUSE_GANG
31237 && cp_lexer_next_token_is (lexer, CPP_COMMA))
31239 cp_lexer_consume_token (lexer);
31240 continue;
31242 break;
31244 while (1);
31246 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31247 goto cleanup_error;
31250 check_no_duplicate_clause (list, kind, str, loc);
31252 c = build_omp_clause (loc, kind);
31254 if (ops[1])
31255 OMP_CLAUSE_OPERAND (c, 1) = ops[1];
31257 OMP_CLAUSE_OPERAND (c, 0) = ops[0];
31258 OMP_CLAUSE_CHAIN (c) = list;
31260 return c;
31262 cleanup_error:
31263 cp_parser_skip_to_closing_parenthesis (parser, false, false, true);
31264 return list;
31267 /* OpenACC 2.0:
31268 tile ( size-expr-list ) */
31270 static tree
31271 cp_parser_oacc_clause_tile (cp_parser *parser, location_t clause_loc, tree list)
31273 tree c, expr = error_mark_node;
31274 tree tile = NULL_TREE;
31276 /* Collapse and tile are mutually exclusive. (The spec doesn't say
31277 so, but the spec authors never considered such a case and have
31278 differing opinions on what it might mean, including 'not
31279 allowed'.) */
31280 check_no_duplicate_clause (list, OMP_CLAUSE_TILE, "tile", clause_loc);
31281 check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse",
31282 clause_loc);
31284 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31285 return list;
31289 if (tile && !cp_parser_require (parser, CPP_COMMA, RT_COMMA))
31290 return list;
31292 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT)
31293 && (cp_lexer_nth_token_is (parser->lexer, 2, CPP_COMMA)
31294 || cp_lexer_nth_token_is (parser->lexer, 2, CPP_CLOSE_PAREN)))
31296 cp_lexer_consume_token (parser->lexer);
31297 expr = integer_zero_node;
31299 else
31300 expr = cp_parser_constant_expression (parser);
31302 tile = tree_cons (NULL_TREE, expr, tile);
31304 while (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN));
31306 /* Consume the trailing ')'. */
31307 cp_lexer_consume_token (parser->lexer);
31309 c = build_omp_clause (clause_loc, OMP_CLAUSE_TILE);
31310 tile = nreverse (tile);
31311 OMP_CLAUSE_TILE_LIST (c) = tile;
31312 OMP_CLAUSE_CHAIN (c) = list;
31313 return c;
31316 /* OpenACC 2.0
31317 Parse wait clause or directive parameters. */
31319 static tree
31320 cp_parser_oacc_wait_list (cp_parser *parser, location_t clause_loc, tree list)
31322 vec<tree, va_gc> *args;
31323 tree t, args_tree;
31325 args = cp_parser_parenthesized_expression_list (parser, non_attr,
31326 /*cast_p=*/false,
31327 /*allow_expansion_p=*/true,
31328 /*non_constant_p=*/NULL);
31330 if (args == NULL || args->length () == 0)
31332 cp_parser_error (parser, "expected integer expression before ')'");
31333 if (args != NULL)
31334 release_tree_vector (args);
31335 return list;
31338 args_tree = build_tree_list_vec (args);
31340 release_tree_vector (args);
31342 for (t = args_tree; t; t = TREE_CHAIN (t))
31344 tree targ = TREE_VALUE (t);
31346 if (targ != error_mark_node)
31348 if (!INTEGRAL_TYPE_P (TREE_TYPE (targ)))
31349 error ("%<wait%> expression must be integral");
31350 else
31352 tree c = build_omp_clause (clause_loc, OMP_CLAUSE_WAIT);
31354 mark_rvalue_use (targ);
31355 OMP_CLAUSE_DECL (c) = targ;
31356 OMP_CLAUSE_CHAIN (c) = list;
31357 list = c;
31362 return list;
31365 /* OpenACC:
31366 wait ( int-expr-list ) */
31368 static tree
31369 cp_parser_oacc_clause_wait (cp_parser *parser, tree list)
31371 location_t location = cp_lexer_peek_token (parser->lexer)->location;
31373 if (cp_lexer_peek_token (parser->lexer)->type != CPP_OPEN_PAREN)
31374 return list;
31376 list = cp_parser_oacc_wait_list (parser, location, list);
31378 return list;
31381 /* OpenMP 3.0:
31382 collapse ( constant-expression ) */
31384 static tree
31385 cp_parser_omp_clause_collapse (cp_parser *parser, tree list, location_t location)
31387 tree c, num;
31388 location_t loc;
31389 HOST_WIDE_INT n;
31391 loc = cp_lexer_peek_token (parser->lexer)->location;
31392 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31393 return list;
31395 num = cp_parser_constant_expression (parser);
31397 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31398 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31399 /*or_comma=*/false,
31400 /*consume_paren=*/true);
31402 if (num == error_mark_node)
31403 return list;
31404 num = fold_non_dependent_expr (num);
31405 if (!tree_fits_shwi_p (num)
31406 || !INTEGRAL_TYPE_P (TREE_TYPE (num))
31407 || (n = tree_to_shwi (num)) <= 0
31408 || (int) n != n)
31410 error_at (loc, "collapse argument needs positive constant integer expression");
31411 return list;
31414 check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse", location);
31415 check_no_duplicate_clause (list, OMP_CLAUSE_TILE, "tile", location);
31416 c = build_omp_clause (loc, OMP_CLAUSE_COLLAPSE);
31417 OMP_CLAUSE_CHAIN (c) = list;
31418 OMP_CLAUSE_COLLAPSE_EXPR (c) = num;
31420 return c;
31423 /* OpenMP 2.5:
31424 default ( shared | none )
31426 OpenACC 2.0
31427 default (none) */
31429 static tree
31430 cp_parser_omp_clause_default (cp_parser *parser, tree list,
31431 location_t location, bool is_oacc)
31433 enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
31434 tree c;
31436 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31437 return list;
31438 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
31440 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
31441 const char *p = IDENTIFIER_POINTER (id);
31443 switch (p[0])
31445 case 'n':
31446 if (strcmp ("none", p) != 0)
31447 goto invalid_kind;
31448 kind = OMP_CLAUSE_DEFAULT_NONE;
31449 break;
31451 case 's':
31452 if (strcmp ("shared", p) != 0 || is_oacc)
31453 goto invalid_kind;
31454 kind = OMP_CLAUSE_DEFAULT_SHARED;
31455 break;
31457 default:
31458 goto invalid_kind;
31461 cp_lexer_consume_token (parser->lexer);
31463 else
31465 invalid_kind:
31466 if (is_oacc)
31467 cp_parser_error (parser, "expected %<none%>");
31468 else
31469 cp_parser_error (parser, "expected %<none%> or %<shared%>");
31472 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31473 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31474 /*or_comma=*/false,
31475 /*consume_paren=*/true);
31477 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
31478 return list;
31480 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default", location);
31481 c = build_omp_clause (location, OMP_CLAUSE_DEFAULT);
31482 OMP_CLAUSE_CHAIN (c) = list;
31483 OMP_CLAUSE_DEFAULT_KIND (c) = kind;
31485 return c;
31488 /* OpenMP 3.1:
31489 final ( expression ) */
31491 static tree
31492 cp_parser_omp_clause_final (cp_parser *parser, tree list, location_t location)
31494 tree t, c;
31496 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31497 return list;
31499 t = cp_parser_condition (parser);
31501 if (t == error_mark_node
31502 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31503 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31504 /*or_comma=*/false,
31505 /*consume_paren=*/true);
31507 check_no_duplicate_clause (list, OMP_CLAUSE_FINAL, "final", location);
31509 c = build_omp_clause (location, OMP_CLAUSE_FINAL);
31510 OMP_CLAUSE_FINAL_EXPR (c) = t;
31511 OMP_CLAUSE_CHAIN (c) = list;
31513 return c;
31516 /* OpenMP 2.5:
31517 if ( expression )
31519 OpenMP 4.5:
31520 if ( directive-name-modifier : expression )
31522 directive-name-modifier:
31523 parallel | task | taskloop | target data | target | target update
31524 | target enter data | target exit data */
31526 static tree
31527 cp_parser_omp_clause_if (cp_parser *parser, tree list, location_t location,
31528 bool is_omp)
31530 tree t, c;
31531 enum tree_code if_modifier = ERROR_MARK;
31533 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31534 return list;
31536 if (is_omp && cp_lexer_next_token_is (parser->lexer, CPP_NAME))
31538 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
31539 const char *p = IDENTIFIER_POINTER (id);
31540 int n = 2;
31542 if (strcmp ("parallel", p) == 0)
31543 if_modifier = OMP_PARALLEL;
31544 else if (strcmp ("task", p) == 0)
31545 if_modifier = OMP_TASK;
31546 else if (strcmp ("taskloop", p) == 0)
31547 if_modifier = OMP_TASKLOOP;
31548 else if (strcmp ("target", p) == 0)
31550 if_modifier = OMP_TARGET;
31551 if (cp_lexer_nth_token_is (parser->lexer, 2, CPP_NAME))
31553 id = cp_lexer_peek_nth_token (parser->lexer, 2)->u.value;
31554 p = IDENTIFIER_POINTER (id);
31555 if (strcmp ("data", p) == 0)
31556 if_modifier = OMP_TARGET_DATA;
31557 else if (strcmp ("update", p) == 0)
31558 if_modifier = OMP_TARGET_UPDATE;
31559 else if (strcmp ("enter", p) == 0)
31560 if_modifier = OMP_TARGET_ENTER_DATA;
31561 else if (strcmp ("exit", p) == 0)
31562 if_modifier = OMP_TARGET_EXIT_DATA;
31563 if (if_modifier != OMP_TARGET)
31564 n = 3;
31565 else
31567 location_t loc
31568 = cp_lexer_peek_nth_token (parser->lexer, 2)->location;
31569 error_at (loc, "expected %<data%>, %<update%>, %<enter%> "
31570 "or %<exit%>");
31571 if_modifier = ERROR_MARK;
31573 if (if_modifier == OMP_TARGET_ENTER_DATA
31574 || if_modifier == OMP_TARGET_EXIT_DATA)
31576 if (cp_lexer_nth_token_is (parser->lexer, 3, CPP_NAME))
31578 id = cp_lexer_peek_nth_token (parser->lexer, 3)->u.value;
31579 p = IDENTIFIER_POINTER (id);
31580 if (strcmp ("data", p) == 0)
31581 n = 4;
31583 if (n != 4)
31585 location_t loc
31586 = cp_lexer_peek_nth_token (parser->lexer, 3)->location;
31587 error_at (loc, "expected %<data%>");
31588 if_modifier = ERROR_MARK;
31593 if (if_modifier != ERROR_MARK)
31595 if (cp_lexer_nth_token_is (parser->lexer, n, CPP_COLON))
31597 while (n-- > 0)
31598 cp_lexer_consume_token (parser->lexer);
31600 else
31602 if (n > 2)
31604 location_t loc
31605 = cp_lexer_peek_nth_token (parser->lexer, n)->location;
31606 error_at (loc, "expected %<:%>");
31608 if_modifier = ERROR_MARK;
31613 t = cp_parser_condition (parser);
31615 if (t == error_mark_node
31616 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31617 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31618 /*or_comma=*/false,
31619 /*consume_paren=*/true);
31621 for (c = list; c ; c = OMP_CLAUSE_CHAIN (c))
31622 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IF)
31624 if (if_modifier != ERROR_MARK
31625 && OMP_CLAUSE_IF_MODIFIER (c) == if_modifier)
31627 const char *p = NULL;
31628 switch (if_modifier)
31630 case OMP_PARALLEL: p = "parallel"; break;
31631 case OMP_TASK: p = "task"; break;
31632 case OMP_TASKLOOP: p = "taskloop"; break;
31633 case OMP_TARGET_DATA: p = "target data"; break;
31634 case OMP_TARGET: p = "target"; break;
31635 case OMP_TARGET_UPDATE: p = "target update"; break;
31636 case OMP_TARGET_ENTER_DATA: p = "enter data"; break;
31637 case OMP_TARGET_EXIT_DATA: p = "exit data"; break;
31638 default: gcc_unreachable ();
31640 error_at (location, "too many %<if%> clauses with %qs modifier",
31642 return list;
31644 else if (OMP_CLAUSE_IF_MODIFIER (c) == if_modifier)
31646 if (!is_omp)
31647 error_at (location, "too many %<if%> clauses");
31648 else
31649 error_at (location, "too many %<if%> clauses without modifier");
31650 return list;
31652 else if (if_modifier == ERROR_MARK
31653 || OMP_CLAUSE_IF_MODIFIER (c) == ERROR_MARK)
31655 error_at (location, "if any %<if%> clause has modifier, then all "
31656 "%<if%> clauses have to use modifier");
31657 return list;
31661 c = build_omp_clause (location, OMP_CLAUSE_IF);
31662 OMP_CLAUSE_IF_MODIFIER (c) = if_modifier;
31663 OMP_CLAUSE_IF_EXPR (c) = t;
31664 OMP_CLAUSE_CHAIN (c) = list;
31666 return c;
31669 /* OpenMP 3.1:
31670 mergeable */
31672 static tree
31673 cp_parser_omp_clause_mergeable (cp_parser * /*parser*/,
31674 tree list, location_t location)
31676 tree c;
31678 check_no_duplicate_clause (list, OMP_CLAUSE_MERGEABLE, "mergeable",
31679 location);
31681 c = build_omp_clause (location, OMP_CLAUSE_MERGEABLE);
31682 OMP_CLAUSE_CHAIN (c) = list;
31683 return c;
31686 /* OpenMP 2.5:
31687 nowait */
31689 static tree
31690 cp_parser_omp_clause_nowait (cp_parser * /*parser*/,
31691 tree list, location_t location)
31693 tree c;
31695 check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait", location);
31697 c = build_omp_clause (location, OMP_CLAUSE_NOWAIT);
31698 OMP_CLAUSE_CHAIN (c) = list;
31699 return c;
31702 /* OpenMP 2.5:
31703 num_threads ( expression ) */
31705 static tree
31706 cp_parser_omp_clause_num_threads (cp_parser *parser, tree list,
31707 location_t location)
31709 tree t, c;
31711 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31712 return list;
31714 t = cp_parser_expression (parser);
31716 if (t == error_mark_node
31717 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31718 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31719 /*or_comma=*/false,
31720 /*consume_paren=*/true);
31722 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS,
31723 "num_threads", location);
31725 c = build_omp_clause (location, OMP_CLAUSE_NUM_THREADS);
31726 OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
31727 OMP_CLAUSE_CHAIN (c) = list;
31729 return c;
31732 /* OpenMP 4.5:
31733 num_tasks ( expression ) */
31735 static tree
31736 cp_parser_omp_clause_num_tasks (cp_parser *parser, tree list,
31737 location_t location)
31739 tree t, c;
31741 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31742 return list;
31744 t = cp_parser_expression (parser);
31746 if (t == error_mark_node
31747 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31748 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31749 /*or_comma=*/false,
31750 /*consume_paren=*/true);
31752 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_TASKS,
31753 "num_tasks", location);
31755 c = build_omp_clause (location, OMP_CLAUSE_NUM_TASKS);
31756 OMP_CLAUSE_NUM_TASKS_EXPR (c) = t;
31757 OMP_CLAUSE_CHAIN (c) = list;
31759 return c;
31762 /* OpenMP 4.5:
31763 grainsize ( expression ) */
31765 static tree
31766 cp_parser_omp_clause_grainsize (cp_parser *parser, tree list,
31767 location_t location)
31769 tree t, c;
31771 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31772 return list;
31774 t = cp_parser_expression (parser);
31776 if (t == error_mark_node
31777 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31778 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31779 /*or_comma=*/false,
31780 /*consume_paren=*/true);
31782 check_no_duplicate_clause (list, OMP_CLAUSE_GRAINSIZE,
31783 "grainsize", location);
31785 c = build_omp_clause (location, OMP_CLAUSE_GRAINSIZE);
31786 OMP_CLAUSE_GRAINSIZE_EXPR (c) = t;
31787 OMP_CLAUSE_CHAIN (c) = list;
31789 return c;
31792 /* OpenMP 4.5:
31793 priority ( expression ) */
31795 static tree
31796 cp_parser_omp_clause_priority (cp_parser *parser, tree list,
31797 location_t location)
31799 tree t, c;
31801 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31802 return list;
31804 t = cp_parser_expression (parser);
31806 if (t == error_mark_node
31807 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31808 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31809 /*or_comma=*/false,
31810 /*consume_paren=*/true);
31812 check_no_duplicate_clause (list, OMP_CLAUSE_PRIORITY,
31813 "priority", location);
31815 c = build_omp_clause (location, OMP_CLAUSE_PRIORITY);
31816 OMP_CLAUSE_PRIORITY_EXPR (c) = t;
31817 OMP_CLAUSE_CHAIN (c) = list;
31819 return c;
31822 /* OpenMP 4.5:
31823 hint ( expression ) */
31825 static tree
31826 cp_parser_omp_clause_hint (cp_parser *parser, tree list,
31827 location_t location)
31829 tree t, c;
31831 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31832 return list;
31834 t = cp_parser_expression (parser);
31836 if (t == error_mark_node
31837 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31838 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31839 /*or_comma=*/false,
31840 /*consume_paren=*/true);
31842 check_no_duplicate_clause (list, OMP_CLAUSE_HINT, "hint", location);
31844 c = build_omp_clause (location, OMP_CLAUSE_HINT);
31845 OMP_CLAUSE_HINT_EXPR (c) = t;
31846 OMP_CLAUSE_CHAIN (c) = list;
31848 return c;
31851 /* OpenMP 4.5:
31852 defaultmap ( tofrom : scalar ) */
31854 static tree
31855 cp_parser_omp_clause_defaultmap (cp_parser *parser, tree list,
31856 location_t location)
31858 tree c, id;
31859 const char *p;
31861 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31862 return list;
31864 if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME))
31866 cp_parser_error (parser, "expected %<tofrom%>");
31867 goto out_err;
31869 id = cp_lexer_peek_token (parser->lexer)->u.value;
31870 p = IDENTIFIER_POINTER (id);
31871 if (strcmp (p, "tofrom") != 0)
31873 cp_parser_error (parser, "expected %<tofrom%>");
31874 goto out_err;
31876 cp_lexer_consume_token (parser->lexer);
31877 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
31878 goto out_err;
31880 if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME))
31882 cp_parser_error (parser, "expected %<scalar%>");
31883 goto out_err;
31885 id = cp_lexer_peek_token (parser->lexer)->u.value;
31886 p = IDENTIFIER_POINTER (id);
31887 if (strcmp (p, "scalar") != 0)
31889 cp_parser_error (parser, "expected %<scalar%>");
31890 goto out_err;
31892 cp_lexer_consume_token (parser->lexer);
31893 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31894 goto out_err;
31896 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULTMAP, "defaultmap",
31897 location);
31899 c = build_omp_clause (location, OMP_CLAUSE_DEFAULTMAP);
31900 OMP_CLAUSE_CHAIN (c) = list;
31901 return c;
31903 out_err:
31904 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31905 /*or_comma=*/false,
31906 /*consume_paren=*/true);
31907 return list;
31910 /* OpenMP 2.5:
31911 ordered
31913 OpenMP 4.5:
31914 ordered ( constant-expression ) */
31916 static tree
31917 cp_parser_omp_clause_ordered (cp_parser *parser,
31918 tree list, location_t location)
31920 tree c, num = NULL_TREE;
31921 HOST_WIDE_INT n;
31923 check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED,
31924 "ordered", location);
31926 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
31928 cp_lexer_consume_token (parser->lexer);
31930 num = cp_parser_constant_expression (parser);
31932 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31933 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31934 /*or_comma=*/false,
31935 /*consume_paren=*/true);
31937 if (num == error_mark_node)
31938 return list;
31939 num = fold_non_dependent_expr (num);
31940 if (!tree_fits_shwi_p (num)
31941 || !INTEGRAL_TYPE_P (TREE_TYPE (num))
31942 || (n = tree_to_shwi (num)) <= 0
31943 || (int) n != n)
31945 error_at (location,
31946 "ordered argument needs positive constant integer "
31947 "expression");
31948 return list;
31952 c = build_omp_clause (location, OMP_CLAUSE_ORDERED);
31953 OMP_CLAUSE_ORDERED_EXPR (c) = num;
31954 OMP_CLAUSE_CHAIN (c) = list;
31955 return c;
31958 /* OpenMP 2.5:
31959 reduction ( reduction-operator : variable-list )
31961 reduction-operator:
31962 One of: + * - & ^ | && ||
31964 OpenMP 3.1:
31966 reduction-operator:
31967 One of: + * - & ^ | && || min max
31969 OpenMP 4.0:
31971 reduction-operator:
31972 One of: + * - & ^ | && ||
31973 id-expression */
31975 static tree
31976 cp_parser_omp_clause_reduction (cp_parser *parser, tree list)
31978 enum tree_code code = ERROR_MARK;
31979 tree nlist, c, id = NULL_TREE;
31981 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31982 return list;
31984 switch (cp_lexer_peek_token (parser->lexer)->type)
31986 case CPP_PLUS: code = PLUS_EXPR; break;
31987 case CPP_MULT: code = MULT_EXPR; break;
31988 case CPP_MINUS: code = MINUS_EXPR; break;
31989 case CPP_AND: code = BIT_AND_EXPR; break;
31990 case CPP_XOR: code = BIT_XOR_EXPR; break;
31991 case CPP_OR: code = BIT_IOR_EXPR; break;
31992 case CPP_AND_AND: code = TRUTH_ANDIF_EXPR; break;
31993 case CPP_OR_OR: code = TRUTH_ORIF_EXPR; break;
31994 default: break;
31997 if (code != ERROR_MARK)
31998 cp_lexer_consume_token (parser->lexer);
31999 else
32001 bool saved_colon_corrects_to_scope_p;
32002 saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
32003 parser->colon_corrects_to_scope_p = false;
32004 id = cp_parser_id_expression (parser, /*template_p=*/false,
32005 /*check_dependency_p=*/true,
32006 /*template_p=*/NULL,
32007 /*declarator_p=*/false,
32008 /*optional_p=*/false);
32009 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
32010 if (identifier_p (id))
32012 const char *p = IDENTIFIER_POINTER (id);
32014 if (strcmp (p, "min") == 0)
32015 code = MIN_EXPR;
32016 else if (strcmp (p, "max") == 0)
32017 code = MAX_EXPR;
32018 else if (id == cp_operator_id (PLUS_EXPR))
32019 code = PLUS_EXPR;
32020 else if (id == cp_operator_id (MULT_EXPR))
32021 code = MULT_EXPR;
32022 else if (id == cp_operator_id (MINUS_EXPR))
32023 code = MINUS_EXPR;
32024 else if (id == cp_operator_id (BIT_AND_EXPR))
32025 code = BIT_AND_EXPR;
32026 else if (id == cp_operator_id (BIT_IOR_EXPR))
32027 code = BIT_IOR_EXPR;
32028 else if (id == cp_operator_id (BIT_XOR_EXPR))
32029 code = BIT_XOR_EXPR;
32030 else if (id == cp_operator_id (TRUTH_ANDIF_EXPR))
32031 code = TRUTH_ANDIF_EXPR;
32032 else if (id == cp_operator_id (TRUTH_ORIF_EXPR))
32033 code = TRUTH_ORIF_EXPR;
32034 id = omp_reduction_id (code, id, NULL_TREE);
32035 tree scope = parser->scope;
32036 if (scope)
32037 id = build_qualified_name (NULL_TREE, scope, id, false);
32038 parser->scope = NULL_TREE;
32039 parser->qualifying_scope = NULL_TREE;
32040 parser->object_scope = NULL_TREE;
32042 else
32044 error ("invalid reduction-identifier");
32045 resync_fail:
32046 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32047 /*or_comma=*/false,
32048 /*consume_paren=*/true);
32049 return list;
32053 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
32054 goto resync_fail;
32056 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_REDUCTION, list,
32057 NULL);
32058 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
32060 OMP_CLAUSE_REDUCTION_CODE (c) = code;
32061 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = id;
32064 return nlist;
32067 /* OpenMP 2.5:
32068 schedule ( schedule-kind )
32069 schedule ( schedule-kind , expression )
32071 schedule-kind:
32072 static | dynamic | guided | runtime | auto
32074 OpenMP 4.5:
32075 schedule ( schedule-modifier : schedule-kind )
32076 schedule ( schedule-modifier [ , schedule-modifier ] : schedule-kind , expression )
32078 schedule-modifier:
32079 simd
32080 monotonic
32081 nonmonotonic */
32083 static tree
32084 cp_parser_omp_clause_schedule (cp_parser *parser, tree list, location_t location)
32086 tree c, t;
32087 int modifiers = 0, nmodifiers = 0;
32089 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
32090 return list;
32092 c = build_omp_clause (location, OMP_CLAUSE_SCHEDULE);
32094 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32096 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
32097 const char *p = IDENTIFIER_POINTER (id);
32098 if (strcmp ("simd", p) == 0)
32099 OMP_CLAUSE_SCHEDULE_SIMD (c) = 1;
32100 else if (strcmp ("monotonic", p) == 0)
32101 modifiers |= OMP_CLAUSE_SCHEDULE_MONOTONIC;
32102 else if (strcmp ("nonmonotonic", p) == 0)
32103 modifiers |= OMP_CLAUSE_SCHEDULE_NONMONOTONIC;
32104 else
32105 break;
32106 cp_lexer_consume_token (parser->lexer);
32107 if (nmodifiers++ == 0
32108 && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
32109 cp_lexer_consume_token (parser->lexer);
32110 else
32112 cp_parser_require (parser, CPP_COLON, RT_COLON);
32113 break;
32117 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32119 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
32120 const char *p = IDENTIFIER_POINTER (id);
32122 switch (p[0])
32124 case 'd':
32125 if (strcmp ("dynamic", p) != 0)
32126 goto invalid_kind;
32127 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
32128 break;
32130 case 'g':
32131 if (strcmp ("guided", p) != 0)
32132 goto invalid_kind;
32133 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
32134 break;
32136 case 'r':
32137 if (strcmp ("runtime", p) != 0)
32138 goto invalid_kind;
32139 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
32140 break;
32142 default:
32143 goto invalid_kind;
32146 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
32147 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
32148 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
32149 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_AUTO;
32150 else
32151 goto invalid_kind;
32152 cp_lexer_consume_token (parser->lexer);
32154 if ((modifiers & (OMP_CLAUSE_SCHEDULE_MONOTONIC
32155 | OMP_CLAUSE_SCHEDULE_NONMONOTONIC))
32156 == (OMP_CLAUSE_SCHEDULE_MONOTONIC
32157 | OMP_CLAUSE_SCHEDULE_NONMONOTONIC))
32159 error_at (location, "both %<monotonic%> and %<nonmonotonic%> modifiers "
32160 "specified");
32161 modifiers = 0;
32164 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
32166 cp_token *token;
32167 cp_lexer_consume_token (parser->lexer);
32169 token = cp_lexer_peek_token (parser->lexer);
32170 t = cp_parser_assignment_expression (parser);
32172 if (t == error_mark_node)
32173 goto resync_fail;
32174 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
32175 error_at (token->location, "schedule %<runtime%> does not take "
32176 "a %<chunk_size%> parameter");
32177 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_AUTO)
32178 error_at (token->location, "schedule %<auto%> does not take "
32179 "a %<chunk_size%> parameter");
32180 else
32181 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
32183 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
32184 goto resync_fail;
32186 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
32187 goto resync_fail;
32189 OMP_CLAUSE_SCHEDULE_KIND (c)
32190 = (enum omp_clause_schedule_kind)
32191 (OMP_CLAUSE_SCHEDULE_KIND (c) | modifiers);
32193 check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule", location);
32194 OMP_CLAUSE_CHAIN (c) = list;
32195 return c;
32197 invalid_kind:
32198 cp_parser_error (parser, "invalid schedule kind");
32199 resync_fail:
32200 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32201 /*or_comma=*/false,
32202 /*consume_paren=*/true);
32203 return list;
32206 /* OpenMP 3.0:
32207 untied */
32209 static tree
32210 cp_parser_omp_clause_untied (cp_parser * /*parser*/,
32211 tree list, location_t location)
32213 tree c;
32215 check_no_duplicate_clause (list, OMP_CLAUSE_UNTIED, "untied", location);
32217 c = build_omp_clause (location, OMP_CLAUSE_UNTIED);
32218 OMP_CLAUSE_CHAIN (c) = list;
32219 return c;
32222 /* OpenMP 4.0:
32223 inbranch
32224 notinbranch */
32226 static tree
32227 cp_parser_omp_clause_branch (cp_parser * /*parser*/, enum omp_clause_code code,
32228 tree list, location_t location)
32230 check_no_duplicate_clause (list, code, omp_clause_code_name[code], location);
32231 tree c = build_omp_clause (location, code);
32232 OMP_CLAUSE_CHAIN (c) = list;
32233 return c;
32236 /* OpenMP 4.0:
32237 parallel
32239 sections
32240 taskgroup */
32242 static tree
32243 cp_parser_omp_clause_cancelkind (cp_parser * /*parser*/,
32244 enum omp_clause_code code,
32245 tree list, location_t location)
32247 tree c = build_omp_clause (location, code);
32248 OMP_CLAUSE_CHAIN (c) = list;
32249 return c;
32252 /* OpenMP 4.5:
32253 nogroup */
32255 static tree
32256 cp_parser_omp_clause_nogroup (cp_parser * /*parser*/,
32257 tree list, location_t location)
32259 check_no_duplicate_clause (list, OMP_CLAUSE_NOGROUP, "nogroup", location);
32260 tree c = build_omp_clause (location, OMP_CLAUSE_NOGROUP);
32261 OMP_CLAUSE_CHAIN (c) = list;
32262 return c;
32265 /* OpenMP 4.5:
32266 simd
32267 threads */
32269 static tree
32270 cp_parser_omp_clause_orderedkind (cp_parser * /*parser*/,
32271 enum omp_clause_code code,
32272 tree list, location_t location)
32274 check_no_duplicate_clause (list, code, omp_clause_code_name[code], location);
32275 tree c = build_omp_clause (location, code);
32276 OMP_CLAUSE_CHAIN (c) = list;
32277 return c;
32280 /* OpenMP 4.0:
32281 num_teams ( expression ) */
32283 static tree
32284 cp_parser_omp_clause_num_teams (cp_parser *parser, tree list,
32285 location_t location)
32287 tree t, c;
32289 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
32290 return list;
32292 t = cp_parser_expression (parser);
32294 if (t == error_mark_node
32295 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
32296 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32297 /*or_comma=*/false,
32298 /*consume_paren=*/true);
32300 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_TEAMS,
32301 "num_teams", location);
32303 c = build_omp_clause (location, OMP_CLAUSE_NUM_TEAMS);
32304 OMP_CLAUSE_NUM_TEAMS_EXPR (c) = t;
32305 OMP_CLAUSE_CHAIN (c) = list;
32307 return c;
32310 /* OpenMP 4.0:
32311 thread_limit ( expression ) */
32313 static tree
32314 cp_parser_omp_clause_thread_limit (cp_parser *parser, tree list,
32315 location_t location)
32317 tree t, c;
32319 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
32320 return list;
32322 t = cp_parser_expression (parser);
32324 if (t == error_mark_node
32325 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
32326 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32327 /*or_comma=*/false,
32328 /*consume_paren=*/true);
32330 check_no_duplicate_clause (list, OMP_CLAUSE_THREAD_LIMIT,
32331 "thread_limit", location);
32333 c = build_omp_clause (location, OMP_CLAUSE_THREAD_LIMIT);
32334 OMP_CLAUSE_THREAD_LIMIT_EXPR (c) = t;
32335 OMP_CLAUSE_CHAIN (c) = list;
32337 return c;
32340 /* OpenMP 4.0:
32341 aligned ( variable-list )
32342 aligned ( variable-list : constant-expression ) */
32344 static tree
32345 cp_parser_omp_clause_aligned (cp_parser *parser, tree list)
32347 tree nlist, c, alignment = NULL_TREE;
32348 bool colon;
32350 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
32351 return list;
32353 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_ALIGNED, list,
32354 &colon);
32356 if (colon)
32358 alignment = cp_parser_constant_expression (parser);
32360 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
32361 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32362 /*or_comma=*/false,
32363 /*consume_paren=*/true);
32365 if (alignment == error_mark_node)
32366 alignment = NULL_TREE;
32369 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
32370 OMP_CLAUSE_ALIGNED_ALIGNMENT (c) = alignment;
32372 return nlist;
32375 /* OpenMP 4.0:
32376 linear ( variable-list )
32377 linear ( variable-list : expression )
32379 OpenMP 4.5:
32380 linear ( modifier ( variable-list ) )
32381 linear ( modifier ( variable-list ) : expression ) */
32383 static tree
32384 cp_parser_omp_clause_linear (cp_parser *parser, tree list,
32385 bool is_cilk_simd_fn, bool declare_simd)
32387 tree nlist, c, step = integer_one_node;
32388 bool colon;
32389 enum omp_clause_linear_kind kind = OMP_CLAUSE_LINEAR_DEFAULT;
32391 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
32392 return list;
32394 if (!is_cilk_simd_fn
32395 && cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32397 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
32398 const char *p = IDENTIFIER_POINTER (id);
32400 if (strcmp ("ref", p) == 0)
32401 kind = OMP_CLAUSE_LINEAR_REF;
32402 else if (strcmp ("val", p) == 0)
32403 kind = OMP_CLAUSE_LINEAR_VAL;
32404 else if (strcmp ("uval", p) == 0)
32405 kind = OMP_CLAUSE_LINEAR_UVAL;
32406 if (cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_PAREN))
32407 cp_lexer_consume_token (parser->lexer);
32408 else
32409 kind = OMP_CLAUSE_LINEAR_DEFAULT;
32412 if (kind == OMP_CLAUSE_LINEAR_DEFAULT)
32413 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_LINEAR, list,
32414 &colon);
32415 else
32417 nlist = cp_parser_omp_var_list (parser, OMP_CLAUSE_LINEAR, list);
32418 colon = cp_lexer_next_token_is (parser->lexer, CPP_COLON);
32419 if (colon)
32420 cp_parser_require (parser, CPP_COLON, RT_COLON);
32421 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
32422 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32423 /*or_comma=*/false,
32424 /*consume_paren=*/true);
32427 if (colon)
32429 step = NULL_TREE;
32430 if (declare_simd
32431 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
32432 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_CLOSE_PAREN))
32434 cp_token *token = cp_lexer_peek_token (parser->lexer);
32435 cp_parser_parse_tentatively (parser);
32436 step = cp_parser_id_expression (parser, /*template_p=*/false,
32437 /*check_dependency_p=*/true,
32438 /*template_p=*/NULL,
32439 /*declarator_p=*/false,
32440 /*optional_p=*/false);
32441 if (step != error_mark_node)
32442 step = cp_parser_lookup_name_simple (parser, step, token->location);
32443 if (step == error_mark_node)
32445 step = NULL_TREE;
32446 cp_parser_abort_tentative_parse (parser);
32448 else if (!cp_parser_parse_definitely (parser))
32449 step = NULL_TREE;
32451 if (!step)
32452 step = cp_parser_expression (parser);
32454 if (is_cilk_simd_fn && TREE_CODE (step) == PARM_DECL)
32456 sorry ("using parameters for %<linear%> step is not supported yet");
32457 step = integer_one_node;
32459 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
32460 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32461 /*or_comma=*/false,
32462 /*consume_paren=*/true);
32464 if (step == error_mark_node)
32465 return list;
32468 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
32470 OMP_CLAUSE_LINEAR_STEP (c) = step;
32471 OMP_CLAUSE_LINEAR_KIND (c) = kind;
32474 return nlist;
32477 /* OpenMP 4.0:
32478 safelen ( constant-expression ) */
32480 static tree
32481 cp_parser_omp_clause_safelen (cp_parser *parser, tree list,
32482 location_t location)
32484 tree t, c;
32486 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
32487 return list;
32489 t = cp_parser_constant_expression (parser);
32491 if (t == error_mark_node
32492 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
32493 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32494 /*or_comma=*/false,
32495 /*consume_paren=*/true);
32497 check_no_duplicate_clause (list, OMP_CLAUSE_SAFELEN, "safelen", location);
32499 c = build_omp_clause (location, OMP_CLAUSE_SAFELEN);
32500 OMP_CLAUSE_SAFELEN_EXPR (c) = t;
32501 OMP_CLAUSE_CHAIN (c) = list;
32503 return c;
32506 /* OpenMP 4.0:
32507 simdlen ( constant-expression ) */
32509 static tree
32510 cp_parser_omp_clause_simdlen (cp_parser *parser, tree list,
32511 location_t location)
32513 tree t, c;
32515 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
32516 return list;
32518 t = cp_parser_constant_expression (parser);
32520 if (t == error_mark_node
32521 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
32522 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32523 /*or_comma=*/false,
32524 /*consume_paren=*/true);
32526 check_no_duplicate_clause (list, OMP_CLAUSE_SIMDLEN, "simdlen", location);
32528 c = build_omp_clause (location, OMP_CLAUSE_SIMDLEN);
32529 OMP_CLAUSE_SIMDLEN_EXPR (c) = t;
32530 OMP_CLAUSE_CHAIN (c) = list;
32532 return c;
32535 /* OpenMP 4.5:
32536 vec:
32537 identifier [+/- integer]
32538 vec , identifier [+/- integer]
32541 static tree
32542 cp_parser_omp_clause_depend_sink (cp_parser *parser, location_t clause_loc,
32543 tree list)
32545 tree vec = NULL;
32547 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
32549 cp_parser_error (parser, "expected identifier");
32550 return list;
32553 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32555 location_t id_loc = cp_lexer_peek_token (parser->lexer)->location;
32556 tree t, identifier = cp_parser_identifier (parser);
32557 tree addend = NULL;
32559 if (identifier == error_mark_node)
32560 t = error_mark_node;
32561 else
32563 t = cp_parser_lookup_name_simple
32564 (parser, identifier,
32565 cp_lexer_peek_token (parser->lexer)->location);
32566 if (t == error_mark_node)
32567 cp_parser_name_lookup_error (parser, identifier, t, NLE_NULL,
32568 id_loc);
32571 bool neg = false;
32572 if (cp_lexer_next_token_is (parser->lexer, CPP_MINUS))
32573 neg = true;
32574 else if (!cp_lexer_next_token_is (parser->lexer, CPP_PLUS))
32576 addend = integer_zero_node;
32577 goto add_to_vector;
32579 cp_lexer_consume_token (parser->lexer);
32581 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NUMBER))
32583 cp_parser_error (parser, "expected integer");
32584 return list;
32587 addend = cp_lexer_peek_token (parser->lexer)->u.value;
32588 if (TREE_CODE (addend) != INTEGER_CST)
32590 cp_parser_error (parser, "expected integer");
32591 return list;
32593 cp_lexer_consume_token (parser->lexer);
32595 add_to_vector:
32596 if (t != error_mark_node)
32598 vec = tree_cons (addend, t, vec);
32599 if (neg)
32600 OMP_CLAUSE_DEPEND_SINK_NEGATIVE (vec) = 1;
32603 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
32604 break;
32606 cp_lexer_consume_token (parser->lexer);
32609 if (cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN) && vec)
32611 tree u = build_omp_clause (clause_loc, OMP_CLAUSE_DEPEND);
32612 OMP_CLAUSE_DEPEND_KIND (u) = OMP_CLAUSE_DEPEND_SINK;
32613 OMP_CLAUSE_DECL (u) = nreverse (vec);
32614 OMP_CLAUSE_CHAIN (u) = list;
32615 return u;
32617 return list;
32620 /* OpenMP 4.0:
32621 depend ( depend-kind : variable-list )
32623 depend-kind:
32624 in | out | inout
32626 OpenMP 4.5:
32627 depend ( source )
32629 depend ( sink : vec ) */
32631 static tree
32632 cp_parser_omp_clause_depend (cp_parser *parser, tree list, location_t loc)
32634 tree nlist, c;
32635 enum omp_clause_depend_kind kind = OMP_CLAUSE_DEPEND_INOUT;
32637 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
32638 return list;
32640 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32642 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
32643 const char *p = IDENTIFIER_POINTER (id);
32645 if (strcmp ("in", p) == 0)
32646 kind = OMP_CLAUSE_DEPEND_IN;
32647 else if (strcmp ("inout", p) == 0)
32648 kind = OMP_CLAUSE_DEPEND_INOUT;
32649 else if (strcmp ("out", p) == 0)
32650 kind = OMP_CLAUSE_DEPEND_OUT;
32651 else if (strcmp ("source", p) == 0)
32652 kind = OMP_CLAUSE_DEPEND_SOURCE;
32653 else if (strcmp ("sink", p) == 0)
32654 kind = OMP_CLAUSE_DEPEND_SINK;
32655 else
32656 goto invalid_kind;
32658 else
32659 goto invalid_kind;
32661 cp_lexer_consume_token (parser->lexer);
32663 if (kind == OMP_CLAUSE_DEPEND_SOURCE)
32665 c = build_omp_clause (loc, OMP_CLAUSE_DEPEND);
32666 OMP_CLAUSE_DEPEND_KIND (c) = kind;
32667 OMP_CLAUSE_DECL (c) = NULL_TREE;
32668 OMP_CLAUSE_CHAIN (c) = list;
32669 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
32670 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32671 /*or_comma=*/false,
32672 /*consume_paren=*/true);
32673 return c;
32676 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
32677 goto resync_fail;
32679 if (kind == OMP_CLAUSE_DEPEND_SINK)
32680 nlist = cp_parser_omp_clause_depend_sink (parser, loc, list);
32681 else
32683 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_DEPEND,
32684 list, NULL);
32686 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
32687 OMP_CLAUSE_DEPEND_KIND (c) = kind;
32689 return nlist;
32691 invalid_kind:
32692 cp_parser_error (parser, "invalid depend kind");
32693 resync_fail:
32694 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32695 /*or_comma=*/false,
32696 /*consume_paren=*/true);
32697 return list;
32700 /* OpenMP 4.0:
32701 map ( map-kind : variable-list )
32702 map ( variable-list )
32704 map-kind:
32705 alloc | to | from | tofrom
32707 OpenMP 4.5:
32708 map-kind:
32709 alloc | to | from | tofrom | release | delete
32711 map ( always [,] map-kind: variable-list ) */
32713 static tree
32714 cp_parser_omp_clause_map (cp_parser *parser, tree list)
32716 tree nlist, c;
32717 enum gomp_map_kind kind = GOMP_MAP_TOFROM;
32718 bool always = false;
32720 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
32721 return list;
32723 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32725 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
32726 const char *p = IDENTIFIER_POINTER (id);
32728 if (strcmp ("always", p) == 0)
32730 int nth = 2;
32731 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COMMA)
32732 nth++;
32733 if ((cp_lexer_peek_nth_token (parser->lexer, nth)->type == CPP_NAME
32734 || (cp_lexer_peek_nth_token (parser->lexer, nth)->keyword
32735 == RID_DELETE))
32736 && (cp_lexer_peek_nth_token (parser->lexer, nth + 1)->type
32737 == CPP_COLON))
32739 always = true;
32740 cp_lexer_consume_token (parser->lexer);
32741 if (nth == 3)
32742 cp_lexer_consume_token (parser->lexer);
32747 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
32748 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
32750 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
32751 const char *p = IDENTIFIER_POINTER (id);
32753 if (strcmp ("alloc", p) == 0)
32754 kind = GOMP_MAP_ALLOC;
32755 else if (strcmp ("to", p) == 0)
32756 kind = always ? GOMP_MAP_ALWAYS_TO : GOMP_MAP_TO;
32757 else if (strcmp ("from", p) == 0)
32758 kind = always ? GOMP_MAP_ALWAYS_FROM : GOMP_MAP_FROM;
32759 else if (strcmp ("tofrom", p) == 0)
32760 kind = always ? GOMP_MAP_ALWAYS_TOFROM : GOMP_MAP_TOFROM;
32761 else if (strcmp ("release", p) == 0)
32762 kind = GOMP_MAP_RELEASE;
32763 else
32765 cp_parser_error (parser, "invalid map kind");
32766 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32767 /*or_comma=*/false,
32768 /*consume_paren=*/true);
32769 return list;
32771 cp_lexer_consume_token (parser->lexer);
32772 cp_lexer_consume_token (parser->lexer);
32774 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DELETE)
32775 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
32777 kind = GOMP_MAP_DELETE;
32778 cp_lexer_consume_token (parser->lexer);
32779 cp_lexer_consume_token (parser->lexer);
32782 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_MAP, list,
32783 NULL);
32785 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
32786 OMP_CLAUSE_SET_MAP_KIND (c, kind);
32788 return nlist;
32791 /* OpenMP 4.0:
32792 device ( expression ) */
32794 static tree
32795 cp_parser_omp_clause_device (cp_parser *parser, tree list,
32796 location_t location)
32798 tree t, c;
32800 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
32801 return list;
32803 t = cp_parser_expression (parser);
32805 if (t == error_mark_node
32806 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
32807 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32808 /*or_comma=*/false,
32809 /*consume_paren=*/true);
32811 check_no_duplicate_clause (list, OMP_CLAUSE_DEVICE,
32812 "device", location);
32814 c = build_omp_clause (location, OMP_CLAUSE_DEVICE);
32815 OMP_CLAUSE_DEVICE_ID (c) = t;
32816 OMP_CLAUSE_CHAIN (c) = list;
32818 return c;
32821 /* OpenMP 4.0:
32822 dist_schedule ( static )
32823 dist_schedule ( static , expression ) */
32825 static tree
32826 cp_parser_omp_clause_dist_schedule (cp_parser *parser, tree list,
32827 location_t location)
32829 tree c, t;
32831 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
32832 return list;
32834 c = build_omp_clause (location, OMP_CLAUSE_DIST_SCHEDULE);
32836 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
32837 goto invalid_kind;
32838 cp_lexer_consume_token (parser->lexer);
32840 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
32842 cp_lexer_consume_token (parser->lexer);
32844 t = cp_parser_assignment_expression (parser);
32846 if (t == error_mark_node)
32847 goto resync_fail;
32848 OMP_CLAUSE_DIST_SCHEDULE_CHUNK_EXPR (c) = t;
32850 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
32851 goto resync_fail;
32853 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
32854 goto resync_fail;
32856 check_no_duplicate_clause (list, OMP_CLAUSE_DIST_SCHEDULE, "dist_schedule",
32857 location);
32858 OMP_CLAUSE_CHAIN (c) = list;
32859 return c;
32861 invalid_kind:
32862 cp_parser_error (parser, "invalid dist_schedule kind");
32863 resync_fail:
32864 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32865 /*or_comma=*/false,
32866 /*consume_paren=*/true);
32867 return list;
32870 /* OpenMP 4.0:
32871 proc_bind ( proc-bind-kind )
32873 proc-bind-kind:
32874 master | close | spread */
32876 static tree
32877 cp_parser_omp_clause_proc_bind (cp_parser *parser, tree list,
32878 location_t location)
32880 tree c;
32881 enum omp_clause_proc_bind_kind kind;
32883 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
32884 return list;
32886 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32888 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
32889 const char *p = IDENTIFIER_POINTER (id);
32891 if (strcmp ("master", p) == 0)
32892 kind = OMP_CLAUSE_PROC_BIND_MASTER;
32893 else if (strcmp ("close", p) == 0)
32894 kind = OMP_CLAUSE_PROC_BIND_CLOSE;
32895 else if (strcmp ("spread", p) == 0)
32896 kind = OMP_CLAUSE_PROC_BIND_SPREAD;
32897 else
32898 goto invalid_kind;
32900 else
32901 goto invalid_kind;
32903 cp_lexer_consume_token (parser->lexer);
32904 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
32905 goto resync_fail;
32907 c = build_omp_clause (location, OMP_CLAUSE_PROC_BIND);
32908 check_no_duplicate_clause (list, OMP_CLAUSE_PROC_BIND, "proc_bind",
32909 location);
32910 OMP_CLAUSE_PROC_BIND_KIND (c) = kind;
32911 OMP_CLAUSE_CHAIN (c) = list;
32912 return c;
32914 invalid_kind:
32915 cp_parser_error (parser, "invalid depend kind");
32916 resync_fail:
32917 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32918 /*or_comma=*/false,
32919 /*consume_paren=*/true);
32920 return list;
32923 /* OpenACC:
32924 async [( int-expr )] */
32926 static tree
32927 cp_parser_oacc_clause_async (cp_parser *parser, tree list)
32929 tree c, t;
32930 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
32932 t = build_int_cst (integer_type_node, GOMP_ASYNC_NOVAL);
32934 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
32936 cp_lexer_consume_token (parser->lexer);
32938 t = cp_parser_expression (parser);
32939 if (t == error_mark_node
32940 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
32941 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32942 /*or_comma=*/false,
32943 /*consume_paren=*/true);
32946 check_no_duplicate_clause (list, OMP_CLAUSE_ASYNC, "async", loc);
32948 c = build_omp_clause (loc, OMP_CLAUSE_ASYNC);
32949 OMP_CLAUSE_ASYNC_EXPR (c) = t;
32950 OMP_CLAUSE_CHAIN (c) = list;
32951 list = c;
32953 return list;
32956 /* Parse all OpenACC clauses. The set clauses allowed by the directive
32957 is a bitmask in MASK. Return the list of clauses found. */
32959 static tree
32960 cp_parser_oacc_all_clauses (cp_parser *parser, omp_clause_mask mask,
32961 const char *where, cp_token *pragma_tok,
32962 bool finish_p = true)
32964 tree clauses = NULL;
32965 bool first = true;
32967 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
32969 location_t here;
32970 pragma_omp_clause c_kind;
32971 omp_clause_code code;
32972 const char *c_name;
32973 tree prev = clauses;
32975 if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
32976 cp_lexer_consume_token (parser->lexer);
32978 here = cp_lexer_peek_token (parser->lexer)->location;
32979 c_kind = cp_parser_omp_clause_name (parser);
32981 switch (c_kind)
32983 case PRAGMA_OACC_CLAUSE_ASYNC:
32984 clauses = cp_parser_oacc_clause_async (parser, clauses);
32985 c_name = "async";
32986 break;
32987 case PRAGMA_OACC_CLAUSE_AUTO:
32988 clauses = cp_parser_oacc_simple_clause (parser, OMP_CLAUSE_AUTO,
32989 clauses, here);
32990 c_name = "auto";
32991 break;
32992 case PRAGMA_OACC_CLAUSE_COLLAPSE:
32993 clauses = cp_parser_omp_clause_collapse (parser, clauses, here);
32994 c_name = "collapse";
32995 break;
32996 case PRAGMA_OACC_CLAUSE_COPY:
32997 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
32998 c_name = "copy";
32999 break;
33000 case PRAGMA_OACC_CLAUSE_COPYIN:
33001 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33002 c_name = "copyin";
33003 break;
33004 case PRAGMA_OACC_CLAUSE_COPYOUT:
33005 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33006 c_name = "copyout";
33007 break;
33008 case PRAGMA_OACC_CLAUSE_CREATE:
33009 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33010 c_name = "create";
33011 break;
33012 case PRAGMA_OACC_CLAUSE_DELETE:
33013 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33014 c_name = "delete";
33015 break;
33016 case PRAGMA_OMP_CLAUSE_DEFAULT:
33017 clauses = cp_parser_omp_clause_default (parser, clauses, here, true);
33018 c_name = "default";
33019 break;
33020 case PRAGMA_OACC_CLAUSE_DEVICE:
33021 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33022 c_name = "device";
33023 break;
33024 case PRAGMA_OACC_CLAUSE_DEVICEPTR:
33025 clauses = cp_parser_oacc_data_clause_deviceptr (parser, clauses);
33026 c_name = "deviceptr";
33027 break;
33028 case PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT:
33029 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33030 c_name = "device_resident";
33031 break;
33032 case PRAGMA_OACC_CLAUSE_FIRSTPRIVATE:
33033 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
33034 clauses);
33035 c_name = "firstprivate";
33036 break;
33037 case PRAGMA_OACC_CLAUSE_GANG:
33038 c_name = "gang";
33039 clauses = cp_parser_oacc_shape_clause (parser, OMP_CLAUSE_GANG,
33040 c_name, clauses);
33041 break;
33042 case PRAGMA_OACC_CLAUSE_HOST:
33043 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33044 c_name = "host";
33045 break;
33046 case PRAGMA_OACC_CLAUSE_IF:
33047 clauses = cp_parser_omp_clause_if (parser, clauses, here, false);
33048 c_name = "if";
33049 break;
33050 case PRAGMA_OACC_CLAUSE_INDEPENDENT:
33051 clauses = cp_parser_oacc_simple_clause (parser,
33052 OMP_CLAUSE_INDEPENDENT,
33053 clauses, here);
33054 c_name = "independent";
33055 break;
33056 case PRAGMA_OACC_CLAUSE_LINK:
33057 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33058 c_name = "link";
33059 break;
33060 case PRAGMA_OACC_CLAUSE_NUM_GANGS:
33061 code = OMP_CLAUSE_NUM_GANGS;
33062 c_name = "num_gangs";
33063 clauses = cp_parser_oacc_single_int_clause (parser, code, c_name,
33064 clauses);
33065 break;
33066 case PRAGMA_OACC_CLAUSE_NUM_WORKERS:
33067 c_name = "num_workers";
33068 code = OMP_CLAUSE_NUM_WORKERS;
33069 clauses = cp_parser_oacc_single_int_clause (parser, code, c_name,
33070 clauses);
33071 break;
33072 case PRAGMA_OACC_CLAUSE_PRESENT:
33073 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33074 c_name = "present";
33075 break;
33076 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY:
33077 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33078 c_name = "present_or_copy";
33079 break;
33080 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN:
33081 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33082 c_name = "present_or_copyin";
33083 break;
33084 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT:
33085 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33086 c_name = "present_or_copyout";
33087 break;
33088 case PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE:
33089 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33090 c_name = "present_or_create";
33091 break;
33092 case PRAGMA_OACC_CLAUSE_PRIVATE:
33093 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
33094 clauses);
33095 c_name = "private";
33096 break;
33097 case PRAGMA_OACC_CLAUSE_REDUCTION:
33098 clauses = cp_parser_omp_clause_reduction (parser, clauses);
33099 c_name = "reduction";
33100 break;
33101 case PRAGMA_OACC_CLAUSE_SELF:
33102 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33103 c_name = "self";
33104 break;
33105 case PRAGMA_OACC_CLAUSE_SEQ:
33106 clauses = cp_parser_oacc_simple_clause (parser, OMP_CLAUSE_SEQ,
33107 clauses, here);
33108 c_name = "seq";
33109 break;
33110 case PRAGMA_OACC_CLAUSE_TILE:
33111 clauses = cp_parser_oacc_clause_tile (parser, here, clauses);
33112 c_name = "tile";
33113 break;
33114 case PRAGMA_OACC_CLAUSE_USE_DEVICE:
33115 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_USE_DEVICE_PTR,
33116 clauses);
33117 c_name = "use_device";
33118 break;
33119 case PRAGMA_OACC_CLAUSE_VECTOR:
33120 c_name = "vector";
33121 clauses = cp_parser_oacc_shape_clause (parser, OMP_CLAUSE_VECTOR,
33122 c_name, clauses);
33123 break;
33124 case PRAGMA_OACC_CLAUSE_VECTOR_LENGTH:
33125 c_name = "vector_length";
33126 code = OMP_CLAUSE_VECTOR_LENGTH;
33127 clauses = cp_parser_oacc_single_int_clause (parser, code, c_name,
33128 clauses);
33129 break;
33130 case PRAGMA_OACC_CLAUSE_WAIT:
33131 clauses = cp_parser_oacc_clause_wait (parser, clauses);
33132 c_name = "wait";
33133 break;
33134 case PRAGMA_OACC_CLAUSE_WORKER:
33135 c_name = "worker";
33136 clauses = cp_parser_oacc_shape_clause (parser, OMP_CLAUSE_WORKER,
33137 c_name, clauses);
33138 break;
33139 default:
33140 cp_parser_error (parser, "expected %<#pragma acc%> clause");
33141 goto saw_error;
33144 first = false;
33146 if (((mask >> c_kind) & 1) == 0)
33148 /* Remove the invalid clause(s) from the list to avoid
33149 confusing the rest of the compiler. */
33150 clauses = prev;
33151 error_at (here, "%qs is not valid for %qs", c_name, where);
33155 saw_error:
33156 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
33158 if (finish_p)
33159 return finish_omp_clauses (clauses, C_ORT_ACC);
33161 return clauses;
33164 /* Parse all OpenMP clauses. The set clauses allowed by the directive
33165 is a bitmask in MASK. Return the list of clauses found; the result
33166 of clause default goes in *pdefault. */
33168 static tree
33169 cp_parser_omp_all_clauses (cp_parser *parser, omp_clause_mask mask,
33170 const char *where, cp_token *pragma_tok,
33171 bool finish_p = true)
33173 tree clauses = NULL;
33174 bool first = true;
33175 cp_token *token = NULL;
33177 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
33179 pragma_omp_clause c_kind;
33180 const char *c_name;
33181 tree prev = clauses;
33183 if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
33184 cp_lexer_consume_token (parser->lexer);
33186 token = cp_lexer_peek_token (parser->lexer);
33187 c_kind = cp_parser_omp_clause_name (parser);
33189 switch (c_kind)
33191 case PRAGMA_OMP_CLAUSE_COLLAPSE:
33192 clauses = cp_parser_omp_clause_collapse (parser, clauses,
33193 token->location);
33194 c_name = "collapse";
33195 break;
33196 case PRAGMA_OMP_CLAUSE_COPYIN:
33197 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYIN, clauses);
33198 c_name = "copyin";
33199 break;
33200 case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
33201 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYPRIVATE,
33202 clauses);
33203 c_name = "copyprivate";
33204 break;
33205 case PRAGMA_OMP_CLAUSE_DEFAULT:
33206 clauses = cp_parser_omp_clause_default (parser, clauses,
33207 token->location, false);
33208 c_name = "default";
33209 break;
33210 case PRAGMA_OMP_CLAUSE_FINAL:
33211 clauses = cp_parser_omp_clause_final (parser, clauses, token->location);
33212 c_name = "final";
33213 break;
33214 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
33215 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
33216 clauses);
33217 c_name = "firstprivate";
33218 break;
33219 case PRAGMA_OMP_CLAUSE_GRAINSIZE:
33220 clauses = cp_parser_omp_clause_grainsize (parser, clauses,
33221 token->location);
33222 c_name = "grainsize";
33223 break;
33224 case PRAGMA_OMP_CLAUSE_HINT:
33225 clauses = cp_parser_omp_clause_hint (parser, clauses,
33226 token->location);
33227 c_name = "hint";
33228 break;
33229 case PRAGMA_OMP_CLAUSE_DEFAULTMAP:
33230 clauses = cp_parser_omp_clause_defaultmap (parser, clauses,
33231 token->location);
33232 c_name = "defaultmap";
33233 break;
33234 case PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR:
33235 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_USE_DEVICE_PTR,
33236 clauses);
33237 c_name = "use_device_ptr";
33238 break;
33239 case PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR:
33240 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_IS_DEVICE_PTR,
33241 clauses);
33242 c_name = "is_device_ptr";
33243 break;
33244 case PRAGMA_OMP_CLAUSE_IF:
33245 clauses = cp_parser_omp_clause_if (parser, clauses, token->location,
33246 true);
33247 c_name = "if";
33248 break;
33249 case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
33250 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LASTPRIVATE,
33251 clauses);
33252 c_name = "lastprivate";
33253 break;
33254 case PRAGMA_OMP_CLAUSE_MERGEABLE:
33255 clauses = cp_parser_omp_clause_mergeable (parser, clauses,
33256 token->location);
33257 c_name = "mergeable";
33258 break;
33259 case PRAGMA_OMP_CLAUSE_NOWAIT:
33260 clauses = cp_parser_omp_clause_nowait (parser, clauses, token->location);
33261 c_name = "nowait";
33262 break;
33263 case PRAGMA_OMP_CLAUSE_NUM_TASKS:
33264 clauses = cp_parser_omp_clause_num_tasks (parser, clauses,
33265 token->location);
33266 c_name = "num_tasks";
33267 break;
33268 case PRAGMA_OMP_CLAUSE_NUM_THREADS:
33269 clauses = cp_parser_omp_clause_num_threads (parser, clauses,
33270 token->location);
33271 c_name = "num_threads";
33272 break;
33273 case PRAGMA_OMP_CLAUSE_ORDERED:
33274 clauses = cp_parser_omp_clause_ordered (parser, clauses,
33275 token->location);
33276 c_name = "ordered";
33277 break;
33278 case PRAGMA_OMP_CLAUSE_PRIORITY:
33279 clauses = cp_parser_omp_clause_priority (parser, clauses,
33280 token->location);
33281 c_name = "priority";
33282 break;
33283 case PRAGMA_OMP_CLAUSE_PRIVATE:
33284 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
33285 clauses);
33286 c_name = "private";
33287 break;
33288 case PRAGMA_OMP_CLAUSE_REDUCTION:
33289 clauses = cp_parser_omp_clause_reduction (parser, clauses);
33290 c_name = "reduction";
33291 break;
33292 case PRAGMA_OMP_CLAUSE_SCHEDULE:
33293 clauses = cp_parser_omp_clause_schedule (parser, clauses,
33294 token->location);
33295 c_name = "schedule";
33296 break;
33297 case PRAGMA_OMP_CLAUSE_SHARED:
33298 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_SHARED,
33299 clauses);
33300 c_name = "shared";
33301 break;
33302 case PRAGMA_OMP_CLAUSE_UNTIED:
33303 clauses = cp_parser_omp_clause_untied (parser, clauses,
33304 token->location);
33305 c_name = "untied";
33306 break;
33307 case PRAGMA_OMP_CLAUSE_INBRANCH:
33308 case PRAGMA_CILK_CLAUSE_MASK:
33309 clauses = cp_parser_omp_clause_branch (parser, OMP_CLAUSE_INBRANCH,
33310 clauses, token->location);
33311 c_name = "inbranch";
33312 break;
33313 case PRAGMA_OMP_CLAUSE_NOTINBRANCH:
33314 case PRAGMA_CILK_CLAUSE_NOMASK:
33315 clauses = cp_parser_omp_clause_branch (parser,
33316 OMP_CLAUSE_NOTINBRANCH,
33317 clauses, token->location);
33318 c_name = "notinbranch";
33319 break;
33320 case PRAGMA_OMP_CLAUSE_PARALLEL:
33321 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_PARALLEL,
33322 clauses, token->location);
33323 c_name = "parallel";
33324 if (!first)
33326 clause_not_first:
33327 error_at (token->location, "%qs must be the first clause of %qs",
33328 c_name, where);
33329 clauses = prev;
33331 break;
33332 case PRAGMA_OMP_CLAUSE_FOR:
33333 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_FOR,
33334 clauses, token->location);
33335 c_name = "for";
33336 if (!first)
33337 goto clause_not_first;
33338 break;
33339 case PRAGMA_OMP_CLAUSE_SECTIONS:
33340 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_SECTIONS,
33341 clauses, token->location);
33342 c_name = "sections";
33343 if (!first)
33344 goto clause_not_first;
33345 break;
33346 case PRAGMA_OMP_CLAUSE_TASKGROUP:
33347 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_TASKGROUP,
33348 clauses, token->location);
33349 c_name = "taskgroup";
33350 if (!first)
33351 goto clause_not_first;
33352 break;
33353 case PRAGMA_OMP_CLAUSE_LINK:
33354 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LINK, clauses);
33355 c_name = "to";
33356 break;
33357 case PRAGMA_OMP_CLAUSE_TO:
33358 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINK)) != 0)
33359 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_TO_DECLARE,
33360 clauses);
33361 else
33362 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_TO, clauses);
33363 c_name = "to";
33364 break;
33365 case PRAGMA_OMP_CLAUSE_FROM:
33366 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FROM, clauses);
33367 c_name = "from";
33368 break;
33369 case PRAGMA_OMP_CLAUSE_UNIFORM:
33370 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_UNIFORM,
33371 clauses);
33372 c_name = "uniform";
33373 break;
33374 case PRAGMA_OMP_CLAUSE_NUM_TEAMS:
33375 clauses = cp_parser_omp_clause_num_teams (parser, clauses,
33376 token->location);
33377 c_name = "num_teams";
33378 break;
33379 case PRAGMA_OMP_CLAUSE_THREAD_LIMIT:
33380 clauses = cp_parser_omp_clause_thread_limit (parser, clauses,
33381 token->location);
33382 c_name = "thread_limit";
33383 break;
33384 case PRAGMA_OMP_CLAUSE_ALIGNED:
33385 clauses = cp_parser_omp_clause_aligned (parser, clauses);
33386 c_name = "aligned";
33387 break;
33388 case PRAGMA_OMP_CLAUSE_LINEAR:
33390 bool cilk_simd_fn = false, declare_simd = false;
33391 if (((mask >> PRAGMA_CILK_CLAUSE_VECTORLENGTH) & 1) != 0)
33392 cilk_simd_fn = true;
33393 else if (((mask >> PRAGMA_OMP_CLAUSE_UNIFORM) & 1) != 0)
33394 declare_simd = true;
33395 clauses = cp_parser_omp_clause_linear (parser, clauses,
33396 cilk_simd_fn, declare_simd);
33398 c_name = "linear";
33399 break;
33400 case PRAGMA_OMP_CLAUSE_DEPEND:
33401 clauses = cp_parser_omp_clause_depend (parser, clauses,
33402 token->location);
33403 c_name = "depend";
33404 break;
33405 case PRAGMA_OMP_CLAUSE_MAP:
33406 clauses = cp_parser_omp_clause_map (parser, clauses);
33407 c_name = "map";
33408 break;
33409 case PRAGMA_OMP_CLAUSE_DEVICE:
33410 clauses = cp_parser_omp_clause_device (parser, clauses,
33411 token->location);
33412 c_name = "device";
33413 break;
33414 case PRAGMA_OMP_CLAUSE_DIST_SCHEDULE:
33415 clauses = cp_parser_omp_clause_dist_schedule (parser, clauses,
33416 token->location);
33417 c_name = "dist_schedule";
33418 break;
33419 case PRAGMA_OMP_CLAUSE_PROC_BIND:
33420 clauses = cp_parser_omp_clause_proc_bind (parser, clauses,
33421 token->location);
33422 c_name = "proc_bind";
33423 break;
33424 case PRAGMA_OMP_CLAUSE_SAFELEN:
33425 clauses = cp_parser_omp_clause_safelen (parser, clauses,
33426 token->location);
33427 c_name = "safelen";
33428 break;
33429 case PRAGMA_OMP_CLAUSE_SIMDLEN:
33430 clauses = cp_parser_omp_clause_simdlen (parser, clauses,
33431 token->location);
33432 c_name = "simdlen";
33433 break;
33434 case PRAGMA_OMP_CLAUSE_NOGROUP:
33435 clauses = cp_parser_omp_clause_nogroup (parser, clauses,
33436 token->location);
33437 c_name = "nogroup";
33438 break;
33439 case PRAGMA_OMP_CLAUSE_THREADS:
33440 clauses
33441 = cp_parser_omp_clause_orderedkind (parser, OMP_CLAUSE_THREADS,
33442 clauses, token->location);
33443 c_name = "threads";
33444 break;
33445 case PRAGMA_OMP_CLAUSE_SIMD:
33446 clauses
33447 = cp_parser_omp_clause_orderedkind (parser, OMP_CLAUSE_SIMD,
33448 clauses, token->location);
33449 c_name = "simd";
33450 break;
33451 case PRAGMA_CILK_CLAUSE_VECTORLENGTH:
33452 clauses = cp_parser_cilk_simd_vectorlength (parser, clauses, true);
33453 c_name = "simdlen";
33454 break;
33455 default:
33456 cp_parser_error (parser, "expected %<#pragma omp%> clause");
33457 goto saw_error;
33460 first = false;
33462 if (((mask >> c_kind) & 1) == 0)
33464 /* Remove the invalid clause(s) from the list to avoid
33465 confusing the rest of the compiler. */
33466 clauses = prev;
33467 error_at (token->location, "%qs is not valid for %qs", c_name, where);
33470 saw_error:
33471 /* In Cilk Plus SIMD enabled functions there is no pragma_token, so
33472 no reason to skip to the end. */
33473 if (!(flag_cilkplus && pragma_tok == NULL))
33474 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
33475 if (finish_p)
33477 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNIFORM)) != 0)
33478 return finish_omp_clauses (clauses, C_ORT_OMP_DECLARE_SIMD);
33479 else
33480 return finish_omp_clauses (clauses, C_ORT_OMP);
33482 return clauses;
33485 /* OpenMP 2.5:
33486 structured-block:
33487 statement
33489 In practice, we're also interested in adding the statement to an
33490 outer node. So it is convenient if we work around the fact that
33491 cp_parser_statement calls add_stmt. */
33493 static unsigned
33494 cp_parser_begin_omp_structured_block (cp_parser *parser)
33496 unsigned save = parser->in_statement;
33498 /* Only move the values to IN_OMP_BLOCK if they weren't false.
33499 This preserves the "not within loop or switch" style error messages
33500 for nonsense cases like
33501 void foo() {
33502 #pragma omp single
33503 break;
33506 if (parser->in_statement)
33507 parser->in_statement = IN_OMP_BLOCK;
33509 return save;
33512 static void
33513 cp_parser_end_omp_structured_block (cp_parser *parser, unsigned save)
33515 parser->in_statement = save;
33518 static tree
33519 cp_parser_omp_structured_block (cp_parser *parser, bool *if_p)
33521 tree stmt = begin_omp_structured_block ();
33522 unsigned int save = cp_parser_begin_omp_structured_block (parser);
33524 cp_parser_statement (parser, NULL_TREE, false, if_p);
33526 cp_parser_end_omp_structured_block (parser, save);
33527 return finish_omp_structured_block (stmt);
33530 /* OpenMP 2.5:
33531 # pragma omp atomic new-line
33532 expression-stmt
33534 expression-stmt:
33535 x binop= expr | x++ | ++x | x-- | --x
33536 binop:
33537 +, *, -, /, &, ^, |, <<, >>
33539 where x is an lvalue expression with scalar type.
33541 OpenMP 3.1:
33542 # pragma omp atomic new-line
33543 update-stmt
33545 # pragma omp atomic read new-line
33546 read-stmt
33548 # pragma omp atomic write new-line
33549 write-stmt
33551 # pragma omp atomic update new-line
33552 update-stmt
33554 # pragma omp atomic capture new-line
33555 capture-stmt
33557 # pragma omp atomic capture new-line
33558 capture-block
33560 read-stmt:
33561 v = x
33562 write-stmt:
33563 x = expr
33564 update-stmt:
33565 expression-stmt | x = x binop expr
33566 capture-stmt:
33567 v = expression-stmt
33568 capture-block:
33569 { v = x; update-stmt; } | { update-stmt; v = x; }
33571 OpenMP 4.0:
33572 update-stmt:
33573 expression-stmt | x = x binop expr | x = expr binop x
33574 capture-stmt:
33575 v = update-stmt
33576 capture-block:
33577 { v = x; update-stmt; } | { update-stmt; v = x; } | { v = x; x = expr; }
33579 where x and v are lvalue expressions with scalar type. */
33581 static void
33582 cp_parser_omp_atomic (cp_parser *parser, cp_token *pragma_tok)
33584 tree lhs = NULL_TREE, rhs = NULL_TREE, v = NULL_TREE, lhs1 = NULL_TREE;
33585 tree rhs1 = NULL_TREE, orig_lhs;
33586 enum tree_code code = OMP_ATOMIC, opcode = NOP_EXPR;
33587 bool structured_block = false;
33588 bool seq_cst = false;
33590 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
33592 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
33593 const char *p = IDENTIFIER_POINTER (id);
33595 if (!strcmp (p, "seq_cst"))
33597 seq_cst = true;
33598 cp_lexer_consume_token (parser->lexer);
33599 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)
33600 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME)
33601 cp_lexer_consume_token (parser->lexer);
33604 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
33606 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
33607 const char *p = IDENTIFIER_POINTER (id);
33609 if (!strcmp (p, "read"))
33610 code = OMP_ATOMIC_READ;
33611 else if (!strcmp (p, "write"))
33612 code = NOP_EXPR;
33613 else if (!strcmp (p, "update"))
33614 code = OMP_ATOMIC;
33615 else if (!strcmp (p, "capture"))
33616 code = OMP_ATOMIC_CAPTURE_NEW;
33617 else
33618 p = NULL;
33619 if (p)
33620 cp_lexer_consume_token (parser->lexer);
33622 if (!seq_cst)
33624 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)
33625 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME)
33626 cp_lexer_consume_token (parser->lexer);
33628 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
33630 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
33631 const char *p = IDENTIFIER_POINTER (id);
33633 if (!strcmp (p, "seq_cst"))
33635 seq_cst = true;
33636 cp_lexer_consume_token (parser->lexer);
33640 cp_parser_require_pragma_eol (parser, pragma_tok);
33642 switch (code)
33644 case OMP_ATOMIC_READ:
33645 case NOP_EXPR: /* atomic write */
33646 v = cp_parser_unary_expression (parser);
33647 if (v == error_mark_node)
33648 goto saw_error;
33649 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
33650 goto saw_error;
33651 if (code == NOP_EXPR)
33652 lhs = cp_parser_expression (parser);
33653 else
33654 lhs = cp_parser_unary_expression (parser);
33655 if (lhs == error_mark_node)
33656 goto saw_error;
33657 if (code == NOP_EXPR)
33659 /* atomic write is represented by OMP_ATOMIC with NOP_EXPR
33660 opcode. */
33661 code = OMP_ATOMIC;
33662 rhs = lhs;
33663 lhs = v;
33664 v = NULL_TREE;
33666 goto done;
33667 case OMP_ATOMIC_CAPTURE_NEW:
33668 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
33670 cp_lexer_consume_token (parser->lexer);
33671 structured_block = true;
33673 else
33675 v = cp_parser_unary_expression (parser);
33676 if (v == error_mark_node)
33677 goto saw_error;
33678 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
33679 goto saw_error;
33681 default:
33682 break;
33685 restart:
33686 lhs = cp_parser_unary_expression (parser);
33687 orig_lhs = lhs;
33688 switch (TREE_CODE (lhs))
33690 case ERROR_MARK:
33691 goto saw_error;
33693 case POSTINCREMENT_EXPR:
33694 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
33695 code = OMP_ATOMIC_CAPTURE_OLD;
33696 /* FALLTHROUGH */
33697 case PREINCREMENT_EXPR:
33698 lhs = TREE_OPERAND (lhs, 0);
33699 opcode = PLUS_EXPR;
33700 rhs = integer_one_node;
33701 break;
33703 case POSTDECREMENT_EXPR:
33704 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
33705 code = OMP_ATOMIC_CAPTURE_OLD;
33706 /* FALLTHROUGH */
33707 case PREDECREMENT_EXPR:
33708 lhs = TREE_OPERAND (lhs, 0);
33709 opcode = MINUS_EXPR;
33710 rhs = integer_one_node;
33711 break;
33713 case COMPOUND_EXPR:
33714 if (TREE_CODE (TREE_OPERAND (lhs, 0)) == SAVE_EXPR
33715 && TREE_CODE (TREE_OPERAND (lhs, 1)) == COMPOUND_EXPR
33716 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (lhs, 1), 0)) == MODIFY_EXPR
33717 && TREE_OPERAND (TREE_OPERAND (lhs, 1), 1) == TREE_OPERAND (lhs, 0)
33718 && TREE_CODE (TREE_TYPE (TREE_OPERAND (TREE_OPERAND
33719 (TREE_OPERAND (lhs, 1), 0), 0)))
33720 == BOOLEAN_TYPE)
33721 /* Undo effects of boolean_increment for post {in,de}crement. */
33722 lhs = TREE_OPERAND (TREE_OPERAND (lhs, 1), 0);
33723 /* FALLTHRU */
33724 case MODIFY_EXPR:
33725 if (TREE_CODE (lhs) == MODIFY_EXPR
33726 && TREE_CODE (TREE_TYPE (TREE_OPERAND (lhs, 0))) == BOOLEAN_TYPE)
33728 /* Undo effects of boolean_increment. */
33729 if (integer_onep (TREE_OPERAND (lhs, 1)))
33731 /* This is pre or post increment. */
33732 rhs = TREE_OPERAND (lhs, 1);
33733 lhs = TREE_OPERAND (lhs, 0);
33734 opcode = NOP_EXPR;
33735 if (code == OMP_ATOMIC_CAPTURE_NEW
33736 && !structured_block
33737 && TREE_CODE (orig_lhs) == COMPOUND_EXPR)
33738 code = OMP_ATOMIC_CAPTURE_OLD;
33739 break;
33742 /* FALLTHRU */
33743 default:
33744 switch (cp_lexer_peek_token (parser->lexer)->type)
33746 case CPP_MULT_EQ:
33747 opcode = MULT_EXPR;
33748 break;
33749 case CPP_DIV_EQ:
33750 opcode = TRUNC_DIV_EXPR;
33751 break;
33752 case CPP_PLUS_EQ:
33753 opcode = PLUS_EXPR;
33754 break;
33755 case CPP_MINUS_EQ:
33756 opcode = MINUS_EXPR;
33757 break;
33758 case CPP_LSHIFT_EQ:
33759 opcode = LSHIFT_EXPR;
33760 break;
33761 case CPP_RSHIFT_EQ:
33762 opcode = RSHIFT_EXPR;
33763 break;
33764 case CPP_AND_EQ:
33765 opcode = BIT_AND_EXPR;
33766 break;
33767 case CPP_OR_EQ:
33768 opcode = BIT_IOR_EXPR;
33769 break;
33770 case CPP_XOR_EQ:
33771 opcode = BIT_XOR_EXPR;
33772 break;
33773 case CPP_EQ:
33774 enum cp_parser_prec oprec;
33775 cp_token *token;
33776 cp_lexer_consume_token (parser->lexer);
33777 cp_parser_parse_tentatively (parser);
33778 rhs1 = cp_parser_simple_cast_expression (parser);
33779 if (rhs1 == error_mark_node)
33781 cp_parser_abort_tentative_parse (parser);
33782 cp_parser_simple_cast_expression (parser);
33783 goto saw_error;
33785 token = cp_lexer_peek_token (parser->lexer);
33786 if (token->type != CPP_SEMICOLON && !cp_tree_equal (lhs, rhs1))
33788 cp_parser_abort_tentative_parse (parser);
33789 cp_parser_parse_tentatively (parser);
33790 rhs = cp_parser_binary_expression (parser, false, true,
33791 PREC_NOT_OPERATOR, NULL);
33792 if (rhs == error_mark_node)
33794 cp_parser_abort_tentative_parse (parser);
33795 cp_parser_binary_expression (parser, false, true,
33796 PREC_NOT_OPERATOR, NULL);
33797 goto saw_error;
33799 switch (TREE_CODE (rhs))
33801 case MULT_EXPR:
33802 case TRUNC_DIV_EXPR:
33803 case RDIV_EXPR:
33804 case PLUS_EXPR:
33805 case MINUS_EXPR:
33806 case LSHIFT_EXPR:
33807 case RSHIFT_EXPR:
33808 case BIT_AND_EXPR:
33809 case BIT_IOR_EXPR:
33810 case BIT_XOR_EXPR:
33811 if (cp_tree_equal (lhs, TREE_OPERAND (rhs, 1)))
33813 if (cp_parser_parse_definitely (parser))
33815 opcode = TREE_CODE (rhs);
33816 rhs1 = TREE_OPERAND (rhs, 0);
33817 rhs = TREE_OPERAND (rhs, 1);
33818 goto stmt_done;
33820 else
33821 goto saw_error;
33823 break;
33824 default:
33825 break;
33827 cp_parser_abort_tentative_parse (parser);
33828 if (structured_block && code == OMP_ATOMIC_CAPTURE_OLD)
33830 rhs = cp_parser_expression (parser);
33831 if (rhs == error_mark_node)
33832 goto saw_error;
33833 opcode = NOP_EXPR;
33834 rhs1 = NULL_TREE;
33835 goto stmt_done;
33837 cp_parser_error (parser,
33838 "invalid form of %<#pragma omp atomic%>");
33839 goto saw_error;
33841 if (!cp_parser_parse_definitely (parser))
33842 goto saw_error;
33843 switch (token->type)
33845 case CPP_SEMICOLON:
33846 if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
33848 code = OMP_ATOMIC_CAPTURE_OLD;
33849 v = lhs;
33850 lhs = NULL_TREE;
33851 lhs1 = rhs1;
33852 rhs1 = NULL_TREE;
33853 cp_lexer_consume_token (parser->lexer);
33854 goto restart;
33856 else if (structured_block)
33858 opcode = NOP_EXPR;
33859 rhs = rhs1;
33860 rhs1 = NULL_TREE;
33861 goto stmt_done;
33863 cp_parser_error (parser,
33864 "invalid form of %<#pragma omp atomic%>");
33865 goto saw_error;
33866 case CPP_MULT:
33867 opcode = MULT_EXPR;
33868 break;
33869 case CPP_DIV:
33870 opcode = TRUNC_DIV_EXPR;
33871 break;
33872 case CPP_PLUS:
33873 opcode = PLUS_EXPR;
33874 break;
33875 case CPP_MINUS:
33876 opcode = MINUS_EXPR;
33877 break;
33878 case CPP_LSHIFT:
33879 opcode = LSHIFT_EXPR;
33880 break;
33881 case CPP_RSHIFT:
33882 opcode = RSHIFT_EXPR;
33883 break;
33884 case CPP_AND:
33885 opcode = BIT_AND_EXPR;
33886 break;
33887 case CPP_OR:
33888 opcode = BIT_IOR_EXPR;
33889 break;
33890 case CPP_XOR:
33891 opcode = BIT_XOR_EXPR;
33892 break;
33893 default:
33894 cp_parser_error (parser,
33895 "invalid operator for %<#pragma omp atomic%>");
33896 goto saw_error;
33898 oprec = TOKEN_PRECEDENCE (token);
33899 gcc_assert (oprec != PREC_NOT_OPERATOR);
33900 if (commutative_tree_code (opcode))
33901 oprec = (enum cp_parser_prec) (oprec - 1);
33902 cp_lexer_consume_token (parser->lexer);
33903 rhs = cp_parser_binary_expression (parser, false, false,
33904 oprec, NULL);
33905 if (rhs == error_mark_node)
33906 goto saw_error;
33907 goto stmt_done;
33908 /* FALLTHROUGH */
33909 default:
33910 cp_parser_error (parser,
33911 "invalid operator for %<#pragma omp atomic%>");
33912 goto saw_error;
33914 cp_lexer_consume_token (parser->lexer);
33916 rhs = cp_parser_expression (parser);
33917 if (rhs == error_mark_node)
33918 goto saw_error;
33919 break;
33921 stmt_done:
33922 if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
33924 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
33925 goto saw_error;
33926 v = cp_parser_unary_expression (parser);
33927 if (v == error_mark_node)
33928 goto saw_error;
33929 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
33930 goto saw_error;
33931 lhs1 = cp_parser_unary_expression (parser);
33932 if (lhs1 == error_mark_node)
33933 goto saw_error;
33935 if (structured_block)
33937 cp_parser_consume_semicolon_at_end_of_statement (parser);
33938 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
33940 done:
33941 finish_omp_atomic (code, opcode, lhs, rhs, v, lhs1, rhs1, seq_cst);
33942 if (!structured_block)
33943 cp_parser_consume_semicolon_at_end_of_statement (parser);
33944 return;
33946 saw_error:
33947 cp_parser_skip_to_end_of_block_or_statement (parser);
33948 if (structured_block)
33950 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
33951 cp_lexer_consume_token (parser->lexer);
33952 else if (code == OMP_ATOMIC_CAPTURE_NEW)
33954 cp_parser_skip_to_end_of_block_or_statement (parser);
33955 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
33956 cp_lexer_consume_token (parser->lexer);
33962 /* OpenMP 2.5:
33963 # pragma omp barrier new-line */
33965 static void
33966 cp_parser_omp_barrier (cp_parser *parser, cp_token *pragma_tok)
33968 cp_parser_require_pragma_eol (parser, pragma_tok);
33969 finish_omp_barrier ();
33972 /* OpenMP 2.5:
33973 # pragma omp critical [(name)] new-line
33974 structured-block
33976 OpenMP 4.5:
33977 # pragma omp critical [(name) [hint(expression)]] new-line
33978 structured-block */
33980 #define OMP_CRITICAL_CLAUSE_MASK \
33981 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_HINT) )
33983 static tree
33984 cp_parser_omp_critical (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
33986 tree stmt, name = NULL_TREE, clauses = NULL_TREE;
33988 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
33990 cp_lexer_consume_token (parser->lexer);
33992 name = cp_parser_identifier (parser);
33994 if (name == error_mark_node
33995 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
33996 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33997 /*or_comma=*/false,
33998 /*consume_paren=*/true);
33999 if (name == error_mark_node)
34000 name = NULL;
34002 clauses = cp_parser_omp_all_clauses (parser,
34003 OMP_CRITICAL_CLAUSE_MASK,
34004 "#pragma omp critical", pragma_tok);
34006 else
34007 cp_parser_require_pragma_eol (parser, pragma_tok);
34009 stmt = cp_parser_omp_structured_block (parser, if_p);
34010 return c_finish_omp_critical (input_location, stmt, name, clauses);
34013 /* OpenMP 2.5:
34014 # pragma omp flush flush-vars[opt] new-line
34016 flush-vars:
34017 ( variable-list ) */
34019 static void
34020 cp_parser_omp_flush (cp_parser *parser, cp_token *pragma_tok)
34022 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
34023 (void) cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
34024 cp_parser_require_pragma_eol (parser, pragma_tok);
34026 finish_omp_flush ();
34029 /* Helper function, to parse omp for increment expression. */
34031 static tree
34032 cp_parser_omp_for_cond (cp_parser *parser, tree decl, enum tree_code code)
34034 tree cond = cp_parser_binary_expression (parser, false, true,
34035 PREC_NOT_OPERATOR, NULL);
34036 if (cond == error_mark_node
34037 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
34039 cp_parser_skip_to_end_of_statement (parser);
34040 return error_mark_node;
34043 switch (TREE_CODE (cond))
34045 case GT_EXPR:
34046 case GE_EXPR:
34047 case LT_EXPR:
34048 case LE_EXPR:
34049 break;
34050 case NE_EXPR:
34051 if (code == CILK_SIMD || code == CILK_FOR)
34052 break;
34053 /* Fall through: OpenMP disallows NE_EXPR. */
34054 gcc_fallthrough ();
34055 default:
34056 return error_mark_node;
34059 /* If decl is an iterator, preserve LHS and RHS of the relational
34060 expr until finish_omp_for. */
34061 if (decl
34062 && (type_dependent_expression_p (decl)
34063 || CLASS_TYPE_P (TREE_TYPE (decl))))
34064 return cond;
34066 return build_x_binary_op (EXPR_LOC_OR_LOC (cond, input_location),
34067 TREE_CODE (cond),
34068 TREE_OPERAND (cond, 0), ERROR_MARK,
34069 TREE_OPERAND (cond, 1), ERROR_MARK,
34070 /*overload=*/NULL, tf_warning_or_error);
34073 /* Helper function, to parse omp for increment expression. */
34075 static tree
34076 cp_parser_omp_for_incr (cp_parser *parser, tree decl)
34078 cp_token *token = cp_lexer_peek_token (parser->lexer);
34079 enum tree_code op;
34080 tree lhs, rhs;
34081 cp_id_kind idk;
34082 bool decl_first;
34084 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
34086 op = (token->type == CPP_PLUS_PLUS
34087 ? PREINCREMENT_EXPR : PREDECREMENT_EXPR);
34088 cp_lexer_consume_token (parser->lexer);
34089 lhs = cp_parser_simple_cast_expression (parser);
34090 if (lhs != decl
34091 && (!processing_template_decl || !cp_tree_equal (lhs, decl)))
34092 return error_mark_node;
34093 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
34096 lhs = cp_parser_primary_expression (parser, false, false, false, &idk);
34097 if (lhs != decl
34098 && (!processing_template_decl || !cp_tree_equal (lhs, decl)))
34099 return error_mark_node;
34101 token = cp_lexer_peek_token (parser->lexer);
34102 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
34104 op = (token->type == CPP_PLUS_PLUS
34105 ? POSTINCREMENT_EXPR : POSTDECREMENT_EXPR);
34106 cp_lexer_consume_token (parser->lexer);
34107 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
34110 op = cp_parser_assignment_operator_opt (parser);
34111 if (op == ERROR_MARK)
34112 return error_mark_node;
34114 if (op != NOP_EXPR)
34116 rhs = cp_parser_assignment_expression (parser);
34117 rhs = build2 (op, TREE_TYPE (decl), decl, rhs);
34118 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
34121 lhs = cp_parser_binary_expression (parser, false, false,
34122 PREC_ADDITIVE_EXPRESSION, NULL);
34123 token = cp_lexer_peek_token (parser->lexer);
34124 decl_first = (lhs == decl
34125 || (processing_template_decl && cp_tree_equal (lhs, decl)));
34126 if (decl_first)
34127 lhs = NULL_TREE;
34128 if (token->type != CPP_PLUS
34129 && token->type != CPP_MINUS)
34130 return error_mark_node;
34134 op = token->type == CPP_PLUS ? PLUS_EXPR : MINUS_EXPR;
34135 cp_lexer_consume_token (parser->lexer);
34136 rhs = cp_parser_binary_expression (parser, false, false,
34137 PREC_ADDITIVE_EXPRESSION, NULL);
34138 token = cp_lexer_peek_token (parser->lexer);
34139 if (token->type == CPP_PLUS || token->type == CPP_MINUS || decl_first)
34141 if (lhs == NULL_TREE)
34143 if (op == PLUS_EXPR)
34144 lhs = rhs;
34145 else
34146 lhs = build_x_unary_op (input_location, NEGATE_EXPR, rhs,
34147 tf_warning_or_error);
34149 else
34150 lhs = build_x_binary_op (input_location, op, lhs, ERROR_MARK, rhs,
34151 ERROR_MARK, NULL, tf_warning_or_error);
34154 while (token->type == CPP_PLUS || token->type == CPP_MINUS);
34156 if (!decl_first)
34158 if ((rhs != decl
34159 && (!processing_template_decl || !cp_tree_equal (rhs, decl)))
34160 || op == MINUS_EXPR)
34161 return error_mark_node;
34162 rhs = build2 (op, TREE_TYPE (decl), lhs, decl);
34164 else
34165 rhs = build2 (PLUS_EXPR, TREE_TYPE (decl), decl, lhs);
34167 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
34170 /* Parse the initialization statement of either an OpenMP for loop or
34171 a Cilk Plus for loop.
34173 Return true if the resulting construct should have an
34174 OMP_CLAUSE_PRIVATE added to it. */
34176 static tree
34177 cp_parser_omp_for_loop_init (cp_parser *parser,
34178 enum tree_code code,
34179 tree &this_pre_body,
34180 vec<tree, va_gc> *for_block,
34181 tree &init,
34182 tree &orig_init,
34183 tree &decl,
34184 tree &real_decl)
34186 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
34187 return NULL_TREE;
34189 tree add_private_clause = NULL_TREE;
34191 /* See 2.5.1 (in OpenMP 3.0, similar wording is in 2.5 standard too):
34193 init-expr:
34194 var = lb
34195 integer-type var = lb
34196 random-access-iterator-type var = lb
34197 pointer-type var = lb
34199 cp_decl_specifier_seq type_specifiers;
34201 /* First, try to parse as an initialized declaration. See
34202 cp_parser_condition, from whence the bulk of this is copied. */
34204 cp_parser_parse_tentatively (parser);
34205 cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
34206 /*is_trailing_return=*/false,
34207 &type_specifiers);
34208 if (cp_parser_parse_definitely (parser))
34210 /* If parsing a type specifier seq succeeded, then this
34211 MUST be a initialized declaration. */
34212 tree asm_specification, attributes;
34213 cp_declarator *declarator;
34215 declarator = cp_parser_declarator (parser,
34216 CP_PARSER_DECLARATOR_NAMED,
34217 /*ctor_dtor_or_conv_p=*/NULL,
34218 /*parenthesized_p=*/NULL,
34219 /*member_p=*/false,
34220 /*friend_p=*/false);
34221 attributes = cp_parser_attributes_opt (parser);
34222 asm_specification = cp_parser_asm_specification_opt (parser);
34224 if (declarator == cp_error_declarator)
34225 cp_parser_skip_to_end_of_statement (parser);
34227 else
34229 tree pushed_scope, auto_node;
34231 decl = start_decl (declarator, &type_specifiers,
34232 SD_INITIALIZED, attributes,
34233 /*prefix_attributes=*/NULL_TREE,
34234 &pushed_scope);
34236 auto_node = type_uses_auto (TREE_TYPE (decl));
34237 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
34239 if (cp_lexer_next_token_is (parser->lexer,
34240 CPP_OPEN_PAREN))
34242 if (code != CILK_SIMD && code != CILK_FOR)
34243 error ("parenthesized initialization is not allowed in "
34244 "OpenMP %<for%> loop");
34245 else
34246 error ("parenthesized initialization is "
34247 "not allowed in for-loop");
34249 else
34250 /* Trigger an error. */
34251 cp_parser_require (parser, CPP_EQ, RT_EQ);
34253 init = error_mark_node;
34254 cp_parser_skip_to_end_of_statement (parser);
34256 else if (CLASS_TYPE_P (TREE_TYPE (decl))
34257 || type_dependent_expression_p (decl)
34258 || auto_node)
34260 bool is_direct_init, is_non_constant_init;
34262 init = cp_parser_initializer (parser,
34263 &is_direct_init,
34264 &is_non_constant_init);
34266 if (auto_node)
34268 TREE_TYPE (decl)
34269 = do_auto_deduction (TREE_TYPE (decl), init,
34270 auto_node);
34272 if (!CLASS_TYPE_P (TREE_TYPE (decl))
34273 && !type_dependent_expression_p (decl))
34274 goto non_class;
34277 cp_finish_decl (decl, init, !is_non_constant_init,
34278 asm_specification,
34279 LOOKUP_ONLYCONVERTING);
34280 orig_init = init;
34281 if (CLASS_TYPE_P (TREE_TYPE (decl)))
34283 vec_safe_push (for_block, this_pre_body);
34284 init = NULL_TREE;
34286 else
34288 init = pop_stmt_list (this_pre_body);
34289 if (init && TREE_CODE (init) == STATEMENT_LIST)
34291 tree_stmt_iterator i = tsi_start (init);
34292 /* Move lambda DECL_EXPRs to FOR_BLOCK. */
34293 while (!tsi_end_p (i))
34295 tree t = tsi_stmt (i);
34296 if (TREE_CODE (t) == DECL_EXPR
34297 && TREE_CODE (DECL_EXPR_DECL (t)) == TYPE_DECL)
34299 tsi_delink (&i);
34300 vec_safe_push (for_block, t);
34301 continue;
34303 break;
34305 if (tsi_one_before_end_p (i))
34307 tree t = tsi_stmt (i);
34308 tsi_delink (&i);
34309 free_stmt_list (init);
34310 init = t;
34314 this_pre_body = NULL_TREE;
34316 else
34318 /* Consume '='. */
34319 cp_lexer_consume_token (parser->lexer);
34320 init = cp_parser_assignment_expression (parser);
34322 non_class:
34323 if (TREE_CODE (TREE_TYPE (decl)) == REFERENCE_TYPE)
34324 init = error_mark_node;
34325 else
34326 cp_finish_decl (decl, NULL_TREE,
34327 /*init_const_expr_p=*/false,
34328 asm_specification,
34329 LOOKUP_ONLYCONVERTING);
34332 if (pushed_scope)
34333 pop_scope (pushed_scope);
34336 else
34338 cp_id_kind idk;
34339 /* If parsing a type specifier sequence failed, then
34340 this MUST be a simple expression. */
34341 if (code == CILK_FOR)
34342 error ("%<_Cilk_for%> allows expression instead of declaration only "
34343 "in C, not in C++");
34344 cp_parser_parse_tentatively (parser);
34345 decl = cp_parser_primary_expression (parser, false, false,
34346 false, &idk);
34347 cp_token *last_tok = cp_lexer_peek_token (parser->lexer);
34348 if (!cp_parser_error_occurred (parser)
34349 && decl
34350 && (TREE_CODE (decl) == COMPONENT_REF
34351 || (TREE_CODE (decl) == SCOPE_REF && TREE_TYPE (decl))))
34353 cp_parser_abort_tentative_parse (parser);
34354 cp_parser_parse_tentatively (parser);
34355 cp_token *token = cp_lexer_peek_token (parser->lexer);
34356 tree name = cp_parser_id_expression (parser, /*template_p=*/false,
34357 /*check_dependency_p=*/true,
34358 /*template_p=*/NULL,
34359 /*declarator_p=*/false,
34360 /*optional_p=*/false);
34361 if (name != error_mark_node
34362 && last_tok == cp_lexer_peek_token (parser->lexer))
34364 decl = cp_parser_lookup_name_simple (parser, name,
34365 token->location);
34366 if (TREE_CODE (decl) == FIELD_DECL)
34367 add_private_clause = omp_privatize_field (decl, false);
34369 cp_parser_abort_tentative_parse (parser);
34370 cp_parser_parse_tentatively (parser);
34371 decl = cp_parser_primary_expression (parser, false, false,
34372 false, &idk);
34374 if (!cp_parser_error_occurred (parser)
34375 && decl
34376 && DECL_P (decl)
34377 && CLASS_TYPE_P (TREE_TYPE (decl)))
34379 tree rhs;
34381 cp_parser_parse_definitely (parser);
34382 cp_parser_require (parser, CPP_EQ, RT_EQ);
34383 rhs = cp_parser_assignment_expression (parser);
34384 orig_init = rhs;
34385 finish_expr_stmt (build_x_modify_expr (EXPR_LOCATION (rhs),
34386 decl, NOP_EXPR,
34387 rhs,
34388 tf_warning_or_error));
34389 if (!add_private_clause)
34390 add_private_clause = decl;
34392 else
34394 decl = NULL;
34395 cp_parser_abort_tentative_parse (parser);
34396 init = cp_parser_expression (parser);
34397 if (init)
34399 if (TREE_CODE (init) == MODIFY_EXPR
34400 || TREE_CODE (init) == MODOP_EXPR)
34401 real_decl = TREE_OPERAND (init, 0);
34405 return add_private_clause;
34408 /* Parse the restricted form of the for statement allowed by OpenMP. */
34410 static tree
34411 cp_parser_omp_for_loop (cp_parser *parser, enum tree_code code, tree clauses,
34412 tree *cclauses, bool *if_p)
34414 tree init, orig_init, cond, incr, body, decl, pre_body = NULL_TREE, ret;
34415 tree real_decl, initv, condv, incrv, declv;
34416 tree this_pre_body, cl, ordered_cl = NULL_TREE;
34417 location_t loc_first;
34418 bool collapse_err = false;
34419 int i, collapse = 1, ordered = 0, count, nbraces = 0;
34420 vec<tree, va_gc> *for_block = make_tree_vector ();
34421 auto_vec<tree, 4> orig_inits;
34422 bool tiling = false;
34424 for (cl = clauses; cl; cl = OMP_CLAUSE_CHAIN (cl))
34425 if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_COLLAPSE)
34426 collapse = tree_to_shwi (OMP_CLAUSE_COLLAPSE_EXPR (cl));
34427 else if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_TILE)
34429 tiling = true;
34430 collapse = list_length (OMP_CLAUSE_TILE_LIST (cl));
34432 else if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_ORDERED
34433 && OMP_CLAUSE_ORDERED_EXPR (cl))
34435 ordered_cl = cl;
34436 ordered = tree_to_shwi (OMP_CLAUSE_ORDERED_EXPR (cl));
34439 if (ordered && ordered < collapse)
34441 error_at (OMP_CLAUSE_LOCATION (ordered_cl),
34442 "%<ordered%> clause parameter is less than %<collapse%>");
34443 OMP_CLAUSE_ORDERED_EXPR (ordered_cl)
34444 = build_int_cst (NULL_TREE, collapse);
34445 ordered = collapse;
34447 if (ordered)
34449 for (tree *pc = &clauses; *pc; )
34450 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_LINEAR)
34452 error_at (OMP_CLAUSE_LOCATION (*pc),
34453 "%<linear%> clause may not be specified together "
34454 "with %<ordered%> clause with a parameter");
34455 *pc = OMP_CLAUSE_CHAIN (*pc);
34457 else
34458 pc = &OMP_CLAUSE_CHAIN (*pc);
34461 gcc_assert (tiling || (collapse >= 1 && ordered >= 0));
34462 count = ordered ? ordered : collapse;
34464 declv = make_tree_vec (count);
34465 initv = make_tree_vec (count);
34466 condv = make_tree_vec (count);
34467 incrv = make_tree_vec (count);
34469 loc_first = cp_lexer_peek_token (parser->lexer)->location;
34471 for (i = 0; i < count; i++)
34473 int bracecount = 0;
34474 tree add_private_clause = NULL_TREE;
34475 location_t loc;
34477 if (code != CILK_FOR
34478 && !cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
34480 if (!collapse_err)
34481 cp_parser_error (parser, "for statement expected");
34482 return NULL;
34484 if (code == CILK_FOR
34485 && !cp_lexer_next_token_is_keyword (parser->lexer, RID_CILK_FOR))
34487 if (!collapse_err)
34488 cp_parser_error (parser, "_Cilk_for statement expected");
34489 return NULL;
34491 loc = cp_lexer_consume_token (parser->lexer)->location;
34493 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
34494 return NULL;
34496 init = orig_init = decl = real_decl = NULL;
34497 this_pre_body = push_stmt_list ();
34499 add_private_clause
34500 = cp_parser_omp_for_loop_init (parser, code,
34501 this_pre_body, for_block,
34502 init, orig_init, decl, real_decl);
34504 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
34505 if (this_pre_body)
34507 this_pre_body = pop_stmt_list (this_pre_body);
34508 if (pre_body)
34510 tree t = pre_body;
34511 pre_body = push_stmt_list ();
34512 add_stmt (t);
34513 add_stmt (this_pre_body);
34514 pre_body = pop_stmt_list (pre_body);
34516 else
34517 pre_body = this_pre_body;
34520 if (decl)
34521 real_decl = decl;
34522 if (cclauses != NULL
34523 && cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL] != NULL
34524 && real_decl != NULL_TREE)
34526 tree *c;
34527 for (c = &cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL]; *c ; )
34528 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_FIRSTPRIVATE
34529 && OMP_CLAUSE_DECL (*c) == real_decl)
34531 error_at (loc, "iteration variable %qD"
34532 " should not be firstprivate", real_decl);
34533 *c = OMP_CLAUSE_CHAIN (*c);
34535 else if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_LASTPRIVATE
34536 && OMP_CLAUSE_DECL (*c) == real_decl)
34538 /* Move lastprivate (decl) clause to OMP_FOR_CLAUSES. */
34539 tree l = *c;
34540 *c = OMP_CLAUSE_CHAIN (*c);
34541 if (code == OMP_SIMD)
34543 OMP_CLAUSE_CHAIN (l) = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
34544 cclauses[C_OMP_CLAUSE_SPLIT_FOR] = l;
34546 else
34548 OMP_CLAUSE_CHAIN (l) = clauses;
34549 clauses = l;
34551 add_private_clause = NULL_TREE;
34553 else
34555 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_PRIVATE
34556 && OMP_CLAUSE_DECL (*c) == real_decl)
34557 add_private_clause = NULL_TREE;
34558 c = &OMP_CLAUSE_CHAIN (*c);
34562 if (add_private_clause)
34564 tree c;
34565 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
34567 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
34568 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
34569 && OMP_CLAUSE_DECL (c) == decl)
34570 break;
34571 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
34572 && OMP_CLAUSE_DECL (c) == decl)
34573 error_at (loc, "iteration variable %qD "
34574 "should not be firstprivate",
34575 decl);
34576 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
34577 && OMP_CLAUSE_DECL (c) == decl)
34578 error_at (loc, "iteration variable %qD should not be reduction",
34579 decl);
34581 if (c == NULL)
34583 if (code != OMP_SIMD)
34584 c = build_omp_clause (loc, OMP_CLAUSE_PRIVATE);
34585 else if (collapse == 1)
34586 c = build_omp_clause (loc, OMP_CLAUSE_LINEAR);
34587 else
34588 c = build_omp_clause (loc, OMP_CLAUSE_LASTPRIVATE);
34589 OMP_CLAUSE_DECL (c) = add_private_clause;
34590 c = finish_omp_clauses (c, C_ORT_OMP);
34591 if (c)
34593 OMP_CLAUSE_CHAIN (c) = clauses;
34594 clauses = c;
34595 /* For linear, signal that we need to fill up
34596 the so far unknown linear step. */
34597 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINEAR)
34598 OMP_CLAUSE_LINEAR_STEP (c) = NULL_TREE;
34603 cond = NULL;
34604 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
34605 cond = cp_parser_omp_for_cond (parser, decl, code);
34606 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
34608 incr = NULL;
34609 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
34611 /* If decl is an iterator, preserve the operator on decl
34612 until finish_omp_for. */
34613 if (real_decl
34614 && ((processing_template_decl
34615 && (TREE_TYPE (real_decl) == NULL_TREE
34616 || !POINTER_TYPE_P (TREE_TYPE (real_decl))))
34617 || CLASS_TYPE_P (TREE_TYPE (real_decl))))
34618 incr = cp_parser_omp_for_incr (parser, real_decl);
34619 else
34620 incr = cp_parser_expression (parser);
34621 if (!EXPR_HAS_LOCATION (incr))
34622 protected_set_expr_location (incr, input_location);
34625 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
34626 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
34627 /*or_comma=*/false,
34628 /*consume_paren=*/true);
34630 TREE_VEC_ELT (declv, i) = decl;
34631 TREE_VEC_ELT (initv, i) = init;
34632 TREE_VEC_ELT (condv, i) = cond;
34633 TREE_VEC_ELT (incrv, i) = incr;
34634 if (orig_init)
34636 orig_inits.safe_grow_cleared (i + 1);
34637 orig_inits[i] = orig_init;
34640 if (i == count - 1)
34641 break;
34643 /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
34644 in between the collapsed for loops to be still considered perfectly
34645 nested. Hopefully the final version clarifies this.
34646 For now handle (multiple) {'s and empty statements. */
34647 cp_parser_parse_tentatively (parser);
34648 for (;;)
34650 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
34651 break;
34652 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
34654 cp_lexer_consume_token (parser->lexer);
34655 bracecount++;
34657 else if (bracecount
34658 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
34659 cp_lexer_consume_token (parser->lexer);
34660 else
34662 loc = cp_lexer_peek_token (parser->lexer)->location;
34663 error_at (loc, "not enough for loops to collapse");
34664 collapse_err = true;
34665 cp_parser_abort_tentative_parse (parser);
34666 declv = NULL_TREE;
34667 break;
34671 if (declv)
34673 cp_parser_parse_definitely (parser);
34674 nbraces += bracecount;
34678 if (nbraces)
34679 if_p = NULL;
34681 /* Note that we saved the original contents of this flag when we entered
34682 the structured block, and so we don't need to re-save it here. */
34683 if (code == CILK_SIMD || code == CILK_FOR)
34684 parser->in_statement = IN_CILK_SIMD_FOR;
34685 else
34686 parser->in_statement = IN_OMP_FOR;
34688 /* Note that the grammar doesn't call for a structured block here,
34689 though the loop as a whole is a structured block. */
34690 body = push_stmt_list ();
34691 cp_parser_statement (parser, NULL_TREE, false, if_p);
34692 body = pop_stmt_list (body);
34694 if (declv == NULL_TREE)
34695 ret = NULL_TREE;
34696 else
34697 ret = finish_omp_for (loc_first, code, declv, NULL, initv, condv, incrv,
34698 body, pre_body, &orig_inits, clauses);
34700 while (nbraces)
34702 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
34704 cp_lexer_consume_token (parser->lexer);
34705 nbraces--;
34707 else if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
34708 cp_lexer_consume_token (parser->lexer);
34709 else
34711 if (!collapse_err)
34713 error_at (cp_lexer_peek_token (parser->lexer)->location,
34714 "collapsed loops not perfectly nested");
34716 collapse_err = true;
34717 cp_parser_statement_seq_opt (parser, NULL);
34718 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
34719 break;
34723 while (!for_block->is_empty ())
34725 tree t = for_block->pop ();
34726 if (TREE_CODE (t) == STATEMENT_LIST)
34727 add_stmt (pop_stmt_list (t));
34728 else
34729 add_stmt (t);
34731 release_tree_vector (for_block);
34733 return ret;
34736 /* Helper function for OpenMP parsing, split clauses and call
34737 finish_omp_clauses on each of the set of clauses afterwards. */
34739 static void
34740 cp_omp_split_clauses (location_t loc, enum tree_code code,
34741 omp_clause_mask mask, tree clauses, tree *cclauses)
34743 int i;
34744 c_omp_split_clauses (loc, code, mask, clauses, cclauses);
34745 for (i = 0; i < C_OMP_CLAUSE_SPLIT_COUNT; i++)
34746 if (cclauses[i])
34747 cclauses[i] = finish_omp_clauses (cclauses[i], C_ORT_OMP);
34750 /* OpenMP 4.0:
34751 #pragma omp simd simd-clause[optseq] new-line
34752 for-loop */
34754 #define OMP_SIMD_CLAUSE_MASK \
34755 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SAFELEN) \
34756 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN) \
34757 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
34758 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
34759 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
34760 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
34761 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
34762 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
34764 static tree
34765 cp_parser_omp_simd (cp_parser *parser, cp_token *pragma_tok,
34766 char *p_name, omp_clause_mask mask, tree *cclauses,
34767 bool *if_p)
34769 tree clauses, sb, ret;
34770 unsigned int save;
34771 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
34773 strcat (p_name, " simd");
34774 mask |= OMP_SIMD_CLAUSE_MASK;
34776 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
34777 cclauses == NULL);
34778 if (cclauses)
34780 cp_omp_split_clauses (loc, OMP_SIMD, mask, clauses, cclauses);
34781 clauses = cclauses[C_OMP_CLAUSE_SPLIT_SIMD];
34782 tree c = omp_find_clause (cclauses[C_OMP_CLAUSE_SPLIT_FOR],
34783 OMP_CLAUSE_ORDERED);
34784 if (c && OMP_CLAUSE_ORDERED_EXPR (c))
34786 error_at (OMP_CLAUSE_LOCATION (c),
34787 "%<ordered%> clause with parameter may not be specified "
34788 "on %qs construct", p_name);
34789 OMP_CLAUSE_ORDERED_EXPR (c) = NULL_TREE;
34793 sb = begin_omp_structured_block ();
34794 save = cp_parser_begin_omp_structured_block (parser);
34796 ret = cp_parser_omp_for_loop (parser, OMP_SIMD, clauses, cclauses, if_p);
34798 cp_parser_end_omp_structured_block (parser, save);
34799 add_stmt (finish_omp_structured_block (sb));
34801 return ret;
34804 /* OpenMP 2.5:
34805 #pragma omp for for-clause[optseq] new-line
34806 for-loop
34808 OpenMP 4.0:
34809 #pragma omp for simd for-simd-clause[optseq] new-line
34810 for-loop */
34812 #define OMP_FOR_CLAUSE_MASK \
34813 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
34814 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
34815 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
34816 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
34817 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
34818 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED) \
34819 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SCHEDULE) \
34820 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT) \
34821 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
34823 static tree
34824 cp_parser_omp_for (cp_parser *parser, cp_token *pragma_tok,
34825 char *p_name, omp_clause_mask mask, tree *cclauses,
34826 bool *if_p)
34828 tree clauses, sb, ret;
34829 unsigned int save;
34830 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
34832 strcat (p_name, " for");
34833 mask |= OMP_FOR_CLAUSE_MASK;
34834 /* parallel for{, simd} disallows nowait clause, but for
34835 target {teams distribute ,}parallel for{, simd} it should be accepted. */
34836 if (cclauses && (mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP)) == 0)
34837 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT);
34838 /* Composite distribute parallel for{, simd} disallows ordered clause. */
34839 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
34840 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED);
34842 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
34844 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
34845 const char *p = IDENTIFIER_POINTER (id);
34847 if (strcmp (p, "simd") == 0)
34849 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
34850 if (cclauses == NULL)
34851 cclauses = cclauses_buf;
34853 cp_lexer_consume_token (parser->lexer);
34854 if (!flag_openmp) /* flag_openmp_simd */
34855 return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
34856 cclauses, if_p);
34857 sb = begin_omp_structured_block ();
34858 save = cp_parser_begin_omp_structured_block (parser);
34859 ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
34860 cclauses, if_p);
34861 cp_parser_end_omp_structured_block (parser, save);
34862 tree body = finish_omp_structured_block (sb);
34863 if (ret == NULL)
34864 return ret;
34865 ret = make_node (OMP_FOR);
34866 TREE_TYPE (ret) = void_type_node;
34867 OMP_FOR_BODY (ret) = body;
34868 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
34869 SET_EXPR_LOCATION (ret, loc);
34870 add_stmt (ret);
34871 return ret;
34874 if (!flag_openmp) /* flag_openmp_simd */
34876 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
34877 return NULL_TREE;
34880 /* Composite distribute parallel for disallows linear clause. */
34881 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
34882 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR);
34884 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
34885 cclauses == NULL);
34886 if (cclauses)
34888 cp_omp_split_clauses (loc, OMP_FOR, mask, clauses, cclauses);
34889 clauses = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
34892 sb = begin_omp_structured_block ();
34893 save = cp_parser_begin_omp_structured_block (parser);
34895 ret = cp_parser_omp_for_loop (parser, OMP_FOR, clauses, cclauses, if_p);
34897 cp_parser_end_omp_structured_block (parser, save);
34898 add_stmt (finish_omp_structured_block (sb));
34900 return ret;
34903 /* OpenMP 2.5:
34904 # pragma omp master new-line
34905 structured-block */
34907 static tree
34908 cp_parser_omp_master (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
34910 cp_parser_require_pragma_eol (parser, pragma_tok);
34911 return c_finish_omp_master (input_location,
34912 cp_parser_omp_structured_block (parser, if_p));
34915 /* OpenMP 2.5:
34916 # pragma omp ordered new-line
34917 structured-block
34919 OpenMP 4.5:
34920 # pragma omp ordered ordered-clauses new-line
34921 structured-block */
34923 #define OMP_ORDERED_CLAUSE_MASK \
34924 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREADS) \
34925 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMD))
34927 #define OMP_ORDERED_DEPEND_CLAUSE_MASK \
34928 (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND)
34930 static bool
34931 cp_parser_omp_ordered (cp_parser *parser, cp_token *pragma_tok,
34932 enum pragma_context context, bool *if_p)
34934 location_t loc = pragma_tok->location;
34936 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
34938 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
34939 const char *p = IDENTIFIER_POINTER (id);
34941 if (strcmp (p, "depend") == 0)
34943 if (context == pragma_stmt)
34945 error_at (pragma_tok->location, "%<#pragma omp ordered%> with "
34946 "%<depend%> clause may only be used in compound "
34947 "statements");
34948 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
34949 return false;
34951 tree clauses
34952 = cp_parser_omp_all_clauses (parser,
34953 OMP_ORDERED_DEPEND_CLAUSE_MASK,
34954 "#pragma omp ordered", pragma_tok);
34955 c_finish_omp_ordered (loc, clauses, NULL_TREE);
34956 return false;
34960 tree clauses
34961 = cp_parser_omp_all_clauses (parser, OMP_ORDERED_CLAUSE_MASK,
34962 "#pragma omp ordered", pragma_tok);
34963 c_finish_omp_ordered (loc, clauses,
34964 cp_parser_omp_structured_block (parser, if_p));
34965 return true;
34968 /* OpenMP 2.5:
34970 section-scope:
34971 { section-sequence }
34973 section-sequence:
34974 section-directive[opt] structured-block
34975 section-sequence section-directive structured-block */
34977 static tree
34978 cp_parser_omp_sections_scope (cp_parser *parser)
34980 tree stmt, substmt;
34981 bool error_suppress = false;
34982 cp_token *tok;
34984 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
34985 return NULL_TREE;
34987 stmt = push_stmt_list ();
34989 if (cp_parser_pragma_kind (cp_lexer_peek_token (parser->lexer))
34990 != PRAGMA_OMP_SECTION)
34992 substmt = cp_parser_omp_structured_block (parser, NULL);
34993 substmt = build1 (OMP_SECTION, void_type_node, substmt);
34994 add_stmt (substmt);
34997 while (1)
34999 tok = cp_lexer_peek_token (parser->lexer);
35000 if (tok->type == CPP_CLOSE_BRACE)
35001 break;
35002 if (tok->type == CPP_EOF)
35003 break;
35005 if (cp_parser_pragma_kind (tok) == PRAGMA_OMP_SECTION)
35007 cp_lexer_consume_token (parser->lexer);
35008 cp_parser_require_pragma_eol (parser, tok);
35009 error_suppress = false;
35011 else if (!error_suppress)
35013 cp_parser_error (parser, "expected %<#pragma omp section%> or %<}%>");
35014 error_suppress = true;
35017 substmt = cp_parser_omp_structured_block (parser, NULL);
35018 substmt = build1 (OMP_SECTION, void_type_node, substmt);
35019 add_stmt (substmt);
35021 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
35023 substmt = pop_stmt_list (stmt);
35025 stmt = make_node (OMP_SECTIONS);
35026 TREE_TYPE (stmt) = void_type_node;
35027 OMP_SECTIONS_BODY (stmt) = substmt;
35029 add_stmt (stmt);
35030 return stmt;
35033 /* OpenMP 2.5:
35034 # pragma omp sections sections-clause[optseq] newline
35035 sections-scope */
35037 #define OMP_SECTIONS_CLAUSE_MASK \
35038 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35039 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35040 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
35041 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
35042 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
35044 static tree
35045 cp_parser_omp_sections (cp_parser *parser, cp_token *pragma_tok,
35046 char *p_name, omp_clause_mask mask, tree *cclauses)
35048 tree clauses, ret;
35049 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
35051 strcat (p_name, " sections");
35052 mask |= OMP_SECTIONS_CLAUSE_MASK;
35053 if (cclauses)
35054 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT);
35056 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
35057 cclauses == NULL);
35058 if (cclauses)
35060 cp_omp_split_clauses (loc, OMP_SECTIONS, mask, clauses, cclauses);
35061 clauses = cclauses[C_OMP_CLAUSE_SPLIT_SECTIONS];
35064 ret = cp_parser_omp_sections_scope (parser);
35065 if (ret)
35066 OMP_SECTIONS_CLAUSES (ret) = clauses;
35068 return ret;
35071 /* OpenMP 2.5:
35072 # pragma omp parallel parallel-clause[optseq] new-line
35073 structured-block
35074 # pragma omp parallel for parallel-for-clause[optseq] new-line
35075 structured-block
35076 # pragma omp parallel sections parallel-sections-clause[optseq] new-line
35077 structured-block
35079 OpenMP 4.0:
35080 # pragma omp parallel for simd parallel-for-simd-clause[optseq] new-line
35081 structured-block */
35083 #define OMP_PARALLEL_CLAUSE_MASK \
35084 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
35085 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35086 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35087 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
35088 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
35089 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYIN) \
35090 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
35091 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_THREADS) \
35092 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PROC_BIND))
35094 static tree
35095 cp_parser_omp_parallel (cp_parser *parser, cp_token *pragma_tok,
35096 char *p_name, omp_clause_mask mask, tree *cclauses,
35097 bool *if_p)
35099 tree stmt, clauses, block;
35100 unsigned int save;
35101 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
35103 strcat (p_name, " parallel");
35104 mask |= OMP_PARALLEL_CLAUSE_MASK;
35105 /* #pragma omp target parallel{, for, for simd} disallow copyin clause. */
35106 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP)) != 0
35107 && (mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) == 0)
35108 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYIN);
35110 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
35112 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
35113 if (cclauses == NULL)
35114 cclauses = cclauses_buf;
35116 cp_lexer_consume_token (parser->lexer);
35117 if (!flag_openmp) /* flag_openmp_simd */
35118 return cp_parser_omp_for (parser, pragma_tok, p_name, mask, cclauses,
35119 if_p);
35120 block = begin_omp_parallel ();
35121 save = cp_parser_begin_omp_structured_block (parser);
35122 tree ret = cp_parser_omp_for (parser, pragma_tok, p_name, mask, cclauses,
35123 if_p);
35124 cp_parser_end_omp_structured_block (parser, save);
35125 stmt = finish_omp_parallel (cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
35126 block);
35127 if (ret == NULL_TREE)
35128 return ret;
35129 OMP_PARALLEL_COMBINED (stmt) = 1;
35130 return stmt;
35132 /* When combined with distribute, parallel has to be followed by for.
35133 #pragma omp target parallel is allowed though. */
35134 else if (cclauses
35135 && (mask & (OMP_CLAUSE_MASK_1
35136 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
35138 error_at (loc, "expected %<for%> after %qs", p_name);
35139 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35140 return NULL_TREE;
35142 else if (!flag_openmp) /* flag_openmp_simd */
35144 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35145 return NULL_TREE;
35147 else if (cclauses == NULL && cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35149 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
35150 const char *p = IDENTIFIER_POINTER (id);
35151 if (strcmp (p, "sections") == 0)
35153 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
35154 cclauses = cclauses_buf;
35156 cp_lexer_consume_token (parser->lexer);
35157 block = begin_omp_parallel ();
35158 save = cp_parser_begin_omp_structured_block (parser);
35159 cp_parser_omp_sections (parser, pragma_tok, p_name, mask, cclauses);
35160 cp_parser_end_omp_structured_block (parser, save);
35161 stmt = finish_omp_parallel (cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
35162 block);
35163 OMP_PARALLEL_COMBINED (stmt) = 1;
35164 return stmt;
35168 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
35169 cclauses == NULL);
35170 if (cclauses)
35172 cp_omp_split_clauses (loc, OMP_PARALLEL, mask, clauses, cclauses);
35173 clauses = cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL];
35176 block = begin_omp_parallel ();
35177 save = cp_parser_begin_omp_structured_block (parser);
35178 cp_parser_statement (parser, NULL_TREE, false, if_p);
35179 cp_parser_end_omp_structured_block (parser, save);
35180 stmt = finish_omp_parallel (clauses, block);
35181 return stmt;
35184 /* OpenMP 2.5:
35185 # pragma omp single single-clause[optseq] new-line
35186 structured-block */
35188 #define OMP_SINGLE_CLAUSE_MASK \
35189 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35190 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35191 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \
35192 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
35194 static tree
35195 cp_parser_omp_single (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
35197 tree stmt = make_node (OMP_SINGLE);
35198 TREE_TYPE (stmt) = void_type_node;
35200 OMP_SINGLE_CLAUSES (stmt)
35201 = cp_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
35202 "#pragma omp single", pragma_tok);
35203 OMP_SINGLE_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
35205 return add_stmt (stmt);
35208 /* OpenMP 3.0:
35209 # pragma omp task task-clause[optseq] new-line
35210 structured-block */
35212 #define OMP_TASK_CLAUSE_MASK \
35213 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
35214 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED) \
35215 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
35216 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35217 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35218 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
35219 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL) \
35220 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE) \
35221 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
35222 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIORITY))
35224 static tree
35225 cp_parser_omp_task (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
35227 tree clauses, block;
35228 unsigned int save;
35230 clauses = cp_parser_omp_all_clauses (parser, OMP_TASK_CLAUSE_MASK,
35231 "#pragma omp task", pragma_tok);
35232 block = begin_omp_task ();
35233 save = cp_parser_begin_omp_structured_block (parser);
35234 cp_parser_statement (parser, NULL_TREE, false, if_p);
35235 cp_parser_end_omp_structured_block (parser, save);
35236 return finish_omp_task (clauses, block);
35239 /* OpenMP 3.0:
35240 # pragma omp taskwait new-line */
35242 static void
35243 cp_parser_omp_taskwait (cp_parser *parser, cp_token *pragma_tok)
35245 cp_parser_require_pragma_eol (parser, pragma_tok);
35246 finish_omp_taskwait ();
35249 /* OpenMP 3.1:
35250 # pragma omp taskyield new-line */
35252 static void
35253 cp_parser_omp_taskyield (cp_parser *parser, cp_token *pragma_tok)
35255 cp_parser_require_pragma_eol (parser, pragma_tok);
35256 finish_omp_taskyield ();
35259 /* OpenMP 4.0:
35260 # pragma omp taskgroup new-line
35261 structured-block */
35263 static tree
35264 cp_parser_omp_taskgroup (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
35266 cp_parser_require_pragma_eol (parser, pragma_tok);
35267 return c_finish_omp_taskgroup (input_location,
35268 cp_parser_omp_structured_block (parser,
35269 if_p));
35273 /* OpenMP 2.5:
35274 # pragma omp threadprivate (variable-list) */
35276 static void
35277 cp_parser_omp_threadprivate (cp_parser *parser, cp_token *pragma_tok)
35279 tree vars;
35281 vars = cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
35282 cp_parser_require_pragma_eol (parser, pragma_tok);
35284 finish_omp_threadprivate (vars);
35287 /* OpenMP 4.0:
35288 # pragma omp cancel cancel-clause[optseq] new-line */
35290 #define OMP_CANCEL_CLAUSE_MASK \
35291 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
35292 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
35293 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
35294 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP) \
35295 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
35297 static void
35298 cp_parser_omp_cancel (cp_parser *parser, cp_token *pragma_tok)
35300 tree clauses = cp_parser_omp_all_clauses (parser, OMP_CANCEL_CLAUSE_MASK,
35301 "#pragma omp cancel", pragma_tok);
35302 finish_omp_cancel (clauses);
35305 /* OpenMP 4.0:
35306 # pragma omp cancellation point cancelpt-clause[optseq] new-line */
35308 #define OMP_CANCELLATION_POINT_CLAUSE_MASK \
35309 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
35310 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
35311 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
35312 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP))
35314 static void
35315 cp_parser_omp_cancellation_point (cp_parser *parser, cp_token *pragma_tok,
35316 enum pragma_context context)
35318 tree clauses;
35319 bool point_seen = false;
35321 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35323 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
35324 const char *p = IDENTIFIER_POINTER (id);
35326 if (strcmp (p, "point") == 0)
35328 cp_lexer_consume_token (parser->lexer);
35329 point_seen = true;
35332 if (!point_seen)
35334 cp_parser_error (parser, "expected %<point%>");
35335 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35336 return;
35339 if (context != pragma_compound)
35341 if (context == pragma_stmt)
35342 error_at (pragma_tok->location,
35343 "%<#pragma omp cancellation point%> may only be used in"
35344 " compound statements");
35345 else
35346 cp_parser_error (parser, "expected declaration specifiers");
35347 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35348 return;
35351 clauses = cp_parser_omp_all_clauses (parser,
35352 OMP_CANCELLATION_POINT_CLAUSE_MASK,
35353 "#pragma omp cancellation point",
35354 pragma_tok);
35355 finish_omp_cancellation_point (clauses);
35358 /* OpenMP 4.0:
35359 #pragma omp distribute distribute-clause[optseq] new-line
35360 for-loop */
35362 #define OMP_DISTRIBUTE_CLAUSE_MASK \
35363 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35364 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35365 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
35366 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)\
35367 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
35369 static tree
35370 cp_parser_omp_distribute (cp_parser *parser, cp_token *pragma_tok,
35371 char *p_name, omp_clause_mask mask, tree *cclauses,
35372 bool *if_p)
35374 tree clauses, sb, ret;
35375 unsigned int save;
35376 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
35378 strcat (p_name, " distribute");
35379 mask |= OMP_DISTRIBUTE_CLAUSE_MASK;
35381 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35383 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
35384 const char *p = IDENTIFIER_POINTER (id);
35385 bool simd = false;
35386 bool parallel = false;
35388 if (strcmp (p, "simd") == 0)
35389 simd = true;
35390 else
35391 parallel = strcmp (p, "parallel") == 0;
35392 if (parallel || simd)
35394 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
35395 if (cclauses == NULL)
35396 cclauses = cclauses_buf;
35397 cp_lexer_consume_token (parser->lexer);
35398 if (!flag_openmp) /* flag_openmp_simd */
35400 if (simd)
35401 return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
35402 cclauses, if_p);
35403 else
35404 return cp_parser_omp_parallel (parser, pragma_tok, p_name, mask,
35405 cclauses, if_p);
35407 sb = begin_omp_structured_block ();
35408 save = cp_parser_begin_omp_structured_block (parser);
35409 if (simd)
35410 ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
35411 cclauses, if_p);
35412 else
35413 ret = cp_parser_omp_parallel (parser, pragma_tok, p_name, mask,
35414 cclauses, if_p);
35415 cp_parser_end_omp_structured_block (parser, save);
35416 tree body = finish_omp_structured_block (sb);
35417 if (ret == NULL)
35418 return ret;
35419 ret = make_node (OMP_DISTRIBUTE);
35420 TREE_TYPE (ret) = void_type_node;
35421 OMP_FOR_BODY (ret) = body;
35422 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_DISTRIBUTE];
35423 SET_EXPR_LOCATION (ret, loc);
35424 add_stmt (ret);
35425 return ret;
35428 if (!flag_openmp) /* flag_openmp_simd */
35430 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35431 return NULL_TREE;
35434 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
35435 cclauses == NULL);
35436 if (cclauses)
35438 cp_omp_split_clauses (loc, OMP_DISTRIBUTE, mask, clauses, cclauses);
35439 clauses = cclauses[C_OMP_CLAUSE_SPLIT_DISTRIBUTE];
35442 sb = begin_omp_structured_block ();
35443 save = cp_parser_begin_omp_structured_block (parser);
35445 ret = cp_parser_omp_for_loop (parser, OMP_DISTRIBUTE, clauses, NULL, if_p);
35447 cp_parser_end_omp_structured_block (parser, save);
35448 add_stmt (finish_omp_structured_block (sb));
35450 return ret;
35453 /* OpenMP 4.0:
35454 # pragma omp teams teams-clause[optseq] new-line
35455 structured-block */
35457 #define OMP_TEAMS_CLAUSE_MASK \
35458 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35459 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35460 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
35461 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
35462 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TEAMS) \
35463 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREAD_LIMIT) \
35464 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT))
35466 static tree
35467 cp_parser_omp_teams (cp_parser *parser, cp_token *pragma_tok,
35468 char *p_name, omp_clause_mask mask, tree *cclauses,
35469 bool *if_p)
35471 tree clauses, sb, ret;
35472 unsigned int save;
35473 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
35475 strcat (p_name, " teams");
35476 mask |= OMP_TEAMS_CLAUSE_MASK;
35478 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35480 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
35481 const char *p = IDENTIFIER_POINTER (id);
35482 if (strcmp (p, "distribute") == 0)
35484 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
35485 if (cclauses == NULL)
35486 cclauses = cclauses_buf;
35488 cp_lexer_consume_token (parser->lexer);
35489 if (!flag_openmp) /* flag_openmp_simd */
35490 return cp_parser_omp_distribute (parser, pragma_tok, p_name, mask,
35491 cclauses, if_p);
35492 sb = begin_omp_structured_block ();
35493 save = cp_parser_begin_omp_structured_block (parser);
35494 ret = cp_parser_omp_distribute (parser, pragma_tok, p_name, mask,
35495 cclauses, if_p);
35496 cp_parser_end_omp_structured_block (parser, save);
35497 tree body = finish_omp_structured_block (sb);
35498 if (ret == NULL)
35499 return ret;
35500 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
35501 ret = make_node (OMP_TEAMS);
35502 TREE_TYPE (ret) = void_type_node;
35503 OMP_TEAMS_CLAUSES (ret) = clauses;
35504 OMP_TEAMS_BODY (ret) = body;
35505 OMP_TEAMS_COMBINED (ret) = 1;
35506 SET_EXPR_LOCATION (ret, loc);
35507 return add_stmt (ret);
35510 if (!flag_openmp) /* flag_openmp_simd */
35512 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35513 return NULL_TREE;
35516 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
35517 cclauses == NULL);
35518 if (cclauses)
35520 cp_omp_split_clauses (loc, OMP_TEAMS, mask, clauses, cclauses);
35521 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
35524 tree stmt = make_node (OMP_TEAMS);
35525 TREE_TYPE (stmt) = void_type_node;
35526 OMP_TEAMS_CLAUSES (stmt) = clauses;
35527 OMP_TEAMS_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
35528 SET_EXPR_LOCATION (stmt, loc);
35530 return add_stmt (stmt);
35533 /* OpenMP 4.0:
35534 # pragma omp target data target-data-clause[optseq] new-line
35535 structured-block */
35537 #define OMP_TARGET_DATA_CLAUSE_MASK \
35538 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
35539 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
35540 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
35541 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR))
35543 static tree
35544 cp_parser_omp_target_data (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
35546 tree clauses
35547 = cp_parser_omp_all_clauses (parser, OMP_TARGET_DATA_CLAUSE_MASK,
35548 "#pragma omp target data", pragma_tok);
35549 int map_seen = 0;
35550 for (tree *pc = &clauses; *pc;)
35552 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
35553 switch (OMP_CLAUSE_MAP_KIND (*pc))
35555 case GOMP_MAP_TO:
35556 case GOMP_MAP_ALWAYS_TO:
35557 case GOMP_MAP_FROM:
35558 case GOMP_MAP_ALWAYS_FROM:
35559 case GOMP_MAP_TOFROM:
35560 case GOMP_MAP_ALWAYS_TOFROM:
35561 case GOMP_MAP_ALLOC:
35562 map_seen = 3;
35563 break;
35564 case GOMP_MAP_FIRSTPRIVATE_POINTER:
35565 case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
35566 case GOMP_MAP_ALWAYS_POINTER:
35567 break;
35568 default:
35569 map_seen |= 1;
35570 error_at (OMP_CLAUSE_LOCATION (*pc),
35571 "%<#pragma omp target data%> with map-type other "
35572 "than %<to%>, %<from%>, %<tofrom%> or %<alloc%> "
35573 "on %<map%> clause");
35574 *pc = OMP_CLAUSE_CHAIN (*pc);
35575 continue;
35577 pc = &OMP_CLAUSE_CHAIN (*pc);
35580 if (map_seen != 3)
35582 if (map_seen == 0)
35583 error_at (pragma_tok->location,
35584 "%<#pragma omp target data%> must contain at least "
35585 "one %<map%> clause");
35586 return NULL_TREE;
35589 tree stmt = make_node (OMP_TARGET_DATA);
35590 TREE_TYPE (stmt) = void_type_node;
35591 OMP_TARGET_DATA_CLAUSES (stmt) = clauses;
35593 keep_next_level (true);
35594 OMP_TARGET_DATA_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
35596 SET_EXPR_LOCATION (stmt, pragma_tok->location);
35597 return add_stmt (stmt);
35600 /* OpenMP 4.5:
35601 # pragma omp target enter data target-enter-data-clause[optseq] new-line
35602 structured-block */
35604 #define OMP_TARGET_ENTER_DATA_CLAUSE_MASK \
35605 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
35606 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
35607 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
35608 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
35609 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
35611 static tree
35612 cp_parser_omp_target_enter_data (cp_parser *parser, cp_token *pragma_tok,
35613 enum pragma_context context)
35615 bool data_seen = false;
35616 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35618 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
35619 const char *p = IDENTIFIER_POINTER (id);
35621 if (strcmp (p, "data") == 0)
35623 cp_lexer_consume_token (parser->lexer);
35624 data_seen = true;
35627 if (!data_seen)
35629 cp_parser_error (parser, "expected %<data%>");
35630 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35631 return NULL_TREE;
35634 if (context == pragma_stmt)
35636 error_at (pragma_tok->location,
35637 "%<#pragma omp target enter data%> may only be "
35638 "used in compound statements");
35639 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35640 return NULL_TREE;
35643 tree clauses
35644 = cp_parser_omp_all_clauses (parser, OMP_TARGET_ENTER_DATA_CLAUSE_MASK,
35645 "#pragma omp target enter data", pragma_tok);
35646 int map_seen = 0;
35647 for (tree *pc = &clauses; *pc;)
35649 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
35650 switch (OMP_CLAUSE_MAP_KIND (*pc))
35652 case GOMP_MAP_TO:
35653 case GOMP_MAP_ALWAYS_TO:
35654 case GOMP_MAP_ALLOC:
35655 map_seen = 3;
35656 break;
35657 case GOMP_MAP_FIRSTPRIVATE_POINTER:
35658 case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
35659 case GOMP_MAP_ALWAYS_POINTER:
35660 break;
35661 default:
35662 map_seen |= 1;
35663 error_at (OMP_CLAUSE_LOCATION (*pc),
35664 "%<#pragma omp target enter data%> with map-type other "
35665 "than %<to%> or %<alloc%> on %<map%> clause");
35666 *pc = OMP_CLAUSE_CHAIN (*pc);
35667 continue;
35669 pc = &OMP_CLAUSE_CHAIN (*pc);
35672 if (map_seen != 3)
35674 if (map_seen == 0)
35675 error_at (pragma_tok->location,
35676 "%<#pragma omp target enter data%> must contain at least "
35677 "one %<map%> clause");
35678 return NULL_TREE;
35681 tree stmt = make_node (OMP_TARGET_ENTER_DATA);
35682 TREE_TYPE (stmt) = void_type_node;
35683 OMP_TARGET_ENTER_DATA_CLAUSES (stmt) = clauses;
35684 SET_EXPR_LOCATION (stmt, pragma_tok->location);
35685 return add_stmt (stmt);
35688 /* OpenMP 4.5:
35689 # pragma omp target exit data target-enter-data-clause[optseq] new-line
35690 structured-block */
35692 #define OMP_TARGET_EXIT_DATA_CLAUSE_MASK \
35693 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
35694 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
35695 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
35696 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
35697 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
35699 static tree
35700 cp_parser_omp_target_exit_data (cp_parser *parser, cp_token *pragma_tok,
35701 enum pragma_context context)
35703 bool data_seen = false;
35704 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35706 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
35707 const char *p = IDENTIFIER_POINTER (id);
35709 if (strcmp (p, "data") == 0)
35711 cp_lexer_consume_token (parser->lexer);
35712 data_seen = true;
35715 if (!data_seen)
35717 cp_parser_error (parser, "expected %<data%>");
35718 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35719 return NULL_TREE;
35722 if (context == pragma_stmt)
35724 error_at (pragma_tok->location,
35725 "%<#pragma omp target exit data%> may only be "
35726 "used in compound statements");
35727 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35728 return NULL_TREE;
35731 tree clauses
35732 = cp_parser_omp_all_clauses (parser, OMP_TARGET_EXIT_DATA_CLAUSE_MASK,
35733 "#pragma omp target exit data", pragma_tok);
35734 int map_seen = 0;
35735 for (tree *pc = &clauses; *pc;)
35737 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
35738 switch (OMP_CLAUSE_MAP_KIND (*pc))
35740 case GOMP_MAP_FROM:
35741 case GOMP_MAP_ALWAYS_FROM:
35742 case GOMP_MAP_RELEASE:
35743 case GOMP_MAP_DELETE:
35744 map_seen = 3;
35745 break;
35746 case GOMP_MAP_FIRSTPRIVATE_POINTER:
35747 case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
35748 case GOMP_MAP_ALWAYS_POINTER:
35749 break;
35750 default:
35751 map_seen |= 1;
35752 error_at (OMP_CLAUSE_LOCATION (*pc),
35753 "%<#pragma omp target exit data%> with map-type other "
35754 "than %<from%>, %<release%> or %<delete%> on %<map%>"
35755 " clause");
35756 *pc = OMP_CLAUSE_CHAIN (*pc);
35757 continue;
35759 pc = &OMP_CLAUSE_CHAIN (*pc);
35762 if (map_seen != 3)
35764 if (map_seen == 0)
35765 error_at (pragma_tok->location,
35766 "%<#pragma omp target exit data%> must contain at least "
35767 "one %<map%> clause");
35768 return NULL_TREE;
35771 tree stmt = make_node (OMP_TARGET_EXIT_DATA);
35772 TREE_TYPE (stmt) = void_type_node;
35773 OMP_TARGET_EXIT_DATA_CLAUSES (stmt) = clauses;
35774 SET_EXPR_LOCATION (stmt, pragma_tok->location);
35775 return add_stmt (stmt);
35778 /* OpenMP 4.0:
35779 # pragma omp target update target-update-clause[optseq] new-line */
35781 #define OMP_TARGET_UPDATE_CLAUSE_MASK \
35782 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FROM) \
35783 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO) \
35784 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
35785 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
35786 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
35787 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
35789 static bool
35790 cp_parser_omp_target_update (cp_parser *parser, cp_token *pragma_tok,
35791 enum pragma_context context)
35793 if (context == pragma_stmt)
35795 error_at (pragma_tok->location,
35796 "%<#pragma omp target update%> may only be "
35797 "used in compound statements");
35798 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35799 return false;
35802 tree clauses
35803 = cp_parser_omp_all_clauses (parser, OMP_TARGET_UPDATE_CLAUSE_MASK,
35804 "#pragma omp target update", pragma_tok);
35805 if (omp_find_clause (clauses, OMP_CLAUSE_TO) == NULL_TREE
35806 && omp_find_clause (clauses, OMP_CLAUSE_FROM) == NULL_TREE)
35808 error_at (pragma_tok->location,
35809 "%<#pragma omp target update%> must contain at least one "
35810 "%<from%> or %<to%> clauses");
35811 return false;
35814 tree stmt = make_node (OMP_TARGET_UPDATE);
35815 TREE_TYPE (stmt) = void_type_node;
35816 OMP_TARGET_UPDATE_CLAUSES (stmt) = clauses;
35817 SET_EXPR_LOCATION (stmt, pragma_tok->location);
35818 add_stmt (stmt);
35819 return false;
35822 /* OpenMP 4.0:
35823 # pragma omp target target-clause[optseq] new-line
35824 structured-block */
35826 #define OMP_TARGET_CLAUSE_MASK \
35827 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
35828 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
35829 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
35830 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
35831 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT) \
35832 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35833 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35834 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULTMAP) \
35835 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR))
35837 static bool
35838 cp_parser_omp_target (cp_parser *parser, cp_token *pragma_tok,
35839 enum pragma_context context, bool *if_p)
35841 tree *pc = NULL, stmt;
35843 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35845 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
35846 const char *p = IDENTIFIER_POINTER (id);
35847 enum tree_code ccode = ERROR_MARK;
35849 if (strcmp (p, "teams") == 0)
35850 ccode = OMP_TEAMS;
35851 else if (strcmp (p, "parallel") == 0)
35852 ccode = OMP_PARALLEL;
35853 else if (strcmp (p, "simd") == 0)
35854 ccode = OMP_SIMD;
35855 if (ccode != ERROR_MARK)
35857 tree cclauses[C_OMP_CLAUSE_SPLIT_COUNT];
35858 char p_name[sizeof ("#pragma omp target teams distribute "
35859 "parallel for simd")];
35861 cp_lexer_consume_token (parser->lexer);
35862 strcpy (p_name, "#pragma omp target");
35863 if (!flag_openmp) /* flag_openmp_simd */
35865 tree stmt;
35866 switch (ccode)
35868 case OMP_TEAMS:
35869 stmt = cp_parser_omp_teams (parser, pragma_tok, p_name,
35870 OMP_TARGET_CLAUSE_MASK,
35871 cclauses, if_p);
35872 break;
35873 case OMP_PARALLEL:
35874 stmt = cp_parser_omp_parallel (parser, pragma_tok, p_name,
35875 OMP_TARGET_CLAUSE_MASK,
35876 cclauses, if_p);
35877 break;
35878 case OMP_SIMD:
35879 stmt = cp_parser_omp_simd (parser, pragma_tok, p_name,
35880 OMP_TARGET_CLAUSE_MASK,
35881 cclauses, if_p);
35882 break;
35883 default:
35884 gcc_unreachable ();
35886 return stmt != NULL_TREE;
35888 keep_next_level (true);
35889 tree sb = begin_omp_structured_block (), ret;
35890 unsigned save = cp_parser_begin_omp_structured_block (parser);
35891 switch (ccode)
35893 case OMP_TEAMS:
35894 ret = cp_parser_omp_teams (parser, pragma_tok, p_name,
35895 OMP_TARGET_CLAUSE_MASK, cclauses,
35896 if_p);
35897 break;
35898 case OMP_PARALLEL:
35899 ret = cp_parser_omp_parallel (parser, pragma_tok, p_name,
35900 OMP_TARGET_CLAUSE_MASK, cclauses,
35901 if_p);
35902 break;
35903 case OMP_SIMD:
35904 ret = cp_parser_omp_simd (parser, pragma_tok, p_name,
35905 OMP_TARGET_CLAUSE_MASK, cclauses,
35906 if_p);
35907 break;
35908 default:
35909 gcc_unreachable ();
35911 cp_parser_end_omp_structured_block (parser, save);
35912 tree body = finish_omp_structured_block (sb);
35913 if (ret == NULL_TREE)
35914 return false;
35915 if (ccode == OMP_TEAMS && !processing_template_decl)
35917 /* For combined target teams, ensure the num_teams and
35918 thread_limit clause expressions are evaluated on the host,
35919 before entering the target construct. */
35920 tree c;
35921 for (c = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
35922 c; c = OMP_CLAUSE_CHAIN (c))
35923 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_NUM_TEAMS
35924 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_THREAD_LIMIT)
35925 && TREE_CODE (OMP_CLAUSE_OPERAND (c, 0)) != INTEGER_CST)
35927 tree expr = OMP_CLAUSE_OPERAND (c, 0);
35928 expr = force_target_expr (TREE_TYPE (expr), expr, tf_none);
35929 if (expr == error_mark_node)
35930 continue;
35931 tree tmp = TARGET_EXPR_SLOT (expr);
35932 add_stmt (expr);
35933 OMP_CLAUSE_OPERAND (c, 0) = expr;
35934 tree tc = build_omp_clause (OMP_CLAUSE_LOCATION (c),
35935 OMP_CLAUSE_FIRSTPRIVATE);
35936 OMP_CLAUSE_DECL (tc) = tmp;
35937 OMP_CLAUSE_CHAIN (tc)
35938 = cclauses[C_OMP_CLAUSE_SPLIT_TARGET];
35939 cclauses[C_OMP_CLAUSE_SPLIT_TARGET] = tc;
35942 tree stmt = make_node (OMP_TARGET);
35943 TREE_TYPE (stmt) = void_type_node;
35944 OMP_TARGET_CLAUSES (stmt) = cclauses[C_OMP_CLAUSE_SPLIT_TARGET];
35945 OMP_TARGET_BODY (stmt) = body;
35946 OMP_TARGET_COMBINED (stmt) = 1;
35947 SET_EXPR_LOCATION (stmt, pragma_tok->location);
35948 add_stmt (stmt);
35949 pc = &OMP_TARGET_CLAUSES (stmt);
35950 goto check_clauses;
35952 else if (!flag_openmp) /* flag_openmp_simd */
35954 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35955 return false;
35957 else if (strcmp (p, "data") == 0)
35959 cp_lexer_consume_token (parser->lexer);
35960 cp_parser_omp_target_data (parser, pragma_tok, if_p);
35961 return true;
35963 else if (strcmp (p, "enter") == 0)
35965 cp_lexer_consume_token (parser->lexer);
35966 cp_parser_omp_target_enter_data (parser, pragma_tok, context);
35967 return false;
35969 else if (strcmp (p, "exit") == 0)
35971 cp_lexer_consume_token (parser->lexer);
35972 cp_parser_omp_target_exit_data (parser, pragma_tok, context);
35973 return false;
35975 else if (strcmp (p, "update") == 0)
35977 cp_lexer_consume_token (parser->lexer);
35978 return cp_parser_omp_target_update (parser, pragma_tok, context);
35981 if (!flag_openmp) /* flag_openmp_simd */
35983 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35984 return false;
35987 stmt = make_node (OMP_TARGET);
35988 TREE_TYPE (stmt) = void_type_node;
35990 OMP_TARGET_CLAUSES (stmt)
35991 = cp_parser_omp_all_clauses (parser, OMP_TARGET_CLAUSE_MASK,
35992 "#pragma omp target", pragma_tok);
35993 pc = &OMP_TARGET_CLAUSES (stmt);
35994 keep_next_level (true);
35995 OMP_TARGET_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
35997 SET_EXPR_LOCATION (stmt, pragma_tok->location);
35998 add_stmt (stmt);
36000 check_clauses:
36001 while (*pc)
36003 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
36004 switch (OMP_CLAUSE_MAP_KIND (*pc))
36006 case GOMP_MAP_TO:
36007 case GOMP_MAP_ALWAYS_TO:
36008 case GOMP_MAP_FROM:
36009 case GOMP_MAP_ALWAYS_FROM:
36010 case GOMP_MAP_TOFROM:
36011 case GOMP_MAP_ALWAYS_TOFROM:
36012 case GOMP_MAP_ALLOC:
36013 case GOMP_MAP_FIRSTPRIVATE_POINTER:
36014 case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
36015 case GOMP_MAP_ALWAYS_POINTER:
36016 break;
36017 default:
36018 error_at (OMP_CLAUSE_LOCATION (*pc),
36019 "%<#pragma omp target%> with map-type other "
36020 "than %<to%>, %<from%>, %<tofrom%> or %<alloc%> "
36021 "on %<map%> clause");
36022 *pc = OMP_CLAUSE_CHAIN (*pc);
36023 continue;
36025 pc = &OMP_CLAUSE_CHAIN (*pc);
36027 return true;
36030 /* OpenACC 2.0:
36031 # pragma acc cache (variable-list) new-line
36034 static tree
36035 cp_parser_oacc_cache (cp_parser *parser, cp_token *pragma_tok)
36037 tree stmt, clauses;
36039 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE__CACHE_, NULL_TREE);
36040 clauses = finish_omp_clauses (clauses, C_ORT_ACC);
36042 cp_parser_require_pragma_eol (parser, cp_lexer_peek_token (parser->lexer));
36044 stmt = make_node (OACC_CACHE);
36045 TREE_TYPE (stmt) = void_type_node;
36046 OACC_CACHE_CLAUSES (stmt) = clauses;
36047 SET_EXPR_LOCATION (stmt, pragma_tok->location);
36048 add_stmt (stmt);
36050 return stmt;
36053 /* OpenACC 2.0:
36054 # pragma acc data oacc-data-clause[optseq] new-line
36055 structured-block */
36057 #define OACC_DATA_CLAUSE_MASK \
36058 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
36059 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
36060 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
36061 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
36062 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
36063 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
36064 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
36065 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
36066 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
36067 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
36068 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE))
36070 static tree
36071 cp_parser_oacc_data (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
36073 tree stmt, clauses, block;
36074 unsigned int save;
36076 clauses = cp_parser_oacc_all_clauses (parser, OACC_DATA_CLAUSE_MASK,
36077 "#pragma acc data", pragma_tok);
36079 block = begin_omp_parallel ();
36080 save = cp_parser_begin_omp_structured_block (parser);
36081 cp_parser_statement (parser, NULL_TREE, false, if_p);
36082 cp_parser_end_omp_structured_block (parser, save);
36083 stmt = finish_oacc_data (clauses, block);
36084 return stmt;
36087 /* OpenACC 2.0:
36088 # pragma acc host_data <clauses> new-line
36089 structured-block */
36091 #define OACC_HOST_DATA_CLAUSE_MASK \
36092 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_USE_DEVICE) )
36094 static tree
36095 cp_parser_oacc_host_data (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
36097 tree stmt, clauses, block;
36098 unsigned int save;
36100 clauses = cp_parser_oacc_all_clauses (parser, OACC_HOST_DATA_CLAUSE_MASK,
36101 "#pragma acc host_data", pragma_tok);
36103 block = begin_omp_parallel ();
36104 save = cp_parser_begin_omp_structured_block (parser);
36105 cp_parser_statement (parser, NULL_TREE, false, if_p);
36106 cp_parser_end_omp_structured_block (parser, save);
36107 stmt = finish_oacc_host_data (clauses, block);
36108 return stmt;
36111 /* OpenACC 2.0:
36112 # pragma acc declare oacc-data-clause[optseq] new-line
36115 #define OACC_DECLARE_CLAUSE_MASK \
36116 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
36117 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
36118 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
36119 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
36120 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
36121 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT) \
36122 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_LINK) \
36123 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
36124 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
36125 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
36126 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
36127 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE))
36129 static tree
36130 cp_parser_oacc_declare (cp_parser *parser, cp_token *pragma_tok)
36132 tree clauses, stmt;
36133 bool error = false;
36135 clauses = cp_parser_oacc_all_clauses (parser, OACC_DECLARE_CLAUSE_MASK,
36136 "#pragma acc declare", pragma_tok, true);
36139 if (omp_find_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
36141 error_at (pragma_tok->location,
36142 "no valid clauses specified in %<#pragma acc declare%>");
36143 return NULL_TREE;
36146 for (tree t = clauses; t; t = OMP_CLAUSE_CHAIN (t))
36148 location_t loc = OMP_CLAUSE_LOCATION (t);
36149 tree decl = OMP_CLAUSE_DECL (t);
36150 if (!DECL_P (decl))
36152 error_at (loc, "array section in %<#pragma acc declare%>");
36153 error = true;
36154 continue;
36156 gcc_assert (OMP_CLAUSE_CODE (t) == OMP_CLAUSE_MAP);
36157 switch (OMP_CLAUSE_MAP_KIND (t))
36159 case GOMP_MAP_FIRSTPRIVATE_POINTER:
36160 case GOMP_MAP_FORCE_ALLOC:
36161 case GOMP_MAP_FORCE_TO:
36162 case GOMP_MAP_FORCE_DEVICEPTR:
36163 case GOMP_MAP_DEVICE_RESIDENT:
36164 break;
36166 case GOMP_MAP_LINK:
36167 if (!global_bindings_p ()
36168 && (TREE_STATIC (decl)
36169 || !DECL_EXTERNAL (decl)))
36171 error_at (loc,
36172 "%qD must be a global variable in "
36173 "%<#pragma acc declare link%>",
36174 decl);
36175 error = true;
36176 continue;
36178 break;
36180 default:
36181 if (global_bindings_p ())
36183 error_at (loc, "invalid OpenACC clause at file scope");
36184 error = true;
36185 continue;
36187 if (DECL_EXTERNAL (decl))
36189 error_at (loc,
36190 "invalid use of %<extern%> variable %qD "
36191 "in %<#pragma acc declare%>", decl);
36192 error = true;
36193 continue;
36195 else if (TREE_PUBLIC (decl))
36197 error_at (loc,
36198 "invalid use of %<global%> variable %qD "
36199 "in %<#pragma acc declare%>", decl);
36200 error = true;
36201 continue;
36203 break;
36206 if (lookup_attribute ("omp declare target", DECL_ATTRIBUTES (decl))
36207 || lookup_attribute ("omp declare target link",
36208 DECL_ATTRIBUTES (decl)))
36210 error_at (loc, "variable %qD used more than once with "
36211 "%<#pragma acc declare%>", decl);
36212 error = true;
36213 continue;
36216 if (!error)
36218 tree id;
36220 if (OMP_CLAUSE_MAP_KIND (t) == GOMP_MAP_LINK)
36221 id = get_identifier ("omp declare target link");
36222 else
36223 id = get_identifier ("omp declare target");
36225 DECL_ATTRIBUTES (decl)
36226 = tree_cons (id, NULL_TREE, DECL_ATTRIBUTES (decl));
36227 if (global_bindings_p ())
36229 symtab_node *node = symtab_node::get (decl);
36230 if (node != NULL)
36232 node->offloadable = 1;
36233 if (ENABLE_OFFLOADING)
36235 g->have_offload = true;
36236 if (is_a <varpool_node *> (node))
36237 vec_safe_push (offload_vars, decl);
36244 if (error || global_bindings_p ())
36245 return NULL_TREE;
36247 stmt = make_node (OACC_DECLARE);
36248 TREE_TYPE (stmt) = void_type_node;
36249 OACC_DECLARE_CLAUSES (stmt) = clauses;
36250 SET_EXPR_LOCATION (stmt, pragma_tok->location);
36252 add_stmt (stmt);
36254 return NULL_TREE;
36257 /* OpenACC 2.0:
36258 # pragma acc enter data oacc-enter-data-clause[optseq] new-line
36262 # pragma acc exit data oacc-exit-data-clause[optseq] new-line
36264 LOC is the location of the #pragma token.
36267 #define OACC_ENTER_DATA_CLAUSE_MASK \
36268 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
36269 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
36270 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
36271 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
36272 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
36273 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE) \
36274 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
36276 #define OACC_EXIT_DATA_CLAUSE_MASK \
36277 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
36278 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
36279 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
36280 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DELETE) \
36281 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
36283 static tree
36284 cp_parser_oacc_enter_exit_data (cp_parser *parser, cp_token *pragma_tok,
36285 bool enter)
36287 location_t loc = pragma_tok->location;
36288 tree stmt, clauses;
36289 const char *p = "";
36291 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36292 p = IDENTIFIER_POINTER (cp_lexer_peek_token (parser->lexer)->u.value);
36294 if (strcmp (p, "data") != 0)
36296 error_at (loc, "expected %<data%> after %<#pragma acc %s%>",
36297 enter ? "enter" : "exit");
36298 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36299 return NULL_TREE;
36302 cp_lexer_consume_token (parser->lexer);
36304 if (enter)
36305 clauses = cp_parser_oacc_all_clauses (parser, OACC_ENTER_DATA_CLAUSE_MASK,
36306 "#pragma acc enter data", pragma_tok);
36307 else
36308 clauses = cp_parser_oacc_all_clauses (parser, OACC_EXIT_DATA_CLAUSE_MASK,
36309 "#pragma acc exit data", pragma_tok);
36311 if (omp_find_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
36313 error_at (loc, "%<#pragma acc %s data%> has no data movement clause",
36314 enter ? "enter" : "exit");
36315 return NULL_TREE;
36318 stmt = enter ? make_node (OACC_ENTER_DATA) : make_node (OACC_EXIT_DATA);
36319 TREE_TYPE (stmt) = void_type_node;
36320 OMP_STANDALONE_CLAUSES (stmt) = clauses;
36321 SET_EXPR_LOCATION (stmt, pragma_tok->location);
36322 add_stmt (stmt);
36323 return stmt;
36326 /* OpenACC 2.0:
36327 # pragma acc loop oacc-loop-clause[optseq] new-line
36328 structured-block */
36330 #define OACC_LOOP_CLAUSE_MASK \
36331 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COLLAPSE) \
36332 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRIVATE) \
36333 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION) \
36334 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_GANG) \
36335 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR) \
36336 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WORKER) \
36337 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_AUTO) \
36338 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_INDEPENDENT) \
36339 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SEQ) \
36340 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_TILE))
36342 static tree
36343 cp_parser_oacc_loop (cp_parser *parser, cp_token *pragma_tok, char *p_name,
36344 omp_clause_mask mask, tree *cclauses, bool *if_p)
36346 bool is_parallel = ((mask >> PRAGMA_OACC_CLAUSE_REDUCTION) & 1) == 1;
36348 strcat (p_name, " loop");
36349 mask |= OACC_LOOP_CLAUSE_MASK;
36351 tree clauses = cp_parser_oacc_all_clauses (parser, mask, p_name, pragma_tok,
36352 cclauses == NULL);
36353 if (cclauses)
36355 clauses = c_oacc_split_loop_clauses (clauses, cclauses, is_parallel);
36356 if (*cclauses)
36357 *cclauses = finish_omp_clauses (*cclauses, C_ORT_ACC);
36358 if (clauses)
36359 clauses = finish_omp_clauses (clauses, C_ORT_ACC);
36362 tree block = begin_omp_structured_block ();
36363 int save = cp_parser_begin_omp_structured_block (parser);
36364 tree stmt = cp_parser_omp_for_loop (parser, OACC_LOOP, clauses, NULL, if_p);
36365 cp_parser_end_omp_structured_block (parser, save);
36366 add_stmt (finish_omp_structured_block (block));
36368 return stmt;
36371 /* OpenACC 2.0:
36372 # pragma acc kernels oacc-kernels-clause[optseq] new-line
36373 structured-block
36377 # pragma acc parallel oacc-parallel-clause[optseq] new-line
36378 structured-block
36381 #define OACC_KERNELS_CLAUSE_MASK \
36382 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
36383 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
36384 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
36385 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
36386 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
36387 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEFAULT) \
36388 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
36389 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
36390 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
36391 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
36392 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
36393 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
36394 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE) \
36395 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
36397 #define OACC_PARALLEL_CLAUSE_MASK \
36398 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
36399 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
36400 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
36401 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
36402 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
36403 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEFAULT) \
36404 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
36405 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_FIRSTPRIVATE) \
36406 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
36407 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_GANGS) \
36408 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_WORKERS) \
36409 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
36410 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
36411 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
36412 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
36413 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE) \
36414 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRIVATE) \
36415 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION) \
36416 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR_LENGTH) \
36417 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
36419 static tree
36420 cp_parser_oacc_kernels_parallel (cp_parser *parser, cp_token *pragma_tok,
36421 char *p_name, bool *if_p)
36423 omp_clause_mask mask;
36424 enum tree_code code;
36425 switch (cp_parser_pragma_kind (pragma_tok))
36427 case PRAGMA_OACC_KERNELS:
36428 strcat (p_name, " kernels");
36429 mask = OACC_KERNELS_CLAUSE_MASK;
36430 code = OACC_KERNELS;
36431 break;
36432 case PRAGMA_OACC_PARALLEL:
36433 strcat (p_name, " parallel");
36434 mask = OACC_PARALLEL_CLAUSE_MASK;
36435 code = OACC_PARALLEL;
36436 break;
36437 default:
36438 gcc_unreachable ();
36441 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36443 const char *p
36444 = IDENTIFIER_POINTER (cp_lexer_peek_token (parser->lexer)->u.value);
36445 if (strcmp (p, "loop") == 0)
36447 cp_lexer_consume_token (parser->lexer);
36448 tree block = begin_omp_parallel ();
36449 tree clauses;
36450 cp_parser_oacc_loop (parser, pragma_tok, p_name, mask, &clauses,
36451 if_p);
36452 return finish_omp_construct (code, block, clauses);
36456 tree clauses = cp_parser_oacc_all_clauses (parser, mask, p_name, pragma_tok);
36458 tree block = begin_omp_parallel ();
36459 unsigned int save = cp_parser_begin_omp_structured_block (parser);
36460 cp_parser_statement (parser, NULL_TREE, false, if_p);
36461 cp_parser_end_omp_structured_block (parser, save);
36462 return finish_omp_construct (code, block, clauses);
36465 /* OpenACC 2.0:
36466 # pragma acc update oacc-update-clause[optseq] new-line
36469 #define OACC_UPDATE_CLAUSE_MASK \
36470 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
36471 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICE) \
36472 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_HOST) \
36473 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
36474 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SELF) \
36475 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT))
36477 static tree
36478 cp_parser_oacc_update (cp_parser *parser, cp_token *pragma_tok)
36480 tree stmt, clauses;
36482 clauses = cp_parser_oacc_all_clauses (parser, OACC_UPDATE_CLAUSE_MASK,
36483 "#pragma acc update", pragma_tok);
36485 if (omp_find_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
36487 error_at (pragma_tok->location,
36488 "%<#pragma acc update%> must contain at least one "
36489 "%<device%> or %<host%> or %<self%> clause");
36490 return NULL_TREE;
36493 stmt = make_node (OACC_UPDATE);
36494 TREE_TYPE (stmt) = void_type_node;
36495 OACC_UPDATE_CLAUSES (stmt) = clauses;
36496 SET_EXPR_LOCATION (stmt, pragma_tok->location);
36497 add_stmt (stmt);
36498 return stmt;
36501 /* OpenACC 2.0:
36502 # pragma acc wait [(intseq)] oacc-wait-clause[optseq] new-line
36504 LOC is the location of the #pragma token.
36507 #define OACC_WAIT_CLAUSE_MASK \
36508 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC))
36510 static tree
36511 cp_parser_oacc_wait (cp_parser *parser, cp_token *pragma_tok)
36513 tree clauses, list = NULL_TREE, stmt = NULL_TREE;
36514 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
36516 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
36517 list = cp_parser_oacc_wait_list (parser, loc, list);
36519 clauses = cp_parser_oacc_all_clauses (parser, OACC_WAIT_CLAUSE_MASK,
36520 "#pragma acc wait", pragma_tok);
36522 stmt = c_finish_oacc_wait (loc, list, clauses);
36523 stmt = finish_expr_stmt (stmt);
36525 return stmt;
36528 /* OpenMP 4.0:
36529 # pragma omp declare simd declare-simd-clauses[optseq] new-line */
36531 #define OMP_DECLARE_SIMD_CLAUSE_MASK \
36532 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN) \
36533 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
36534 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
36535 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNIFORM) \
36536 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_INBRANCH) \
36537 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOTINBRANCH))
36539 static void
36540 cp_parser_omp_declare_simd (cp_parser *parser, cp_token *pragma_tok,
36541 enum pragma_context context)
36543 bool first_p = parser->omp_declare_simd == NULL;
36544 cp_omp_declare_simd_data data;
36545 if (first_p)
36547 data.error_seen = false;
36548 data.fndecl_seen = false;
36549 data.tokens = vNULL;
36550 data.clauses = NULL_TREE;
36551 /* It is safe to take the address of a local variable; it will only be
36552 used while this scope is live. */
36553 parser->omp_declare_simd = &data;
36556 /* Store away all pragma tokens. */
36557 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
36558 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
36559 cp_lexer_consume_token (parser->lexer);
36560 if (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
36561 parser->omp_declare_simd->error_seen = true;
36562 cp_parser_require_pragma_eol (parser, pragma_tok);
36563 struct cp_token_cache *cp
36564 = cp_token_cache_new (pragma_tok, cp_lexer_peek_token (parser->lexer));
36565 parser->omp_declare_simd->tokens.safe_push (cp);
36567 if (first_p)
36569 while (cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA))
36570 cp_parser_pragma (parser, context, NULL);
36571 switch (context)
36573 case pragma_external:
36574 cp_parser_declaration (parser);
36575 break;
36576 case pragma_member:
36577 cp_parser_member_declaration (parser);
36578 break;
36579 case pragma_objc_icode:
36580 cp_parser_block_declaration (parser, /*statement_p=*/false);
36581 break;
36582 default:
36583 cp_parser_declaration_statement (parser);
36584 break;
36586 if (parser->omp_declare_simd
36587 && !parser->omp_declare_simd->error_seen
36588 && !parser->omp_declare_simd->fndecl_seen)
36589 error_at (pragma_tok->location,
36590 "%<#pragma omp declare simd%> not immediately followed by "
36591 "function declaration or definition");
36592 data.tokens.release ();
36593 parser->omp_declare_simd = NULL;
36597 /* Handles the delayed parsing of the Cilk Plus SIMD-enabled function.
36598 This function is modelled similar to the late parsing of omp declare
36599 simd. */
36601 static tree
36602 cp_parser_late_parsing_cilk_simd_fn_info (cp_parser *parser, tree attrs)
36604 struct cp_token_cache *ce;
36605 cp_omp_declare_simd_data *info = parser->cilk_simd_fn_info;
36606 int ii = 0;
36608 if (parser->omp_declare_simd != NULL
36609 || lookup_attribute ("simd", attrs))
36611 error ("%<#pragma omp declare simd%> or %<simd%> attribute cannot be "
36612 "used in the same function marked as a Cilk Plus SIMD-enabled "
36613 "function");
36614 parser->cilk_simd_fn_info->tokens.release ();
36615 XDELETE (parser->cilk_simd_fn_info);
36616 parser->cilk_simd_fn_info = NULL;
36617 return attrs;
36619 if (!info->error_seen && info->fndecl_seen)
36621 error ("vector attribute not immediately followed by a single function"
36622 " declaration or definition");
36623 info->error_seen = true;
36625 if (info->error_seen)
36626 return attrs;
36628 FOR_EACH_VEC_ELT (info->tokens, ii, ce)
36630 tree c, cl;
36632 cp_parser_push_lexer_for_tokens (parser, ce);
36633 parser->lexer->in_pragma = true;
36634 cl = cp_parser_omp_all_clauses (parser, CILK_SIMD_FN_CLAUSE_MASK,
36635 "SIMD-enabled functions attribute",
36636 NULL);
36637 cp_parser_pop_lexer (parser);
36638 if (cl)
36639 cl = tree_cons (NULL_TREE, cl, NULL_TREE);
36641 c = build_tree_list (get_identifier ("cilk simd function"), NULL_TREE);
36642 TREE_CHAIN (c) = attrs;
36643 attrs = c;
36645 c = build_tree_list (get_identifier ("omp declare simd"), cl);
36646 TREE_CHAIN (c) = attrs;
36647 if (processing_template_decl)
36648 ATTR_IS_DEPENDENT (c) = 1;
36649 attrs = c;
36651 info->fndecl_seen = true;
36652 parser->cilk_simd_fn_info->tokens.release ();
36653 XDELETE (parser->cilk_simd_fn_info);
36654 parser->cilk_simd_fn_info = NULL;
36655 return attrs;
36658 /* Finalize #pragma omp declare simd clauses after direct declarator has
36659 been parsed, and put that into "omp declare simd" attribute. */
36661 static tree
36662 cp_parser_late_parsing_omp_declare_simd (cp_parser *parser, tree attrs)
36664 struct cp_token_cache *ce;
36665 cp_omp_declare_simd_data *data = parser->omp_declare_simd;
36666 int i;
36668 if (!data->error_seen && data->fndecl_seen)
36670 error ("%<#pragma omp declare simd%> not immediately followed by "
36671 "a single function declaration or definition");
36672 data->error_seen = true;
36674 if (data->error_seen)
36675 return attrs;
36677 FOR_EACH_VEC_ELT (data->tokens, i, ce)
36679 tree c, cl;
36681 cp_parser_push_lexer_for_tokens (parser, ce);
36682 parser->lexer->in_pragma = true;
36683 gcc_assert (cp_lexer_peek_token (parser->lexer)->type == CPP_PRAGMA);
36684 cp_token *pragma_tok = cp_lexer_consume_token (parser->lexer);
36685 cp_lexer_consume_token (parser->lexer);
36686 cl = cp_parser_omp_all_clauses (parser, OMP_DECLARE_SIMD_CLAUSE_MASK,
36687 "#pragma omp declare simd", pragma_tok);
36688 cp_parser_pop_lexer (parser);
36689 if (cl)
36690 cl = tree_cons (NULL_TREE, cl, NULL_TREE);
36691 c = build_tree_list (get_identifier ("omp declare simd"), cl);
36692 TREE_CHAIN (c) = attrs;
36693 if (processing_template_decl)
36694 ATTR_IS_DEPENDENT (c) = 1;
36695 attrs = c;
36698 data->fndecl_seen = true;
36699 return attrs;
36703 /* OpenMP 4.0:
36704 # pragma omp declare target new-line
36705 declarations and definitions
36706 # pragma omp end declare target new-line
36708 OpenMP 4.5:
36709 # pragma omp declare target ( extended-list ) new-line
36711 # pragma omp declare target declare-target-clauses[seq] new-line */
36713 #define OMP_DECLARE_TARGET_CLAUSE_MASK \
36714 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO) \
36715 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINK))
36717 static void
36718 cp_parser_omp_declare_target (cp_parser *parser, cp_token *pragma_tok)
36720 tree clauses = NULL_TREE;
36721 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36722 clauses
36723 = cp_parser_omp_all_clauses (parser, OMP_DECLARE_TARGET_CLAUSE_MASK,
36724 "#pragma omp declare target", pragma_tok);
36725 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
36727 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_TO_DECLARE,
36728 clauses);
36729 clauses = finish_omp_clauses (clauses, C_ORT_OMP);
36730 cp_parser_require_pragma_eol (parser, pragma_tok);
36732 else
36734 cp_parser_require_pragma_eol (parser, pragma_tok);
36735 scope_chain->omp_declare_target_attribute++;
36736 return;
36738 if (scope_chain->omp_declare_target_attribute)
36739 error_at (pragma_tok->location,
36740 "%<#pragma omp declare target%> with clauses in between "
36741 "%<#pragma omp declare target%> without clauses and "
36742 "%<#pragma omp end declare target%>");
36743 for (tree c = clauses; c; c = OMP_CLAUSE_CHAIN (c))
36745 tree t = OMP_CLAUSE_DECL (c), id;
36746 tree at1 = lookup_attribute ("omp declare target", DECL_ATTRIBUTES (t));
36747 tree at2 = lookup_attribute ("omp declare target link",
36748 DECL_ATTRIBUTES (t));
36749 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINK)
36751 id = get_identifier ("omp declare target link");
36752 std::swap (at1, at2);
36754 else
36755 id = get_identifier ("omp declare target");
36756 if (at2)
36758 error_at (OMP_CLAUSE_LOCATION (c),
36759 "%qD specified both in declare target %<link%> and %<to%>"
36760 " clauses", t);
36761 continue;
36763 if (!at1)
36765 DECL_ATTRIBUTES (t) = tree_cons (id, NULL_TREE, DECL_ATTRIBUTES (t));
36766 if (TREE_CODE (t) != FUNCTION_DECL && !is_global_var (t))
36767 continue;
36769 symtab_node *node = symtab_node::get (t);
36770 if (node != NULL)
36772 node->offloadable = 1;
36773 if (ENABLE_OFFLOADING)
36775 g->have_offload = true;
36776 if (is_a <varpool_node *> (node))
36777 vec_safe_push (offload_vars, t);
36784 static void
36785 cp_parser_omp_end_declare_target (cp_parser *parser, cp_token *pragma_tok)
36787 const char *p = "";
36788 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36790 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
36791 p = IDENTIFIER_POINTER (id);
36793 if (strcmp (p, "declare") == 0)
36795 cp_lexer_consume_token (parser->lexer);
36796 p = "";
36797 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36799 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
36800 p = IDENTIFIER_POINTER (id);
36802 if (strcmp (p, "target") == 0)
36803 cp_lexer_consume_token (parser->lexer);
36804 else
36806 cp_parser_error (parser, "expected %<target%>");
36807 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36808 return;
36811 else
36813 cp_parser_error (parser, "expected %<declare%>");
36814 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36815 return;
36817 cp_parser_require_pragma_eol (parser, pragma_tok);
36818 if (!scope_chain->omp_declare_target_attribute)
36819 error_at (pragma_tok->location,
36820 "%<#pragma omp end declare target%> without corresponding "
36821 "%<#pragma omp declare target%>");
36822 else
36823 scope_chain->omp_declare_target_attribute--;
36826 /* Helper function of cp_parser_omp_declare_reduction. Parse the combiner
36827 expression and optional initializer clause of
36828 #pragma omp declare reduction. We store the expression(s) as
36829 either 3, 6 or 7 special statements inside of the artificial function's
36830 body. The first two statements are DECL_EXPRs for the artificial
36831 OMP_OUT resp. OMP_IN variables, followed by a statement with the combiner
36832 expression that uses those variables.
36833 If there was any INITIALIZER clause, this is followed by further statements,
36834 the fourth and fifth statements are DECL_EXPRs for the artificial
36835 OMP_PRIV resp. OMP_ORIG variables. If the INITIALIZER clause wasn't the
36836 constructor variant (first token after open paren is not omp_priv),
36837 then the sixth statement is a statement with the function call expression
36838 that uses the OMP_PRIV and optionally OMP_ORIG variable.
36839 Otherwise, the sixth statement is whatever statement cp_finish_decl emits
36840 to initialize the OMP_PRIV artificial variable and there is seventh
36841 statement, a DECL_EXPR of the OMP_PRIV statement again. */
36843 static bool
36844 cp_parser_omp_declare_reduction_exprs (tree fndecl, cp_parser *parser)
36846 tree type = TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (fndecl)));
36847 gcc_assert (TREE_CODE (type) == REFERENCE_TYPE);
36848 type = TREE_TYPE (type);
36849 tree omp_out = build_lang_decl (VAR_DECL, get_identifier ("omp_out"), type);
36850 DECL_ARTIFICIAL (omp_out) = 1;
36851 pushdecl (omp_out);
36852 add_decl_expr (omp_out);
36853 tree omp_in = build_lang_decl (VAR_DECL, get_identifier ("omp_in"), type);
36854 DECL_ARTIFICIAL (omp_in) = 1;
36855 pushdecl (omp_in);
36856 add_decl_expr (omp_in);
36857 tree combiner;
36858 tree omp_priv = NULL_TREE, omp_orig = NULL_TREE, initializer = NULL_TREE;
36860 keep_next_level (true);
36861 tree block = begin_omp_structured_block ();
36862 combiner = cp_parser_expression (parser);
36863 finish_expr_stmt (combiner);
36864 block = finish_omp_structured_block (block);
36865 add_stmt (block);
36867 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
36868 return false;
36870 const char *p = "";
36871 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36873 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
36874 p = IDENTIFIER_POINTER (id);
36877 if (strcmp (p, "initializer") == 0)
36879 cp_lexer_consume_token (parser->lexer);
36880 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
36881 return false;
36883 p = "";
36884 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36886 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
36887 p = IDENTIFIER_POINTER (id);
36890 omp_priv = build_lang_decl (VAR_DECL, get_identifier ("omp_priv"), type);
36891 DECL_ARTIFICIAL (omp_priv) = 1;
36892 pushdecl (omp_priv);
36893 add_decl_expr (omp_priv);
36894 omp_orig = build_lang_decl (VAR_DECL, get_identifier ("omp_orig"), type);
36895 DECL_ARTIFICIAL (omp_orig) = 1;
36896 pushdecl (omp_orig);
36897 add_decl_expr (omp_orig);
36899 keep_next_level (true);
36900 block = begin_omp_structured_block ();
36902 bool ctor = false;
36903 if (strcmp (p, "omp_priv") == 0)
36905 bool is_direct_init, is_non_constant_init;
36906 ctor = true;
36907 cp_lexer_consume_token (parser->lexer);
36908 /* Reject initializer (omp_priv) and initializer (omp_priv ()). */
36909 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
36910 || (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
36911 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
36912 == CPP_CLOSE_PAREN
36913 && cp_lexer_peek_nth_token (parser->lexer, 3)->type
36914 == CPP_CLOSE_PAREN))
36916 finish_omp_structured_block (block);
36917 error ("invalid initializer clause");
36918 return false;
36920 initializer = cp_parser_initializer (parser, &is_direct_init,
36921 &is_non_constant_init);
36922 cp_finish_decl (omp_priv, initializer, !is_non_constant_init,
36923 NULL_TREE, LOOKUP_ONLYCONVERTING);
36925 else
36927 cp_parser_parse_tentatively (parser);
36928 tree fn_name = cp_parser_id_expression (parser, /*template_p=*/false,
36929 /*check_dependency_p=*/true,
36930 /*template_p=*/NULL,
36931 /*declarator_p=*/false,
36932 /*optional_p=*/false);
36933 vec<tree, va_gc> *args;
36934 if (fn_name == error_mark_node
36935 || cp_parser_error_occurred (parser)
36936 || !cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
36937 || ((args = cp_parser_parenthesized_expression_list
36938 (parser, non_attr, /*cast_p=*/false,
36939 /*allow_expansion_p=*/true,
36940 /*non_constant_p=*/NULL)),
36941 cp_parser_error_occurred (parser)))
36943 finish_omp_structured_block (block);
36944 cp_parser_abort_tentative_parse (parser);
36945 cp_parser_error (parser, "expected id-expression (arguments)");
36946 return false;
36948 unsigned int i;
36949 tree arg;
36950 FOR_EACH_VEC_SAFE_ELT (args, i, arg)
36951 if (arg == omp_priv
36952 || (TREE_CODE (arg) == ADDR_EXPR
36953 && TREE_OPERAND (arg, 0) == omp_priv))
36954 break;
36955 cp_parser_abort_tentative_parse (parser);
36956 if (arg == NULL_TREE)
36957 error ("one of the initializer call arguments should be %<omp_priv%>"
36958 " or %<&omp_priv%>");
36959 initializer = cp_parser_postfix_expression (parser, false, false, false,
36960 false, NULL);
36961 finish_expr_stmt (initializer);
36964 block = finish_omp_structured_block (block);
36965 cp_walk_tree (&block, cp_remove_omp_priv_cleanup_stmt, omp_priv, NULL);
36966 add_stmt (block);
36968 if (ctor)
36969 add_decl_expr (omp_orig);
36971 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
36972 return false;
36975 if (!cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA_EOL))
36976 cp_parser_required_error (parser, RT_PRAGMA_EOL, /*keyword=*/false);
36978 return true;
36981 /* OpenMP 4.0
36982 #pragma omp declare reduction (reduction-id : typename-list : expression) \
36983 initializer-clause[opt] new-line
36985 initializer-clause:
36986 initializer (omp_priv initializer)
36987 initializer (function-name (argument-list)) */
36989 static void
36990 cp_parser_omp_declare_reduction (cp_parser *parser, cp_token *pragma_tok,
36991 enum pragma_context)
36993 auto_vec<tree> types;
36994 enum tree_code reduc_code = ERROR_MARK;
36995 tree reduc_id = NULL_TREE, orig_reduc_id = NULL_TREE, type;
36996 unsigned int i;
36997 cp_token *first_token;
36998 cp_token_cache *cp;
36999 int errs;
37000 void *p;
37002 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
37003 p = obstack_alloc (&declarator_obstack, 0);
37005 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
37006 goto fail;
37008 switch (cp_lexer_peek_token (parser->lexer)->type)
37010 case CPP_PLUS:
37011 reduc_code = PLUS_EXPR;
37012 break;
37013 case CPP_MULT:
37014 reduc_code = MULT_EXPR;
37015 break;
37016 case CPP_MINUS:
37017 reduc_code = MINUS_EXPR;
37018 break;
37019 case CPP_AND:
37020 reduc_code = BIT_AND_EXPR;
37021 break;
37022 case CPP_XOR:
37023 reduc_code = BIT_XOR_EXPR;
37024 break;
37025 case CPP_OR:
37026 reduc_code = BIT_IOR_EXPR;
37027 break;
37028 case CPP_AND_AND:
37029 reduc_code = TRUTH_ANDIF_EXPR;
37030 break;
37031 case CPP_OR_OR:
37032 reduc_code = TRUTH_ORIF_EXPR;
37033 break;
37034 case CPP_NAME:
37035 reduc_id = orig_reduc_id = cp_parser_identifier (parser);
37036 break;
37037 default:
37038 cp_parser_error (parser, "expected %<+%>, %<*%>, %<-%>, %<&%>, %<^%>, "
37039 "%<|%>, %<&&%>, %<||%> or identifier");
37040 goto fail;
37043 if (reduc_code != ERROR_MARK)
37044 cp_lexer_consume_token (parser->lexer);
37046 reduc_id = omp_reduction_id (reduc_code, reduc_id, NULL_TREE);
37047 if (reduc_id == error_mark_node)
37048 goto fail;
37050 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
37051 goto fail;
37053 /* Types may not be defined in declare reduction type list. */
37054 const char *saved_message;
37055 saved_message = parser->type_definition_forbidden_message;
37056 parser->type_definition_forbidden_message
37057 = G_("types may not be defined in declare reduction type list");
37058 bool saved_colon_corrects_to_scope_p;
37059 saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
37060 parser->colon_corrects_to_scope_p = false;
37061 bool saved_colon_doesnt_start_class_def_p;
37062 saved_colon_doesnt_start_class_def_p
37063 = parser->colon_doesnt_start_class_def_p;
37064 parser->colon_doesnt_start_class_def_p = true;
37066 while (true)
37068 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
37069 type = cp_parser_type_id (parser);
37070 if (type == error_mark_node)
37072 else if (ARITHMETIC_TYPE_P (type)
37073 && (orig_reduc_id == NULL_TREE
37074 || (TREE_CODE (type) != COMPLEX_TYPE
37075 && (strcmp (IDENTIFIER_POINTER (orig_reduc_id),
37076 "min") == 0
37077 || strcmp (IDENTIFIER_POINTER (orig_reduc_id),
37078 "max") == 0))))
37079 error_at (loc, "predeclared arithmetic type %qT in "
37080 "%<#pragma omp declare reduction%>", type);
37081 else if (TREE_CODE (type) == FUNCTION_TYPE
37082 || TREE_CODE (type) == METHOD_TYPE
37083 || TREE_CODE (type) == ARRAY_TYPE)
37084 error_at (loc, "function or array type %qT in "
37085 "%<#pragma omp declare reduction%>", type);
37086 else if (TREE_CODE (type) == REFERENCE_TYPE)
37087 error_at (loc, "reference type %qT in "
37088 "%<#pragma omp declare reduction%>", type);
37089 else if (TYPE_QUALS_NO_ADDR_SPACE (type))
37090 error_at (loc, "const, volatile or __restrict qualified type %qT in "
37091 "%<#pragma omp declare reduction%>", type);
37092 else
37093 types.safe_push (type);
37095 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
37096 cp_lexer_consume_token (parser->lexer);
37097 else
37098 break;
37101 /* Restore the saved message. */
37102 parser->type_definition_forbidden_message = saved_message;
37103 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
37104 parser->colon_doesnt_start_class_def_p
37105 = saved_colon_doesnt_start_class_def_p;
37107 if (!cp_parser_require (parser, CPP_COLON, RT_COLON)
37108 || types.is_empty ())
37110 fail:
37111 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37112 goto done;
37115 first_token = cp_lexer_peek_token (parser->lexer);
37116 cp = NULL;
37117 errs = errorcount;
37118 FOR_EACH_VEC_ELT (types, i, type)
37120 tree fntype
37121 = build_function_type_list (void_type_node,
37122 cp_build_reference_type (type, false),
37123 NULL_TREE);
37124 tree this_reduc_id = reduc_id;
37125 if (!dependent_type_p (type))
37126 this_reduc_id = omp_reduction_id (ERROR_MARK, reduc_id, type);
37127 tree fndecl = build_lang_decl (FUNCTION_DECL, this_reduc_id, fntype);
37128 DECL_SOURCE_LOCATION (fndecl) = pragma_tok->location;
37129 DECL_ARTIFICIAL (fndecl) = 1;
37130 DECL_EXTERNAL (fndecl) = 1;
37131 DECL_DECLARED_INLINE_P (fndecl) = 1;
37132 DECL_IGNORED_P (fndecl) = 1;
37133 DECL_OMP_DECLARE_REDUCTION_P (fndecl) = 1;
37134 SET_DECL_ASSEMBLER_NAME (fndecl, get_identifier ("<udr>"));
37135 DECL_ATTRIBUTES (fndecl)
37136 = tree_cons (get_identifier ("gnu_inline"), NULL_TREE,
37137 DECL_ATTRIBUTES (fndecl));
37138 if (processing_template_decl)
37139 fndecl = push_template_decl (fndecl);
37140 bool block_scope = false;
37141 tree block = NULL_TREE;
37142 if (current_function_decl)
37144 block_scope = true;
37145 DECL_CONTEXT (fndecl) = global_namespace;
37146 if (!processing_template_decl)
37147 pushdecl (fndecl);
37149 else if (current_class_type)
37151 if (cp == NULL)
37153 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
37154 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
37155 cp_lexer_consume_token (parser->lexer);
37156 if (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
37157 goto fail;
37158 cp = cp_token_cache_new (first_token,
37159 cp_lexer_peek_nth_token (parser->lexer,
37160 2));
37162 DECL_STATIC_FUNCTION_P (fndecl) = 1;
37163 finish_member_declaration (fndecl);
37164 DECL_PENDING_INLINE_INFO (fndecl) = cp;
37165 DECL_PENDING_INLINE_P (fndecl) = 1;
37166 vec_safe_push (unparsed_funs_with_definitions, fndecl);
37167 continue;
37169 else
37171 DECL_CONTEXT (fndecl) = current_namespace;
37172 pushdecl (fndecl);
37174 if (!block_scope)
37175 start_preparsed_function (fndecl, NULL_TREE, SF_PRE_PARSED);
37176 else
37177 block = begin_omp_structured_block ();
37178 if (cp)
37180 cp_parser_push_lexer_for_tokens (parser, cp);
37181 parser->lexer->in_pragma = true;
37183 if (!cp_parser_omp_declare_reduction_exprs (fndecl, parser))
37185 if (!block_scope)
37186 finish_function (0);
37187 else
37188 DECL_CONTEXT (fndecl) = current_function_decl;
37189 if (cp)
37190 cp_parser_pop_lexer (parser);
37191 goto fail;
37193 if (cp)
37194 cp_parser_pop_lexer (parser);
37195 if (!block_scope)
37196 finish_function (0);
37197 else
37199 DECL_CONTEXT (fndecl) = current_function_decl;
37200 block = finish_omp_structured_block (block);
37201 if (TREE_CODE (block) == BIND_EXPR)
37202 DECL_SAVED_TREE (fndecl) = BIND_EXPR_BODY (block);
37203 else if (TREE_CODE (block) == STATEMENT_LIST)
37204 DECL_SAVED_TREE (fndecl) = block;
37205 if (processing_template_decl)
37206 add_decl_expr (fndecl);
37208 cp_check_omp_declare_reduction (fndecl);
37209 if (cp == NULL && types.length () > 1)
37210 cp = cp_token_cache_new (first_token,
37211 cp_lexer_peek_nth_token (parser->lexer, 2));
37212 if (errs != errorcount)
37213 break;
37216 cp_parser_require_pragma_eol (parser, pragma_tok);
37218 done:
37219 /* Free any declarators allocated. */
37220 obstack_free (&declarator_obstack, p);
37223 /* OpenMP 4.0
37224 #pragma omp declare simd declare-simd-clauses[optseq] new-line
37225 #pragma omp declare reduction (reduction-id : typename-list : expression) \
37226 initializer-clause[opt] new-line
37227 #pragma omp declare target new-line */
37229 static void
37230 cp_parser_omp_declare (cp_parser *parser, cp_token *pragma_tok,
37231 enum pragma_context context)
37233 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37235 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37236 const char *p = IDENTIFIER_POINTER (id);
37238 if (strcmp (p, "simd") == 0)
37240 cp_lexer_consume_token (parser->lexer);
37241 cp_parser_omp_declare_simd (parser, pragma_tok,
37242 context);
37243 return;
37245 cp_ensure_no_omp_declare_simd (parser);
37246 if (strcmp (p, "reduction") == 0)
37248 cp_lexer_consume_token (parser->lexer);
37249 cp_parser_omp_declare_reduction (parser, pragma_tok,
37250 context);
37251 return;
37253 if (!flag_openmp) /* flag_openmp_simd */
37255 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37256 return;
37258 if (strcmp (p, "target") == 0)
37260 cp_lexer_consume_token (parser->lexer);
37261 cp_parser_omp_declare_target (parser, pragma_tok);
37262 return;
37265 cp_parser_error (parser, "expected %<simd%> or %<reduction%> "
37266 "or %<target%>");
37267 cp_parser_require_pragma_eol (parser, pragma_tok);
37270 /* OpenMP 4.5:
37271 #pragma omp taskloop taskloop-clause[optseq] new-line
37272 for-loop
37274 #pragma omp taskloop simd taskloop-simd-clause[optseq] new-line
37275 for-loop */
37277 #define OMP_TASKLOOP_CLAUSE_MASK \
37278 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
37279 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
37280 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
37281 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
37282 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
37283 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_GRAINSIZE) \
37284 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TASKS) \
37285 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE) \
37286 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED) \
37287 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
37288 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL) \
37289 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE) \
37290 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOGROUP) \
37291 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIORITY))
37293 static tree
37294 cp_parser_omp_taskloop (cp_parser *parser, cp_token *pragma_tok,
37295 char *p_name, omp_clause_mask mask, tree *cclauses,
37296 bool *if_p)
37298 tree clauses, sb, ret;
37299 unsigned int save;
37300 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
37302 strcat (p_name, " taskloop");
37303 mask |= OMP_TASKLOOP_CLAUSE_MASK;
37305 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37307 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37308 const char *p = IDENTIFIER_POINTER (id);
37310 if (strcmp (p, "simd") == 0)
37312 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
37313 if (cclauses == NULL)
37314 cclauses = cclauses_buf;
37316 cp_lexer_consume_token (parser->lexer);
37317 if (!flag_openmp) /* flag_openmp_simd */
37318 return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
37319 cclauses, if_p);
37320 sb = begin_omp_structured_block ();
37321 save = cp_parser_begin_omp_structured_block (parser);
37322 ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
37323 cclauses, if_p);
37324 cp_parser_end_omp_structured_block (parser, save);
37325 tree body = finish_omp_structured_block (sb);
37326 if (ret == NULL)
37327 return ret;
37328 ret = make_node (OMP_TASKLOOP);
37329 TREE_TYPE (ret) = void_type_node;
37330 OMP_FOR_BODY (ret) = body;
37331 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_TASKLOOP];
37332 SET_EXPR_LOCATION (ret, loc);
37333 add_stmt (ret);
37334 return ret;
37337 if (!flag_openmp) /* flag_openmp_simd */
37339 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37340 return NULL_TREE;
37343 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
37344 cclauses == NULL);
37345 if (cclauses)
37347 cp_omp_split_clauses (loc, OMP_TASKLOOP, mask, clauses, cclauses);
37348 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TASKLOOP];
37351 sb = begin_omp_structured_block ();
37352 save = cp_parser_begin_omp_structured_block (parser);
37354 ret = cp_parser_omp_for_loop (parser, OMP_TASKLOOP, clauses, cclauses,
37355 if_p);
37357 cp_parser_end_omp_structured_block (parser, save);
37358 add_stmt (finish_omp_structured_block (sb));
37360 return ret;
37364 /* OpenACC 2.0:
37365 # pragma acc routine oacc-routine-clause[optseq] new-line
37366 function-definition
37368 # pragma acc routine ( name ) oacc-routine-clause[optseq] new-line
37371 #define OACC_ROUTINE_CLAUSE_MASK \
37372 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_GANG) \
37373 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WORKER) \
37374 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR) \
37375 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SEQ))
37378 /* Parse the OpenACC routine pragma. This has an optional '( name )'
37379 component, which must resolve to a declared namespace-scope
37380 function. The clauses are either processed directly (for a named
37381 function), or defered until the immediatley following declaration
37382 is parsed. */
37384 static void
37385 cp_parser_oacc_routine (cp_parser *parser, cp_token *pragma_tok,
37386 enum pragma_context context)
37388 gcc_checking_assert (context == pragma_external);
37389 /* The checking for "another pragma following this one" in the "no optional
37390 '( name )'" case makes sure that we dont re-enter. */
37391 gcc_checking_assert (parser->oacc_routine == NULL);
37393 cp_oacc_routine_data data;
37394 data.error_seen = false;
37395 data.fndecl_seen = false;
37396 data.tokens = vNULL;
37397 data.clauses = NULL_TREE;
37398 data.loc = pragma_tok->location;
37399 /* It is safe to take the address of a local variable; it will only be
37400 used while this scope is live. */
37401 parser->oacc_routine = &data;
37403 /* Look for optional '( name )'. */
37404 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
37406 cp_lexer_consume_token (parser->lexer); /* '(' */
37408 /* We parse the name as an id-expression. If it resolves to
37409 anything other than a non-overloaded function at namespace
37410 scope, it's an error. */
37411 location_t name_loc = cp_lexer_peek_token (parser->lexer)->location;
37412 tree name = cp_parser_id_expression (parser,
37413 /*template_keyword_p=*/false,
37414 /*check_dependency_p=*/false,
37415 /*template_p=*/NULL,
37416 /*declarator_p=*/false,
37417 /*optional_p=*/false);
37418 tree decl = cp_parser_lookup_name_simple (parser, name, name_loc);
37419 if (name != error_mark_node && decl == error_mark_node)
37420 cp_parser_name_lookup_error (parser, name, decl, NLE_NULL, name_loc);
37422 if (decl == error_mark_node
37423 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
37425 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37426 parser->oacc_routine = NULL;
37427 return;
37430 data.clauses
37431 = cp_parser_oacc_all_clauses (parser, OACC_ROUTINE_CLAUSE_MASK,
37432 "#pragma acc routine",
37433 cp_lexer_peek_token (parser->lexer));
37435 if (decl && is_overloaded_fn (decl)
37436 && (TREE_CODE (decl) != FUNCTION_DECL
37437 || DECL_FUNCTION_TEMPLATE_P (decl)))
37439 error_at (name_loc,
37440 "%<#pragma acc routine%> names a set of overloads");
37441 parser->oacc_routine = NULL;
37442 return;
37445 /* Perhaps we should use the same rule as declarations in different
37446 namespaces? */
37447 if (!DECL_NAMESPACE_SCOPE_P (decl))
37449 error_at (name_loc,
37450 "%qD does not refer to a namespace scope function", decl);
37451 parser->oacc_routine = NULL;
37452 return;
37455 if (TREE_CODE (decl) != FUNCTION_DECL)
37457 error_at (name_loc, "%qD does not refer to a function", decl);
37458 parser->oacc_routine = NULL;
37459 return;
37462 cp_finalize_oacc_routine (parser, decl, false);
37463 parser->oacc_routine = NULL;
37465 else /* No optional '( name )'. */
37467 /* Store away all pragma tokens. */
37468 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
37469 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
37470 cp_lexer_consume_token (parser->lexer);
37471 if (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
37472 parser->oacc_routine->error_seen = true;
37473 cp_parser_require_pragma_eol (parser, pragma_tok);
37474 struct cp_token_cache *cp
37475 = cp_token_cache_new (pragma_tok, cp_lexer_peek_token (parser->lexer));
37476 parser->oacc_routine->tokens.safe_push (cp);
37478 /* Emit a helpful diagnostic if there's another pragma following this
37479 one. */
37480 if (cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA))
37482 cp_ensure_no_oacc_routine (parser);
37483 data.tokens.release ();
37484 /* ..., and then just keep going. */
37485 return;
37488 /* We only have to consider the pragma_external case here. */
37489 cp_parser_declaration (parser);
37490 if (parser->oacc_routine
37491 && !parser->oacc_routine->fndecl_seen)
37492 cp_ensure_no_oacc_routine (parser);
37493 else
37494 parser->oacc_routine = NULL;
37495 data.tokens.release ();
37499 /* Finalize #pragma acc routine clauses after direct declarator has
37500 been parsed. */
37502 static tree
37503 cp_parser_late_parsing_oacc_routine (cp_parser *parser, tree attrs)
37505 struct cp_token_cache *ce;
37506 cp_oacc_routine_data *data = parser->oacc_routine;
37508 if (!data->error_seen && data->fndecl_seen)
37510 error_at (data->loc,
37511 "%<#pragma acc routine%> not immediately followed by "
37512 "a single function declaration or definition");
37513 data->error_seen = true;
37515 if (data->error_seen)
37516 return attrs;
37518 gcc_checking_assert (data->tokens.length () == 1);
37519 ce = data->tokens[0];
37521 cp_parser_push_lexer_for_tokens (parser, ce);
37522 parser->lexer->in_pragma = true;
37523 gcc_assert (cp_lexer_peek_token (parser->lexer)->type == CPP_PRAGMA);
37525 cp_token *pragma_tok = cp_lexer_consume_token (parser->lexer);
37526 gcc_checking_assert (parser->oacc_routine->clauses == NULL_TREE);
37527 parser->oacc_routine->clauses
37528 = cp_parser_oacc_all_clauses (parser, OACC_ROUTINE_CLAUSE_MASK,
37529 "#pragma acc routine", pragma_tok);
37530 cp_parser_pop_lexer (parser);
37531 /* Later, cp_finalize_oacc_routine will process the clauses, and then set
37532 fndecl_seen. */
37534 return attrs;
37537 /* Apply any saved OpenACC routine clauses to a just-parsed
37538 declaration. */
37540 static void
37541 cp_finalize_oacc_routine (cp_parser *parser, tree fndecl, bool is_defn)
37543 if (__builtin_expect (parser->oacc_routine != NULL, 0))
37545 /* Keep going if we're in error reporting mode. */
37546 if (parser->oacc_routine->error_seen
37547 || fndecl == error_mark_node)
37548 return;
37550 if (parser->oacc_routine->fndecl_seen)
37552 error_at (parser->oacc_routine->loc,
37553 "%<#pragma acc routine%> not immediately followed by"
37554 " a single function declaration or definition");
37555 parser->oacc_routine = NULL;
37556 return;
37558 if (TREE_CODE (fndecl) != FUNCTION_DECL)
37560 cp_ensure_no_oacc_routine (parser);
37561 return;
37564 if (oacc_get_fn_attrib (fndecl))
37566 error_at (parser->oacc_routine->loc,
37567 "%<#pragma acc routine%> already applied to %qD", fndecl);
37568 parser->oacc_routine = NULL;
37569 return;
37572 if (TREE_USED (fndecl) || (!is_defn && DECL_SAVED_TREE (fndecl)))
37574 error_at (parser->oacc_routine->loc,
37575 TREE_USED (fndecl)
37576 ? G_("%<#pragma acc routine%> must be applied before use")
37577 : G_("%<#pragma acc routine%> must be applied before "
37578 "definition"));
37579 parser->oacc_routine = NULL;
37580 return;
37583 /* Process the routine's dimension clauses. */
37584 tree dims = oacc_build_routine_dims (parser->oacc_routine->clauses);
37585 oacc_replace_fn_attrib (fndecl, dims);
37587 /* Add an "omp declare target" attribute. */
37588 DECL_ATTRIBUTES (fndecl)
37589 = tree_cons (get_identifier ("omp declare target"),
37590 NULL_TREE, DECL_ATTRIBUTES (fndecl));
37592 /* Don't unset parser->oacc_routine here: we may still need it to
37593 diagnose wrong usage. But, remember that we've used this "#pragma acc
37594 routine". */
37595 parser->oacc_routine->fndecl_seen = true;
37599 /* Main entry point to OpenMP statement pragmas. */
37601 static void
37602 cp_parser_omp_construct (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
37604 tree stmt;
37605 char p_name[sizeof "#pragma omp teams distribute parallel for simd"];
37606 omp_clause_mask mask (0);
37608 switch (cp_parser_pragma_kind (pragma_tok))
37610 case PRAGMA_OACC_ATOMIC:
37611 cp_parser_omp_atomic (parser, pragma_tok);
37612 return;
37613 case PRAGMA_OACC_CACHE:
37614 stmt = cp_parser_oacc_cache (parser, pragma_tok);
37615 break;
37616 case PRAGMA_OACC_DATA:
37617 stmt = cp_parser_oacc_data (parser, pragma_tok, if_p);
37618 break;
37619 case PRAGMA_OACC_ENTER_DATA:
37620 stmt = cp_parser_oacc_enter_exit_data (parser, pragma_tok, true);
37621 break;
37622 case PRAGMA_OACC_EXIT_DATA:
37623 stmt = cp_parser_oacc_enter_exit_data (parser, pragma_tok, false);
37624 break;
37625 case PRAGMA_OACC_HOST_DATA:
37626 stmt = cp_parser_oacc_host_data (parser, pragma_tok, if_p);
37627 break;
37628 case PRAGMA_OACC_KERNELS:
37629 case PRAGMA_OACC_PARALLEL:
37630 strcpy (p_name, "#pragma acc");
37631 stmt = cp_parser_oacc_kernels_parallel (parser, pragma_tok, p_name,
37632 if_p);
37633 break;
37634 case PRAGMA_OACC_LOOP:
37635 strcpy (p_name, "#pragma acc");
37636 stmt = cp_parser_oacc_loop (parser, pragma_tok, p_name, mask, NULL,
37637 if_p);
37638 break;
37639 case PRAGMA_OACC_UPDATE:
37640 stmt = cp_parser_oacc_update (parser, pragma_tok);
37641 break;
37642 case PRAGMA_OACC_WAIT:
37643 stmt = cp_parser_oacc_wait (parser, pragma_tok);
37644 break;
37645 case PRAGMA_OMP_ATOMIC:
37646 cp_parser_omp_atomic (parser, pragma_tok);
37647 return;
37648 case PRAGMA_OMP_CRITICAL:
37649 stmt = cp_parser_omp_critical (parser, pragma_tok, if_p);
37650 break;
37651 case PRAGMA_OMP_DISTRIBUTE:
37652 strcpy (p_name, "#pragma omp");
37653 stmt = cp_parser_omp_distribute (parser, pragma_tok, p_name, mask, NULL,
37654 if_p);
37655 break;
37656 case PRAGMA_OMP_FOR:
37657 strcpy (p_name, "#pragma omp");
37658 stmt = cp_parser_omp_for (parser, pragma_tok, p_name, mask, NULL,
37659 if_p);
37660 break;
37661 case PRAGMA_OMP_MASTER:
37662 stmt = cp_parser_omp_master (parser, pragma_tok, if_p);
37663 break;
37664 case PRAGMA_OMP_PARALLEL:
37665 strcpy (p_name, "#pragma omp");
37666 stmt = cp_parser_omp_parallel (parser, pragma_tok, p_name, mask, NULL,
37667 if_p);
37668 break;
37669 case PRAGMA_OMP_SECTIONS:
37670 strcpy (p_name, "#pragma omp");
37671 stmt = cp_parser_omp_sections (parser, pragma_tok, p_name, mask, NULL);
37672 break;
37673 case PRAGMA_OMP_SIMD:
37674 strcpy (p_name, "#pragma omp");
37675 stmt = cp_parser_omp_simd (parser, pragma_tok, p_name, mask, NULL,
37676 if_p);
37677 break;
37678 case PRAGMA_OMP_SINGLE:
37679 stmt = cp_parser_omp_single (parser, pragma_tok, if_p);
37680 break;
37681 case PRAGMA_OMP_TASK:
37682 stmt = cp_parser_omp_task (parser, pragma_tok, if_p);
37683 break;
37684 case PRAGMA_OMP_TASKGROUP:
37685 stmt = cp_parser_omp_taskgroup (parser, pragma_tok, if_p);
37686 break;
37687 case PRAGMA_OMP_TASKLOOP:
37688 strcpy (p_name, "#pragma omp");
37689 stmt = cp_parser_omp_taskloop (parser, pragma_tok, p_name, mask, NULL,
37690 if_p);
37691 break;
37692 case PRAGMA_OMP_TEAMS:
37693 strcpy (p_name, "#pragma omp");
37694 stmt = cp_parser_omp_teams (parser, pragma_tok, p_name, mask, NULL,
37695 if_p);
37696 break;
37697 default:
37698 gcc_unreachable ();
37701 protected_set_expr_location (stmt, pragma_tok->location);
37704 /* Transactional Memory parsing routines. */
37706 /* Parse a transaction attribute.
37708 txn-attribute:
37709 attribute
37710 [ [ identifier ] ]
37712 We use this instead of cp_parser_attributes_opt for transactions to avoid
37713 the pedwarn in C++98 mode. */
37715 static tree
37716 cp_parser_txn_attribute_opt (cp_parser *parser)
37718 cp_token *token;
37719 tree attr_name, attr = NULL;
37721 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
37722 return cp_parser_attributes_opt (parser);
37724 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
37725 return NULL_TREE;
37726 cp_lexer_consume_token (parser->lexer);
37727 if (!cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE))
37728 goto error1;
37730 token = cp_lexer_peek_token (parser->lexer);
37731 if (token->type == CPP_NAME || token->type == CPP_KEYWORD)
37733 token = cp_lexer_consume_token (parser->lexer);
37735 attr_name = (token->type == CPP_KEYWORD
37736 /* For keywords, use the canonical spelling,
37737 not the parsed identifier. */
37738 ? ridpointers[(int) token->keyword]
37739 : token->u.value);
37740 attr = build_tree_list (attr_name, NULL_TREE);
37742 else
37743 cp_parser_error (parser, "expected identifier");
37745 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
37746 error1:
37747 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
37748 return attr;
37751 /* Parse a __transaction_atomic or __transaction_relaxed statement.
37753 transaction-statement:
37754 __transaction_atomic txn-attribute[opt] txn-noexcept-spec[opt]
37755 compound-statement
37756 __transaction_relaxed txn-noexcept-spec[opt] compound-statement
37759 static tree
37760 cp_parser_transaction (cp_parser *parser, cp_token *token)
37762 unsigned char old_in = parser->in_transaction;
37763 unsigned char this_in = 1, new_in;
37764 enum rid keyword = token->keyword;
37765 tree stmt, attrs, noex;
37767 cp_lexer_consume_token (parser->lexer);
37769 if (keyword == RID_TRANSACTION_RELAXED
37770 || keyword == RID_SYNCHRONIZED)
37771 this_in |= TM_STMT_ATTR_RELAXED;
37772 else
37774 attrs = cp_parser_txn_attribute_opt (parser);
37775 if (attrs)
37776 this_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
37779 /* Parse a noexcept specification. */
37780 if (keyword == RID_ATOMIC_NOEXCEPT)
37781 noex = boolean_true_node;
37782 else if (keyword == RID_ATOMIC_CANCEL)
37784 /* cancel-and-throw is unimplemented. */
37785 sorry ("atomic_cancel");
37786 noex = NULL_TREE;
37788 else
37789 noex = cp_parser_noexcept_specification_opt (parser, true, NULL, true);
37791 /* Keep track if we're in the lexical scope of an outer transaction. */
37792 new_in = this_in | (old_in & TM_STMT_ATTR_OUTER);
37794 stmt = begin_transaction_stmt (token->location, NULL, this_in);
37796 parser->in_transaction = new_in;
37797 cp_parser_compound_statement (parser, NULL, BCS_TRANSACTION, false);
37798 parser->in_transaction = old_in;
37800 finish_transaction_stmt (stmt, NULL, this_in, noex);
37802 return stmt;
37805 /* Parse a __transaction_atomic or __transaction_relaxed expression.
37807 transaction-expression:
37808 __transaction_atomic txn-noexcept-spec[opt] ( expression )
37809 __transaction_relaxed txn-noexcept-spec[opt] ( expression )
37812 static tree
37813 cp_parser_transaction_expression (cp_parser *parser, enum rid keyword)
37815 unsigned char old_in = parser->in_transaction;
37816 unsigned char this_in = 1;
37817 cp_token *token;
37818 tree expr, noex;
37819 bool noex_expr;
37820 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
37822 gcc_assert (keyword == RID_TRANSACTION_ATOMIC
37823 || keyword == RID_TRANSACTION_RELAXED);
37825 if (!flag_tm)
37826 error_at (loc,
37827 keyword == RID_TRANSACTION_RELAXED
37828 ? G_("%<__transaction_relaxed%> without transactional memory "
37829 "support enabled")
37830 : G_("%<__transaction_atomic%> without transactional memory "
37831 "support enabled"));
37833 token = cp_parser_require_keyword (parser, keyword,
37834 (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
37835 : RT_TRANSACTION_RELAXED));
37836 gcc_assert (token != NULL);
37838 if (keyword == RID_TRANSACTION_RELAXED)
37839 this_in |= TM_STMT_ATTR_RELAXED;
37841 /* Set this early. This might mean that we allow transaction_cancel in
37842 an expression that we find out later actually has to be a constexpr.
37843 However, we expect that cxx_constant_value will be able to deal with
37844 this; also, if the noexcept has no constexpr, then what we parse next
37845 really is a transaction's body. */
37846 parser->in_transaction = this_in;
37848 /* Parse a noexcept specification. */
37849 noex = cp_parser_noexcept_specification_opt (parser, false, &noex_expr,
37850 true);
37852 if (!noex || !noex_expr
37853 || cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
37855 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
37857 expr = cp_parser_expression (parser);
37858 expr = finish_parenthesized_expr (expr);
37860 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
37862 else
37864 /* The only expression that is available got parsed for the noexcept
37865 already. noexcept is true then. */
37866 expr = noex;
37867 noex = boolean_true_node;
37870 expr = build_transaction_expr (token->location, expr, this_in, noex);
37871 parser->in_transaction = old_in;
37873 if (cp_parser_non_integral_constant_expression (parser, NIC_TRANSACTION))
37874 return error_mark_node;
37876 return (flag_tm ? expr : error_mark_node);
37879 /* Parse a function-transaction-block.
37881 function-transaction-block:
37882 __transaction_atomic txn-attribute[opt] ctor-initializer[opt]
37883 function-body
37884 __transaction_atomic txn-attribute[opt] function-try-block
37885 __transaction_relaxed ctor-initializer[opt] function-body
37886 __transaction_relaxed function-try-block
37889 static bool
37890 cp_parser_function_transaction (cp_parser *parser, enum rid keyword)
37892 unsigned char old_in = parser->in_transaction;
37893 unsigned char new_in = 1;
37894 tree compound_stmt, stmt, attrs;
37895 bool ctor_initializer_p;
37896 cp_token *token;
37898 gcc_assert (keyword == RID_TRANSACTION_ATOMIC
37899 || keyword == RID_TRANSACTION_RELAXED);
37900 token = cp_parser_require_keyword (parser, keyword,
37901 (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
37902 : RT_TRANSACTION_RELAXED));
37903 gcc_assert (token != NULL);
37905 if (keyword == RID_TRANSACTION_RELAXED)
37906 new_in |= TM_STMT_ATTR_RELAXED;
37907 else
37909 attrs = cp_parser_txn_attribute_opt (parser);
37910 if (attrs)
37911 new_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
37914 stmt = begin_transaction_stmt (token->location, &compound_stmt, new_in);
37916 parser->in_transaction = new_in;
37918 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
37919 ctor_initializer_p = cp_parser_function_try_block (parser);
37920 else
37921 ctor_initializer_p = cp_parser_ctor_initializer_opt_and_function_body
37922 (parser, /*in_function_try_block=*/false);
37924 parser->in_transaction = old_in;
37926 finish_transaction_stmt (stmt, compound_stmt, new_in, NULL_TREE);
37928 return ctor_initializer_p;
37931 /* Parse a __transaction_cancel statement.
37933 cancel-statement:
37934 __transaction_cancel txn-attribute[opt] ;
37935 __transaction_cancel txn-attribute[opt] throw-expression ;
37937 ??? Cancel and throw is not yet implemented. */
37939 static tree
37940 cp_parser_transaction_cancel (cp_parser *parser)
37942 cp_token *token;
37943 bool is_outer = false;
37944 tree stmt, attrs;
37946 token = cp_parser_require_keyword (parser, RID_TRANSACTION_CANCEL,
37947 RT_TRANSACTION_CANCEL);
37948 gcc_assert (token != NULL);
37950 attrs = cp_parser_txn_attribute_opt (parser);
37951 if (attrs)
37952 is_outer = (parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER) != 0);
37954 /* ??? Parse cancel-and-throw here. */
37956 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
37958 if (!flag_tm)
37960 error_at (token->location, "%<__transaction_cancel%> without "
37961 "transactional memory support enabled");
37962 return error_mark_node;
37964 else if (parser->in_transaction & TM_STMT_ATTR_RELAXED)
37966 error_at (token->location, "%<__transaction_cancel%> within a "
37967 "%<__transaction_relaxed%>");
37968 return error_mark_node;
37970 else if (is_outer)
37972 if ((parser->in_transaction & TM_STMT_ATTR_OUTER) == 0
37973 && !is_tm_may_cancel_outer (current_function_decl))
37975 error_at (token->location, "outer %<__transaction_cancel%> not "
37976 "within outer %<__transaction_atomic%>");
37977 error_at (token->location,
37978 " or a %<transaction_may_cancel_outer%> function");
37979 return error_mark_node;
37982 else if (parser->in_transaction == 0)
37984 error_at (token->location, "%<__transaction_cancel%> not within "
37985 "%<__transaction_atomic%>");
37986 return error_mark_node;
37989 stmt = build_tm_abort_call (token->location, is_outer);
37990 add_stmt (stmt);
37992 return stmt;
37995 /* The parser. */
37997 static GTY (()) cp_parser *the_parser;
38000 /* Special handling for the first token or line in the file. The first
38001 thing in the file might be #pragma GCC pch_preprocess, which loads a
38002 PCH file, which is a GC collection point. So we need to handle this
38003 first pragma without benefit of an existing lexer structure.
38005 Always returns one token to the caller in *FIRST_TOKEN. This is
38006 either the true first token of the file, or the first token after
38007 the initial pragma. */
38009 static void
38010 cp_parser_initial_pragma (cp_token *first_token)
38012 tree name = NULL;
38014 cp_lexer_get_preprocessor_token (NULL, first_token);
38015 if (cp_parser_pragma_kind (first_token) != PRAGMA_GCC_PCH_PREPROCESS)
38016 return;
38018 cp_lexer_get_preprocessor_token (NULL, first_token);
38019 if (first_token->type == CPP_STRING)
38021 name = first_token->u.value;
38023 cp_lexer_get_preprocessor_token (NULL, first_token);
38024 if (first_token->type != CPP_PRAGMA_EOL)
38025 error_at (first_token->location,
38026 "junk at end of %<#pragma GCC pch_preprocess%>");
38028 else
38029 error_at (first_token->location, "expected string literal");
38031 /* Skip to the end of the pragma. */
38032 while (first_token->type != CPP_PRAGMA_EOL && first_token->type != CPP_EOF)
38033 cp_lexer_get_preprocessor_token (NULL, first_token);
38035 /* Now actually load the PCH file. */
38036 if (name)
38037 c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
38039 /* Read one more token to return to our caller. We have to do this
38040 after reading the PCH file in, since its pointers have to be
38041 live. */
38042 cp_lexer_get_preprocessor_token (NULL, first_token);
38045 /* Parses the grainsize pragma for the _Cilk_for statement.
38046 Syntax:
38047 #pragma cilk grainsize = <VALUE>. */
38049 static void
38050 cp_parser_cilk_grainsize (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
38052 if (cp_parser_require (parser, CPP_EQ, RT_EQ))
38054 tree exp = cp_parser_binary_expression (parser, false, false,
38055 PREC_NOT_OPERATOR, NULL);
38056 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
38057 if (!exp || exp == error_mark_node)
38059 error_at (pragma_tok->location, "invalid grainsize for _Cilk_for");
38060 return;
38063 /* Make sure the next token is _Cilk_for, it is invalid otherwise. */
38064 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CILK_FOR))
38065 cp_parser_cilk_for (parser, exp, if_p);
38066 else
38067 warning_at (cp_lexer_peek_token (parser->lexer)->location, 0,
38068 "%<#pragma cilk grainsize%> is not followed by "
38069 "%<_Cilk_for%>");
38070 return;
38072 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
38075 /* Normal parsing of a pragma token. Here we can (and must) use the
38076 regular lexer. */
38078 static bool
38079 cp_parser_pragma (cp_parser *parser, enum pragma_context context, bool *if_p)
38081 cp_token *pragma_tok;
38082 unsigned int id;
38083 tree stmt;
38084 bool ret;
38086 pragma_tok = cp_lexer_consume_token (parser->lexer);
38087 gcc_assert (pragma_tok->type == CPP_PRAGMA);
38088 parser->lexer->in_pragma = true;
38090 id = cp_parser_pragma_kind (pragma_tok);
38091 if (id != PRAGMA_OMP_DECLARE && id != PRAGMA_OACC_ROUTINE)
38092 cp_ensure_no_omp_declare_simd (parser);
38093 switch (id)
38095 case PRAGMA_GCC_PCH_PREPROCESS:
38096 error_at (pragma_tok->location,
38097 "%<#pragma GCC pch_preprocess%> must be first");
38098 break;
38100 case PRAGMA_OMP_BARRIER:
38101 switch (context)
38103 case pragma_compound:
38104 cp_parser_omp_barrier (parser, pragma_tok);
38105 return false;
38106 case pragma_stmt:
38107 error_at (pragma_tok->location, "%<#pragma omp barrier%> may only be "
38108 "used in compound statements");
38109 break;
38110 default:
38111 goto bad_stmt;
38113 break;
38115 case PRAGMA_OMP_FLUSH:
38116 switch (context)
38118 case pragma_compound:
38119 cp_parser_omp_flush (parser, pragma_tok);
38120 return false;
38121 case pragma_stmt:
38122 error_at (pragma_tok->location, "%<#pragma omp flush%> may only be "
38123 "used in compound statements");
38124 break;
38125 default:
38126 goto bad_stmt;
38128 break;
38130 case PRAGMA_OMP_TASKWAIT:
38131 switch (context)
38133 case pragma_compound:
38134 cp_parser_omp_taskwait (parser, pragma_tok);
38135 return false;
38136 case pragma_stmt:
38137 error_at (pragma_tok->location,
38138 "%<#pragma omp taskwait%> may only be "
38139 "used in compound statements");
38140 break;
38141 default:
38142 goto bad_stmt;
38144 break;
38146 case PRAGMA_OMP_TASKYIELD:
38147 switch (context)
38149 case pragma_compound:
38150 cp_parser_omp_taskyield (parser, pragma_tok);
38151 return false;
38152 case pragma_stmt:
38153 error_at (pragma_tok->location,
38154 "%<#pragma omp taskyield%> may only be "
38155 "used in compound statements");
38156 break;
38157 default:
38158 goto bad_stmt;
38160 break;
38162 case PRAGMA_OMP_CANCEL:
38163 switch (context)
38165 case pragma_compound:
38166 cp_parser_omp_cancel (parser, pragma_tok);
38167 return false;
38168 case pragma_stmt:
38169 error_at (pragma_tok->location,
38170 "%<#pragma omp cancel%> may only be "
38171 "used in compound statements");
38172 break;
38173 default:
38174 goto bad_stmt;
38176 break;
38178 case PRAGMA_OMP_CANCELLATION_POINT:
38179 cp_parser_omp_cancellation_point (parser, pragma_tok, context);
38180 return false;
38182 case PRAGMA_OMP_THREADPRIVATE:
38183 cp_parser_omp_threadprivate (parser, pragma_tok);
38184 return false;
38186 case PRAGMA_OMP_DECLARE:
38187 cp_parser_omp_declare (parser, pragma_tok, context);
38188 return false;
38190 case PRAGMA_OACC_DECLARE:
38191 cp_parser_oacc_declare (parser, pragma_tok);
38192 return false;
38194 case PRAGMA_OACC_ENTER_DATA:
38195 if (context == pragma_stmt)
38197 cp_parser_error (parser, "%<#pragma acc enter data%> may only be "
38198 "used in compound statements");
38199 break;
38201 else if (context != pragma_compound)
38202 goto bad_stmt;
38203 cp_parser_omp_construct (parser, pragma_tok, if_p);
38204 return true;
38206 case PRAGMA_OACC_EXIT_DATA:
38207 if (context == pragma_stmt)
38209 cp_parser_error (parser, "%<#pragma acc exit data%> may only be "
38210 "used in compound statements");
38211 break;
38213 else if (context != pragma_compound)
38214 goto bad_stmt;
38215 cp_parser_omp_construct (parser, pragma_tok, if_p);
38216 return true;
38218 case PRAGMA_OACC_ROUTINE:
38219 if (context != pragma_external)
38221 error_at (pragma_tok->location,
38222 "%<#pragma acc routine%> must be at file scope");
38223 break;
38225 cp_parser_oacc_routine (parser, pragma_tok, context);
38226 return false;
38228 case PRAGMA_OACC_UPDATE:
38229 if (context == pragma_stmt)
38231 cp_parser_error (parser, "%<#pragma acc update%> may only be "
38232 "used in compound statements");
38233 break;
38235 else if (context != pragma_compound)
38236 goto bad_stmt;
38237 cp_parser_omp_construct (parser, pragma_tok, if_p);
38238 return true;
38240 case PRAGMA_OACC_WAIT:
38241 if (context == pragma_stmt)
38243 cp_parser_error (parser, "%<#pragma acc wait%> may only be "
38244 "used in compound statements");
38245 break;
38247 else if (context != pragma_compound)
38248 goto bad_stmt;
38249 cp_parser_omp_construct (parser, pragma_tok, if_p);
38250 return true;
38252 case PRAGMA_OACC_ATOMIC:
38253 case PRAGMA_OACC_CACHE:
38254 case PRAGMA_OACC_DATA:
38255 case PRAGMA_OACC_HOST_DATA:
38256 case PRAGMA_OACC_KERNELS:
38257 case PRAGMA_OACC_PARALLEL:
38258 case PRAGMA_OACC_LOOP:
38259 case PRAGMA_OMP_ATOMIC:
38260 case PRAGMA_OMP_CRITICAL:
38261 case PRAGMA_OMP_DISTRIBUTE:
38262 case PRAGMA_OMP_FOR:
38263 case PRAGMA_OMP_MASTER:
38264 case PRAGMA_OMP_PARALLEL:
38265 case PRAGMA_OMP_SECTIONS:
38266 case PRAGMA_OMP_SIMD:
38267 case PRAGMA_OMP_SINGLE:
38268 case PRAGMA_OMP_TASK:
38269 case PRAGMA_OMP_TASKGROUP:
38270 case PRAGMA_OMP_TASKLOOP:
38271 case PRAGMA_OMP_TEAMS:
38272 if (context != pragma_stmt && context != pragma_compound)
38273 goto bad_stmt;
38274 stmt = push_omp_privatization_clauses (false);
38275 cp_parser_omp_construct (parser, pragma_tok, if_p);
38276 pop_omp_privatization_clauses (stmt);
38277 return true;
38279 case PRAGMA_OMP_ORDERED:
38280 if (context != pragma_stmt && context != pragma_compound)
38281 goto bad_stmt;
38282 stmt = push_omp_privatization_clauses (false);
38283 ret = cp_parser_omp_ordered (parser, pragma_tok, context, if_p);
38284 pop_omp_privatization_clauses (stmt);
38285 return ret;
38287 case PRAGMA_OMP_TARGET:
38288 if (context != pragma_stmt && context != pragma_compound)
38289 goto bad_stmt;
38290 stmt = push_omp_privatization_clauses (false);
38291 ret = cp_parser_omp_target (parser, pragma_tok, context, if_p);
38292 pop_omp_privatization_clauses (stmt);
38293 return ret;
38295 case PRAGMA_OMP_END_DECLARE_TARGET:
38296 cp_parser_omp_end_declare_target (parser, pragma_tok);
38297 return false;
38299 case PRAGMA_OMP_SECTION:
38300 error_at (pragma_tok->location,
38301 "%<#pragma omp section%> may only be used in "
38302 "%<#pragma omp sections%> construct");
38303 break;
38305 case PRAGMA_IVDEP:
38307 if (context == pragma_external)
38309 error_at (pragma_tok->location,
38310 "%<#pragma GCC ivdep%> must be inside a function");
38311 break;
38313 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
38314 cp_token *tok;
38315 tok = cp_lexer_peek_token (the_parser->lexer);
38316 if (tok->type != CPP_KEYWORD
38317 || (tok->keyword != RID_FOR && tok->keyword != RID_WHILE
38318 && tok->keyword != RID_DO))
38320 cp_parser_error (parser, "for, while or do statement expected");
38321 return false;
38323 cp_parser_iteration_statement (parser, if_p, true);
38324 return true;
38327 case PRAGMA_CILK_SIMD:
38328 if (context == pragma_external)
38330 error_at (pragma_tok->location,
38331 "%<#pragma simd%> must be inside a function");
38332 break;
38334 stmt = push_omp_privatization_clauses (false);
38335 cp_parser_cilk_simd (parser, pragma_tok, if_p);
38336 pop_omp_privatization_clauses (stmt);
38337 return true;
38339 case PRAGMA_CILK_GRAINSIZE:
38340 if (context == pragma_external)
38342 error_at (pragma_tok->location,
38343 "%<#pragma cilk grainsize%> must be inside a function");
38344 break;
38347 /* Ignore the pragma if Cilk Plus is not enabled. */
38348 if (flag_cilkplus)
38350 cp_parser_cilk_grainsize (parser, pragma_tok, if_p);
38351 return true;
38353 else
38355 error_at (pragma_tok->location, "-fcilkplus must be enabled to use "
38356 "%<#pragma cilk grainsize%>");
38357 break;
38360 default:
38361 gcc_assert (id >= PRAGMA_FIRST_EXTERNAL);
38362 c_invoke_pragma_handler (id);
38363 break;
38365 bad_stmt:
38366 cp_parser_error (parser, "expected declaration specifiers");
38367 break;
38370 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
38371 return false;
38374 /* The interface the pragma parsers have to the lexer. */
38376 enum cpp_ttype
38377 pragma_lex (tree *value, location_t *loc)
38379 cp_token *tok = cp_lexer_peek_token (the_parser->lexer);
38380 enum cpp_ttype ret = tok->type;
38382 *value = tok->u.value;
38383 if (loc)
38384 *loc = tok->location;
38386 if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
38387 ret = CPP_EOF;
38388 else if (ret == CPP_STRING)
38389 *value = cp_parser_string_literal (the_parser, false, false);
38390 else
38392 if (ret == CPP_KEYWORD)
38393 ret = CPP_NAME;
38394 cp_lexer_consume_token (the_parser->lexer);
38397 return ret;
38401 /* External interface. */
38403 /* Parse one entire translation unit. */
38405 void
38406 c_parse_file (void)
38408 static bool already_called = false;
38410 if (already_called)
38411 fatal_error (input_location,
38412 "inter-module optimizations not implemented for C++");
38413 already_called = true;
38415 the_parser = cp_parser_new ();
38416 push_deferring_access_checks (flag_access_control
38417 ? dk_no_deferred : dk_no_check);
38418 cp_parser_translation_unit (the_parser);
38419 the_parser = NULL;
38422 /* Parses the Cilk Plus #pragma simd and SIMD-enabled function attribute's
38423 vectorlength clause:
38424 Syntax:
38425 vectorlength ( constant-expression ) */
38427 static tree
38428 cp_parser_cilk_simd_vectorlength (cp_parser *parser, tree clauses,
38429 bool is_simd_fn)
38431 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
38432 tree expr;
38433 /* The vectorlength clause in #pragma simd behaves exactly like OpenMP's
38434 safelen clause. Thus, vectorlength is represented as OMP 4.0
38435 safelen. For SIMD-enabled function it is represented by OMP 4.0
38436 simdlen. */
38437 if (!is_simd_fn)
38438 check_no_duplicate_clause (clauses, OMP_CLAUSE_SAFELEN, "vectorlength",
38439 loc);
38440 else
38441 check_no_duplicate_clause (clauses, OMP_CLAUSE_SIMDLEN, "vectorlength",
38442 loc);
38444 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
38445 return error_mark_node;
38447 expr = cp_parser_constant_expression (parser);
38448 expr = maybe_constant_value (expr);
38450 /* If expr == error_mark_node, then don't emit any errors nor
38451 create a clause. if any of the above functions returns
38452 error mark node then they would have emitted an error message. */
38453 if (expr == error_mark_node)
38455 else if (!TREE_TYPE (expr)
38456 || !TREE_CONSTANT (expr)
38457 || !INTEGRAL_TYPE_P (TREE_TYPE (expr)))
38458 error_at (loc, "vectorlength must be an integer constant");
38459 else if (TREE_CONSTANT (expr)
38460 && !pow2p_hwi (TREE_INT_CST_LOW (expr)))
38461 error_at (loc, "vectorlength must be a power of 2");
38462 else
38464 tree c;
38465 if (!is_simd_fn)
38467 c = build_omp_clause (loc, OMP_CLAUSE_SAFELEN);
38468 OMP_CLAUSE_SAFELEN_EXPR (c) = expr;
38469 OMP_CLAUSE_CHAIN (c) = clauses;
38470 clauses = c;
38472 else
38474 c = build_omp_clause (loc, OMP_CLAUSE_SIMDLEN);
38475 OMP_CLAUSE_SIMDLEN_EXPR (c) = expr;
38476 OMP_CLAUSE_CHAIN (c) = clauses;
38477 clauses = c;
38481 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
38482 return error_mark_node;
38483 return clauses;
38486 /* Handles the Cilk Plus #pragma simd linear clause.
38487 Syntax:
38488 linear ( simd-linear-variable-list )
38490 simd-linear-variable-list:
38491 simd-linear-variable
38492 simd-linear-variable-list , simd-linear-variable
38494 simd-linear-variable:
38495 id-expression
38496 id-expression : simd-linear-step
38498 simd-linear-step:
38499 conditional-expression */
38501 static tree
38502 cp_parser_cilk_simd_linear (cp_parser *parser, tree clauses)
38504 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
38506 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
38507 return clauses;
38508 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
38510 cp_parser_error (parser, "expected identifier");
38511 cp_parser_skip_to_closing_parenthesis (parser, false, false, true);
38512 return error_mark_node;
38515 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
38516 parser->colon_corrects_to_scope_p = false;
38517 while (1)
38519 cp_token *token = cp_lexer_peek_token (parser->lexer);
38520 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
38522 cp_parser_error (parser, "expected variable-name");
38523 clauses = error_mark_node;
38524 break;
38527 tree var_name = cp_parser_id_expression (parser, false, true, NULL,
38528 false, false);
38529 tree decl = cp_parser_lookup_name_simple (parser, var_name,
38530 token->location);
38531 if (decl == error_mark_node)
38533 cp_parser_name_lookup_error (parser, var_name, decl, NLE_NULL,
38534 token->location);
38535 clauses = error_mark_node;
38537 else
38539 tree e = NULL_TREE;
38540 tree step_size = integer_one_node;
38542 /* If present, parse the linear step. Otherwise, assume the default
38543 value of 1. */
38544 if (cp_lexer_peek_token (parser->lexer)->type == CPP_COLON)
38546 cp_lexer_consume_token (parser->lexer);
38548 e = cp_parser_assignment_expression (parser);
38549 e = maybe_constant_value (e);
38551 if (e == error_mark_node)
38553 /* If an error has occurred, then the whole pragma is
38554 considered ill-formed. Thus, no reason to keep
38555 parsing. */
38556 clauses = error_mark_node;
38557 break;
38559 else if (type_dependent_expression_p (e)
38560 || value_dependent_expression_p (e)
38561 || (TREE_TYPE (e)
38562 && INTEGRAL_TYPE_P (TREE_TYPE (e))
38563 && (TREE_CONSTANT (e)
38564 || DECL_P (e))))
38565 step_size = e;
38566 else
38567 cp_parser_error (parser,
38568 "step size must be an integer constant "
38569 "expression or an integer variable");
38572 /* Use the OMP_CLAUSE_LINEAR, which has the same semantics. */
38573 tree l = build_omp_clause (loc, OMP_CLAUSE_LINEAR);
38574 OMP_CLAUSE_DECL (l) = decl;
38575 OMP_CLAUSE_LINEAR_STEP (l) = step_size;
38576 OMP_CLAUSE_CHAIN (l) = clauses;
38577 clauses = l;
38579 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
38580 cp_lexer_consume_token (parser->lexer);
38581 else if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
38582 break;
38583 else
38585 error_at (cp_lexer_peek_token (parser->lexer)->location,
38586 "expected %<,%> or %<)%> after %qE", decl);
38587 clauses = error_mark_node;
38588 break;
38591 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
38592 cp_parser_skip_to_closing_parenthesis (parser, false, false, true);
38593 return clauses;
38596 /* Returns the name of the next clause. If the clause is not
38597 recognized, then PRAGMA_CILK_CLAUSE_NONE is returned and the next
38598 token is not consumed. Otherwise, the appropriate enum from the
38599 pragma_simd_clause is returned and the token is consumed. */
38601 static pragma_omp_clause
38602 cp_parser_cilk_simd_clause_name (cp_parser *parser)
38604 pragma_omp_clause clause_type;
38605 cp_token *token = cp_lexer_peek_token (parser->lexer);
38607 if (token->keyword == RID_PRIVATE)
38608 clause_type = PRAGMA_CILK_CLAUSE_PRIVATE;
38609 else if (!token->u.value || token->type != CPP_NAME)
38610 return PRAGMA_CILK_CLAUSE_NONE;
38611 else if (!strcmp (IDENTIFIER_POINTER (token->u.value), "vectorlength"))
38612 clause_type = PRAGMA_CILK_CLAUSE_VECTORLENGTH;
38613 else if (!strcmp (IDENTIFIER_POINTER (token->u.value), "linear"))
38614 clause_type = PRAGMA_CILK_CLAUSE_LINEAR;
38615 else if (!strcmp (IDENTIFIER_POINTER (token->u.value), "firstprivate"))
38616 clause_type = PRAGMA_CILK_CLAUSE_FIRSTPRIVATE;
38617 else if (!strcmp (IDENTIFIER_POINTER (token->u.value), "lastprivate"))
38618 clause_type = PRAGMA_CILK_CLAUSE_LASTPRIVATE;
38619 else if (!strcmp (IDENTIFIER_POINTER (token->u.value), "reduction"))
38620 clause_type = PRAGMA_CILK_CLAUSE_REDUCTION;
38621 else
38622 return PRAGMA_CILK_CLAUSE_NONE;
38624 cp_lexer_consume_token (parser->lexer);
38625 return clause_type;
38628 /* Parses all the #pragma simd clauses. Returns a list of clauses found. */
38630 static tree
38631 cp_parser_cilk_simd_all_clauses (cp_parser *parser, cp_token *pragma_token)
38633 tree clauses = NULL_TREE;
38635 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
38636 && clauses != error_mark_node)
38638 pragma_omp_clause c_kind;
38639 c_kind = cp_parser_cilk_simd_clause_name (parser);
38640 if (c_kind == PRAGMA_CILK_CLAUSE_VECTORLENGTH)
38641 clauses = cp_parser_cilk_simd_vectorlength (parser, clauses, false);
38642 else if (c_kind == PRAGMA_CILK_CLAUSE_LINEAR)
38643 clauses = cp_parser_cilk_simd_linear (parser, clauses);
38644 else if (c_kind == PRAGMA_CILK_CLAUSE_PRIVATE)
38645 /* Use the OpenMP 4.0 equivalent function. */
38646 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE, clauses);
38647 else if (c_kind == PRAGMA_CILK_CLAUSE_FIRSTPRIVATE)
38648 /* Use the OpenMP 4.0 equivalent function. */
38649 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
38650 clauses);
38651 else if (c_kind == PRAGMA_CILK_CLAUSE_LASTPRIVATE)
38652 /* Use the OMP 4.0 equivalent function. */
38653 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LASTPRIVATE,
38654 clauses);
38655 else if (c_kind == PRAGMA_CILK_CLAUSE_REDUCTION)
38656 /* Use the OMP 4.0 equivalent function. */
38657 clauses = cp_parser_omp_clause_reduction (parser, clauses);
38658 else
38660 clauses = error_mark_node;
38661 cp_parser_error (parser, "expected %<#pragma simd%> clause");
38662 break;
38666 cp_parser_skip_to_pragma_eol (parser, pragma_token);
38668 if (clauses == error_mark_node)
38669 return error_mark_node;
38670 else
38671 return finish_omp_clauses (clauses, C_ORT_CILK);
38674 /* Main entry-point for parsing Cilk Plus <#pragma simd> for loops. */
38676 static void
38677 cp_parser_cilk_simd (cp_parser *parser, cp_token *pragma_token, bool *if_p)
38679 tree clauses = cp_parser_cilk_simd_all_clauses (parser, pragma_token);
38681 if (clauses == error_mark_node)
38682 return;
38684 if (cp_lexer_next_token_is_not_keyword (parser->lexer, RID_FOR))
38686 error_at (cp_lexer_peek_token (parser->lexer)->location,
38687 "for statement expected");
38688 return;
38691 tree sb = begin_omp_structured_block ();
38692 int save = cp_parser_begin_omp_structured_block (parser);
38693 tree ret = cp_parser_omp_for_loop (parser, CILK_SIMD, clauses, NULL, if_p);
38694 if (ret)
38695 cpp_validate_cilk_plus_loop (OMP_FOR_BODY (ret));
38696 cp_parser_end_omp_structured_block (parser, save);
38697 add_stmt (finish_omp_structured_block (sb));
38700 /* Main entry-point for parsing Cilk Plus _Cilk_for
38701 loops. The return value is error_mark_node
38702 when errors happen and CILK_FOR tree on success. */
38704 static tree
38705 cp_parser_cilk_for (cp_parser *parser, tree grain, bool *if_p)
38707 if (cp_lexer_next_token_is_not_keyword (parser->lexer, RID_CILK_FOR))
38708 gcc_unreachable ();
38710 tree sb = begin_omp_structured_block ();
38711 int save = cp_parser_begin_omp_structured_block (parser);
38713 tree clauses = build_omp_clause (EXPR_LOCATION (grain), OMP_CLAUSE_SCHEDULE);
38714 OMP_CLAUSE_SCHEDULE_KIND (clauses) = OMP_CLAUSE_SCHEDULE_CILKFOR;
38715 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (clauses) = grain;
38716 clauses = finish_omp_clauses (clauses, C_ORT_CILK);
38718 tree ret = cp_parser_omp_for_loop (parser, CILK_FOR, clauses, NULL, if_p);
38719 if (ret)
38720 cpp_validate_cilk_plus_loop (ret);
38721 else
38722 ret = error_mark_node;
38724 cp_parser_end_omp_structured_block (parser, save);
38725 add_stmt (finish_omp_structured_block (sb));
38726 return ret;
38729 /* Create an identifier for a generic parameter type (a synthesized
38730 template parameter implied by `auto' or a concept identifier). */
38732 static GTY(()) int generic_parm_count;
38733 static tree
38734 make_generic_type_name ()
38736 char buf[32];
38737 sprintf (buf, "auto:%d", ++generic_parm_count);
38738 return get_identifier (buf);
38741 /* Predicate that behaves as is_auto_or_concept but matches the parent
38742 node of the generic type rather than the generic type itself. This
38743 allows for type transformation in add_implicit_template_parms. */
38745 static inline bool
38746 tree_type_is_auto_or_concept (const_tree t)
38748 return TREE_TYPE (t) && is_auto_or_concept (TREE_TYPE (t));
38751 /* Add an implicit template type parameter to the CURRENT_TEMPLATE_PARMS
38752 (creating a new template parameter list if necessary). Returns the newly
38753 created template type parm. */
38755 static tree
38756 synthesize_implicit_template_parm (cp_parser *parser, tree constr)
38758 gcc_assert (current_binding_level->kind == sk_function_parms);
38760 /* Before committing to modifying any scope, if we're in an
38761 implicit template scope, and we're trying to synthesize a
38762 constrained parameter, try to find a previous parameter with
38763 the same name. This is the same-type rule for abbreviated
38764 function templates.
38766 NOTE: We can generate implicit parameters when tentatively
38767 parsing a nested name specifier, only to reject that parse
38768 later. However, matching the same template-id as part of a
38769 direct-declarator should generate an identical template
38770 parameter, so this rule will merge them. */
38771 if (parser->implicit_template_scope && constr)
38773 tree t = parser->implicit_template_parms;
38774 while (t)
38776 if (equivalent_placeholder_constraints (TREE_TYPE (t), constr))
38778 tree d = TREE_VALUE (t);
38779 if (TREE_CODE (d) == PARM_DECL)
38780 /* Return the TEMPLATE_PARM_INDEX. */
38781 d = DECL_INITIAL (d);
38782 return d;
38784 t = TREE_CHAIN (t);
38788 /* We are either continuing a function template that already contains implicit
38789 template parameters, creating a new fully-implicit function template, or
38790 extending an existing explicit function template with implicit template
38791 parameters. */
38793 cp_binding_level *const entry_scope = current_binding_level;
38795 bool become_template = false;
38796 cp_binding_level *parent_scope = 0;
38798 if (parser->implicit_template_scope)
38800 gcc_assert (parser->implicit_template_parms);
38802 current_binding_level = parser->implicit_template_scope;
38804 else
38806 /* Roll back to the existing template parameter scope (in the case of
38807 extending an explicit function template) or introduce a new template
38808 parameter scope ahead of the function parameter scope (or class scope
38809 in the case of out-of-line member definitions). The function scope is
38810 added back after template parameter synthesis below. */
38812 cp_binding_level *scope = entry_scope;
38814 while (scope->kind == sk_function_parms)
38816 parent_scope = scope;
38817 scope = scope->level_chain;
38819 if (current_class_type && !LAMBDA_TYPE_P (current_class_type))
38821 /* If not defining a class, then any class scope is a scope level in
38822 an out-of-line member definition. In this case simply wind back
38823 beyond the first such scope to inject the template parameter list.
38824 Otherwise wind back to the class being defined. The latter can
38825 occur in class member friend declarations such as:
38827 class A {
38828 void foo (auto);
38830 class B {
38831 friend void A::foo (auto);
38834 The template parameter list synthesized for the friend declaration
38835 must be injected in the scope of 'B'. This can also occur in
38836 erroneous cases such as:
38838 struct A {
38839 struct B {
38840 void foo (auto);
38842 void B::foo (auto) {}
38845 Here the attempted definition of 'B::foo' within 'A' is ill-formed
38846 but, nevertheless, the template parameter list synthesized for the
38847 declarator should be injected into the scope of 'A' as if the
38848 ill-formed template was specified explicitly. */
38850 while (scope->kind == sk_class && !scope->defining_class_p)
38852 parent_scope = scope;
38853 scope = scope->level_chain;
38857 current_binding_level = scope;
38859 if (scope->kind != sk_template_parms
38860 || !function_being_declared_is_template_p (parser))
38862 /* Introduce a new template parameter list for implicit template
38863 parameters. */
38865 become_template = true;
38867 parser->implicit_template_scope
38868 = begin_scope (sk_template_parms, NULL);
38870 ++processing_template_decl;
38872 parser->fully_implicit_function_template_p = true;
38873 ++parser->num_template_parameter_lists;
38875 else
38877 /* Synthesize implicit template parameters at the end of the explicit
38878 template parameter list. */
38880 gcc_assert (current_template_parms);
38882 parser->implicit_template_scope = scope;
38884 tree v = INNERMOST_TEMPLATE_PARMS (current_template_parms);
38885 parser->implicit_template_parms
38886 = TREE_VEC_ELT (v, TREE_VEC_LENGTH (v) - 1);
38890 /* Synthesize a new template parameter and track the current template
38891 parameter chain with implicit_template_parms. */
38893 tree proto = constr ? DECL_INITIAL (constr) : NULL_TREE;
38894 tree synth_id = make_generic_type_name ();
38895 tree synth_tmpl_parm;
38896 bool non_type = false;
38898 if (proto == NULL_TREE || TREE_CODE (proto) == TYPE_DECL)
38899 synth_tmpl_parm
38900 = finish_template_type_parm (class_type_node, synth_id);
38901 else if (TREE_CODE (proto) == TEMPLATE_DECL)
38902 synth_tmpl_parm
38903 = finish_constrained_template_template_parm (proto, synth_id);
38904 else
38906 synth_tmpl_parm = copy_decl (proto);
38907 DECL_NAME (synth_tmpl_parm) = synth_id;
38908 non_type = true;
38911 // Attach the constraint to the parm before processing.
38912 tree node = build_tree_list (NULL_TREE, synth_tmpl_parm);
38913 TREE_TYPE (node) = constr;
38914 tree new_parm
38915 = process_template_parm (parser->implicit_template_parms,
38916 input_location,
38917 node,
38918 /*non_type=*/non_type,
38919 /*param_pack=*/false);
38921 // Chain the new parameter to the list of implicit parameters.
38922 if (parser->implicit_template_parms)
38923 parser->implicit_template_parms
38924 = TREE_CHAIN (parser->implicit_template_parms);
38925 else
38926 parser->implicit_template_parms = new_parm;
38928 tree new_decl = getdecls ();
38929 if (non_type)
38930 /* Return the TEMPLATE_PARM_INDEX, not the PARM_DECL. */
38931 new_decl = DECL_INITIAL (new_decl);
38933 /* If creating a fully implicit function template, start the new implicit
38934 template parameter list with this synthesized type, otherwise grow the
38935 current template parameter list. */
38937 if (become_template)
38939 parent_scope->level_chain = current_binding_level;
38941 tree new_parms = make_tree_vec (1);
38942 TREE_VEC_ELT (new_parms, 0) = parser->implicit_template_parms;
38943 current_template_parms = tree_cons (size_int (processing_template_decl),
38944 new_parms, current_template_parms);
38946 else
38948 tree& new_parms = INNERMOST_TEMPLATE_PARMS (current_template_parms);
38949 int new_parm_idx = TREE_VEC_LENGTH (new_parms);
38950 new_parms = grow_tree_vec (new_parms, new_parm_idx + 1);
38951 TREE_VEC_ELT (new_parms, new_parm_idx) = parser->implicit_template_parms;
38954 // If the new parameter was constrained, we need to add that to the
38955 // constraints in the template parameter list.
38956 if (tree req = TEMPLATE_PARM_CONSTRAINTS (tree_last (new_parm)))
38958 tree reqs = TEMPLATE_PARMS_CONSTRAINTS (current_template_parms);
38959 reqs = conjoin_constraints (reqs, req);
38960 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) = reqs;
38963 current_binding_level = entry_scope;
38965 return new_decl;
38968 /* Finish the declaration of a fully implicit function template. Such a
38969 template has no explicit template parameter list so has not been through the
38970 normal template head and tail processing. synthesize_implicit_template_parm
38971 tries to do the head; this tries to do the tail. MEMBER_DECL_OPT should be
38972 provided if the declaration is a class member such that its template
38973 declaration can be completed. If MEMBER_DECL_OPT is provided the finished
38974 form is returned. Otherwise NULL_TREE is returned. */
38976 static tree
38977 finish_fully_implicit_template (cp_parser *parser, tree member_decl_opt)
38979 gcc_assert (parser->fully_implicit_function_template_p);
38981 if (member_decl_opt && member_decl_opt != error_mark_node
38982 && DECL_VIRTUAL_P (member_decl_opt))
38984 error_at (DECL_SOURCE_LOCATION (member_decl_opt),
38985 "implicit templates may not be %<virtual%>");
38986 DECL_VIRTUAL_P (member_decl_opt) = false;
38989 if (member_decl_opt)
38990 member_decl_opt = finish_member_template_decl (member_decl_opt);
38991 end_template_decl ();
38993 parser->fully_implicit_function_template_p = false;
38994 --parser->num_template_parameter_lists;
38996 return member_decl_opt;
38999 #include "gt-cp-parser.h"