2017-02-19 Paul Thomas <pault@gcc.gnu.org>
[official-gcc.git] / gcc / cp / parser.c
blobfeeafcec6ad21c37b4e86db5b3b4fb07d14792d0
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) == FUNCTION_DECL
6938 && warn_restrict)
6940 unsigned i;
6941 tree arg;
6942 FOR_EACH_VEC_SAFE_ELT (args, i, arg)
6943 TREE_VISITED (arg) = 0;
6945 unsigned param_pos = 0;
6946 for (tree decl = DECL_ARGUMENTS (postfix_expression);
6947 decl != NULL_TREE;
6948 decl = DECL_CHAIN (decl), param_pos++)
6950 tree type = TREE_TYPE (decl);
6951 if (POINTER_TYPE_P (type) && TYPE_RESTRICT (type)
6952 && !TYPE_READONLY (TREE_TYPE (type)))
6953 warn_for_restrict (param_pos, args);
6956 FOR_EACH_VEC_SAFE_ELT (args, i, arg)
6957 TREE_VISITED (arg) = 0;
6960 if (TREE_CODE (postfix_expression) == COMPONENT_REF)
6962 tree instance = TREE_OPERAND (postfix_expression, 0);
6963 tree fn = TREE_OPERAND (postfix_expression, 1);
6965 if (processing_template_decl
6966 && (type_dependent_object_expression_p (instance)
6967 || (!BASELINK_P (fn)
6968 && TREE_CODE (fn) != FIELD_DECL)
6969 || type_dependent_expression_p (fn)
6970 || any_type_dependent_arguments_p (args)))
6972 maybe_generic_this_capture (instance, fn);
6973 postfix_expression
6974 = build_nt_call_vec (postfix_expression, args);
6975 release_tree_vector (args);
6976 break;
6979 if (BASELINK_P (fn))
6981 postfix_expression
6982 = (build_new_method_call
6983 (instance, fn, &args, NULL_TREE,
6984 (idk == CP_ID_KIND_QUALIFIED
6985 ? LOOKUP_NORMAL|LOOKUP_NONVIRTUAL
6986 : LOOKUP_NORMAL),
6987 /*fn_p=*/NULL,
6988 complain));
6990 else
6991 postfix_expression
6992 = finish_call_expr (postfix_expression, &args,
6993 /*disallow_virtual=*/false,
6994 /*koenig_p=*/false,
6995 complain);
6997 else if (TREE_CODE (postfix_expression) == OFFSET_REF
6998 || TREE_CODE (postfix_expression) == MEMBER_REF
6999 || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
7000 postfix_expression = (build_offset_ref_call_from_tree
7001 (postfix_expression, &args,
7002 complain));
7003 else if (idk == CP_ID_KIND_QUALIFIED)
7004 /* A call to a static class member, or a namespace-scope
7005 function. */
7006 postfix_expression
7007 = finish_call_expr (postfix_expression, &args,
7008 /*disallow_virtual=*/true,
7009 koenig_p,
7010 complain);
7011 else
7012 /* All other function calls. */
7013 postfix_expression
7014 = finish_call_expr (postfix_expression, &args,
7015 /*disallow_virtual=*/false,
7016 koenig_p,
7017 complain);
7019 if (close_paren_loc != UNKNOWN_LOCATION)
7021 location_t combined_loc = make_location (token->location,
7022 start_loc,
7023 close_paren_loc);
7024 postfix_expression.set_location (combined_loc);
7027 /* The POSTFIX_EXPRESSION is certainly no longer an id. */
7028 idk = CP_ID_KIND_NONE;
7030 release_tree_vector (args);
7032 break;
7034 case CPP_DOT:
7035 case CPP_DEREF:
7036 /* postfix-expression . template [opt] id-expression
7037 postfix-expression . pseudo-destructor-name
7038 postfix-expression -> template [opt] id-expression
7039 postfix-expression -> pseudo-destructor-name */
7041 /* Consume the `.' or `->' operator. */
7042 cp_lexer_consume_token (parser->lexer);
7044 postfix_expression
7045 = cp_parser_postfix_dot_deref_expression (parser, token->type,
7046 postfix_expression,
7047 false, &idk, loc);
7049 is_member_access = true;
7050 break;
7052 case CPP_PLUS_PLUS:
7053 /* postfix-expression ++ */
7054 /* Consume the `++' token. */
7055 cp_lexer_consume_token (parser->lexer);
7056 /* Generate a representation for the complete expression. */
7057 postfix_expression
7058 = finish_increment_expr (postfix_expression,
7059 POSTINCREMENT_EXPR);
7060 /* Increments may not appear in constant-expressions. */
7061 if (cp_parser_non_integral_constant_expression (parser, NIC_INC))
7062 postfix_expression = error_mark_node;
7063 idk = CP_ID_KIND_NONE;
7064 is_member_access = false;
7065 break;
7067 case CPP_MINUS_MINUS:
7068 /* postfix-expression -- */
7069 /* Consume the `--' token. */
7070 cp_lexer_consume_token (parser->lexer);
7071 /* Generate a representation for the complete expression. */
7072 postfix_expression
7073 = finish_increment_expr (postfix_expression,
7074 POSTDECREMENT_EXPR);
7075 /* Decrements may not appear in constant-expressions. */
7076 if (cp_parser_non_integral_constant_expression (parser, NIC_DEC))
7077 postfix_expression = error_mark_node;
7078 idk = CP_ID_KIND_NONE;
7079 is_member_access = false;
7080 break;
7082 default:
7083 if (pidk_return != NULL)
7084 * pidk_return = idk;
7085 if (member_access_only_p)
7086 return is_member_access
7087 ? postfix_expression
7088 : cp_expr (error_mark_node);
7089 else
7090 return postfix_expression;
7094 /* We should never get here. */
7095 gcc_unreachable ();
7096 return error_mark_node;
7099 /* This function parses Cilk Plus array notations. If a normal array expr. is
7100 parsed then the array index is passed back to the caller through *INIT_INDEX
7101 and the function returns a NULL_TREE. If array notation expr. is parsed,
7102 then *INIT_INDEX is ignored by the caller and the function returns
7103 a tree of type ARRAY_NOTATION_REF. If some error occurred it returns
7104 error_mark_node. */
7106 static tree
7107 cp_parser_array_notation (location_t loc, cp_parser *parser, tree *init_index,
7108 tree array_value)
7110 cp_token *token = NULL;
7111 tree length_index, stride = NULL_TREE, value_tree, array_type;
7112 if (!array_value || array_value == error_mark_node)
7114 cp_parser_skip_to_end_of_statement (parser);
7115 return error_mark_node;
7118 array_type = TREE_TYPE (array_value);
7120 bool saved_colon_corrects = parser->colon_corrects_to_scope_p;
7121 parser->colon_corrects_to_scope_p = false;
7122 token = cp_lexer_peek_token (parser->lexer);
7124 if (!token)
7126 cp_parser_error (parser, "expected %<:%> or numeral");
7127 return error_mark_node;
7129 else if (token->type == CPP_COLON)
7131 /* Consume the ':'. */
7132 cp_lexer_consume_token (parser->lexer);
7134 /* If we are here, then we have a case like this A[:]. */
7135 if (cp_lexer_peek_token (parser->lexer)->type != CPP_CLOSE_SQUARE)
7137 cp_parser_error (parser, "expected %<]%>");
7138 cp_parser_skip_to_end_of_statement (parser);
7139 return error_mark_node;
7141 *init_index = NULL_TREE;
7142 stride = NULL_TREE;
7143 length_index = NULL_TREE;
7145 else
7147 /* If we are here, then there are three valid possibilities:
7148 1. ARRAY [ EXP ]
7149 2. ARRAY [ EXP : EXP ]
7150 3. ARRAY [ EXP : EXP : EXP ] */
7152 *init_index = cp_parser_expression (parser);
7153 if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
7155 /* This indicates that we have a normal array expression. */
7156 parser->colon_corrects_to_scope_p = saved_colon_corrects;
7157 return NULL_TREE;
7160 /* Consume the ':'. */
7161 cp_lexer_consume_token (parser->lexer);
7162 length_index = cp_parser_expression (parser);
7163 if (cp_lexer_peek_token (parser->lexer)->type == CPP_COLON)
7165 cp_lexer_consume_token (parser->lexer);
7166 stride = cp_parser_expression (parser);
7169 parser->colon_corrects_to_scope_p = saved_colon_corrects;
7171 if (*init_index == error_mark_node || length_index == error_mark_node
7172 || stride == error_mark_node || array_type == error_mark_node)
7174 if (cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_SQUARE)
7175 cp_lexer_consume_token (parser->lexer);
7176 return error_mark_node;
7178 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
7180 value_tree = build_array_notation_ref (loc, array_value, *init_index,
7181 length_index, stride, array_type);
7182 return value_tree;
7185 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
7186 by cp_parser_builtin_offsetof. We're looking for
7188 postfix-expression [ expression ]
7189 postfix-expression [ braced-init-list ] (C++11)
7191 FOR_OFFSETOF is set if we're being called in that context, which
7192 changes how we deal with integer constant expressions. */
7194 static tree
7195 cp_parser_postfix_open_square_expression (cp_parser *parser,
7196 tree postfix_expression,
7197 bool for_offsetof,
7198 bool decltype_p)
7200 tree index = NULL_TREE;
7201 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
7202 bool saved_greater_than_is_operator_p;
7204 /* Consume the `[' token. */
7205 cp_lexer_consume_token (parser->lexer);
7207 saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
7208 parser->greater_than_is_operator_p = true;
7210 /* Parse the index expression. */
7211 /* ??? For offsetof, there is a question of what to allow here. If
7212 offsetof is not being used in an integral constant expression context,
7213 then we *could* get the right answer by computing the value at runtime.
7214 If we are in an integral constant expression context, then we might
7215 could accept any constant expression; hard to say without analysis.
7216 Rather than open the barn door too wide right away, allow only integer
7217 constant expressions here. */
7218 if (for_offsetof)
7219 index = cp_parser_constant_expression (parser);
7220 else
7222 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7224 bool expr_nonconst_p;
7225 cp_lexer_set_source_position (parser->lexer);
7226 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
7227 index = cp_parser_braced_list (parser, &expr_nonconst_p);
7228 if (flag_cilkplus
7229 && cp_lexer_peek_token (parser->lexer)->type == CPP_COLON)
7231 error_at (cp_lexer_peek_token (parser->lexer)->location,
7232 "braced list index is not allowed with array "
7233 "notation");
7234 cp_parser_skip_to_end_of_statement (parser);
7235 return error_mark_node;
7238 else if (flag_cilkplus)
7240 /* Here are have these two options:
7241 ARRAY[EXP : EXP] - Array notation expr with default
7242 stride of 1.
7243 ARRAY[EXP : EXP : EXP] - Array Notation with user-defined
7244 stride. */
7245 tree an_exp = cp_parser_array_notation (loc, parser, &index,
7246 postfix_expression);
7247 if (an_exp)
7248 return an_exp;
7250 else
7251 index = cp_parser_expression (parser);
7254 parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
7256 /* Look for the closing `]'. */
7257 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
7259 /* Build the ARRAY_REF. */
7260 postfix_expression = grok_array_decl (loc, postfix_expression,
7261 index, decltype_p);
7263 /* When not doing offsetof, array references are not permitted in
7264 constant-expressions. */
7265 if (!for_offsetof
7266 && (cp_parser_non_integral_constant_expression (parser, NIC_ARRAY_REF)))
7267 postfix_expression = error_mark_node;
7269 return postfix_expression;
7272 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
7273 by cp_parser_builtin_offsetof. We're looking for
7275 postfix-expression . template [opt] id-expression
7276 postfix-expression . pseudo-destructor-name
7277 postfix-expression -> template [opt] id-expression
7278 postfix-expression -> pseudo-destructor-name
7280 FOR_OFFSETOF is set if we're being called in that context. That sorta
7281 limits what of the above we'll actually accept, but nevermind.
7282 TOKEN_TYPE is the "." or "->" token, which will already have been
7283 removed from the stream. */
7285 static tree
7286 cp_parser_postfix_dot_deref_expression (cp_parser *parser,
7287 enum cpp_ttype token_type,
7288 cp_expr postfix_expression,
7289 bool for_offsetof, cp_id_kind *idk,
7290 location_t location)
7292 tree name;
7293 bool dependent_p;
7294 bool pseudo_destructor_p;
7295 tree scope = NULL_TREE;
7296 location_t start_loc = postfix_expression.get_start ();
7298 /* If this is a `->' operator, dereference the pointer. */
7299 if (token_type == CPP_DEREF)
7300 postfix_expression = build_x_arrow (location, postfix_expression,
7301 tf_warning_or_error);
7302 /* Check to see whether or not the expression is type-dependent and
7303 not the current instantiation. */
7304 dependent_p = type_dependent_object_expression_p (postfix_expression);
7305 /* The identifier following the `->' or `.' is not qualified. */
7306 parser->scope = NULL_TREE;
7307 parser->qualifying_scope = NULL_TREE;
7308 parser->object_scope = NULL_TREE;
7309 *idk = CP_ID_KIND_NONE;
7311 /* Enter the scope corresponding to the type of the object
7312 given by the POSTFIX_EXPRESSION. */
7313 if (!dependent_p)
7315 scope = TREE_TYPE (postfix_expression);
7316 /* According to the standard, no expression should ever have
7317 reference type. Unfortunately, we do not currently match
7318 the standard in this respect in that our internal representation
7319 of an expression may have reference type even when the standard
7320 says it does not. Therefore, we have to manually obtain the
7321 underlying type here. */
7322 scope = non_reference (scope);
7323 /* The type of the POSTFIX_EXPRESSION must be complete. */
7324 /* Unlike the object expression in other contexts, *this is not
7325 required to be of complete type for purposes of class member
7326 access (5.2.5) outside the member function body. */
7327 if (postfix_expression != current_class_ref
7328 && scope != error_mark_node
7329 && !(processing_template_decl
7330 && current_class_type
7331 && (same_type_ignoring_top_level_qualifiers_p
7332 (scope, current_class_type))))
7334 scope = complete_type (scope);
7335 if (!COMPLETE_TYPE_P (scope)
7336 /* Avoid clobbering e.g. OVERLOADs or DECLs. */
7337 && EXPR_P (postfix_expression))
7339 /* In a template, be permissive by treating an object expression
7340 of incomplete type as dependent (after a pedwarn). */
7341 diagnostic_t kind = (processing_template_decl
7342 ? DK_PEDWARN
7343 : DK_ERROR);
7344 cxx_incomplete_type_diagnostic
7345 (location_of (postfix_expression),
7346 postfix_expression, scope, kind);
7347 if (processing_template_decl)
7349 dependent_p = true;
7350 scope = TREE_TYPE (postfix_expression) = NULL_TREE;
7355 if (!dependent_p)
7357 /* Let the name lookup machinery know that we are processing a
7358 class member access expression. */
7359 parser->context->object_type = scope;
7360 /* If something went wrong, we want to be able to discern that case,
7361 as opposed to the case where there was no SCOPE due to the type
7362 of expression being dependent. */
7363 if (!scope)
7364 scope = error_mark_node;
7365 /* If the SCOPE was erroneous, make the various semantic analysis
7366 functions exit quickly -- and without issuing additional error
7367 messages. */
7368 if (scope == error_mark_node)
7369 postfix_expression = error_mark_node;
7373 if (dependent_p)
7374 /* Tell cp_parser_lookup_name that there was an object, even though it's
7375 type-dependent. */
7376 parser->context->object_type = unknown_type_node;
7378 /* Assume this expression is not a pseudo-destructor access. */
7379 pseudo_destructor_p = false;
7381 /* If the SCOPE is a scalar type, then, if this is a valid program,
7382 we must be looking at a pseudo-destructor-name. If POSTFIX_EXPRESSION
7383 is type dependent, it can be pseudo-destructor-name or something else.
7384 Try to parse it as pseudo-destructor-name first. */
7385 if ((scope && SCALAR_TYPE_P (scope)) || dependent_p)
7387 tree s;
7388 tree type;
7390 cp_parser_parse_tentatively (parser);
7391 /* Parse the pseudo-destructor-name. */
7392 s = NULL_TREE;
7393 cp_parser_pseudo_destructor_name (parser, postfix_expression,
7394 &s, &type);
7395 if (dependent_p
7396 && (cp_parser_error_occurred (parser)
7397 || !SCALAR_TYPE_P (type)))
7398 cp_parser_abort_tentative_parse (parser);
7399 else if (cp_parser_parse_definitely (parser))
7401 pseudo_destructor_p = true;
7402 postfix_expression
7403 = finish_pseudo_destructor_expr (postfix_expression,
7404 s, type, location);
7408 if (!pseudo_destructor_p)
7410 /* If the SCOPE is not a scalar type, we are looking at an
7411 ordinary class member access expression, rather than a
7412 pseudo-destructor-name. */
7413 bool template_p;
7414 cp_token *token = cp_lexer_peek_token (parser->lexer);
7415 /* Parse the id-expression. */
7416 name = (cp_parser_id_expression
7417 (parser,
7418 cp_parser_optional_template_keyword (parser),
7419 /*check_dependency_p=*/true,
7420 &template_p,
7421 /*declarator_p=*/false,
7422 /*optional_p=*/false));
7423 /* In general, build a SCOPE_REF if the member name is qualified.
7424 However, if the name was not dependent and has already been
7425 resolved; there is no need to build the SCOPE_REF. For example;
7427 struct X { void f(); };
7428 template <typename T> void f(T* t) { t->X::f(); }
7430 Even though "t" is dependent, "X::f" is not and has been resolved
7431 to a BASELINK; there is no need to include scope information. */
7433 /* But we do need to remember that there was an explicit scope for
7434 virtual function calls. */
7435 if (parser->scope)
7436 *idk = CP_ID_KIND_QUALIFIED;
7438 /* If the name is a template-id that names a type, we will get a
7439 TYPE_DECL here. That is invalid code. */
7440 if (TREE_CODE (name) == TYPE_DECL)
7442 error_at (token->location, "invalid use of %qD", name);
7443 postfix_expression = error_mark_node;
7445 else
7447 if (name != error_mark_node && !BASELINK_P (name) && parser->scope)
7449 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
7451 error_at (token->location, "%<%D::%D%> is not a class member",
7452 parser->scope, name);
7453 postfix_expression = error_mark_node;
7455 else
7456 name = build_qualified_name (/*type=*/NULL_TREE,
7457 parser->scope,
7458 name,
7459 template_p);
7460 parser->scope = NULL_TREE;
7461 parser->qualifying_scope = NULL_TREE;
7462 parser->object_scope = NULL_TREE;
7464 if (parser->scope && name && BASELINK_P (name))
7465 adjust_result_of_qualified_name_lookup
7466 (name, parser->scope, scope);
7467 postfix_expression
7468 = finish_class_member_access_expr (postfix_expression, name,
7469 template_p,
7470 tf_warning_or_error);
7471 /* Build a location e.g.:
7472 ptr->access_expr
7473 ~~~^~~~~~~~~~~~~
7474 where the caret is at the deref token, ranging from
7475 the start of postfix_expression to the end of the access expr. */
7476 location_t end_loc
7477 = get_finish (cp_lexer_previous_token (parser->lexer)->location);
7478 location_t combined_loc
7479 = make_location (input_location, start_loc, end_loc);
7480 protected_set_expr_location (postfix_expression, combined_loc);
7484 /* We no longer need to look up names in the scope of the object on
7485 the left-hand side of the `.' or `->' operator. */
7486 parser->context->object_type = NULL_TREE;
7488 /* Outside of offsetof, these operators may not appear in
7489 constant-expressions. */
7490 if (!for_offsetof
7491 && (cp_parser_non_integral_constant_expression
7492 (parser, token_type == CPP_DEREF ? NIC_ARROW : NIC_POINT)))
7493 postfix_expression = error_mark_node;
7495 return postfix_expression;
7498 /* Parse a parenthesized expression-list.
7500 expression-list:
7501 assignment-expression
7502 expression-list, assignment-expression
7504 attribute-list:
7505 expression-list
7506 identifier
7507 identifier, expression-list
7509 CAST_P is true if this expression is the target of a cast.
7511 ALLOW_EXPANSION_P is true if this expression allows expansion of an
7512 argument pack.
7514 Returns a vector of trees. Each element is a representation of an
7515 assignment-expression. NULL is returned if the ( and or ) are
7516 missing. An empty, but allocated, vector is returned on no
7517 expressions. The parentheses are eaten. IS_ATTRIBUTE_LIST is id_attr
7518 if we are parsing an attribute list for an attribute that wants a
7519 plain identifier argument, normal_attr for an attribute that wants
7520 an expression, or non_attr if we aren't parsing an attribute list. If
7521 NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P indicates whether or
7522 not all of the expressions in the list were constant.
7523 If CLOSE_PAREN_LOC is non-NULL, and no errors occur, then *CLOSE_PAREN_LOC
7524 will be written to with the location of the closing parenthesis. If
7525 an error occurs, it may or may not be written to. */
7527 static vec<tree, va_gc> *
7528 cp_parser_parenthesized_expression_list (cp_parser* parser,
7529 int is_attribute_list,
7530 bool cast_p,
7531 bool allow_expansion_p,
7532 bool *non_constant_p,
7533 location_t *close_paren_loc)
7535 vec<tree, va_gc> *expression_list;
7536 bool fold_expr_p = is_attribute_list != non_attr;
7537 tree identifier = NULL_TREE;
7538 bool saved_greater_than_is_operator_p;
7540 /* Assume all the expressions will be constant. */
7541 if (non_constant_p)
7542 *non_constant_p = false;
7544 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
7545 return NULL;
7547 expression_list = make_tree_vector ();
7549 /* Within a parenthesized expression, a `>' token is always
7550 the greater-than operator. */
7551 saved_greater_than_is_operator_p
7552 = parser->greater_than_is_operator_p;
7553 parser->greater_than_is_operator_p = true;
7555 /* Consume expressions until there are no more. */
7556 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
7557 while (true)
7559 tree expr;
7561 /* At the beginning of attribute lists, check to see if the
7562 next token is an identifier. */
7563 if (is_attribute_list == id_attr
7564 && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
7566 cp_token *token;
7568 /* Consume the identifier. */
7569 token = cp_lexer_consume_token (parser->lexer);
7570 /* Save the identifier. */
7571 identifier = token->u.value;
7573 else
7575 bool expr_non_constant_p;
7577 /* Parse the next assignment-expression. */
7578 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7580 /* A braced-init-list. */
7581 cp_lexer_set_source_position (parser->lexer);
7582 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
7583 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
7584 if (non_constant_p && expr_non_constant_p)
7585 *non_constant_p = true;
7587 else if (non_constant_p)
7589 expr = (cp_parser_constant_expression
7590 (parser, /*allow_non_constant_p=*/true,
7591 &expr_non_constant_p));
7592 if (expr_non_constant_p)
7593 *non_constant_p = true;
7595 else
7596 expr = cp_parser_assignment_expression (parser, /*pidk=*/NULL,
7597 cast_p);
7599 if (fold_expr_p)
7600 expr = instantiate_non_dependent_expr (expr);
7602 /* If we have an ellipsis, then this is an expression
7603 expansion. */
7604 if (allow_expansion_p
7605 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
7607 /* Consume the `...'. */
7608 cp_lexer_consume_token (parser->lexer);
7610 /* Build the argument pack. */
7611 expr = make_pack_expansion (expr);
7614 /* Add it to the list. We add error_mark_node
7615 expressions to the list, so that we can still tell if
7616 the correct form for a parenthesized expression-list
7617 is found. That gives better errors. */
7618 vec_safe_push (expression_list, expr);
7620 if (expr == error_mark_node)
7621 goto skip_comma;
7624 /* After the first item, attribute lists look the same as
7625 expression lists. */
7626 is_attribute_list = non_attr;
7628 get_comma:;
7629 /* If the next token isn't a `,', then we are done. */
7630 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
7631 break;
7633 /* Otherwise, consume the `,' and keep going. */
7634 cp_lexer_consume_token (parser->lexer);
7637 if (close_paren_loc)
7638 *close_paren_loc = cp_lexer_peek_token (parser->lexer)->location;
7640 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
7642 int ending;
7644 skip_comma:;
7645 /* We try and resync to an unnested comma, as that will give the
7646 user better diagnostics. */
7647 ending = cp_parser_skip_to_closing_parenthesis (parser,
7648 /*recovering=*/true,
7649 /*or_comma=*/true,
7650 /*consume_paren=*/true);
7651 if (ending < 0)
7652 goto get_comma;
7653 if (!ending)
7655 parser->greater_than_is_operator_p
7656 = saved_greater_than_is_operator_p;
7657 return NULL;
7661 parser->greater_than_is_operator_p
7662 = saved_greater_than_is_operator_p;
7664 if (identifier)
7665 vec_safe_insert (expression_list, 0, identifier);
7667 return expression_list;
7670 /* Parse a pseudo-destructor-name.
7672 pseudo-destructor-name:
7673 :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
7674 :: [opt] nested-name-specifier template template-id :: ~ type-name
7675 :: [opt] nested-name-specifier [opt] ~ type-name
7677 If either of the first two productions is used, sets *SCOPE to the
7678 TYPE specified before the final `::'. Otherwise, *SCOPE is set to
7679 NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name,
7680 or ERROR_MARK_NODE if the parse fails. */
7682 static void
7683 cp_parser_pseudo_destructor_name (cp_parser* parser,
7684 tree object,
7685 tree* scope,
7686 tree* type)
7688 bool nested_name_specifier_p;
7690 /* Handle ~auto. */
7691 if (cp_lexer_next_token_is (parser->lexer, CPP_COMPL)
7692 && cp_lexer_nth_token_is_keyword (parser->lexer, 2, RID_AUTO)
7693 && !type_dependent_expression_p (object))
7695 if (cxx_dialect < cxx14)
7696 pedwarn (input_location, 0,
7697 "%<~auto%> only available with "
7698 "-std=c++14 or -std=gnu++14");
7699 cp_lexer_consume_token (parser->lexer);
7700 cp_lexer_consume_token (parser->lexer);
7701 *scope = NULL_TREE;
7702 *type = TREE_TYPE (object);
7703 return;
7706 /* Assume that things will not work out. */
7707 *type = error_mark_node;
7709 /* Look for the optional `::' operator. */
7710 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
7711 /* Look for the optional nested-name-specifier. */
7712 nested_name_specifier_p
7713 = (cp_parser_nested_name_specifier_opt (parser,
7714 /*typename_keyword_p=*/false,
7715 /*check_dependency_p=*/true,
7716 /*type_p=*/false,
7717 /*is_declaration=*/false)
7718 != NULL_TREE);
7719 /* Now, if we saw a nested-name-specifier, we might be doing the
7720 second production. */
7721 if (nested_name_specifier_p
7722 && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
7724 /* Consume the `template' keyword. */
7725 cp_lexer_consume_token (parser->lexer);
7726 /* Parse the template-id. */
7727 cp_parser_template_id (parser,
7728 /*template_keyword_p=*/true,
7729 /*check_dependency_p=*/false,
7730 class_type,
7731 /*is_declaration=*/true);
7732 /* Look for the `::' token. */
7733 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
7735 /* If the next token is not a `~', then there might be some
7736 additional qualification. */
7737 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
7739 /* At this point, we're looking for "type-name :: ~". The type-name
7740 must not be a class-name, since this is a pseudo-destructor. So,
7741 it must be either an enum-name, or a typedef-name -- both of which
7742 are just identifiers. So, we peek ahead to check that the "::"
7743 and "~" tokens are present; if they are not, then we can avoid
7744 calling type_name. */
7745 if (cp_lexer_peek_token (parser->lexer)->type != CPP_NAME
7746 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE
7747 || cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_COMPL)
7749 cp_parser_error (parser, "non-scalar type");
7750 return;
7753 /* Look for the type-name. */
7754 *scope = TREE_TYPE (cp_parser_nonclass_name (parser));
7755 if (*scope == error_mark_node)
7756 return;
7758 /* Look for the `::' token. */
7759 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
7761 else
7762 *scope = NULL_TREE;
7764 /* Look for the `~'. */
7765 cp_parser_require (parser, CPP_COMPL, RT_COMPL);
7767 /* Once we see the ~, this has to be a pseudo-destructor. */
7768 if (!processing_template_decl && !cp_parser_error_occurred (parser))
7769 cp_parser_commit_to_topmost_tentative_parse (parser);
7771 /* Look for the type-name again. We are not responsible for
7772 checking that it matches the first type-name. */
7773 *type = TREE_TYPE (cp_parser_nonclass_name (parser));
7776 /* Parse a unary-expression.
7778 unary-expression:
7779 postfix-expression
7780 ++ cast-expression
7781 -- cast-expression
7782 unary-operator cast-expression
7783 sizeof unary-expression
7784 sizeof ( type-id )
7785 alignof ( type-id ) [C++0x]
7786 new-expression
7787 delete-expression
7789 GNU Extensions:
7791 unary-expression:
7792 __extension__ cast-expression
7793 __alignof__ unary-expression
7794 __alignof__ ( type-id )
7795 alignof unary-expression [C++0x]
7796 __real__ cast-expression
7797 __imag__ cast-expression
7798 && identifier
7799 sizeof ( type-id ) { initializer-list , [opt] }
7800 alignof ( type-id ) { initializer-list , [opt] } [C++0x]
7801 __alignof__ ( type-id ) { initializer-list , [opt] }
7803 ADDRESS_P is true iff the unary-expression is appearing as the
7804 operand of the `&' operator. CAST_P is true if this expression is
7805 the target of a cast.
7807 Returns a representation of the expression. */
7809 static cp_expr
7810 cp_parser_unary_expression (cp_parser *parser, cp_id_kind * pidk,
7811 bool address_p, bool cast_p, bool decltype_p)
7813 cp_token *token;
7814 enum tree_code unary_operator;
7816 /* Peek at the next token. */
7817 token = cp_lexer_peek_token (parser->lexer);
7818 /* Some keywords give away the kind of expression. */
7819 if (token->type == CPP_KEYWORD)
7821 enum rid keyword = token->keyword;
7823 switch (keyword)
7825 case RID_ALIGNOF:
7826 case RID_SIZEOF:
7828 tree operand, ret;
7829 enum tree_code op;
7830 location_t first_loc;
7832 op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
7833 /* Consume the token. */
7834 cp_lexer_consume_token (parser->lexer);
7835 first_loc = cp_lexer_peek_token (parser->lexer)->location;
7836 /* Parse the operand. */
7837 operand = cp_parser_sizeof_operand (parser, keyword);
7839 if (TYPE_P (operand))
7840 ret = cxx_sizeof_or_alignof_type (operand, op, true);
7841 else
7843 /* ISO C++ defines alignof only with types, not with
7844 expressions. So pedwarn if alignof is used with a non-
7845 type expression. However, __alignof__ is ok. */
7846 if (!strcmp (IDENTIFIER_POINTER (token->u.value), "alignof"))
7847 pedwarn (token->location, OPT_Wpedantic,
7848 "ISO C++ does not allow %<alignof%> "
7849 "with a non-type");
7851 ret = cxx_sizeof_or_alignof_expr (operand, op, true);
7853 /* For SIZEOF_EXPR, just issue diagnostics, but keep
7854 SIZEOF_EXPR with the original operand. */
7855 if (op == SIZEOF_EXPR && ret != error_mark_node)
7857 if (TREE_CODE (ret) != SIZEOF_EXPR || TYPE_P (operand))
7859 if (!processing_template_decl && TYPE_P (operand))
7861 ret = build_min (SIZEOF_EXPR, size_type_node,
7862 build1 (NOP_EXPR, operand,
7863 error_mark_node));
7864 SIZEOF_EXPR_TYPE_P (ret) = 1;
7866 else
7867 ret = build_min (SIZEOF_EXPR, size_type_node, operand);
7868 TREE_SIDE_EFFECTS (ret) = 0;
7869 TREE_READONLY (ret) = 1;
7871 SET_EXPR_LOCATION (ret, first_loc);
7873 return ret;
7876 case RID_NEW:
7877 return cp_parser_new_expression (parser);
7879 case RID_DELETE:
7880 return cp_parser_delete_expression (parser);
7882 case RID_EXTENSION:
7884 /* The saved value of the PEDANTIC flag. */
7885 int saved_pedantic;
7886 tree expr;
7888 /* Save away the PEDANTIC flag. */
7889 cp_parser_extension_opt (parser, &saved_pedantic);
7890 /* Parse the cast-expression. */
7891 expr = cp_parser_simple_cast_expression (parser);
7892 /* Restore the PEDANTIC flag. */
7893 pedantic = saved_pedantic;
7895 return expr;
7898 case RID_REALPART:
7899 case RID_IMAGPART:
7901 tree expression;
7903 /* Consume the `__real__' or `__imag__' token. */
7904 cp_lexer_consume_token (parser->lexer);
7905 /* Parse the cast-expression. */
7906 expression = cp_parser_simple_cast_expression (parser);
7907 /* Create the complete representation. */
7908 return build_x_unary_op (token->location,
7909 (keyword == RID_REALPART
7910 ? REALPART_EXPR : IMAGPART_EXPR),
7911 expression,
7912 tf_warning_or_error);
7914 break;
7916 case RID_TRANSACTION_ATOMIC:
7917 case RID_TRANSACTION_RELAXED:
7918 return cp_parser_transaction_expression (parser, keyword);
7920 case RID_NOEXCEPT:
7922 tree expr;
7923 const char *saved_message;
7924 bool saved_integral_constant_expression_p;
7925 bool saved_non_integral_constant_expression_p;
7926 bool saved_greater_than_is_operator_p;
7928 cp_lexer_consume_token (parser->lexer);
7929 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
7931 saved_message = parser->type_definition_forbidden_message;
7932 parser->type_definition_forbidden_message
7933 = G_("types may not be defined in %<noexcept%> expressions");
7935 saved_integral_constant_expression_p
7936 = parser->integral_constant_expression_p;
7937 saved_non_integral_constant_expression_p
7938 = parser->non_integral_constant_expression_p;
7939 parser->integral_constant_expression_p = false;
7941 saved_greater_than_is_operator_p
7942 = parser->greater_than_is_operator_p;
7943 parser->greater_than_is_operator_p = true;
7945 ++cp_unevaluated_operand;
7946 ++c_inhibit_evaluation_warnings;
7947 ++cp_noexcept_operand;
7948 expr = cp_parser_expression (parser);
7949 --cp_noexcept_operand;
7950 --c_inhibit_evaluation_warnings;
7951 --cp_unevaluated_operand;
7953 parser->greater_than_is_operator_p
7954 = saved_greater_than_is_operator_p;
7956 parser->integral_constant_expression_p
7957 = saved_integral_constant_expression_p;
7958 parser->non_integral_constant_expression_p
7959 = saved_non_integral_constant_expression_p;
7961 parser->type_definition_forbidden_message = saved_message;
7963 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
7964 return finish_noexcept_expr (expr, tf_warning_or_error);
7967 default:
7968 break;
7972 /* Look for the `:: new' and `:: delete', which also signal the
7973 beginning of a new-expression, or delete-expression,
7974 respectively. If the next token is `::', then it might be one of
7975 these. */
7976 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
7978 enum rid keyword;
7980 /* See if the token after the `::' is one of the keywords in
7981 which we're interested. */
7982 keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
7983 /* If it's `new', we have a new-expression. */
7984 if (keyword == RID_NEW)
7985 return cp_parser_new_expression (parser);
7986 /* Similarly, for `delete'. */
7987 else if (keyword == RID_DELETE)
7988 return cp_parser_delete_expression (parser);
7991 /* Look for a unary operator. */
7992 unary_operator = cp_parser_unary_operator (token);
7993 /* The `++' and `--' operators can be handled similarly, even though
7994 they are not technically unary-operators in the grammar. */
7995 if (unary_operator == ERROR_MARK)
7997 if (token->type == CPP_PLUS_PLUS)
7998 unary_operator = PREINCREMENT_EXPR;
7999 else if (token->type == CPP_MINUS_MINUS)
8000 unary_operator = PREDECREMENT_EXPR;
8001 /* Handle the GNU address-of-label extension. */
8002 else if (cp_parser_allow_gnu_extensions_p (parser)
8003 && token->type == CPP_AND_AND)
8005 tree identifier;
8006 tree expression;
8007 location_t start_loc = token->location;
8009 /* Consume the '&&' token. */
8010 cp_lexer_consume_token (parser->lexer);
8011 /* Look for the identifier. */
8012 location_t finish_loc
8013 = get_finish (cp_lexer_peek_token (parser->lexer)->location);
8014 identifier = cp_parser_identifier (parser);
8015 /* Construct a location of the form:
8016 &&label
8017 ^~~~~~~
8018 with caret==start at the "&&", finish at the end of the label. */
8019 location_t combined_loc
8020 = make_location (start_loc, start_loc, finish_loc);
8021 /* Create an expression representing the address. */
8022 expression = finish_label_address_expr (identifier, combined_loc);
8023 if (cp_parser_non_integral_constant_expression (parser,
8024 NIC_ADDR_LABEL))
8025 expression = error_mark_node;
8026 return expression;
8029 if (unary_operator != ERROR_MARK)
8031 cp_expr cast_expression;
8032 cp_expr expression = error_mark_node;
8033 non_integral_constant non_constant_p = NIC_NONE;
8034 location_t loc = token->location;
8035 tsubst_flags_t complain = complain_flags (decltype_p);
8037 /* Consume the operator token. */
8038 token = cp_lexer_consume_token (parser->lexer);
8039 enum cpp_ttype op_ttype = cp_lexer_peek_token (parser->lexer)->type;
8041 /* Parse the cast-expression. */
8042 cast_expression
8043 = cp_parser_cast_expression (parser,
8044 unary_operator == ADDR_EXPR,
8045 /*cast_p=*/false,
8046 /*decltype*/false,
8047 pidk);
8049 /* Make a location:
8050 OP_TOKEN CAST_EXPRESSION
8051 ^~~~~~~~~~~~~~~~~~~~~~~~~
8052 with start==caret at the operator token, and
8053 extending to the end of the cast_expression. */
8054 loc = make_location (loc, loc, cast_expression.get_finish ());
8056 /* Now, build an appropriate representation. */
8057 switch (unary_operator)
8059 case INDIRECT_REF:
8060 non_constant_p = NIC_STAR;
8061 expression = build_x_indirect_ref (loc, cast_expression,
8062 RO_UNARY_STAR,
8063 complain);
8064 /* TODO: build_x_indirect_ref does not always honor the
8065 location, so ensure it is set. */
8066 expression.set_location (loc);
8067 break;
8069 case ADDR_EXPR:
8070 non_constant_p = NIC_ADDR;
8071 /* Fall through. */
8072 case BIT_NOT_EXPR:
8073 expression = build_x_unary_op (loc, unary_operator,
8074 cast_expression,
8075 complain);
8076 /* TODO: build_x_unary_op does not always honor the location,
8077 so ensure it is set. */
8078 expression.set_location (loc);
8079 break;
8081 case PREINCREMENT_EXPR:
8082 case PREDECREMENT_EXPR:
8083 non_constant_p = unary_operator == PREINCREMENT_EXPR
8084 ? NIC_PREINCREMENT : NIC_PREDECREMENT;
8085 /* Fall through. */
8086 case NEGATE_EXPR:
8087 /* Immediately fold negation of a constant, unless the constant is 0
8088 (since -0 == 0) or it would overflow. */
8089 if (unary_operator == NEGATE_EXPR && op_ttype == CPP_NUMBER
8090 && CONSTANT_CLASS_P (cast_expression)
8091 && !integer_zerop (cast_expression)
8092 && !TREE_OVERFLOW (cast_expression))
8094 tree folded = fold_build1 (unary_operator,
8095 TREE_TYPE (cast_expression),
8096 cast_expression);
8097 if (CONSTANT_CLASS_P (folded) && !TREE_OVERFLOW (folded))
8099 expression = cp_expr (folded, loc);
8100 break;
8103 /* Fall through. */
8104 case UNARY_PLUS_EXPR:
8105 case TRUTH_NOT_EXPR:
8106 expression = finish_unary_op_expr (loc, unary_operator,
8107 cast_expression, complain);
8108 break;
8110 default:
8111 gcc_unreachable ();
8114 if (non_constant_p != NIC_NONE
8115 && cp_parser_non_integral_constant_expression (parser,
8116 non_constant_p))
8117 expression = error_mark_node;
8119 return expression;
8122 return cp_parser_postfix_expression (parser, address_p, cast_p,
8123 /*member_access_only_p=*/false,
8124 decltype_p,
8125 pidk);
8128 /* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a
8129 unary-operator, the corresponding tree code is returned. */
8131 static enum tree_code
8132 cp_parser_unary_operator (cp_token* token)
8134 switch (token->type)
8136 case CPP_MULT:
8137 return INDIRECT_REF;
8139 case CPP_AND:
8140 return ADDR_EXPR;
8142 case CPP_PLUS:
8143 return UNARY_PLUS_EXPR;
8145 case CPP_MINUS:
8146 return NEGATE_EXPR;
8148 case CPP_NOT:
8149 return TRUTH_NOT_EXPR;
8151 case CPP_COMPL:
8152 return BIT_NOT_EXPR;
8154 default:
8155 return ERROR_MARK;
8159 /* Parse a new-expression.
8161 new-expression:
8162 :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
8163 :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
8165 Returns a representation of the expression. */
8167 static tree
8168 cp_parser_new_expression (cp_parser* parser)
8170 bool global_scope_p;
8171 vec<tree, va_gc> *placement;
8172 tree type;
8173 vec<tree, va_gc> *initializer;
8174 tree nelts = NULL_TREE;
8175 tree ret;
8177 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
8179 /* Look for the optional `::' operator. */
8180 global_scope_p
8181 = (cp_parser_global_scope_opt (parser,
8182 /*current_scope_valid_p=*/false)
8183 != NULL_TREE);
8184 /* Look for the `new' operator. */
8185 cp_parser_require_keyword (parser, RID_NEW, RT_NEW);
8186 /* There's no easy way to tell a new-placement from the
8187 `( type-id )' construct. */
8188 cp_parser_parse_tentatively (parser);
8189 /* Look for a new-placement. */
8190 placement = cp_parser_new_placement (parser);
8191 /* If that didn't work out, there's no new-placement. */
8192 if (!cp_parser_parse_definitely (parser))
8194 if (placement != NULL)
8195 release_tree_vector (placement);
8196 placement = NULL;
8199 /* If the next token is a `(', then we have a parenthesized
8200 type-id. */
8201 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
8203 cp_token *token;
8204 const char *saved_message = parser->type_definition_forbidden_message;
8206 /* Consume the `('. */
8207 cp_lexer_consume_token (parser->lexer);
8209 /* Parse the type-id. */
8210 parser->type_definition_forbidden_message
8211 = G_("types may not be defined in a new-expression");
8213 type_id_in_expr_sentinel s (parser);
8214 type = cp_parser_type_id (parser);
8216 parser->type_definition_forbidden_message = saved_message;
8218 /* Look for the closing `)'. */
8219 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
8220 token = cp_lexer_peek_token (parser->lexer);
8221 /* There should not be a direct-new-declarator in this production,
8222 but GCC used to allowed this, so we check and emit a sensible error
8223 message for this case. */
8224 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
8226 error_at (token->location,
8227 "array bound forbidden after parenthesized type-id");
8228 inform (token->location,
8229 "try removing the parentheses around the type-id");
8230 cp_parser_direct_new_declarator (parser);
8233 /* Otherwise, there must be a new-type-id. */
8234 else
8235 type = cp_parser_new_type_id (parser, &nelts);
8237 /* If the next token is a `(' or '{', then we have a new-initializer. */
8238 cp_token *token = cp_lexer_peek_token (parser->lexer);
8239 if (token->type == CPP_OPEN_PAREN
8240 || token->type == CPP_OPEN_BRACE)
8241 initializer = cp_parser_new_initializer (parser);
8242 else
8243 initializer = NULL;
8245 /* A new-expression may not appear in an integral constant
8246 expression. */
8247 if (cp_parser_non_integral_constant_expression (parser, NIC_NEW))
8248 ret = error_mark_node;
8249 /* 5.3.4/2: "If the auto type-specifier appears in the type-specifier-seq
8250 of a new-type-id or type-id of a new-expression, the new-expression shall
8251 contain a new-initializer of the form ( assignment-expression )".
8252 Additionally, consistently with the spirit of DR 1467, we want to accept
8253 'new auto { 2 }' too. */
8254 else if (type_uses_auto (type)
8255 && (vec_safe_length (initializer) != 1
8256 || (BRACE_ENCLOSED_INITIALIZER_P ((*initializer)[0])
8257 && CONSTRUCTOR_NELTS ((*initializer)[0]) != 1)))
8259 error_at (token->location,
8260 "initialization of new-expression for type %<auto%> "
8261 "requires exactly one element");
8262 ret = error_mark_node;
8264 else
8266 /* Construct a location e.g.:
8267 ptr = new int[100]
8268 ^~~~~~~~~~~~
8269 with caret == start at the start of the "new" token, and the end
8270 at the end of the final token we consumed. */
8271 cp_token *end_tok = cp_lexer_previous_token (parser->lexer);
8272 location_t end_loc = get_finish (end_tok->location);
8273 location_t combined_loc = make_location (start_loc, start_loc, end_loc);
8275 /* Create a representation of the new-expression. */
8276 ret = build_new (&placement, type, nelts, &initializer, global_scope_p,
8277 tf_warning_or_error);
8278 protected_set_expr_location (ret, combined_loc);
8281 if (placement != NULL)
8282 release_tree_vector (placement);
8283 if (initializer != NULL)
8284 release_tree_vector (initializer);
8286 return ret;
8289 /* Parse a new-placement.
8291 new-placement:
8292 ( expression-list )
8294 Returns the same representation as for an expression-list. */
8296 static vec<tree, va_gc> *
8297 cp_parser_new_placement (cp_parser* parser)
8299 vec<tree, va_gc> *expression_list;
8301 /* Parse the expression-list. */
8302 expression_list = (cp_parser_parenthesized_expression_list
8303 (parser, non_attr, /*cast_p=*/false,
8304 /*allow_expansion_p=*/true,
8305 /*non_constant_p=*/NULL));
8307 if (expression_list && expression_list->is_empty ())
8308 error ("expected expression-list or type-id");
8310 return expression_list;
8313 /* Parse a new-type-id.
8315 new-type-id:
8316 type-specifier-seq new-declarator [opt]
8318 Returns the TYPE allocated. If the new-type-id indicates an array
8319 type, *NELTS is set to the number of elements in the last array
8320 bound; the TYPE will not include the last array bound. */
8322 static tree
8323 cp_parser_new_type_id (cp_parser* parser, tree *nelts)
8325 cp_decl_specifier_seq type_specifier_seq;
8326 cp_declarator *new_declarator;
8327 cp_declarator *declarator;
8328 cp_declarator *outer_declarator;
8329 const char *saved_message;
8331 /* The type-specifier sequence must not contain type definitions.
8332 (It cannot contain declarations of new types either, but if they
8333 are not definitions we will catch that because they are not
8334 complete.) */
8335 saved_message = parser->type_definition_forbidden_message;
8336 parser->type_definition_forbidden_message
8337 = G_("types may not be defined in a new-type-id");
8338 /* Parse the type-specifier-seq. */
8339 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
8340 /*is_trailing_return=*/false,
8341 &type_specifier_seq);
8342 /* Restore the old message. */
8343 parser->type_definition_forbidden_message = saved_message;
8345 if (type_specifier_seq.type == error_mark_node)
8346 return error_mark_node;
8348 /* Parse the new-declarator. */
8349 new_declarator = cp_parser_new_declarator_opt (parser);
8351 /* Determine the number of elements in the last array dimension, if
8352 any. */
8353 *nelts = NULL_TREE;
8354 /* Skip down to the last array dimension. */
8355 declarator = new_declarator;
8356 outer_declarator = NULL;
8357 while (declarator && (declarator->kind == cdk_pointer
8358 || declarator->kind == cdk_ptrmem))
8360 outer_declarator = declarator;
8361 declarator = declarator->declarator;
8363 while (declarator
8364 && declarator->kind == cdk_array
8365 && declarator->declarator
8366 && declarator->declarator->kind == cdk_array)
8368 outer_declarator = declarator;
8369 declarator = declarator->declarator;
8372 if (declarator && declarator->kind == cdk_array)
8374 *nelts = declarator->u.array.bounds;
8375 if (*nelts == error_mark_node)
8376 *nelts = integer_one_node;
8378 if (outer_declarator)
8379 outer_declarator->declarator = declarator->declarator;
8380 else
8381 new_declarator = NULL;
8384 return groktypename (&type_specifier_seq, new_declarator, false);
8387 /* Parse an (optional) new-declarator.
8389 new-declarator:
8390 ptr-operator new-declarator [opt]
8391 direct-new-declarator
8393 Returns the declarator. */
8395 static cp_declarator *
8396 cp_parser_new_declarator_opt (cp_parser* parser)
8398 enum tree_code code;
8399 tree type, std_attributes = NULL_TREE;
8400 cp_cv_quals cv_quals;
8402 /* We don't know if there's a ptr-operator next, or not. */
8403 cp_parser_parse_tentatively (parser);
8404 /* Look for a ptr-operator. */
8405 code = cp_parser_ptr_operator (parser, &type, &cv_quals, &std_attributes);
8406 /* If that worked, look for more new-declarators. */
8407 if (cp_parser_parse_definitely (parser))
8409 cp_declarator *declarator;
8411 /* Parse another optional declarator. */
8412 declarator = cp_parser_new_declarator_opt (parser);
8414 declarator = cp_parser_make_indirect_declarator
8415 (code, type, cv_quals, declarator, std_attributes);
8417 return declarator;
8420 /* If the next token is a `[', there is a direct-new-declarator. */
8421 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
8422 return cp_parser_direct_new_declarator (parser);
8424 return NULL;
8427 /* Parse a direct-new-declarator.
8429 direct-new-declarator:
8430 [ expression ]
8431 direct-new-declarator [constant-expression]
8435 static cp_declarator *
8436 cp_parser_direct_new_declarator (cp_parser* parser)
8438 cp_declarator *declarator = NULL;
8440 while (true)
8442 tree expression;
8443 cp_token *token;
8445 /* Look for the opening `['. */
8446 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
8448 token = cp_lexer_peek_token (parser->lexer);
8449 expression = cp_parser_expression (parser);
8450 /* The standard requires that the expression have integral
8451 type. DR 74 adds enumeration types. We believe that the
8452 real intent is that these expressions be handled like the
8453 expression in a `switch' condition, which also allows
8454 classes with a single conversion to integral or
8455 enumeration type. */
8456 if (!processing_template_decl)
8458 expression
8459 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
8460 expression,
8461 /*complain=*/true);
8462 if (!expression)
8464 error_at (token->location,
8465 "expression in new-declarator must have integral "
8466 "or enumeration type");
8467 expression = error_mark_node;
8471 /* Look for the closing `]'. */
8472 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
8474 /* Add this bound to the declarator. */
8475 declarator = make_array_declarator (declarator, expression);
8477 /* If the next token is not a `[', then there are no more
8478 bounds. */
8479 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
8480 break;
8483 return declarator;
8486 /* Parse a new-initializer.
8488 new-initializer:
8489 ( expression-list [opt] )
8490 braced-init-list
8492 Returns a representation of the expression-list. */
8494 static vec<tree, va_gc> *
8495 cp_parser_new_initializer (cp_parser* parser)
8497 vec<tree, va_gc> *expression_list;
8499 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
8501 tree t;
8502 bool expr_non_constant_p;
8503 cp_lexer_set_source_position (parser->lexer);
8504 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
8505 t = cp_parser_braced_list (parser, &expr_non_constant_p);
8506 CONSTRUCTOR_IS_DIRECT_INIT (t) = 1;
8507 expression_list = make_tree_vector_single (t);
8509 else
8510 expression_list = (cp_parser_parenthesized_expression_list
8511 (parser, non_attr, /*cast_p=*/false,
8512 /*allow_expansion_p=*/true,
8513 /*non_constant_p=*/NULL));
8515 return expression_list;
8518 /* Parse a delete-expression.
8520 delete-expression:
8521 :: [opt] delete cast-expression
8522 :: [opt] delete [ ] cast-expression
8524 Returns a representation of the expression. */
8526 static tree
8527 cp_parser_delete_expression (cp_parser* parser)
8529 bool global_scope_p;
8530 bool array_p;
8531 tree expression;
8533 /* Look for the optional `::' operator. */
8534 global_scope_p
8535 = (cp_parser_global_scope_opt (parser,
8536 /*current_scope_valid_p=*/false)
8537 != NULL_TREE);
8538 /* Look for the `delete' keyword. */
8539 cp_parser_require_keyword (parser, RID_DELETE, RT_DELETE);
8540 /* See if the array syntax is in use. */
8541 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
8543 /* Consume the `[' token. */
8544 cp_lexer_consume_token (parser->lexer);
8545 /* Look for the `]' token. */
8546 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
8547 /* Remember that this is the `[]' construct. */
8548 array_p = true;
8550 else
8551 array_p = false;
8553 /* Parse the cast-expression. */
8554 expression = cp_parser_simple_cast_expression (parser);
8556 /* A delete-expression may not appear in an integral constant
8557 expression. */
8558 if (cp_parser_non_integral_constant_expression (parser, NIC_DEL))
8559 return error_mark_node;
8561 return delete_sanity (expression, NULL_TREE, array_p, global_scope_p,
8562 tf_warning_or_error);
8565 /* Returns 1 if TOKEN may start a cast-expression and isn't '++', '--',
8566 neither '[' in C++11; -1 if TOKEN is '++', '--', or '[' in C++11;
8567 0 otherwise. */
8569 static int
8570 cp_parser_tokens_start_cast_expression (cp_parser *parser)
8572 cp_token *token = cp_lexer_peek_token (parser->lexer);
8573 switch (token->type)
8575 case CPP_COMMA:
8576 case CPP_SEMICOLON:
8577 case CPP_QUERY:
8578 case CPP_COLON:
8579 case CPP_CLOSE_SQUARE:
8580 case CPP_CLOSE_PAREN:
8581 case CPP_CLOSE_BRACE:
8582 case CPP_OPEN_BRACE:
8583 case CPP_DOT:
8584 case CPP_DOT_STAR:
8585 case CPP_DEREF:
8586 case CPP_DEREF_STAR:
8587 case CPP_DIV:
8588 case CPP_MOD:
8589 case CPP_LSHIFT:
8590 case CPP_RSHIFT:
8591 case CPP_LESS:
8592 case CPP_GREATER:
8593 case CPP_LESS_EQ:
8594 case CPP_GREATER_EQ:
8595 case CPP_EQ_EQ:
8596 case CPP_NOT_EQ:
8597 case CPP_EQ:
8598 case CPP_MULT_EQ:
8599 case CPP_DIV_EQ:
8600 case CPP_MOD_EQ:
8601 case CPP_PLUS_EQ:
8602 case CPP_MINUS_EQ:
8603 case CPP_RSHIFT_EQ:
8604 case CPP_LSHIFT_EQ:
8605 case CPP_AND_EQ:
8606 case CPP_XOR_EQ:
8607 case CPP_OR_EQ:
8608 case CPP_XOR:
8609 case CPP_OR:
8610 case CPP_OR_OR:
8611 case CPP_EOF:
8612 case CPP_ELLIPSIS:
8613 return 0;
8615 case CPP_OPEN_PAREN:
8616 /* In ((type ()) () the last () isn't a valid cast-expression,
8617 so the whole must be parsed as postfix-expression. */
8618 return cp_lexer_peek_nth_token (parser->lexer, 2)->type
8619 != CPP_CLOSE_PAREN;
8621 case CPP_OPEN_SQUARE:
8622 /* '[' may start a primary-expression in obj-c++ and in C++11,
8623 as a lambda-expression, eg, '(void)[]{}'. */
8624 if (cxx_dialect >= cxx11)
8625 return -1;
8626 return c_dialect_objc ();
8628 case CPP_PLUS_PLUS:
8629 case CPP_MINUS_MINUS:
8630 /* '++' and '--' may or may not start a cast-expression:
8632 struct T { void operator++(int); };
8633 void f() { (T())++; }
8637 int a;
8638 (int)++a; */
8639 return -1;
8641 default:
8642 return 1;
8646 /* Parse a cast-expression.
8648 cast-expression:
8649 unary-expression
8650 ( type-id ) cast-expression
8652 ADDRESS_P is true iff the unary-expression is appearing as the
8653 operand of the `&' operator. CAST_P is true if this expression is
8654 the target of a cast.
8656 Returns a representation of the expression. */
8658 static cp_expr
8659 cp_parser_cast_expression (cp_parser *parser, bool address_p, bool cast_p,
8660 bool decltype_p, cp_id_kind * pidk)
8662 /* If it's a `(', then we might be looking at a cast. */
8663 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
8665 tree type = NULL_TREE;
8666 cp_expr expr (NULL_TREE);
8667 int cast_expression = 0;
8668 const char *saved_message;
8670 /* There's no way to know yet whether or not this is a cast.
8671 For example, `(int (3))' is a unary-expression, while `(int)
8672 3' is a cast. So, we resort to parsing tentatively. */
8673 cp_parser_parse_tentatively (parser);
8674 /* Types may not be defined in a cast. */
8675 saved_message = parser->type_definition_forbidden_message;
8676 parser->type_definition_forbidden_message
8677 = G_("types may not be defined in casts");
8678 /* Consume the `('. */
8679 cp_token *open_paren = cp_lexer_consume_token (parser->lexer);
8680 location_t open_paren_loc = open_paren->location;
8682 /* A very tricky bit is that `(struct S) { 3 }' is a
8683 compound-literal (which we permit in C++ as an extension).
8684 But, that construct is not a cast-expression -- it is a
8685 postfix-expression. (The reason is that `(struct S) { 3 }.i'
8686 is legal; if the compound-literal were a cast-expression,
8687 you'd need an extra set of parentheses.) But, if we parse
8688 the type-id, and it happens to be a class-specifier, then we
8689 will commit to the parse at that point, because we cannot
8690 undo the action that is done when creating a new class. So,
8691 then we cannot back up and do a postfix-expression.
8693 Another tricky case is the following (c++/29234):
8695 struct S { void operator () (); };
8697 void foo ()
8699 ( S()() );
8702 As a type-id we parse the parenthesized S()() as a function
8703 returning a function, groktypename complains and we cannot
8704 back up in this case either.
8706 Therefore, we scan ahead to the closing `)', and check to see
8707 if the tokens after the `)' can start a cast-expression. Otherwise
8708 we are dealing with an unary-expression, a postfix-expression
8709 or something else.
8711 Yet another tricky case, in C++11, is the following (c++/54891):
8713 (void)[]{};
8715 The issue is that usually, besides the case of lambda-expressions,
8716 the parenthesized type-id cannot be followed by '[', and, eg, we
8717 want to parse '(C ())[2];' in parse/pr26997.C as unary-expression.
8718 Thus, if cp_parser_tokens_start_cast_expression returns -1, below
8719 we don't commit, we try a cast-expression, then an unary-expression.
8721 Save tokens so that we can put them back. */
8722 cp_lexer_save_tokens (parser->lexer);
8724 /* We may be looking at a cast-expression. */
8725 if (cp_parser_skip_to_closing_parenthesis (parser, false, false,
8726 /*consume_paren=*/true))
8727 cast_expression
8728 = cp_parser_tokens_start_cast_expression (parser);
8730 /* Roll back the tokens we skipped. */
8731 cp_lexer_rollback_tokens (parser->lexer);
8732 /* If we aren't looking at a cast-expression, simulate an error so
8733 that the call to cp_parser_error_occurred below returns true. */
8734 if (!cast_expression)
8735 cp_parser_simulate_error (parser);
8736 else
8738 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
8739 parser->in_type_id_in_expr_p = true;
8740 /* Look for the type-id. */
8741 type = cp_parser_type_id (parser);
8742 /* Look for the closing `)'. */
8743 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
8744 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
8747 /* Restore the saved message. */
8748 parser->type_definition_forbidden_message = saved_message;
8750 /* At this point this can only be either a cast or a
8751 parenthesized ctor such as `(T ())' that looks like a cast to
8752 function returning T. */
8753 if (!cp_parser_error_occurred (parser))
8755 /* Only commit if the cast-expression doesn't start with
8756 '++', '--', or '[' in C++11. */
8757 if (cast_expression > 0)
8758 cp_parser_commit_to_topmost_tentative_parse (parser);
8760 expr = cp_parser_cast_expression (parser,
8761 /*address_p=*/false,
8762 /*cast_p=*/true,
8763 /*decltype_p=*/false,
8764 pidk);
8766 if (cp_parser_parse_definitely (parser))
8768 /* Warn about old-style casts, if so requested. */
8769 if (warn_old_style_cast
8770 && !in_system_header_at (input_location)
8771 && !VOID_TYPE_P (type)
8772 && current_lang_name != lang_name_c)
8773 warning (OPT_Wold_style_cast, "use of old-style cast");
8775 /* Only type conversions to integral or enumeration types
8776 can be used in constant-expressions. */
8777 if (!cast_valid_in_integral_constant_expression_p (type)
8778 && cp_parser_non_integral_constant_expression (parser,
8779 NIC_CAST))
8780 return error_mark_node;
8782 /* Perform the cast. */
8783 /* Make a location:
8784 (TYPE) EXPR
8785 ^~~~~~~~~~~
8786 with start==caret at the open paren, extending to the
8787 end of "expr". */
8788 location_t cast_loc = make_location (open_paren_loc,
8789 open_paren_loc,
8790 expr.get_finish ());
8791 expr = build_c_cast (cast_loc, type, expr);
8792 return expr;
8795 else
8796 cp_parser_abort_tentative_parse (parser);
8799 /* If we get here, then it's not a cast, so it must be a
8800 unary-expression. */
8801 return cp_parser_unary_expression (parser, pidk, address_p,
8802 cast_p, decltype_p);
8805 /* Parse a binary expression of the general form:
8807 pm-expression:
8808 cast-expression
8809 pm-expression .* cast-expression
8810 pm-expression ->* cast-expression
8812 multiplicative-expression:
8813 pm-expression
8814 multiplicative-expression * pm-expression
8815 multiplicative-expression / pm-expression
8816 multiplicative-expression % pm-expression
8818 additive-expression:
8819 multiplicative-expression
8820 additive-expression + multiplicative-expression
8821 additive-expression - multiplicative-expression
8823 shift-expression:
8824 additive-expression
8825 shift-expression << additive-expression
8826 shift-expression >> additive-expression
8828 relational-expression:
8829 shift-expression
8830 relational-expression < shift-expression
8831 relational-expression > shift-expression
8832 relational-expression <= shift-expression
8833 relational-expression >= shift-expression
8835 GNU Extension:
8837 relational-expression:
8838 relational-expression <? shift-expression
8839 relational-expression >? shift-expression
8841 equality-expression:
8842 relational-expression
8843 equality-expression == relational-expression
8844 equality-expression != relational-expression
8846 and-expression:
8847 equality-expression
8848 and-expression & equality-expression
8850 exclusive-or-expression:
8851 and-expression
8852 exclusive-or-expression ^ and-expression
8854 inclusive-or-expression:
8855 exclusive-or-expression
8856 inclusive-or-expression | exclusive-or-expression
8858 logical-and-expression:
8859 inclusive-or-expression
8860 logical-and-expression && inclusive-or-expression
8862 logical-or-expression:
8863 logical-and-expression
8864 logical-or-expression || logical-and-expression
8866 All these are implemented with a single function like:
8868 binary-expression:
8869 simple-cast-expression
8870 binary-expression <token> binary-expression
8872 CAST_P is true if this expression is the target of a cast.
8874 The binops_by_token map is used to get the tree codes for each <token> type.
8875 binary-expressions are associated according to a precedence table. */
8877 #define TOKEN_PRECEDENCE(token) \
8878 (((token->type == CPP_GREATER \
8879 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT)) \
8880 && !parser->greater_than_is_operator_p) \
8881 ? PREC_NOT_OPERATOR \
8882 : binops_by_token[token->type].prec)
8884 static cp_expr
8885 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
8886 bool no_toplevel_fold_p,
8887 bool decltype_p,
8888 enum cp_parser_prec prec,
8889 cp_id_kind * pidk)
8891 cp_parser_expression_stack stack;
8892 cp_parser_expression_stack_entry *sp = &stack[0];
8893 cp_parser_expression_stack_entry current;
8894 cp_expr rhs;
8895 cp_token *token;
8896 enum tree_code rhs_type;
8897 enum cp_parser_prec new_prec, lookahead_prec;
8898 tree overload;
8900 /* Parse the first expression. */
8901 current.lhs_type = (cp_lexer_next_token_is (parser->lexer, CPP_NOT)
8902 ? TRUTH_NOT_EXPR : ERROR_MARK);
8903 current.lhs = cp_parser_cast_expression (parser, /*address_p=*/false,
8904 cast_p, decltype_p, pidk);
8905 current.prec = prec;
8907 if (cp_parser_error_occurred (parser))
8908 return error_mark_node;
8910 for (;;)
8912 /* Get an operator token. */
8913 token = cp_lexer_peek_token (parser->lexer);
8915 if (warn_cxx11_compat
8916 && token->type == CPP_RSHIFT
8917 && !parser->greater_than_is_operator_p)
8919 if (warning_at (token->location, OPT_Wc__11_compat,
8920 "%<>>%> operator is treated"
8921 " as two right angle brackets in C++11"))
8922 inform (token->location,
8923 "suggest parentheses around %<>>%> expression");
8926 new_prec = TOKEN_PRECEDENCE (token);
8927 if (new_prec != PREC_NOT_OPERATOR
8928 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS))
8929 /* This is a fold-expression; handle it later. */
8930 new_prec = PREC_NOT_OPERATOR;
8932 /* Popping an entry off the stack means we completed a subexpression:
8933 - either we found a token which is not an operator (`>' where it is not
8934 an operator, or prec == PREC_NOT_OPERATOR), in which case popping
8935 will happen repeatedly;
8936 - or, we found an operator which has lower priority. This is the case
8937 where the recursive descent *ascends*, as in `3 * 4 + 5' after
8938 parsing `3 * 4'. */
8939 if (new_prec <= current.prec)
8941 if (sp == stack)
8942 break;
8943 else
8944 goto pop;
8947 get_rhs:
8948 current.tree_type = binops_by_token[token->type].tree_type;
8949 current.loc = token->location;
8951 /* We used the operator token. */
8952 cp_lexer_consume_token (parser->lexer);
8954 /* For "false && x" or "true || x", x will never be executed;
8955 disable warnings while evaluating it. */
8956 if (current.tree_type == TRUTH_ANDIF_EXPR)
8957 c_inhibit_evaluation_warnings +=
8958 cp_fully_fold (current.lhs) == truthvalue_false_node;
8959 else if (current.tree_type == TRUTH_ORIF_EXPR)
8960 c_inhibit_evaluation_warnings +=
8961 cp_fully_fold (current.lhs) == truthvalue_true_node;
8963 /* Extract another operand. It may be the RHS of this expression
8964 or the LHS of a new, higher priority expression. */
8965 rhs_type = (cp_lexer_next_token_is (parser->lexer, CPP_NOT)
8966 ? TRUTH_NOT_EXPR : ERROR_MARK);
8967 rhs = cp_parser_simple_cast_expression (parser);
8969 /* Get another operator token. Look up its precedence to avoid
8970 building a useless (immediately popped) stack entry for common
8971 cases such as 3 + 4 + 5 or 3 * 4 + 5. */
8972 token = cp_lexer_peek_token (parser->lexer);
8973 lookahead_prec = TOKEN_PRECEDENCE (token);
8974 if (lookahead_prec != PREC_NOT_OPERATOR
8975 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS))
8976 lookahead_prec = PREC_NOT_OPERATOR;
8977 if (lookahead_prec > new_prec)
8979 /* ... and prepare to parse the RHS of the new, higher priority
8980 expression. Since precedence levels on the stack are
8981 monotonically increasing, we do not have to care about
8982 stack overflows. */
8983 *sp = current;
8984 ++sp;
8985 current.lhs = rhs;
8986 current.lhs_type = rhs_type;
8987 current.prec = new_prec;
8988 new_prec = lookahead_prec;
8989 goto get_rhs;
8991 pop:
8992 lookahead_prec = new_prec;
8993 /* If the stack is not empty, we have parsed into LHS the right side
8994 (`4' in the example above) of an expression we had suspended.
8995 We can use the information on the stack to recover the LHS (`3')
8996 from the stack together with the tree code (`MULT_EXPR'), and
8997 the precedence of the higher level subexpression
8998 (`PREC_ADDITIVE_EXPRESSION'). TOKEN is the CPP_PLUS token,
8999 which will be used to actually build the additive expression. */
9000 rhs = current.lhs;
9001 rhs_type = current.lhs_type;
9002 --sp;
9003 current = *sp;
9006 /* Undo the disabling of warnings done above. */
9007 if (current.tree_type == TRUTH_ANDIF_EXPR)
9008 c_inhibit_evaluation_warnings -=
9009 cp_fully_fold (current.lhs) == truthvalue_false_node;
9010 else if (current.tree_type == TRUTH_ORIF_EXPR)
9011 c_inhibit_evaluation_warnings -=
9012 cp_fully_fold (current.lhs) == truthvalue_true_node;
9014 if (warn_logical_not_paren
9015 && TREE_CODE_CLASS (current.tree_type) == tcc_comparison
9016 && current.lhs_type == TRUTH_NOT_EXPR
9017 /* Avoid warning for !!x == y. */
9018 && (TREE_CODE (current.lhs) != NE_EXPR
9019 || !integer_zerop (TREE_OPERAND (current.lhs, 1)))
9020 && (TREE_CODE (current.lhs) != TRUTH_NOT_EXPR
9021 || (TREE_CODE (TREE_OPERAND (current.lhs, 0)) != TRUTH_NOT_EXPR
9022 /* Avoid warning for !b == y where b is boolean. */
9023 && (TREE_TYPE (TREE_OPERAND (current.lhs, 0)) == NULL_TREE
9024 || (TREE_CODE (TREE_TYPE (TREE_OPERAND (current.lhs, 0)))
9025 != BOOLEAN_TYPE))))
9026 /* Avoid warning for !!b == y where b is boolean. */
9027 && (!DECL_P (current.lhs)
9028 || TREE_TYPE (current.lhs) == NULL_TREE
9029 || TREE_CODE (TREE_TYPE (current.lhs)) != BOOLEAN_TYPE))
9030 warn_logical_not_parentheses (current.loc, current.tree_type,
9031 current.lhs, maybe_constant_value (rhs));
9033 overload = NULL;
9035 location_t combined_loc = make_location (current.loc,
9036 current.lhs.get_start (),
9037 rhs.get_finish ());
9039 /* ??? Currently we pass lhs_type == ERROR_MARK and rhs_type ==
9040 ERROR_MARK for everything that is not a binary expression.
9041 This makes warn_about_parentheses miss some warnings that
9042 involve unary operators. For unary expressions we should
9043 pass the correct tree_code unless the unary expression was
9044 surrounded by parentheses.
9046 if (no_toplevel_fold_p
9047 && lookahead_prec <= current.prec
9048 && sp == stack)
9049 current.lhs = build2_loc (combined_loc,
9050 current.tree_type,
9051 TREE_CODE_CLASS (current.tree_type)
9052 == tcc_comparison
9053 ? boolean_type_node : TREE_TYPE (current.lhs),
9054 current.lhs, rhs);
9055 else
9057 current.lhs = build_x_binary_op (combined_loc, current.tree_type,
9058 current.lhs, current.lhs_type,
9059 rhs, rhs_type, &overload,
9060 complain_flags (decltype_p));
9061 /* TODO: build_x_binary_op doesn't always honor the location. */
9062 current.lhs.set_location (combined_loc);
9064 current.lhs_type = current.tree_type;
9066 /* If the binary operator required the use of an overloaded operator,
9067 then this expression cannot be an integral constant-expression.
9068 An overloaded operator can be used even if both operands are
9069 otherwise permissible in an integral constant-expression if at
9070 least one of the operands is of enumeration type. */
9072 if (overload
9073 && cp_parser_non_integral_constant_expression (parser,
9074 NIC_OVERLOADED))
9075 return error_mark_node;
9078 return current.lhs;
9081 static cp_expr
9082 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
9083 bool no_toplevel_fold_p,
9084 enum cp_parser_prec prec,
9085 cp_id_kind * pidk)
9087 return cp_parser_binary_expression (parser, cast_p, no_toplevel_fold_p,
9088 /*decltype*/false, prec, pidk);
9091 /* Parse the `? expression : assignment-expression' part of a
9092 conditional-expression. The LOGICAL_OR_EXPR is the
9093 logical-or-expression that started the conditional-expression.
9094 Returns a representation of the entire conditional-expression.
9096 This routine is used by cp_parser_assignment_expression.
9098 ? expression : assignment-expression
9100 GNU Extensions:
9102 ? : assignment-expression */
9104 static tree
9105 cp_parser_question_colon_clause (cp_parser* parser, cp_expr logical_or_expr)
9107 tree expr, folded_logical_or_expr = cp_fully_fold (logical_or_expr);
9108 cp_expr assignment_expr;
9109 struct cp_token *token;
9110 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
9112 /* Consume the `?' token. */
9113 cp_lexer_consume_token (parser->lexer);
9114 token = cp_lexer_peek_token (parser->lexer);
9115 if (cp_parser_allow_gnu_extensions_p (parser)
9116 && token->type == CPP_COLON)
9118 pedwarn (token->location, OPT_Wpedantic,
9119 "ISO C++ does not allow ?: with omitted middle operand");
9120 /* Implicit true clause. */
9121 expr = NULL_TREE;
9122 c_inhibit_evaluation_warnings +=
9123 folded_logical_or_expr == truthvalue_true_node;
9124 warn_for_omitted_condop (token->location, logical_or_expr);
9126 else
9128 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
9129 parser->colon_corrects_to_scope_p = false;
9130 /* Parse the expression. */
9131 c_inhibit_evaluation_warnings +=
9132 folded_logical_or_expr == truthvalue_false_node;
9133 expr = cp_parser_expression (parser);
9134 c_inhibit_evaluation_warnings +=
9135 ((folded_logical_or_expr == truthvalue_true_node)
9136 - (folded_logical_or_expr == truthvalue_false_node));
9137 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
9140 /* The next token should be a `:'. */
9141 cp_parser_require (parser, CPP_COLON, RT_COLON);
9142 /* Parse the assignment-expression. */
9143 assignment_expr = cp_parser_assignment_expression (parser);
9144 c_inhibit_evaluation_warnings -=
9145 folded_logical_or_expr == truthvalue_true_node;
9147 /* Make a location:
9148 LOGICAL_OR_EXPR ? EXPR : ASSIGNMENT_EXPR
9149 ~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
9150 with the caret at the "?", ranging from the start of
9151 the logical_or_expr to the end of the assignment_expr. */
9152 loc = make_location (loc,
9153 logical_or_expr.get_start (),
9154 assignment_expr.get_finish ());
9156 /* Build the conditional-expression. */
9157 return build_x_conditional_expr (loc, logical_or_expr,
9158 expr,
9159 assignment_expr,
9160 tf_warning_or_error);
9163 /* Parse an assignment-expression.
9165 assignment-expression:
9166 conditional-expression
9167 logical-or-expression assignment-operator assignment_expression
9168 throw-expression
9170 CAST_P is true if this expression is the target of a cast.
9171 DECLTYPE_P is true if this expression is the operand of decltype.
9173 Returns a representation for the expression. */
9175 static cp_expr
9176 cp_parser_assignment_expression (cp_parser* parser, cp_id_kind * pidk,
9177 bool cast_p, bool decltype_p)
9179 cp_expr expr;
9181 /* If the next token is the `throw' keyword, then we're looking at
9182 a throw-expression. */
9183 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
9184 expr = cp_parser_throw_expression (parser);
9185 /* Otherwise, it must be that we are looking at a
9186 logical-or-expression. */
9187 else
9189 /* Parse the binary expressions (logical-or-expression). */
9190 expr = cp_parser_binary_expression (parser, cast_p, false,
9191 decltype_p,
9192 PREC_NOT_OPERATOR, pidk);
9193 /* If the next token is a `?' then we're actually looking at a
9194 conditional-expression. */
9195 if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
9196 return cp_parser_question_colon_clause (parser, expr);
9197 else
9199 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
9201 /* If it's an assignment-operator, we're using the second
9202 production. */
9203 enum tree_code assignment_operator
9204 = cp_parser_assignment_operator_opt (parser);
9205 if (assignment_operator != ERROR_MARK)
9207 bool non_constant_p;
9209 /* Parse the right-hand side of the assignment. */
9210 cp_expr rhs = cp_parser_initializer_clause (parser,
9211 &non_constant_p);
9213 if (BRACE_ENCLOSED_INITIALIZER_P (rhs))
9214 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
9216 /* An assignment may not appear in a
9217 constant-expression. */
9218 if (cp_parser_non_integral_constant_expression (parser,
9219 NIC_ASSIGNMENT))
9220 return error_mark_node;
9221 /* Build the assignment expression. Its default
9222 location:
9223 LHS = RHS
9224 ~~~~^~~~~
9225 is the location of the '=' token as the
9226 caret, ranging from the start of the lhs to the
9227 end of the rhs. */
9228 loc = make_location (loc,
9229 expr.get_start (),
9230 rhs.get_finish ());
9231 expr = build_x_modify_expr (loc, expr,
9232 assignment_operator,
9233 rhs,
9234 complain_flags (decltype_p));
9235 /* TODO: build_x_modify_expr doesn't honor the location,
9236 so we must set it here. */
9237 expr.set_location (loc);
9242 return expr;
9245 /* Parse an (optional) assignment-operator.
9247 assignment-operator: one of
9248 = *= /= %= += -= >>= <<= &= ^= |=
9250 GNU Extension:
9252 assignment-operator: one of
9253 <?= >?=
9255 If the next token is an assignment operator, the corresponding tree
9256 code is returned, and the token is consumed. For example, for
9257 `+=', PLUS_EXPR is returned. For `=' itself, the code returned is
9258 NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%',
9259 TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment
9260 operator, ERROR_MARK is returned. */
9262 static enum tree_code
9263 cp_parser_assignment_operator_opt (cp_parser* parser)
9265 enum tree_code op;
9266 cp_token *token;
9268 /* Peek at the next token. */
9269 token = cp_lexer_peek_token (parser->lexer);
9271 switch (token->type)
9273 case CPP_EQ:
9274 op = NOP_EXPR;
9275 break;
9277 case CPP_MULT_EQ:
9278 op = MULT_EXPR;
9279 break;
9281 case CPP_DIV_EQ:
9282 op = TRUNC_DIV_EXPR;
9283 break;
9285 case CPP_MOD_EQ:
9286 op = TRUNC_MOD_EXPR;
9287 break;
9289 case CPP_PLUS_EQ:
9290 op = PLUS_EXPR;
9291 break;
9293 case CPP_MINUS_EQ:
9294 op = MINUS_EXPR;
9295 break;
9297 case CPP_RSHIFT_EQ:
9298 op = RSHIFT_EXPR;
9299 break;
9301 case CPP_LSHIFT_EQ:
9302 op = LSHIFT_EXPR;
9303 break;
9305 case CPP_AND_EQ:
9306 op = BIT_AND_EXPR;
9307 break;
9309 case CPP_XOR_EQ:
9310 op = BIT_XOR_EXPR;
9311 break;
9313 case CPP_OR_EQ:
9314 op = BIT_IOR_EXPR;
9315 break;
9317 default:
9318 /* Nothing else is an assignment operator. */
9319 op = ERROR_MARK;
9322 /* An operator followed by ... is a fold-expression, handled elsewhere. */
9323 if (op != ERROR_MARK
9324 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS))
9325 op = ERROR_MARK;
9327 /* If it was an assignment operator, consume it. */
9328 if (op != ERROR_MARK)
9329 cp_lexer_consume_token (parser->lexer);
9331 return op;
9334 /* Parse an expression.
9336 expression:
9337 assignment-expression
9338 expression , assignment-expression
9340 CAST_P is true if this expression is the target of a cast.
9341 DECLTYPE_P is true if this expression is the immediate operand of decltype,
9342 except possibly parenthesized or on the RHS of a comma (N3276).
9344 Returns a representation of the expression. */
9346 static cp_expr
9347 cp_parser_expression (cp_parser* parser, cp_id_kind * pidk,
9348 bool cast_p, bool decltype_p)
9350 cp_expr expression = NULL_TREE;
9351 location_t loc = UNKNOWN_LOCATION;
9353 while (true)
9355 cp_expr assignment_expression;
9357 /* Parse the next assignment-expression. */
9358 assignment_expression
9359 = cp_parser_assignment_expression (parser, pidk, cast_p, decltype_p);
9361 /* We don't create a temporary for a call that is the immediate operand
9362 of decltype or on the RHS of a comma. But when we see a comma, we
9363 need to create a temporary for a call on the LHS. */
9364 if (decltype_p && !processing_template_decl
9365 && TREE_CODE (assignment_expression) == CALL_EXPR
9366 && CLASS_TYPE_P (TREE_TYPE (assignment_expression))
9367 && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
9368 assignment_expression
9369 = build_cplus_new (TREE_TYPE (assignment_expression),
9370 assignment_expression, tf_warning_or_error);
9372 /* If this is the first assignment-expression, we can just
9373 save it away. */
9374 if (!expression)
9375 expression = assignment_expression;
9376 else
9378 /* Create a location with caret at the comma, ranging
9379 from the start of the LHS to the end of the RHS. */
9380 loc = make_location (loc,
9381 expression.get_start (),
9382 assignment_expression.get_finish ());
9383 expression = build_x_compound_expr (loc, expression,
9384 assignment_expression,
9385 complain_flags (decltype_p));
9386 expression.set_location (loc);
9388 /* If the next token is not a comma, or we're in a fold-expression, then
9389 we are done with the expression. */
9390 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA)
9391 || cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS))
9392 break;
9393 /* Consume the `,'. */
9394 loc = cp_lexer_peek_token (parser->lexer)->location;
9395 cp_lexer_consume_token (parser->lexer);
9396 /* A comma operator cannot appear in a constant-expression. */
9397 if (cp_parser_non_integral_constant_expression (parser, NIC_COMMA))
9398 expression = error_mark_node;
9401 return expression;
9404 /* Parse a constant-expression.
9406 constant-expression:
9407 conditional-expression
9409 If ALLOW_NON_CONSTANT_P a non-constant expression is silently
9410 accepted. If ALLOW_NON_CONSTANT_P is true and the expression is not
9411 constant, *NON_CONSTANT_P is set to TRUE. If ALLOW_NON_CONSTANT_P
9412 is false, NON_CONSTANT_P should be NULL. */
9414 static cp_expr
9415 cp_parser_constant_expression (cp_parser* parser,
9416 bool allow_non_constant_p,
9417 bool *non_constant_p)
9419 bool saved_integral_constant_expression_p;
9420 bool saved_allow_non_integral_constant_expression_p;
9421 bool saved_non_integral_constant_expression_p;
9422 cp_expr expression;
9424 /* It might seem that we could simply parse the
9425 conditional-expression, and then check to see if it were
9426 TREE_CONSTANT. However, an expression that is TREE_CONSTANT is
9427 one that the compiler can figure out is constant, possibly after
9428 doing some simplifications or optimizations. The standard has a
9429 precise definition of constant-expression, and we must honor
9430 that, even though it is somewhat more restrictive.
9432 For example:
9434 int i[(2, 3)];
9436 is not a legal declaration, because `(2, 3)' is not a
9437 constant-expression. The `,' operator is forbidden in a
9438 constant-expression. However, GCC's constant-folding machinery
9439 will fold this operation to an INTEGER_CST for `3'. */
9441 /* Save the old settings. */
9442 saved_integral_constant_expression_p = parser->integral_constant_expression_p;
9443 saved_allow_non_integral_constant_expression_p
9444 = parser->allow_non_integral_constant_expression_p;
9445 saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
9446 /* We are now parsing a constant-expression. */
9447 parser->integral_constant_expression_p = true;
9448 parser->allow_non_integral_constant_expression_p
9449 = (allow_non_constant_p || cxx_dialect >= cxx11);
9450 parser->non_integral_constant_expression_p = false;
9451 /* Although the grammar says "conditional-expression", we parse an
9452 "assignment-expression", which also permits "throw-expression"
9453 and the use of assignment operators. In the case that
9454 ALLOW_NON_CONSTANT_P is false, we get better errors than we would
9455 otherwise. In the case that ALLOW_NON_CONSTANT_P is true, it is
9456 actually essential that we look for an assignment-expression.
9457 For example, cp_parser_initializer_clauses uses this function to
9458 determine whether a particular assignment-expression is in fact
9459 constant. */
9460 expression = cp_parser_assignment_expression (parser);
9461 /* Restore the old settings. */
9462 parser->integral_constant_expression_p
9463 = saved_integral_constant_expression_p;
9464 parser->allow_non_integral_constant_expression_p
9465 = saved_allow_non_integral_constant_expression_p;
9466 if (cxx_dialect >= cxx11)
9468 /* Require an rvalue constant expression here; that's what our
9469 callers expect. Reference constant expressions are handled
9470 separately in e.g. cp_parser_template_argument. */
9471 bool is_const = potential_rvalue_constant_expression (expression);
9472 parser->non_integral_constant_expression_p = !is_const;
9473 if (!is_const && !allow_non_constant_p)
9474 require_potential_rvalue_constant_expression (expression);
9476 if (allow_non_constant_p)
9477 *non_constant_p = parser->non_integral_constant_expression_p;
9478 parser->non_integral_constant_expression_p
9479 = saved_non_integral_constant_expression_p;
9481 return expression;
9484 /* Parse __builtin_offsetof.
9486 offsetof-expression:
9487 "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
9489 offsetof-member-designator:
9490 id-expression
9491 | offsetof-member-designator "." id-expression
9492 | offsetof-member-designator "[" expression "]"
9493 | offsetof-member-designator "->" id-expression */
9495 static cp_expr
9496 cp_parser_builtin_offsetof (cp_parser *parser)
9498 int save_ice_p, save_non_ice_p;
9499 tree type;
9500 cp_expr expr;
9501 cp_id_kind dummy;
9502 cp_token *token;
9503 location_t finish_loc;
9505 /* We're about to accept non-integral-constant things, but will
9506 definitely yield an integral constant expression. Save and
9507 restore these values around our local parsing. */
9508 save_ice_p = parser->integral_constant_expression_p;
9509 save_non_ice_p = parser->non_integral_constant_expression_p;
9511 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
9513 /* Consume the "__builtin_offsetof" token. */
9514 cp_lexer_consume_token (parser->lexer);
9515 /* Consume the opening `('. */
9516 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
9517 /* Parse the type-id. */
9518 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
9519 type = cp_parser_type_id (parser);
9520 /* Look for the `,'. */
9521 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
9522 token = cp_lexer_peek_token (parser->lexer);
9524 /* Build the (type *)null that begins the traditional offsetof macro. */
9525 tree object_ptr
9526 = build_static_cast (build_pointer_type (type), null_pointer_node,
9527 tf_warning_or_error);
9529 /* Parse the offsetof-member-designator. We begin as if we saw "expr->". */
9530 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, object_ptr,
9531 true, &dummy, token->location);
9532 while (true)
9534 token = cp_lexer_peek_token (parser->lexer);
9535 switch (token->type)
9537 case CPP_OPEN_SQUARE:
9538 /* offsetof-member-designator "[" expression "]" */
9539 expr = cp_parser_postfix_open_square_expression (parser, expr,
9540 true, false);
9541 break;
9543 case CPP_DEREF:
9544 /* offsetof-member-designator "->" identifier */
9545 expr = grok_array_decl (token->location, expr,
9546 integer_zero_node, false);
9547 /* FALLTHRU */
9549 case CPP_DOT:
9550 /* offsetof-member-designator "." identifier */
9551 cp_lexer_consume_token (parser->lexer);
9552 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT,
9553 expr, true, &dummy,
9554 token->location);
9555 break;
9557 case CPP_CLOSE_PAREN:
9558 /* Consume the ")" token. */
9559 finish_loc = cp_lexer_peek_token (parser->lexer)->location;
9560 cp_lexer_consume_token (parser->lexer);
9561 goto success;
9563 default:
9564 /* Error. We know the following require will fail, but
9565 that gives the proper error message. */
9566 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
9567 cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
9568 expr = error_mark_node;
9569 goto failure;
9573 success:
9574 /* Make a location of the form:
9575 __builtin_offsetof (struct s, f)
9576 ~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~
9577 with caret at the type-id, ranging from the start of the
9578 "_builtin_offsetof" token to the close paren. */
9579 loc = make_location (loc, start_loc, finish_loc);
9580 /* The result will be an INTEGER_CST, so we need to explicitly
9581 preserve the location. */
9582 expr = cp_expr (finish_offsetof (object_ptr, expr, loc), loc);
9584 failure:
9585 parser->integral_constant_expression_p = save_ice_p;
9586 parser->non_integral_constant_expression_p = save_non_ice_p;
9588 return expr;
9591 /* Parse a trait expression.
9593 Returns a representation of the expression, the underlying type
9594 of the type at issue when KEYWORD is RID_UNDERLYING_TYPE. */
9596 static tree
9597 cp_parser_trait_expr (cp_parser* parser, enum rid keyword)
9599 cp_trait_kind kind;
9600 tree type1, type2 = NULL_TREE;
9601 bool binary = false;
9602 bool variadic = false;
9604 switch (keyword)
9606 case RID_HAS_NOTHROW_ASSIGN:
9607 kind = CPTK_HAS_NOTHROW_ASSIGN;
9608 break;
9609 case RID_HAS_NOTHROW_CONSTRUCTOR:
9610 kind = CPTK_HAS_NOTHROW_CONSTRUCTOR;
9611 break;
9612 case RID_HAS_NOTHROW_COPY:
9613 kind = CPTK_HAS_NOTHROW_COPY;
9614 break;
9615 case RID_HAS_TRIVIAL_ASSIGN:
9616 kind = CPTK_HAS_TRIVIAL_ASSIGN;
9617 break;
9618 case RID_HAS_TRIVIAL_CONSTRUCTOR:
9619 kind = CPTK_HAS_TRIVIAL_CONSTRUCTOR;
9620 break;
9621 case RID_HAS_TRIVIAL_COPY:
9622 kind = CPTK_HAS_TRIVIAL_COPY;
9623 break;
9624 case RID_HAS_TRIVIAL_DESTRUCTOR:
9625 kind = CPTK_HAS_TRIVIAL_DESTRUCTOR;
9626 break;
9627 case RID_HAS_UNIQUE_OBJ_REPRESENTATIONS:
9628 kind = CPTK_HAS_UNIQUE_OBJ_REPRESENTATIONS;
9629 break;
9630 case RID_HAS_VIRTUAL_DESTRUCTOR:
9631 kind = CPTK_HAS_VIRTUAL_DESTRUCTOR;
9632 break;
9633 case RID_IS_ABSTRACT:
9634 kind = CPTK_IS_ABSTRACT;
9635 break;
9636 case RID_IS_BASE_OF:
9637 kind = CPTK_IS_BASE_OF;
9638 binary = true;
9639 break;
9640 case RID_IS_CLASS:
9641 kind = CPTK_IS_CLASS;
9642 break;
9643 case RID_IS_EMPTY:
9644 kind = CPTK_IS_EMPTY;
9645 break;
9646 case RID_IS_ENUM:
9647 kind = CPTK_IS_ENUM;
9648 break;
9649 case RID_IS_FINAL:
9650 kind = CPTK_IS_FINAL;
9651 break;
9652 case RID_IS_LITERAL_TYPE:
9653 kind = CPTK_IS_LITERAL_TYPE;
9654 break;
9655 case RID_IS_POD:
9656 kind = CPTK_IS_POD;
9657 break;
9658 case RID_IS_POLYMORPHIC:
9659 kind = CPTK_IS_POLYMORPHIC;
9660 break;
9661 case RID_IS_SAME_AS:
9662 kind = CPTK_IS_SAME_AS;
9663 binary = true;
9664 break;
9665 case RID_IS_STD_LAYOUT:
9666 kind = CPTK_IS_STD_LAYOUT;
9667 break;
9668 case RID_IS_TRIVIAL:
9669 kind = CPTK_IS_TRIVIAL;
9670 break;
9671 case RID_IS_TRIVIALLY_ASSIGNABLE:
9672 kind = CPTK_IS_TRIVIALLY_ASSIGNABLE;
9673 binary = true;
9674 break;
9675 case RID_IS_TRIVIALLY_CONSTRUCTIBLE:
9676 kind = CPTK_IS_TRIVIALLY_CONSTRUCTIBLE;
9677 variadic = true;
9678 break;
9679 case RID_IS_TRIVIALLY_COPYABLE:
9680 kind = CPTK_IS_TRIVIALLY_COPYABLE;
9681 break;
9682 case RID_IS_UNION:
9683 kind = CPTK_IS_UNION;
9684 break;
9685 case RID_UNDERLYING_TYPE:
9686 kind = CPTK_UNDERLYING_TYPE;
9687 break;
9688 case RID_BASES:
9689 kind = CPTK_BASES;
9690 break;
9691 case RID_DIRECT_BASES:
9692 kind = CPTK_DIRECT_BASES;
9693 break;
9694 default:
9695 gcc_unreachable ();
9698 /* Consume the token. */
9699 cp_lexer_consume_token (parser->lexer);
9701 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
9704 type_id_in_expr_sentinel s (parser);
9705 type1 = cp_parser_type_id (parser);
9708 if (type1 == error_mark_node)
9709 return error_mark_node;
9711 if (binary)
9713 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
9716 type_id_in_expr_sentinel s (parser);
9717 type2 = cp_parser_type_id (parser);
9720 if (type2 == error_mark_node)
9721 return error_mark_node;
9723 else if (variadic)
9725 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
9727 cp_lexer_consume_token (parser->lexer);
9728 tree elt = cp_parser_type_id (parser);
9729 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
9731 cp_lexer_consume_token (parser->lexer);
9732 elt = make_pack_expansion (elt);
9734 if (elt == error_mark_node)
9735 return error_mark_node;
9736 type2 = tree_cons (NULL_TREE, elt, type2);
9740 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
9742 /* Complete the trait expression, which may mean either processing
9743 the trait expr now or saving it for template instantiation. */
9744 switch (kind)
9746 case CPTK_UNDERLYING_TYPE:
9747 return finish_underlying_type (type1);
9748 case CPTK_BASES:
9749 return finish_bases (type1, false);
9750 case CPTK_DIRECT_BASES:
9751 return finish_bases (type1, true);
9752 default:
9753 return finish_trait_expr (kind, type1, type2);
9757 /* Lambdas that appear in variable initializer or default argument scope
9758 get that in their mangling, so we need to record it. We might as well
9759 use the count for function and namespace scopes as well. */
9760 static GTY(()) tree lambda_scope;
9761 static GTY(()) int lambda_count;
9762 struct GTY(()) tree_int
9764 tree t;
9765 int i;
9767 static GTY(()) vec<tree_int, va_gc> *lambda_scope_stack;
9769 static void
9770 start_lambda_scope (tree decl)
9772 tree_int ti;
9773 gcc_assert (decl);
9774 /* Once we're inside a function, we ignore other scopes and just push
9775 the function again so that popping works properly. */
9776 if (current_function_decl && TREE_CODE (decl) != FUNCTION_DECL)
9777 decl = current_function_decl;
9778 ti.t = lambda_scope;
9779 ti.i = lambda_count;
9780 vec_safe_push (lambda_scope_stack, ti);
9781 if (lambda_scope != decl)
9783 /* Don't reset the count if we're still in the same function. */
9784 lambda_scope = decl;
9785 lambda_count = 0;
9789 static void
9790 record_lambda_scope (tree lambda)
9792 LAMBDA_EXPR_EXTRA_SCOPE (lambda) = lambda_scope;
9793 LAMBDA_EXPR_DISCRIMINATOR (lambda) = lambda_count++;
9796 static void
9797 finish_lambda_scope (void)
9799 tree_int *p = &lambda_scope_stack->last ();
9800 if (lambda_scope != p->t)
9802 lambda_scope = p->t;
9803 lambda_count = p->i;
9805 lambda_scope_stack->pop ();
9808 /* Parse a lambda expression.
9810 lambda-expression:
9811 lambda-introducer lambda-declarator [opt] compound-statement
9813 Returns a representation of the expression. */
9815 static cp_expr
9816 cp_parser_lambda_expression (cp_parser* parser)
9818 tree lambda_expr = build_lambda_expr ();
9819 tree type;
9820 bool ok = true;
9821 cp_token *token = cp_lexer_peek_token (parser->lexer);
9822 cp_token_position start = 0;
9824 LAMBDA_EXPR_LOCATION (lambda_expr) = token->location;
9826 if (cp_unevaluated_operand)
9828 if (!token->error_reported)
9830 error_at (LAMBDA_EXPR_LOCATION (lambda_expr),
9831 "lambda-expression in unevaluated context");
9832 token->error_reported = true;
9834 ok = false;
9836 else if (parser->in_template_argument_list_p)
9838 if (!token->error_reported)
9840 error_at (token->location, "lambda-expression in template-argument");
9841 token->error_reported = true;
9843 ok = false;
9846 /* We may be in the middle of deferred access check. Disable
9847 it now. */
9848 push_deferring_access_checks (dk_no_deferred);
9850 cp_parser_lambda_introducer (parser, lambda_expr);
9852 type = begin_lambda_type (lambda_expr);
9853 if (type == error_mark_node)
9854 return error_mark_node;
9856 record_lambda_scope (lambda_expr);
9858 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
9859 determine_visibility (TYPE_NAME (type));
9861 /* Now that we've started the type, add the capture fields for any
9862 explicit captures. */
9863 register_capture_members (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr));
9866 /* Inside the class, surrounding template-parameter-lists do not apply. */
9867 unsigned int saved_num_template_parameter_lists
9868 = parser->num_template_parameter_lists;
9869 unsigned char in_statement = parser->in_statement;
9870 bool in_switch_statement_p = parser->in_switch_statement_p;
9871 bool fully_implicit_function_template_p
9872 = parser->fully_implicit_function_template_p;
9873 tree implicit_template_parms = parser->implicit_template_parms;
9874 cp_binding_level* implicit_template_scope = parser->implicit_template_scope;
9875 bool auto_is_implicit_function_template_parm_p
9876 = parser->auto_is_implicit_function_template_parm_p;
9878 parser->num_template_parameter_lists = 0;
9879 parser->in_statement = 0;
9880 parser->in_switch_statement_p = false;
9881 parser->fully_implicit_function_template_p = false;
9882 parser->implicit_template_parms = 0;
9883 parser->implicit_template_scope = 0;
9884 parser->auto_is_implicit_function_template_parm_p = false;
9886 /* By virtue of defining a local class, a lambda expression has access to
9887 the private variables of enclosing classes. */
9889 ok &= cp_parser_lambda_declarator_opt (parser, lambda_expr);
9891 if (ok && cp_parser_error_occurred (parser))
9892 ok = false;
9894 if (ok)
9896 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
9897 && cp_parser_start_tentative_firewall (parser))
9898 start = token;
9899 cp_parser_lambda_body (parser, lambda_expr);
9901 else if (cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
9903 if (cp_parser_skip_to_closing_brace (parser))
9904 cp_lexer_consume_token (parser->lexer);
9907 /* The capture list was built up in reverse order; fix that now. */
9908 LAMBDA_EXPR_CAPTURE_LIST (lambda_expr)
9909 = nreverse (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr));
9911 if (ok)
9912 maybe_add_lambda_conv_op (type);
9914 type = finish_struct (type, /*attributes=*/NULL_TREE);
9916 parser->num_template_parameter_lists = saved_num_template_parameter_lists;
9917 parser->in_statement = in_statement;
9918 parser->in_switch_statement_p = in_switch_statement_p;
9919 parser->fully_implicit_function_template_p
9920 = fully_implicit_function_template_p;
9921 parser->implicit_template_parms = implicit_template_parms;
9922 parser->implicit_template_scope = implicit_template_scope;
9923 parser->auto_is_implicit_function_template_parm_p
9924 = auto_is_implicit_function_template_parm_p;
9927 /* This field is only used during parsing of the lambda. */
9928 LAMBDA_EXPR_THIS_CAPTURE (lambda_expr) = NULL_TREE;
9930 /* This lambda shouldn't have any proxies left at this point. */
9931 gcc_assert (LAMBDA_EXPR_PENDING_PROXIES (lambda_expr) == NULL);
9932 /* And now that we're done, push proxies for an enclosing lambda. */
9933 insert_pending_capture_proxies ();
9935 if (ok)
9936 lambda_expr = build_lambda_object (lambda_expr);
9937 else
9938 lambda_expr = error_mark_node;
9940 cp_parser_end_tentative_firewall (parser, start, lambda_expr);
9942 pop_deferring_access_checks ();
9944 return lambda_expr;
9947 /* Parse the beginning of a lambda expression.
9949 lambda-introducer:
9950 [ lambda-capture [opt] ]
9952 LAMBDA_EXPR is the current representation of the lambda expression. */
9954 static void
9955 cp_parser_lambda_introducer (cp_parser* parser, tree lambda_expr)
9957 /* Need commas after the first capture. */
9958 bool first = true;
9960 /* Eat the leading `['. */
9961 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
9963 /* Record default capture mode. "[&" "[=" "[&," "[=," */
9964 if (cp_lexer_next_token_is (parser->lexer, CPP_AND)
9965 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_NAME)
9966 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_REFERENCE;
9967 else if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
9968 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_COPY;
9970 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE)
9972 cp_lexer_consume_token (parser->lexer);
9973 first = false;
9976 while (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_SQUARE))
9978 cp_token* capture_token;
9979 tree capture_id;
9980 tree capture_init_expr;
9981 cp_id_kind idk = CP_ID_KIND_NONE;
9982 bool explicit_init_p = false;
9984 enum capture_kind_type
9986 BY_COPY,
9987 BY_REFERENCE
9989 enum capture_kind_type capture_kind = BY_COPY;
9991 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
9993 error ("expected end of capture-list");
9994 return;
9997 if (first)
9998 first = false;
9999 else
10000 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
10002 /* Possibly capture `this'. */
10003 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THIS))
10005 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
10006 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_COPY)
10007 pedwarn (loc, 0, "explicit by-copy capture of %<this%> redundant "
10008 "with by-copy capture default");
10009 cp_lexer_consume_token (parser->lexer);
10010 add_capture (lambda_expr,
10011 /*id=*/this_identifier,
10012 /*initializer=*/finish_this_expr (),
10013 /*by_reference_p=*/true,
10014 explicit_init_p);
10015 continue;
10018 /* Possibly capture `*this'. */
10019 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT)
10020 && cp_lexer_nth_token_is_keyword (parser->lexer, 2, RID_THIS))
10022 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
10023 if (cxx_dialect < cxx1z)
10024 pedwarn (loc, 0, "%<*this%> capture only available with "
10025 "-std=c++1z or -std=gnu++1z");
10026 cp_lexer_consume_token (parser->lexer);
10027 cp_lexer_consume_token (parser->lexer);
10028 add_capture (lambda_expr,
10029 /*id=*/this_identifier,
10030 /*initializer=*/finish_this_expr (),
10031 /*by_reference_p=*/false,
10032 explicit_init_p);
10033 continue;
10036 /* Remember whether we want to capture as a reference or not. */
10037 if (cp_lexer_next_token_is (parser->lexer, CPP_AND))
10039 capture_kind = BY_REFERENCE;
10040 cp_lexer_consume_token (parser->lexer);
10043 /* Get the identifier. */
10044 capture_token = cp_lexer_peek_token (parser->lexer);
10045 capture_id = cp_parser_identifier (parser);
10047 if (capture_id == error_mark_node)
10048 /* Would be nice to have a cp_parser_skip_to_closing_x for general
10049 delimiters, but I modified this to stop on unnested ']' as well. It
10050 was already changed to stop on unnested '}', so the
10051 "closing_parenthesis" name is no more misleading with my change. */
10053 cp_parser_skip_to_closing_parenthesis (parser,
10054 /*recovering=*/true,
10055 /*or_comma=*/true,
10056 /*consume_paren=*/true);
10057 break;
10060 /* Find the initializer for this capture. */
10061 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ)
10062 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
10063 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
10065 bool direct, non_constant;
10066 /* An explicit initializer exists. */
10067 if (cxx_dialect < cxx14)
10068 pedwarn (input_location, 0,
10069 "lambda capture initializers "
10070 "only available with -std=c++14 or -std=gnu++14");
10071 capture_init_expr = cp_parser_initializer (parser, &direct,
10072 &non_constant);
10073 explicit_init_p = true;
10074 if (capture_init_expr == NULL_TREE)
10076 error ("empty initializer for lambda init-capture");
10077 capture_init_expr = error_mark_node;
10080 else
10082 const char* error_msg;
10084 /* Turn the identifier into an id-expression. */
10085 capture_init_expr
10086 = cp_parser_lookup_name_simple (parser, capture_id,
10087 capture_token->location);
10089 if (capture_init_expr == error_mark_node)
10091 unqualified_name_lookup_error (capture_id);
10092 continue;
10094 else if (DECL_P (capture_init_expr)
10095 && (!VAR_P (capture_init_expr)
10096 && TREE_CODE (capture_init_expr) != PARM_DECL))
10098 error_at (capture_token->location,
10099 "capture of non-variable %qD ",
10100 capture_init_expr);
10101 inform (DECL_SOURCE_LOCATION (capture_init_expr),
10102 "%q#D declared here", capture_init_expr);
10103 continue;
10105 if (VAR_P (capture_init_expr)
10106 && decl_storage_duration (capture_init_expr) != dk_auto)
10108 if (pedwarn (capture_token->location, 0, "capture of variable "
10109 "%qD with non-automatic storage duration",
10110 capture_init_expr))
10111 inform (DECL_SOURCE_LOCATION (capture_init_expr),
10112 "%q#D declared here", capture_init_expr);
10113 continue;
10116 capture_init_expr
10117 = finish_id_expression
10118 (capture_id,
10119 capture_init_expr,
10120 parser->scope,
10121 &idk,
10122 /*integral_constant_expression_p=*/false,
10123 /*allow_non_integral_constant_expression_p=*/false,
10124 /*non_integral_constant_expression_p=*/NULL,
10125 /*template_p=*/false,
10126 /*done=*/true,
10127 /*address_p=*/false,
10128 /*template_arg_p=*/false,
10129 &error_msg,
10130 capture_token->location);
10132 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
10134 cp_lexer_consume_token (parser->lexer);
10135 capture_init_expr = make_pack_expansion (capture_init_expr);
10137 else
10138 check_for_bare_parameter_packs (capture_init_expr);
10141 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE
10142 && !explicit_init_p)
10144 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_COPY
10145 && capture_kind == BY_COPY)
10146 pedwarn (capture_token->location, 0, "explicit by-copy capture "
10147 "of %qD redundant with by-copy capture default",
10148 capture_id);
10149 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_REFERENCE
10150 && capture_kind == BY_REFERENCE)
10151 pedwarn (capture_token->location, 0, "explicit by-reference "
10152 "capture of %qD redundant with by-reference capture "
10153 "default", capture_id);
10156 add_capture (lambda_expr,
10157 capture_id,
10158 capture_init_expr,
10159 /*by_reference_p=*/capture_kind == BY_REFERENCE,
10160 explicit_init_p);
10163 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
10166 /* Parse the (optional) middle of a lambda expression.
10168 lambda-declarator:
10169 < template-parameter-list [opt] >
10170 ( parameter-declaration-clause [opt] )
10171 attribute-specifier [opt]
10172 decl-specifier-seq [opt]
10173 exception-specification [opt]
10174 lambda-return-type-clause [opt]
10176 LAMBDA_EXPR is the current representation of the lambda expression. */
10178 static bool
10179 cp_parser_lambda_declarator_opt (cp_parser* parser, tree lambda_expr)
10181 /* 5.1.1.4 of the standard says:
10182 If a lambda-expression does not include a lambda-declarator, it is as if
10183 the lambda-declarator were ().
10184 This means an empty parameter list, no attributes, and no exception
10185 specification. */
10186 tree param_list = void_list_node;
10187 tree attributes = NULL_TREE;
10188 tree exception_spec = NULL_TREE;
10189 tree template_param_list = NULL_TREE;
10190 tree tx_qual = NULL_TREE;
10191 cp_decl_specifier_seq lambda_specs;
10192 clear_decl_specs (&lambda_specs);
10194 /* The template-parameter-list is optional, but must begin with
10195 an opening angle if present. */
10196 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
10198 if (cxx_dialect < cxx14)
10199 pedwarn (parser->lexer->next_token->location, 0,
10200 "lambda templates are only available with "
10201 "-std=c++14 or -std=gnu++14");
10202 else
10203 pedwarn (parser->lexer->next_token->location, OPT_Wpedantic,
10204 "ISO C++ does not support lambda templates");
10206 cp_lexer_consume_token (parser->lexer);
10208 template_param_list = cp_parser_template_parameter_list (parser);
10210 cp_parser_skip_to_end_of_template_parameter_list (parser);
10212 /* We just processed one more parameter list. */
10213 ++parser->num_template_parameter_lists;
10216 /* The parameter-declaration-clause is optional (unless
10217 template-parameter-list was given), but must begin with an
10218 opening parenthesis if present. */
10219 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
10221 cp_lexer_consume_token (parser->lexer);
10223 begin_scope (sk_function_parms, /*entity=*/NULL_TREE);
10225 /* Parse parameters. */
10226 param_list = cp_parser_parameter_declaration_clause (parser);
10228 /* Default arguments shall not be specified in the
10229 parameter-declaration-clause of a lambda-declarator. */
10230 if (cxx_dialect < cxx14)
10231 for (tree t = param_list; t; t = TREE_CHAIN (t))
10232 if (TREE_PURPOSE (t) && DECL_P (TREE_VALUE (t)))
10233 pedwarn (DECL_SOURCE_LOCATION (TREE_VALUE (t)), OPT_Wpedantic,
10234 "default argument specified for lambda parameter");
10236 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
10238 attributes = cp_parser_attributes_opt (parser);
10240 /* In the decl-specifier-seq of the lambda-declarator, each
10241 decl-specifier shall either be mutable or constexpr. */
10242 int declares_class_or_enum;
10243 if (cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer))
10244 cp_parser_decl_specifier_seq (parser,
10245 CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR,
10246 &lambda_specs, &declares_class_or_enum);
10247 if (lambda_specs.storage_class == sc_mutable)
10249 LAMBDA_EXPR_MUTABLE_P (lambda_expr) = 1;
10250 if (lambda_specs.conflicting_specifiers_p)
10251 error_at (lambda_specs.locations[ds_storage_class],
10252 "duplicate %<mutable%>");
10255 tx_qual = cp_parser_tx_qualifier_opt (parser);
10257 /* Parse optional exception specification. */
10258 exception_spec = cp_parser_exception_specification_opt (parser);
10260 /* Parse optional trailing return type. */
10261 if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
10263 cp_lexer_consume_token (parser->lexer);
10264 LAMBDA_EXPR_RETURN_TYPE (lambda_expr)
10265 = cp_parser_trailing_type_id (parser);
10268 /* The function parameters must be in scope all the way until after the
10269 trailing-return-type in case of decltype. */
10270 pop_bindings_and_leave_scope ();
10272 else if (template_param_list != NULL_TREE) // generate diagnostic
10273 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
10275 /* Create the function call operator.
10277 Messing with declarators like this is no uglier than building up the
10278 FUNCTION_DECL by hand, and this is less likely to get out of sync with
10279 other code. */
10281 cp_decl_specifier_seq return_type_specs;
10282 cp_declarator* declarator;
10283 tree fco;
10284 int quals;
10285 void *p;
10287 clear_decl_specs (&return_type_specs);
10288 if (LAMBDA_EXPR_RETURN_TYPE (lambda_expr))
10289 return_type_specs.type = LAMBDA_EXPR_RETURN_TYPE (lambda_expr);
10290 else
10291 /* Maybe we will deduce the return type later. */
10292 return_type_specs.type = make_auto ();
10294 if (lambda_specs.locations[ds_constexpr])
10296 if (cxx_dialect >= cxx1z)
10297 return_type_specs.locations[ds_constexpr]
10298 = lambda_specs.locations[ds_constexpr];
10299 else
10300 error_at (lambda_specs.locations[ds_constexpr], "%<constexpr%> "
10301 "lambda only available with -std=c++1z or -std=gnu++1z");
10304 p = obstack_alloc (&declarator_obstack, 0);
10306 declarator = make_id_declarator (NULL_TREE, cp_operator_id (CALL_EXPR),
10307 sfk_none);
10309 quals = (LAMBDA_EXPR_MUTABLE_P (lambda_expr)
10310 ? TYPE_UNQUALIFIED : TYPE_QUAL_CONST);
10311 declarator = make_call_declarator (declarator, param_list, quals,
10312 VIRT_SPEC_UNSPECIFIED,
10313 REF_QUAL_NONE,
10314 tx_qual,
10315 exception_spec,
10316 /*late_return_type=*/NULL_TREE,
10317 /*requires_clause*/NULL_TREE);
10318 declarator->id_loc = LAMBDA_EXPR_LOCATION (lambda_expr);
10320 fco = grokmethod (&return_type_specs,
10321 declarator,
10322 attributes);
10323 if (fco != error_mark_node)
10325 DECL_INITIALIZED_IN_CLASS_P (fco) = 1;
10326 DECL_ARTIFICIAL (fco) = 1;
10327 /* Give the object parameter a different name. */
10328 DECL_NAME (DECL_ARGUMENTS (fco)) = get_identifier ("__closure");
10329 if (LAMBDA_EXPR_RETURN_TYPE (lambda_expr))
10330 TYPE_HAS_LATE_RETURN_TYPE (TREE_TYPE (fco)) = 1;
10332 if (template_param_list)
10334 fco = finish_member_template_decl (fco);
10335 finish_template_decl (template_param_list);
10336 --parser->num_template_parameter_lists;
10338 else if (parser->fully_implicit_function_template_p)
10339 fco = finish_fully_implicit_template (parser, fco);
10341 finish_member_declaration (fco);
10343 obstack_free (&declarator_obstack, p);
10345 return (fco != error_mark_node);
10349 /* Parse the body of a lambda expression, which is simply
10351 compound-statement
10353 but which requires special handling.
10354 LAMBDA_EXPR is the current representation of the lambda expression. */
10356 static void
10357 cp_parser_lambda_body (cp_parser* parser, tree lambda_expr)
10359 bool nested = (current_function_decl != NULL_TREE);
10360 bool local_variables_forbidden_p = parser->local_variables_forbidden_p;
10361 if (nested)
10362 push_function_context ();
10363 else
10364 /* Still increment function_depth so that we don't GC in the
10365 middle of an expression. */
10366 ++function_depth;
10367 vec<tree> omp_privatization_save;
10368 save_omp_privatization_clauses (omp_privatization_save);
10369 /* Clear this in case we're in the middle of a default argument. */
10370 parser->local_variables_forbidden_p = false;
10372 /* Finish the function call operator
10373 - class_specifier
10374 + late_parsing_for_member
10375 + function_definition_after_declarator
10376 + ctor_initializer_opt_and_function_body */
10378 tree fco = lambda_function (lambda_expr);
10379 tree body;
10380 bool done = false;
10381 tree compound_stmt;
10382 tree cap;
10384 /* Let the front end know that we are going to be defining this
10385 function. */
10386 start_preparsed_function (fco,
10387 NULL_TREE,
10388 SF_PRE_PARSED | SF_INCLASS_INLINE);
10390 start_lambda_scope (fco);
10391 body = begin_function_body ();
10393 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
10394 goto out;
10396 /* Push the proxies for any explicit captures. */
10397 for (cap = LAMBDA_EXPR_CAPTURE_LIST (lambda_expr); cap;
10398 cap = TREE_CHAIN (cap))
10399 build_capture_proxy (TREE_PURPOSE (cap));
10401 compound_stmt = begin_compound_stmt (0);
10403 /* 5.1.1.4 of the standard says:
10404 If a lambda-expression does not include a trailing-return-type, it
10405 is as if the trailing-return-type denotes the following type:
10406 * if the compound-statement is of the form
10407 { return attribute-specifier [opt] expression ; }
10408 the type of the returned expression after lvalue-to-rvalue
10409 conversion (_conv.lval_ 4.1), array-to-pointer conversion
10410 (_conv.array_ 4.2), and function-to-pointer conversion
10411 (_conv.func_ 4.3);
10412 * otherwise, void. */
10414 /* In a lambda that has neither a lambda-return-type-clause
10415 nor a deducible form, errors should be reported for return statements
10416 in the body. Since we used void as the placeholder return type, parsing
10417 the body as usual will give such desired behavior. */
10418 if (!LAMBDA_EXPR_RETURN_TYPE (lambda_expr)
10419 && cp_lexer_peek_nth_token (parser->lexer, 1)->keyword == RID_RETURN
10420 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SEMICOLON)
10422 tree expr = NULL_TREE;
10423 cp_id_kind idk = CP_ID_KIND_NONE;
10425 /* Parse tentatively in case there's more after the initial return
10426 statement. */
10427 cp_parser_parse_tentatively (parser);
10429 cp_parser_require_keyword (parser, RID_RETURN, RT_RETURN);
10431 expr = cp_parser_expression (parser, &idk);
10433 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10434 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
10436 if (cp_parser_parse_definitely (parser))
10438 if (!processing_template_decl)
10440 tree type = lambda_return_type (expr);
10441 apply_deduced_return_type (fco, type);
10442 if (type == error_mark_node)
10443 expr = error_mark_node;
10446 /* Will get error here if type not deduced yet. */
10447 finish_return_stmt (expr);
10449 done = true;
10453 if (!done)
10455 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
10456 cp_parser_label_declaration (parser);
10457 cp_parser_statement_seq_opt (parser, NULL_TREE);
10458 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
10461 finish_compound_stmt (compound_stmt);
10463 out:
10464 finish_function_body (body);
10465 finish_lambda_scope ();
10467 /* Finish the function and generate code for it if necessary. */
10468 tree fn = finish_function (/*inline*/2);
10470 /* Only expand if the call op is not a template. */
10471 if (!DECL_TEMPLATE_INFO (fco))
10472 expand_or_defer_fn (fn);
10475 restore_omp_privatization_clauses (omp_privatization_save);
10476 parser->local_variables_forbidden_p = local_variables_forbidden_p;
10477 if (nested)
10478 pop_function_context();
10479 else
10480 --function_depth;
10483 /* Statements [gram.stmt.stmt] */
10485 /* Parse a statement.
10487 statement:
10488 labeled-statement
10489 expression-statement
10490 compound-statement
10491 selection-statement
10492 iteration-statement
10493 jump-statement
10494 declaration-statement
10495 try-block
10497 C++11:
10499 statement:
10500 labeled-statement
10501 attribute-specifier-seq (opt) expression-statement
10502 attribute-specifier-seq (opt) compound-statement
10503 attribute-specifier-seq (opt) selection-statement
10504 attribute-specifier-seq (opt) iteration-statement
10505 attribute-specifier-seq (opt) jump-statement
10506 declaration-statement
10507 attribute-specifier-seq (opt) try-block
10509 init-statement:
10510 expression-statement
10511 simple-declaration
10513 TM Extension:
10515 statement:
10516 atomic-statement
10518 IN_COMPOUND is true when the statement is nested inside a
10519 cp_parser_compound_statement; this matters for certain pragmas.
10521 If IF_P is not NULL, *IF_P is set to indicate whether the statement
10522 is a (possibly labeled) if statement which is not enclosed in braces
10523 and has an else clause. This is used to implement -Wparentheses.
10525 CHAIN is a vector of if-else-if conditions. */
10527 static void
10528 cp_parser_statement (cp_parser* parser, tree in_statement_expr,
10529 bool in_compound, bool *if_p, vec<tree> *chain)
10531 tree statement, std_attrs = NULL_TREE;
10532 cp_token *token;
10533 location_t statement_location, attrs_location;
10535 restart:
10536 if (if_p != NULL)
10537 *if_p = false;
10538 /* There is no statement yet. */
10539 statement = NULL_TREE;
10541 saved_token_sentinel saved_tokens (parser->lexer);
10542 attrs_location = cp_lexer_peek_token (parser->lexer)->location;
10543 if (c_dialect_objc ())
10544 /* In obj-c++, seeing '[[' might be the either the beginning of
10545 c++11 attributes, or a nested objc-message-expression. So
10546 let's parse the c++11 attributes tentatively. */
10547 cp_parser_parse_tentatively (parser);
10548 std_attrs = cp_parser_std_attribute_spec_seq (parser);
10549 if (c_dialect_objc ())
10551 if (!cp_parser_parse_definitely (parser))
10552 std_attrs = NULL_TREE;
10555 /* Peek at the next token. */
10556 token = cp_lexer_peek_token (parser->lexer);
10557 /* Remember the location of the first token in the statement. */
10558 statement_location = token->location;
10559 /* If this is a keyword, then that will often determine what kind of
10560 statement we have. */
10561 if (token->type == CPP_KEYWORD)
10563 enum rid keyword = token->keyword;
10565 switch (keyword)
10567 case RID_CASE:
10568 case RID_DEFAULT:
10569 /* Looks like a labeled-statement with a case label.
10570 Parse the label, and then use tail recursion to parse
10571 the statement. */
10572 cp_parser_label_for_labeled_statement (parser, std_attrs);
10573 in_compound = false;
10574 goto restart;
10576 case RID_IF:
10577 case RID_SWITCH:
10578 statement = cp_parser_selection_statement (parser, if_p, chain);
10579 break;
10581 case RID_WHILE:
10582 case RID_DO:
10583 case RID_FOR:
10584 statement = cp_parser_iteration_statement (parser, if_p, false);
10585 break;
10587 case RID_CILK_FOR:
10588 if (!flag_cilkplus)
10590 error_at (cp_lexer_peek_token (parser->lexer)->location,
10591 "-fcilkplus must be enabled to use %<_Cilk_for%>");
10592 cp_lexer_consume_token (parser->lexer);
10593 statement = error_mark_node;
10595 else
10596 statement = cp_parser_cilk_for (parser, integer_zero_node, if_p);
10597 break;
10599 case RID_BREAK:
10600 case RID_CONTINUE:
10601 case RID_RETURN:
10602 case RID_GOTO:
10603 statement = cp_parser_jump_statement (parser);
10604 break;
10606 case RID_CILK_SYNC:
10607 cp_lexer_consume_token (parser->lexer);
10608 if (flag_cilkplus)
10610 tree sync_expr = build_cilk_sync ();
10611 SET_EXPR_LOCATION (sync_expr,
10612 token->location);
10613 statement = finish_expr_stmt (sync_expr);
10615 else
10617 error_at (token->location, "-fcilkplus must be enabled to use"
10618 " %<_Cilk_sync%>");
10619 statement = error_mark_node;
10621 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10622 break;
10624 /* Objective-C++ exception-handling constructs. */
10625 case RID_AT_TRY:
10626 case RID_AT_CATCH:
10627 case RID_AT_FINALLY:
10628 case RID_AT_SYNCHRONIZED:
10629 case RID_AT_THROW:
10630 statement = cp_parser_objc_statement (parser);
10631 break;
10633 case RID_TRY:
10634 statement = cp_parser_try_block (parser);
10635 break;
10637 case RID_NAMESPACE:
10638 /* This must be a namespace alias definition. */
10639 cp_parser_declaration_statement (parser);
10640 return;
10642 case RID_TRANSACTION_ATOMIC:
10643 case RID_TRANSACTION_RELAXED:
10644 case RID_SYNCHRONIZED:
10645 case RID_ATOMIC_NOEXCEPT:
10646 case RID_ATOMIC_CANCEL:
10647 statement = cp_parser_transaction (parser, token);
10648 break;
10649 case RID_TRANSACTION_CANCEL:
10650 statement = cp_parser_transaction_cancel (parser);
10651 break;
10653 default:
10654 /* It might be a keyword like `int' that can start a
10655 declaration-statement. */
10656 break;
10659 else if (token->type == CPP_NAME)
10661 /* If the next token is a `:', then we are looking at a
10662 labeled-statement. */
10663 token = cp_lexer_peek_nth_token (parser->lexer, 2);
10664 if (token->type == CPP_COLON)
10666 /* Looks like a labeled-statement with an ordinary label.
10667 Parse the label, and then use tail recursion to parse
10668 the statement. */
10670 cp_parser_label_for_labeled_statement (parser, std_attrs);
10671 in_compound = false;
10672 goto restart;
10675 /* Anything that starts with a `{' must be a compound-statement. */
10676 else if (token->type == CPP_OPEN_BRACE)
10677 statement = cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
10678 /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
10679 a statement all its own. */
10680 else if (token->type == CPP_PRAGMA)
10682 /* Only certain OpenMP pragmas are attached to statements, and thus
10683 are considered statements themselves. All others are not. In
10684 the context of a compound, accept the pragma as a "statement" and
10685 return so that we can check for a close brace. Otherwise we
10686 require a real statement and must go back and read one. */
10687 if (in_compound)
10688 cp_parser_pragma (parser, pragma_compound, if_p);
10689 else if (!cp_parser_pragma (parser, pragma_stmt, if_p))
10690 goto restart;
10691 return;
10693 else if (token->type == CPP_EOF)
10695 cp_parser_error (parser, "expected statement");
10696 return;
10699 /* Everything else must be a declaration-statement or an
10700 expression-statement. Try for the declaration-statement
10701 first, unless we are looking at a `;', in which case we know that
10702 we have an expression-statement. */
10703 if (!statement)
10705 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
10707 if (std_attrs != NULL_TREE)
10709 /* Attributes should be parsed as part of the the
10710 declaration, so let's un-parse them. */
10711 saved_tokens.rollback();
10712 std_attrs = NULL_TREE;
10715 cp_parser_parse_tentatively (parser);
10716 /* Try to parse the declaration-statement. */
10717 cp_parser_declaration_statement (parser);
10718 /* If that worked, we're done. */
10719 if (cp_parser_parse_definitely (parser))
10720 return;
10722 /* Look for an expression-statement instead. */
10723 statement = cp_parser_expression_statement (parser, in_statement_expr);
10725 /* Handle [[fallthrough]];. */
10726 if (attribute_fallthrough_p (std_attrs))
10728 /* The next token after the fallthrough attribute is ';'. */
10729 if (statement == NULL_TREE)
10731 /* Turn [[fallthrough]]; into FALLTHROUGH ();. */
10732 statement = build_call_expr_internal_loc (statement_location,
10733 IFN_FALLTHROUGH,
10734 void_type_node, 0);
10735 finish_expr_stmt (statement);
10737 else
10738 warning_at (statement_location, OPT_Wattributes,
10739 "%<fallthrough%> attribute not followed by %<;%>");
10740 std_attrs = NULL_TREE;
10744 /* Set the line number for the statement. */
10745 if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
10746 SET_EXPR_LOCATION (statement, statement_location);
10748 /* Allow "[[fallthrough]];", but warn otherwise. */
10749 if (std_attrs != NULL_TREE)
10750 warning_at (attrs_location,
10751 OPT_Wattributes,
10752 "attributes at the beginning of statement are ignored");
10755 /* Parse the label for a labeled-statement, i.e.
10757 identifier :
10758 case constant-expression :
10759 default :
10761 GNU Extension:
10762 case constant-expression ... constant-expression : statement
10764 When a label is parsed without errors, the label is added to the
10765 parse tree by the finish_* functions, so this function doesn't
10766 have to return the label. */
10768 static void
10769 cp_parser_label_for_labeled_statement (cp_parser* parser, tree attributes)
10771 cp_token *token;
10772 tree label = NULL_TREE;
10773 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
10775 /* The next token should be an identifier. */
10776 token = cp_lexer_peek_token (parser->lexer);
10777 if (token->type != CPP_NAME
10778 && token->type != CPP_KEYWORD)
10780 cp_parser_error (parser, "expected labeled-statement");
10781 return;
10784 /* Remember whether this case or a user-defined label is allowed to fall
10785 through to. */
10786 bool fallthrough_p = token->flags & PREV_FALLTHROUGH;
10788 parser->colon_corrects_to_scope_p = false;
10789 switch (token->keyword)
10791 case RID_CASE:
10793 tree expr, expr_hi;
10794 cp_token *ellipsis;
10796 /* Consume the `case' token. */
10797 cp_lexer_consume_token (parser->lexer);
10798 /* Parse the constant-expression. */
10799 expr = cp_parser_constant_expression (parser);
10800 if (check_for_bare_parameter_packs (expr))
10801 expr = error_mark_node;
10803 ellipsis = cp_lexer_peek_token (parser->lexer);
10804 if (ellipsis->type == CPP_ELLIPSIS)
10806 /* Consume the `...' token. */
10807 cp_lexer_consume_token (parser->lexer);
10808 expr_hi = cp_parser_constant_expression (parser);
10809 if (check_for_bare_parameter_packs (expr_hi))
10810 expr_hi = error_mark_node;
10812 /* We don't need to emit warnings here, as the common code
10813 will do this for us. */
10815 else
10816 expr_hi = NULL_TREE;
10818 if (parser->in_switch_statement_p)
10820 tree l = finish_case_label (token->location, expr, expr_hi);
10821 if (l && TREE_CODE (l) == CASE_LABEL_EXPR)
10822 FALLTHROUGH_LABEL_P (CASE_LABEL (l)) = fallthrough_p;
10824 else
10825 error_at (token->location,
10826 "case label %qE not within a switch statement",
10827 expr);
10829 break;
10831 case RID_DEFAULT:
10832 /* Consume the `default' token. */
10833 cp_lexer_consume_token (parser->lexer);
10835 if (parser->in_switch_statement_p)
10837 tree l = finish_case_label (token->location, NULL_TREE, NULL_TREE);
10838 if (l && TREE_CODE (l) == CASE_LABEL_EXPR)
10839 FALLTHROUGH_LABEL_P (CASE_LABEL (l)) = fallthrough_p;
10841 else
10842 error_at (token->location, "case label not within a switch statement");
10843 break;
10845 default:
10846 /* Anything else must be an ordinary label. */
10847 label = finish_label_stmt (cp_parser_identifier (parser));
10848 if (label && TREE_CODE (label) == LABEL_DECL)
10849 FALLTHROUGH_LABEL_P (label) = fallthrough_p;
10850 break;
10853 /* Require the `:' token. */
10854 cp_parser_require (parser, CPP_COLON, RT_COLON);
10856 /* An ordinary label may optionally be followed by attributes.
10857 However, this is only permitted if the attributes are then
10858 followed by a semicolon. This is because, for backward
10859 compatibility, when parsing
10860 lab: __attribute__ ((unused)) int i;
10861 we want the attribute to attach to "i", not "lab". */
10862 if (label != NULL_TREE
10863 && cp_next_tokens_can_be_gnu_attribute_p (parser))
10865 tree attrs;
10866 cp_parser_parse_tentatively (parser);
10867 attrs = cp_parser_gnu_attributes_opt (parser);
10868 if (attrs == NULL_TREE
10869 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
10870 cp_parser_abort_tentative_parse (parser);
10871 else if (!cp_parser_parse_definitely (parser))
10873 else
10874 attributes = chainon (attributes, attrs);
10877 if (attributes != NULL_TREE)
10878 cplus_decl_attributes (&label, attributes, 0);
10880 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
10883 /* Parse an expression-statement.
10885 expression-statement:
10886 expression [opt] ;
10888 Returns the new EXPR_STMT -- or NULL_TREE if the expression
10889 statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
10890 indicates whether this expression-statement is part of an
10891 expression statement. */
10893 static tree
10894 cp_parser_expression_statement (cp_parser* parser, tree in_statement_expr)
10896 tree statement = NULL_TREE;
10897 cp_token *token = cp_lexer_peek_token (parser->lexer);
10898 location_t loc = token->location;
10900 /* There might be attribute fallthrough. */
10901 tree attr = cp_parser_gnu_attributes_opt (parser);
10903 /* If the next token is a ';', then there is no expression
10904 statement. */
10905 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
10907 statement = cp_parser_expression (parser);
10908 if (statement == error_mark_node
10909 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
10911 cp_parser_skip_to_end_of_block_or_statement (parser);
10912 return error_mark_node;
10916 /* Handle [[fallthrough]];. */
10917 if (attribute_fallthrough_p (attr))
10919 /* The next token after the fallthrough attribute is ';'. */
10920 if (statement == NULL_TREE)
10921 /* Turn [[fallthrough]]; into FALLTHROUGH ();. */
10922 statement = build_call_expr_internal_loc (loc, IFN_FALLTHROUGH,
10923 void_type_node, 0);
10924 else
10925 warning_at (loc, OPT_Wattributes,
10926 "%<fallthrough%> attribute not followed by %<;%>");
10927 attr = NULL_TREE;
10930 /* Allow "[[fallthrough]];", but warn otherwise. */
10931 if (attr != NULL_TREE)
10932 warning_at (loc, OPT_Wattributes,
10933 "attributes at the beginning of statement are ignored");
10935 /* Give a helpful message for "A<T>::type t;" and the like. */
10936 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
10937 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
10939 if (TREE_CODE (statement) == SCOPE_REF)
10940 error_at (token->location, "need %<typename%> before %qE because "
10941 "%qT is a dependent scope",
10942 statement, TREE_OPERAND (statement, 0));
10943 else if (is_overloaded_fn (statement)
10944 && DECL_CONSTRUCTOR_P (get_first_fn (statement)))
10946 /* A::A a; */
10947 tree fn = get_first_fn (statement);
10948 error_at (token->location,
10949 "%<%T::%D%> names the constructor, not the type",
10950 DECL_CONTEXT (fn), DECL_NAME (fn));
10954 /* Consume the final `;'. */
10955 cp_parser_consume_semicolon_at_end_of_statement (parser);
10957 if (in_statement_expr
10958 && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
10959 /* This is the final expression statement of a statement
10960 expression. */
10961 statement = finish_stmt_expr_expr (statement, in_statement_expr);
10962 else if (statement)
10963 statement = finish_expr_stmt (statement);
10965 return statement;
10968 /* Parse a compound-statement.
10970 compound-statement:
10971 { statement-seq [opt] }
10973 GNU extension:
10975 compound-statement:
10976 { label-declaration-seq [opt] statement-seq [opt] }
10978 label-declaration-seq:
10979 label-declaration
10980 label-declaration-seq label-declaration
10982 Returns a tree representing the statement. */
10984 static tree
10985 cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr,
10986 int bcs_flags, bool function_body)
10988 tree compound_stmt;
10990 /* Consume the `{'. */
10991 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
10992 return error_mark_node;
10993 if (DECL_DECLARED_CONSTEXPR_P (current_function_decl)
10994 && !function_body && cxx_dialect < cxx14)
10995 pedwarn (input_location, OPT_Wpedantic,
10996 "compound-statement in constexpr function");
10997 /* Begin the compound-statement. */
10998 compound_stmt = begin_compound_stmt (bcs_flags);
10999 /* If the next keyword is `__label__' we have a label declaration. */
11000 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
11001 cp_parser_label_declaration (parser);
11002 /* Parse an (optional) statement-seq. */
11003 cp_parser_statement_seq_opt (parser, in_statement_expr);
11004 /* Finish the compound-statement. */
11005 finish_compound_stmt (compound_stmt);
11006 /* Consume the `}'. */
11007 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
11009 return compound_stmt;
11012 /* Parse an (optional) statement-seq.
11014 statement-seq:
11015 statement
11016 statement-seq [opt] statement */
11018 static void
11019 cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr)
11021 /* Scan statements until there aren't any more. */
11022 while (true)
11024 cp_token *token = cp_lexer_peek_token (parser->lexer);
11026 /* If we are looking at a `}', then we have run out of
11027 statements; the same is true if we have reached the end
11028 of file, or have stumbled upon a stray '@end'. */
11029 if (token->type == CPP_CLOSE_BRACE
11030 || token->type == CPP_EOF
11031 || token->type == CPP_PRAGMA_EOL
11032 || (token->type == CPP_KEYWORD && token->keyword == RID_AT_END))
11033 break;
11035 /* If we are in a compound statement and find 'else' then
11036 something went wrong. */
11037 else if (token->type == CPP_KEYWORD && token->keyword == RID_ELSE)
11039 if (parser->in_statement & IN_IF_STMT)
11040 break;
11041 else
11043 token = cp_lexer_consume_token (parser->lexer);
11044 error_at (token->location, "%<else%> without a previous %<if%>");
11048 /* Parse the statement. */
11049 cp_parser_statement (parser, in_statement_expr, true, NULL);
11053 /* Return true if we're looking at (init; cond), false otherwise. */
11055 static bool
11056 cp_parser_init_statement_p (cp_parser *parser)
11058 /* Save tokens so that we can put them back. */
11059 cp_lexer_save_tokens (parser->lexer);
11061 /* Look for ';' that is not nested in () or {}. */
11062 int ret = cp_parser_skip_to_closing_parenthesis_1 (parser,
11063 /*recovering=*/false,
11064 CPP_SEMICOLON,
11065 /*consume_paren=*/false);
11067 /* Roll back the tokens we skipped. */
11068 cp_lexer_rollback_tokens (parser->lexer);
11070 return ret == -1;
11073 /* Parse a selection-statement.
11075 selection-statement:
11076 if ( init-statement [opt] condition ) statement
11077 if ( init-statement [opt] condition ) statement else statement
11078 switch ( init-statement [opt] condition ) statement
11080 Returns the new IF_STMT or SWITCH_STMT.
11082 If IF_P is not NULL, *IF_P is set to indicate whether the statement
11083 is a (possibly labeled) if statement which is not enclosed in
11084 braces and has an else clause. This is used to implement
11085 -Wparentheses.
11087 CHAIN is a vector of if-else-if conditions. This is used to implement
11088 -Wduplicated-cond. */
11090 static tree
11091 cp_parser_selection_statement (cp_parser* parser, bool *if_p,
11092 vec<tree> *chain)
11094 cp_token *token;
11095 enum rid keyword;
11096 token_indent_info guard_tinfo;
11098 if (if_p != NULL)
11099 *if_p = false;
11101 /* Peek at the next token. */
11102 token = cp_parser_require (parser, CPP_KEYWORD, RT_SELECT);
11103 guard_tinfo = get_token_indent_info (token);
11105 /* See what kind of keyword it is. */
11106 keyword = token->keyword;
11107 switch (keyword)
11109 case RID_IF:
11110 case RID_SWITCH:
11112 tree statement;
11113 tree condition;
11115 bool cx = false;
11116 if (keyword == RID_IF
11117 && cp_lexer_next_token_is_keyword (parser->lexer,
11118 RID_CONSTEXPR))
11120 cx = true;
11121 cp_token *tok = cp_lexer_consume_token (parser->lexer);
11122 if (cxx_dialect < cxx1z && !in_system_header_at (tok->location))
11123 pedwarn (tok->location, 0, "%<if constexpr%> only available "
11124 "with -std=c++1z or -std=gnu++1z");
11127 /* Look for the `('. */
11128 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
11130 cp_parser_skip_to_end_of_statement (parser);
11131 return error_mark_node;
11134 /* Begin the selection-statement. */
11135 if (keyword == RID_IF)
11137 statement = begin_if_stmt ();
11138 IF_STMT_CONSTEXPR_P (statement) = cx;
11140 else
11141 statement = begin_switch_stmt ();
11143 /* Parse the optional init-statement. */
11144 if (cp_parser_init_statement_p (parser))
11146 tree decl;
11147 if (cxx_dialect < cxx1z)
11148 pedwarn (cp_lexer_peek_token (parser->lexer)->location, 0,
11149 "init-statement in selection statements only available "
11150 "with -std=c++1z or -std=gnu++1z");
11151 cp_parser_init_statement (parser, &decl);
11154 /* Parse the condition. */
11155 condition = cp_parser_condition (parser);
11156 /* Look for the `)'. */
11157 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
11158 cp_parser_skip_to_closing_parenthesis (parser, true, false,
11159 /*consume_paren=*/true);
11161 if (keyword == RID_IF)
11163 bool nested_if;
11164 unsigned char in_statement;
11166 /* Add the condition. */
11167 condition = finish_if_stmt_cond (condition, statement);
11169 if (warn_duplicated_cond)
11170 warn_duplicated_cond_add_or_warn (token->location, condition,
11171 &chain);
11173 /* Parse the then-clause. */
11174 in_statement = parser->in_statement;
11175 parser->in_statement |= IN_IF_STMT;
11177 /* Outside a template, the non-selected branch of a constexpr
11178 if is a 'discarded statement', i.e. unevaluated. */
11179 bool was_discarded = in_discarded_stmt;
11180 bool discard_then = (cx && !processing_template_decl
11181 && integer_zerop (condition));
11182 if (discard_then)
11184 in_discarded_stmt = true;
11185 ++c_inhibit_evaluation_warnings;
11188 cp_parser_implicitly_scoped_statement (parser, &nested_if,
11189 guard_tinfo);
11191 parser->in_statement = in_statement;
11193 finish_then_clause (statement);
11195 if (discard_then)
11197 THEN_CLAUSE (statement) = NULL_TREE;
11198 in_discarded_stmt = was_discarded;
11199 --c_inhibit_evaluation_warnings;
11202 /* If the next token is `else', parse the else-clause. */
11203 if (cp_lexer_next_token_is_keyword (parser->lexer,
11204 RID_ELSE))
11206 bool discard_else = (cx && !processing_template_decl
11207 && integer_nonzerop (condition));
11208 if (discard_else)
11210 in_discarded_stmt = true;
11211 ++c_inhibit_evaluation_warnings;
11214 guard_tinfo
11215 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
11216 /* Consume the `else' keyword. */
11217 cp_lexer_consume_token (parser->lexer);
11218 if (warn_duplicated_cond)
11220 if (cp_lexer_next_token_is_keyword (parser->lexer,
11221 RID_IF)
11222 && chain == NULL)
11224 /* We've got "if (COND) else if (COND2)". Start
11225 the condition chain and add COND as the first
11226 element. */
11227 chain = new vec<tree> ();
11228 if (!CONSTANT_CLASS_P (condition)
11229 && !TREE_SIDE_EFFECTS (condition))
11231 /* Wrap it in a NOP_EXPR so that we can set the
11232 location of the condition. */
11233 tree e = build1 (NOP_EXPR, TREE_TYPE (condition),
11234 condition);
11235 SET_EXPR_LOCATION (e, token->location);
11236 chain->safe_push (e);
11239 else if (!cp_lexer_next_token_is_keyword (parser->lexer,
11240 RID_IF))
11242 /* This is if-else without subsequent if. Zap the
11243 condition chain; we would have already warned at
11244 this point. */
11245 delete chain;
11246 chain = NULL;
11249 begin_else_clause (statement);
11250 /* Parse the else-clause. */
11251 cp_parser_implicitly_scoped_statement (parser, NULL,
11252 guard_tinfo, chain);
11254 finish_else_clause (statement);
11256 /* If we are currently parsing a then-clause, then
11257 IF_P will not be NULL. We set it to true to
11258 indicate that this if statement has an else clause.
11259 This may trigger the Wparentheses warning below
11260 when we get back up to the parent if statement. */
11261 if (if_p != NULL)
11262 *if_p = true;
11264 if (discard_else)
11266 ELSE_CLAUSE (statement) = NULL_TREE;
11267 in_discarded_stmt = was_discarded;
11268 --c_inhibit_evaluation_warnings;
11271 else
11273 /* This if statement does not have an else clause. If
11274 NESTED_IF is true, then the then-clause has an if
11275 statement which does have an else clause. We warn
11276 about the potential ambiguity. */
11277 if (nested_if)
11278 warning_at (EXPR_LOCATION (statement), OPT_Wdangling_else,
11279 "suggest explicit braces to avoid ambiguous"
11280 " %<else%>");
11281 if (warn_duplicated_cond)
11283 /* We don't need the condition chain anymore. */
11284 delete chain;
11285 chain = NULL;
11289 /* Now we're all done with the if-statement. */
11290 finish_if_stmt (statement);
11292 else
11294 bool in_switch_statement_p;
11295 unsigned char in_statement;
11297 /* Add the condition. */
11298 finish_switch_cond (condition, statement);
11300 /* Parse the body of the switch-statement. */
11301 in_switch_statement_p = parser->in_switch_statement_p;
11302 in_statement = parser->in_statement;
11303 parser->in_switch_statement_p = true;
11304 parser->in_statement |= IN_SWITCH_STMT;
11305 cp_parser_implicitly_scoped_statement (parser, if_p,
11306 guard_tinfo);
11307 parser->in_switch_statement_p = in_switch_statement_p;
11308 parser->in_statement = in_statement;
11310 /* Now we're all done with the switch-statement. */
11311 finish_switch_stmt (statement);
11314 return statement;
11316 break;
11318 default:
11319 cp_parser_error (parser, "expected selection-statement");
11320 return error_mark_node;
11324 /* Parse a condition.
11326 condition:
11327 expression
11328 type-specifier-seq declarator = initializer-clause
11329 type-specifier-seq declarator braced-init-list
11331 GNU Extension:
11333 condition:
11334 type-specifier-seq declarator asm-specification [opt]
11335 attributes [opt] = assignment-expression
11337 Returns the expression that should be tested. */
11339 static tree
11340 cp_parser_condition (cp_parser* parser)
11342 cp_decl_specifier_seq type_specifiers;
11343 const char *saved_message;
11344 int declares_class_or_enum;
11346 /* Try the declaration first. */
11347 cp_parser_parse_tentatively (parser);
11348 /* New types are not allowed in the type-specifier-seq for a
11349 condition. */
11350 saved_message = parser->type_definition_forbidden_message;
11351 parser->type_definition_forbidden_message
11352 = G_("types may not be defined in conditions");
11353 /* Parse the type-specifier-seq. */
11354 cp_parser_decl_specifier_seq (parser,
11355 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR,
11356 &type_specifiers,
11357 &declares_class_or_enum);
11358 /* Restore the saved message. */
11359 parser->type_definition_forbidden_message = saved_message;
11360 /* If all is well, we might be looking at a declaration. */
11361 if (!cp_parser_error_occurred (parser))
11363 tree decl;
11364 tree asm_specification;
11365 tree attributes;
11366 cp_declarator *declarator;
11367 tree initializer = NULL_TREE;
11369 /* Parse the declarator. */
11370 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
11371 /*ctor_dtor_or_conv_p=*/NULL,
11372 /*parenthesized_p=*/NULL,
11373 /*member_p=*/false,
11374 /*friend_p=*/false);
11375 /* Parse the attributes. */
11376 attributes = cp_parser_attributes_opt (parser);
11377 /* Parse the asm-specification. */
11378 asm_specification = cp_parser_asm_specification_opt (parser);
11379 /* If the next token is not an `=' or '{', then we might still be
11380 looking at an expression. For example:
11382 if (A(a).x)
11384 looks like a decl-specifier-seq and a declarator -- but then
11385 there is no `=', so this is an expression. */
11386 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
11387 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
11388 cp_parser_simulate_error (parser);
11390 /* If we did see an `=' or '{', then we are looking at a declaration
11391 for sure. */
11392 if (cp_parser_parse_definitely (parser))
11394 tree pushed_scope;
11395 bool non_constant_p;
11396 int flags = LOOKUP_ONLYCONVERTING;
11398 /* Create the declaration. */
11399 decl = start_decl (declarator, &type_specifiers,
11400 /*initialized_p=*/true,
11401 attributes, /*prefix_attributes=*/NULL_TREE,
11402 &pushed_scope);
11404 /* Parse the initializer. */
11405 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
11407 initializer = cp_parser_braced_list (parser, &non_constant_p);
11408 CONSTRUCTOR_IS_DIRECT_INIT (initializer) = 1;
11409 flags = 0;
11411 else
11413 /* Consume the `='. */
11414 cp_parser_require (parser, CPP_EQ, RT_EQ);
11415 initializer = cp_parser_initializer_clause (parser, &non_constant_p);
11417 if (BRACE_ENCLOSED_INITIALIZER_P (initializer))
11418 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
11420 /* Process the initializer. */
11421 cp_finish_decl (decl,
11422 initializer, !non_constant_p,
11423 asm_specification,
11424 flags);
11426 if (pushed_scope)
11427 pop_scope (pushed_scope);
11429 return convert_from_reference (decl);
11432 /* If we didn't even get past the declarator successfully, we are
11433 definitely not looking at a declaration. */
11434 else
11435 cp_parser_abort_tentative_parse (parser);
11437 /* Otherwise, we are looking at an expression. */
11438 return cp_parser_expression (parser);
11441 /* Parses a for-statement or range-for-statement until the closing ')',
11442 not included. */
11444 static tree
11445 cp_parser_for (cp_parser *parser, bool ivdep)
11447 tree init, scope, decl;
11448 bool is_range_for;
11450 /* Begin the for-statement. */
11451 scope = begin_for_scope (&init);
11453 /* Parse the initialization. */
11454 is_range_for = cp_parser_init_statement (parser, &decl);
11456 if (is_range_for)
11457 return cp_parser_range_for (parser, scope, init, decl, ivdep);
11458 else
11459 return cp_parser_c_for (parser, scope, init, ivdep);
11462 static tree
11463 cp_parser_c_for (cp_parser *parser, tree scope, tree init, bool ivdep)
11465 /* Normal for loop */
11466 tree condition = NULL_TREE;
11467 tree expression = NULL_TREE;
11468 tree stmt;
11470 stmt = begin_for_stmt (scope, init);
11471 /* The init-statement has already been parsed in
11472 cp_parser_init_statement, so no work is needed here. */
11473 finish_init_stmt (stmt);
11475 /* If there's a condition, process it. */
11476 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
11477 condition = cp_parser_condition (parser);
11478 else if (ivdep)
11480 cp_parser_error (parser, "missing loop condition in loop with "
11481 "%<GCC ivdep%> pragma");
11482 condition = error_mark_node;
11484 finish_for_cond (condition, stmt, ivdep);
11485 /* Look for the `;'. */
11486 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
11488 /* If there's an expression, process it. */
11489 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
11490 expression = cp_parser_expression (parser);
11491 finish_for_expr (expression, stmt);
11493 return stmt;
11496 /* Tries to parse a range-based for-statement:
11498 range-based-for:
11499 decl-specifier-seq declarator : expression
11501 The decl-specifier-seq declarator and the `:' are already parsed by
11502 cp_parser_init_statement. If processing_template_decl it returns a
11503 newly created RANGE_FOR_STMT; if not, it is converted to a
11504 regular FOR_STMT. */
11506 static tree
11507 cp_parser_range_for (cp_parser *parser, tree scope, tree init, tree range_decl,
11508 bool ivdep)
11510 tree stmt, range_expr;
11511 auto_vec <cxx_binding *, 16> bindings;
11512 auto_vec <tree, 16> names;
11513 tree decomp_first_name = NULL_TREE;
11514 unsigned int decomp_cnt = 0;
11516 /* Get the range declaration momentarily out of the way so that
11517 the range expression doesn't clash with it. */
11518 if (range_decl != error_mark_node)
11520 if (DECL_HAS_VALUE_EXPR_P (range_decl))
11522 tree v = DECL_VALUE_EXPR (range_decl);
11523 /* For decomposition declaration get all of the corresponding
11524 declarations out of the way. */
11525 if (TREE_CODE (v) == ARRAY_REF
11526 && VAR_P (TREE_OPERAND (v, 0))
11527 && DECL_DECOMPOSITION_P (TREE_OPERAND (v, 0)))
11529 tree d = range_decl;
11530 range_decl = TREE_OPERAND (v, 0);
11531 decomp_cnt = tree_to_uhwi (TREE_OPERAND (v, 1)) + 1;
11532 decomp_first_name = d;
11533 for (unsigned int i = 0; i < decomp_cnt; i++, d = DECL_CHAIN (d))
11535 tree name = DECL_NAME (d);
11536 names.safe_push (name);
11537 bindings.safe_push (IDENTIFIER_BINDING (name));
11538 IDENTIFIER_BINDING (name)
11539 = IDENTIFIER_BINDING (name)->previous;
11543 if (names.is_empty ())
11545 tree name = DECL_NAME (range_decl);
11546 names.safe_push (name);
11547 bindings.safe_push (IDENTIFIER_BINDING (name));
11548 IDENTIFIER_BINDING (name) = IDENTIFIER_BINDING (name)->previous;
11552 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
11554 bool expr_non_constant_p;
11555 range_expr = cp_parser_braced_list (parser, &expr_non_constant_p);
11557 else
11558 range_expr = cp_parser_expression (parser);
11560 /* Put the range declaration(s) back into scope. */
11561 for (unsigned int i = 0; i < names.length (); i++)
11563 cxx_binding *binding = bindings[i];
11564 binding->previous = IDENTIFIER_BINDING (names[i]);
11565 IDENTIFIER_BINDING (names[i]) = binding;
11568 /* If in template, STMT is converted to a normal for-statement
11569 at instantiation. If not, it is done just ahead. */
11570 if (processing_template_decl)
11572 if (check_for_bare_parameter_packs (range_expr))
11573 range_expr = error_mark_node;
11574 stmt = begin_range_for_stmt (scope, init);
11575 if (ivdep)
11576 RANGE_FOR_IVDEP (stmt) = 1;
11577 finish_range_for_decl (stmt, range_decl, range_expr);
11578 if (!type_dependent_expression_p (range_expr)
11579 /* do_auto_deduction doesn't mess with template init-lists. */
11580 && !BRACE_ENCLOSED_INITIALIZER_P (range_expr))
11581 do_range_for_auto_deduction (range_decl, range_expr);
11583 else
11585 stmt = begin_for_stmt (scope, init);
11586 stmt = cp_convert_range_for (stmt, range_decl, range_expr,
11587 decomp_first_name, decomp_cnt, ivdep);
11589 return stmt;
11592 /* Subroutine of cp_convert_range_for: given the initializer expression,
11593 builds up the range temporary. */
11595 static tree
11596 build_range_temp (tree range_expr)
11598 tree range_type, range_temp;
11600 /* Find out the type deduced by the declaration
11601 `auto &&__range = range_expr'. */
11602 range_type = cp_build_reference_type (make_auto (), true);
11603 range_type = do_auto_deduction (range_type, range_expr,
11604 type_uses_auto (range_type));
11606 /* Create the __range variable. */
11607 range_temp = build_decl (input_location, VAR_DECL,
11608 get_identifier ("__for_range"), range_type);
11609 TREE_USED (range_temp) = 1;
11610 DECL_ARTIFICIAL (range_temp) = 1;
11612 return range_temp;
11615 /* Used by cp_parser_range_for in template context: we aren't going to
11616 do a full conversion yet, but we still need to resolve auto in the
11617 type of the for-range-declaration if present. This is basically
11618 a shortcut version of cp_convert_range_for. */
11620 static void
11621 do_range_for_auto_deduction (tree decl, tree range_expr)
11623 tree auto_node = type_uses_auto (TREE_TYPE (decl));
11624 if (auto_node)
11626 tree begin_dummy, end_dummy, range_temp, iter_type, iter_decl;
11627 range_temp = convert_from_reference (build_range_temp (range_expr));
11628 iter_type = (cp_parser_perform_range_for_lookup
11629 (range_temp, &begin_dummy, &end_dummy));
11630 if (iter_type)
11632 iter_decl = build_decl (input_location, VAR_DECL, NULL_TREE,
11633 iter_type);
11634 iter_decl = build_x_indirect_ref (input_location, iter_decl, RO_NULL,
11635 tf_warning_or_error);
11636 TREE_TYPE (decl) = do_auto_deduction (TREE_TYPE (decl),
11637 iter_decl, auto_node);
11642 /* Converts a range-based for-statement into a normal
11643 for-statement, as per the definition.
11645 for (RANGE_DECL : RANGE_EXPR)
11646 BLOCK
11648 should be equivalent to:
11651 auto &&__range = RANGE_EXPR;
11652 for (auto __begin = BEGIN_EXPR, end = END_EXPR;
11653 __begin != __end;
11654 ++__begin)
11656 RANGE_DECL = *__begin;
11657 BLOCK
11661 If RANGE_EXPR is an array:
11662 BEGIN_EXPR = __range
11663 END_EXPR = __range + ARRAY_SIZE(__range)
11664 Else if RANGE_EXPR has a member 'begin' or 'end':
11665 BEGIN_EXPR = __range.begin()
11666 END_EXPR = __range.end()
11667 Else:
11668 BEGIN_EXPR = begin(__range)
11669 END_EXPR = end(__range);
11671 If __range has a member 'begin' but not 'end', or vice versa, we must
11672 still use the second alternative (it will surely fail, however).
11673 When calling begin()/end() in the third alternative we must use
11674 argument dependent lookup, but always considering 'std' as an associated
11675 namespace. */
11677 tree
11678 cp_convert_range_for (tree statement, tree range_decl, tree range_expr,
11679 tree decomp_first_name, unsigned int decomp_cnt,
11680 bool ivdep)
11682 tree begin, end;
11683 tree iter_type, begin_expr, end_expr;
11684 tree condition, expression;
11686 if (range_decl == error_mark_node || range_expr == error_mark_node)
11687 /* If an error happened previously do nothing or else a lot of
11688 unhelpful errors would be issued. */
11689 begin_expr = end_expr = iter_type = error_mark_node;
11690 else
11692 tree range_temp;
11694 if (VAR_P (range_expr)
11695 && array_of_runtime_bound_p (TREE_TYPE (range_expr)))
11696 /* Can't bind a reference to an array of runtime bound. */
11697 range_temp = range_expr;
11698 else
11700 range_temp = build_range_temp (range_expr);
11701 pushdecl (range_temp);
11702 cp_finish_decl (range_temp, range_expr,
11703 /*is_constant_init*/false, NULL_TREE,
11704 LOOKUP_ONLYCONVERTING);
11705 range_temp = convert_from_reference (range_temp);
11707 iter_type = cp_parser_perform_range_for_lookup (range_temp,
11708 &begin_expr, &end_expr);
11711 /* The new for initialization statement. */
11712 begin = build_decl (input_location, VAR_DECL,
11713 get_identifier ("__for_begin"), iter_type);
11714 TREE_USED (begin) = 1;
11715 DECL_ARTIFICIAL (begin) = 1;
11716 pushdecl (begin);
11717 cp_finish_decl (begin, begin_expr,
11718 /*is_constant_init*/false, NULL_TREE,
11719 LOOKUP_ONLYCONVERTING);
11721 if (cxx_dialect >= cxx1z)
11722 iter_type = cv_unqualified (TREE_TYPE (end_expr));
11723 end = build_decl (input_location, VAR_DECL,
11724 get_identifier ("__for_end"), iter_type);
11725 TREE_USED (end) = 1;
11726 DECL_ARTIFICIAL (end) = 1;
11727 pushdecl (end);
11728 cp_finish_decl (end, end_expr,
11729 /*is_constant_init*/false, NULL_TREE,
11730 LOOKUP_ONLYCONVERTING);
11732 finish_init_stmt (statement);
11734 /* The new for condition. */
11735 condition = build_x_binary_op (input_location, NE_EXPR,
11736 begin, ERROR_MARK,
11737 end, ERROR_MARK,
11738 NULL, tf_warning_or_error);
11739 finish_for_cond (condition, statement, ivdep);
11741 /* The new increment expression. */
11742 expression = finish_unary_op_expr (input_location,
11743 PREINCREMENT_EXPR, begin,
11744 tf_warning_or_error);
11745 finish_for_expr (expression, statement);
11747 /* The declaration is initialized with *__begin inside the loop body. */
11748 cp_finish_decl (range_decl,
11749 build_x_indirect_ref (input_location, begin, RO_NULL,
11750 tf_warning_or_error),
11751 /*is_constant_init*/false, NULL_TREE,
11752 LOOKUP_ONLYCONVERTING);
11753 if (VAR_P (range_decl) && DECL_DECOMPOSITION_P (range_decl))
11754 cp_finish_decomp (range_decl, decomp_first_name, decomp_cnt);
11756 return statement;
11759 /* Solves BEGIN_EXPR and END_EXPR as described in cp_convert_range_for.
11760 We need to solve both at the same time because the method used
11761 depends on the existence of members begin or end.
11762 Returns the type deduced for the iterator expression. */
11764 static tree
11765 cp_parser_perform_range_for_lookup (tree range, tree *begin, tree *end)
11767 if (error_operand_p (range))
11769 *begin = *end = error_mark_node;
11770 return error_mark_node;
11773 if (!COMPLETE_TYPE_P (complete_type (TREE_TYPE (range))))
11775 error ("range-based %<for%> expression of type %qT "
11776 "has incomplete type", TREE_TYPE (range));
11777 *begin = *end = error_mark_node;
11778 return error_mark_node;
11780 if (TREE_CODE (TREE_TYPE (range)) == ARRAY_TYPE)
11782 /* If RANGE is an array, we will use pointer arithmetic. */
11783 *begin = decay_conversion (range, tf_warning_or_error);
11784 *end = build_binary_op (input_location, PLUS_EXPR,
11785 range,
11786 array_type_nelts_top (TREE_TYPE (range)),
11788 return TREE_TYPE (*begin);
11790 else
11792 /* If it is not an array, we must do a bit of magic. */
11793 tree id_begin, id_end;
11794 tree member_begin, member_end;
11796 *begin = *end = error_mark_node;
11798 id_begin = get_identifier ("begin");
11799 id_end = get_identifier ("end");
11800 member_begin = lookup_member (TREE_TYPE (range), id_begin,
11801 /*protect=*/2, /*want_type=*/false,
11802 tf_warning_or_error);
11803 member_end = lookup_member (TREE_TYPE (range), id_end,
11804 /*protect=*/2, /*want_type=*/false,
11805 tf_warning_or_error);
11807 if (member_begin != NULL_TREE || member_end != NULL_TREE)
11809 /* Use the member functions. */
11810 if (member_begin != NULL_TREE)
11811 *begin = cp_parser_range_for_member_function (range, id_begin);
11812 else
11813 error ("range-based %<for%> expression of type %qT has an "
11814 "%<end%> member but not a %<begin%>", TREE_TYPE (range));
11816 if (member_end != NULL_TREE)
11817 *end = cp_parser_range_for_member_function (range, id_end);
11818 else
11819 error ("range-based %<for%> expression of type %qT has a "
11820 "%<begin%> member but not an %<end%>", TREE_TYPE (range));
11822 else
11824 /* Use global functions with ADL. */
11825 vec<tree, va_gc> *vec;
11826 vec = make_tree_vector ();
11828 vec_safe_push (vec, range);
11830 member_begin = perform_koenig_lookup (id_begin, vec,
11831 tf_warning_or_error);
11832 *begin = finish_call_expr (member_begin, &vec, false, true,
11833 tf_warning_or_error);
11834 member_end = perform_koenig_lookup (id_end, vec,
11835 tf_warning_or_error);
11836 *end = finish_call_expr (member_end, &vec, false, true,
11837 tf_warning_or_error);
11839 release_tree_vector (vec);
11842 /* Last common checks. */
11843 if (*begin == error_mark_node || *end == error_mark_node)
11845 /* If one of the expressions is an error do no more checks. */
11846 *begin = *end = error_mark_node;
11847 return error_mark_node;
11849 else if (type_dependent_expression_p (*begin)
11850 || type_dependent_expression_p (*end))
11851 /* Can happen, when, eg, in a template context, Koenig lookup
11852 can't resolve begin/end (c++/58503). */
11853 return NULL_TREE;
11854 else
11856 tree iter_type = cv_unqualified (TREE_TYPE (*begin));
11857 /* The unqualified type of the __begin and __end temporaries should
11858 be the same, as required by the multiple auto declaration. */
11859 if (!same_type_p (iter_type, cv_unqualified (TREE_TYPE (*end))))
11861 if (cxx_dialect >= cxx1z
11862 && (build_x_binary_op (input_location, NE_EXPR,
11863 *begin, ERROR_MARK,
11864 *end, ERROR_MARK,
11865 NULL, tf_none)
11866 != error_mark_node))
11867 /* P0184R0 allows __begin and __end to have different types,
11868 but make sure they are comparable so we can give a better
11869 diagnostic. */;
11870 else
11871 error ("inconsistent begin/end types in range-based %<for%> "
11872 "statement: %qT and %qT",
11873 TREE_TYPE (*begin), TREE_TYPE (*end));
11875 return iter_type;
11880 /* Helper function for cp_parser_perform_range_for_lookup.
11881 Builds a tree for RANGE.IDENTIFIER(). */
11883 static tree
11884 cp_parser_range_for_member_function (tree range, tree identifier)
11886 tree member, res;
11887 vec<tree, va_gc> *vec;
11889 member = finish_class_member_access_expr (range, identifier,
11890 false, tf_warning_or_error);
11891 if (member == error_mark_node)
11892 return error_mark_node;
11894 vec = make_tree_vector ();
11895 res = finish_call_expr (member, &vec,
11896 /*disallow_virtual=*/false,
11897 /*koenig_p=*/false,
11898 tf_warning_or_error);
11899 release_tree_vector (vec);
11900 return res;
11903 /* Parse an iteration-statement.
11905 iteration-statement:
11906 while ( condition ) statement
11907 do statement while ( expression ) ;
11908 for ( init-statement condition [opt] ; expression [opt] )
11909 statement
11911 Returns the new WHILE_STMT, DO_STMT, FOR_STMT or RANGE_FOR_STMT. */
11913 static tree
11914 cp_parser_iteration_statement (cp_parser* parser, bool *if_p, bool ivdep)
11916 cp_token *token;
11917 enum rid keyword;
11918 tree statement;
11919 unsigned char in_statement;
11920 token_indent_info guard_tinfo;
11922 /* Peek at the next token. */
11923 token = cp_parser_require (parser, CPP_KEYWORD, RT_INTERATION);
11924 if (!token)
11925 return error_mark_node;
11927 guard_tinfo = get_token_indent_info (token);
11929 /* Remember whether or not we are already within an iteration
11930 statement. */
11931 in_statement = parser->in_statement;
11933 /* See what kind of keyword it is. */
11934 keyword = token->keyword;
11935 switch (keyword)
11937 case RID_WHILE:
11939 tree condition;
11941 /* Begin the while-statement. */
11942 statement = begin_while_stmt ();
11943 /* Look for the `('. */
11944 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
11945 /* Parse the condition. */
11946 condition = cp_parser_condition (parser);
11947 finish_while_stmt_cond (condition, statement, ivdep);
11948 /* Look for the `)'. */
11949 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
11950 /* Parse the dependent statement. */
11951 parser->in_statement = IN_ITERATION_STMT;
11952 cp_parser_already_scoped_statement (parser, if_p, guard_tinfo);
11953 parser->in_statement = in_statement;
11954 /* We're done with the while-statement. */
11955 finish_while_stmt (statement);
11957 break;
11959 case RID_DO:
11961 tree expression;
11963 /* Begin the do-statement. */
11964 statement = begin_do_stmt ();
11965 /* Parse the body of the do-statement. */
11966 parser->in_statement = IN_ITERATION_STMT;
11967 cp_parser_implicitly_scoped_statement (parser, NULL, guard_tinfo);
11968 parser->in_statement = in_statement;
11969 finish_do_body (statement);
11970 /* Look for the `while' keyword. */
11971 cp_parser_require_keyword (parser, RID_WHILE, RT_WHILE);
11972 /* Look for the `('. */
11973 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
11974 /* Parse the expression. */
11975 expression = cp_parser_expression (parser);
11976 /* We're done with the do-statement. */
11977 finish_do_stmt (expression, statement, ivdep);
11978 /* Look for the `)'. */
11979 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
11980 /* Look for the `;'. */
11981 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
11983 break;
11985 case RID_FOR:
11987 /* Look for the `('. */
11988 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
11990 statement = cp_parser_for (parser, ivdep);
11992 /* Look for the `)'. */
11993 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
11995 /* Parse the body of the for-statement. */
11996 parser->in_statement = IN_ITERATION_STMT;
11997 cp_parser_already_scoped_statement (parser, if_p, guard_tinfo);
11998 parser->in_statement = in_statement;
12000 /* We're done with the for-statement. */
12001 finish_for_stmt (statement);
12003 break;
12005 default:
12006 cp_parser_error (parser, "expected iteration-statement");
12007 statement = error_mark_node;
12008 break;
12011 return statement;
12014 /* Parse a init-statement or the declarator of a range-based-for.
12015 Returns true if a range-based-for declaration is seen.
12017 init-statement:
12018 expression-statement
12019 simple-declaration */
12021 static bool
12022 cp_parser_init_statement (cp_parser* parser, tree *decl)
12024 /* If the next token is a `;', then we have an empty
12025 expression-statement. Grammatically, this is also a
12026 simple-declaration, but an invalid one, because it does not
12027 declare anything. Therefore, if we did not handle this case
12028 specially, we would issue an error message about an invalid
12029 declaration. */
12030 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
12032 bool is_range_for = false;
12033 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
12035 /* A colon is used in range-based for. */
12036 parser->colon_corrects_to_scope_p = false;
12038 /* We're going to speculatively look for a declaration, falling back
12039 to an expression, if necessary. */
12040 cp_parser_parse_tentatively (parser);
12041 /* Parse the declaration. */
12042 cp_parser_simple_declaration (parser,
12043 /*function_definition_allowed_p=*/false,
12044 decl);
12045 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
12046 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
12048 /* It is a range-for, consume the ':' */
12049 cp_lexer_consume_token (parser->lexer);
12050 is_range_for = true;
12051 if (cxx_dialect < cxx11)
12053 pedwarn (cp_lexer_peek_token (parser->lexer)->location, 0,
12054 "range-based %<for%> loops only available with "
12055 "-std=c++11 or -std=gnu++11");
12056 *decl = error_mark_node;
12059 else
12060 /* The ';' is not consumed yet because we told
12061 cp_parser_simple_declaration not to. */
12062 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12064 if (cp_parser_parse_definitely (parser))
12065 return is_range_for;
12066 /* If the tentative parse failed, then we shall need to look for an
12067 expression-statement. */
12069 /* If we are here, it is an expression-statement. */
12070 cp_parser_expression_statement (parser, NULL_TREE);
12071 return false;
12074 /* Parse a jump-statement.
12076 jump-statement:
12077 break ;
12078 continue ;
12079 return expression [opt] ;
12080 return braced-init-list ;
12081 goto identifier ;
12083 GNU extension:
12085 jump-statement:
12086 goto * expression ;
12088 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR. */
12090 static tree
12091 cp_parser_jump_statement (cp_parser* parser)
12093 tree statement = error_mark_node;
12094 cp_token *token;
12095 enum rid keyword;
12096 unsigned char in_statement;
12098 /* Peek at the next token. */
12099 token = cp_parser_require (parser, CPP_KEYWORD, RT_JUMP);
12100 if (!token)
12101 return error_mark_node;
12103 /* See what kind of keyword it is. */
12104 keyword = token->keyword;
12105 switch (keyword)
12107 case RID_BREAK:
12108 in_statement = parser->in_statement & ~IN_IF_STMT;
12109 switch (in_statement)
12111 case 0:
12112 error_at (token->location, "break statement not within loop or switch");
12113 break;
12114 default:
12115 gcc_assert ((in_statement & IN_SWITCH_STMT)
12116 || in_statement == IN_ITERATION_STMT);
12117 statement = finish_break_stmt ();
12118 if (in_statement == IN_ITERATION_STMT)
12119 break_maybe_infinite_loop ();
12120 break;
12121 case IN_OMP_BLOCK:
12122 error_at (token->location, "invalid exit from OpenMP structured block");
12123 break;
12124 case IN_OMP_FOR:
12125 error_at (token->location, "break statement used with OpenMP for loop");
12126 break;
12127 case IN_CILK_SIMD_FOR:
12128 error_at (token->location, "break statement used with Cilk Plus for loop");
12129 break;
12131 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12132 break;
12134 case RID_CONTINUE:
12135 switch (parser->in_statement & ~(IN_SWITCH_STMT | IN_IF_STMT))
12137 case 0:
12138 error_at (token->location, "continue statement not within a loop");
12139 break;
12140 case IN_CILK_SIMD_FOR:
12141 error_at (token->location,
12142 "continue statement within %<#pragma simd%> loop body");
12143 /* Fall through. */
12144 case IN_ITERATION_STMT:
12145 case IN_OMP_FOR:
12146 statement = finish_continue_stmt ();
12147 break;
12148 case IN_OMP_BLOCK:
12149 error_at (token->location, "invalid exit from OpenMP structured block");
12150 break;
12151 default:
12152 gcc_unreachable ();
12154 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12155 break;
12157 case RID_RETURN:
12159 tree expr;
12160 bool expr_non_constant_p;
12162 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12164 cp_lexer_set_source_position (parser->lexer);
12165 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
12166 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
12168 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
12169 expr = cp_parser_expression (parser);
12170 else
12171 /* If the next token is a `;', then there is no
12172 expression. */
12173 expr = NULL_TREE;
12174 /* Build the return-statement. */
12175 if (current_function_auto_return_pattern && in_discarded_stmt)
12176 /* Don't deduce from a discarded return statement. */;
12177 else
12178 statement = finish_return_stmt (expr);
12179 /* Look for the final `;'. */
12180 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12182 break;
12184 case RID_GOTO:
12185 if (parser->in_function_body
12186 && DECL_DECLARED_CONSTEXPR_P (current_function_decl))
12188 error ("%<goto%> in %<constexpr%> function");
12189 cp_function_chain->invalid_constexpr = true;
12192 /* Create the goto-statement. */
12193 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
12195 /* Issue a warning about this use of a GNU extension. */
12196 pedwarn (token->location, OPT_Wpedantic, "ISO C++ forbids computed gotos");
12197 /* Consume the '*' token. */
12198 cp_lexer_consume_token (parser->lexer);
12199 /* Parse the dependent expression. */
12200 finish_goto_stmt (cp_parser_expression (parser));
12202 else
12203 finish_goto_stmt (cp_parser_identifier (parser));
12204 /* Look for the final `;'. */
12205 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12206 break;
12208 default:
12209 cp_parser_error (parser, "expected jump-statement");
12210 break;
12213 return statement;
12216 /* Parse a declaration-statement.
12218 declaration-statement:
12219 block-declaration */
12221 static void
12222 cp_parser_declaration_statement (cp_parser* parser)
12224 void *p;
12226 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
12227 p = obstack_alloc (&declarator_obstack, 0);
12229 /* Parse the block-declaration. */
12230 cp_parser_block_declaration (parser, /*statement_p=*/true);
12232 /* Free any declarators allocated. */
12233 obstack_free (&declarator_obstack, p);
12236 /* Some dependent statements (like `if (cond) statement'), are
12237 implicitly in their own scope. In other words, if the statement is
12238 a single statement (as opposed to a compound-statement), it is
12239 none-the-less treated as if it were enclosed in braces. Any
12240 declarations appearing in the dependent statement are out of scope
12241 after control passes that point. This function parses a statement,
12242 but ensures that is in its own scope, even if it is not a
12243 compound-statement.
12245 If IF_P is not NULL, *IF_P is set to indicate whether the statement
12246 is a (possibly labeled) if statement which is not enclosed in
12247 braces and has an else clause. This is used to implement
12248 -Wparentheses.
12250 CHAIN is a vector of if-else-if conditions. This is used to implement
12251 -Wduplicated-cond.
12253 Returns the new statement. */
12255 static tree
12256 cp_parser_implicitly_scoped_statement (cp_parser* parser, bool *if_p,
12257 const token_indent_info &guard_tinfo,
12258 vec<tree> *chain)
12260 tree statement;
12261 location_t body_loc = cp_lexer_peek_token (parser->lexer)->location;
12262 token_indent_info body_tinfo
12263 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
12265 if (if_p != NULL)
12266 *if_p = false;
12268 /* Mark if () ; with a special NOP_EXPR. */
12269 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
12271 cp_lexer_consume_token (parser->lexer);
12272 statement = add_stmt (build_empty_stmt (body_loc));
12274 if (guard_tinfo.keyword == RID_IF
12275 && !cp_lexer_next_token_is_keyword (parser->lexer, RID_ELSE))
12276 warning_at (body_loc, OPT_Wempty_body,
12277 "suggest braces around empty body in an %<if%> statement");
12278 else if (guard_tinfo.keyword == RID_ELSE)
12279 warning_at (body_loc, OPT_Wempty_body,
12280 "suggest braces around empty body in an %<else%> statement");
12282 /* if a compound is opened, we simply parse the statement directly. */
12283 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12284 statement = cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
12285 /* If the token is not a `{', then we must take special action. */
12286 else
12288 /* Create a compound-statement. */
12289 statement = begin_compound_stmt (0);
12290 /* Parse the dependent-statement. */
12291 cp_parser_statement (parser, NULL_TREE, false, if_p, chain);
12292 /* Finish the dummy compound-statement. */
12293 finish_compound_stmt (statement);
12296 token_indent_info next_tinfo
12297 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
12298 warn_for_misleading_indentation (guard_tinfo, body_tinfo, next_tinfo);
12300 /* Return the statement. */
12301 return statement;
12304 /* For some dependent statements (like `while (cond) statement'), we
12305 have already created a scope. Therefore, even if the dependent
12306 statement is a compound-statement, we do not want to create another
12307 scope. */
12309 static void
12310 cp_parser_already_scoped_statement (cp_parser* parser, bool *if_p,
12311 const token_indent_info &guard_tinfo)
12313 /* If the token is a `{', then we must take special action. */
12314 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
12316 token_indent_info body_tinfo
12317 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
12319 cp_parser_statement (parser, NULL_TREE, false, if_p);
12320 token_indent_info next_tinfo
12321 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
12322 warn_for_misleading_indentation (guard_tinfo, body_tinfo, next_tinfo);
12324 else
12326 /* Avoid calling cp_parser_compound_statement, so that we
12327 don't create a new scope. Do everything else by hand. */
12328 cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
12329 /* If the next keyword is `__label__' we have a label declaration. */
12330 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
12331 cp_parser_label_declaration (parser);
12332 /* Parse an (optional) statement-seq. */
12333 cp_parser_statement_seq_opt (parser, NULL_TREE);
12334 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
12338 /* Declarations [gram.dcl.dcl] */
12340 /* Parse an optional declaration-sequence.
12342 declaration-seq:
12343 declaration
12344 declaration-seq declaration */
12346 static void
12347 cp_parser_declaration_seq_opt (cp_parser* parser)
12349 while (true)
12351 cp_token *token;
12353 token = cp_lexer_peek_token (parser->lexer);
12355 if (token->type == CPP_CLOSE_BRACE
12356 || token->type == CPP_EOF
12357 || token->type == CPP_PRAGMA_EOL)
12358 break;
12360 if (token->type == CPP_SEMICOLON)
12362 /* A declaration consisting of a single semicolon is
12363 invalid. Allow it unless we're being pedantic. */
12364 cp_lexer_consume_token (parser->lexer);
12365 if (!in_system_header_at (input_location))
12366 pedwarn (input_location, OPT_Wpedantic, "extra %<;%>");
12367 continue;
12370 /* If we're entering or exiting a region that's implicitly
12371 extern "C", modify the lang context appropriately. */
12372 if (!parser->implicit_extern_c && token->implicit_extern_c)
12374 push_lang_context (lang_name_c);
12375 parser->implicit_extern_c = true;
12377 else if (parser->implicit_extern_c && !token->implicit_extern_c)
12379 pop_lang_context ();
12380 parser->implicit_extern_c = false;
12383 if (token->type == CPP_PRAGMA)
12385 /* A top-level declaration can consist solely of a #pragma.
12386 A nested declaration cannot, so this is done here and not
12387 in cp_parser_declaration. (A #pragma at block scope is
12388 handled in cp_parser_statement.) */
12389 cp_parser_pragma (parser, pragma_external, NULL);
12390 continue;
12393 /* Parse the declaration itself. */
12394 cp_parser_declaration (parser);
12398 /* Parse a declaration.
12400 declaration:
12401 block-declaration
12402 function-definition
12403 template-declaration
12404 explicit-instantiation
12405 explicit-specialization
12406 linkage-specification
12407 namespace-definition
12409 C++17:
12410 deduction-guide
12412 GNU extension:
12414 declaration:
12415 __extension__ declaration */
12417 static void
12418 cp_parser_declaration (cp_parser* parser)
12420 cp_token token1;
12421 cp_token token2;
12422 int saved_pedantic;
12423 void *p;
12424 tree attributes = NULL_TREE;
12426 /* Check for the `__extension__' keyword. */
12427 if (cp_parser_extension_opt (parser, &saved_pedantic))
12429 /* Parse the qualified declaration. */
12430 cp_parser_declaration (parser);
12431 /* Restore the PEDANTIC flag. */
12432 pedantic = saved_pedantic;
12434 return;
12437 /* Try to figure out what kind of declaration is present. */
12438 token1 = *cp_lexer_peek_token (parser->lexer);
12440 if (token1.type != CPP_EOF)
12441 token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
12442 else
12444 token2.type = CPP_EOF;
12445 token2.keyword = RID_MAX;
12448 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
12449 p = obstack_alloc (&declarator_obstack, 0);
12451 /* If the next token is `extern' and the following token is a string
12452 literal, then we have a linkage specification. */
12453 if (token1.keyword == RID_EXTERN
12454 && cp_parser_is_pure_string_literal (&token2))
12455 cp_parser_linkage_specification (parser);
12456 /* If the next token is `template', then we have either a template
12457 declaration, an explicit instantiation, or an explicit
12458 specialization. */
12459 else if (token1.keyword == RID_TEMPLATE)
12461 /* `template <>' indicates a template specialization. */
12462 if (token2.type == CPP_LESS
12463 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
12464 cp_parser_explicit_specialization (parser);
12465 /* `template <' indicates a template declaration. */
12466 else if (token2.type == CPP_LESS)
12467 cp_parser_template_declaration (parser, /*member_p=*/false);
12468 /* Anything else must be an explicit instantiation. */
12469 else
12470 cp_parser_explicit_instantiation (parser);
12472 /* If the next token is `export', then we have a template
12473 declaration. */
12474 else if (token1.keyword == RID_EXPORT)
12475 cp_parser_template_declaration (parser, /*member_p=*/false);
12476 /* If the next token is `extern', 'static' or 'inline' and the one
12477 after that is `template', we have a GNU extended explicit
12478 instantiation directive. */
12479 else if (cp_parser_allow_gnu_extensions_p (parser)
12480 && (token1.keyword == RID_EXTERN
12481 || token1.keyword == RID_STATIC
12482 || token1.keyword == RID_INLINE)
12483 && token2.keyword == RID_TEMPLATE)
12484 cp_parser_explicit_instantiation (parser);
12485 /* If the next token is `namespace', check for a named or unnamed
12486 namespace definition. */
12487 else if (token1.keyword == RID_NAMESPACE
12488 && (/* A named namespace definition. */
12489 (token2.type == CPP_NAME
12490 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
12491 != CPP_EQ))
12492 || (token2.type == CPP_OPEN_SQUARE
12493 && cp_lexer_peek_nth_token (parser->lexer, 3)->type
12494 == CPP_OPEN_SQUARE)
12495 /* An unnamed namespace definition. */
12496 || token2.type == CPP_OPEN_BRACE
12497 || token2.keyword == RID_ATTRIBUTE))
12498 cp_parser_namespace_definition (parser);
12499 /* An inline (associated) namespace definition. */
12500 else if (token1.keyword == RID_INLINE
12501 && token2.keyword == RID_NAMESPACE)
12502 cp_parser_namespace_definition (parser);
12503 /* Objective-C++ declaration/definition. */
12504 else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1.keyword))
12505 cp_parser_objc_declaration (parser, NULL_TREE);
12506 else if (c_dialect_objc ()
12507 && token1.keyword == RID_ATTRIBUTE
12508 && cp_parser_objc_valid_prefix_attributes (parser, &attributes))
12509 cp_parser_objc_declaration (parser, attributes);
12510 /* At this point we may have a template declared by a concept
12511 introduction. */
12512 else if (flag_concepts
12513 && cp_parser_template_declaration_after_export (parser,
12514 /*member_p=*/false))
12515 /* We did. */;
12516 else
12517 /* Try to parse a block-declaration, or a function-definition. */
12518 cp_parser_block_declaration (parser, /*statement_p=*/false);
12520 /* Free any declarators allocated. */
12521 obstack_free (&declarator_obstack, p);
12524 /* Parse a block-declaration.
12526 block-declaration:
12527 simple-declaration
12528 asm-definition
12529 namespace-alias-definition
12530 using-declaration
12531 using-directive
12533 GNU Extension:
12535 block-declaration:
12536 __extension__ block-declaration
12538 C++0x Extension:
12540 block-declaration:
12541 static_assert-declaration
12543 If STATEMENT_P is TRUE, then this block-declaration is occurring as
12544 part of a declaration-statement. */
12546 static void
12547 cp_parser_block_declaration (cp_parser *parser,
12548 bool statement_p)
12550 cp_token *token1;
12551 int saved_pedantic;
12553 /* Check for the `__extension__' keyword. */
12554 if (cp_parser_extension_opt (parser, &saved_pedantic))
12556 /* Parse the qualified declaration. */
12557 cp_parser_block_declaration (parser, statement_p);
12558 /* Restore the PEDANTIC flag. */
12559 pedantic = saved_pedantic;
12561 return;
12564 /* Peek at the next token to figure out which kind of declaration is
12565 present. */
12566 token1 = cp_lexer_peek_token (parser->lexer);
12568 /* If the next keyword is `asm', we have an asm-definition. */
12569 if (token1->keyword == RID_ASM)
12571 if (statement_p)
12572 cp_parser_commit_to_tentative_parse (parser);
12573 cp_parser_asm_definition (parser);
12575 /* If the next keyword is `namespace', we have a
12576 namespace-alias-definition. */
12577 else if (token1->keyword == RID_NAMESPACE)
12578 cp_parser_namespace_alias_definition (parser);
12579 /* If the next keyword is `using', we have a
12580 using-declaration, a using-directive, or an alias-declaration. */
12581 else if (token1->keyword == RID_USING)
12583 cp_token *token2;
12585 if (statement_p)
12586 cp_parser_commit_to_tentative_parse (parser);
12587 /* If the token after `using' is `namespace', then we have a
12588 using-directive. */
12589 token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
12590 if (token2->keyword == RID_NAMESPACE)
12591 cp_parser_using_directive (parser);
12592 /* If the second token after 'using' is '=', then we have an
12593 alias-declaration. */
12594 else if (cxx_dialect >= cxx11
12595 && token2->type == CPP_NAME
12596 && ((cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_EQ)
12597 || (cp_nth_tokens_can_be_attribute_p (parser, 3))))
12598 cp_parser_alias_declaration (parser);
12599 /* Otherwise, it's a using-declaration. */
12600 else
12601 cp_parser_using_declaration (parser,
12602 /*access_declaration_p=*/false);
12604 /* If the next keyword is `__label__' we have a misplaced label
12605 declaration. */
12606 else if (token1->keyword == RID_LABEL)
12608 cp_lexer_consume_token (parser->lexer);
12609 error_at (token1->location, "%<__label__%> not at the beginning of a block");
12610 cp_parser_skip_to_end_of_statement (parser);
12611 /* If the next token is now a `;', consume it. */
12612 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
12613 cp_lexer_consume_token (parser->lexer);
12615 /* If the next token is `static_assert' we have a static assertion. */
12616 else if (token1->keyword == RID_STATIC_ASSERT)
12617 cp_parser_static_assert (parser, /*member_p=*/false);
12618 /* Anything else must be a simple-declaration. */
12619 else
12620 cp_parser_simple_declaration (parser, !statement_p,
12621 /*maybe_range_for_decl*/NULL);
12624 /* Parse a simple-declaration.
12626 simple-declaration:
12627 decl-specifier-seq [opt] init-declarator-list [opt] ;
12628 decl-specifier-seq ref-qualifier [opt] [ identifier-list ]
12629 brace-or-equal-initializer ;
12631 init-declarator-list:
12632 init-declarator
12633 init-declarator-list , init-declarator
12635 If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
12636 function-definition as a simple-declaration.
12638 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
12639 parsed declaration if it is an uninitialized single declarator not followed
12640 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
12641 if present, will not be consumed. */
12643 static void
12644 cp_parser_simple_declaration (cp_parser* parser,
12645 bool function_definition_allowed_p,
12646 tree *maybe_range_for_decl)
12648 cp_decl_specifier_seq decl_specifiers;
12649 int declares_class_or_enum;
12650 bool saw_declarator;
12651 location_t comma_loc = UNKNOWN_LOCATION;
12652 location_t init_loc = UNKNOWN_LOCATION;
12654 if (maybe_range_for_decl)
12655 *maybe_range_for_decl = NULL_TREE;
12657 /* Defer access checks until we know what is being declared; the
12658 checks for names appearing in the decl-specifier-seq should be
12659 done as if we were in the scope of the thing being declared. */
12660 push_deferring_access_checks (dk_deferred);
12662 /* Parse the decl-specifier-seq. We have to keep track of whether
12663 or not the decl-specifier-seq declares a named class or
12664 enumeration type, since that is the only case in which the
12665 init-declarator-list is allowed to be empty.
12667 [dcl.dcl]
12669 In a simple-declaration, the optional init-declarator-list can be
12670 omitted only when declaring a class or enumeration, that is when
12671 the decl-specifier-seq contains either a class-specifier, an
12672 elaborated-type-specifier, or an enum-specifier. */
12673 cp_parser_decl_specifier_seq (parser,
12674 CP_PARSER_FLAGS_OPTIONAL,
12675 &decl_specifiers,
12676 &declares_class_or_enum);
12677 /* We no longer need to defer access checks. */
12678 stop_deferring_access_checks ();
12680 /* In a block scope, a valid declaration must always have a
12681 decl-specifier-seq. By not trying to parse declarators, we can
12682 resolve the declaration/expression ambiguity more quickly. */
12683 if (!function_definition_allowed_p
12684 && !decl_specifiers.any_specifiers_p)
12686 cp_parser_error (parser, "expected declaration");
12687 goto done;
12690 /* If the next two tokens are both identifiers, the code is
12691 erroneous. The usual cause of this situation is code like:
12693 T t;
12695 where "T" should name a type -- but does not. */
12696 if (!decl_specifiers.any_type_specifiers_p
12697 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
12699 /* If parsing tentatively, we should commit; we really are
12700 looking at a declaration. */
12701 cp_parser_commit_to_tentative_parse (parser);
12702 /* Give up. */
12703 goto done;
12706 /* If we have seen at least one decl-specifier, and the next token
12707 is not a parenthesis, then we must be looking at a declaration.
12708 (After "int (" we might be looking at a functional cast.) */
12709 if (decl_specifiers.any_specifiers_p
12710 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN)
12711 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
12712 && !cp_parser_error_occurred (parser))
12713 cp_parser_commit_to_tentative_parse (parser);
12715 /* Look for C++17 decomposition declaration. */
12716 for (size_t n = 1; ; n++)
12717 if (cp_lexer_nth_token_is (parser->lexer, n, CPP_AND)
12718 || cp_lexer_nth_token_is (parser->lexer, n, CPP_AND_AND))
12719 continue;
12720 else if (cp_lexer_nth_token_is (parser->lexer, n, CPP_OPEN_SQUARE)
12721 && !cp_lexer_nth_token_is (parser->lexer, n + 1, CPP_OPEN_SQUARE)
12722 && decl_specifiers.any_specifiers_p)
12724 tree decl
12725 = cp_parser_decomposition_declaration (parser, &decl_specifiers,
12726 maybe_range_for_decl,
12727 &init_loc);
12729 /* The next token should be either a `,' or a `;'. */
12730 cp_token *token = cp_lexer_peek_token (parser->lexer);
12731 /* If it's a `;', we are done. */
12732 if (token->type == CPP_SEMICOLON || maybe_range_for_decl)
12733 goto finish;
12734 /* Anything else is an error. */
12735 else
12737 /* If we have already issued an error message we don't need
12738 to issue another one. */
12739 if ((decl != error_mark_node
12740 && DECL_INITIAL (decl) != error_mark_node)
12741 || cp_parser_uncommitted_to_tentative_parse_p (parser))
12742 cp_parser_error (parser, "expected %<,%> or %<;%>");
12743 /* Skip tokens until we reach the end of the statement. */
12744 cp_parser_skip_to_end_of_statement (parser);
12745 /* If the next token is now a `;', consume it. */
12746 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
12747 cp_lexer_consume_token (parser->lexer);
12748 goto done;
12751 else
12752 break;
12754 tree last_type;
12755 bool auto_specifier_p;
12756 /* NULL_TREE if both variable and function declaration are allowed,
12757 error_mark_node if function declaration are not allowed and
12758 a FUNCTION_DECL that should be diagnosed if it is followed by
12759 variable declarations. */
12760 tree auto_function_declaration;
12762 last_type = NULL_TREE;
12763 auto_specifier_p
12764 = decl_specifiers.type && type_uses_auto (decl_specifiers.type);
12765 auto_function_declaration = NULL_TREE;
12767 /* Keep going until we hit the `;' at the end of the simple
12768 declaration. */
12769 saw_declarator = false;
12770 while (cp_lexer_next_token_is_not (parser->lexer,
12771 CPP_SEMICOLON))
12773 cp_token *token;
12774 bool function_definition_p;
12775 tree decl;
12776 tree auto_result = NULL_TREE;
12778 if (saw_declarator)
12780 /* If we are processing next declarator, comma is expected */
12781 token = cp_lexer_peek_token (parser->lexer);
12782 gcc_assert (token->type == CPP_COMMA);
12783 cp_lexer_consume_token (parser->lexer);
12784 if (maybe_range_for_decl)
12786 *maybe_range_for_decl = error_mark_node;
12787 if (comma_loc == UNKNOWN_LOCATION)
12788 comma_loc = token->location;
12791 else
12792 saw_declarator = true;
12794 /* Parse the init-declarator. */
12795 decl = cp_parser_init_declarator (parser, &decl_specifiers,
12796 /*checks=*/NULL,
12797 function_definition_allowed_p,
12798 /*member_p=*/false,
12799 declares_class_or_enum,
12800 &function_definition_p,
12801 maybe_range_for_decl,
12802 &init_loc,
12803 &auto_result);
12804 /* If an error occurred while parsing tentatively, exit quickly.
12805 (That usually happens when in the body of a function; each
12806 statement is treated as a declaration-statement until proven
12807 otherwise.) */
12808 if (cp_parser_error_occurred (parser))
12809 goto done;
12811 if (auto_specifier_p && cxx_dialect >= cxx14)
12813 /* If the init-declarator-list contains more than one
12814 init-declarator, they shall all form declarations of
12815 variables. */
12816 if (auto_function_declaration == NULL_TREE)
12817 auto_function_declaration
12818 = TREE_CODE (decl) == FUNCTION_DECL ? decl : error_mark_node;
12819 else if (TREE_CODE (decl) == FUNCTION_DECL
12820 || auto_function_declaration != error_mark_node)
12822 error_at (decl_specifiers.locations[ds_type_spec],
12823 "non-variable %qD in declaration with more than one "
12824 "declarator with placeholder type",
12825 TREE_CODE (decl) == FUNCTION_DECL
12826 ? decl : auto_function_declaration);
12827 auto_function_declaration = error_mark_node;
12831 if (auto_result
12832 && (!processing_template_decl || !type_uses_auto (auto_result)))
12834 if (last_type
12835 && last_type != error_mark_node
12836 && !same_type_p (auto_result, last_type))
12838 /* If the list of declarators contains more than one declarator,
12839 the type of each declared variable is determined as described
12840 above. If the type deduced for the template parameter U is not
12841 the same in each deduction, the program is ill-formed. */
12842 error_at (decl_specifiers.locations[ds_type_spec],
12843 "inconsistent deduction for %qT: %qT and then %qT",
12844 decl_specifiers.type, last_type, auto_result);
12845 last_type = error_mark_node;
12847 else
12848 last_type = auto_result;
12851 /* Handle function definitions specially. */
12852 if (function_definition_p)
12854 /* If the next token is a `,', then we are probably
12855 processing something like:
12857 void f() {}, *p;
12859 which is erroneous. */
12860 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
12862 cp_token *token = cp_lexer_peek_token (parser->lexer);
12863 error_at (token->location,
12864 "mixing"
12865 " declarations and function-definitions is forbidden");
12867 /* Otherwise, we're done with the list of declarators. */
12868 else
12870 pop_deferring_access_checks ();
12871 return;
12874 if (maybe_range_for_decl && *maybe_range_for_decl == NULL_TREE)
12875 *maybe_range_for_decl = decl;
12876 /* The next token should be either a `,' or a `;'. */
12877 token = cp_lexer_peek_token (parser->lexer);
12878 /* If it's a `,', there are more declarators to come. */
12879 if (token->type == CPP_COMMA)
12880 /* will be consumed next time around */;
12881 /* If it's a `;', we are done. */
12882 else if (token->type == CPP_SEMICOLON)
12883 break;
12884 else if (maybe_range_for_decl)
12886 if (declares_class_or_enum && token->type == CPP_COLON)
12887 permerror (decl_specifiers.locations[ds_type_spec],
12888 "types may not be defined in a for-range-declaration");
12889 break;
12891 /* Anything else is an error. */
12892 else
12894 /* If we have already issued an error message we don't need
12895 to issue another one. */
12896 if ((decl != error_mark_node
12897 && DECL_INITIAL (decl) != error_mark_node)
12898 || cp_parser_uncommitted_to_tentative_parse_p (parser))
12899 cp_parser_error (parser, "expected %<,%> or %<;%>");
12900 /* Skip tokens until we reach the end of the statement. */
12901 cp_parser_skip_to_end_of_statement (parser);
12902 /* If the next token is now a `;', consume it. */
12903 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
12904 cp_lexer_consume_token (parser->lexer);
12905 goto done;
12907 /* After the first time around, a function-definition is not
12908 allowed -- even if it was OK at first. For example:
12910 int i, f() {}
12912 is not valid. */
12913 function_definition_allowed_p = false;
12916 /* Issue an error message if no declarators are present, and the
12917 decl-specifier-seq does not itself declare a class or
12918 enumeration: [dcl.dcl]/3. */
12919 if (!saw_declarator)
12921 if (cp_parser_declares_only_class_p (parser))
12923 if (!declares_class_or_enum
12924 && decl_specifiers.type
12925 && OVERLOAD_TYPE_P (decl_specifiers.type))
12926 /* Ensure an error is issued anyway when finish_decltype_type,
12927 called via cp_parser_decl_specifier_seq, returns a class or
12928 an enumeration (c++/51786). */
12929 decl_specifiers.type = NULL_TREE;
12930 shadow_tag (&decl_specifiers);
12932 /* Perform any deferred access checks. */
12933 perform_deferred_access_checks (tf_warning_or_error);
12936 /* Consume the `;'. */
12937 finish:
12938 if (!maybe_range_for_decl)
12939 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12940 else if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
12942 if (init_loc != UNKNOWN_LOCATION)
12943 error_at (init_loc, "initializer in range-based %<for%> loop");
12944 if (comma_loc != UNKNOWN_LOCATION)
12945 error_at (comma_loc,
12946 "multiple declarations in range-based %<for%> loop");
12949 done:
12950 pop_deferring_access_checks ();
12953 /* Helper of cp_parser_simple_declaration, parse a decomposition declaration.
12954 decl-specifier-seq ref-qualifier [opt] [ identifier-list ]
12955 initializer ; */
12957 static tree
12958 cp_parser_decomposition_declaration (cp_parser *parser,
12959 cp_decl_specifier_seq *decl_specifiers,
12960 tree *maybe_range_for_decl,
12961 location_t *init_loc)
12963 cp_ref_qualifier ref_qual = cp_parser_ref_qualifier_opt (parser);
12964 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
12965 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
12967 /* Parse the identifier-list. */
12968 auto_vec<cp_expr, 10> v;
12969 if (!cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_SQUARE))
12970 while (true)
12972 cp_expr e = cp_parser_identifier (parser);
12973 if (e.get_value () == error_mark_node)
12974 break;
12975 v.safe_push (e);
12976 if (!cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
12977 break;
12978 cp_lexer_consume_token (parser->lexer);
12981 location_t end_loc = cp_lexer_peek_token (parser->lexer)->location;
12982 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
12984 end_loc = UNKNOWN_LOCATION;
12985 cp_parser_skip_to_closing_parenthesis_1 (parser, true, CPP_CLOSE_SQUARE,
12986 false);
12987 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_SQUARE))
12988 cp_lexer_consume_token (parser->lexer);
12989 else
12991 cp_parser_skip_to_end_of_statement (parser);
12992 return error_mark_node;
12996 if (cxx_dialect < cxx1z)
12997 pedwarn (loc, 0, "decomposition declaration only available with "
12998 "-std=c++1z or -std=gnu++1z");
13000 tree pushed_scope;
13001 cp_declarator *declarator = make_declarator (cdk_decomp);
13002 loc = end_loc == UNKNOWN_LOCATION ? loc : make_location (loc, loc, end_loc);
13003 declarator->id_loc = loc;
13004 if (ref_qual != REF_QUAL_NONE)
13005 declarator = make_reference_declarator (TYPE_UNQUALIFIED, declarator,
13006 ref_qual == REF_QUAL_RVALUE,
13007 NULL_TREE);
13008 tree decl = start_decl (declarator, decl_specifiers, SD_INITIALIZED,
13009 NULL_TREE, decl_specifiers->attributes,
13010 &pushed_scope);
13011 tree orig_decl = decl;
13013 unsigned int i;
13014 cp_expr e;
13015 cp_decl_specifier_seq decl_specs;
13016 clear_decl_specs (&decl_specs);
13017 decl_specs.type = make_auto ();
13018 tree prev = decl;
13019 FOR_EACH_VEC_ELT (v, i, e)
13021 if (i == 0)
13022 declarator = make_id_declarator (NULL_TREE, e.get_value (), sfk_none);
13023 else
13024 declarator->u.id.unqualified_name = e.get_value ();
13025 declarator->id_loc = e.get_location ();
13026 tree elt_pushed_scope;
13027 tree decl2 = start_decl (declarator, &decl_specs, SD_INITIALIZED,
13028 NULL_TREE, NULL_TREE, &elt_pushed_scope);
13029 if (decl2 == error_mark_node)
13030 decl = error_mark_node;
13031 else if (decl != error_mark_node && DECL_CHAIN (decl2) != prev)
13033 /* Ensure we've diagnosed redeclaration if we aren't creating
13034 a new VAR_DECL. */
13035 gcc_assert (errorcount);
13036 decl = error_mark_node;
13038 else
13039 prev = decl2;
13040 if (elt_pushed_scope)
13041 pop_scope (elt_pushed_scope);
13044 if (v.is_empty ())
13046 error_at (loc, "empty decomposition declaration");
13047 decl = error_mark_node;
13050 if (maybe_range_for_decl == NULL
13051 || cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
13053 bool non_constant_p = false, is_direct_init = false;
13054 *init_loc = cp_lexer_peek_token (parser->lexer)->location;
13055 tree initializer = cp_parser_initializer (parser, &is_direct_init,
13056 &non_constant_p);
13058 if (decl != error_mark_node)
13060 cp_finish_decl (decl, initializer, non_constant_p, NULL_TREE,
13061 is_direct_init ? LOOKUP_NORMAL : LOOKUP_IMPLICIT);
13062 cp_finish_decomp (decl, prev, v.length ());
13065 else if (decl != error_mark_node)
13067 *maybe_range_for_decl = prev;
13068 /* Ensure DECL_VALUE_EXPR is created for all the decls but
13069 the underlying DECL. */
13070 cp_finish_decomp (decl, prev, v.length ());
13073 if (pushed_scope)
13074 pop_scope (pushed_scope);
13076 if (decl == error_mark_node && DECL_P (orig_decl))
13078 if (DECL_NAMESPACE_SCOPE_P (orig_decl))
13079 SET_DECL_ASSEMBLER_NAME (orig_decl, get_identifier ("<decomp>"));
13082 return decl;
13085 /* Parse a decl-specifier-seq.
13087 decl-specifier-seq:
13088 decl-specifier-seq [opt] decl-specifier
13089 decl-specifier attribute-specifier-seq [opt] (C++11)
13091 decl-specifier:
13092 storage-class-specifier
13093 type-specifier
13094 function-specifier
13095 friend
13096 typedef
13098 GNU Extension:
13100 decl-specifier:
13101 attributes
13103 Concepts Extension:
13105 decl-specifier:
13106 concept
13108 Set *DECL_SPECS to a representation of the decl-specifier-seq.
13110 The parser flags FLAGS is used to control type-specifier parsing.
13112 *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
13113 flags:
13115 1: one of the decl-specifiers is an elaborated-type-specifier
13116 (i.e., a type declaration)
13117 2: one of the decl-specifiers is an enum-specifier or a
13118 class-specifier (i.e., a type definition)
13122 static void
13123 cp_parser_decl_specifier_seq (cp_parser* parser,
13124 cp_parser_flags flags,
13125 cp_decl_specifier_seq *decl_specs,
13126 int* declares_class_or_enum)
13128 bool constructor_possible_p = !parser->in_declarator_p;
13129 bool found_decl_spec = false;
13130 cp_token *start_token = NULL;
13131 cp_decl_spec ds;
13133 /* Clear DECL_SPECS. */
13134 clear_decl_specs (decl_specs);
13136 /* Assume no class or enumeration type is declared. */
13137 *declares_class_or_enum = 0;
13139 /* Keep reading specifiers until there are no more to read. */
13140 while (true)
13142 bool constructor_p;
13143 cp_token *token;
13144 ds = ds_last;
13146 /* Peek at the next token. */
13147 token = cp_lexer_peek_token (parser->lexer);
13149 /* Save the first token of the decl spec list for error
13150 reporting. */
13151 if (!start_token)
13152 start_token = token;
13153 /* Handle attributes. */
13154 if (cp_next_tokens_can_be_attribute_p (parser))
13156 /* Parse the attributes. */
13157 tree attrs = cp_parser_attributes_opt (parser);
13159 /* In a sequence of declaration specifiers, c++11 attributes
13160 appertain to the type that precede them. In that case
13161 [dcl.spec]/1 says:
13163 The attribute-specifier-seq affects the type only for
13164 the declaration it appears in, not other declarations
13165 involving the same type.
13167 But for now let's force the user to position the
13168 attribute either at the beginning of the declaration or
13169 after the declarator-id, which would clearly mean that it
13170 applies to the declarator. */
13171 if (cxx11_attribute_p (attrs))
13173 if (!found_decl_spec)
13174 /* The c++11 attribute is at the beginning of the
13175 declaration. It appertains to the entity being
13176 declared. */;
13177 else
13179 if (decl_specs->type && CLASS_TYPE_P (decl_specs->type))
13181 /* This is an attribute following a
13182 class-specifier. */
13183 if (decl_specs->type_definition_p)
13184 warn_misplaced_attr_for_class_type (token->location,
13185 decl_specs->type);
13186 attrs = NULL_TREE;
13188 else
13190 decl_specs->std_attributes
13191 = chainon (decl_specs->std_attributes,
13192 attrs);
13193 if (decl_specs->locations[ds_std_attribute] == 0)
13194 decl_specs->locations[ds_std_attribute] = token->location;
13196 continue;
13200 decl_specs->attributes
13201 = chainon (decl_specs->attributes,
13202 attrs);
13203 if (decl_specs->locations[ds_attribute] == 0)
13204 decl_specs->locations[ds_attribute] = token->location;
13205 continue;
13207 /* Assume we will find a decl-specifier keyword. */
13208 found_decl_spec = true;
13209 /* If the next token is an appropriate keyword, we can simply
13210 add it to the list. */
13211 switch (token->keyword)
13213 /* decl-specifier:
13214 friend
13215 constexpr */
13216 case RID_FRIEND:
13217 if (!at_class_scope_p ())
13219 error_at (token->location, "%<friend%> used outside of class");
13220 cp_lexer_purge_token (parser->lexer);
13222 else
13224 ds = ds_friend;
13225 /* Consume the token. */
13226 cp_lexer_consume_token (parser->lexer);
13228 break;
13230 case RID_CONSTEXPR:
13231 ds = ds_constexpr;
13232 cp_lexer_consume_token (parser->lexer);
13233 break;
13235 case RID_CONCEPT:
13236 ds = ds_concept;
13237 cp_lexer_consume_token (parser->lexer);
13238 break;
13240 /* function-specifier:
13241 inline
13242 virtual
13243 explicit */
13244 case RID_INLINE:
13245 case RID_VIRTUAL:
13246 case RID_EXPLICIT:
13247 cp_parser_function_specifier_opt (parser, decl_specs);
13248 break;
13250 /* decl-specifier:
13251 typedef */
13252 case RID_TYPEDEF:
13253 ds = ds_typedef;
13254 /* Consume the token. */
13255 cp_lexer_consume_token (parser->lexer);
13256 /* A constructor declarator cannot appear in a typedef. */
13257 constructor_possible_p = false;
13258 /* The "typedef" keyword can only occur in a declaration; we
13259 may as well commit at this point. */
13260 cp_parser_commit_to_tentative_parse (parser);
13262 if (decl_specs->storage_class != sc_none)
13263 decl_specs->conflicting_specifiers_p = true;
13264 break;
13266 /* storage-class-specifier:
13267 auto
13268 register
13269 static
13270 extern
13271 mutable
13273 GNU Extension:
13274 thread */
13275 case RID_AUTO:
13276 if (cxx_dialect == cxx98)
13278 /* Consume the token. */
13279 cp_lexer_consume_token (parser->lexer);
13281 /* Complain about `auto' as a storage specifier, if
13282 we're complaining about C++0x compatibility. */
13283 warning_at (token->location, OPT_Wc__11_compat, "%<auto%>"
13284 " changes meaning in C++11; please remove it");
13286 /* Set the storage class anyway. */
13287 cp_parser_set_storage_class (parser, decl_specs, RID_AUTO,
13288 token);
13290 else
13291 /* C++0x auto type-specifier. */
13292 found_decl_spec = false;
13293 break;
13295 case RID_REGISTER:
13296 case RID_STATIC:
13297 case RID_EXTERN:
13298 case RID_MUTABLE:
13299 /* Consume the token. */
13300 cp_lexer_consume_token (parser->lexer);
13301 cp_parser_set_storage_class (parser, decl_specs, token->keyword,
13302 token);
13303 break;
13304 case RID_THREAD:
13305 /* Consume the token. */
13306 ds = ds_thread;
13307 cp_lexer_consume_token (parser->lexer);
13308 break;
13310 default:
13311 /* We did not yet find a decl-specifier yet. */
13312 found_decl_spec = false;
13313 break;
13316 if (found_decl_spec
13317 && (flags & CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR)
13318 && token->keyword != RID_CONSTEXPR)
13319 error ("decl-specifier invalid in condition");
13321 if (found_decl_spec
13322 && (flags & CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR)
13323 && token->keyword != RID_MUTABLE
13324 && token->keyword != RID_CONSTEXPR)
13325 error_at (token->location, "%qD invalid in lambda",
13326 ridpointers[token->keyword]);
13328 if (ds != ds_last)
13329 set_and_check_decl_spec_loc (decl_specs, ds, token);
13331 /* Constructors are a special case. The `S' in `S()' is not a
13332 decl-specifier; it is the beginning of the declarator. */
13333 constructor_p
13334 = (!found_decl_spec
13335 && constructor_possible_p
13336 && (cp_parser_constructor_declarator_p
13337 (parser, decl_spec_seq_has_spec_p (decl_specs, ds_friend))));
13339 /* If we don't have a DECL_SPEC yet, then we must be looking at
13340 a type-specifier. */
13341 if (!found_decl_spec && !constructor_p)
13343 int decl_spec_declares_class_or_enum;
13344 bool is_cv_qualifier;
13345 tree type_spec;
13347 type_spec
13348 = cp_parser_type_specifier (parser, flags,
13349 decl_specs,
13350 /*is_declaration=*/true,
13351 &decl_spec_declares_class_or_enum,
13352 &is_cv_qualifier);
13353 *declares_class_or_enum |= decl_spec_declares_class_or_enum;
13355 /* If this type-specifier referenced a user-defined type
13356 (a typedef, class-name, etc.), then we can't allow any
13357 more such type-specifiers henceforth.
13359 [dcl.spec]
13361 The longest sequence of decl-specifiers that could
13362 possibly be a type name is taken as the
13363 decl-specifier-seq of a declaration. The sequence shall
13364 be self-consistent as described below.
13366 [dcl.type]
13368 As a general rule, at most one type-specifier is allowed
13369 in the complete decl-specifier-seq of a declaration. The
13370 only exceptions are the following:
13372 -- const or volatile can be combined with any other
13373 type-specifier.
13375 -- signed or unsigned can be combined with char, long,
13376 short, or int.
13378 -- ..
13380 Example:
13382 typedef char* Pc;
13383 void g (const int Pc);
13385 Here, Pc is *not* part of the decl-specifier seq; it's
13386 the declarator. Therefore, once we see a type-specifier
13387 (other than a cv-qualifier), we forbid any additional
13388 user-defined types. We *do* still allow things like `int
13389 int' to be considered a decl-specifier-seq, and issue the
13390 error message later. */
13391 if (type_spec && !is_cv_qualifier)
13392 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
13393 /* A constructor declarator cannot follow a type-specifier. */
13394 if (type_spec)
13396 constructor_possible_p = false;
13397 found_decl_spec = true;
13398 if (!is_cv_qualifier)
13399 decl_specs->any_type_specifiers_p = true;
13403 /* If we still do not have a DECL_SPEC, then there are no more
13404 decl-specifiers. */
13405 if (!found_decl_spec)
13406 break;
13408 decl_specs->any_specifiers_p = true;
13409 /* After we see one decl-specifier, further decl-specifiers are
13410 always optional. */
13411 flags |= CP_PARSER_FLAGS_OPTIONAL;
13414 /* Don't allow a friend specifier with a class definition. */
13415 if (decl_spec_seq_has_spec_p (decl_specs, ds_friend)
13416 && (*declares_class_or_enum & 2))
13417 error_at (decl_specs->locations[ds_friend],
13418 "class definition may not be declared a friend");
13421 /* Parse an (optional) storage-class-specifier.
13423 storage-class-specifier:
13424 auto
13425 register
13426 static
13427 extern
13428 mutable
13430 GNU Extension:
13432 storage-class-specifier:
13433 thread
13435 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
13437 static tree
13438 cp_parser_storage_class_specifier_opt (cp_parser* parser)
13440 switch (cp_lexer_peek_token (parser->lexer)->keyword)
13442 case RID_AUTO:
13443 if (cxx_dialect != cxx98)
13444 return NULL_TREE;
13445 /* Fall through for C++98. */
13446 gcc_fallthrough ();
13448 case RID_REGISTER:
13449 case RID_STATIC:
13450 case RID_EXTERN:
13451 case RID_MUTABLE:
13452 case RID_THREAD:
13453 /* Consume the token. */
13454 return cp_lexer_consume_token (parser->lexer)->u.value;
13456 default:
13457 return NULL_TREE;
13461 /* Parse an (optional) function-specifier.
13463 function-specifier:
13464 inline
13465 virtual
13466 explicit
13468 Returns an IDENTIFIER_NODE corresponding to the keyword used.
13469 Updates DECL_SPECS, if it is non-NULL. */
13471 static tree
13472 cp_parser_function_specifier_opt (cp_parser* parser,
13473 cp_decl_specifier_seq *decl_specs)
13475 cp_token *token = cp_lexer_peek_token (parser->lexer);
13476 switch (token->keyword)
13478 case RID_INLINE:
13479 set_and_check_decl_spec_loc (decl_specs, ds_inline, token);
13480 break;
13482 case RID_VIRTUAL:
13483 /* 14.5.2.3 [temp.mem]
13485 A member function template shall not be virtual. */
13486 if (PROCESSING_REAL_TEMPLATE_DECL_P ()
13487 && current_class_type)
13488 error_at (token->location, "templates may not be %<virtual%>");
13489 else
13490 set_and_check_decl_spec_loc (decl_specs, ds_virtual, token);
13491 break;
13493 case RID_EXPLICIT:
13494 set_and_check_decl_spec_loc (decl_specs, ds_explicit, token);
13495 break;
13497 default:
13498 return NULL_TREE;
13501 /* Consume the token. */
13502 return cp_lexer_consume_token (parser->lexer)->u.value;
13505 /* Parse a linkage-specification.
13507 linkage-specification:
13508 extern string-literal { declaration-seq [opt] }
13509 extern string-literal declaration */
13511 static void
13512 cp_parser_linkage_specification (cp_parser* parser)
13514 tree linkage;
13516 /* Look for the `extern' keyword. */
13517 cp_parser_require_keyword (parser, RID_EXTERN, RT_EXTERN);
13519 /* Look for the string-literal. */
13520 linkage = cp_parser_string_literal (parser, false, false);
13522 /* Transform the literal into an identifier. If the literal is a
13523 wide-character string, or contains embedded NULs, then we can't
13524 handle it as the user wants. */
13525 if (strlen (TREE_STRING_POINTER (linkage))
13526 != (size_t) (TREE_STRING_LENGTH (linkage) - 1))
13528 cp_parser_error (parser, "invalid linkage-specification");
13529 /* Assume C++ linkage. */
13530 linkage = lang_name_cplusplus;
13532 else
13533 linkage = get_identifier (TREE_STRING_POINTER (linkage));
13535 /* We're now using the new linkage. */
13536 push_lang_context (linkage);
13538 /* If the next token is a `{', then we're using the first
13539 production. */
13540 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
13542 cp_ensure_no_omp_declare_simd (parser);
13543 cp_ensure_no_oacc_routine (parser);
13545 /* Consume the `{' token. */
13546 cp_lexer_consume_token (parser->lexer);
13547 /* Parse the declarations. */
13548 cp_parser_declaration_seq_opt (parser);
13549 /* Look for the closing `}'. */
13550 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
13552 /* Otherwise, there's just one declaration. */
13553 else
13555 bool saved_in_unbraced_linkage_specification_p;
13557 saved_in_unbraced_linkage_specification_p
13558 = parser->in_unbraced_linkage_specification_p;
13559 parser->in_unbraced_linkage_specification_p = true;
13560 cp_parser_declaration (parser);
13561 parser->in_unbraced_linkage_specification_p
13562 = saved_in_unbraced_linkage_specification_p;
13565 /* We're done with the linkage-specification. */
13566 pop_lang_context ();
13569 /* Parse a static_assert-declaration.
13571 static_assert-declaration:
13572 static_assert ( constant-expression , string-literal ) ;
13573 static_assert ( constant-expression ) ; (C++1Z)
13575 If MEMBER_P, this static_assert is a class member. */
13577 static void
13578 cp_parser_static_assert(cp_parser *parser, bool member_p)
13580 tree condition;
13581 tree message;
13582 cp_token *token;
13583 location_t saved_loc;
13584 bool dummy;
13586 /* Peek at the `static_assert' token so we can keep track of exactly
13587 where the static assertion started. */
13588 token = cp_lexer_peek_token (parser->lexer);
13589 saved_loc = token->location;
13591 /* Look for the `static_assert' keyword. */
13592 if (!cp_parser_require_keyword (parser, RID_STATIC_ASSERT,
13593 RT_STATIC_ASSERT))
13594 return;
13596 /* We know we are in a static assertion; commit to any tentative
13597 parse. */
13598 if (cp_parser_parsing_tentatively (parser))
13599 cp_parser_commit_to_tentative_parse (parser);
13601 /* Parse the `(' starting the static assertion condition. */
13602 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
13604 /* Parse the constant-expression. Allow a non-constant expression
13605 here in order to give better diagnostics in finish_static_assert. */
13606 condition =
13607 cp_parser_constant_expression (parser,
13608 /*allow_non_constant_p=*/true,
13609 /*non_constant_p=*/&dummy);
13611 if (cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
13613 if (cxx_dialect < cxx1z)
13614 pedwarn (input_location, OPT_Wpedantic,
13615 "static_assert without a message "
13616 "only available with -std=c++1z or -std=gnu++1z");
13617 /* Eat the ')' */
13618 cp_lexer_consume_token (parser->lexer);
13619 message = build_string (1, "");
13620 TREE_TYPE (message) = char_array_type_node;
13621 fix_string_type (message);
13623 else
13625 /* Parse the separating `,'. */
13626 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
13628 /* Parse the string-literal message. */
13629 message = cp_parser_string_literal (parser,
13630 /*translate=*/false,
13631 /*wide_ok=*/true);
13633 /* A `)' completes the static assertion. */
13634 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
13635 cp_parser_skip_to_closing_parenthesis (parser,
13636 /*recovering=*/true,
13637 /*or_comma=*/false,
13638 /*consume_paren=*/true);
13641 /* A semicolon terminates the declaration. */
13642 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
13644 /* Complete the static assertion, which may mean either processing
13645 the static assert now or saving it for template instantiation. */
13646 finish_static_assert (condition, message, saved_loc, member_p);
13649 /* Parse the expression in decltype ( expression ). */
13651 static tree
13652 cp_parser_decltype_expr (cp_parser *parser,
13653 bool &id_expression_or_member_access_p)
13655 cp_token *id_expr_start_token;
13656 tree expr;
13658 /* Since we're going to preserve any side-effects from this parse, set up a
13659 firewall to protect our callers from cp_parser_commit_to_tentative_parse
13660 in the expression. */
13661 tentative_firewall firewall (parser);
13663 /* First, try parsing an id-expression. */
13664 id_expr_start_token = cp_lexer_peek_token (parser->lexer);
13665 cp_parser_parse_tentatively (parser);
13666 expr = cp_parser_id_expression (parser,
13667 /*template_keyword_p=*/false,
13668 /*check_dependency_p=*/true,
13669 /*template_p=*/NULL,
13670 /*declarator_p=*/false,
13671 /*optional_p=*/false);
13673 if (!cp_parser_error_occurred (parser) && expr != error_mark_node)
13675 bool non_integral_constant_expression_p = false;
13676 tree id_expression = expr;
13677 cp_id_kind idk;
13678 const char *error_msg;
13680 if (identifier_p (expr))
13681 /* Lookup the name we got back from the id-expression. */
13682 expr = cp_parser_lookup_name_simple (parser, expr,
13683 id_expr_start_token->location);
13685 if (expr
13686 && expr != error_mark_node
13687 && TREE_CODE (expr) != TYPE_DECL
13688 && (TREE_CODE (expr) != BIT_NOT_EXPR
13689 || !TYPE_P (TREE_OPERAND (expr, 0)))
13690 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
13692 /* Complete lookup of the id-expression. */
13693 expr = (finish_id_expression
13694 (id_expression, expr, parser->scope, &idk,
13695 /*integral_constant_expression_p=*/false,
13696 /*allow_non_integral_constant_expression_p=*/true,
13697 &non_integral_constant_expression_p,
13698 /*template_p=*/false,
13699 /*done=*/true,
13700 /*address_p=*/false,
13701 /*template_arg_p=*/false,
13702 &error_msg,
13703 id_expr_start_token->location));
13705 if (expr == error_mark_node)
13706 /* We found an id-expression, but it was something that we
13707 should not have found. This is an error, not something
13708 we can recover from, so note that we found an
13709 id-expression and we'll recover as gracefully as
13710 possible. */
13711 id_expression_or_member_access_p = true;
13714 if (expr
13715 && expr != error_mark_node
13716 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
13717 /* We have an id-expression. */
13718 id_expression_or_member_access_p = true;
13721 if (!id_expression_or_member_access_p)
13723 /* Abort the id-expression parse. */
13724 cp_parser_abort_tentative_parse (parser);
13726 /* Parsing tentatively, again. */
13727 cp_parser_parse_tentatively (parser);
13729 /* Parse a class member access. */
13730 expr = cp_parser_postfix_expression (parser, /*address_p=*/false,
13731 /*cast_p=*/false, /*decltype*/true,
13732 /*member_access_only_p=*/true, NULL);
13734 if (expr
13735 && expr != error_mark_node
13736 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
13737 /* We have an id-expression. */
13738 id_expression_or_member_access_p = true;
13741 if (id_expression_or_member_access_p)
13742 /* We have parsed the complete id-expression or member access. */
13743 cp_parser_parse_definitely (parser);
13744 else
13746 /* Abort our attempt to parse an id-expression or member access
13747 expression. */
13748 cp_parser_abort_tentative_parse (parser);
13750 /* Parse a full expression. */
13751 expr = cp_parser_expression (parser, /*pidk=*/NULL, /*cast_p=*/false,
13752 /*decltype_p=*/true);
13755 return expr;
13758 /* Parse a `decltype' type. Returns the type.
13760 simple-type-specifier:
13761 decltype ( expression )
13762 C++14 proposal:
13763 decltype ( auto ) */
13765 static tree
13766 cp_parser_decltype (cp_parser *parser)
13768 tree expr;
13769 bool id_expression_or_member_access_p = false;
13770 const char *saved_message;
13771 bool saved_integral_constant_expression_p;
13772 bool saved_non_integral_constant_expression_p;
13773 bool saved_greater_than_is_operator_p;
13774 cp_token *start_token = cp_lexer_peek_token (parser->lexer);
13776 if (start_token->type == CPP_DECLTYPE)
13778 /* Already parsed. */
13779 cp_lexer_consume_token (parser->lexer);
13780 return saved_checks_value (start_token->u.tree_check_value);
13783 /* Look for the `decltype' token. */
13784 if (!cp_parser_require_keyword (parser, RID_DECLTYPE, RT_DECLTYPE))
13785 return error_mark_node;
13787 /* Parse the opening `('. */
13788 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
13789 return error_mark_node;
13791 /* decltype (auto) */
13792 if (cxx_dialect >= cxx14
13793 && cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
13795 cp_lexer_consume_token (parser->lexer);
13796 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
13797 return error_mark_node;
13798 expr = make_decltype_auto ();
13799 AUTO_IS_DECLTYPE (expr) = true;
13800 goto rewrite;
13803 /* Types cannot be defined in a `decltype' expression. Save away the
13804 old message. */
13805 saved_message = parser->type_definition_forbidden_message;
13807 /* And create the new one. */
13808 parser->type_definition_forbidden_message
13809 = G_("types may not be defined in %<decltype%> expressions");
13811 /* The restrictions on constant-expressions do not apply inside
13812 decltype expressions. */
13813 saved_integral_constant_expression_p
13814 = parser->integral_constant_expression_p;
13815 saved_non_integral_constant_expression_p
13816 = parser->non_integral_constant_expression_p;
13817 parser->integral_constant_expression_p = false;
13819 /* Within a parenthesized expression, a `>' token is always
13820 the greater-than operator. */
13821 saved_greater_than_is_operator_p
13822 = parser->greater_than_is_operator_p;
13823 parser->greater_than_is_operator_p = true;
13825 /* Do not actually evaluate the expression. */
13826 ++cp_unevaluated_operand;
13828 /* Do not warn about problems with the expression. */
13829 ++c_inhibit_evaluation_warnings;
13831 expr = cp_parser_decltype_expr (parser, id_expression_or_member_access_p);
13833 /* Go back to evaluating expressions. */
13834 --cp_unevaluated_operand;
13835 --c_inhibit_evaluation_warnings;
13837 /* The `>' token might be the end of a template-id or
13838 template-parameter-list now. */
13839 parser->greater_than_is_operator_p
13840 = saved_greater_than_is_operator_p;
13842 /* Restore the old message and the integral constant expression
13843 flags. */
13844 parser->type_definition_forbidden_message = saved_message;
13845 parser->integral_constant_expression_p
13846 = saved_integral_constant_expression_p;
13847 parser->non_integral_constant_expression_p
13848 = saved_non_integral_constant_expression_p;
13850 /* Parse to the closing `)'. */
13851 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
13853 cp_parser_skip_to_closing_parenthesis (parser, true, false,
13854 /*consume_paren=*/true);
13855 return error_mark_node;
13858 expr = finish_decltype_type (expr, id_expression_or_member_access_p,
13859 tf_warning_or_error);
13861 rewrite:
13862 /* Replace the decltype with a CPP_DECLTYPE so we don't need to parse
13863 it again. */
13864 start_token->type = CPP_DECLTYPE;
13865 start_token->u.tree_check_value = ggc_cleared_alloc<struct tree_check> ();
13866 start_token->u.tree_check_value->value = expr;
13867 start_token->u.tree_check_value->checks = get_deferred_access_checks ();
13868 start_token->keyword = RID_MAX;
13869 cp_lexer_purge_tokens_after (parser->lexer, start_token);
13871 return expr;
13874 /* Special member functions [gram.special] */
13876 /* Parse a conversion-function-id.
13878 conversion-function-id:
13879 operator conversion-type-id
13881 Returns an IDENTIFIER_NODE representing the operator. */
13883 static tree
13884 cp_parser_conversion_function_id (cp_parser* parser)
13886 tree type;
13887 tree saved_scope;
13888 tree saved_qualifying_scope;
13889 tree saved_object_scope;
13890 tree pushed_scope = NULL_TREE;
13892 /* Look for the `operator' token. */
13893 if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
13894 return error_mark_node;
13895 /* When we parse the conversion-type-id, the current scope will be
13896 reset. However, we need that information in able to look up the
13897 conversion function later, so we save it here. */
13898 saved_scope = parser->scope;
13899 saved_qualifying_scope = parser->qualifying_scope;
13900 saved_object_scope = parser->object_scope;
13901 /* We must enter the scope of the class so that the names of
13902 entities declared within the class are available in the
13903 conversion-type-id. For example, consider:
13905 struct S {
13906 typedef int I;
13907 operator I();
13910 S::operator I() { ... }
13912 In order to see that `I' is a type-name in the definition, we
13913 must be in the scope of `S'. */
13914 if (saved_scope)
13915 pushed_scope = push_scope (saved_scope);
13916 /* Parse the conversion-type-id. */
13917 type = cp_parser_conversion_type_id (parser);
13918 /* Leave the scope of the class, if any. */
13919 if (pushed_scope)
13920 pop_scope (pushed_scope);
13921 /* Restore the saved scope. */
13922 parser->scope = saved_scope;
13923 parser->qualifying_scope = saved_qualifying_scope;
13924 parser->object_scope = saved_object_scope;
13925 /* If the TYPE is invalid, indicate failure. */
13926 if (type == error_mark_node)
13927 return error_mark_node;
13928 return mangle_conv_op_name_for_type (type);
13931 /* Parse a conversion-type-id:
13933 conversion-type-id:
13934 type-specifier-seq conversion-declarator [opt]
13936 Returns the TYPE specified. */
13938 static tree
13939 cp_parser_conversion_type_id (cp_parser* parser)
13941 tree attributes;
13942 cp_decl_specifier_seq type_specifiers;
13943 cp_declarator *declarator;
13944 tree type_specified;
13945 const char *saved_message;
13947 /* Parse the attributes. */
13948 attributes = cp_parser_attributes_opt (parser);
13950 saved_message = parser->type_definition_forbidden_message;
13951 parser->type_definition_forbidden_message
13952 = G_("types may not be defined in a conversion-type-id");
13954 /* Parse the type-specifiers. */
13955 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
13956 /*is_trailing_return=*/false,
13957 &type_specifiers);
13959 parser->type_definition_forbidden_message = saved_message;
13961 /* If that didn't work, stop. */
13962 if (type_specifiers.type == error_mark_node)
13963 return error_mark_node;
13964 /* Parse the conversion-declarator. */
13965 declarator = cp_parser_conversion_declarator_opt (parser);
13967 type_specified = grokdeclarator (declarator, &type_specifiers, TYPENAME,
13968 /*initialized=*/0, &attributes);
13969 if (attributes)
13970 cplus_decl_attributes (&type_specified, attributes, /*flags=*/0);
13972 /* Don't give this error when parsing tentatively. This happens to
13973 work because we always parse this definitively once. */
13974 if (! cp_parser_uncommitted_to_tentative_parse_p (parser)
13975 && type_uses_auto (type_specified))
13977 if (cxx_dialect < cxx14)
13979 error ("invalid use of %<auto%> in conversion operator");
13980 return error_mark_node;
13982 else if (template_parm_scope_p ())
13983 warning (0, "use of %<auto%> in member template "
13984 "conversion operator can never be deduced");
13987 return type_specified;
13990 /* Parse an (optional) conversion-declarator.
13992 conversion-declarator:
13993 ptr-operator conversion-declarator [opt]
13997 static cp_declarator *
13998 cp_parser_conversion_declarator_opt (cp_parser* parser)
14000 enum tree_code code;
14001 tree class_type, std_attributes = NULL_TREE;
14002 cp_cv_quals cv_quals;
14004 /* We don't know if there's a ptr-operator next, or not. */
14005 cp_parser_parse_tentatively (parser);
14006 /* Try the ptr-operator. */
14007 code = cp_parser_ptr_operator (parser, &class_type, &cv_quals,
14008 &std_attributes);
14009 /* If it worked, look for more conversion-declarators. */
14010 if (cp_parser_parse_definitely (parser))
14012 cp_declarator *declarator;
14014 /* Parse another optional declarator. */
14015 declarator = cp_parser_conversion_declarator_opt (parser);
14017 declarator = cp_parser_make_indirect_declarator
14018 (code, class_type, cv_quals, declarator, std_attributes);
14020 return declarator;
14023 return NULL;
14026 /* Parse an (optional) ctor-initializer.
14028 ctor-initializer:
14029 : mem-initializer-list
14031 Returns TRUE iff the ctor-initializer was actually present. */
14033 static bool
14034 cp_parser_ctor_initializer_opt (cp_parser* parser)
14036 /* If the next token is not a `:', then there is no
14037 ctor-initializer. */
14038 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
14040 /* Do default initialization of any bases and members. */
14041 if (DECL_CONSTRUCTOR_P (current_function_decl))
14042 finish_mem_initializers (NULL_TREE);
14044 return false;
14047 /* Consume the `:' token. */
14048 cp_lexer_consume_token (parser->lexer);
14049 /* And the mem-initializer-list. */
14050 cp_parser_mem_initializer_list (parser);
14052 return true;
14055 /* Parse a mem-initializer-list.
14057 mem-initializer-list:
14058 mem-initializer ... [opt]
14059 mem-initializer ... [opt] , mem-initializer-list */
14061 static void
14062 cp_parser_mem_initializer_list (cp_parser* parser)
14064 tree mem_initializer_list = NULL_TREE;
14065 tree target_ctor = error_mark_node;
14066 cp_token *token = cp_lexer_peek_token (parser->lexer);
14068 /* Let the semantic analysis code know that we are starting the
14069 mem-initializer-list. */
14070 if (!DECL_CONSTRUCTOR_P (current_function_decl))
14071 error_at (token->location,
14072 "only constructors take member initializers");
14074 /* Loop through the list. */
14075 while (true)
14077 tree mem_initializer;
14079 token = cp_lexer_peek_token (parser->lexer);
14080 /* Parse the mem-initializer. */
14081 mem_initializer = cp_parser_mem_initializer (parser);
14082 /* If the next token is a `...', we're expanding member initializers. */
14083 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
14085 /* Consume the `...'. */
14086 cp_lexer_consume_token (parser->lexer);
14088 /* The TREE_PURPOSE must be a _TYPE, because base-specifiers
14089 can be expanded but members cannot. */
14090 if (mem_initializer != error_mark_node
14091 && !TYPE_P (TREE_PURPOSE (mem_initializer)))
14093 error_at (token->location,
14094 "cannot expand initializer for member %<%D%>",
14095 TREE_PURPOSE (mem_initializer));
14096 mem_initializer = error_mark_node;
14099 /* Construct the pack expansion type. */
14100 if (mem_initializer != error_mark_node)
14101 mem_initializer = make_pack_expansion (mem_initializer);
14103 if (target_ctor != error_mark_node
14104 && mem_initializer != error_mark_node)
14106 error ("mem-initializer for %qD follows constructor delegation",
14107 TREE_PURPOSE (mem_initializer));
14108 mem_initializer = error_mark_node;
14110 /* Look for a target constructor. */
14111 if (mem_initializer != error_mark_node
14112 && CLASS_TYPE_P (TREE_PURPOSE (mem_initializer))
14113 && same_type_p (TREE_PURPOSE (mem_initializer), current_class_type))
14115 maybe_warn_cpp0x (CPP0X_DELEGATING_CTORS);
14116 if (mem_initializer_list)
14118 error ("constructor delegation follows mem-initializer for %qD",
14119 TREE_PURPOSE (mem_initializer_list));
14120 mem_initializer = error_mark_node;
14122 target_ctor = mem_initializer;
14124 /* Add it to the list, unless it was erroneous. */
14125 if (mem_initializer != error_mark_node)
14127 TREE_CHAIN (mem_initializer) = mem_initializer_list;
14128 mem_initializer_list = mem_initializer;
14130 /* If the next token is not a `,', we're done. */
14131 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
14132 break;
14133 /* Consume the `,' token. */
14134 cp_lexer_consume_token (parser->lexer);
14137 /* Perform semantic analysis. */
14138 if (DECL_CONSTRUCTOR_P (current_function_decl))
14139 finish_mem_initializers (mem_initializer_list);
14142 /* Parse a mem-initializer.
14144 mem-initializer:
14145 mem-initializer-id ( expression-list [opt] )
14146 mem-initializer-id braced-init-list
14148 GNU extension:
14150 mem-initializer:
14151 ( expression-list [opt] )
14153 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
14154 class) or FIELD_DECL (for a non-static data member) to initialize;
14155 the TREE_VALUE is the expression-list. An empty initialization
14156 list is represented by void_list_node. */
14158 static tree
14159 cp_parser_mem_initializer (cp_parser* parser)
14161 tree mem_initializer_id;
14162 tree expression_list;
14163 tree member;
14164 cp_token *token = cp_lexer_peek_token (parser->lexer);
14166 /* Find out what is being initialized. */
14167 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
14169 permerror (token->location,
14170 "anachronistic old-style base class initializer");
14171 mem_initializer_id = NULL_TREE;
14173 else
14175 mem_initializer_id = cp_parser_mem_initializer_id (parser);
14176 if (mem_initializer_id == error_mark_node)
14177 return mem_initializer_id;
14179 member = expand_member_init (mem_initializer_id);
14180 if (member && !DECL_P (member))
14181 in_base_initializer = 1;
14183 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
14185 bool expr_non_constant_p;
14186 cp_lexer_set_source_position (parser->lexer);
14187 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
14188 expression_list = cp_parser_braced_list (parser, &expr_non_constant_p);
14189 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
14190 expression_list = build_tree_list (NULL_TREE, expression_list);
14192 else
14194 vec<tree, va_gc> *vec;
14195 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
14196 /*cast_p=*/false,
14197 /*allow_expansion_p=*/true,
14198 /*non_constant_p=*/NULL);
14199 if (vec == NULL)
14200 return error_mark_node;
14201 expression_list = build_tree_list_vec (vec);
14202 release_tree_vector (vec);
14205 if (expression_list == error_mark_node)
14206 return error_mark_node;
14207 if (!expression_list)
14208 expression_list = void_type_node;
14210 in_base_initializer = 0;
14212 return member ? build_tree_list (member, expression_list) : error_mark_node;
14215 /* Parse a mem-initializer-id.
14217 mem-initializer-id:
14218 :: [opt] nested-name-specifier [opt] class-name
14219 decltype-specifier (C++11)
14220 identifier
14222 Returns a TYPE indicating the class to be initialized for the first
14223 production (and the second in C++11). Returns an IDENTIFIER_NODE
14224 indicating the data member to be initialized for the last production. */
14226 static tree
14227 cp_parser_mem_initializer_id (cp_parser* parser)
14229 bool global_scope_p;
14230 bool nested_name_specifier_p;
14231 bool template_p = false;
14232 tree id;
14234 cp_token *token = cp_lexer_peek_token (parser->lexer);
14236 /* `typename' is not allowed in this context ([temp.res]). */
14237 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
14239 error_at (token->location,
14240 "keyword %<typename%> not allowed in this context (a qualified "
14241 "member initializer is implicitly a type)");
14242 cp_lexer_consume_token (parser->lexer);
14244 /* Look for the optional `::' operator. */
14245 global_scope_p
14246 = (cp_parser_global_scope_opt (parser,
14247 /*current_scope_valid_p=*/false)
14248 != NULL_TREE);
14249 /* Look for the optional nested-name-specifier. The simplest way to
14250 implement:
14252 [temp.res]
14254 The keyword `typename' is not permitted in a base-specifier or
14255 mem-initializer; in these contexts a qualified name that
14256 depends on a template-parameter is implicitly assumed to be a
14257 type name.
14259 is to assume that we have seen the `typename' keyword at this
14260 point. */
14261 nested_name_specifier_p
14262 = (cp_parser_nested_name_specifier_opt (parser,
14263 /*typename_keyword_p=*/true,
14264 /*check_dependency_p=*/true,
14265 /*type_p=*/true,
14266 /*is_declaration=*/true)
14267 != NULL_TREE);
14268 if (nested_name_specifier_p)
14269 template_p = cp_parser_optional_template_keyword (parser);
14270 /* If there is a `::' operator or a nested-name-specifier, then we
14271 are definitely looking for a class-name. */
14272 if (global_scope_p || nested_name_specifier_p)
14273 return cp_parser_class_name (parser,
14274 /*typename_keyword_p=*/true,
14275 /*template_keyword_p=*/template_p,
14276 typename_type,
14277 /*check_dependency_p=*/true,
14278 /*class_head_p=*/false,
14279 /*is_declaration=*/true);
14280 /* Otherwise, we could also be looking for an ordinary identifier. */
14281 cp_parser_parse_tentatively (parser);
14282 if (cp_lexer_next_token_is_decltype (parser->lexer))
14283 /* Try a decltype-specifier. */
14284 id = cp_parser_decltype (parser);
14285 else
14286 /* Otherwise, try a class-name. */
14287 id = cp_parser_class_name (parser,
14288 /*typename_keyword_p=*/true,
14289 /*template_keyword_p=*/false,
14290 none_type,
14291 /*check_dependency_p=*/true,
14292 /*class_head_p=*/false,
14293 /*is_declaration=*/true);
14294 /* If we found one, we're done. */
14295 if (cp_parser_parse_definitely (parser))
14296 return id;
14297 /* Otherwise, look for an ordinary identifier. */
14298 return cp_parser_identifier (parser);
14301 /* Overloading [gram.over] */
14303 /* Parse an operator-function-id.
14305 operator-function-id:
14306 operator operator
14308 Returns an IDENTIFIER_NODE for the operator which is a
14309 human-readable spelling of the identifier, e.g., `operator +'. */
14311 static cp_expr
14312 cp_parser_operator_function_id (cp_parser* parser)
14314 /* Look for the `operator' keyword. */
14315 if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
14316 return error_mark_node;
14317 /* And then the name of the operator itself. */
14318 return cp_parser_operator (parser);
14321 /* Return an identifier node for a user-defined literal operator.
14322 The suffix identifier is chained to the operator name identifier. */
14324 tree
14325 cp_literal_operator_id (const char* name)
14327 tree identifier;
14328 char *buffer = XNEWVEC (char, strlen (UDLIT_OP_ANSI_PREFIX)
14329 + strlen (name) + 10);
14330 sprintf (buffer, UDLIT_OP_ANSI_FORMAT, name);
14331 identifier = get_identifier (buffer);
14333 return identifier;
14336 /* Parse an operator.
14338 operator:
14339 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
14340 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
14341 || ++ -- , ->* -> () []
14343 GNU Extensions:
14345 operator:
14346 <? >? <?= >?=
14348 Returns an IDENTIFIER_NODE for the operator which is a
14349 human-readable spelling of the identifier, e.g., `operator +'. */
14351 static cp_expr
14352 cp_parser_operator (cp_parser* parser)
14354 tree id = NULL_TREE;
14355 cp_token *token;
14356 bool utf8 = false;
14358 /* Peek at the next token. */
14359 token = cp_lexer_peek_token (parser->lexer);
14361 location_t start_loc = token->location;
14363 /* Figure out which operator we have. */
14364 switch (token->type)
14366 case CPP_KEYWORD:
14368 enum tree_code op;
14370 /* The keyword should be either `new' or `delete'. */
14371 if (token->keyword == RID_NEW)
14372 op = NEW_EXPR;
14373 else if (token->keyword == RID_DELETE)
14374 op = DELETE_EXPR;
14375 else
14376 break;
14378 /* Consume the `new' or `delete' token. */
14379 location_t end_loc = cp_lexer_consume_token (parser->lexer)->location;
14381 /* Peek at the next token. */
14382 token = cp_lexer_peek_token (parser->lexer);
14383 /* If it's a `[' token then this is the array variant of the
14384 operator. */
14385 if (token->type == CPP_OPEN_SQUARE)
14387 /* Consume the `[' token. */
14388 cp_lexer_consume_token (parser->lexer);
14389 /* Look for the `]' token. */
14390 if (cp_token *close_token
14391 = cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
14392 end_loc = close_token->location;
14393 id = cp_operator_id (op == NEW_EXPR
14394 ? VEC_NEW_EXPR : VEC_DELETE_EXPR);
14396 /* Otherwise, we have the non-array variant. */
14397 else
14398 id = cp_operator_id (op);
14400 location_t loc = make_location (start_loc, start_loc, end_loc);
14402 return cp_expr (id, loc);
14405 case CPP_PLUS:
14406 id = cp_operator_id (PLUS_EXPR);
14407 break;
14409 case CPP_MINUS:
14410 id = cp_operator_id (MINUS_EXPR);
14411 break;
14413 case CPP_MULT:
14414 id = cp_operator_id (MULT_EXPR);
14415 break;
14417 case CPP_DIV:
14418 id = cp_operator_id (TRUNC_DIV_EXPR);
14419 break;
14421 case CPP_MOD:
14422 id = cp_operator_id (TRUNC_MOD_EXPR);
14423 break;
14425 case CPP_XOR:
14426 id = cp_operator_id (BIT_XOR_EXPR);
14427 break;
14429 case CPP_AND:
14430 id = cp_operator_id (BIT_AND_EXPR);
14431 break;
14433 case CPP_OR:
14434 id = cp_operator_id (BIT_IOR_EXPR);
14435 break;
14437 case CPP_COMPL:
14438 id = cp_operator_id (BIT_NOT_EXPR);
14439 break;
14441 case CPP_NOT:
14442 id = cp_operator_id (TRUTH_NOT_EXPR);
14443 break;
14445 case CPP_EQ:
14446 id = cp_assignment_operator_id (NOP_EXPR);
14447 break;
14449 case CPP_LESS:
14450 id = cp_operator_id (LT_EXPR);
14451 break;
14453 case CPP_GREATER:
14454 id = cp_operator_id (GT_EXPR);
14455 break;
14457 case CPP_PLUS_EQ:
14458 id = cp_assignment_operator_id (PLUS_EXPR);
14459 break;
14461 case CPP_MINUS_EQ:
14462 id = cp_assignment_operator_id (MINUS_EXPR);
14463 break;
14465 case CPP_MULT_EQ:
14466 id = cp_assignment_operator_id (MULT_EXPR);
14467 break;
14469 case CPP_DIV_EQ:
14470 id = cp_assignment_operator_id (TRUNC_DIV_EXPR);
14471 break;
14473 case CPP_MOD_EQ:
14474 id = cp_assignment_operator_id (TRUNC_MOD_EXPR);
14475 break;
14477 case CPP_XOR_EQ:
14478 id = cp_assignment_operator_id (BIT_XOR_EXPR);
14479 break;
14481 case CPP_AND_EQ:
14482 id = cp_assignment_operator_id (BIT_AND_EXPR);
14483 break;
14485 case CPP_OR_EQ:
14486 id = cp_assignment_operator_id (BIT_IOR_EXPR);
14487 break;
14489 case CPP_LSHIFT:
14490 id = cp_operator_id (LSHIFT_EXPR);
14491 break;
14493 case CPP_RSHIFT:
14494 id = cp_operator_id (RSHIFT_EXPR);
14495 break;
14497 case CPP_LSHIFT_EQ:
14498 id = cp_assignment_operator_id (LSHIFT_EXPR);
14499 break;
14501 case CPP_RSHIFT_EQ:
14502 id = cp_assignment_operator_id (RSHIFT_EXPR);
14503 break;
14505 case CPP_EQ_EQ:
14506 id = cp_operator_id (EQ_EXPR);
14507 break;
14509 case CPP_NOT_EQ:
14510 id = cp_operator_id (NE_EXPR);
14511 break;
14513 case CPP_LESS_EQ:
14514 id = cp_operator_id (LE_EXPR);
14515 break;
14517 case CPP_GREATER_EQ:
14518 id = cp_operator_id (GE_EXPR);
14519 break;
14521 case CPP_AND_AND:
14522 id = cp_operator_id (TRUTH_ANDIF_EXPR);
14523 break;
14525 case CPP_OR_OR:
14526 id = cp_operator_id (TRUTH_ORIF_EXPR);
14527 break;
14529 case CPP_PLUS_PLUS:
14530 id = cp_operator_id (POSTINCREMENT_EXPR);
14531 break;
14533 case CPP_MINUS_MINUS:
14534 id = cp_operator_id (PREDECREMENT_EXPR);
14535 break;
14537 case CPP_COMMA:
14538 id = cp_operator_id (COMPOUND_EXPR);
14539 break;
14541 case CPP_DEREF_STAR:
14542 id = cp_operator_id (MEMBER_REF);
14543 break;
14545 case CPP_DEREF:
14546 id = cp_operator_id (COMPONENT_REF);
14547 break;
14549 case CPP_OPEN_PAREN:
14550 /* Consume the `('. */
14551 cp_lexer_consume_token (parser->lexer);
14552 /* Look for the matching `)'. */
14553 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
14554 return cp_operator_id (CALL_EXPR);
14556 case CPP_OPEN_SQUARE:
14557 /* Consume the `['. */
14558 cp_lexer_consume_token (parser->lexer);
14559 /* Look for the matching `]'. */
14560 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
14561 return cp_operator_id (ARRAY_REF);
14563 case CPP_UTF8STRING:
14564 case CPP_UTF8STRING_USERDEF:
14565 utf8 = true;
14566 /* FALLTHRU */
14567 case CPP_STRING:
14568 case CPP_WSTRING:
14569 case CPP_STRING16:
14570 case CPP_STRING32:
14571 case CPP_STRING_USERDEF:
14572 case CPP_WSTRING_USERDEF:
14573 case CPP_STRING16_USERDEF:
14574 case CPP_STRING32_USERDEF:
14576 tree str, string_tree;
14577 int sz, len;
14579 if (cxx_dialect == cxx98)
14580 maybe_warn_cpp0x (CPP0X_USER_DEFINED_LITERALS);
14582 /* Consume the string. */
14583 str = cp_parser_string_literal (parser, /*translate=*/true,
14584 /*wide_ok=*/true, /*lookup_udlit=*/false);
14585 if (str == error_mark_node)
14586 return error_mark_node;
14587 else if (TREE_CODE (str) == USERDEF_LITERAL)
14589 string_tree = USERDEF_LITERAL_VALUE (str);
14590 id = USERDEF_LITERAL_SUFFIX_ID (str);
14592 else
14594 string_tree = str;
14595 /* Look for the suffix identifier. */
14596 token = cp_lexer_peek_token (parser->lexer);
14597 if (token->type == CPP_NAME)
14598 id = cp_parser_identifier (parser);
14599 else if (token->type == CPP_KEYWORD)
14601 error ("unexpected keyword;"
14602 " remove space between quotes and suffix identifier");
14603 return error_mark_node;
14605 else
14607 error ("expected suffix identifier");
14608 return error_mark_node;
14611 sz = TREE_INT_CST_LOW (TYPE_SIZE_UNIT
14612 (TREE_TYPE (TREE_TYPE (string_tree))));
14613 len = TREE_STRING_LENGTH (string_tree) / sz - 1;
14614 if (len != 0)
14616 error ("expected empty string after %<operator%> keyword");
14617 return error_mark_node;
14619 if (utf8 || TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (string_tree)))
14620 != char_type_node)
14622 error ("invalid encoding prefix in literal operator");
14623 return error_mark_node;
14625 if (id != error_mark_node)
14627 const char *name = IDENTIFIER_POINTER (id);
14628 id = cp_literal_operator_id (name);
14630 return id;
14633 default:
14634 /* Anything else is an error. */
14635 break;
14638 /* If we have selected an identifier, we need to consume the
14639 operator token. */
14640 if (id)
14641 cp_lexer_consume_token (parser->lexer);
14642 /* Otherwise, no valid operator name was present. */
14643 else
14645 cp_parser_error (parser, "expected operator");
14646 id = error_mark_node;
14649 return cp_expr (id, start_loc);
14652 /* Parse a template-declaration.
14654 template-declaration:
14655 export [opt] template < template-parameter-list > declaration
14657 If MEMBER_P is TRUE, this template-declaration occurs within a
14658 class-specifier.
14660 The grammar rule given by the standard isn't correct. What
14661 is really meant is:
14663 template-declaration:
14664 export [opt] template-parameter-list-seq
14665 decl-specifier-seq [opt] init-declarator [opt] ;
14666 export [opt] template-parameter-list-seq
14667 function-definition
14669 template-parameter-list-seq:
14670 template-parameter-list-seq [opt]
14671 template < template-parameter-list >
14673 Concept Extensions:
14675 template-parameter-list-seq:
14676 template < template-parameter-list > requires-clause [opt]
14678 requires-clause:
14679 requires logical-or-expression */
14681 static void
14682 cp_parser_template_declaration (cp_parser* parser, bool member_p)
14684 /* Check for `export'. */
14685 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
14687 /* Consume the `export' token. */
14688 cp_lexer_consume_token (parser->lexer);
14689 /* Warn that we do not support `export'. */
14690 warning (0, "keyword %<export%> not implemented, and will be ignored");
14693 cp_parser_template_declaration_after_export (parser, member_p);
14696 /* Parse a template-parameter-list.
14698 template-parameter-list:
14699 template-parameter
14700 template-parameter-list , template-parameter
14702 Returns a TREE_LIST. Each node represents a template parameter.
14703 The nodes are connected via their TREE_CHAINs. */
14705 static tree
14706 cp_parser_template_parameter_list (cp_parser* parser)
14708 tree parameter_list = NULL_TREE;
14710 begin_template_parm_list ();
14712 /* The loop below parses the template parms. We first need to know
14713 the total number of template parms to be able to compute proper
14714 canonical types of each dependent type. So after the loop, when
14715 we know the total number of template parms,
14716 end_template_parm_list computes the proper canonical types and
14717 fixes up the dependent types accordingly. */
14718 while (true)
14720 tree parameter;
14721 bool is_non_type;
14722 bool is_parameter_pack;
14723 location_t parm_loc;
14725 /* Parse the template-parameter. */
14726 parm_loc = cp_lexer_peek_token (parser->lexer)->location;
14727 parameter = cp_parser_template_parameter (parser,
14728 &is_non_type,
14729 &is_parameter_pack);
14730 /* Add it to the list. */
14731 if (parameter != error_mark_node)
14732 parameter_list = process_template_parm (parameter_list,
14733 parm_loc,
14734 parameter,
14735 is_non_type,
14736 is_parameter_pack);
14737 else
14739 tree err_parm = build_tree_list (parameter, parameter);
14740 parameter_list = chainon (parameter_list, err_parm);
14743 /* If the next token is not a `,', we're done. */
14744 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
14745 break;
14746 /* Otherwise, consume the `,' token. */
14747 cp_lexer_consume_token (parser->lexer);
14750 return end_template_parm_list (parameter_list);
14753 /* Parse a introduction-list.
14755 introduction-list:
14756 introduced-parameter
14757 introduction-list , introduced-parameter
14759 introduced-parameter:
14760 ...[opt] identifier
14762 Returns a TREE_VEC of WILDCARD_DECLs. If the parameter is a pack
14763 then the introduced parm will have WILDCARD_PACK_P set. In addition, the
14764 WILDCARD_DECL will also have DECL_NAME set and token location in
14765 DECL_SOURCE_LOCATION. */
14767 static tree
14768 cp_parser_introduction_list (cp_parser *parser)
14770 vec<tree, va_gc> *introduction_vec = make_tree_vector ();
14772 while (true)
14774 bool is_pack = cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS);
14775 if (is_pack)
14776 cp_lexer_consume_token (parser->lexer);
14778 /* Build placeholder. */
14779 tree parm = build_nt (WILDCARD_DECL);
14780 DECL_SOURCE_LOCATION (parm)
14781 = cp_lexer_peek_token (parser->lexer)->location;
14782 DECL_NAME (parm) = cp_parser_identifier (parser);
14783 WILDCARD_PACK_P (parm) = is_pack;
14784 vec_safe_push (introduction_vec, parm);
14786 /* If the next token is not a `,', we're done. */
14787 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
14788 break;
14789 /* Otherwise, consume the `,' token. */
14790 cp_lexer_consume_token (parser->lexer);
14793 /* Convert the vec into a TREE_VEC. */
14794 tree introduction_list = make_tree_vec (introduction_vec->length ());
14795 unsigned int n;
14796 tree parm;
14797 FOR_EACH_VEC_ELT (*introduction_vec, n, parm)
14798 TREE_VEC_ELT (introduction_list, n) = parm;
14800 release_tree_vector (introduction_vec);
14801 return introduction_list;
14804 /* Given a declarator, get the declarator-id part, or NULL_TREE if this
14805 is an abstract declarator. */
14807 static inline cp_declarator*
14808 get_id_declarator (cp_declarator *declarator)
14810 cp_declarator *d = declarator;
14811 while (d && d->kind != cdk_id)
14812 d = d->declarator;
14813 return d;
14816 /* Get the unqualified-id from the DECLARATOR or NULL_TREE if this
14817 is an abstract declarator. */
14819 static inline tree
14820 get_unqualified_id (cp_declarator *declarator)
14822 declarator = get_id_declarator (declarator);
14823 if (declarator)
14824 return declarator->u.id.unqualified_name;
14825 else
14826 return NULL_TREE;
14829 /* Returns true if DECL represents a constrained-parameter. */
14831 static inline bool
14832 is_constrained_parameter (tree decl)
14834 return (decl
14835 && TREE_CODE (decl) == TYPE_DECL
14836 && CONSTRAINED_PARM_CONCEPT (decl)
14837 && DECL_P (CONSTRAINED_PARM_CONCEPT (decl)));
14840 /* Returns true if PARM declares a constrained-parameter. */
14842 static inline bool
14843 is_constrained_parameter (cp_parameter_declarator *parm)
14845 return is_constrained_parameter (parm->decl_specifiers.type);
14848 /* Check that the type parameter is only a declarator-id, and that its
14849 type is not cv-qualified. */
14851 bool
14852 cp_parser_check_constrained_type_parm (cp_parser *parser,
14853 cp_parameter_declarator *parm)
14855 if (!parm->declarator)
14856 return true;
14858 if (parm->declarator->kind != cdk_id)
14860 cp_parser_error (parser, "invalid constrained type parameter");
14861 return false;
14864 /* Don't allow cv-qualified type parameters. */
14865 if (decl_spec_seq_has_spec_p (&parm->decl_specifiers, ds_const)
14866 || decl_spec_seq_has_spec_p (&parm->decl_specifiers, ds_volatile))
14868 cp_parser_error (parser, "cv-qualified type parameter");
14869 return false;
14872 return true;
14875 /* Finish parsing/processing a template type parameter and checking
14876 various restrictions. */
14878 static inline tree
14879 cp_parser_constrained_type_template_parm (cp_parser *parser,
14880 tree id,
14881 cp_parameter_declarator* parmdecl)
14883 if (cp_parser_check_constrained_type_parm (parser, parmdecl))
14884 return finish_template_type_parm (class_type_node, id);
14885 else
14886 return error_mark_node;
14889 static tree
14890 finish_constrained_template_template_parm (tree proto, tree id)
14892 /* FIXME: This should probably be copied, and we may need to adjust
14893 the template parameter depths. */
14894 tree saved_parms = current_template_parms;
14895 begin_template_parm_list ();
14896 current_template_parms = DECL_TEMPLATE_PARMS (proto);
14897 end_template_parm_list ();
14899 tree parm = finish_template_template_parm (class_type_node, id);
14900 current_template_parms = saved_parms;
14902 return parm;
14905 /* Finish parsing/processing a template template parameter by borrowing
14906 the template parameter list from the prototype parameter. */
14908 static tree
14909 cp_parser_constrained_template_template_parm (cp_parser *parser,
14910 tree proto,
14911 tree id,
14912 cp_parameter_declarator *parmdecl)
14914 if (!cp_parser_check_constrained_type_parm (parser, parmdecl))
14915 return error_mark_node;
14916 return finish_constrained_template_template_parm (proto, id);
14919 /* Create a new non-type template parameter from the given PARM
14920 declarator. */
14922 static tree
14923 constrained_non_type_template_parm (bool *is_non_type,
14924 cp_parameter_declarator *parm)
14926 *is_non_type = true;
14927 cp_declarator *decl = parm->declarator;
14928 cp_decl_specifier_seq *specs = &parm->decl_specifiers;
14929 specs->type = TREE_TYPE (DECL_INITIAL (specs->type));
14930 return grokdeclarator (decl, specs, TPARM, 0, NULL);
14933 /* Build a constrained template parameter based on the PARMDECL
14934 declarator. The type of PARMDECL is the constrained type, which
14935 refers to the prototype template parameter that ultimately
14936 specifies the type of the declared parameter. */
14938 static tree
14939 finish_constrained_parameter (cp_parser *parser,
14940 cp_parameter_declarator *parmdecl,
14941 bool *is_non_type,
14942 bool *is_parameter_pack)
14944 tree decl = parmdecl->decl_specifiers.type;
14945 tree id = get_unqualified_id (parmdecl->declarator);
14946 tree def = parmdecl->default_argument;
14947 tree proto = DECL_INITIAL (decl);
14949 /* A template parameter constrained by a variadic concept shall also
14950 be declared as a template parameter pack. */
14951 bool is_variadic = template_parameter_pack_p (proto);
14952 if (is_variadic && !*is_parameter_pack)
14953 cp_parser_error (parser, "variadic constraint introduced without %<...%>");
14955 /* Build the parameter. Return an error if the declarator was invalid. */
14956 tree parm;
14957 if (TREE_CODE (proto) == TYPE_DECL)
14958 parm = cp_parser_constrained_type_template_parm (parser, id, parmdecl);
14959 else if (TREE_CODE (proto) == TEMPLATE_DECL)
14960 parm = cp_parser_constrained_template_template_parm (parser, proto, id,
14961 parmdecl);
14962 else
14963 parm = constrained_non_type_template_parm (is_non_type, parmdecl);
14964 if (parm == error_mark_node)
14965 return error_mark_node;
14967 /* Finish the parameter decl and create a node attaching the
14968 default argument and constraint. */
14969 parm = build_tree_list (def, parm);
14970 TEMPLATE_PARM_CONSTRAINTS (parm) = decl;
14972 return parm;
14975 /* Returns true if the parsed type actually represents the declaration
14976 of a type template-parameter. */
14978 static inline bool
14979 declares_constrained_type_template_parameter (tree type)
14981 return (is_constrained_parameter (type)
14982 && TREE_CODE (TREE_TYPE (type)) == TEMPLATE_TYPE_PARM);
14986 /* Returns true if the parsed type actually represents the declaration of
14987 a template template-parameter. */
14989 static bool
14990 declares_constrained_template_template_parameter (tree type)
14992 return (is_constrained_parameter (type)
14993 && TREE_CODE (TREE_TYPE (type)) == TEMPLATE_TEMPLATE_PARM);
14996 /* Parse a default argument for a type template-parameter.
14997 Note that diagnostics are handled in cp_parser_template_parameter. */
14999 static tree
15000 cp_parser_default_type_template_argument (cp_parser *parser)
15002 gcc_assert (cp_lexer_next_token_is (parser->lexer, CPP_EQ));
15004 /* Consume the `=' token. */
15005 cp_lexer_consume_token (parser->lexer);
15007 cp_token *token = cp_lexer_peek_token (parser->lexer);
15009 /* Parse the default-argument. */
15010 push_deferring_access_checks (dk_no_deferred);
15011 tree default_argument = cp_parser_type_id (parser);
15012 pop_deferring_access_checks ();
15014 if (flag_concepts && type_uses_auto (default_argument))
15016 error_at (token->location,
15017 "invalid use of %<auto%> in default template argument");
15018 return error_mark_node;
15021 return default_argument;
15024 /* Parse a default argument for a template template-parameter. */
15026 static tree
15027 cp_parser_default_template_template_argument (cp_parser *parser)
15029 gcc_assert (cp_lexer_next_token_is (parser->lexer, CPP_EQ));
15031 bool is_template;
15033 /* Consume the `='. */
15034 cp_lexer_consume_token (parser->lexer);
15035 /* Parse the id-expression. */
15036 push_deferring_access_checks (dk_no_deferred);
15037 /* save token before parsing the id-expression, for error
15038 reporting */
15039 const cp_token* token = cp_lexer_peek_token (parser->lexer);
15040 tree default_argument
15041 = cp_parser_id_expression (parser,
15042 /*template_keyword_p=*/false,
15043 /*check_dependency_p=*/true,
15044 /*template_p=*/&is_template,
15045 /*declarator_p=*/false,
15046 /*optional_p=*/false);
15047 if (TREE_CODE (default_argument) == TYPE_DECL)
15048 /* If the id-expression was a template-id that refers to
15049 a template-class, we already have the declaration here,
15050 so no further lookup is needed. */
15052 else
15053 /* Look up the name. */
15054 default_argument
15055 = cp_parser_lookup_name (parser, default_argument,
15056 none_type,
15057 /*is_template=*/is_template,
15058 /*is_namespace=*/false,
15059 /*check_dependency=*/true,
15060 /*ambiguous_decls=*/NULL,
15061 token->location);
15062 /* See if the default argument is valid. */
15063 default_argument = check_template_template_default_arg (default_argument);
15064 pop_deferring_access_checks ();
15065 return default_argument;
15068 /* Parse a template-parameter.
15070 template-parameter:
15071 type-parameter
15072 parameter-declaration
15074 If all goes well, returns a TREE_LIST. The TREE_VALUE represents
15075 the parameter. The TREE_PURPOSE is the default value, if any.
15076 Returns ERROR_MARK_NODE on failure. *IS_NON_TYPE is set to true
15077 iff this parameter is a non-type parameter. *IS_PARAMETER_PACK is
15078 set to true iff this parameter is a parameter pack. */
15080 static tree
15081 cp_parser_template_parameter (cp_parser* parser, bool *is_non_type,
15082 bool *is_parameter_pack)
15084 cp_token *token;
15085 cp_parameter_declarator *parameter_declarator;
15086 tree parm;
15088 /* Assume it is a type parameter or a template parameter. */
15089 *is_non_type = false;
15090 /* Assume it not a parameter pack. */
15091 *is_parameter_pack = false;
15092 /* Peek at the next token. */
15093 token = cp_lexer_peek_token (parser->lexer);
15094 /* If it is `class' or `template', we have a type-parameter. */
15095 if (token->keyword == RID_TEMPLATE)
15096 return cp_parser_type_parameter (parser, is_parameter_pack);
15097 /* If it is `class' or `typename' we do not know yet whether it is a
15098 type parameter or a non-type parameter. Consider:
15100 template <typename T, typename T::X X> ...
15104 template <class C, class D*> ...
15106 Here, the first parameter is a type parameter, and the second is
15107 a non-type parameter. We can tell by looking at the token after
15108 the identifier -- if it is a `,', `=', or `>' then we have a type
15109 parameter. */
15110 if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
15112 /* Peek at the token after `class' or `typename'. */
15113 token = cp_lexer_peek_nth_token (parser->lexer, 2);
15114 /* If it's an ellipsis, we have a template type parameter
15115 pack. */
15116 if (token->type == CPP_ELLIPSIS)
15117 return cp_parser_type_parameter (parser, is_parameter_pack);
15118 /* If it's an identifier, skip it. */
15119 if (token->type == CPP_NAME)
15120 token = cp_lexer_peek_nth_token (parser->lexer, 3);
15121 /* Now, see if the token looks like the end of a template
15122 parameter. */
15123 if (token->type == CPP_COMMA
15124 || token->type == CPP_EQ
15125 || token->type == CPP_GREATER)
15126 return cp_parser_type_parameter (parser, is_parameter_pack);
15129 /* Otherwise, it is a non-type parameter or a constrained parameter.
15131 [temp.param]
15133 When parsing a default template-argument for a non-type
15134 template-parameter, the first non-nested `>' is taken as the end
15135 of the template parameter-list rather than a greater-than
15136 operator. */
15137 parameter_declarator
15138 = cp_parser_parameter_declaration (parser, /*template_parm_p=*/true,
15139 /*parenthesized_p=*/NULL);
15141 if (!parameter_declarator)
15142 return error_mark_node;
15144 /* If the parameter declaration is marked as a parameter pack, set
15145 *IS_PARAMETER_PACK to notify the caller. */
15146 if (parameter_declarator->template_parameter_pack_p)
15147 *is_parameter_pack = true;
15149 if (parameter_declarator->default_argument)
15151 /* Can happen in some cases of erroneous input (c++/34892). */
15152 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
15153 /* Consume the `...' for better error recovery. */
15154 cp_lexer_consume_token (parser->lexer);
15157 // The parameter may have been constrained.
15158 if (is_constrained_parameter (parameter_declarator))
15159 return finish_constrained_parameter (parser,
15160 parameter_declarator,
15161 is_non_type,
15162 is_parameter_pack);
15164 // Now we're sure that the parameter is a non-type parameter.
15165 *is_non_type = true;
15167 parm = grokdeclarator (parameter_declarator->declarator,
15168 &parameter_declarator->decl_specifiers,
15169 TPARM, /*initialized=*/0,
15170 /*attrlist=*/NULL);
15171 if (parm == error_mark_node)
15172 return error_mark_node;
15174 return build_tree_list (parameter_declarator->default_argument, parm);
15177 /* Parse a type-parameter.
15179 type-parameter:
15180 class identifier [opt]
15181 class identifier [opt] = type-id
15182 typename identifier [opt]
15183 typename identifier [opt] = type-id
15184 template < template-parameter-list > class identifier [opt]
15185 template < template-parameter-list > class identifier [opt]
15186 = id-expression
15188 GNU Extension (variadic templates):
15190 type-parameter:
15191 class ... identifier [opt]
15192 typename ... identifier [opt]
15194 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
15195 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
15196 the declaration of the parameter.
15198 Sets *IS_PARAMETER_PACK if this is a template parameter pack. */
15200 static tree
15201 cp_parser_type_parameter (cp_parser* parser, bool *is_parameter_pack)
15203 cp_token *token;
15204 tree parameter;
15206 /* Look for a keyword to tell us what kind of parameter this is. */
15207 token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_TYPENAME_TEMPLATE);
15208 if (!token)
15209 return error_mark_node;
15211 switch (token->keyword)
15213 case RID_CLASS:
15214 case RID_TYPENAME:
15216 tree identifier;
15217 tree default_argument;
15219 /* If the next token is an ellipsis, we have a template
15220 argument pack. */
15221 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
15223 /* Consume the `...' token. */
15224 cp_lexer_consume_token (parser->lexer);
15225 maybe_warn_variadic_templates ();
15227 *is_parameter_pack = true;
15230 /* If the next token is an identifier, then it names the
15231 parameter. */
15232 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
15233 identifier = cp_parser_identifier (parser);
15234 else
15235 identifier = NULL_TREE;
15237 /* Create the parameter. */
15238 parameter = finish_template_type_parm (class_type_node, identifier);
15240 /* If the next token is an `=', we have a default argument. */
15241 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
15243 default_argument
15244 = cp_parser_default_type_template_argument (parser);
15246 /* Template parameter packs cannot have default
15247 arguments. */
15248 if (*is_parameter_pack)
15250 if (identifier)
15251 error_at (token->location,
15252 "template parameter pack %qD cannot have a "
15253 "default argument", identifier);
15254 else
15255 error_at (token->location,
15256 "template parameter packs cannot have "
15257 "default arguments");
15258 default_argument = NULL_TREE;
15260 else if (check_for_bare_parameter_packs (default_argument))
15261 default_argument = error_mark_node;
15263 else
15264 default_argument = NULL_TREE;
15266 /* Create the combined representation of the parameter and the
15267 default argument. */
15268 parameter = build_tree_list (default_argument, parameter);
15270 break;
15272 case RID_TEMPLATE:
15274 tree identifier;
15275 tree default_argument;
15277 /* Look for the `<'. */
15278 cp_parser_require (parser, CPP_LESS, RT_LESS);
15279 /* Parse the template-parameter-list. */
15280 cp_parser_template_parameter_list (parser);
15281 /* Look for the `>'. */
15282 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
15284 // If template requirements are present, parse them.
15285 if (flag_concepts)
15287 tree reqs = get_shorthand_constraints (current_template_parms);
15288 if (tree r = cp_parser_requires_clause_opt (parser))
15289 reqs = conjoin_constraints (reqs, normalize_expression (r));
15290 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) = reqs;
15293 /* Look for the `class' or 'typename' keywords. */
15294 cp_parser_type_parameter_key (parser);
15295 /* If the next token is an ellipsis, we have a template
15296 argument pack. */
15297 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
15299 /* Consume the `...' token. */
15300 cp_lexer_consume_token (parser->lexer);
15301 maybe_warn_variadic_templates ();
15303 *is_parameter_pack = true;
15305 /* If the next token is an `=', then there is a
15306 default-argument. If the next token is a `>', we are at
15307 the end of the parameter-list. If the next token is a `,',
15308 then we are at the end of this parameter. */
15309 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
15310 && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
15311 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
15313 identifier = cp_parser_identifier (parser);
15314 /* Treat invalid names as if the parameter were nameless. */
15315 if (identifier == error_mark_node)
15316 identifier = NULL_TREE;
15318 else
15319 identifier = NULL_TREE;
15321 /* Create the template parameter. */
15322 parameter = finish_template_template_parm (class_type_node,
15323 identifier);
15325 /* If the next token is an `=', then there is a
15326 default-argument. */
15327 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
15329 default_argument
15330 = cp_parser_default_template_template_argument (parser);
15332 /* Template parameter packs cannot have default
15333 arguments. */
15334 if (*is_parameter_pack)
15336 if (identifier)
15337 error_at (token->location,
15338 "template parameter pack %qD cannot "
15339 "have a default argument",
15340 identifier);
15341 else
15342 error_at (token->location, "template parameter packs cannot "
15343 "have default arguments");
15344 default_argument = NULL_TREE;
15347 else
15348 default_argument = NULL_TREE;
15350 /* Create the combined representation of the parameter and the
15351 default argument. */
15352 parameter = build_tree_list (default_argument, parameter);
15354 break;
15356 default:
15357 gcc_unreachable ();
15358 break;
15361 return parameter;
15364 /* Parse a template-id.
15366 template-id:
15367 template-name < template-argument-list [opt] >
15369 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
15370 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
15371 returned. Otherwise, if the template-name names a function, or set
15372 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
15373 names a class, returns a TYPE_DECL for the specialization.
15375 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
15376 uninstantiated templates. */
15378 static tree
15379 cp_parser_template_id (cp_parser *parser,
15380 bool template_keyword_p,
15381 bool check_dependency_p,
15382 enum tag_types tag_type,
15383 bool is_declaration)
15385 tree templ;
15386 tree arguments;
15387 tree template_id;
15388 cp_token_position start_of_id = 0;
15389 cp_token *next_token = NULL, *next_token_2 = NULL;
15390 bool is_identifier;
15392 /* If the next token corresponds to a template-id, there is no need
15393 to reparse it. */
15394 next_token = cp_lexer_peek_token (parser->lexer);
15395 if (next_token->type == CPP_TEMPLATE_ID)
15397 cp_lexer_consume_token (parser->lexer);
15398 return saved_checks_value (next_token->u.tree_check_value);
15401 /* Avoid performing name lookup if there is no possibility of
15402 finding a template-id. */
15403 if ((next_token->type != CPP_NAME && next_token->keyword != RID_OPERATOR)
15404 || (next_token->type == CPP_NAME
15405 && !cp_parser_nth_token_starts_template_argument_list_p
15406 (parser, 2)))
15408 cp_parser_error (parser, "expected template-id");
15409 return error_mark_node;
15412 /* Remember where the template-id starts. */
15413 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
15414 start_of_id = cp_lexer_token_position (parser->lexer, false);
15416 push_deferring_access_checks (dk_deferred);
15418 /* Parse the template-name. */
15419 is_identifier = false;
15420 templ = cp_parser_template_name (parser, template_keyword_p,
15421 check_dependency_p,
15422 is_declaration,
15423 tag_type,
15424 &is_identifier);
15425 if (templ == error_mark_node || is_identifier)
15427 pop_deferring_access_checks ();
15428 return templ;
15431 /* Since we're going to preserve any side-effects from this parse, set up a
15432 firewall to protect our callers from cp_parser_commit_to_tentative_parse
15433 in the template arguments. */
15434 tentative_firewall firewall (parser);
15436 /* If we find the sequence `[:' after a template-name, it's probably
15437 a digraph-typo for `< ::'. Substitute the tokens and check if we can
15438 parse correctly the argument list. */
15439 if (((next_token = cp_lexer_peek_token (parser->lexer))->type
15440 == CPP_OPEN_SQUARE)
15441 && next_token->flags & DIGRAPH
15442 && ((next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2))->type
15443 == CPP_COLON)
15444 && !(next_token_2->flags & PREV_WHITE))
15446 cp_parser_parse_tentatively (parser);
15447 /* Change `:' into `::'. */
15448 next_token_2->type = CPP_SCOPE;
15449 /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
15450 CPP_LESS. */
15451 cp_lexer_consume_token (parser->lexer);
15453 /* Parse the arguments. */
15454 arguments = cp_parser_enclosed_template_argument_list (parser);
15455 if (!cp_parser_parse_definitely (parser))
15457 /* If we couldn't parse an argument list, then we revert our changes
15458 and return simply an error. Maybe this is not a template-id
15459 after all. */
15460 next_token_2->type = CPP_COLON;
15461 cp_parser_error (parser, "expected %<<%>");
15462 pop_deferring_access_checks ();
15463 return error_mark_node;
15465 /* Otherwise, emit an error about the invalid digraph, but continue
15466 parsing because we got our argument list. */
15467 if (permerror (next_token->location,
15468 "%<<::%> cannot begin a template-argument list"))
15470 static bool hint = false;
15471 inform (next_token->location,
15472 "%<<:%> is an alternate spelling for %<[%>."
15473 " Insert whitespace between %<<%> and %<::%>");
15474 if (!hint && !flag_permissive)
15476 inform (next_token->location, "(if you use %<-fpermissive%> "
15477 "or %<-std=c++11%>, or %<-std=gnu++11%> G++ will "
15478 "accept your code)");
15479 hint = true;
15483 else
15485 /* Look for the `<' that starts the template-argument-list. */
15486 if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
15488 pop_deferring_access_checks ();
15489 return error_mark_node;
15491 /* Parse the arguments. */
15492 arguments = cp_parser_enclosed_template_argument_list (parser);
15495 /* Build a representation of the specialization. */
15496 if (identifier_p (templ))
15497 template_id = build_min_nt_loc (next_token->location,
15498 TEMPLATE_ID_EXPR,
15499 templ, arguments);
15500 else if (DECL_TYPE_TEMPLATE_P (templ)
15501 || DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
15503 bool entering_scope;
15504 /* In "template <typename T> ... A<T>::", A<T> is the abstract A
15505 template (rather than some instantiation thereof) only if
15506 is not nested within some other construct. For example, in
15507 "template <typename T> void f(T) { A<T>::", A<T> is just an
15508 instantiation of A. */
15509 entering_scope = (template_parm_scope_p ()
15510 && cp_lexer_next_token_is (parser->lexer,
15511 CPP_SCOPE));
15512 template_id
15513 = finish_template_type (templ, arguments, entering_scope);
15515 /* A template-like identifier may be a partial concept id. */
15516 else if (flag_concepts
15517 && (template_id = (cp_parser_maybe_partial_concept_id
15518 (parser, templ, arguments))))
15519 return template_id;
15520 else if (variable_template_p (templ))
15522 template_id = lookup_template_variable (templ, arguments);
15523 if (TREE_CODE (template_id) == TEMPLATE_ID_EXPR)
15524 SET_EXPR_LOCATION (template_id, next_token->location);
15526 else
15528 /* If it's not a class-template or a template-template, it should be
15529 a function-template. */
15530 gcc_assert ((DECL_FUNCTION_TEMPLATE_P (templ)
15531 || TREE_CODE (templ) == OVERLOAD
15532 || BASELINK_P (templ)));
15534 template_id = lookup_template_function (templ, arguments);
15535 if (TREE_CODE (template_id) == TEMPLATE_ID_EXPR)
15536 SET_EXPR_LOCATION (template_id, next_token->location);
15539 /* If parsing tentatively, replace the sequence of tokens that makes
15540 up the template-id with a CPP_TEMPLATE_ID token. That way,
15541 should we re-parse the token stream, we will not have to repeat
15542 the effort required to do the parse, nor will we issue duplicate
15543 error messages about problems during instantiation of the
15544 template. */
15545 if (start_of_id
15546 /* Don't do this if we had a parse error in a declarator; re-parsing
15547 might succeed if a name changes meaning (60361). */
15548 && !(cp_parser_error_occurred (parser)
15549 && cp_parser_parsing_tentatively (parser)
15550 && parser->in_declarator_p))
15552 cp_token *token = cp_lexer_token_at (parser->lexer, start_of_id);
15554 /* Reset the contents of the START_OF_ID token. */
15555 token->type = CPP_TEMPLATE_ID;
15557 /* Update the location to be of the form:
15558 template-name < template-argument-list [opt] >
15559 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
15560 with caret == start at the start of the template-name,
15561 ranging until the closing '>'. */
15562 location_t finish_loc
15563 = get_finish (cp_lexer_previous_token (parser->lexer)->location);
15564 location_t combined_loc
15565 = make_location (token->location, token->location, finish_loc);
15566 token->location = combined_loc;
15568 /* Retrieve any deferred checks. Do not pop this access checks yet
15569 so the memory will not be reclaimed during token replacing below. */
15570 token->u.tree_check_value = ggc_cleared_alloc<struct tree_check> ();
15571 token->u.tree_check_value->value = template_id;
15572 token->u.tree_check_value->checks = get_deferred_access_checks ();
15573 token->keyword = RID_MAX;
15575 /* Purge all subsequent tokens. */
15576 cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
15578 /* ??? Can we actually assume that, if template_id ==
15579 error_mark_node, we will have issued a diagnostic to the
15580 user, as opposed to simply marking the tentative parse as
15581 failed? */
15582 if (cp_parser_error_occurred (parser) && template_id != error_mark_node)
15583 error_at (token->location, "parse error in template argument list");
15586 pop_to_parent_deferring_access_checks ();
15587 return template_id;
15590 /* Parse a template-name.
15592 template-name:
15593 identifier
15595 The standard should actually say:
15597 template-name:
15598 identifier
15599 operator-function-id
15601 A defect report has been filed about this issue.
15603 A conversion-function-id cannot be a template name because they cannot
15604 be part of a template-id. In fact, looking at this code:
15606 a.operator K<int>()
15608 the conversion-function-id is "operator K<int>", and K<int> is a type-id.
15609 It is impossible to call a templated conversion-function-id with an
15610 explicit argument list, since the only allowed template parameter is
15611 the type to which it is converting.
15613 If TEMPLATE_KEYWORD_P is true, then we have just seen the
15614 `template' keyword, in a construction like:
15616 T::template f<3>()
15618 In that case `f' is taken to be a template-name, even though there
15619 is no way of knowing for sure.
15621 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
15622 name refers to a set of overloaded functions, at least one of which
15623 is a template, or an IDENTIFIER_NODE with the name of the template,
15624 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
15625 names are looked up inside uninstantiated templates. */
15627 static tree
15628 cp_parser_template_name (cp_parser* parser,
15629 bool template_keyword_p,
15630 bool check_dependency_p,
15631 bool is_declaration,
15632 enum tag_types tag_type,
15633 bool *is_identifier)
15635 tree identifier;
15636 tree decl;
15637 tree fns;
15638 cp_token *token = cp_lexer_peek_token (parser->lexer);
15640 /* If the next token is `operator', then we have either an
15641 operator-function-id or a conversion-function-id. */
15642 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
15644 /* We don't know whether we're looking at an
15645 operator-function-id or a conversion-function-id. */
15646 cp_parser_parse_tentatively (parser);
15647 /* Try an operator-function-id. */
15648 identifier = cp_parser_operator_function_id (parser);
15649 /* If that didn't work, try a conversion-function-id. */
15650 if (!cp_parser_parse_definitely (parser))
15652 cp_parser_error (parser, "expected template-name");
15653 return error_mark_node;
15656 /* Look for the identifier. */
15657 else
15658 identifier = cp_parser_identifier (parser);
15660 /* If we didn't find an identifier, we don't have a template-id. */
15661 if (identifier == error_mark_node)
15662 return error_mark_node;
15664 /* If the name immediately followed the `template' keyword, then it
15665 is a template-name. However, if the next token is not `<', then
15666 we do not treat it as a template-name, since it is not being used
15667 as part of a template-id. This enables us to handle constructs
15668 like:
15670 template <typename T> struct S { S(); };
15671 template <typename T> S<T>::S();
15673 correctly. We would treat `S' as a template -- if it were `S<T>'
15674 -- but we do not if there is no `<'. */
15676 if (processing_template_decl
15677 && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
15679 /* In a declaration, in a dependent context, we pretend that the
15680 "template" keyword was present in order to improve error
15681 recovery. For example, given:
15683 template <typename T> void f(T::X<int>);
15685 we want to treat "X<int>" as a template-id. */
15686 if (is_declaration
15687 && !template_keyword_p
15688 && parser->scope && TYPE_P (parser->scope)
15689 && check_dependency_p
15690 && dependent_scope_p (parser->scope)
15691 /* Do not do this for dtors (or ctors), since they never
15692 need the template keyword before their name. */
15693 && !constructor_name_p (identifier, parser->scope))
15695 cp_token_position start = 0;
15697 /* Explain what went wrong. */
15698 error_at (token->location, "non-template %qD used as template",
15699 identifier);
15700 inform (token->location, "use %<%T::template %D%> to indicate that it is a template",
15701 parser->scope, identifier);
15702 /* If parsing tentatively, find the location of the "<" token. */
15703 if (cp_parser_simulate_error (parser))
15704 start = cp_lexer_token_position (parser->lexer, true);
15705 /* Parse the template arguments so that we can issue error
15706 messages about them. */
15707 cp_lexer_consume_token (parser->lexer);
15708 cp_parser_enclosed_template_argument_list (parser);
15709 /* Skip tokens until we find a good place from which to
15710 continue parsing. */
15711 cp_parser_skip_to_closing_parenthesis (parser,
15712 /*recovering=*/true,
15713 /*or_comma=*/true,
15714 /*consume_paren=*/false);
15715 /* If parsing tentatively, permanently remove the
15716 template argument list. That will prevent duplicate
15717 error messages from being issued about the missing
15718 "template" keyword. */
15719 if (start)
15720 cp_lexer_purge_tokens_after (parser->lexer, start);
15721 if (is_identifier)
15722 *is_identifier = true;
15723 parser->context->object_type = NULL_TREE;
15724 return identifier;
15727 /* If the "template" keyword is present, then there is generally
15728 no point in doing name-lookup, so we just return IDENTIFIER.
15729 But, if the qualifying scope is non-dependent then we can
15730 (and must) do name-lookup normally. */
15731 if (template_keyword_p
15732 && (!parser->scope
15733 || (TYPE_P (parser->scope)
15734 && dependent_type_p (parser->scope))))
15736 /* We're optimizing away the call to cp_parser_lookup_name, but we
15737 still need to do this. */
15738 parser->context->object_type = NULL_TREE;
15739 return identifier;
15743 /* Look up the name. */
15744 decl = cp_parser_lookup_name (parser, identifier,
15745 tag_type,
15746 /*is_template=*/true,
15747 /*is_namespace=*/false,
15748 check_dependency_p,
15749 /*ambiguous_decls=*/NULL,
15750 token->location);
15752 decl = strip_using_decl (decl);
15754 /* If DECL is a template, then the name was a template-name. */
15755 if (TREE_CODE (decl) == TEMPLATE_DECL)
15757 if (TREE_DEPRECATED (decl)
15758 && deprecated_state != DEPRECATED_SUPPRESS)
15759 warn_deprecated_use (decl, NULL_TREE);
15761 else
15763 tree fn = NULL_TREE;
15765 /* The standard does not explicitly indicate whether a name that
15766 names a set of overloaded declarations, some of which are
15767 templates, is a template-name. However, such a name should
15768 be a template-name; otherwise, there is no way to form a
15769 template-id for the overloaded templates. */
15770 fns = BASELINK_P (decl) ? BASELINK_FUNCTIONS (decl) : decl;
15771 if (TREE_CODE (fns) == OVERLOAD)
15772 for (fn = fns; fn; fn = OVL_NEXT (fn))
15773 if (TREE_CODE (OVL_CURRENT (fn)) == TEMPLATE_DECL)
15774 break;
15776 if (!fn)
15778 /* The name does not name a template. */
15779 cp_parser_error (parser, "expected template-name");
15780 return error_mark_node;
15784 /* If DECL is dependent, and refers to a function, then just return
15785 its name; we will look it up again during template instantiation. */
15786 if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
15788 tree scope = ovl_scope (decl);
15789 if (TYPE_P (scope) && dependent_type_p (scope))
15790 return identifier;
15793 return decl;
15796 /* Parse a template-argument-list.
15798 template-argument-list:
15799 template-argument ... [opt]
15800 template-argument-list , template-argument ... [opt]
15802 Returns a TREE_VEC containing the arguments. */
15804 static tree
15805 cp_parser_template_argument_list (cp_parser* parser)
15807 tree fixed_args[10];
15808 unsigned n_args = 0;
15809 unsigned alloced = 10;
15810 tree *arg_ary = fixed_args;
15811 tree vec;
15812 bool saved_in_template_argument_list_p;
15813 bool saved_ice_p;
15814 bool saved_non_ice_p;
15816 saved_in_template_argument_list_p = parser->in_template_argument_list_p;
15817 parser->in_template_argument_list_p = true;
15818 /* Even if the template-id appears in an integral
15819 constant-expression, the contents of the argument list do
15820 not. */
15821 saved_ice_p = parser->integral_constant_expression_p;
15822 parser->integral_constant_expression_p = false;
15823 saved_non_ice_p = parser->non_integral_constant_expression_p;
15824 parser->non_integral_constant_expression_p = false;
15826 /* Parse the arguments. */
15829 tree argument;
15831 if (n_args)
15832 /* Consume the comma. */
15833 cp_lexer_consume_token (parser->lexer);
15835 /* Parse the template-argument. */
15836 argument = cp_parser_template_argument (parser);
15838 /* If the next token is an ellipsis, we're expanding a template
15839 argument pack. */
15840 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
15842 if (argument == error_mark_node)
15844 cp_token *token = cp_lexer_peek_token (parser->lexer);
15845 error_at (token->location,
15846 "expected parameter pack before %<...%>");
15848 /* Consume the `...' token. */
15849 cp_lexer_consume_token (parser->lexer);
15851 /* Make the argument into a TYPE_PACK_EXPANSION or
15852 EXPR_PACK_EXPANSION. */
15853 argument = make_pack_expansion (argument);
15856 if (n_args == alloced)
15858 alloced *= 2;
15860 if (arg_ary == fixed_args)
15862 arg_ary = XNEWVEC (tree, alloced);
15863 memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
15865 else
15866 arg_ary = XRESIZEVEC (tree, arg_ary, alloced);
15868 arg_ary[n_args++] = argument;
15870 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
15872 vec = make_tree_vec (n_args);
15874 while (n_args--)
15875 TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
15877 if (arg_ary != fixed_args)
15878 free (arg_ary);
15879 parser->non_integral_constant_expression_p = saved_non_ice_p;
15880 parser->integral_constant_expression_p = saved_ice_p;
15881 parser->in_template_argument_list_p = saved_in_template_argument_list_p;
15882 if (CHECKING_P)
15883 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec, TREE_VEC_LENGTH (vec));
15884 return vec;
15887 /* Parse a template-argument.
15889 template-argument:
15890 assignment-expression
15891 type-id
15892 id-expression
15894 The representation is that of an assignment-expression, type-id, or
15895 id-expression -- except that the qualified id-expression is
15896 evaluated, so that the value returned is either a DECL or an
15897 OVERLOAD.
15899 Although the standard says "assignment-expression", it forbids
15900 throw-expressions or assignments in the template argument.
15901 Therefore, we use "conditional-expression" instead. */
15903 static tree
15904 cp_parser_template_argument (cp_parser* parser)
15906 tree argument;
15907 bool template_p;
15908 bool address_p;
15909 bool maybe_type_id = false;
15910 cp_token *token = NULL, *argument_start_token = NULL;
15911 location_t loc = 0;
15912 cp_id_kind idk;
15914 /* There's really no way to know what we're looking at, so we just
15915 try each alternative in order.
15917 [temp.arg]
15919 In a template-argument, an ambiguity between a type-id and an
15920 expression is resolved to a type-id, regardless of the form of
15921 the corresponding template-parameter.
15923 Therefore, we try a type-id first. */
15924 cp_parser_parse_tentatively (parser);
15925 argument = cp_parser_template_type_arg (parser);
15926 /* If there was no error parsing the type-id but the next token is a
15927 '>>', our behavior depends on which dialect of C++ we're
15928 parsing. In C++98, we probably found a typo for '> >'. But there
15929 are type-id which are also valid expressions. For instance:
15931 struct X { int operator >> (int); };
15932 template <int V> struct Foo {};
15933 Foo<X () >> 5> r;
15935 Here 'X()' is a valid type-id of a function type, but the user just
15936 wanted to write the expression "X() >> 5". Thus, we remember that we
15937 found a valid type-id, but we still try to parse the argument as an
15938 expression to see what happens.
15940 In C++0x, the '>>' will be considered two separate '>'
15941 tokens. */
15942 if (!cp_parser_error_occurred (parser)
15943 && cxx_dialect == cxx98
15944 && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
15946 maybe_type_id = true;
15947 cp_parser_abort_tentative_parse (parser);
15949 else
15951 /* If the next token isn't a `,' or a `>', then this argument wasn't
15952 really finished. This means that the argument is not a valid
15953 type-id. */
15954 if (!cp_parser_next_token_ends_template_argument_p (parser))
15955 cp_parser_error (parser, "expected template-argument");
15956 /* If that worked, we're done. */
15957 if (cp_parser_parse_definitely (parser))
15958 return argument;
15960 /* We're still not sure what the argument will be. */
15961 cp_parser_parse_tentatively (parser);
15962 /* Try a template. */
15963 argument_start_token = cp_lexer_peek_token (parser->lexer);
15964 argument = cp_parser_id_expression (parser,
15965 /*template_keyword_p=*/false,
15966 /*check_dependency_p=*/true,
15967 &template_p,
15968 /*declarator_p=*/false,
15969 /*optional_p=*/false);
15970 /* If the next token isn't a `,' or a `>', then this argument wasn't
15971 really finished. */
15972 if (!cp_parser_next_token_ends_template_argument_p (parser))
15973 cp_parser_error (parser, "expected template-argument");
15974 if (!cp_parser_error_occurred (parser))
15976 /* Figure out what is being referred to. If the id-expression
15977 was for a class template specialization, then we will have a
15978 TYPE_DECL at this point. There is no need to do name lookup
15979 at this point in that case. */
15980 if (TREE_CODE (argument) != TYPE_DECL)
15981 argument = cp_parser_lookup_name (parser, argument,
15982 none_type,
15983 /*is_template=*/template_p,
15984 /*is_namespace=*/false,
15985 /*check_dependency=*/true,
15986 /*ambiguous_decls=*/NULL,
15987 argument_start_token->location);
15988 /* Handle a constrained-type-specifier for a non-type template
15989 parameter. */
15990 if (tree decl = cp_parser_maybe_concept_name (parser, argument))
15991 argument = decl;
15992 else if (TREE_CODE (argument) != TEMPLATE_DECL
15993 && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
15994 cp_parser_error (parser, "expected template-name");
15996 if (cp_parser_parse_definitely (parser))
15998 if (TREE_DEPRECATED (argument))
15999 warn_deprecated_use (argument, NULL_TREE);
16000 return argument;
16002 /* It must be a non-type argument. In C++17 any constant-expression is
16003 allowed. */
16004 if (cxx_dialect > cxx14)
16005 goto general_expr;
16007 /* Otherwise, the permitted cases are given in [temp.arg.nontype]:
16009 -- an integral constant-expression of integral or enumeration
16010 type; or
16012 -- the name of a non-type template-parameter; or
16014 -- the name of an object or function with external linkage...
16016 -- the address of an object or function with external linkage...
16018 -- a pointer to member... */
16019 /* Look for a non-type template parameter. */
16020 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
16022 cp_parser_parse_tentatively (parser);
16023 argument = cp_parser_primary_expression (parser,
16024 /*address_p=*/false,
16025 /*cast_p=*/false,
16026 /*template_arg_p=*/true,
16027 &idk);
16028 if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
16029 || !cp_parser_next_token_ends_template_argument_p (parser))
16030 cp_parser_simulate_error (parser);
16031 if (cp_parser_parse_definitely (parser))
16032 return argument;
16035 /* If the next token is "&", the argument must be the address of an
16036 object or function with external linkage. */
16037 address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
16038 if (address_p)
16040 loc = cp_lexer_peek_token (parser->lexer)->location;
16041 cp_lexer_consume_token (parser->lexer);
16043 /* See if we might have an id-expression. */
16044 token = cp_lexer_peek_token (parser->lexer);
16045 if (token->type == CPP_NAME
16046 || token->keyword == RID_OPERATOR
16047 || token->type == CPP_SCOPE
16048 || token->type == CPP_TEMPLATE_ID
16049 || token->type == CPP_NESTED_NAME_SPECIFIER)
16051 cp_parser_parse_tentatively (parser);
16052 argument = cp_parser_primary_expression (parser,
16053 address_p,
16054 /*cast_p=*/false,
16055 /*template_arg_p=*/true,
16056 &idk);
16057 if (cp_parser_error_occurred (parser)
16058 || !cp_parser_next_token_ends_template_argument_p (parser))
16059 cp_parser_abort_tentative_parse (parser);
16060 else
16062 tree probe;
16064 if (INDIRECT_REF_P (argument))
16066 /* Strip the dereference temporarily. */
16067 gcc_assert (REFERENCE_REF_P (argument));
16068 argument = TREE_OPERAND (argument, 0);
16071 /* If we're in a template, we represent a qualified-id referring
16072 to a static data member as a SCOPE_REF even if the scope isn't
16073 dependent so that we can check access control later. */
16074 probe = argument;
16075 if (TREE_CODE (probe) == SCOPE_REF)
16076 probe = TREE_OPERAND (probe, 1);
16077 if (VAR_P (probe))
16079 /* A variable without external linkage might still be a
16080 valid constant-expression, so no error is issued here
16081 if the external-linkage check fails. */
16082 if (!address_p && !DECL_EXTERNAL_LINKAGE_P (probe))
16083 cp_parser_simulate_error (parser);
16085 else if (is_overloaded_fn (argument))
16086 /* All overloaded functions are allowed; if the external
16087 linkage test does not pass, an error will be issued
16088 later. */
16090 else if (address_p
16091 && (TREE_CODE (argument) == OFFSET_REF
16092 || TREE_CODE (argument) == SCOPE_REF))
16093 /* A pointer-to-member. */
16095 else if (TREE_CODE (argument) == TEMPLATE_PARM_INDEX)
16097 else
16098 cp_parser_simulate_error (parser);
16100 if (cp_parser_parse_definitely (parser))
16102 if (address_p)
16103 argument = build_x_unary_op (loc, ADDR_EXPR, argument,
16104 tf_warning_or_error);
16105 else
16106 argument = convert_from_reference (argument);
16107 return argument;
16111 /* If the argument started with "&", there are no other valid
16112 alternatives at this point. */
16113 if (address_p)
16115 cp_parser_error (parser, "invalid non-type template argument");
16116 return error_mark_node;
16119 general_expr:
16120 /* If the argument wasn't successfully parsed as a type-id followed
16121 by '>>', the argument can only be a constant expression now.
16122 Otherwise, we try parsing the constant-expression tentatively,
16123 because the argument could really be a type-id. */
16124 if (maybe_type_id)
16125 cp_parser_parse_tentatively (parser);
16127 if (cxx_dialect <= cxx14)
16128 argument = cp_parser_constant_expression (parser);
16129 else
16131 /* With C++17 generalized non-type template arguments we need to handle
16132 lvalue constant expressions, too. */
16133 argument = cp_parser_assignment_expression (parser);
16134 require_potential_constant_expression (argument);
16137 if (!maybe_type_id)
16138 return argument;
16139 if (!cp_parser_next_token_ends_template_argument_p (parser))
16140 cp_parser_error (parser, "expected template-argument");
16141 if (cp_parser_parse_definitely (parser))
16142 return argument;
16143 /* We did our best to parse the argument as a non type-id, but that
16144 was the only alternative that matched (albeit with a '>' after
16145 it). We can assume it's just a typo from the user, and a
16146 diagnostic will then be issued. */
16147 return cp_parser_template_type_arg (parser);
16150 /* Parse an explicit-instantiation.
16152 explicit-instantiation:
16153 template declaration
16155 Although the standard says `declaration', what it really means is:
16157 explicit-instantiation:
16158 template decl-specifier-seq [opt] declarator [opt] ;
16160 Things like `template int S<int>::i = 5, int S<double>::j;' are not
16161 supposed to be allowed. A defect report has been filed about this
16162 issue.
16164 GNU Extension:
16166 explicit-instantiation:
16167 storage-class-specifier template
16168 decl-specifier-seq [opt] declarator [opt] ;
16169 function-specifier template
16170 decl-specifier-seq [opt] declarator [opt] ; */
16172 static void
16173 cp_parser_explicit_instantiation (cp_parser* parser)
16175 int declares_class_or_enum;
16176 cp_decl_specifier_seq decl_specifiers;
16177 tree extension_specifier = NULL_TREE;
16179 timevar_push (TV_TEMPLATE_INST);
16181 /* Look for an (optional) storage-class-specifier or
16182 function-specifier. */
16183 if (cp_parser_allow_gnu_extensions_p (parser))
16185 extension_specifier
16186 = cp_parser_storage_class_specifier_opt (parser);
16187 if (!extension_specifier)
16188 extension_specifier
16189 = cp_parser_function_specifier_opt (parser,
16190 /*decl_specs=*/NULL);
16193 /* Look for the `template' keyword. */
16194 cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
16195 /* Let the front end know that we are processing an explicit
16196 instantiation. */
16197 begin_explicit_instantiation ();
16198 /* [temp.explicit] says that we are supposed to ignore access
16199 control while processing explicit instantiation directives. */
16200 push_deferring_access_checks (dk_no_check);
16201 /* Parse a decl-specifier-seq. */
16202 cp_parser_decl_specifier_seq (parser,
16203 CP_PARSER_FLAGS_OPTIONAL,
16204 &decl_specifiers,
16205 &declares_class_or_enum);
16206 /* If there was exactly one decl-specifier, and it declared a class,
16207 and there's no declarator, then we have an explicit type
16208 instantiation. */
16209 if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
16211 tree type;
16213 type = check_tag_decl (&decl_specifiers,
16214 /*explicit_type_instantiation_p=*/true);
16215 /* Turn access control back on for names used during
16216 template instantiation. */
16217 pop_deferring_access_checks ();
16218 if (type)
16219 do_type_instantiation (type, extension_specifier,
16220 /*complain=*/tf_error);
16222 else
16224 cp_declarator *declarator;
16225 tree decl;
16227 /* Parse the declarator. */
16228 declarator
16229 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
16230 /*ctor_dtor_or_conv_p=*/NULL,
16231 /*parenthesized_p=*/NULL,
16232 /*member_p=*/false,
16233 /*friend_p=*/false);
16234 if (declares_class_or_enum & 2)
16235 cp_parser_check_for_definition_in_return_type (declarator,
16236 decl_specifiers.type,
16237 decl_specifiers.locations[ds_type_spec]);
16238 if (declarator != cp_error_declarator)
16240 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_inline))
16241 permerror (decl_specifiers.locations[ds_inline],
16242 "explicit instantiation shall not use"
16243 " %<inline%> specifier");
16244 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_constexpr))
16245 permerror (decl_specifiers.locations[ds_constexpr],
16246 "explicit instantiation shall not use"
16247 " %<constexpr%> specifier");
16249 decl = grokdeclarator (declarator, &decl_specifiers,
16250 NORMAL, 0, &decl_specifiers.attributes);
16251 /* Turn access control back on for names used during
16252 template instantiation. */
16253 pop_deferring_access_checks ();
16254 /* Do the explicit instantiation. */
16255 do_decl_instantiation (decl, extension_specifier);
16257 else
16259 pop_deferring_access_checks ();
16260 /* Skip the body of the explicit instantiation. */
16261 cp_parser_skip_to_end_of_statement (parser);
16264 /* We're done with the instantiation. */
16265 end_explicit_instantiation ();
16267 cp_parser_consume_semicolon_at_end_of_statement (parser);
16269 timevar_pop (TV_TEMPLATE_INST);
16272 /* Parse an explicit-specialization.
16274 explicit-specialization:
16275 template < > declaration
16277 Although the standard says `declaration', what it really means is:
16279 explicit-specialization:
16280 template <> decl-specifier [opt] init-declarator [opt] ;
16281 template <> function-definition
16282 template <> explicit-specialization
16283 template <> template-declaration */
16285 static void
16286 cp_parser_explicit_specialization (cp_parser* parser)
16288 bool need_lang_pop;
16289 cp_token *token = cp_lexer_peek_token (parser->lexer);
16291 /* Look for the `template' keyword. */
16292 cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
16293 /* Look for the `<'. */
16294 cp_parser_require (parser, CPP_LESS, RT_LESS);
16295 /* Look for the `>'. */
16296 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
16297 /* We have processed another parameter list. */
16298 ++parser->num_template_parameter_lists;
16299 /* [temp]
16301 A template ... explicit specialization ... shall not have C
16302 linkage. */
16303 if (current_lang_name == lang_name_c)
16305 error_at (token->location, "template specialization with C linkage");
16306 /* Give it C++ linkage to avoid confusing other parts of the
16307 front end. */
16308 push_lang_context (lang_name_cplusplus);
16309 need_lang_pop = true;
16311 else
16312 need_lang_pop = false;
16313 /* Let the front end know that we are beginning a specialization. */
16314 if (!begin_specialization ())
16316 end_specialization ();
16317 return;
16320 /* If the next keyword is `template', we need to figure out whether
16321 or not we're looking a template-declaration. */
16322 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
16324 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
16325 && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
16326 cp_parser_template_declaration_after_export (parser,
16327 /*member_p=*/false);
16328 else
16329 cp_parser_explicit_specialization (parser);
16331 else
16332 /* Parse the dependent declaration. */
16333 cp_parser_single_declaration (parser,
16334 /*checks=*/NULL,
16335 /*member_p=*/false,
16336 /*explicit_specialization_p=*/true,
16337 /*friend_p=*/NULL);
16338 /* We're done with the specialization. */
16339 end_specialization ();
16340 /* For the erroneous case of a template with C linkage, we pushed an
16341 implicit C++ linkage scope; exit that scope now. */
16342 if (need_lang_pop)
16343 pop_lang_context ();
16344 /* We're done with this parameter list. */
16345 --parser->num_template_parameter_lists;
16348 /* Parse a type-specifier.
16350 type-specifier:
16351 simple-type-specifier
16352 class-specifier
16353 enum-specifier
16354 elaborated-type-specifier
16355 cv-qualifier
16357 GNU Extension:
16359 type-specifier:
16360 __complex__
16362 Returns a representation of the type-specifier. For a
16363 class-specifier, enum-specifier, or elaborated-type-specifier, a
16364 TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
16366 The parser flags FLAGS is used to control type-specifier parsing.
16368 If IS_DECLARATION is TRUE, then this type-specifier is appearing
16369 in a decl-specifier-seq.
16371 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
16372 class-specifier, enum-specifier, or elaborated-type-specifier, then
16373 *DECLARES_CLASS_OR_ENUM is set to a nonzero value. The value is 1
16374 if a type is declared; 2 if it is defined. Otherwise, it is set to
16375 zero.
16377 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
16378 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
16379 is set to FALSE. */
16381 static tree
16382 cp_parser_type_specifier (cp_parser* parser,
16383 cp_parser_flags flags,
16384 cp_decl_specifier_seq *decl_specs,
16385 bool is_declaration,
16386 int* declares_class_or_enum,
16387 bool* is_cv_qualifier)
16389 tree type_spec = NULL_TREE;
16390 cp_token *token;
16391 enum rid keyword;
16392 cp_decl_spec ds = ds_last;
16394 /* Assume this type-specifier does not declare a new type. */
16395 if (declares_class_or_enum)
16396 *declares_class_or_enum = 0;
16397 /* And that it does not specify a cv-qualifier. */
16398 if (is_cv_qualifier)
16399 *is_cv_qualifier = false;
16400 /* Peek at the next token. */
16401 token = cp_lexer_peek_token (parser->lexer);
16403 /* If we're looking at a keyword, we can use that to guide the
16404 production we choose. */
16405 keyword = token->keyword;
16406 switch (keyword)
16408 case RID_ENUM:
16409 if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
16410 goto elaborated_type_specifier;
16412 /* Look for the enum-specifier. */
16413 type_spec = cp_parser_enum_specifier (parser);
16414 /* If that worked, we're done. */
16415 if (type_spec)
16417 if (declares_class_or_enum)
16418 *declares_class_or_enum = 2;
16419 if (decl_specs)
16420 cp_parser_set_decl_spec_type (decl_specs,
16421 type_spec,
16422 token,
16423 /*type_definition_p=*/true);
16424 return type_spec;
16426 else
16427 goto elaborated_type_specifier;
16429 /* Any of these indicate either a class-specifier, or an
16430 elaborated-type-specifier. */
16431 case RID_CLASS:
16432 case RID_STRUCT:
16433 case RID_UNION:
16434 if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
16435 goto elaborated_type_specifier;
16437 /* Parse tentatively so that we can back up if we don't find a
16438 class-specifier. */
16439 cp_parser_parse_tentatively (parser);
16440 /* Look for the class-specifier. */
16441 type_spec = cp_parser_class_specifier (parser);
16442 invoke_plugin_callbacks (PLUGIN_FINISH_TYPE, type_spec);
16443 /* If that worked, we're done. */
16444 if (cp_parser_parse_definitely (parser))
16446 if (declares_class_or_enum)
16447 *declares_class_or_enum = 2;
16448 if (decl_specs)
16449 cp_parser_set_decl_spec_type (decl_specs,
16450 type_spec,
16451 token,
16452 /*type_definition_p=*/true);
16453 return type_spec;
16456 /* Fall through. */
16457 elaborated_type_specifier:
16458 /* We're declaring (not defining) a class or enum. */
16459 if (declares_class_or_enum)
16460 *declares_class_or_enum = 1;
16462 /* Fall through. */
16463 case RID_TYPENAME:
16464 /* Look for an elaborated-type-specifier. */
16465 type_spec
16466 = (cp_parser_elaborated_type_specifier
16467 (parser,
16468 decl_spec_seq_has_spec_p (decl_specs, ds_friend),
16469 is_declaration));
16470 if (decl_specs)
16471 cp_parser_set_decl_spec_type (decl_specs,
16472 type_spec,
16473 token,
16474 /*type_definition_p=*/false);
16475 return type_spec;
16477 case RID_CONST:
16478 ds = ds_const;
16479 if (is_cv_qualifier)
16480 *is_cv_qualifier = true;
16481 break;
16483 case RID_VOLATILE:
16484 ds = ds_volatile;
16485 if (is_cv_qualifier)
16486 *is_cv_qualifier = true;
16487 break;
16489 case RID_RESTRICT:
16490 ds = ds_restrict;
16491 if (is_cv_qualifier)
16492 *is_cv_qualifier = true;
16493 break;
16495 case RID_COMPLEX:
16496 /* The `__complex__' keyword is a GNU extension. */
16497 ds = ds_complex;
16498 break;
16500 default:
16501 break;
16504 /* Handle simple keywords. */
16505 if (ds != ds_last)
16507 if (decl_specs)
16509 set_and_check_decl_spec_loc (decl_specs, ds, token);
16510 decl_specs->any_specifiers_p = true;
16512 return cp_lexer_consume_token (parser->lexer)->u.value;
16515 /* If we do not already have a type-specifier, assume we are looking
16516 at a simple-type-specifier. */
16517 type_spec = cp_parser_simple_type_specifier (parser,
16518 decl_specs,
16519 flags);
16521 /* If we didn't find a type-specifier, and a type-specifier was not
16522 optional in this context, issue an error message. */
16523 if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
16525 cp_parser_error (parser, "expected type specifier");
16526 return error_mark_node;
16529 return type_spec;
16532 /* Parse a simple-type-specifier.
16534 simple-type-specifier:
16535 :: [opt] nested-name-specifier [opt] type-name
16536 :: [opt] nested-name-specifier template template-id
16537 char
16538 wchar_t
16539 bool
16540 short
16542 long
16543 signed
16544 unsigned
16545 float
16546 double
16547 void
16549 C++11 Extension:
16551 simple-type-specifier:
16552 auto
16553 decltype ( expression )
16554 char16_t
16555 char32_t
16556 __underlying_type ( type-id )
16558 C++17 extension:
16560 nested-name-specifier(opt) template-name
16562 GNU Extension:
16564 simple-type-specifier:
16565 __int128
16566 __typeof__ unary-expression
16567 __typeof__ ( type-id )
16568 __typeof__ ( type-id ) { initializer-list , [opt] }
16570 Concepts Extension:
16572 simple-type-specifier:
16573 constrained-type-specifier
16575 Returns the indicated TYPE_DECL. If DECL_SPECS is not NULL, it is
16576 appropriately updated. */
16578 static tree
16579 cp_parser_simple_type_specifier (cp_parser* parser,
16580 cp_decl_specifier_seq *decl_specs,
16581 cp_parser_flags flags)
16583 tree type = NULL_TREE;
16584 cp_token *token;
16585 int idx;
16587 /* Peek at the next token. */
16588 token = cp_lexer_peek_token (parser->lexer);
16590 /* If we're looking at a keyword, things are easy. */
16591 switch (token->keyword)
16593 case RID_CHAR:
16594 if (decl_specs)
16595 decl_specs->explicit_char_p = true;
16596 type = char_type_node;
16597 break;
16598 case RID_CHAR16:
16599 type = char16_type_node;
16600 break;
16601 case RID_CHAR32:
16602 type = char32_type_node;
16603 break;
16604 case RID_WCHAR:
16605 type = wchar_type_node;
16606 break;
16607 case RID_BOOL:
16608 type = boolean_type_node;
16609 break;
16610 case RID_SHORT:
16611 set_and_check_decl_spec_loc (decl_specs, ds_short, token);
16612 type = short_integer_type_node;
16613 break;
16614 case RID_INT:
16615 if (decl_specs)
16616 decl_specs->explicit_int_p = true;
16617 type = integer_type_node;
16618 break;
16619 case RID_INT_N_0:
16620 case RID_INT_N_1:
16621 case RID_INT_N_2:
16622 case RID_INT_N_3:
16623 idx = token->keyword - RID_INT_N_0;
16624 if (! int_n_enabled_p [idx])
16625 break;
16626 if (decl_specs)
16628 decl_specs->explicit_intN_p = true;
16629 decl_specs->int_n_idx = idx;
16631 type = int_n_trees [idx].signed_type;
16632 break;
16633 case RID_LONG:
16634 if (decl_specs)
16635 set_and_check_decl_spec_loc (decl_specs, ds_long, token);
16636 type = long_integer_type_node;
16637 break;
16638 case RID_SIGNED:
16639 set_and_check_decl_spec_loc (decl_specs, ds_signed, token);
16640 type = integer_type_node;
16641 break;
16642 case RID_UNSIGNED:
16643 set_and_check_decl_spec_loc (decl_specs, ds_unsigned, token);
16644 type = unsigned_type_node;
16645 break;
16646 case RID_FLOAT:
16647 type = float_type_node;
16648 break;
16649 case RID_DOUBLE:
16650 type = double_type_node;
16651 break;
16652 case RID_VOID:
16653 type = void_type_node;
16654 break;
16656 case RID_AUTO:
16657 maybe_warn_cpp0x (CPP0X_AUTO);
16658 if (parser->auto_is_implicit_function_template_parm_p)
16660 /* The 'auto' might be the placeholder return type for a function decl
16661 with trailing return type. */
16662 bool have_trailing_return_fn_decl = false;
16664 cp_parser_parse_tentatively (parser);
16665 cp_lexer_consume_token (parser->lexer);
16666 while (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
16667 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA)
16668 && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
16669 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
16671 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
16673 cp_lexer_consume_token (parser->lexer);
16674 cp_parser_skip_to_closing_parenthesis (parser,
16675 /*recovering*/false,
16676 /*or_comma*/false,
16677 /*consume_paren*/true);
16678 continue;
16681 if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
16683 have_trailing_return_fn_decl = true;
16684 break;
16687 cp_lexer_consume_token (parser->lexer);
16689 cp_parser_abort_tentative_parse (parser);
16691 if (have_trailing_return_fn_decl)
16693 type = make_auto ();
16694 break;
16697 if (cxx_dialect >= cxx14)
16699 type = synthesize_implicit_template_parm (parser, NULL_TREE);
16700 type = TREE_TYPE (type);
16702 else
16703 type = error_mark_node;
16705 if (current_class_type && LAMBDA_TYPE_P (current_class_type))
16707 if (cxx_dialect < cxx14)
16708 error_at (token->location,
16709 "use of %<auto%> in lambda parameter declaration "
16710 "only available with "
16711 "-std=c++14 or -std=gnu++14");
16713 else if (cxx_dialect < cxx14)
16714 error_at (token->location,
16715 "use of %<auto%> in parameter declaration "
16716 "only available with "
16717 "-std=c++14 or -std=gnu++14");
16718 else if (!flag_concepts)
16719 pedwarn (token->location, OPT_Wpedantic,
16720 "ISO C++ forbids use of %<auto%> in parameter "
16721 "declaration");
16723 else
16724 type = make_auto ();
16725 break;
16727 case RID_DECLTYPE:
16728 /* Since DR 743, decltype can either be a simple-type-specifier by
16729 itself or begin a nested-name-specifier. Parsing it will replace
16730 it with a CPP_DECLTYPE, so just rewind and let the CPP_DECLTYPE
16731 handling below decide what to do. */
16732 cp_parser_decltype (parser);
16733 cp_lexer_set_token_position (parser->lexer, token);
16734 break;
16736 case RID_TYPEOF:
16737 /* Consume the `typeof' token. */
16738 cp_lexer_consume_token (parser->lexer);
16739 /* Parse the operand to `typeof'. */
16740 type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
16741 /* If it is not already a TYPE, take its type. */
16742 if (!TYPE_P (type))
16743 type = finish_typeof (type);
16745 if (decl_specs)
16746 cp_parser_set_decl_spec_type (decl_specs, type,
16747 token,
16748 /*type_definition_p=*/false);
16750 return type;
16752 case RID_UNDERLYING_TYPE:
16753 type = cp_parser_trait_expr (parser, RID_UNDERLYING_TYPE);
16754 if (decl_specs)
16755 cp_parser_set_decl_spec_type (decl_specs, type,
16756 token,
16757 /*type_definition_p=*/false);
16759 return type;
16761 case RID_BASES:
16762 case RID_DIRECT_BASES:
16763 type = cp_parser_trait_expr (parser, token->keyword);
16764 if (decl_specs)
16765 cp_parser_set_decl_spec_type (decl_specs, type,
16766 token,
16767 /*type_definition_p=*/false);
16768 return type;
16769 default:
16770 break;
16773 /* If token is an already-parsed decltype not followed by ::,
16774 it's a simple-type-specifier. */
16775 if (token->type == CPP_DECLTYPE
16776 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
16778 type = saved_checks_value (token->u.tree_check_value);
16779 if (decl_specs)
16781 cp_parser_set_decl_spec_type (decl_specs, type,
16782 token,
16783 /*type_definition_p=*/false);
16784 /* Remember that we are handling a decltype in order to
16785 implement the resolution of DR 1510 when the argument
16786 isn't instantiation dependent. */
16787 decl_specs->decltype_p = true;
16789 cp_lexer_consume_token (parser->lexer);
16790 return type;
16793 /* If the type-specifier was for a built-in type, we're done. */
16794 if (type)
16796 /* Record the type. */
16797 if (decl_specs
16798 && (token->keyword != RID_SIGNED
16799 && token->keyword != RID_UNSIGNED
16800 && token->keyword != RID_SHORT
16801 && token->keyword != RID_LONG))
16802 cp_parser_set_decl_spec_type (decl_specs,
16803 type,
16804 token,
16805 /*type_definition_p=*/false);
16806 if (decl_specs)
16807 decl_specs->any_specifiers_p = true;
16809 /* Consume the token. */
16810 cp_lexer_consume_token (parser->lexer);
16812 if (type == error_mark_node)
16813 return error_mark_node;
16815 /* There is no valid C++ program where a non-template type is
16816 followed by a "<". That usually indicates that the user thought
16817 that the type was a template. */
16818 cp_parser_check_for_invalid_template_id (parser, type, none_type,
16819 token->location);
16821 return TYPE_NAME (type);
16824 /* The type-specifier must be a user-defined type. */
16825 if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
16827 bool qualified_p;
16828 bool global_p;
16830 /* Don't gobble tokens or issue error messages if this is an
16831 optional type-specifier. */
16832 if ((flags & CP_PARSER_FLAGS_OPTIONAL) || cxx_dialect >= cxx1z)
16833 cp_parser_parse_tentatively (parser);
16835 token = cp_lexer_peek_token (parser->lexer);
16837 /* Look for the optional `::' operator. */
16838 global_p
16839 = (cp_parser_global_scope_opt (parser,
16840 /*current_scope_valid_p=*/false)
16841 != NULL_TREE);
16842 /* Look for the nested-name specifier. */
16843 qualified_p
16844 = (cp_parser_nested_name_specifier_opt (parser,
16845 /*typename_keyword_p=*/false,
16846 /*check_dependency_p=*/true,
16847 /*type_p=*/false,
16848 /*is_declaration=*/false)
16849 != NULL_TREE);
16850 /* If we have seen a nested-name-specifier, and the next token
16851 is `template', then we are using the template-id production. */
16852 if (parser->scope
16853 && cp_parser_optional_template_keyword (parser))
16855 /* Look for the template-id. */
16856 type = cp_parser_template_id (parser,
16857 /*template_keyword_p=*/true,
16858 /*check_dependency_p=*/true,
16859 none_type,
16860 /*is_declaration=*/false);
16861 /* If the template-id did not name a type, we are out of
16862 luck. */
16863 if (TREE_CODE (type) != TYPE_DECL)
16865 cp_parser_error (parser, "expected template-id for type");
16866 type = NULL_TREE;
16869 /* Otherwise, look for a type-name. */
16870 else
16871 type = cp_parser_type_name (parser);
16872 /* Keep track of all name-lookups performed in class scopes. */
16873 if (type
16874 && !global_p
16875 && !qualified_p
16876 && TREE_CODE (type) == TYPE_DECL
16877 && identifier_p (DECL_NAME (type)))
16878 maybe_note_name_used_in_class (DECL_NAME (type), type);
16879 /* If it didn't work out, we don't have a TYPE. */
16880 if (((flags & CP_PARSER_FLAGS_OPTIONAL) || cxx_dialect >= cxx1z)
16881 && !cp_parser_parse_definitely (parser))
16882 type = NULL_TREE;
16883 if (!type && cxx_dialect >= cxx1z)
16885 if (flags & CP_PARSER_FLAGS_OPTIONAL)
16886 cp_parser_parse_tentatively (parser);
16888 cp_parser_global_scope_opt (parser,
16889 /*current_scope_valid_p=*/false);
16890 cp_parser_nested_name_specifier_opt (parser,
16891 /*typename_keyword_p=*/false,
16892 /*check_dependency_p=*/true,
16893 /*type_p=*/false,
16894 /*is_declaration=*/false);
16895 tree name = cp_parser_identifier (parser);
16896 if (name && TREE_CODE (name) == IDENTIFIER_NODE
16897 && parser->scope != error_mark_node)
16899 tree tmpl = cp_parser_lookup_name (parser, name,
16900 none_type,
16901 /*is_template=*/false,
16902 /*is_namespace=*/false,
16903 /*check_dependency=*/true,
16904 /*ambiguous_decls=*/NULL,
16905 token->location);
16906 if (tmpl && tmpl != error_mark_node
16907 && (DECL_CLASS_TEMPLATE_P (tmpl)
16908 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl)))
16909 type = make_template_placeholder (tmpl);
16910 else
16912 type = error_mark_node;
16913 if (!cp_parser_simulate_error (parser))
16914 cp_parser_name_lookup_error (parser, name, tmpl,
16915 NLE_TYPE, token->location);
16918 else
16919 type = error_mark_node;
16921 if ((flags & CP_PARSER_FLAGS_OPTIONAL)
16922 && !cp_parser_parse_definitely (parser))
16923 type = NULL_TREE;
16925 if (type && decl_specs)
16926 cp_parser_set_decl_spec_type (decl_specs, type,
16927 token,
16928 /*type_definition_p=*/false);
16931 /* If we didn't get a type-name, issue an error message. */
16932 if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
16934 cp_parser_error (parser, "expected type-name");
16935 return error_mark_node;
16938 if (type && type != error_mark_node)
16940 /* See if TYPE is an Objective-C type, and if so, parse and
16941 accept any protocol references following it. Do this before
16942 the cp_parser_check_for_invalid_template_id() call, because
16943 Objective-C types can be followed by '<...>' which would
16944 enclose protocol names rather than template arguments, and so
16945 everything is fine. */
16946 if (c_dialect_objc () && !parser->scope
16947 && (objc_is_id (type) || objc_is_class_name (type)))
16949 tree protos = cp_parser_objc_protocol_refs_opt (parser);
16950 tree qual_type = objc_get_protocol_qualified_type (type, protos);
16952 /* Clobber the "unqualified" type previously entered into
16953 DECL_SPECS with the new, improved protocol-qualified version. */
16954 if (decl_specs)
16955 decl_specs->type = qual_type;
16957 return qual_type;
16960 /* There is no valid C++ program where a non-template type is
16961 followed by a "<". That usually indicates that the user
16962 thought that the type was a template. */
16963 cp_parser_check_for_invalid_template_id (parser, TREE_TYPE (type),
16964 none_type,
16965 token->location);
16968 return type;
16971 /* Parse a type-name.
16973 type-name:
16974 class-name
16975 enum-name
16976 typedef-name
16977 simple-template-id [in c++0x]
16979 enum-name:
16980 identifier
16982 typedef-name:
16983 identifier
16985 Concepts:
16987 type-name:
16988 concept-name
16989 partial-concept-id
16991 concept-name:
16992 identifier
16994 Returns a TYPE_DECL for the type. */
16996 static tree
16997 cp_parser_type_name (cp_parser* parser)
16999 return cp_parser_type_name (parser, /*typename_keyword_p=*/false);
17002 /* See above. */
17003 static tree
17004 cp_parser_type_name (cp_parser* parser, bool typename_keyword_p)
17006 tree type_decl;
17008 /* We can't know yet whether it is a class-name or not. */
17009 cp_parser_parse_tentatively (parser);
17010 /* Try a class-name. */
17011 type_decl = cp_parser_class_name (parser,
17012 typename_keyword_p,
17013 /*template_keyword_p=*/false,
17014 none_type,
17015 /*check_dependency_p=*/true,
17016 /*class_head_p=*/false,
17017 /*is_declaration=*/false);
17018 /* If it's not a class-name, keep looking. */
17019 if (!cp_parser_parse_definitely (parser))
17021 if (cxx_dialect < cxx11)
17022 /* It must be a typedef-name or an enum-name. */
17023 return cp_parser_nonclass_name (parser);
17025 cp_parser_parse_tentatively (parser);
17026 /* It is either a simple-template-id representing an
17027 instantiation of an alias template... */
17028 type_decl = cp_parser_template_id (parser,
17029 /*template_keyword_p=*/false,
17030 /*check_dependency_p=*/true,
17031 none_type,
17032 /*is_declaration=*/false);
17033 /* Note that this must be an instantiation of an alias template
17034 because [temp.names]/6 says:
17036 A template-id that names an alias template specialization
17037 is a type-name.
17039 Whereas [temp.names]/7 says:
17041 A simple-template-id that names a class template
17042 specialization is a class-name.
17044 With concepts, this could also be a partial-concept-id that
17045 declares a non-type template parameter. */
17046 if (type_decl != NULL_TREE
17047 && TREE_CODE (type_decl) == TYPE_DECL
17048 && TYPE_DECL_ALIAS_P (type_decl))
17049 gcc_assert (DECL_TEMPLATE_INSTANTIATION (type_decl));
17050 else if (is_constrained_parameter (type_decl))
17051 /* Don't do anything. */ ;
17052 else
17053 cp_parser_simulate_error (parser);
17055 if (!cp_parser_parse_definitely (parser))
17056 /* ... Or a typedef-name or an enum-name. */
17057 return cp_parser_nonclass_name (parser);
17060 return type_decl;
17063 /* Check if DECL and ARGS can form a constrained-type-specifier.
17064 If ARGS is non-null, we try to form a concept check of the
17065 form DECL<?, ARGS> where ? is a wildcard that matches any
17066 kind of template argument. If ARGS is NULL, then we try to
17067 form a concept check of the form DECL<?>. */
17069 static tree
17070 cp_parser_maybe_constrained_type_specifier (cp_parser *parser,
17071 tree decl, tree args)
17073 gcc_assert (args ? TREE_CODE (args) == TREE_VEC : true);
17075 /* If we a constrained-type-specifier cannot be deduced. */
17076 if (parser->prevent_constrained_type_specifiers)
17077 return NULL_TREE;
17079 /* A constrained type specifier can only be found in an
17080 overload set or as a reference to a template declaration.
17082 FIXME: This might be masking a bug. It's possible that
17083 that the deduction below is causing template specializations
17084 to be formed with the wildcard as an argument. */
17085 if (TREE_CODE (decl) != OVERLOAD && TREE_CODE (decl) != TEMPLATE_DECL)
17086 return NULL_TREE;
17088 /* Try to build a call expression that evaluates the
17089 concept. This can fail if the overload set refers
17090 only to non-templates. */
17091 tree placeholder = build_nt (WILDCARD_DECL);
17092 tree check = build_concept_check (decl, placeholder, args);
17093 if (check == error_mark_node)
17094 return NULL_TREE;
17096 /* Deduce the checked constraint and the prototype parameter.
17098 FIXME: In certain cases, failure to deduce should be a
17099 diagnosable error. */
17100 tree conc;
17101 tree proto;
17102 if (!deduce_constrained_parameter (check, conc, proto))
17103 return NULL_TREE;
17105 /* In template parameter scope, this results in a constrained
17106 parameter. Return a descriptor of that parm. */
17107 if (processing_template_parmlist)
17108 return build_constrained_parameter (conc, proto, args);
17110 /* In a parameter-declaration-clause, constrained-type
17111 specifiers result in invented template parameters. */
17112 if (parser->auto_is_implicit_function_template_parm_p)
17114 tree x = build_constrained_parameter (conc, proto, args);
17115 return synthesize_implicit_template_parm (parser, x);
17117 else
17119 /* Otherwise, we're in a context where the constrained
17120 type name is deduced and the constraint applies
17121 after deduction. */
17122 return make_constrained_auto (conc, args);
17125 return NULL_TREE;
17128 /* If DECL refers to a concept, return a TYPE_DECL representing
17129 the result of using the constrained type specifier in the
17130 current context. DECL refers to a concept if
17132 - it is an overload set containing a function concept taking a single
17133 type argument, or
17135 - it is a variable concept taking a single type argument. */
17137 static tree
17138 cp_parser_maybe_concept_name (cp_parser* parser, tree decl)
17140 if (flag_concepts
17141 && (TREE_CODE (decl) == OVERLOAD
17142 || BASELINK_P (decl)
17143 || variable_concept_p (decl)))
17144 return cp_parser_maybe_constrained_type_specifier (parser, decl, NULL_TREE);
17145 else
17146 return NULL_TREE;
17149 /* Check if DECL and ARGS form a partial-concept-id. If so,
17150 assign ID to the resulting constrained placeholder.
17152 Returns true if the partial-concept-id designates a placeholder
17153 and false otherwise. Note that *id is set to NULL_TREE in
17154 this case. */
17156 static tree
17157 cp_parser_maybe_partial_concept_id (cp_parser *parser, tree decl, tree args)
17159 return cp_parser_maybe_constrained_type_specifier (parser, decl, args);
17162 /* Parse a non-class type-name, that is, either an enum-name, a typedef-name,
17163 or a concept-name.
17165 enum-name:
17166 identifier
17168 typedef-name:
17169 identifier
17171 concept-name:
17172 identifier
17174 Returns a TYPE_DECL for the type. */
17176 static tree
17177 cp_parser_nonclass_name (cp_parser* parser)
17179 tree type_decl;
17180 tree identifier;
17182 cp_token *token = cp_lexer_peek_token (parser->lexer);
17183 identifier = cp_parser_identifier (parser);
17184 if (identifier == error_mark_node)
17185 return error_mark_node;
17187 /* Look up the type-name. */
17188 type_decl = cp_parser_lookup_name_simple (parser, identifier, token->location);
17190 type_decl = strip_using_decl (type_decl);
17192 /* If we found an overload set, then it may refer to a concept-name. */
17193 if (tree decl = cp_parser_maybe_concept_name (parser, type_decl))
17194 type_decl = decl;
17196 if (TREE_CODE (type_decl) != TYPE_DECL
17197 && (objc_is_id (identifier) || objc_is_class_name (identifier)))
17199 /* See if this is an Objective-C type. */
17200 tree protos = cp_parser_objc_protocol_refs_opt (parser);
17201 tree type = objc_get_protocol_qualified_type (identifier, protos);
17202 if (type)
17203 type_decl = TYPE_NAME (type);
17206 /* Issue an error if we did not find a type-name. */
17207 if (TREE_CODE (type_decl) != TYPE_DECL
17208 /* In Objective-C, we have the complication that class names are
17209 normally type names and start declarations (eg, the
17210 "NSObject" in "NSObject *object;"), but can be used in an
17211 Objective-C 2.0 dot-syntax (as in "NSObject.version") which
17212 is an expression. So, a classname followed by a dot is not a
17213 valid type-name. */
17214 || (objc_is_class_name (TREE_TYPE (type_decl))
17215 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT))
17217 if (!cp_parser_simulate_error (parser))
17218 cp_parser_name_lookup_error (parser, identifier, type_decl,
17219 NLE_TYPE, token->location);
17220 return error_mark_node;
17222 /* Remember that the name was used in the definition of the
17223 current class so that we can check later to see if the
17224 meaning would have been different after the class was
17225 entirely defined. */
17226 else if (type_decl != error_mark_node
17227 && !parser->scope)
17228 maybe_note_name_used_in_class (identifier, type_decl);
17230 return type_decl;
17233 /* Parse an elaborated-type-specifier. Note that the grammar given
17234 here incorporates the resolution to DR68.
17236 elaborated-type-specifier:
17237 class-key :: [opt] nested-name-specifier [opt] identifier
17238 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
17239 enum-key :: [opt] nested-name-specifier [opt] identifier
17240 typename :: [opt] nested-name-specifier identifier
17241 typename :: [opt] nested-name-specifier template [opt]
17242 template-id
17244 GNU extension:
17246 elaborated-type-specifier:
17247 class-key attributes :: [opt] nested-name-specifier [opt] identifier
17248 class-key attributes :: [opt] nested-name-specifier [opt]
17249 template [opt] template-id
17250 enum attributes :: [opt] nested-name-specifier [opt] identifier
17252 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
17253 declared `friend'. If IS_DECLARATION is TRUE, then this
17254 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
17255 something is being declared.
17257 Returns the TYPE specified. */
17259 static tree
17260 cp_parser_elaborated_type_specifier (cp_parser* parser,
17261 bool is_friend,
17262 bool is_declaration)
17264 enum tag_types tag_type;
17265 tree identifier;
17266 tree type = NULL_TREE;
17267 tree attributes = NULL_TREE;
17268 tree globalscope;
17269 cp_token *token = NULL;
17271 /* See if we're looking at the `enum' keyword. */
17272 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
17274 /* Consume the `enum' token. */
17275 cp_lexer_consume_token (parser->lexer);
17276 /* Remember that it's an enumeration type. */
17277 tag_type = enum_type;
17278 /* Issue a warning if the `struct' or `class' key (for C++0x scoped
17279 enums) is used here. */
17280 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
17281 || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
17283 pedwarn (input_location, 0, "elaborated-type-specifier "
17284 "for a scoped enum must not use the %<%D%> keyword",
17285 cp_lexer_peek_token (parser->lexer)->u.value);
17286 /* Consume the `struct' or `class' and parse it anyway. */
17287 cp_lexer_consume_token (parser->lexer);
17289 /* Parse the attributes. */
17290 attributes = cp_parser_attributes_opt (parser);
17292 /* Or, it might be `typename'. */
17293 else if (cp_lexer_next_token_is_keyword (parser->lexer,
17294 RID_TYPENAME))
17296 /* Consume the `typename' token. */
17297 cp_lexer_consume_token (parser->lexer);
17298 /* Remember that it's a `typename' type. */
17299 tag_type = typename_type;
17301 /* Otherwise it must be a class-key. */
17302 else
17304 tag_type = cp_parser_class_key (parser);
17305 if (tag_type == none_type)
17306 return error_mark_node;
17307 /* Parse the attributes. */
17308 attributes = cp_parser_attributes_opt (parser);
17311 /* Look for the `::' operator. */
17312 globalscope = cp_parser_global_scope_opt (parser,
17313 /*current_scope_valid_p=*/false);
17314 /* Look for the nested-name-specifier. */
17315 tree nested_name_specifier;
17316 if (tag_type == typename_type && !globalscope)
17318 nested_name_specifier
17319 = cp_parser_nested_name_specifier (parser,
17320 /*typename_keyword_p=*/true,
17321 /*check_dependency_p=*/true,
17322 /*type_p=*/true,
17323 is_declaration);
17324 if (!nested_name_specifier)
17325 return error_mark_node;
17327 else
17328 /* Even though `typename' is not present, the proposed resolution
17329 to Core Issue 180 says that in `class A<T>::B', `B' should be
17330 considered a type-name, even if `A<T>' is dependent. */
17331 nested_name_specifier
17332 = cp_parser_nested_name_specifier_opt (parser,
17333 /*typename_keyword_p=*/true,
17334 /*check_dependency_p=*/true,
17335 /*type_p=*/true,
17336 is_declaration);
17337 /* For everything but enumeration types, consider a template-id.
17338 For an enumeration type, consider only a plain identifier. */
17339 if (tag_type != enum_type)
17341 bool template_p = false;
17342 tree decl;
17344 /* Allow the `template' keyword. */
17345 template_p = cp_parser_optional_template_keyword (parser);
17346 /* If we didn't see `template', we don't know if there's a
17347 template-id or not. */
17348 if (!template_p)
17349 cp_parser_parse_tentatively (parser);
17350 /* Parse the template-id. */
17351 token = cp_lexer_peek_token (parser->lexer);
17352 decl = cp_parser_template_id (parser, template_p,
17353 /*check_dependency_p=*/true,
17354 tag_type,
17355 is_declaration);
17356 /* If we didn't find a template-id, look for an ordinary
17357 identifier. */
17358 if (!template_p && !cp_parser_parse_definitely (parser))
17360 /* We can get here when cp_parser_template_id, called by
17361 cp_parser_class_name with tag_type == none_type, succeeds
17362 and caches a BASELINK. Then, when called again here,
17363 instead of failing and returning an error_mark_node
17364 returns it (see template/typename17.C in C++11).
17365 ??? Could we diagnose this earlier? */
17366 else if (tag_type == typename_type && BASELINK_P (decl))
17368 cp_parser_diagnose_invalid_type_name (parser, decl, token->location);
17369 type = error_mark_node;
17371 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
17372 in effect, then we must assume that, upon instantiation, the
17373 template will correspond to a class. */
17374 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
17375 && tag_type == typename_type)
17376 type = make_typename_type (parser->scope, decl,
17377 typename_type,
17378 /*complain=*/tf_error);
17379 /* If the `typename' keyword is in effect and DECL is not a type
17380 decl, then type is non existent. */
17381 else if (tag_type == typename_type && TREE_CODE (decl) != TYPE_DECL)
17383 else if (TREE_CODE (decl) == TYPE_DECL)
17385 type = check_elaborated_type_specifier (tag_type, decl,
17386 /*allow_template_p=*/true);
17388 /* If the next token is a semicolon, this must be a specialization,
17389 instantiation, or friend declaration. Check the scope while we
17390 still know whether or not we had a nested-name-specifier. */
17391 if (type != error_mark_node
17392 && !nested_name_specifier && !is_friend
17393 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
17394 check_unqualified_spec_or_inst (type, token->location);
17396 else if (decl == error_mark_node)
17397 type = error_mark_node;
17400 if (!type)
17402 token = cp_lexer_peek_token (parser->lexer);
17403 identifier = cp_parser_identifier (parser);
17405 if (identifier == error_mark_node)
17407 parser->scope = NULL_TREE;
17408 return error_mark_node;
17411 /* For a `typename', we needn't call xref_tag. */
17412 if (tag_type == typename_type
17413 && TREE_CODE (parser->scope) != NAMESPACE_DECL)
17414 return cp_parser_make_typename_type (parser, identifier,
17415 token->location);
17417 /* Template parameter lists apply only if we are not within a
17418 function parameter list. */
17419 bool template_parm_lists_apply
17420 = parser->num_template_parameter_lists;
17421 if (template_parm_lists_apply)
17422 for (cp_binding_level *s = current_binding_level;
17423 s && s->kind != sk_template_parms;
17424 s = s->level_chain)
17425 if (s->kind == sk_function_parms)
17426 template_parm_lists_apply = false;
17428 /* Look up a qualified name in the usual way. */
17429 if (parser->scope)
17431 tree decl;
17432 tree ambiguous_decls;
17434 decl = cp_parser_lookup_name (parser, identifier,
17435 tag_type,
17436 /*is_template=*/false,
17437 /*is_namespace=*/false,
17438 /*check_dependency=*/true,
17439 &ambiguous_decls,
17440 token->location);
17442 /* If the lookup was ambiguous, an error will already have been
17443 issued. */
17444 if (ambiguous_decls)
17445 return error_mark_node;
17447 /* If we are parsing friend declaration, DECL may be a
17448 TEMPLATE_DECL tree node here. However, we need to check
17449 whether this TEMPLATE_DECL results in valid code. Consider
17450 the following example:
17452 namespace N {
17453 template <class T> class C {};
17455 class X {
17456 template <class T> friend class N::C; // #1, valid code
17458 template <class T> class Y {
17459 friend class N::C; // #2, invalid code
17462 For both case #1 and #2, we arrive at a TEMPLATE_DECL after
17463 name lookup of `N::C'. We see that friend declaration must
17464 be template for the code to be valid. Note that
17465 processing_template_decl does not work here since it is
17466 always 1 for the above two cases. */
17468 decl = (cp_parser_maybe_treat_template_as_class
17469 (decl, /*tag_name_p=*/is_friend
17470 && template_parm_lists_apply));
17472 if (TREE_CODE (decl) != TYPE_DECL)
17474 cp_parser_diagnose_invalid_type_name (parser,
17475 identifier,
17476 token->location);
17477 return error_mark_node;
17480 if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
17482 bool allow_template = (template_parm_lists_apply
17483 || DECL_SELF_REFERENCE_P (decl));
17484 type = check_elaborated_type_specifier (tag_type, decl,
17485 allow_template);
17487 if (type == error_mark_node)
17488 return error_mark_node;
17491 /* Forward declarations of nested types, such as
17493 class C1::C2;
17494 class C1::C2::C3;
17496 are invalid unless all components preceding the final '::'
17497 are complete. If all enclosing types are complete, these
17498 declarations become merely pointless.
17500 Invalid forward declarations of nested types are errors
17501 caught elsewhere in parsing. Those that are pointless arrive
17502 here. */
17504 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
17505 && !is_friend && !processing_explicit_instantiation)
17506 warning (0, "declaration %qD does not declare anything", decl);
17508 type = TREE_TYPE (decl);
17510 else
17512 /* An elaborated-type-specifier sometimes introduces a new type and
17513 sometimes names an existing type. Normally, the rule is that it
17514 introduces a new type only if there is not an existing type of
17515 the same name already in scope. For example, given:
17517 struct S {};
17518 void f() { struct S s; }
17520 the `struct S' in the body of `f' is the same `struct S' as in
17521 the global scope; the existing definition is used. However, if
17522 there were no global declaration, this would introduce a new
17523 local class named `S'.
17525 An exception to this rule applies to the following code:
17527 namespace N { struct S; }
17529 Here, the elaborated-type-specifier names a new type
17530 unconditionally; even if there is already an `S' in the
17531 containing scope this declaration names a new type.
17532 This exception only applies if the elaborated-type-specifier
17533 forms the complete declaration:
17535 [class.name]
17537 A declaration consisting solely of `class-key identifier ;' is
17538 either a redeclaration of the name in the current scope or a
17539 forward declaration of the identifier as a class name. It
17540 introduces the name into the current scope.
17542 We are in this situation precisely when the next token is a `;'.
17544 An exception to the exception is that a `friend' declaration does
17545 *not* name a new type; i.e., given:
17547 struct S { friend struct T; };
17549 `T' is not a new type in the scope of `S'.
17551 Also, `new struct S' or `sizeof (struct S)' never results in the
17552 definition of a new type; a new type can only be declared in a
17553 declaration context. */
17555 tag_scope ts;
17556 bool template_p;
17558 if (is_friend)
17559 /* Friends have special name lookup rules. */
17560 ts = ts_within_enclosing_non_class;
17561 else if (is_declaration
17562 && cp_lexer_next_token_is (parser->lexer,
17563 CPP_SEMICOLON))
17564 /* This is a `class-key identifier ;' */
17565 ts = ts_current;
17566 else
17567 ts = ts_global;
17569 template_p =
17570 (template_parm_lists_apply
17571 && (cp_parser_next_token_starts_class_definition_p (parser)
17572 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)));
17573 /* An unqualified name was used to reference this type, so
17574 there were no qualifying templates. */
17575 if (template_parm_lists_apply
17576 && !cp_parser_check_template_parameters (parser,
17577 /*num_templates=*/0,
17578 token->location,
17579 /*declarator=*/NULL))
17580 return error_mark_node;
17581 type = xref_tag (tag_type, identifier, ts, template_p);
17585 if (type == error_mark_node)
17586 return error_mark_node;
17588 /* Allow attributes on forward declarations of classes. */
17589 if (attributes)
17591 if (TREE_CODE (type) == TYPENAME_TYPE)
17592 warning (OPT_Wattributes,
17593 "attributes ignored on uninstantiated type");
17594 else if (tag_type != enum_type && CLASSTYPE_TEMPLATE_INSTANTIATION (type)
17595 && ! processing_explicit_instantiation)
17596 warning (OPT_Wattributes,
17597 "attributes ignored on template instantiation");
17598 else if (is_declaration && cp_parser_declares_only_class_p (parser))
17599 cplus_decl_attributes (&type, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
17600 else
17601 warning (OPT_Wattributes,
17602 "attributes ignored on elaborated-type-specifier that is not a forward declaration");
17605 if (tag_type != enum_type)
17607 /* Indicate whether this class was declared as a `class' or as a
17608 `struct'. */
17609 if (CLASS_TYPE_P (type))
17610 CLASSTYPE_DECLARED_CLASS (type) = (tag_type == class_type);
17611 cp_parser_check_class_key (tag_type, type);
17614 /* A "<" cannot follow an elaborated type specifier. If that
17615 happens, the user was probably trying to form a template-id. */
17616 cp_parser_check_for_invalid_template_id (parser, type, tag_type,
17617 token->location);
17619 return type;
17622 /* Parse an enum-specifier.
17624 enum-specifier:
17625 enum-head { enumerator-list [opt] }
17626 enum-head { enumerator-list , } [C++0x]
17628 enum-head:
17629 enum-key identifier [opt] enum-base [opt]
17630 enum-key nested-name-specifier identifier enum-base [opt]
17632 enum-key:
17633 enum
17634 enum class [C++0x]
17635 enum struct [C++0x]
17637 enum-base: [C++0x]
17638 : type-specifier-seq
17640 opaque-enum-specifier:
17641 enum-key identifier enum-base [opt] ;
17643 GNU Extensions:
17644 enum-key attributes[opt] identifier [opt] enum-base [opt]
17645 { enumerator-list [opt] }attributes[opt]
17646 enum-key attributes[opt] identifier [opt] enum-base [opt]
17647 { enumerator-list, }attributes[opt] [C++0x]
17649 Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
17650 if the token stream isn't an enum-specifier after all. */
17652 static tree
17653 cp_parser_enum_specifier (cp_parser* parser)
17655 tree identifier;
17656 tree type = NULL_TREE;
17657 tree prev_scope;
17658 tree nested_name_specifier = NULL_TREE;
17659 tree attributes;
17660 bool scoped_enum_p = false;
17661 bool has_underlying_type = false;
17662 bool nested_being_defined = false;
17663 bool new_value_list = false;
17664 bool is_new_type = false;
17665 bool is_unnamed = false;
17666 tree underlying_type = NULL_TREE;
17667 cp_token *type_start_token = NULL;
17668 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
17670 parser->colon_corrects_to_scope_p = false;
17672 /* Parse tentatively so that we can back up if we don't find a
17673 enum-specifier. */
17674 cp_parser_parse_tentatively (parser);
17676 /* Caller guarantees that the current token is 'enum', an identifier
17677 possibly follows, and the token after that is an opening brace.
17678 If we don't have an identifier, fabricate an anonymous name for
17679 the enumeration being defined. */
17680 cp_lexer_consume_token (parser->lexer);
17682 /* Parse the "class" or "struct", which indicates a scoped
17683 enumeration type in C++0x. */
17684 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
17685 || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
17687 if (cxx_dialect < cxx11)
17688 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
17690 /* Consume the `struct' or `class' token. */
17691 cp_lexer_consume_token (parser->lexer);
17693 scoped_enum_p = true;
17696 attributes = cp_parser_attributes_opt (parser);
17698 /* Clear the qualification. */
17699 parser->scope = NULL_TREE;
17700 parser->qualifying_scope = NULL_TREE;
17701 parser->object_scope = NULL_TREE;
17703 /* Figure out in what scope the declaration is being placed. */
17704 prev_scope = current_scope ();
17706 type_start_token = cp_lexer_peek_token (parser->lexer);
17708 push_deferring_access_checks (dk_no_check);
17709 nested_name_specifier
17710 = cp_parser_nested_name_specifier_opt (parser,
17711 /*typename_keyword_p=*/true,
17712 /*check_dependency_p=*/false,
17713 /*type_p=*/false,
17714 /*is_declaration=*/false);
17716 if (nested_name_specifier)
17718 tree name;
17720 identifier = cp_parser_identifier (parser);
17721 name = cp_parser_lookup_name (parser, identifier,
17722 enum_type,
17723 /*is_template=*/false,
17724 /*is_namespace=*/false,
17725 /*check_dependency=*/true,
17726 /*ambiguous_decls=*/NULL,
17727 input_location);
17728 if (name && name != error_mark_node)
17730 type = TREE_TYPE (name);
17731 if (TREE_CODE (type) == TYPENAME_TYPE)
17733 /* Are template enums allowed in ISO? */
17734 if (template_parm_scope_p ())
17735 pedwarn (type_start_token->location, OPT_Wpedantic,
17736 "%qD is an enumeration template", name);
17737 /* ignore a typename reference, for it will be solved by name
17738 in start_enum. */
17739 type = NULL_TREE;
17742 else if (nested_name_specifier == error_mark_node)
17743 /* We already issued an error. */;
17744 else
17746 error_at (type_start_token->location,
17747 "%qD does not name an enumeration in %qT",
17748 identifier, nested_name_specifier);
17749 nested_name_specifier = error_mark_node;
17752 else
17754 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
17755 identifier = cp_parser_identifier (parser);
17756 else
17758 identifier = make_anon_name ();
17759 is_unnamed = true;
17760 if (scoped_enum_p)
17761 error_at (type_start_token->location,
17762 "unnamed scoped enum is not allowed");
17765 pop_deferring_access_checks ();
17767 /* Check for the `:' that denotes a specified underlying type in C++0x.
17768 Note that a ':' could also indicate a bitfield width, however. */
17769 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
17771 cp_decl_specifier_seq type_specifiers;
17773 /* Consume the `:'. */
17774 cp_lexer_consume_token (parser->lexer);
17776 /* Parse the type-specifier-seq. */
17777 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
17778 /*is_trailing_return=*/false,
17779 &type_specifiers);
17781 /* At this point this is surely not elaborated type specifier. */
17782 if (!cp_parser_parse_definitely (parser))
17783 return NULL_TREE;
17785 if (cxx_dialect < cxx11)
17786 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
17788 has_underlying_type = true;
17790 /* If that didn't work, stop. */
17791 if (type_specifiers.type != error_mark_node)
17793 underlying_type = grokdeclarator (NULL, &type_specifiers, TYPENAME,
17794 /*initialized=*/0, NULL);
17795 if (underlying_type == error_mark_node
17796 || check_for_bare_parameter_packs (underlying_type))
17797 underlying_type = NULL_TREE;
17801 /* Look for the `{' but don't consume it yet. */
17802 if (!cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
17804 if (cxx_dialect < cxx11 || (!scoped_enum_p && !underlying_type))
17806 cp_parser_error (parser, "expected %<{%>");
17807 if (has_underlying_type)
17809 type = NULL_TREE;
17810 goto out;
17813 /* An opaque-enum-specifier must have a ';' here. */
17814 if ((scoped_enum_p || underlying_type)
17815 && cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
17817 cp_parser_error (parser, "expected %<;%> or %<{%>");
17818 if (has_underlying_type)
17820 type = NULL_TREE;
17821 goto out;
17826 if (!has_underlying_type && !cp_parser_parse_definitely (parser))
17827 return NULL_TREE;
17829 if (nested_name_specifier)
17831 if (CLASS_TYPE_P (nested_name_specifier))
17833 nested_being_defined = TYPE_BEING_DEFINED (nested_name_specifier);
17834 TYPE_BEING_DEFINED (nested_name_specifier) = 1;
17835 push_scope (nested_name_specifier);
17837 else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
17839 push_nested_namespace (nested_name_specifier);
17843 /* Issue an error message if type-definitions are forbidden here. */
17844 if (!cp_parser_check_type_definition (parser))
17845 type = error_mark_node;
17846 else
17847 /* Create the new type. We do this before consuming the opening
17848 brace so the enum will be recorded as being on the line of its
17849 tag (or the 'enum' keyword, if there is no tag). */
17850 type = start_enum (identifier, type, underlying_type,
17851 attributes, scoped_enum_p, &is_new_type);
17853 /* If the next token is not '{' it is an opaque-enum-specifier or an
17854 elaborated-type-specifier. */
17855 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
17857 timevar_push (TV_PARSE_ENUM);
17858 if (nested_name_specifier
17859 && nested_name_specifier != error_mark_node)
17861 /* The following catches invalid code such as:
17862 enum class S<int>::E { A, B, C }; */
17863 if (!processing_specialization
17864 && CLASS_TYPE_P (nested_name_specifier)
17865 && CLASSTYPE_USE_TEMPLATE (nested_name_specifier))
17866 error_at (type_start_token->location, "cannot add an enumerator "
17867 "list to a template instantiation");
17869 if (TREE_CODE (nested_name_specifier) == TYPENAME_TYPE)
17871 error_at (type_start_token->location,
17872 "%<%T::%E%> has not been declared",
17873 TYPE_CONTEXT (nested_name_specifier),
17874 nested_name_specifier);
17875 type = error_mark_node;
17877 else if (TREE_CODE (nested_name_specifier) != NAMESPACE_DECL
17878 && !CLASS_TYPE_P (nested_name_specifier))
17880 error_at (type_start_token->location, "nested name specifier "
17881 "%qT for enum declaration does not name a class "
17882 "or namespace", nested_name_specifier);
17883 type = error_mark_node;
17885 /* If that scope does not contain the scope in which the
17886 class was originally declared, the program is invalid. */
17887 else if (prev_scope && !is_ancestor (prev_scope,
17888 nested_name_specifier))
17890 if (at_namespace_scope_p ())
17891 error_at (type_start_token->location,
17892 "declaration of %qD in namespace %qD which does not "
17893 "enclose %qD",
17894 type, prev_scope, nested_name_specifier);
17895 else
17896 error_at (type_start_token->location,
17897 "declaration of %qD in %qD which does not "
17898 "enclose %qD",
17899 type, prev_scope, nested_name_specifier);
17900 type = error_mark_node;
17902 /* If that scope is the scope where the declaration is being placed
17903 the program is invalid. */
17904 else if (CLASS_TYPE_P (nested_name_specifier)
17905 && CLASS_TYPE_P (prev_scope)
17906 && same_type_p (nested_name_specifier, prev_scope))
17908 permerror (type_start_token->location,
17909 "extra qualification not allowed");
17910 nested_name_specifier = NULL_TREE;
17914 if (scoped_enum_p)
17915 begin_scope (sk_scoped_enum, type);
17917 /* Consume the opening brace. */
17918 cp_lexer_consume_token (parser->lexer);
17920 if (type == error_mark_node)
17921 ; /* Nothing to add */
17922 else if (OPAQUE_ENUM_P (type)
17923 || (cxx_dialect > cxx98 && processing_specialization))
17925 new_value_list = true;
17926 SET_OPAQUE_ENUM_P (type, false);
17927 DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
17929 else
17931 error_at (type_start_token->location,
17932 "multiple definition of %q#T", type);
17933 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)),
17934 "previous definition here");
17935 type = error_mark_node;
17938 if (type == error_mark_node)
17939 cp_parser_skip_to_end_of_block_or_statement (parser);
17940 /* If the next token is not '}', then there are some enumerators. */
17941 else if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
17943 if (is_unnamed && !scoped_enum_p)
17944 pedwarn (type_start_token->location, OPT_Wpedantic,
17945 "ISO C++ forbids empty unnamed enum");
17947 else
17948 cp_parser_enumerator_list (parser, type);
17950 /* Consume the final '}'. */
17951 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
17953 if (scoped_enum_p)
17954 finish_scope ();
17955 timevar_pop (TV_PARSE_ENUM);
17957 else
17959 /* If a ';' follows, then it is an opaque-enum-specifier
17960 and additional restrictions apply. */
17961 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
17963 if (is_unnamed)
17964 error_at (type_start_token->location,
17965 "opaque-enum-specifier without name");
17966 else if (nested_name_specifier)
17967 error_at (type_start_token->location,
17968 "opaque-enum-specifier must use a simple identifier");
17972 /* Look for trailing attributes to apply to this enumeration, and
17973 apply them if appropriate. */
17974 if (cp_parser_allow_gnu_extensions_p (parser))
17976 tree trailing_attr = cp_parser_gnu_attributes_opt (parser);
17977 cplus_decl_attributes (&type,
17978 trailing_attr,
17979 (int) ATTR_FLAG_TYPE_IN_PLACE);
17982 /* Finish up the enumeration. */
17983 if (type != error_mark_node)
17985 if (new_value_list)
17986 finish_enum_value_list (type);
17987 if (is_new_type)
17988 finish_enum (type);
17991 if (nested_name_specifier)
17993 if (CLASS_TYPE_P (nested_name_specifier))
17995 TYPE_BEING_DEFINED (nested_name_specifier) = nested_being_defined;
17996 pop_scope (nested_name_specifier);
17998 else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
18000 pop_nested_namespace (nested_name_specifier);
18003 out:
18004 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
18005 return type;
18008 /* Parse an enumerator-list. The enumerators all have the indicated
18009 TYPE.
18011 enumerator-list:
18012 enumerator-definition
18013 enumerator-list , enumerator-definition */
18015 static void
18016 cp_parser_enumerator_list (cp_parser* parser, tree type)
18018 while (true)
18020 /* Parse an enumerator-definition. */
18021 cp_parser_enumerator_definition (parser, type);
18023 /* If the next token is not a ',', we've reached the end of
18024 the list. */
18025 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
18026 break;
18027 /* Otherwise, consume the `,' and keep going. */
18028 cp_lexer_consume_token (parser->lexer);
18029 /* If the next token is a `}', there is a trailing comma. */
18030 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
18032 if (cxx_dialect < cxx11 && !in_system_header_at (input_location))
18033 pedwarn (input_location, OPT_Wpedantic,
18034 "comma at end of enumerator list");
18035 break;
18040 /* Parse an enumerator-definition. The enumerator has the indicated
18041 TYPE.
18043 enumerator-definition:
18044 enumerator
18045 enumerator = constant-expression
18047 enumerator:
18048 identifier
18050 GNU Extensions:
18052 enumerator-definition:
18053 enumerator attributes [opt]
18054 enumerator attributes [opt] = constant-expression */
18056 static void
18057 cp_parser_enumerator_definition (cp_parser* parser, tree type)
18059 tree identifier;
18060 tree value;
18061 location_t loc;
18063 /* Save the input location because we are interested in the location
18064 of the identifier and not the location of the explicit value. */
18065 loc = cp_lexer_peek_token (parser->lexer)->location;
18067 /* Look for the identifier. */
18068 identifier = cp_parser_identifier (parser);
18069 if (identifier == error_mark_node)
18070 return;
18072 /* Parse any specified attributes. */
18073 tree attrs = cp_parser_attributes_opt (parser);
18075 /* If the next token is an '=', then there is an explicit value. */
18076 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
18078 /* Consume the `=' token. */
18079 cp_lexer_consume_token (parser->lexer);
18080 /* Parse the value. */
18081 value = cp_parser_constant_expression (parser);
18083 else
18084 value = NULL_TREE;
18086 /* If we are processing a template, make sure the initializer of the
18087 enumerator doesn't contain any bare template parameter pack. */
18088 if (check_for_bare_parameter_packs (value))
18089 value = error_mark_node;
18091 /* Create the enumerator. */
18092 build_enumerator (identifier, value, type, attrs, loc);
18095 /* Parse a namespace-name.
18097 namespace-name:
18098 original-namespace-name
18099 namespace-alias
18101 Returns the NAMESPACE_DECL for the namespace. */
18103 static tree
18104 cp_parser_namespace_name (cp_parser* parser)
18106 tree identifier;
18107 tree namespace_decl;
18109 cp_token *token = cp_lexer_peek_token (parser->lexer);
18111 /* Get the name of the namespace. */
18112 identifier = cp_parser_identifier (parser);
18113 if (identifier == error_mark_node)
18114 return error_mark_node;
18116 /* Look up the identifier in the currently active scope. Look only
18117 for namespaces, due to:
18119 [basic.lookup.udir]
18121 When looking up a namespace-name in a using-directive or alias
18122 definition, only namespace names are considered.
18124 And:
18126 [basic.lookup.qual]
18128 During the lookup of a name preceding the :: scope resolution
18129 operator, object, function, and enumerator names are ignored.
18131 (Note that cp_parser_qualifying_entity only calls this
18132 function if the token after the name is the scope resolution
18133 operator.) */
18134 namespace_decl = cp_parser_lookup_name (parser, identifier,
18135 none_type,
18136 /*is_template=*/false,
18137 /*is_namespace=*/true,
18138 /*check_dependency=*/true,
18139 /*ambiguous_decls=*/NULL,
18140 token->location);
18141 /* If it's not a namespace, issue an error. */
18142 if (namespace_decl == error_mark_node
18143 || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
18145 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
18146 error_at (token->location, "%qD is not a namespace-name", identifier);
18147 cp_parser_error (parser, "expected namespace-name");
18148 namespace_decl = error_mark_node;
18151 return namespace_decl;
18154 /* Parse a namespace-definition.
18156 namespace-definition:
18157 named-namespace-definition
18158 unnamed-namespace-definition
18160 named-namespace-definition:
18161 original-namespace-definition
18162 extension-namespace-definition
18164 original-namespace-definition:
18165 namespace identifier { namespace-body }
18167 extension-namespace-definition:
18168 namespace original-namespace-name { namespace-body }
18170 unnamed-namespace-definition:
18171 namespace { namespace-body } */
18173 static void
18174 cp_parser_namespace_definition (cp_parser* parser)
18176 tree identifier, attribs;
18177 bool has_visibility;
18178 bool is_inline;
18179 cp_token* token;
18180 int nested_definition_count = 0;
18182 cp_ensure_no_omp_declare_simd (parser);
18183 cp_ensure_no_oacc_routine (parser);
18184 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_INLINE))
18186 maybe_warn_cpp0x (CPP0X_INLINE_NAMESPACES);
18187 is_inline = true;
18188 cp_lexer_consume_token (parser->lexer);
18190 else
18191 is_inline = false;
18193 /* Look for the `namespace' keyword. */
18194 token = cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
18196 /* Parse any specified attributes before the identifier. */
18197 attribs = cp_parser_attributes_opt (parser);
18199 /* Get the name of the namespace. We do not attempt to distinguish
18200 between an original-namespace-definition and an
18201 extension-namespace-definition at this point. The semantic
18202 analysis routines are responsible for that. */
18203 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
18204 identifier = cp_parser_identifier (parser);
18205 else
18206 identifier = NULL_TREE;
18208 /* Parse any specified attributes after the identifier. */
18209 tree post_ident_attribs = cp_parser_attributes_opt (parser);
18210 if (post_ident_attribs)
18212 if (attribs)
18213 attribs = chainon (attribs, post_ident_attribs);
18214 else
18215 attribs = post_ident_attribs;
18218 /* Start the namespace. */
18219 bool ok = push_namespace (identifier);
18221 /* Parse any nested namespace definition. */
18222 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
18224 if (attribs)
18225 error_at (token->location, "a nested namespace definition cannot have attributes");
18226 if (cxx_dialect < cxx1z)
18227 pedwarn (input_location, OPT_Wpedantic,
18228 "nested namespace definitions only available with "
18229 "-std=c++1z or -std=gnu++1z");
18230 if (is_inline)
18231 error_at (token->location, "a nested namespace definition cannot be inline");
18232 while (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
18234 cp_lexer_consume_token (parser->lexer);
18235 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
18236 identifier = cp_parser_identifier (parser);
18237 else
18239 cp_parser_error (parser, "nested identifier required");
18240 break;
18242 if (push_namespace (identifier))
18243 ++nested_definition_count;
18247 /* Look for the `{' to validate starting the namespace. */
18248 cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
18250 /* "inline namespace" is equivalent to a stub namespace definition
18251 followed by a strong using directive. */
18252 if (is_inline && ok)
18254 tree name_space = current_namespace;
18255 /* Set up namespace association. */
18256 DECL_NAMESPACE_ASSOCIATIONS (name_space)
18257 = tree_cons (CP_DECL_CONTEXT (name_space), NULL_TREE,
18258 DECL_NAMESPACE_ASSOCIATIONS (name_space));
18259 /* Import the contents of the inline namespace. */
18260 pop_namespace ();
18261 do_using_directive (name_space);
18262 push_namespace (identifier);
18265 has_visibility = handle_namespace_attrs (current_namespace, attribs);
18267 warning (OPT_Wnamespaces, "namespace %qD entered", current_namespace);
18269 /* Parse the body of the namespace. */
18270 cp_parser_namespace_body (parser);
18272 if (has_visibility)
18273 pop_visibility (1);
18275 /* Finish the nested namespace definitions. */
18276 while (nested_definition_count--)
18277 pop_namespace ();
18279 /* Finish the namespace. */
18280 if (ok)
18281 pop_namespace ();
18282 /* Look for the final `}'. */
18283 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
18286 /* Parse a namespace-body.
18288 namespace-body:
18289 declaration-seq [opt] */
18291 static void
18292 cp_parser_namespace_body (cp_parser* parser)
18294 cp_parser_declaration_seq_opt (parser);
18297 /* Parse a namespace-alias-definition.
18299 namespace-alias-definition:
18300 namespace identifier = qualified-namespace-specifier ; */
18302 static void
18303 cp_parser_namespace_alias_definition (cp_parser* parser)
18305 tree identifier;
18306 tree namespace_specifier;
18308 cp_token *token = cp_lexer_peek_token (parser->lexer);
18310 /* Look for the `namespace' keyword. */
18311 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
18312 /* Look for the identifier. */
18313 identifier = cp_parser_identifier (parser);
18314 if (identifier == error_mark_node)
18315 return;
18316 /* Look for the `=' token. */
18317 if (!cp_parser_uncommitted_to_tentative_parse_p (parser)
18318 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
18320 error_at (token->location, "%<namespace%> definition is not allowed here");
18321 /* Skip the definition. */
18322 cp_lexer_consume_token (parser->lexer);
18323 if (cp_parser_skip_to_closing_brace (parser))
18324 cp_lexer_consume_token (parser->lexer);
18325 return;
18327 cp_parser_require (parser, CPP_EQ, RT_EQ);
18328 /* Look for the qualified-namespace-specifier. */
18329 namespace_specifier
18330 = cp_parser_qualified_namespace_specifier (parser);
18331 /* Look for the `;' token. */
18332 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
18334 /* Register the alias in the symbol table. */
18335 do_namespace_alias (identifier, namespace_specifier);
18338 /* Parse a qualified-namespace-specifier.
18340 qualified-namespace-specifier:
18341 :: [opt] nested-name-specifier [opt] namespace-name
18343 Returns a NAMESPACE_DECL corresponding to the specified
18344 namespace. */
18346 static tree
18347 cp_parser_qualified_namespace_specifier (cp_parser* parser)
18349 /* Look for the optional `::'. */
18350 cp_parser_global_scope_opt (parser,
18351 /*current_scope_valid_p=*/false);
18353 /* Look for the optional nested-name-specifier. */
18354 cp_parser_nested_name_specifier_opt (parser,
18355 /*typename_keyword_p=*/false,
18356 /*check_dependency_p=*/true,
18357 /*type_p=*/false,
18358 /*is_declaration=*/true);
18360 return cp_parser_namespace_name (parser);
18363 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
18364 access declaration.
18366 using-declaration:
18367 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
18368 using :: unqualified-id ;
18370 access-declaration:
18371 qualified-id ;
18375 static bool
18376 cp_parser_using_declaration (cp_parser* parser,
18377 bool access_declaration_p)
18379 cp_token *token;
18380 bool typename_p = false;
18381 bool global_scope_p;
18382 tree decl;
18383 tree identifier;
18384 tree qscope;
18385 int oldcount = errorcount;
18386 cp_token *diag_token = NULL;
18388 if (access_declaration_p)
18390 diag_token = cp_lexer_peek_token (parser->lexer);
18391 cp_parser_parse_tentatively (parser);
18393 else
18395 /* Look for the `using' keyword. */
18396 cp_parser_require_keyword (parser, RID_USING, RT_USING);
18398 again:
18399 /* Peek at the next token. */
18400 token = cp_lexer_peek_token (parser->lexer);
18401 /* See if it's `typename'. */
18402 if (token->keyword == RID_TYPENAME)
18404 /* Remember that we've seen it. */
18405 typename_p = true;
18406 /* Consume the `typename' token. */
18407 cp_lexer_consume_token (parser->lexer);
18411 /* Look for the optional global scope qualification. */
18412 global_scope_p
18413 = (cp_parser_global_scope_opt (parser,
18414 /*current_scope_valid_p=*/false)
18415 != NULL_TREE);
18417 /* If we saw `typename', or didn't see `::', then there must be a
18418 nested-name-specifier present. */
18419 if (typename_p || !global_scope_p)
18421 qscope = cp_parser_nested_name_specifier (parser, typename_p,
18422 /*check_dependency_p=*/true,
18423 /*type_p=*/false,
18424 /*is_declaration=*/true);
18425 if (!qscope && !cp_parser_uncommitted_to_tentative_parse_p (parser))
18427 cp_parser_skip_to_end_of_block_or_statement (parser);
18428 return false;
18431 /* Otherwise, we could be in either of the two productions. In that
18432 case, treat the nested-name-specifier as optional. */
18433 else
18434 qscope = cp_parser_nested_name_specifier_opt (parser,
18435 /*typename_keyword_p=*/false,
18436 /*check_dependency_p=*/true,
18437 /*type_p=*/false,
18438 /*is_declaration=*/true);
18439 if (!qscope)
18440 qscope = global_namespace;
18441 else if (UNSCOPED_ENUM_P (qscope))
18442 qscope = CP_TYPE_CONTEXT (qscope);
18444 if (access_declaration_p && cp_parser_error_occurred (parser))
18445 /* Something has already gone wrong; there's no need to parse
18446 further. Since an error has occurred, the return value of
18447 cp_parser_parse_definitely will be false, as required. */
18448 return cp_parser_parse_definitely (parser);
18450 token = cp_lexer_peek_token (parser->lexer);
18451 /* Parse the unqualified-id. */
18452 identifier = cp_parser_unqualified_id (parser,
18453 /*template_keyword_p=*/false,
18454 /*check_dependency_p=*/true,
18455 /*declarator_p=*/true,
18456 /*optional_p=*/false);
18458 if (access_declaration_p)
18460 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
18461 cp_parser_simulate_error (parser);
18462 if (!cp_parser_parse_definitely (parser))
18463 return false;
18465 else if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
18467 cp_token *ell = cp_lexer_consume_token (parser->lexer);
18468 if (cxx_dialect < cxx1z
18469 && !in_system_header_at (ell->location))
18470 pedwarn (ell->location, 0,
18471 "pack expansion in using-declaration only available "
18472 "with -std=c++1z or -std=gnu++1z");
18473 qscope = make_pack_expansion (qscope);
18476 /* The function we call to handle a using-declaration is different
18477 depending on what scope we are in. */
18478 if (qscope == error_mark_node || identifier == error_mark_node)
18480 else if (!identifier_p (identifier)
18481 && TREE_CODE (identifier) != BIT_NOT_EXPR)
18482 /* [namespace.udecl]
18484 A using declaration shall not name a template-id. */
18485 error_at (token->location,
18486 "a template-id may not appear in a using-declaration");
18487 else
18489 if (at_class_scope_p ())
18491 /* Create the USING_DECL. */
18492 decl = do_class_using_decl (qscope, identifier);
18494 if (decl && typename_p)
18495 USING_DECL_TYPENAME_P (decl) = 1;
18497 if (check_for_bare_parameter_packs (decl))
18499 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
18500 return false;
18502 else
18503 /* Add it to the list of members in this class. */
18504 finish_member_declaration (decl);
18506 else
18508 decl = cp_parser_lookup_name_simple (parser,
18509 identifier,
18510 token->location);
18511 if (decl == error_mark_node)
18512 cp_parser_name_lookup_error (parser, identifier,
18513 decl, NLE_NULL,
18514 token->location);
18515 else if (check_for_bare_parameter_packs (decl))
18517 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
18518 return false;
18520 else if (!at_namespace_scope_p ())
18521 do_local_using_decl (decl, qscope, identifier);
18522 else
18523 do_toplevel_using_decl (decl, qscope, identifier);
18527 if (!access_declaration_p
18528 && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
18530 cp_token *comma = cp_lexer_consume_token (parser->lexer);
18531 if (cxx_dialect < cxx1z)
18532 pedwarn (comma->location, 0,
18533 "comma-separated list in using-declaration only available "
18534 "with -std=c++1z or -std=gnu++1z");
18535 goto again;
18538 /* Look for the final `;'. */
18539 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
18541 if (access_declaration_p && errorcount == oldcount)
18542 warning_at (diag_token->location, OPT_Wdeprecated,
18543 "access declarations are deprecated "
18544 "in favour of using-declarations; "
18545 "suggestion: add the %<using%> keyword");
18547 return true;
18550 /* Parse an alias-declaration.
18552 alias-declaration:
18553 using identifier attribute-specifier-seq [opt] = type-id */
18555 static tree
18556 cp_parser_alias_declaration (cp_parser* parser)
18558 tree id, type, decl, pushed_scope = NULL_TREE, attributes;
18559 location_t id_location;
18560 cp_declarator *declarator;
18561 cp_decl_specifier_seq decl_specs;
18562 bool member_p;
18563 const char *saved_message = NULL;
18565 /* Look for the `using' keyword. */
18566 cp_token *using_token
18567 = cp_parser_require_keyword (parser, RID_USING, RT_USING);
18568 if (using_token == NULL)
18569 return error_mark_node;
18571 id_location = cp_lexer_peek_token (parser->lexer)->location;
18572 id = cp_parser_identifier (parser);
18573 if (id == error_mark_node)
18574 return error_mark_node;
18576 cp_token *attrs_token = cp_lexer_peek_token (parser->lexer);
18577 attributes = cp_parser_attributes_opt (parser);
18578 if (attributes == error_mark_node)
18579 return error_mark_node;
18581 cp_parser_require (parser, CPP_EQ, RT_EQ);
18583 if (cp_parser_error_occurred (parser))
18584 return error_mark_node;
18586 cp_parser_commit_to_tentative_parse (parser);
18588 /* Now we are going to parse the type-id of the declaration. */
18591 [dcl.type]/3 says:
18593 "A type-specifier-seq shall not define a class or enumeration
18594 unless it appears in the type-id of an alias-declaration (7.1.3) that
18595 is not the declaration of a template-declaration."
18597 In other words, if we currently are in an alias template, the
18598 type-id should not define a type.
18600 So let's set parser->type_definition_forbidden_message in that
18601 case; cp_parser_check_type_definition (called by
18602 cp_parser_class_specifier) will then emit an error if a type is
18603 defined in the type-id. */
18604 if (parser->num_template_parameter_lists)
18606 saved_message = parser->type_definition_forbidden_message;
18607 parser->type_definition_forbidden_message =
18608 G_("types may not be defined in alias template declarations");
18611 type = cp_parser_type_id (parser);
18613 /* Restore the error message if need be. */
18614 if (parser->num_template_parameter_lists)
18615 parser->type_definition_forbidden_message = saved_message;
18617 if (type == error_mark_node
18618 || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
18620 cp_parser_skip_to_end_of_block_or_statement (parser);
18621 return error_mark_node;
18624 /* A typedef-name can also be introduced by an alias-declaration. The
18625 identifier following the using keyword becomes a typedef-name. It has
18626 the same semantics as if it were introduced by the typedef
18627 specifier. In particular, it does not define a new type and it shall
18628 not appear in the type-id. */
18630 clear_decl_specs (&decl_specs);
18631 decl_specs.type = type;
18632 if (attributes != NULL_TREE)
18634 decl_specs.attributes = attributes;
18635 set_and_check_decl_spec_loc (&decl_specs,
18636 ds_attribute,
18637 attrs_token);
18639 set_and_check_decl_spec_loc (&decl_specs,
18640 ds_typedef,
18641 using_token);
18642 set_and_check_decl_spec_loc (&decl_specs,
18643 ds_alias,
18644 using_token);
18646 declarator = make_id_declarator (NULL_TREE, id, sfk_none);
18647 declarator->id_loc = id_location;
18649 member_p = at_class_scope_p ();
18650 if (member_p)
18651 decl = grokfield (declarator, &decl_specs, NULL_TREE, false,
18652 NULL_TREE, attributes);
18653 else
18654 decl = start_decl (declarator, &decl_specs, 0,
18655 attributes, NULL_TREE, &pushed_scope);
18656 if (decl == error_mark_node)
18657 return decl;
18659 // Attach constraints to the alias declaration.
18660 if (flag_concepts && current_template_parms)
18662 tree reqs = TEMPLATE_PARMS_CONSTRAINTS (current_template_parms);
18663 tree constr = build_constraints (reqs, NULL_TREE);
18664 set_constraints (decl, constr);
18667 cp_finish_decl (decl, NULL_TREE, 0, NULL_TREE, 0);
18669 if (pushed_scope)
18670 pop_scope (pushed_scope);
18672 /* If decl is a template, return its TEMPLATE_DECL so that it gets
18673 added into the symbol table; otherwise, return the TYPE_DECL. */
18674 if (DECL_LANG_SPECIFIC (decl)
18675 && DECL_TEMPLATE_INFO (decl)
18676 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl)))
18678 decl = DECL_TI_TEMPLATE (decl);
18679 if (member_p)
18680 check_member_template (decl);
18683 return decl;
18686 /* Parse a using-directive.
18688 using-directive:
18689 using namespace :: [opt] nested-name-specifier [opt]
18690 namespace-name ; */
18692 static void
18693 cp_parser_using_directive (cp_parser* parser)
18695 tree namespace_decl;
18696 tree attribs;
18698 /* Look for the `using' keyword. */
18699 cp_parser_require_keyword (parser, RID_USING, RT_USING);
18700 /* And the `namespace' keyword. */
18701 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
18702 /* Look for the optional `::' operator. */
18703 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
18704 /* And the optional nested-name-specifier. */
18705 cp_parser_nested_name_specifier_opt (parser,
18706 /*typename_keyword_p=*/false,
18707 /*check_dependency_p=*/true,
18708 /*type_p=*/false,
18709 /*is_declaration=*/true);
18710 /* Get the namespace being used. */
18711 namespace_decl = cp_parser_namespace_name (parser);
18712 /* And any specified attributes. */
18713 attribs = cp_parser_attributes_opt (parser);
18714 /* Update the symbol table. */
18715 parse_using_directive (namespace_decl, attribs);
18716 /* Look for the final `;'. */
18717 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
18720 /* Parse an asm-definition.
18722 asm-definition:
18723 asm ( string-literal ) ;
18725 GNU Extension:
18727 asm-definition:
18728 asm volatile [opt] ( string-literal ) ;
18729 asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
18730 asm volatile [opt] ( string-literal : asm-operand-list [opt]
18731 : asm-operand-list [opt] ) ;
18732 asm volatile [opt] ( string-literal : asm-operand-list [opt]
18733 : asm-operand-list [opt]
18734 : asm-clobber-list [opt] ) ;
18735 asm volatile [opt] goto ( string-literal : : asm-operand-list [opt]
18736 : asm-clobber-list [opt]
18737 : asm-goto-list ) ; */
18739 static void
18740 cp_parser_asm_definition (cp_parser* parser)
18742 tree string;
18743 tree outputs = NULL_TREE;
18744 tree inputs = NULL_TREE;
18745 tree clobbers = NULL_TREE;
18746 tree labels = NULL_TREE;
18747 tree asm_stmt;
18748 bool volatile_p = false;
18749 bool extended_p = false;
18750 bool invalid_inputs_p = false;
18751 bool invalid_outputs_p = false;
18752 bool goto_p = false;
18753 required_token missing = RT_NONE;
18755 /* Look for the `asm' keyword. */
18756 cp_parser_require_keyword (parser, RID_ASM, RT_ASM);
18758 if (parser->in_function_body
18759 && DECL_DECLARED_CONSTEXPR_P (current_function_decl))
18761 error ("%<asm%> in %<constexpr%> function");
18762 cp_function_chain->invalid_constexpr = true;
18765 /* See if the next token is `volatile'. */
18766 if (cp_parser_allow_gnu_extensions_p (parser)
18767 && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
18769 /* Remember that we saw the `volatile' keyword. */
18770 volatile_p = true;
18771 /* Consume the token. */
18772 cp_lexer_consume_token (parser->lexer);
18774 if (cp_parser_allow_gnu_extensions_p (parser)
18775 && parser->in_function_body
18776 && cp_lexer_next_token_is_keyword (parser->lexer, RID_GOTO))
18778 /* Remember that we saw the `goto' keyword. */
18779 goto_p = true;
18780 /* Consume the token. */
18781 cp_lexer_consume_token (parser->lexer);
18783 /* Look for the opening `('. */
18784 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
18785 return;
18786 /* Look for the string. */
18787 string = cp_parser_string_literal (parser, false, false);
18788 if (string == error_mark_node)
18790 cp_parser_skip_to_closing_parenthesis (parser, true, false,
18791 /*consume_paren=*/true);
18792 return;
18795 /* If we're allowing GNU extensions, check for the extended assembly
18796 syntax. Unfortunately, the `:' tokens need not be separated by
18797 a space in C, and so, for compatibility, we tolerate that here
18798 too. Doing that means that we have to treat the `::' operator as
18799 two `:' tokens. */
18800 if (cp_parser_allow_gnu_extensions_p (parser)
18801 && parser->in_function_body
18802 && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
18803 || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
18805 bool inputs_p = false;
18806 bool clobbers_p = false;
18807 bool labels_p = false;
18809 /* The extended syntax was used. */
18810 extended_p = true;
18812 /* Look for outputs. */
18813 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
18815 /* Consume the `:'. */
18816 cp_lexer_consume_token (parser->lexer);
18817 /* Parse the output-operands. */
18818 if (cp_lexer_next_token_is_not (parser->lexer,
18819 CPP_COLON)
18820 && cp_lexer_next_token_is_not (parser->lexer,
18821 CPP_SCOPE)
18822 && cp_lexer_next_token_is_not (parser->lexer,
18823 CPP_CLOSE_PAREN)
18824 && !goto_p)
18826 outputs = cp_parser_asm_operand_list (parser);
18827 if (outputs == error_mark_node)
18828 invalid_outputs_p = true;
18831 /* If the next token is `::', there are no outputs, and the
18832 next token is the beginning of the inputs. */
18833 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
18834 /* The inputs are coming next. */
18835 inputs_p = true;
18837 /* Look for inputs. */
18838 if (inputs_p
18839 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
18841 /* Consume the `:' or `::'. */
18842 cp_lexer_consume_token (parser->lexer);
18843 /* Parse the output-operands. */
18844 if (cp_lexer_next_token_is_not (parser->lexer,
18845 CPP_COLON)
18846 && cp_lexer_next_token_is_not (parser->lexer,
18847 CPP_SCOPE)
18848 && cp_lexer_next_token_is_not (parser->lexer,
18849 CPP_CLOSE_PAREN))
18851 inputs = cp_parser_asm_operand_list (parser);
18852 if (inputs == error_mark_node)
18853 invalid_inputs_p = true;
18856 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
18857 /* The clobbers are coming next. */
18858 clobbers_p = true;
18860 /* Look for clobbers. */
18861 if (clobbers_p
18862 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
18864 clobbers_p = true;
18865 /* Consume the `:' or `::'. */
18866 cp_lexer_consume_token (parser->lexer);
18867 /* Parse the clobbers. */
18868 if (cp_lexer_next_token_is_not (parser->lexer,
18869 CPP_COLON)
18870 && cp_lexer_next_token_is_not (parser->lexer,
18871 CPP_CLOSE_PAREN))
18872 clobbers = cp_parser_asm_clobber_list (parser);
18874 else if (goto_p
18875 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
18876 /* The labels are coming next. */
18877 labels_p = true;
18879 /* Look for labels. */
18880 if (labels_p
18881 || (goto_p && cp_lexer_next_token_is (parser->lexer, CPP_COLON)))
18883 labels_p = true;
18884 /* Consume the `:' or `::'. */
18885 cp_lexer_consume_token (parser->lexer);
18886 /* Parse the labels. */
18887 labels = cp_parser_asm_label_list (parser);
18890 if (goto_p && !labels_p)
18891 missing = clobbers_p ? RT_COLON : RT_COLON_SCOPE;
18893 else if (goto_p)
18894 missing = RT_COLON_SCOPE;
18896 /* Look for the closing `)'. */
18897 if (!cp_parser_require (parser, missing ? CPP_COLON : CPP_CLOSE_PAREN,
18898 missing ? missing : RT_CLOSE_PAREN))
18899 cp_parser_skip_to_closing_parenthesis (parser, true, false,
18900 /*consume_paren=*/true);
18901 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
18903 if (!invalid_inputs_p && !invalid_outputs_p)
18905 /* Create the ASM_EXPR. */
18906 if (parser->in_function_body)
18908 asm_stmt = finish_asm_stmt (volatile_p, string, outputs,
18909 inputs, clobbers, labels);
18910 /* If the extended syntax was not used, mark the ASM_EXPR. */
18911 if (!extended_p)
18913 tree temp = asm_stmt;
18914 if (TREE_CODE (temp) == CLEANUP_POINT_EXPR)
18915 temp = TREE_OPERAND (temp, 0);
18917 ASM_INPUT_P (temp) = 1;
18920 else
18921 symtab->finalize_toplevel_asm (string);
18925 /* Given the type TYPE of a declaration with declarator DECLARATOR, return the
18926 type that comes from the decl-specifier-seq. */
18928 static tree
18929 strip_declarator_types (tree type, cp_declarator *declarator)
18931 for (cp_declarator *d = declarator; d;)
18932 switch (d->kind)
18934 case cdk_id:
18935 case cdk_decomp:
18936 case cdk_error:
18937 d = NULL;
18938 break;
18940 default:
18941 if (TYPE_PTRMEMFUNC_P (type))
18942 type = TYPE_PTRMEMFUNC_FN_TYPE (type);
18943 type = TREE_TYPE (type);
18944 d = d->declarator;
18945 break;
18948 return type;
18951 /* Declarators [gram.dcl.decl] */
18953 /* Parse an init-declarator.
18955 init-declarator:
18956 declarator initializer [opt]
18958 GNU Extension:
18960 init-declarator:
18961 declarator asm-specification [opt] attributes [opt] initializer [opt]
18963 function-definition:
18964 decl-specifier-seq [opt] declarator ctor-initializer [opt]
18965 function-body
18966 decl-specifier-seq [opt] declarator function-try-block
18968 GNU Extension:
18970 function-definition:
18971 __extension__ function-definition
18973 TM Extension:
18975 function-definition:
18976 decl-specifier-seq [opt] declarator function-transaction-block
18978 The DECL_SPECIFIERS apply to this declarator. Returns a
18979 representation of the entity declared. If MEMBER_P is TRUE, then
18980 this declarator appears in a class scope. The new DECL created by
18981 this declarator is returned.
18983 The CHECKS are access checks that should be performed once we know
18984 what entity is being declared (and, therefore, what classes have
18985 befriended it).
18987 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
18988 for a function-definition here as well. If the declarator is a
18989 declarator for a function-definition, *FUNCTION_DEFINITION_P will
18990 be TRUE upon return. By that point, the function-definition will
18991 have been completely parsed.
18993 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
18994 is FALSE.
18996 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
18997 parsed declaration if it is an uninitialized single declarator not followed
18998 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
18999 if present, will not be consumed. If returned, this declarator will be
19000 created with SD_INITIALIZED but will not call cp_finish_decl.
19002 If INIT_LOC is not NULL, and *INIT_LOC is equal to UNKNOWN_LOCATION,
19003 and there is an initializer, the pointed location_t is set to the
19004 location of the '=' or `(', or '{' in C++11 token introducing the
19005 initializer. */
19007 static tree
19008 cp_parser_init_declarator (cp_parser* parser,
19009 cp_decl_specifier_seq *decl_specifiers,
19010 vec<deferred_access_check, va_gc> *checks,
19011 bool function_definition_allowed_p,
19012 bool member_p,
19013 int declares_class_or_enum,
19014 bool* function_definition_p,
19015 tree* maybe_range_for_decl,
19016 location_t* init_loc,
19017 tree* auto_result)
19019 cp_token *token = NULL, *asm_spec_start_token = NULL,
19020 *attributes_start_token = NULL;
19021 cp_declarator *declarator;
19022 tree prefix_attributes;
19023 tree attributes = NULL;
19024 tree asm_specification;
19025 tree initializer;
19026 tree decl = NULL_TREE;
19027 tree scope;
19028 int is_initialized;
19029 /* Only valid if IS_INITIALIZED is true. In that case, CPP_EQ if
19030 initialized with "= ..", CPP_OPEN_PAREN if initialized with
19031 "(...)". */
19032 enum cpp_ttype initialization_kind;
19033 bool is_direct_init = false;
19034 bool is_non_constant_init;
19035 int ctor_dtor_or_conv_p;
19036 bool friend_p = cp_parser_friend_p (decl_specifiers);
19037 tree pushed_scope = NULL_TREE;
19038 bool range_for_decl_p = false;
19039 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
19040 location_t tmp_init_loc = UNKNOWN_LOCATION;
19042 /* Gather the attributes that were provided with the
19043 decl-specifiers. */
19044 prefix_attributes = decl_specifiers->attributes;
19046 /* Assume that this is not the declarator for a function
19047 definition. */
19048 if (function_definition_p)
19049 *function_definition_p = false;
19051 /* Default arguments are only permitted for function parameters. */
19052 if (decl_spec_seq_has_spec_p (decl_specifiers, ds_typedef))
19053 parser->default_arg_ok_p = false;
19055 /* Defer access checks while parsing the declarator; we cannot know
19056 what names are accessible until we know what is being
19057 declared. */
19058 resume_deferring_access_checks ();
19060 token = cp_lexer_peek_token (parser->lexer);
19062 /* Parse the declarator. */
19063 declarator
19064 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
19065 &ctor_dtor_or_conv_p,
19066 /*parenthesized_p=*/NULL,
19067 member_p, friend_p);
19068 /* Gather up the deferred checks. */
19069 stop_deferring_access_checks ();
19071 parser->default_arg_ok_p = saved_default_arg_ok_p;
19073 /* If the DECLARATOR was erroneous, there's no need to go
19074 further. */
19075 if (declarator == cp_error_declarator)
19076 return error_mark_node;
19078 /* Check that the number of template-parameter-lists is OK. */
19079 if (!cp_parser_check_declarator_template_parameters (parser, declarator,
19080 token->location))
19081 return error_mark_node;
19083 if (declares_class_or_enum & 2)
19084 cp_parser_check_for_definition_in_return_type (declarator,
19085 decl_specifiers->type,
19086 decl_specifiers->locations[ds_type_spec]);
19088 /* Figure out what scope the entity declared by the DECLARATOR is
19089 located in. `grokdeclarator' sometimes changes the scope, so
19090 we compute it now. */
19091 scope = get_scope_of_declarator (declarator);
19093 /* Perform any lookups in the declared type which were thought to be
19094 dependent, but are not in the scope of the declarator. */
19095 decl_specifiers->type
19096 = maybe_update_decl_type (decl_specifiers->type, scope);
19098 /* If we're allowing GNU extensions, look for an
19099 asm-specification. */
19100 if (cp_parser_allow_gnu_extensions_p (parser))
19102 /* Look for an asm-specification. */
19103 asm_spec_start_token = cp_lexer_peek_token (parser->lexer);
19104 asm_specification = cp_parser_asm_specification_opt (parser);
19106 else
19107 asm_specification = NULL_TREE;
19109 /* Look for attributes. */
19110 attributes_start_token = cp_lexer_peek_token (parser->lexer);
19111 attributes = cp_parser_attributes_opt (parser);
19113 /* Peek at the next token. */
19114 token = cp_lexer_peek_token (parser->lexer);
19116 bool bogus_implicit_tmpl = false;
19118 if (function_declarator_p (declarator))
19120 /* Handle C++17 deduction guides. */
19121 if (!decl_specifiers->type
19122 && ctor_dtor_or_conv_p <= 0
19123 && cxx_dialect >= cxx1z)
19125 cp_declarator *id = get_id_declarator (declarator);
19126 tree name = id->u.id.unqualified_name;
19127 parser->scope = id->u.id.qualifying_scope;
19128 tree tmpl = cp_parser_lookup_name_simple (parser, name, id->id_loc);
19129 if (tmpl
19130 && (DECL_CLASS_TEMPLATE_P (tmpl)
19131 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl)))
19133 id->u.id.unqualified_name = dguide_name (tmpl);
19134 id->u.id.sfk = sfk_deduction_guide;
19135 ctor_dtor_or_conv_p = 1;
19139 /* Check to see if the token indicates the start of a
19140 function-definition. */
19141 if (cp_parser_token_starts_function_definition_p (token))
19143 if (!function_definition_allowed_p)
19145 /* If a function-definition should not appear here, issue an
19146 error message. */
19147 cp_parser_error (parser,
19148 "a function-definition is not allowed here");
19149 return error_mark_node;
19152 location_t func_brace_location
19153 = cp_lexer_peek_token (parser->lexer)->location;
19155 /* Neither attributes nor an asm-specification are allowed
19156 on a function-definition. */
19157 if (asm_specification)
19158 error_at (asm_spec_start_token->location,
19159 "an asm-specification is not allowed "
19160 "on a function-definition");
19161 if (attributes)
19162 error_at (attributes_start_token->location,
19163 "attributes are not allowed "
19164 "on a function-definition");
19165 /* This is a function-definition. */
19166 *function_definition_p = true;
19168 /* Parse the function definition. */
19169 if (member_p)
19170 decl = cp_parser_save_member_function_body (parser,
19171 decl_specifiers,
19172 declarator,
19173 prefix_attributes);
19174 else
19175 decl =
19176 (cp_parser_function_definition_from_specifiers_and_declarator
19177 (parser, decl_specifiers, prefix_attributes, declarator));
19179 if (decl != error_mark_node && DECL_STRUCT_FUNCTION (decl))
19181 /* This is where the prologue starts... */
19182 DECL_STRUCT_FUNCTION (decl)->function_start_locus
19183 = func_brace_location;
19186 return decl;
19189 else if (parser->fully_implicit_function_template_p)
19191 /* A non-template declaration involving a function parameter list
19192 containing an implicit template parameter will be made into a
19193 template. If the resulting declaration is not going to be an
19194 actual function then finish the template scope here to prevent it.
19195 An error message will be issued once we have a decl to talk about.
19197 FIXME probably we should do type deduction rather than create an
19198 implicit template, but the standard currently doesn't allow it. */
19199 bogus_implicit_tmpl = true;
19200 finish_fully_implicit_template (parser, NULL_TREE);
19203 /* [dcl.dcl]
19205 Only in function declarations for constructors, destructors, type
19206 conversions, and deduction guides can the decl-specifier-seq be omitted.
19208 We explicitly postpone this check past the point where we handle
19209 function-definitions because we tolerate function-definitions
19210 that are missing their return types in some modes. */
19211 if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
19213 cp_parser_error (parser,
19214 "expected constructor, destructor, or type conversion");
19215 return error_mark_node;
19218 /* An `=' or an `(', or an '{' in C++0x, indicates an initializer. */
19219 if (token->type == CPP_EQ
19220 || token->type == CPP_OPEN_PAREN
19221 || token->type == CPP_OPEN_BRACE)
19223 is_initialized = SD_INITIALIZED;
19224 initialization_kind = token->type;
19225 if (maybe_range_for_decl)
19226 *maybe_range_for_decl = error_mark_node;
19227 tmp_init_loc = token->location;
19228 if (init_loc && *init_loc == UNKNOWN_LOCATION)
19229 *init_loc = tmp_init_loc;
19231 if (token->type == CPP_EQ
19232 && function_declarator_p (declarator))
19234 cp_token *t2 = cp_lexer_peek_nth_token (parser->lexer, 2);
19235 if (t2->keyword == RID_DEFAULT)
19236 is_initialized = SD_DEFAULTED;
19237 else if (t2->keyword == RID_DELETE)
19238 is_initialized = SD_DELETED;
19241 else
19243 /* If the init-declarator isn't initialized and isn't followed by a
19244 `,' or `;', it's not a valid init-declarator. */
19245 if (token->type != CPP_COMMA
19246 && token->type != CPP_SEMICOLON)
19248 if (maybe_range_for_decl && *maybe_range_for_decl != error_mark_node)
19249 range_for_decl_p = true;
19250 else
19252 if (!maybe_range_for_decl)
19253 cp_parser_error (parser, "expected initializer");
19254 return error_mark_node;
19257 is_initialized = SD_UNINITIALIZED;
19258 initialization_kind = CPP_EOF;
19261 /* Because start_decl has side-effects, we should only call it if we
19262 know we're going ahead. By this point, we know that we cannot
19263 possibly be looking at any other construct. */
19264 cp_parser_commit_to_tentative_parse (parser);
19266 /* Enter the newly declared entry in the symbol table. If we're
19267 processing a declaration in a class-specifier, we wait until
19268 after processing the initializer. */
19269 if (!member_p)
19271 if (parser->in_unbraced_linkage_specification_p)
19272 decl_specifiers->storage_class = sc_extern;
19273 decl = start_decl (declarator, decl_specifiers,
19274 range_for_decl_p? SD_INITIALIZED : is_initialized,
19275 attributes, prefix_attributes, &pushed_scope);
19276 cp_finalize_omp_declare_simd (parser, decl);
19277 cp_finalize_oacc_routine (parser, decl, false);
19278 /* Adjust location of decl if declarator->id_loc is more appropriate:
19279 set, and decl wasn't merged with another decl, in which case its
19280 location would be different from input_location, and more accurate. */
19281 if (DECL_P (decl)
19282 && declarator->id_loc != UNKNOWN_LOCATION
19283 && DECL_SOURCE_LOCATION (decl) == input_location)
19284 DECL_SOURCE_LOCATION (decl) = declarator->id_loc;
19286 else if (scope)
19287 /* Enter the SCOPE. That way unqualified names appearing in the
19288 initializer will be looked up in SCOPE. */
19289 pushed_scope = push_scope (scope);
19291 /* Perform deferred access control checks, now that we know in which
19292 SCOPE the declared entity resides. */
19293 if (!member_p && decl)
19295 tree saved_current_function_decl = NULL_TREE;
19297 /* If the entity being declared is a function, pretend that we
19298 are in its scope. If it is a `friend', it may have access to
19299 things that would not otherwise be accessible. */
19300 if (TREE_CODE (decl) == FUNCTION_DECL)
19302 saved_current_function_decl = current_function_decl;
19303 current_function_decl = decl;
19306 /* Perform access checks for template parameters. */
19307 cp_parser_perform_template_parameter_access_checks (checks);
19309 /* Perform the access control checks for the declarator and the
19310 decl-specifiers. */
19311 perform_deferred_access_checks (tf_warning_or_error);
19313 /* Restore the saved value. */
19314 if (TREE_CODE (decl) == FUNCTION_DECL)
19315 current_function_decl = saved_current_function_decl;
19318 /* Parse the initializer. */
19319 initializer = NULL_TREE;
19320 is_direct_init = false;
19321 is_non_constant_init = true;
19322 if (is_initialized)
19324 if (function_declarator_p (declarator))
19326 if (initialization_kind == CPP_EQ)
19327 initializer = cp_parser_pure_specifier (parser);
19328 else
19330 /* If the declaration was erroneous, we don't really
19331 know what the user intended, so just silently
19332 consume the initializer. */
19333 if (decl != error_mark_node)
19334 error_at (tmp_init_loc, "initializer provided for function");
19335 cp_parser_skip_to_closing_parenthesis (parser,
19336 /*recovering=*/true,
19337 /*or_comma=*/false,
19338 /*consume_paren=*/true);
19341 else
19343 /* We want to record the extra mangling scope for in-class
19344 initializers of class members and initializers of static data
19345 member templates. The former involves deferring
19346 parsing of the initializer until end of class as with default
19347 arguments. So right here we only handle the latter. */
19348 if (!member_p && processing_template_decl)
19349 start_lambda_scope (decl);
19350 initializer = cp_parser_initializer (parser,
19351 &is_direct_init,
19352 &is_non_constant_init);
19353 if (!member_p && processing_template_decl)
19354 finish_lambda_scope ();
19355 if (initializer == error_mark_node)
19356 cp_parser_skip_to_end_of_statement (parser);
19360 /* The old parser allows attributes to appear after a parenthesized
19361 initializer. Mark Mitchell proposed removing this functionality
19362 on the GCC mailing lists on 2002-08-13. This parser accepts the
19363 attributes -- but ignores them. */
19364 if (cp_parser_allow_gnu_extensions_p (parser)
19365 && initialization_kind == CPP_OPEN_PAREN)
19366 if (cp_parser_attributes_opt (parser))
19367 warning (OPT_Wattributes,
19368 "attributes after parenthesized initializer ignored");
19370 /* And now complain about a non-function implicit template. */
19371 if (bogus_implicit_tmpl && decl != error_mark_node)
19372 error_at (DECL_SOURCE_LOCATION (decl),
19373 "non-function %qD declared as implicit template", decl);
19375 /* For an in-class declaration, use `grokfield' to create the
19376 declaration. */
19377 if (member_p)
19379 if (pushed_scope)
19381 pop_scope (pushed_scope);
19382 pushed_scope = NULL_TREE;
19384 decl = grokfield (declarator, decl_specifiers,
19385 initializer, !is_non_constant_init,
19386 /*asmspec=*/NULL_TREE,
19387 chainon (attributes, prefix_attributes));
19388 if (decl && TREE_CODE (decl) == FUNCTION_DECL)
19389 cp_parser_save_default_args (parser, decl);
19390 cp_finalize_omp_declare_simd (parser, decl);
19391 cp_finalize_oacc_routine (parser, decl, false);
19394 /* Finish processing the declaration. But, skip member
19395 declarations. */
19396 if (!member_p && decl && decl != error_mark_node && !range_for_decl_p)
19398 cp_finish_decl (decl,
19399 initializer, !is_non_constant_init,
19400 asm_specification,
19401 /* If the initializer is in parentheses, then this is
19402 a direct-initialization, which means that an
19403 `explicit' constructor is OK. Otherwise, an
19404 `explicit' constructor cannot be used. */
19405 ((is_direct_init || !is_initialized)
19406 ? LOOKUP_NORMAL : LOOKUP_IMPLICIT));
19408 else if ((cxx_dialect != cxx98) && friend_p
19409 && decl && TREE_CODE (decl) == FUNCTION_DECL)
19410 /* Core issue #226 (C++0x only): A default template-argument
19411 shall not be specified in a friend class template
19412 declaration. */
19413 check_default_tmpl_args (decl, current_template_parms, /*is_primary=*/true,
19414 /*is_partial=*/false, /*is_friend_decl=*/1);
19416 if (!friend_p && pushed_scope)
19417 pop_scope (pushed_scope);
19419 if (function_declarator_p (declarator)
19420 && parser->fully_implicit_function_template_p)
19422 if (member_p)
19423 decl = finish_fully_implicit_template (parser, decl);
19424 else
19425 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
19428 if (auto_result && is_initialized && decl_specifiers->type
19429 && type_uses_auto (decl_specifiers->type))
19430 *auto_result = strip_declarator_types (TREE_TYPE (decl), declarator);
19432 return decl;
19435 /* Parse a declarator.
19437 declarator:
19438 direct-declarator
19439 ptr-operator declarator
19441 abstract-declarator:
19442 ptr-operator abstract-declarator [opt]
19443 direct-abstract-declarator
19445 GNU Extensions:
19447 declarator:
19448 attributes [opt] direct-declarator
19449 attributes [opt] ptr-operator declarator
19451 abstract-declarator:
19452 attributes [opt] ptr-operator abstract-declarator [opt]
19453 attributes [opt] direct-abstract-declarator
19455 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
19456 detect constructors, destructors, deduction guides, or conversion operators.
19457 It is set to -1 if the declarator is a name, and +1 if it is a
19458 function. Otherwise it is set to zero. Usually you just want to
19459 test for >0, but internally the negative value is used.
19461 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
19462 a decl-specifier-seq unless it declares a constructor, destructor,
19463 or conversion. It might seem that we could check this condition in
19464 semantic analysis, rather than parsing, but that makes it difficult
19465 to handle something like `f()'. We want to notice that there are
19466 no decl-specifiers, and therefore realize that this is an
19467 expression, not a declaration.)
19469 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
19470 the declarator is a direct-declarator of the form "(...)".
19472 MEMBER_P is true iff this declarator is a member-declarator.
19474 FRIEND_P is true iff this declarator is a friend. */
19476 static cp_declarator *
19477 cp_parser_declarator (cp_parser* parser,
19478 cp_parser_declarator_kind dcl_kind,
19479 int* ctor_dtor_or_conv_p,
19480 bool* parenthesized_p,
19481 bool member_p, bool friend_p)
19483 cp_declarator *declarator;
19484 enum tree_code code;
19485 cp_cv_quals cv_quals;
19486 tree class_type;
19487 tree gnu_attributes = NULL_TREE, std_attributes = NULL_TREE;
19489 /* Assume this is not a constructor, destructor, or type-conversion
19490 operator. */
19491 if (ctor_dtor_or_conv_p)
19492 *ctor_dtor_or_conv_p = 0;
19494 if (cp_parser_allow_gnu_extensions_p (parser))
19495 gnu_attributes = cp_parser_gnu_attributes_opt (parser);
19497 /* Check for the ptr-operator production. */
19498 cp_parser_parse_tentatively (parser);
19499 /* Parse the ptr-operator. */
19500 code = cp_parser_ptr_operator (parser,
19501 &class_type,
19502 &cv_quals,
19503 &std_attributes);
19505 /* If that worked, then we have a ptr-operator. */
19506 if (cp_parser_parse_definitely (parser))
19508 /* If a ptr-operator was found, then this declarator was not
19509 parenthesized. */
19510 if (parenthesized_p)
19511 *parenthesized_p = true;
19512 /* The dependent declarator is optional if we are parsing an
19513 abstract-declarator. */
19514 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
19515 cp_parser_parse_tentatively (parser);
19517 /* Parse the dependent declarator. */
19518 declarator = cp_parser_declarator (parser, dcl_kind,
19519 /*ctor_dtor_or_conv_p=*/NULL,
19520 /*parenthesized_p=*/NULL,
19521 /*member_p=*/false,
19522 friend_p);
19524 /* If we are parsing an abstract-declarator, we must handle the
19525 case where the dependent declarator is absent. */
19526 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
19527 && !cp_parser_parse_definitely (parser))
19528 declarator = NULL;
19530 declarator = cp_parser_make_indirect_declarator
19531 (code, class_type, cv_quals, declarator, std_attributes);
19533 /* Everything else is a direct-declarator. */
19534 else
19536 if (parenthesized_p)
19537 *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
19538 CPP_OPEN_PAREN);
19539 declarator = cp_parser_direct_declarator (parser, dcl_kind,
19540 ctor_dtor_or_conv_p,
19541 member_p, friend_p);
19544 if (gnu_attributes && declarator && declarator != cp_error_declarator)
19545 declarator->attributes = gnu_attributes;
19546 return declarator;
19549 /* Parse a direct-declarator or direct-abstract-declarator.
19551 direct-declarator:
19552 declarator-id
19553 direct-declarator ( parameter-declaration-clause )
19554 cv-qualifier-seq [opt]
19555 ref-qualifier [opt]
19556 exception-specification [opt]
19557 direct-declarator [ constant-expression [opt] ]
19558 ( declarator )
19560 direct-abstract-declarator:
19561 direct-abstract-declarator [opt]
19562 ( parameter-declaration-clause )
19563 cv-qualifier-seq [opt]
19564 ref-qualifier [opt]
19565 exception-specification [opt]
19566 direct-abstract-declarator [opt] [ constant-expression [opt] ]
19567 ( abstract-declarator )
19569 Returns a representation of the declarator. DCL_KIND is
19570 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
19571 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
19572 we are parsing a direct-declarator. It is
19573 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
19574 of ambiguity we prefer an abstract declarator, as per
19575 [dcl.ambig.res]. CTOR_DTOR_OR_CONV_P, MEMBER_P, and FRIEND_P are
19576 as for cp_parser_declarator. */
19578 static cp_declarator *
19579 cp_parser_direct_declarator (cp_parser* parser,
19580 cp_parser_declarator_kind dcl_kind,
19581 int* ctor_dtor_or_conv_p,
19582 bool member_p, bool friend_p)
19584 cp_token *token;
19585 cp_declarator *declarator = NULL;
19586 tree scope = NULL_TREE;
19587 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
19588 bool saved_in_declarator_p = parser->in_declarator_p;
19589 bool first = true;
19590 tree pushed_scope = NULL_TREE;
19592 while (true)
19594 /* Peek at the next token. */
19595 token = cp_lexer_peek_token (parser->lexer);
19596 if (token->type == CPP_OPEN_PAREN)
19598 /* This is either a parameter-declaration-clause, or a
19599 parenthesized declarator. When we know we are parsing a
19600 named declarator, it must be a parenthesized declarator
19601 if FIRST is true. For instance, `(int)' is a
19602 parameter-declaration-clause, with an omitted
19603 direct-abstract-declarator. But `((*))', is a
19604 parenthesized abstract declarator. Finally, when T is a
19605 template parameter `(T)' is a
19606 parameter-declaration-clause, and not a parenthesized
19607 named declarator.
19609 We first try and parse a parameter-declaration-clause,
19610 and then try a nested declarator (if FIRST is true).
19612 It is not an error for it not to be a
19613 parameter-declaration-clause, even when FIRST is
19614 false. Consider,
19616 int i (int);
19617 int i (3);
19619 The first is the declaration of a function while the
19620 second is the definition of a variable, including its
19621 initializer.
19623 Having seen only the parenthesis, we cannot know which of
19624 these two alternatives should be selected. Even more
19625 complex are examples like:
19627 int i (int (a));
19628 int i (int (3));
19630 The former is a function-declaration; the latter is a
19631 variable initialization.
19633 Thus again, we try a parameter-declaration-clause, and if
19634 that fails, we back out and return. */
19636 if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
19638 tree params;
19639 bool is_declarator = false;
19641 /* In a member-declarator, the only valid interpretation
19642 of a parenthesis is the start of a
19643 parameter-declaration-clause. (It is invalid to
19644 initialize a static data member with a parenthesized
19645 initializer; only the "=" form of initialization is
19646 permitted.) */
19647 if (!member_p)
19648 cp_parser_parse_tentatively (parser);
19650 /* Consume the `('. */
19651 cp_lexer_consume_token (parser->lexer);
19652 if (first)
19654 /* If this is going to be an abstract declarator, we're
19655 in a declarator and we can't have default args. */
19656 parser->default_arg_ok_p = false;
19657 parser->in_declarator_p = true;
19660 begin_scope (sk_function_parms, NULL_TREE);
19662 /* Parse the parameter-declaration-clause. */
19663 params = cp_parser_parameter_declaration_clause (parser);
19665 /* Consume the `)'. */
19666 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
19668 /* If all went well, parse the cv-qualifier-seq,
19669 ref-qualifier and the exception-specification. */
19670 if (member_p || cp_parser_parse_definitely (parser))
19672 cp_cv_quals cv_quals;
19673 cp_virt_specifiers virt_specifiers;
19674 cp_ref_qualifier ref_qual;
19675 tree exception_specification;
19676 tree late_return;
19677 tree attrs;
19678 bool memfn = (member_p || (pushed_scope
19679 && CLASS_TYPE_P (pushed_scope)));
19681 is_declarator = true;
19683 if (ctor_dtor_or_conv_p)
19684 *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
19685 first = false;
19687 /* Parse the cv-qualifier-seq. */
19688 cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
19689 /* Parse the ref-qualifier. */
19690 ref_qual = cp_parser_ref_qualifier_opt (parser);
19691 /* Parse the tx-qualifier. */
19692 tree tx_qual = cp_parser_tx_qualifier_opt (parser);
19693 /* And the exception-specification. */
19694 exception_specification
19695 = cp_parser_exception_specification_opt (parser);
19697 attrs = cp_parser_std_attribute_spec_seq (parser);
19699 /* In here, we handle cases where attribute is used after
19700 the function declaration. For example:
19701 void func (int x) __attribute__((vector(..))); */
19702 tree gnu_attrs = NULL_TREE;
19703 if (flag_cilkplus
19704 && cp_next_tokens_can_be_gnu_attribute_p (parser))
19706 cp_parser_parse_tentatively (parser);
19707 tree attr = cp_parser_gnu_attributes_opt (parser);
19708 if (cp_lexer_next_token_is_not (parser->lexer,
19709 CPP_SEMICOLON)
19710 && cp_lexer_next_token_is_not (parser->lexer,
19711 CPP_OPEN_BRACE))
19712 cp_parser_abort_tentative_parse (parser);
19713 else if (!cp_parser_parse_definitely (parser))
19715 else
19716 gnu_attrs = attr;
19718 tree requires_clause = NULL_TREE;
19719 late_return = (cp_parser_late_return_type_opt
19720 (parser, declarator, requires_clause,
19721 memfn ? cv_quals : -1));
19723 /* Parse the virt-specifier-seq. */
19724 virt_specifiers = cp_parser_virt_specifier_seq_opt (parser);
19726 /* Create the function-declarator. */
19727 declarator = make_call_declarator (declarator,
19728 params,
19729 cv_quals,
19730 virt_specifiers,
19731 ref_qual,
19732 tx_qual,
19733 exception_specification,
19734 late_return,
19735 requires_clause);
19736 declarator->std_attributes = attrs;
19737 declarator->attributes = gnu_attrs;
19738 /* Any subsequent parameter lists are to do with
19739 return type, so are not those of the declared
19740 function. */
19741 parser->default_arg_ok_p = false;
19744 /* Remove the function parms from scope. */
19745 pop_bindings_and_leave_scope ();
19747 if (is_declarator)
19748 /* Repeat the main loop. */
19749 continue;
19752 /* If this is the first, we can try a parenthesized
19753 declarator. */
19754 if (first)
19756 bool saved_in_type_id_in_expr_p;
19758 parser->default_arg_ok_p = saved_default_arg_ok_p;
19759 parser->in_declarator_p = saved_in_declarator_p;
19761 /* Consume the `('. */
19762 cp_lexer_consume_token (parser->lexer);
19763 /* Parse the nested declarator. */
19764 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
19765 parser->in_type_id_in_expr_p = true;
19766 declarator
19767 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
19768 /*parenthesized_p=*/NULL,
19769 member_p, friend_p);
19770 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
19771 first = false;
19772 /* Expect a `)'. */
19773 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
19774 declarator = cp_error_declarator;
19775 if (declarator == cp_error_declarator)
19776 break;
19778 goto handle_declarator;
19780 /* Otherwise, we must be done. */
19781 else
19782 break;
19784 else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
19785 && token->type == CPP_OPEN_SQUARE
19786 && !cp_next_tokens_can_be_attribute_p (parser))
19788 /* Parse an array-declarator. */
19789 tree bounds, attrs;
19791 if (ctor_dtor_or_conv_p)
19792 *ctor_dtor_or_conv_p = 0;
19794 first = false;
19795 parser->default_arg_ok_p = false;
19796 parser->in_declarator_p = true;
19797 /* Consume the `['. */
19798 cp_lexer_consume_token (parser->lexer);
19799 /* Peek at the next token. */
19800 token = cp_lexer_peek_token (parser->lexer);
19801 /* If the next token is `]', then there is no
19802 constant-expression. */
19803 if (token->type != CPP_CLOSE_SQUARE)
19805 bool non_constant_p;
19806 bounds
19807 = cp_parser_constant_expression (parser,
19808 /*allow_non_constant=*/true,
19809 &non_constant_p);
19810 if (!non_constant_p)
19811 /* OK */;
19812 else if (error_operand_p (bounds))
19813 /* Already gave an error. */;
19814 else if (!parser->in_function_body
19815 || current_binding_level->kind == sk_function_parms)
19817 /* Normally, the array bound must be an integral constant
19818 expression. However, as an extension, we allow VLAs
19819 in function scopes as long as they aren't part of a
19820 parameter declaration. */
19821 cp_parser_error (parser,
19822 "array bound is not an integer constant");
19823 bounds = error_mark_node;
19825 else if (processing_template_decl
19826 && !type_dependent_expression_p (bounds))
19828 /* Remember this wasn't a constant-expression. */
19829 bounds = build_nop (TREE_TYPE (bounds), bounds);
19830 TREE_SIDE_EFFECTS (bounds) = 1;
19833 else
19834 bounds = NULL_TREE;
19835 /* Look for the closing `]'. */
19836 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
19838 declarator = cp_error_declarator;
19839 break;
19842 attrs = cp_parser_std_attribute_spec_seq (parser);
19843 declarator = make_array_declarator (declarator, bounds);
19844 declarator->std_attributes = attrs;
19846 else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
19849 tree qualifying_scope;
19850 tree unqualified_name;
19851 tree attrs;
19852 special_function_kind sfk;
19853 bool abstract_ok;
19854 bool pack_expansion_p = false;
19855 cp_token *declarator_id_start_token;
19857 /* Parse a declarator-id */
19858 abstract_ok = (dcl_kind == CP_PARSER_DECLARATOR_EITHER);
19859 if (abstract_ok)
19861 cp_parser_parse_tentatively (parser);
19863 /* If we see an ellipsis, we should be looking at a
19864 parameter pack. */
19865 if (token->type == CPP_ELLIPSIS)
19867 /* Consume the `...' */
19868 cp_lexer_consume_token (parser->lexer);
19870 pack_expansion_p = true;
19874 declarator_id_start_token = cp_lexer_peek_token (parser->lexer);
19875 unqualified_name
19876 = cp_parser_declarator_id (parser, /*optional_p=*/abstract_ok);
19877 qualifying_scope = parser->scope;
19878 if (abstract_ok)
19880 bool okay = false;
19882 if (!unqualified_name && pack_expansion_p)
19884 /* Check whether an error occurred. */
19885 okay = !cp_parser_error_occurred (parser);
19887 /* We already consumed the ellipsis to mark a
19888 parameter pack, but we have no way to report it,
19889 so abort the tentative parse. We will be exiting
19890 immediately anyway. */
19891 cp_parser_abort_tentative_parse (parser);
19893 else
19894 okay = cp_parser_parse_definitely (parser);
19896 if (!okay)
19897 unqualified_name = error_mark_node;
19898 else if (unqualified_name
19899 && (qualifying_scope
19900 || (!identifier_p (unqualified_name))))
19902 cp_parser_error (parser, "expected unqualified-id");
19903 unqualified_name = error_mark_node;
19907 if (!unqualified_name)
19908 return NULL;
19909 if (unqualified_name == error_mark_node)
19911 declarator = cp_error_declarator;
19912 pack_expansion_p = false;
19913 declarator->parameter_pack_p = false;
19914 break;
19917 attrs = cp_parser_std_attribute_spec_seq (parser);
19919 if (qualifying_scope && at_namespace_scope_p ()
19920 && TREE_CODE (qualifying_scope) == TYPENAME_TYPE)
19922 /* In the declaration of a member of a template class
19923 outside of the class itself, the SCOPE will sometimes
19924 be a TYPENAME_TYPE. For example, given:
19926 template <typename T>
19927 int S<T>::R::i = 3;
19929 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
19930 this context, we must resolve S<T>::R to an ordinary
19931 type, rather than a typename type.
19933 The reason we normally avoid resolving TYPENAME_TYPEs
19934 is that a specialization of `S' might render
19935 `S<T>::R' not a type. However, if `S' is
19936 specialized, then this `i' will not be used, so there
19937 is no harm in resolving the types here. */
19938 tree type;
19940 /* Resolve the TYPENAME_TYPE. */
19941 type = resolve_typename_type (qualifying_scope,
19942 /*only_current_p=*/false);
19943 /* If that failed, the declarator is invalid. */
19944 if (TREE_CODE (type) == TYPENAME_TYPE)
19946 if (typedef_variant_p (type))
19947 error_at (declarator_id_start_token->location,
19948 "cannot define member of dependent typedef "
19949 "%qT", type);
19950 else
19951 error_at (declarator_id_start_token->location,
19952 "%<%T::%E%> is not a type",
19953 TYPE_CONTEXT (qualifying_scope),
19954 TYPE_IDENTIFIER (qualifying_scope));
19956 qualifying_scope = type;
19959 sfk = sfk_none;
19961 if (unqualified_name)
19963 tree class_type;
19965 if (qualifying_scope
19966 && CLASS_TYPE_P (qualifying_scope))
19967 class_type = qualifying_scope;
19968 else
19969 class_type = current_class_type;
19971 if (TREE_CODE (unqualified_name) == TYPE_DECL)
19973 tree name_type = TREE_TYPE (unqualified_name);
19974 if (class_type && same_type_p (name_type, class_type))
19976 if (qualifying_scope
19977 && CLASSTYPE_USE_TEMPLATE (name_type))
19979 error_at (declarator_id_start_token->location,
19980 "invalid use of constructor as a template");
19981 inform (declarator_id_start_token->location,
19982 "use %<%T::%D%> instead of %<%T::%D%> to "
19983 "name the constructor in a qualified name",
19984 class_type,
19985 DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
19986 class_type, name_type);
19987 declarator = cp_error_declarator;
19988 break;
19990 else
19991 unqualified_name = constructor_name (class_type);
19993 else
19995 /* We do not attempt to print the declarator
19996 here because we do not have enough
19997 information about its original syntactic
19998 form. */
19999 cp_parser_error (parser, "invalid declarator");
20000 declarator = cp_error_declarator;
20001 break;
20005 if (class_type)
20007 if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
20008 sfk = sfk_destructor;
20009 else if (IDENTIFIER_TYPENAME_P (unqualified_name))
20010 sfk = sfk_conversion;
20011 else if (/* There's no way to declare a constructor
20012 for an unnamed type, even if the type
20013 got a name for linkage purposes. */
20014 !TYPE_WAS_UNNAMED (class_type)
20015 /* Handle correctly (c++/19200):
20017 struct S {
20018 struct T{};
20019 friend void S(T);
20022 and also:
20024 namespace N {
20025 void S();
20028 struct S {
20029 friend void N::S();
20030 }; */
20031 && !(friend_p
20032 && class_type != qualifying_scope)
20033 && constructor_name_p (unqualified_name,
20034 class_type))
20036 unqualified_name = constructor_name (class_type);
20037 sfk = sfk_constructor;
20039 else if (is_overloaded_fn (unqualified_name)
20040 && DECL_CONSTRUCTOR_P (get_first_fn
20041 (unqualified_name)))
20042 sfk = sfk_constructor;
20044 if (ctor_dtor_or_conv_p && sfk != sfk_none)
20045 *ctor_dtor_or_conv_p = -1;
20048 declarator = make_id_declarator (qualifying_scope,
20049 unqualified_name,
20050 sfk);
20051 declarator->std_attributes = attrs;
20052 declarator->id_loc = token->location;
20053 declarator->parameter_pack_p = pack_expansion_p;
20055 if (pack_expansion_p)
20056 maybe_warn_variadic_templates ();
20059 handle_declarator:;
20060 scope = get_scope_of_declarator (declarator);
20061 if (scope)
20063 /* Any names that appear after the declarator-id for a
20064 member are looked up in the containing scope. */
20065 if (at_function_scope_p ())
20067 /* But declarations with qualified-ids can't appear in a
20068 function. */
20069 cp_parser_error (parser, "qualified-id in declaration");
20070 declarator = cp_error_declarator;
20071 break;
20073 pushed_scope = push_scope (scope);
20075 parser->in_declarator_p = true;
20076 if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
20077 || (declarator && declarator->kind == cdk_id))
20078 /* Default args are only allowed on function
20079 declarations. */
20080 parser->default_arg_ok_p = saved_default_arg_ok_p;
20081 else
20082 parser->default_arg_ok_p = false;
20084 first = false;
20086 /* We're done. */
20087 else
20088 break;
20091 /* For an abstract declarator, we might wind up with nothing at this
20092 point. That's an error; the declarator is not optional. */
20093 if (!declarator)
20094 cp_parser_error (parser, "expected declarator");
20096 /* If we entered a scope, we must exit it now. */
20097 if (pushed_scope)
20098 pop_scope (pushed_scope);
20100 parser->default_arg_ok_p = saved_default_arg_ok_p;
20101 parser->in_declarator_p = saved_in_declarator_p;
20103 return declarator;
20106 /* Parse a ptr-operator.
20108 ptr-operator:
20109 * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
20110 * cv-qualifier-seq [opt]
20112 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
20113 nested-name-specifier * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
20115 GNU Extension:
20117 ptr-operator:
20118 & cv-qualifier-seq [opt]
20120 Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
20121 Returns ADDR_EXPR if a reference was used, or NON_LVALUE_EXPR for
20122 an rvalue reference. In the case of a pointer-to-member, *TYPE is
20123 filled in with the TYPE containing the member. *CV_QUALS is
20124 filled in with the cv-qualifier-seq, or TYPE_UNQUALIFIED, if there
20125 are no cv-qualifiers. Returns ERROR_MARK if an error occurred.
20126 Note that the tree codes returned by this function have nothing
20127 to do with the types of trees that will be eventually be created
20128 to represent the pointer or reference type being parsed. They are
20129 just constants with suggestive names. */
20130 static enum tree_code
20131 cp_parser_ptr_operator (cp_parser* parser,
20132 tree* type,
20133 cp_cv_quals *cv_quals,
20134 tree *attributes)
20136 enum tree_code code = ERROR_MARK;
20137 cp_token *token;
20138 tree attrs = NULL_TREE;
20140 /* Assume that it's not a pointer-to-member. */
20141 *type = NULL_TREE;
20142 /* And that there are no cv-qualifiers. */
20143 *cv_quals = TYPE_UNQUALIFIED;
20145 /* Peek at the next token. */
20146 token = cp_lexer_peek_token (parser->lexer);
20148 /* If it's a `*', `&' or `&&' we have a pointer or reference. */
20149 if (token->type == CPP_MULT)
20150 code = INDIRECT_REF;
20151 else if (token->type == CPP_AND)
20152 code = ADDR_EXPR;
20153 else if ((cxx_dialect != cxx98) &&
20154 token->type == CPP_AND_AND) /* C++0x only */
20155 code = NON_LVALUE_EXPR;
20157 if (code != ERROR_MARK)
20159 /* Consume the `*', `&' or `&&'. */
20160 cp_lexer_consume_token (parser->lexer);
20162 /* A `*' can be followed by a cv-qualifier-seq, and so can a
20163 `&', if we are allowing GNU extensions. (The only qualifier
20164 that can legally appear after `&' is `restrict', but that is
20165 enforced during semantic analysis. */
20166 if (code == INDIRECT_REF
20167 || cp_parser_allow_gnu_extensions_p (parser))
20168 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
20170 attrs = cp_parser_std_attribute_spec_seq (parser);
20171 if (attributes != NULL)
20172 *attributes = attrs;
20174 else
20176 /* Try the pointer-to-member case. */
20177 cp_parser_parse_tentatively (parser);
20178 /* Look for the optional `::' operator. */
20179 cp_parser_global_scope_opt (parser,
20180 /*current_scope_valid_p=*/false);
20181 /* Look for the nested-name specifier. */
20182 token = cp_lexer_peek_token (parser->lexer);
20183 cp_parser_nested_name_specifier (parser,
20184 /*typename_keyword_p=*/false,
20185 /*check_dependency_p=*/true,
20186 /*type_p=*/false,
20187 /*is_declaration=*/false);
20188 /* If we found it, and the next token is a `*', then we are
20189 indeed looking at a pointer-to-member operator. */
20190 if (!cp_parser_error_occurred (parser)
20191 && cp_parser_require (parser, CPP_MULT, RT_MULT))
20193 /* Indicate that the `*' operator was used. */
20194 code = INDIRECT_REF;
20196 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
20197 error_at (token->location, "%qD is a namespace", parser->scope);
20198 else if (TREE_CODE (parser->scope) == ENUMERAL_TYPE)
20199 error_at (token->location, "cannot form pointer to member of "
20200 "non-class %q#T", parser->scope);
20201 else
20203 /* The type of which the member is a member is given by the
20204 current SCOPE. */
20205 *type = parser->scope;
20206 /* The next name will not be qualified. */
20207 parser->scope = NULL_TREE;
20208 parser->qualifying_scope = NULL_TREE;
20209 parser->object_scope = NULL_TREE;
20210 /* Look for optional c++11 attributes. */
20211 attrs = cp_parser_std_attribute_spec_seq (parser);
20212 if (attributes != NULL)
20213 *attributes = attrs;
20214 /* Look for the optional cv-qualifier-seq. */
20215 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
20218 /* If that didn't work we don't have a ptr-operator. */
20219 if (!cp_parser_parse_definitely (parser))
20220 cp_parser_error (parser, "expected ptr-operator");
20223 return code;
20226 /* Parse an (optional) cv-qualifier-seq.
20228 cv-qualifier-seq:
20229 cv-qualifier cv-qualifier-seq [opt]
20231 cv-qualifier:
20232 const
20233 volatile
20235 GNU Extension:
20237 cv-qualifier:
20238 __restrict__
20240 Returns a bitmask representing the cv-qualifiers. */
20242 static cp_cv_quals
20243 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
20245 cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
20247 while (true)
20249 cp_token *token;
20250 cp_cv_quals cv_qualifier;
20252 /* Peek at the next token. */
20253 token = cp_lexer_peek_token (parser->lexer);
20254 /* See if it's a cv-qualifier. */
20255 switch (token->keyword)
20257 case RID_CONST:
20258 cv_qualifier = TYPE_QUAL_CONST;
20259 break;
20261 case RID_VOLATILE:
20262 cv_qualifier = TYPE_QUAL_VOLATILE;
20263 break;
20265 case RID_RESTRICT:
20266 cv_qualifier = TYPE_QUAL_RESTRICT;
20267 break;
20269 default:
20270 cv_qualifier = TYPE_UNQUALIFIED;
20271 break;
20274 if (!cv_qualifier)
20275 break;
20277 if (cv_quals & cv_qualifier)
20279 error_at (token->location, "duplicate cv-qualifier");
20280 cp_lexer_purge_token (parser->lexer);
20282 else
20284 cp_lexer_consume_token (parser->lexer);
20285 cv_quals |= cv_qualifier;
20289 return cv_quals;
20292 /* Parse an (optional) ref-qualifier
20294 ref-qualifier:
20298 Returns cp_ref_qualifier representing ref-qualifier. */
20300 static cp_ref_qualifier
20301 cp_parser_ref_qualifier_opt (cp_parser* parser)
20303 cp_ref_qualifier ref_qual = REF_QUAL_NONE;
20305 /* Don't try to parse bitwise '&' as a ref-qualifier (c++/57532). */
20306 if (cxx_dialect < cxx11 && cp_parser_parsing_tentatively (parser))
20307 return ref_qual;
20309 while (true)
20311 cp_ref_qualifier curr_ref_qual = REF_QUAL_NONE;
20312 cp_token *token = cp_lexer_peek_token (parser->lexer);
20314 switch (token->type)
20316 case CPP_AND:
20317 curr_ref_qual = REF_QUAL_LVALUE;
20318 break;
20320 case CPP_AND_AND:
20321 curr_ref_qual = REF_QUAL_RVALUE;
20322 break;
20324 default:
20325 curr_ref_qual = REF_QUAL_NONE;
20326 break;
20329 if (!curr_ref_qual)
20330 break;
20331 else if (ref_qual)
20333 error_at (token->location, "multiple ref-qualifiers");
20334 cp_lexer_purge_token (parser->lexer);
20336 else
20338 ref_qual = curr_ref_qual;
20339 cp_lexer_consume_token (parser->lexer);
20343 return ref_qual;
20346 /* Parse an optional tx-qualifier.
20348 tx-qualifier:
20349 transaction_safe
20350 transaction_safe_dynamic */
20352 static tree
20353 cp_parser_tx_qualifier_opt (cp_parser *parser)
20355 cp_token *token = cp_lexer_peek_token (parser->lexer);
20356 if (token->type == CPP_NAME)
20358 tree name = token->u.value;
20359 const char *p = IDENTIFIER_POINTER (name);
20360 const int len = strlen ("transaction_safe");
20361 if (!strncmp (p, "transaction_safe", len))
20363 p += len;
20364 if (*p == '\0'
20365 || !strcmp (p, "_dynamic"))
20367 cp_lexer_consume_token (parser->lexer);
20368 if (!flag_tm)
20370 error ("%E requires %<-fgnu-tm%>", name);
20371 return NULL_TREE;
20373 else
20374 return name;
20378 return NULL_TREE;
20381 /* Parse an (optional) virt-specifier-seq.
20383 virt-specifier-seq:
20384 virt-specifier virt-specifier-seq [opt]
20386 virt-specifier:
20387 override
20388 final
20390 Returns a bitmask representing the virt-specifiers. */
20392 static cp_virt_specifiers
20393 cp_parser_virt_specifier_seq_opt (cp_parser* parser)
20395 cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
20397 while (true)
20399 cp_token *token;
20400 cp_virt_specifiers virt_specifier;
20402 /* Peek at the next token. */
20403 token = cp_lexer_peek_token (parser->lexer);
20404 /* See if it's a virt-specifier-qualifier. */
20405 if (token->type != CPP_NAME)
20406 break;
20407 if (!strcmp (IDENTIFIER_POINTER(token->u.value), "override"))
20409 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS);
20410 virt_specifier = VIRT_SPEC_OVERRIDE;
20412 else if (!strcmp (IDENTIFIER_POINTER(token->u.value), "final"))
20414 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS);
20415 virt_specifier = VIRT_SPEC_FINAL;
20417 else if (!strcmp (IDENTIFIER_POINTER(token->u.value), "__final"))
20419 virt_specifier = VIRT_SPEC_FINAL;
20421 else
20422 break;
20424 if (virt_specifiers & virt_specifier)
20426 error_at (token->location, "duplicate virt-specifier");
20427 cp_lexer_purge_token (parser->lexer);
20429 else
20431 cp_lexer_consume_token (parser->lexer);
20432 virt_specifiers |= virt_specifier;
20435 return virt_specifiers;
20438 /* Used by handling of trailing-return-types and NSDMI, in which 'this'
20439 is in scope even though it isn't real. */
20441 void
20442 inject_this_parameter (tree ctype, cp_cv_quals quals)
20444 tree this_parm;
20446 if (current_class_ptr)
20448 /* We don't clear this between NSDMIs. Is it already what we want? */
20449 tree type = TREE_TYPE (TREE_TYPE (current_class_ptr));
20450 if (same_type_ignoring_top_level_qualifiers_p (ctype, type)
20451 && cp_type_quals (type) == quals)
20452 return;
20455 this_parm = build_this_parm (ctype, quals);
20456 /* Clear this first to avoid shortcut in cp_build_indirect_ref. */
20457 current_class_ptr = NULL_TREE;
20458 current_class_ref
20459 = cp_build_indirect_ref (this_parm, RO_NULL, tf_warning_or_error);
20460 current_class_ptr = this_parm;
20463 /* Return true iff our current scope is a non-static data member
20464 initializer. */
20466 bool
20467 parsing_nsdmi (void)
20469 /* We recognize NSDMI context by the context-less 'this' pointer set up
20470 by the function above. */
20471 if (current_class_ptr
20472 && TREE_CODE (current_class_ptr) == PARM_DECL
20473 && DECL_CONTEXT (current_class_ptr) == NULL_TREE)
20474 return true;
20475 return false;
20478 /* Return true iff our current scope is a default capturing generic lambda
20479 defined within a template. FIXME: This is part of a workaround (see
20480 semantics.c) to handle building lambda closure types correctly in templates
20481 which we ultimately want to defer to instantiation time. */
20483 bool
20484 parsing_default_capturing_generic_lambda_in_template (void)
20486 if (!processing_template_decl || !current_class_type)
20487 return false;
20489 tree lam = CLASSTYPE_LAMBDA_EXPR (current_class_type);
20490 if (!lam || LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lam) == CPLD_NONE)
20491 return false;
20493 tree callop = lambda_function (lam);
20494 if (!callop)
20495 return false;
20497 return (DECL_TEMPLATE_INFO (callop)
20498 && (DECL_TEMPLATE_RESULT (DECL_TI_TEMPLATE (callop)) == callop)
20499 && ((current_nonlambda_class_type ()
20500 && CLASSTYPE_TEMPLATE_INFO (current_nonlambda_class_type ()))
20501 || ((current_nonlambda_function ()
20502 && DECL_TEMPLATE_INFO (current_nonlambda_function ())))));
20505 /* Parse a late-specified return type, if any. This is not a separate
20506 non-terminal, but part of a function declarator, which looks like
20508 -> trailing-type-specifier-seq abstract-declarator(opt)
20510 Returns the type indicated by the type-id.
20512 In addition to this, parse any queued up #pragma omp declare simd
20513 clauses, Cilk Plus SIMD-enabled functions' vector attributes, and
20514 #pragma acc routine clauses.
20516 QUALS is either a bitmask of cv_qualifiers or -1 for a non-member
20517 function. */
20519 static tree
20520 cp_parser_late_return_type_opt (cp_parser* parser, cp_declarator *declarator,
20521 tree& requires_clause, cp_cv_quals quals)
20523 cp_token *token;
20524 tree type = NULL_TREE;
20525 bool declare_simd_p = (parser->omp_declare_simd
20526 && declarator
20527 && declarator->kind == cdk_id);
20529 bool cilk_simd_fn_vector_p = (parser->cilk_simd_fn_info
20530 && declarator && declarator->kind == cdk_id);
20532 bool oacc_routine_p = (parser->oacc_routine
20533 && declarator
20534 && declarator->kind == cdk_id);
20536 /* Peek at the next token. */
20537 token = cp_lexer_peek_token (parser->lexer);
20538 /* A late-specified return type is indicated by an initial '->'. */
20539 if (token->type != CPP_DEREF
20540 && token->keyword != RID_REQUIRES
20541 && !(token->type == CPP_NAME
20542 && token->u.value == ridpointers[RID_REQUIRES])
20543 && !(declare_simd_p || cilk_simd_fn_vector_p || oacc_routine_p))
20544 return NULL_TREE;
20546 tree save_ccp = current_class_ptr;
20547 tree save_ccr = current_class_ref;
20548 if (quals >= 0)
20550 /* DR 1207: 'this' is in scope in the trailing return type. */
20551 inject_this_parameter (current_class_type, quals);
20554 if (token->type == CPP_DEREF)
20556 /* Consume the ->. */
20557 cp_lexer_consume_token (parser->lexer);
20559 type = cp_parser_trailing_type_id (parser);
20562 /* Function declarations may be followed by a trailing
20563 requires-clause. */
20564 requires_clause = cp_parser_requires_clause_opt (parser);
20566 if (cilk_simd_fn_vector_p)
20567 declarator->attributes
20568 = cp_parser_late_parsing_cilk_simd_fn_info (parser,
20569 declarator->attributes);
20570 if (declare_simd_p)
20571 declarator->attributes
20572 = cp_parser_late_parsing_omp_declare_simd (parser,
20573 declarator->attributes);
20574 if (oacc_routine_p)
20575 declarator->attributes
20576 = cp_parser_late_parsing_oacc_routine (parser,
20577 declarator->attributes);
20579 if (quals >= 0)
20581 current_class_ptr = save_ccp;
20582 current_class_ref = save_ccr;
20585 return type;
20588 /* Parse a declarator-id.
20590 declarator-id:
20591 id-expression
20592 :: [opt] nested-name-specifier [opt] type-name
20594 In the `id-expression' case, the value returned is as for
20595 cp_parser_id_expression if the id-expression was an unqualified-id.
20596 If the id-expression was a qualified-id, then a SCOPE_REF is
20597 returned. The first operand is the scope (either a NAMESPACE_DECL
20598 or TREE_TYPE), but the second is still just a representation of an
20599 unqualified-id. */
20601 static tree
20602 cp_parser_declarator_id (cp_parser* parser, bool optional_p)
20604 tree id;
20605 /* The expression must be an id-expression. Assume that qualified
20606 names are the names of types so that:
20608 template <class T>
20609 int S<T>::R::i = 3;
20611 will work; we must treat `S<T>::R' as the name of a type.
20612 Similarly, assume that qualified names are templates, where
20613 required, so that:
20615 template <class T>
20616 int S<T>::R<T>::i = 3;
20618 will work, too. */
20619 id = cp_parser_id_expression (parser,
20620 /*template_keyword_p=*/false,
20621 /*check_dependency_p=*/false,
20622 /*template_p=*/NULL,
20623 /*declarator_p=*/true,
20624 optional_p);
20625 if (id && BASELINK_P (id))
20626 id = BASELINK_FUNCTIONS (id);
20627 return id;
20630 /* Parse a type-id.
20632 type-id:
20633 type-specifier-seq abstract-declarator [opt]
20635 Returns the TYPE specified. */
20637 static tree
20638 cp_parser_type_id_1 (cp_parser* parser, bool is_template_arg,
20639 bool is_trailing_return)
20641 cp_decl_specifier_seq type_specifier_seq;
20642 cp_declarator *abstract_declarator;
20644 /* Parse the type-specifier-seq. */
20645 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
20646 is_trailing_return,
20647 &type_specifier_seq);
20648 if (is_template_arg && type_specifier_seq.type
20649 && TREE_CODE (type_specifier_seq.type) == TEMPLATE_TYPE_PARM
20650 && CLASS_PLACEHOLDER_TEMPLATE (type_specifier_seq.type))
20651 /* A bare template name as a template argument is a template template
20652 argument, not a placeholder, so fail parsing it as a type argument. */
20654 gcc_assert (cp_parser_uncommitted_to_tentative_parse_p (parser));
20655 cp_parser_simulate_error (parser);
20656 return error_mark_node;
20658 if (type_specifier_seq.type == error_mark_node)
20659 return error_mark_node;
20661 /* There might or might not be an abstract declarator. */
20662 cp_parser_parse_tentatively (parser);
20663 /* Look for the declarator. */
20664 abstract_declarator
20665 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
20666 /*parenthesized_p=*/NULL,
20667 /*member_p=*/false,
20668 /*friend_p=*/false);
20669 /* Check to see if there really was a declarator. */
20670 if (!cp_parser_parse_definitely (parser))
20671 abstract_declarator = NULL;
20673 if (type_specifier_seq.type
20674 /* The concepts TS allows 'auto' as a type-id. */
20675 && (!flag_concepts || parser->in_type_id_in_expr_p)
20676 /* None of the valid uses of 'auto' in C++14 involve the type-id
20677 nonterminal, but it is valid in a trailing-return-type. */
20678 && !(cxx_dialect >= cxx14 && is_trailing_return))
20679 if (tree auto_node = type_uses_auto (type_specifier_seq.type))
20681 /* A type-id with type 'auto' is only ok if the abstract declarator
20682 is a function declarator with a late-specified return type.
20684 A type-id with 'auto' is also valid in a trailing-return-type
20685 in a compound-requirement. */
20686 if (abstract_declarator
20687 && abstract_declarator->kind == cdk_function
20688 && abstract_declarator->u.function.late_return_type)
20689 /* OK */;
20690 else if (parser->in_result_type_constraint_p)
20691 /* OK */;
20692 else
20694 location_t loc = type_specifier_seq.locations[ds_type_spec];
20695 if (tree tmpl = CLASS_PLACEHOLDER_TEMPLATE (auto_node))
20697 error_at (loc, "missing template arguments after %qT",
20698 auto_node);
20699 inform (DECL_SOURCE_LOCATION (tmpl), "%qD declared here",
20700 tmpl);
20702 else
20703 error_at (loc, "invalid use of %qT", auto_node);
20704 return error_mark_node;
20708 return groktypename (&type_specifier_seq, abstract_declarator,
20709 is_template_arg);
20712 static tree
20713 cp_parser_type_id (cp_parser *parser)
20715 return cp_parser_type_id_1 (parser, false, false);
20718 static tree
20719 cp_parser_template_type_arg (cp_parser *parser)
20721 tree r;
20722 const char *saved_message = parser->type_definition_forbidden_message;
20723 parser->type_definition_forbidden_message
20724 = G_("types may not be defined in template arguments");
20725 r = cp_parser_type_id_1 (parser, true, false);
20726 parser->type_definition_forbidden_message = saved_message;
20727 if (cxx_dialect >= cxx14 && !flag_concepts && type_uses_auto (r))
20729 error ("invalid use of %<auto%> in template argument");
20730 r = error_mark_node;
20732 return r;
20735 static tree
20736 cp_parser_trailing_type_id (cp_parser *parser)
20738 return cp_parser_type_id_1 (parser, false, true);
20741 /* Parse a type-specifier-seq.
20743 type-specifier-seq:
20744 type-specifier type-specifier-seq [opt]
20746 GNU extension:
20748 type-specifier-seq:
20749 attributes type-specifier-seq [opt]
20751 If IS_DECLARATION is true, we are at the start of a "condition" or
20752 exception-declaration, so we might be followed by a declarator-id.
20754 If IS_TRAILING_RETURN is true, we are in a trailing-return-type,
20755 i.e. we've just seen "->".
20757 Sets *TYPE_SPECIFIER_SEQ to represent the sequence. */
20759 static void
20760 cp_parser_type_specifier_seq (cp_parser* parser,
20761 bool is_declaration,
20762 bool is_trailing_return,
20763 cp_decl_specifier_seq *type_specifier_seq)
20765 bool seen_type_specifier = false;
20766 cp_parser_flags flags = CP_PARSER_FLAGS_OPTIONAL;
20767 cp_token *start_token = NULL;
20769 /* Clear the TYPE_SPECIFIER_SEQ. */
20770 clear_decl_specs (type_specifier_seq);
20772 /* In the context of a trailing return type, enum E { } is an
20773 elaborated-type-specifier followed by a function-body, not an
20774 enum-specifier. */
20775 if (is_trailing_return)
20776 flags |= CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS;
20778 /* Parse the type-specifiers and attributes. */
20779 while (true)
20781 tree type_specifier;
20782 bool is_cv_qualifier;
20784 /* Check for attributes first. */
20785 if (cp_next_tokens_can_be_attribute_p (parser))
20787 type_specifier_seq->attributes =
20788 chainon (type_specifier_seq->attributes,
20789 cp_parser_attributes_opt (parser));
20790 continue;
20793 /* record the token of the beginning of the type specifier seq,
20794 for error reporting purposes*/
20795 if (!start_token)
20796 start_token = cp_lexer_peek_token (parser->lexer);
20798 /* Look for the type-specifier. */
20799 type_specifier = cp_parser_type_specifier (parser,
20800 flags,
20801 type_specifier_seq,
20802 /*is_declaration=*/false,
20803 NULL,
20804 &is_cv_qualifier);
20805 if (!type_specifier)
20807 /* If the first type-specifier could not be found, this is not a
20808 type-specifier-seq at all. */
20809 if (!seen_type_specifier)
20811 /* Set in_declarator_p to avoid skipping to the semicolon. */
20812 int in_decl = parser->in_declarator_p;
20813 parser->in_declarator_p = true;
20815 if (cp_parser_uncommitted_to_tentative_parse_p (parser)
20816 || !cp_parser_parse_and_diagnose_invalid_type_name (parser))
20817 cp_parser_error (parser, "expected type-specifier");
20819 parser->in_declarator_p = in_decl;
20821 type_specifier_seq->type = error_mark_node;
20822 return;
20824 /* If subsequent type-specifiers could not be found, the
20825 type-specifier-seq is complete. */
20826 break;
20829 seen_type_specifier = true;
20830 /* The standard says that a condition can be:
20832 type-specifier-seq declarator = assignment-expression
20834 However, given:
20836 struct S {};
20837 if (int S = ...)
20839 we should treat the "S" as a declarator, not as a
20840 type-specifier. The standard doesn't say that explicitly for
20841 type-specifier-seq, but it does say that for
20842 decl-specifier-seq in an ordinary declaration. Perhaps it
20843 would be clearer just to allow a decl-specifier-seq here, and
20844 then add a semantic restriction that if any decl-specifiers
20845 that are not type-specifiers appear, the program is invalid. */
20846 if (is_declaration && !is_cv_qualifier)
20847 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
20851 /* Return whether the function currently being declared has an associated
20852 template parameter list. */
20854 static bool
20855 function_being_declared_is_template_p (cp_parser* parser)
20857 if (!current_template_parms || processing_template_parmlist)
20858 return false;
20860 if (parser->implicit_template_scope)
20861 return true;
20863 if (at_class_scope_p ()
20864 && TYPE_BEING_DEFINED (current_class_type))
20865 return parser->num_template_parameter_lists != 0;
20867 return ((int) parser->num_template_parameter_lists > template_class_depth
20868 (current_class_type));
20871 /* Parse a parameter-declaration-clause.
20873 parameter-declaration-clause:
20874 parameter-declaration-list [opt] ... [opt]
20875 parameter-declaration-list , ...
20877 Returns a representation for the parameter declarations. A return
20878 value of NULL indicates a parameter-declaration-clause consisting
20879 only of an ellipsis. */
20881 static tree
20882 cp_parser_parameter_declaration_clause (cp_parser* parser)
20884 tree parameters;
20885 cp_token *token;
20886 bool ellipsis_p;
20887 bool is_error;
20889 struct cleanup {
20890 cp_parser* parser;
20891 int auto_is_implicit_function_template_parm_p;
20892 ~cleanup() {
20893 parser->auto_is_implicit_function_template_parm_p
20894 = auto_is_implicit_function_template_parm_p;
20896 } cleanup = { parser, parser->auto_is_implicit_function_template_parm_p };
20898 (void) cleanup;
20900 if (!processing_specialization
20901 && !processing_template_parmlist
20902 && !processing_explicit_instantiation)
20903 if (!current_function_decl
20904 || (current_class_type && LAMBDA_TYPE_P (current_class_type)))
20905 parser->auto_is_implicit_function_template_parm_p = true;
20907 /* Peek at the next token. */
20908 token = cp_lexer_peek_token (parser->lexer);
20909 /* Check for trivial parameter-declaration-clauses. */
20910 if (token->type == CPP_ELLIPSIS)
20912 /* Consume the `...' token. */
20913 cp_lexer_consume_token (parser->lexer);
20914 return NULL_TREE;
20916 else if (token->type == CPP_CLOSE_PAREN)
20917 /* There are no parameters. */
20919 #ifndef NO_IMPLICIT_EXTERN_C
20920 if (in_system_header_at (input_location)
20921 && current_class_type == NULL
20922 && current_lang_name == lang_name_c)
20923 return NULL_TREE;
20924 else
20925 #endif
20926 return void_list_node;
20928 /* Check for `(void)', too, which is a special case. */
20929 else if (token->keyword == RID_VOID
20930 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
20931 == CPP_CLOSE_PAREN))
20933 /* Consume the `void' token. */
20934 cp_lexer_consume_token (parser->lexer);
20935 /* There are no parameters. */
20936 return void_list_node;
20939 /* Parse the parameter-declaration-list. */
20940 parameters = cp_parser_parameter_declaration_list (parser, &is_error);
20941 /* If a parse error occurred while parsing the
20942 parameter-declaration-list, then the entire
20943 parameter-declaration-clause is erroneous. */
20944 if (is_error)
20945 return NULL;
20947 /* Peek at the next token. */
20948 token = cp_lexer_peek_token (parser->lexer);
20949 /* If it's a `,', the clause should terminate with an ellipsis. */
20950 if (token->type == CPP_COMMA)
20952 /* Consume the `,'. */
20953 cp_lexer_consume_token (parser->lexer);
20954 /* Expect an ellipsis. */
20955 ellipsis_p
20956 = (cp_parser_require (parser, CPP_ELLIPSIS, RT_ELLIPSIS) != NULL);
20958 /* It might also be `...' if the optional trailing `,' was
20959 omitted. */
20960 else if (token->type == CPP_ELLIPSIS)
20962 /* Consume the `...' token. */
20963 cp_lexer_consume_token (parser->lexer);
20964 /* And remember that we saw it. */
20965 ellipsis_p = true;
20967 else
20968 ellipsis_p = false;
20970 /* Finish the parameter list. */
20971 if (!ellipsis_p)
20972 parameters = chainon (parameters, void_list_node);
20974 return parameters;
20977 /* Parse a parameter-declaration-list.
20979 parameter-declaration-list:
20980 parameter-declaration
20981 parameter-declaration-list , parameter-declaration
20983 Returns a representation of the parameter-declaration-list, as for
20984 cp_parser_parameter_declaration_clause. However, the
20985 `void_list_node' is never appended to the list. Upon return,
20986 *IS_ERROR will be true iff an error occurred. */
20988 static tree
20989 cp_parser_parameter_declaration_list (cp_parser* parser, bool *is_error)
20991 tree parameters = NULL_TREE;
20992 tree *tail = &parameters;
20993 bool saved_in_unbraced_linkage_specification_p;
20994 int index = 0;
20996 /* Assume all will go well. */
20997 *is_error = false;
20998 /* The special considerations that apply to a function within an
20999 unbraced linkage specifications do not apply to the parameters
21000 to the function. */
21001 saved_in_unbraced_linkage_specification_p
21002 = parser->in_unbraced_linkage_specification_p;
21003 parser->in_unbraced_linkage_specification_p = false;
21005 /* Look for more parameters. */
21006 while (true)
21008 cp_parameter_declarator *parameter;
21009 tree decl = error_mark_node;
21010 bool parenthesized_p = false;
21011 int template_parm_idx = (function_being_declared_is_template_p (parser)?
21012 TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS
21013 (current_template_parms)) : 0);
21015 /* Parse the parameter. */
21016 parameter
21017 = cp_parser_parameter_declaration (parser,
21018 /*template_parm_p=*/false,
21019 &parenthesized_p);
21021 /* We don't know yet if the enclosing context is deprecated, so wait
21022 and warn in grokparms if appropriate. */
21023 deprecated_state = DEPRECATED_SUPPRESS;
21025 if (parameter)
21027 /* If a function parameter pack was specified and an implicit template
21028 parameter was introduced during cp_parser_parameter_declaration,
21029 change any implicit parameters introduced into packs. */
21030 if (parser->implicit_template_parms
21031 && parameter->declarator
21032 && parameter->declarator->parameter_pack_p)
21034 int latest_template_parm_idx = TREE_VEC_LENGTH
21035 (INNERMOST_TEMPLATE_PARMS (current_template_parms));
21037 if (latest_template_parm_idx != template_parm_idx)
21038 parameter->decl_specifiers.type = convert_generic_types_to_packs
21039 (parameter->decl_specifiers.type,
21040 template_parm_idx, latest_template_parm_idx);
21043 decl = grokdeclarator (parameter->declarator,
21044 &parameter->decl_specifiers,
21045 PARM,
21046 parameter->default_argument != NULL_TREE,
21047 &parameter->decl_specifiers.attributes);
21050 deprecated_state = DEPRECATED_NORMAL;
21052 /* If a parse error occurred parsing the parameter declaration,
21053 then the entire parameter-declaration-list is erroneous. */
21054 if (decl == error_mark_node)
21056 *is_error = true;
21057 parameters = error_mark_node;
21058 break;
21061 if (parameter->decl_specifiers.attributes)
21062 cplus_decl_attributes (&decl,
21063 parameter->decl_specifiers.attributes,
21065 if (DECL_NAME (decl))
21066 decl = pushdecl (decl);
21068 if (decl != error_mark_node)
21070 retrofit_lang_decl (decl);
21071 DECL_PARM_INDEX (decl) = ++index;
21072 DECL_PARM_LEVEL (decl) = function_parm_depth ();
21075 /* Add the new parameter to the list. */
21076 *tail = build_tree_list (parameter->default_argument, decl);
21077 tail = &TREE_CHAIN (*tail);
21079 /* Peek at the next token. */
21080 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
21081 || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
21082 /* These are for Objective-C++ */
21083 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
21084 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
21085 /* The parameter-declaration-list is complete. */
21086 break;
21087 else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
21089 cp_token *token;
21091 /* Peek at the next token. */
21092 token = cp_lexer_peek_nth_token (parser->lexer, 2);
21093 /* If it's an ellipsis, then the list is complete. */
21094 if (token->type == CPP_ELLIPSIS)
21095 break;
21096 /* Otherwise, there must be more parameters. Consume the
21097 `,'. */
21098 cp_lexer_consume_token (parser->lexer);
21099 /* When parsing something like:
21101 int i(float f, double d)
21103 we can tell after seeing the declaration for "f" that we
21104 are not looking at an initialization of a variable "i",
21105 but rather at the declaration of a function "i".
21107 Due to the fact that the parsing of template arguments
21108 (as specified to a template-id) requires backtracking we
21109 cannot use this technique when inside a template argument
21110 list. */
21111 if (!parser->in_template_argument_list_p
21112 && !parser->in_type_id_in_expr_p
21113 && cp_parser_uncommitted_to_tentative_parse_p (parser)
21114 /* However, a parameter-declaration of the form
21115 "float(f)" (which is a valid declaration of a
21116 parameter "f") can also be interpreted as an
21117 expression (the conversion of "f" to "float"). */
21118 && !parenthesized_p)
21119 cp_parser_commit_to_tentative_parse (parser);
21121 else
21123 cp_parser_error (parser, "expected %<,%> or %<...%>");
21124 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
21125 cp_parser_skip_to_closing_parenthesis (parser,
21126 /*recovering=*/true,
21127 /*or_comma=*/false,
21128 /*consume_paren=*/false);
21129 break;
21133 parser->in_unbraced_linkage_specification_p
21134 = saved_in_unbraced_linkage_specification_p;
21136 /* Reset implicit_template_scope if we are about to leave the function
21137 parameter list that introduced it. Note that for out-of-line member
21138 definitions, there will be one or more class scopes before we get to
21139 the template parameter scope. */
21141 if (cp_binding_level *its = parser->implicit_template_scope)
21142 if (cp_binding_level *maybe_its = current_binding_level->level_chain)
21144 while (maybe_its->kind == sk_class)
21145 maybe_its = maybe_its->level_chain;
21146 if (maybe_its == its)
21148 parser->implicit_template_parms = 0;
21149 parser->implicit_template_scope = 0;
21153 return parameters;
21156 /* Parse a parameter declaration.
21158 parameter-declaration:
21159 decl-specifier-seq ... [opt] declarator
21160 decl-specifier-seq declarator = assignment-expression
21161 decl-specifier-seq ... [opt] abstract-declarator [opt]
21162 decl-specifier-seq abstract-declarator [opt] = assignment-expression
21164 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
21165 declares a template parameter. (In that case, a non-nested `>'
21166 token encountered during the parsing of the assignment-expression
21167 is not interpreted as a greater-than operator.)
21169 Returns a representation of the parameter, or NULL if an error
21170 occurs. If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
21171 true iff the declarator is of the form "(p)". */
21173 static cp_parameter_declarator *
21174 cp_parser_parameter_declaration (cp_parser *parser,
21175 bool template_parm_p,
21176 bool *parenthesized_p)
21178 int declares_class_or_enum;
21179 cp_decl_specifier_seq decl_specifiers;
21180 cp_declarator *declarator;
21181 tree default_argument;
21182 cp_token *token = NULL, *declarator_token_start = NULL;
21183 const char *saved_message;
21184 bool template_parameter_pack_p = false;
21186 /* In a template parameter, `>' is not an operator.
21188 [temp.param]
21190 When parsing a default template-argument for a non-type
21191 template-parameter, the first non-nested `>' is taken as the end
21192 of the template parameter-list rather than a greater-than
21193 operator. */
21195 /* Type definitions may not appear in parameter types. */
21196 saved_message = parser->type_definition_forbidden_message;
21197 parser->type_definition_forbidden_message
21198 = G_("types may not be defined in parameter types");
21200 /* Parse the declaration-specifiers. */
21201 cp_parser_decl_specifier_seq (parser,
21202 CP_PARSER_FLAGS_NONE,
21203 &decl_specifiers,
21204 &declares_class_or_enum);
21206 /* Complain about missing 'typename' or other invalid type names. */
21207 if (!decl_specifiers.any_type_specifiers_p
21208 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
21209 decl_specifiers.type = error_mark_node;
21211 /* If an error occurred, there's no reason to attempt to parse the
21212 rest of the declaration. */
21213 if (cp_parser_error_occurred (parser))
21215 parser->type_definition_forbidden_message = saved_message;
21216 return NULL;
21219 /* Peek at the next token. */
21220 token = cp_lexer_peek_token (parser->lexer);
21222 /* If the next token is a `)', `,', `=', `>', or `...', then there
21223 is no declarator. However, when variadic templates are enabled,
21224 there may be a declarator following `...'. */
21225 if (token->type == CPP_CLOSE_PAREN
21226 || token->type == CPP_COMMA
21227 || token->type == CPP_EQ
21228 || token->type == CPP_GREATER)
21230 declarator = NULL;
21231 if (parenthesized_p)
21232 *parenthesized_p = false;
21234 /* Otherwise, there should be a declarator. */
21235 else
21237 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
21238 parser->default_arg_ok_p = false;
21240 /* After seeing a decl-specifier-seq, if the next token is not a
21241 "(", there is no possibility that the code is a valid
21242 expression. Therefore, if parsing tentatively, we commit at
21243 this point. */
21244 if (!parser->in_template_argument_list_p
21245 /* In an expression context, having seen:
21247 (int((char ...
21249 we cannot be sure whether we are looking at a
21250 function-type (taking a "char" as a parameter) or a cast
21251 of some object of type "char" to "int". */
21252 && !parser->in_type_id_in_expr_p
21253 && cp_parser_uncommitted_to_tentative_parse_p (parser)
21254 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
21255 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
21256 cp_parser_commit_to_tentative_parse (parser);
21257 /* Parse the declarator. */
21258 declarator_token_start = token;
21259 declarator = cp_parser_declarator (parser,
21260 CP_PARSER_DECLARATOR_EITHER,
21261 /*ctor_dtor_or_conv_p=*/NULL,
21262 parenthesized_p,
21263 /*member_p=*/false,
21264 /*friend_p=*/false);
21265 parser->default_arg_ok_p = saved_default_arg_ok_p;
21266 /* After the declarator, allow more attributes. */
21267 decl_specifiers.attributes
21268 = chainon (decl_specifiers.attributes,
21269 cp_parser_attributes_opt (parser));
21271 /* If the declarator is a template parameter pack, remember that and
21272 clear the flag in the declarator itself so we don't get errors
21273 from grokdeclarator. */
21274 if (template_parm_p && declarator && declarator->parameter_pack_p)
21276 declarator->parameter_pack_p = false;
21277 template_parameter_pack_p = true;
21281 /* If the next token is an ellipsis, and we have not seen a declarator
21282 name, and if either the type of the declarator contains parameter
21283 packs but it is not a TYPE_PACK_EXPANSION or is null (this happens
21284 for, eg, abbreviated integral type names), then we actually have a
21285 parameter pack expansion expression. Otherwise, leave the ellipsis
21286 for a C-style variadic function. */
21287 token = cp_lexer_peek_token (parser->lexer);
21288 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
21290 tree type = decl_specifiers.type;
21292 if (type && DECL_P (type))
21293 type = TREE_TYPE (type);
21295 if (((type
21296 && TREE_CODE (type) != TYPE_PACK_EXPANSION
21297 && (template_parm_p || uses_parameter_packs (type)))
21298 || (!type && template_parm_p))
21299 && declarator_can_be_parameter_pack (declarator))
21301 /* Consume the `...'. */
21302 cp_lexer_consume_token (parser->lexer);
21303 maybe_warn_variadic_templates ();
21305 /* Build a pack expansion type */
21306 if (template_parm_p)
21307 template_parameter_pack_p = true;
21308 else if (declarator)
21309 declarator->parameter_pack_p = true;
21310 else
21311 decl_specifiers.type = make_pack_expansion (type);
21315 /* The restriction on defining new types applies only to the type
21316 of the parameter, not to the default argument. */
21317 parser->type_definition_forbidden_message = saved_message;
21319 /* If the next token is `=', then process a default argument. */
21320 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
21322 tree type = decl_specifiers.type;
21323 token = cp_lexer_peek_token (parser->lexer);
21324 /* If we are defining a class, then the tokens that make up the
21325 default argument must be saved and processed later. */
21326 if (!template_parm_p && at_class_scope_p ()
21327 && TYPE_BEING_DEFINED (current_class_type)
21328 && !LAMBDA_TYPE_P (current_class_type))
21329 default_argument = cp_parser_cache_defarg (parser, /*nsdmi=*/false);
21331 // A constrained-type-specifier may declare a type template-parameter.
21332 else if (declares_constrained_type_template_parameter (type))
21333 default_argument
21334 = cp_parser_default_type_template_argument (parser);
21336 // A constrained-type-specifier may declare a template-template-parameter.
21337 else if (declares_constrained_template_template_parameter (type))
21338 default_argument
21339 = cp_parser_default_template_template_argument (parser);
21341 /* Outside of a class definition, we can just parse the
21342 assignment-expression. */
21343 else
21344 default_argument
21345 = cp_parser_default_argument (parser, template_parm_p);
21347 if (!parser->default_arg_ok_p)
21349 permerror (token->location,
21350 "default arguments are only "
21351 "permitted for function parameters");
21353 else if ((declarator && declarator->parameter_pack_p)
21354 || template_parameter_pack_p
21355 || (decl_specifiers.type
21356 && PACK_EXPANSION_P (decl_specifiers.type)))
21358 /* Find the name of the parameter pack. */
21359 cp_declarator *id_declarator = declarator;
21360 while (id_declarator && id_declarator->kind != cdk_id)
21361 id_declarator = id_declarator->declarator;
21363 if (id_declarator && id_declarator->kind == cdk_id)
21364 error_at (declarator_token_start->location,
21365 template_parm_p
21366 ? G_("template parameter pack %qD "
21367 "cannot have a default argument")
21368 : G_("parameter pack %qD cannot have "
21369 "a default argument"),
21370 id_declarator->u.id.unqualified_name);
21371 else
21372 error_at (declarator_token_start->location,
21373 template_parm_p
21374 ? G_("template parameter pack cannot have "
21375 "a default argument")
21376 : G_("parameter pack cannot have a "
21377 "default argument"));
21379 default_argument = NULL_TREE;
21382 else
21383 default_argument = NULL_TREE;
21385 return make_parameter_declarator (&decl_specifiers,
21386 declarator,
21387 default_argument,
21388 template_parameter_pack_p);
21391 /* Parse a default argument and return it.
21393 TEMPLATE_PARM_P is true if this is a default argument for a
21394 non-type template parameter. */
21395 static tree
21396 cp_parser_default_argument (cp_parser *parser, bool template_parm_p)
21398 tree default_argument = NULL_TREE;
21399 bool saved_greater_than_is_operator_p;
21400 bool saved_local_variables_forbidden_p;
21401 bool non_constant_p, is_direct_init;
21403 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
21404 set correctly. */
21405 saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
21406 parser->greater_than_is_operator_p = !template_parm_p;
21407 /* Local variable names (and the `this' keyword) may not
21408 appear in a default argument. */
21409 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
21410 parser->local_variables_forbidden_p = true;
21411 /* Parse the assignment-expression. */
21412 if (template_parm_p)
21413 push_deferring_access_checks (dk_no_deferred);
21414 tree saved_class_ptr = NULL_TREE;
21415 tree saved_class_ref = NULL_TREE;
21416 /* The "this" pointer is not valid in a default argument. */
21417 if (cfun)
21419 saved_class_ptr = current_class_ptr;
21420 cp_function_chain->x_current_class_ptr = NULL_TREE;
21421 saved_class_ref = current_class_ref;
21422 cp_function_chain->x_current_class_ref = NULL_TREE;
21424 default_argument
21425 = cp_parser_initializer (parser, &is_direct_init, &non_constant_p);
21426 /* Restore the "this" pointer. */
21427 if (cfun)
21429 cp_function_chain->x_current_class_ptr = saved_class_ptr;
21430 cp_function_chain->x_current_class_ref = saved_class_ref;
21432 if (BRACE_ENCLOSED_INITIALIZER_P (default_argument))
21433 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
21434 if (template_parm_p)
21435 pop_deferring_access_checks ();
21436 parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
21437 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
21439 return default_argument;
21442 /* Parse a function-body.
21444 function-body:
21445 compound_statement */
21447 static void
21448 cp_parser_function_body (cp_parser *parser, bool in_function_try_block)
21450 cp_parser_compound_statement (parser, NULL, (in_function_try_block
21451 ? BCS_TRY_BLOCK : BCS_NORMAL),
21452 true);
21455 /* Parse a ctor-initializer-opt followed by a function-body. Return
21456 true if a ctor-initializer was present. When IN_FUNCTION_TRY_BLOCK
21457 is true we are parsing a function-try-block. */
21459 static bool
21460 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser,
21461 bool in_function_try_block)
21463 tree body, list;
21464 bool ctor_initializer_p;
21465 const bool check_body_p =
21466 DECL_CONSTRUCTOR_P (current_function_decl)
21467 && DECL_DECLARED_CONSTEXPR_P (current_function_decl);
21468 tree last = NULL;
21470 /* Begin the function body. */
21471 body = begin_function_body ();
21472 /* Parse the optional ctor-initializer. */
21473 ctor_initializer_p = cp_parser_ctor_initializer_opt (parser);
21475 /* If we're parsing a constexpr constructor definition, we need
21476 to check that the constructor body is indeed empty. However,
21477 before we get to cp_parser_function_body lot of junk has been
21478 generated, so we can't just check that we have an empty block.
21479 Rather we take a snapshot of the outermost block, and check whether
21480 cp_parser_function_body changed its state. */
21481 if (check_body_p)
21483 list = cur_stmt_list;
21484 if (STATEMENT_LIST_TAIL (list))
21485 last = STATEMENT_LIST_TAIL (list)->stmt;
21487 /* Parse the function-body. */
21488 cp_parser_function_body (parser, in_function_try_block);
21489 if (check_body_p)
21490 check_constexpr_ctor_body (last, list, /*complain=*/true);
21491 /* Finish the function body. */
21492 finish_function_body (body);
21494 return ctor_initializer_p;
21497 /* Parse an initializer.
21499 initializer:
21500 = initializer-clause
21501 ( expression-list )
21503 Returns an expression representing the initializer. If no
21504 initializer is present, NULL_TREE is returned.
21506 *IS_DIRECT_INIT is set to FALSE if the `= initializer-clause'
21507 production is used, and TRUE otherwise. *IS_DIRECT_INIT is
21508 set to TRUE if there is no initializer present. If there is an
21509 initializer, and it is not a constant-expression, *NON_CONSTANT_P
21510 is set to true; otherwise it is set to false. */
21512 static tree
21513 cp_parser_initializer (cp_parser* parser, bool* is_direct_init,
21514 bool* non_constant_p)
21516 cp_token *token;
21517 tree init;
21519 /* Peek at the next token. */
21520 token = cp_lexer_peek_token (parser->lexer);
21522 /* Let our caller know whether or not this initializer was
21523 parenthesized. */
21524 *is_direct_init = (token->type != CPP_EQ);
21525 /* Assume that the initializer is constant. */
21526 *non_constant_p = false;
21528 if (token->type == CPP_EQ)
21530 /* Consume the `='. */
21531 cp_lexer_consume_token (parser->lexer);
21532 /* Parse the initializer-clause. */
21533 init = cp_parser_initializer_clause (parser, non_constant_p);
21535 else if (token->type == CPP_OPEN_PAREN)
21537 vec<tree, va_gc> *vec;
21538 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
21539 /*cast_p=*/false,
21540 /*allow_expansion_p=*/true,
21541 non_constant_p);
21542 if (vec == NULL)
21543 return error_mark_node;
21544 init = build_tree_list_vec (vec);
21545 release_tree_vector (vec);
21547 else if (token->type == CPP_OPEN_BRACE)
21549 cp_lexer_set_source_position (parser->lexer);
21550 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
21551 init = cp_parser_braced_list (parser, non_constant_p);
21552 CONSTRUCTOR_IS_DIRECT_INIT (init) = 1;
21554 else
21556 /* Anything else is an error. */
21557 cp_parser_error (parser, "expected initializer");
21558 init = error_mark_node;
21561 if (check_for_bare_parameter_packs (init))
21562 init = error_mark_node;
21564 return init;
21567 /* Parse an initializer-clause.
21569 initializer-clause:
21570 assignment-expression
21571 braced-init-list
21573 Returns an expression representing the initializer.
21575 If the `assignment-expression' production is used the value
21576 returned is simply a representation for the expression.
21578 Otherwise, calls cp_parser_braced_list. */
21580 static cp_expr
21581 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
21583 cp_expr initializer;
21585 /* Assume the expression is constant. */
21586 *non_constant_p = false;
21588 /* If it is not a `{', then we are looking at an
21589 assignment-expression. */
21590 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
21592 initializer
21593 = cp_parser_constant_expression (parser,
21594 /*allow_non_constant_p=*/true,
21595 non_constant_p);
21597 else
21598 initializer = cp_parser_braced_list (parser, non_constant_p);
21600 return initializer;
21603 /* Parse a brace-enclosed initializer list.
21605 braced-init-list:
21606 { initializer-list , [opt] }
21609 Returns a CONSTRUCTOR. The CONSTRUCTOR_ELTS will be
21610 the elements of the initializer-list (or NULL, if the last
21611 production is used). The TREE_TYPE for the CONSTRUCTOR will be
21612 NULL_TREE. There is no way to detect whether or not the optional
21613 trailing `,' was provided. NON_CONSTANT_P is as for
21614 cp_parser_initializer. */
21616 static cp_expr
21617 cp_parser_braced_list (cp_parser* parser, bool* non_constant_p)
21619 tree initializer;
21620 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
21622 /* Consume the `{' token. */
21623 cp_lexer_consume_token (parser->lexer);
21624 /* Create a CONSTRUCTOR to represent the braced-initializer. */
21625 initializer = make_node (CONSTRUCTOR);
21626 /* If it's not a `}', then there is a non-trivial initializer. */
21627 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
21629 /* Parse the initializer list. */
21630 CONSTRUCTOR_ELTS (initializer)
21631 = cp_parser_initializer_list (parser, non_constant_p);
21632 /* A trailing `,' token is allowed. */
21633 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
21634 cp_lexer_consume_token (parser->lexer);
21636 else
21637 *non_constant_p = false;
21638 /* Now, there should be a trailing `}'. */
21639 location_t finish_loc = cp_lexer_peek_token (parser->lexer)->location;
21640 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
21641 TREE_TYPE (initializer) = init_list_type_node;
21643 cp_expr result (initializer);
21644 /* Build a location of the form:
21645 { ... }
21646 ^~~~~~~
21647 with caret==start at the open brace, finish at the close brace. */
21648 location_t combined_loc = make_location (start_loc, start_loc, finish_loc);
21649 result.set_location (combined_loc);
21650 return result;
21653 /* Consume tokens up to, and including, the next non-nested closing `]'.
21654 Returns true iff we found a closing `]'. */
21656 static bool
21657 cp_parser_skip_to_closing_square_bracket (cp_parser *parser)
21659 unsigned square_depth = 0;
21661 while (true)
21663 cp_token * token = cp_lexer_peek_token (parser->lexer);
21665 switch (token->type)
21667 case CPP_EOF:
21668 case CPP_PRAGMA_EOL:
21669 /* If we've run out of tokens, then there is no closing `]'. */
21670 return false;
21672 case CPP_OPEN_SQUARE:
21673 ++square_depth;
21674 break;
21676 case CPP_CLOSE_SQUARE:
21677 if (!square_depth--)
21679 cp_lexer_consume_token (parser->lexer);
21680 return true;
21682 break;
21684 default:
21685 break;
21688 /* Consume the token. */
21689 cp_lexer_consume_token (parser->lexer);
21693 /* Return true if we are looking at an array-designator, false otherwise. */
21695 static bool
21696 cp_parser_array_designator_p (cp_parser *parser)
21698 /* Consume the `['. */
21699 cp_lexer_consume_token (parser->lexer);
21701 cp_lexer_save_tokens (parser->lexer);
21703 /* Skip tokens until the next token is a closing square bracket.
21704 If we find the closing `]', and the next token is a `=', then
21705 we are looking at an array designator. */
21706 bool array_designator_p
21707 = (cp_parser_skip_to_closing_square_bracket (parser)
21708 && cp_lexer_next_token_is (parser->lexer, CPP_EQ));
21710 /* Roll back the tokens we skipped. */
21711 cp_lexer_rollback_tokens (parser->lexer);
21713 return array_designator_p;
21716 /* Parse an initializer-list.
21718 initializer-list:
21719 initializer-clause ... [opt]
21720 initializer-list , initializer-clause ... [opt]
21722 GNU Extension:
21724 initializer-list:
21725 designation initializer-clause ...[opt]
21726 initializer-list , designation initializer-clause ...[opt]
21728 designation:
21729 . identifier =
21730 identifier :
21731 [ constant-expression ] =
21733 Returns a vec of constructor_elt. The VALUE of each elt is an expression
21734 for the initializer. If the INDEX of the elt is non-NULL, it is the
21735 IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is
21736 as for cp_parser_initializer. */
21738 static vec<constructor_elt, va_gc> *
21739 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
21741 vec<constructor_elt, va_gc> *v = NULL;
21743 /* Assume all of the expressions are constant. */
21744 *non_constant_p = false;
21746 /* Parse the rest of the list. */
21747 while (true)
21749 cp_token *token;
21750 tree designator;
21751 tree initializer;
21752 bool clause_non_constant_p;
21754 /* If the next token is an identifier and the following one is a
21755 colon, we are looking at the GNU designated-initializer
21756 syntax. */
21757 if (cp_parser_allow_gnu_extensions_p (parser)
21758 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
21759 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
21761 /* Warn the user that they are using an extension. */
21762 pedwarn (input_location, OPT_Wpedantic,
21763 "ISO C++ does not allow designated initializers");
21764 /* Consume the identifier. */
21765 designator = cp_lexer_consume_token (parser->lexer)->u.value;
21766 /* Consume the `:'. */
21767 cp_lexer_consume_token (parser->lexer);
21769 /* Also handle the C99 syntax, '. id ='. */
21770 else if (cp_parser_allow_gnu_extensions_p (parser)
21771 && cp_lexer_next_token_is (parser->lexer, CPP_DOT)
21772 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME
21773 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_EQ)
21775 /* Warn the user that they are using an extension. */
21776 pedwarn (input_location, OPT_Wpedantic,
21777 "ISO C++ does not allow C99 designated initializers");
21778 /* Consume the `.'. */
21779 cp_lexer_consume_token (parser->lexer);
21780 /* Consume the identifier. */
21781 designator = cp_lexer_consume_token (parser->lexer)->u.value;
21782 /* Consume the `='. */
21783 cp_lexer_consume_token (parser->lexer);
21785 /* Also handle C99 array designators, '[ const ] ='. */
21786 else if (cp_parser_allow_gnu_extensions_p (parser)
21787 && !c_dialect_objc ()
21788 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
21790 /* In C++11, [ could start a lambda-introducer. */
21791 bool non_const = false;
21793 cp_parser_parse_tentatively (parser);
21795 if (!cp_parser_array_designator_p (parser))
21797 cp_parser_simulate_error (parser);
21798 designator = NULL_TREE;
21800 else
21802 designator = cp_parser_constant_expression (parser, true,
21803 &non_const);
21804 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
21805 cp_parser_require (parser, CPP_EQ, RT_EQ);
21808 if (!cp_parser_parse_definitely (parser))
21809 designator = NULL_TREE;
21810 else if (non_const)
21811 require_potential_rvalue_constant_expression (designator);
21813 else
21814 designator = NULL_TREE;
21816 /* Parse the initializer. */
21817 initializer = cp_parser_initializer_clause (parser,
21818 &clause_non_constant_p);
21819 /* If any clause is non-constant, so is the entire initializer. */
21820 if (clause_non_constant_p)
21821 *non_constant_p = true;
21823 /* If we have an ellipsis, this is an initializer pack
21824 expansion. */
21825 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
21827 /* Consume the `...'. */
21828 cp_lexer_consume_token (parser->lexer);
21830 /* Turn the initializer into an initializer expansion. */
21831 initializer = make_pack_expansion (initializer);
21834 /* Add it to the vector. */
21835 CONSTRUCTOR_APPEND_ELT (v, designator, initializer);
21837 /* If the next token is not a comma, we have reached the end of
21838 the list. */
21839 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
21840 break;
21842 /* Peek at the next token. */
21843 token = cp_lexer_peek_nth_token (parser->lexer, 2);
21844 /* If the next token is a `}', then we're still done. An
21845 initializer-clause can have a trailing `,' after the
21846 initializer-list and before the closing `}'. */
21847 if (token->type == CPP_CLOSE_BRACE)
21848 break;
21850 /* Consume the `,' token. */
21851 cp_lexer_consume_token (parser->lexer);
21854 return v;
21857 /* Classes [gram.class] */
21859 /* Parse a class-name.
21861 class-name:
21862 identifier
21863 template-id
21865 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
21866 to indicate that names looked up in dependent types should be
21867 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
21868 keyword has been used to indicate that the name that appears next
21869 is a template. TAG_TYPE indicates the explicit tag given before
21870 the type name, if any. If CHECK_DEPENDENCY_P is FALSE, names are
21871 looked up in dependent scopes. If CLASS_HEAD_P is TRUE, this class
21872 is the class being defined in a class-head. If ENUM_OK is TRUE,
21873 enum-names are also accepted.
21875 Returns the TYPE_DECL representing the class. */
21877 static tree
21878 cp_parser_class_name (cp_parser *parser,
21879 bool typename_keyword_p,
21880 bool template_keyword_p,
21881 enum tag_types tag_type,
21882 bool check_dependency_p,
21883 bool class_head_p,
21884 bool is_declaration,
21885 bool enum_ok)
21887 tree decl;
21888 tree scope;
21889 bool typename_p;
21890 cp_token *token;
21891 tree identifier = NULL_TREE;
21893 /* All class-names start with an identifier. */
21894 token = cp_lexer_peek_token (parser->lexer);
21895 if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
21897 cp_parser_error (parser, "expected class-name");
21898 return error_mark_node;
21901 /* PARSER->SCOPE can be cleared when parsing the template-arguments
21902 to a template-id, so we save it here. */
21903 scope = parser->scope;
21904 if (scope == error_mark_node)
21905 return error_mark_node;
21907 /* Any name names a type if we're following the `typename' keyword
21908 in a qualified name where the enclosing scope is type-dependent. */
21909 typename_p = (typename_keyword_p && scope && TYPE_P (scope)
21910 && dependent_type_p (scope));
21911 /* Handle the common case (an identifier, but not a template-id)
21912 efficiently. */
21913 if (token->type == CPP_NAME
21914 && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
21916 cp_token *identifier_token;
21917 bool ambiguous_p;
21919 /* Look for the identifier. */
21920 identifier_token = cp_lexer_peek_token (parser->lexer);
21921 ambiguous_p = identifier_token->error_reported;
21922 identifier = cp_parser_identifier (parser);
21923 /* If the next token isn't an identifier, we are certainly not
21924 looking at a class-name. */
21925 if (identifier == error_mark_node)
21926 decl = error_mark_node;
21927 /* If we know this is a type-name, there's no need to look it
21928 up. */
21929 else if (typename_p)
21930 decl = identifier;
21931 else
21933 tree ambiguous_decls;
21934 /* If we already know that this lookup is ambiguous, then
21935 we've already issued an error message; there's no reason
21936 to check again. */
21937 if (ambiguous_p)
21939 cp_parser_simulate_error (parser);
21940 return error_mark_node;
21942 /* If the next token is a `::', then the name must be a type
21943 name.
21945 [basic.lookup.qual]
21947 During the lookup for a name preceding the :: scope
21948 resolution operator, object, function, and enumerator
21949 names are ignored. */
21950 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
21951 tag_type = scope_type;
21952 /* Look up the name. */
21953 decl = cp_parser_lookup_name (parser, identifier,
21954 tag_type,
21955 /*is_template=*/false,
21956 /*is_namespace=*/false,
21957 check_dependency_p,
21958 &ambiguous_decls,
21959 identifier_token->location);
21960 if (ambiguous_decls)
21962 if (cp_parser_parsing_tentatively (parser))
21963 cp_parser_simulate_error (parser);
21964 return error_mark_node;
21968 else
21970 /* Try a template-id. */
21971 decl = cp_parser_template_id (parser, template_keyword_p,
21972 check_dependency_p,
21973 tag_type,
21974 is_declaration);
21975 if (decl == error_mark_node)
21976 return error_mark_node;
21979 decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
21981 /* If this is a typename, create a TYPENAME_TYPE. */
21982 if (typename_p && decl != error_mark_node)
21984 decl = make_typename_type (scope, decl, typename_type,
21985 /*complain=*/tf_error);
21986 if (decl != error_mark_node)
21987 decl = TYPE_NAME (decl);
21990 decl = strip_using_decl (decl);
21992 /* Check to see that it is really the name of a class. */
21993 if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
21994 && identifier_p (TREE_OPERAND (decl, 0))
21995 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
21996 /* Situations like this:
21998 template <typename T> struct A {
21999 typename T::template X<int>::I i;
22002 are problematic. Is `T::template X<int>' a class-name? The
22003 standard does not seem to be definitive, but there is no other
22004 valid interpretation of the following `::'. Therefore, those
22005 names are considered class-names. */
22007 decl = make_typename_type (scope, decl, tag_type, tf_error);
22008 if (decl != error_mark_node)
22009 decl = TYPE_NAME (decl);
22011 else if (TREE_CODE (decl) != TYPE_DECL
22012 || TREE_TYPE (decl) == error_mark_node
22013 || !(MAYBE_CLASS_TYPE_P (TREE_TYPE (decl))
22014 || (enum_ok && TREE_CODE (TREE_TYPE (decl)) == ENUMERAL_TYPE))
22015 /* In Objective-C 2.0, a classname followed by '.' starts a
22016 dot-syntax expression, and it's not a type-name. */
22017 || (c_dialect_objc ()
22018 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT
22019 && objc_is_class_name (decl)))
22020 decl = error_mark_node;
22022 if (decl == error_mark_node)
22023 cp_parser_error (parser, "expected class-name");
22024 else if (identifier && !parser->scope)
22025 maybe_note_name_used_in_class (identifier, decl);
22027 return decl;
22030 /* Parse a class-specifier.
22032 class-specifier:
22033 class-head { member-specification [opt] }
22035 Returns the TREE_TYPE representing the class. */
22037 static tree
22038 cp_parser_class_specifier_1 (cp_parser* parser)
22040 tree type;
22041 tree attributes = NULL_TREE;
22042 bool nested_name_specifier_p;
22043 unsigned saved_num_template_parameter_lists;
22044 bool saved_in_function_body;
22045 unsigned char in_statement;
22046 bool in_switch_statement_p;
22047 bool saved_in_unbraced_linkage_specification_p;
22048 tree old_scope = NULL_TREE;
22049 tree scope = NULL_TREE;
22050 cp_token *closing_brace;
22052 push_deferring_access_checks (dk_no_deferred);
22054 /* Parse the class-head. */
22055 type = cp_parser_class_head (parser,
22056 &nested_name_specifier_p);
22057 /* If the class-head was a semantic disaster, skip the entire body
22058 of the class. */
22059 if (!type)
22061 cp_parser_skip_to_end_of_block_or_statement (parser);
22062 pop_deferring_access_checks ();
22063 return error_mark_node;
22066 /* Look for the `{'. */
22067 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
22069 pop_deferring_access_checks ();
22070 return error_mark_node;
22073 cp_ensure_no_omp_declare_simd (parser);
22074 cp_ensure_no_oacc_routine (parser);
22076 /* Issue an error message if type-definitions are forbidden here. */
22077 cp_parser_check_type_definition (parser);
22078 /* Remember that we are defining one more class. */
22079 ++parser->num_classes_being_defined;
22080 /* Inside the class, surrounding template-parameter-lists do not
22081 apply. */
22082 saved_num_template_parameter_lists
22083 = parser->num_template_parameter_lists;
22084 parser->num_template_parameter_lists = 0;
22085 /* We are not in a function body. */
22086 saved_in_function_body = parser->in_function_body;
22087 parser->in_function_body = false;
22088 /* Or in a loop. */
22089 in_statement = parser->in_statement;
22090 parser->in_statement = 0;
22091 /* Or in a switch. */
22092 in_switch_statement_p = parser->in_switch_statement_p;
22093 parser->in_switch_statement_p = false;
22094 /* We are not immediately inside an extern "lang" block. */
22095 saved_in_unbraced_linkage_specification_p
22096 = parser->in_unbraced_linkage_specification_p;
22097 parser->in_unbraced_linkage_specification_p = false;
22099 // Associate constraints with the type.
22100 if (flag_concepts)
22101 type = associate_classtype_constraints (type);
22103 /* Start the class. */
22104 if (nested_name_specifier_p)
22106 scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
22107 old_scope = push_inner_scope (scope);
22109 type = begin_class_definition (type);
22111 if (type == error_mark_node)
22112 /* If the type is erroneous, skip the entire body of the class. */
22113 cp_parser_skip_to_closing_brace (parser);
22114 else
22115 /* Parse the member-specification. */
22116 cp_parser_member_specification_opt (parser);
22118 /* Look for the trailing `}'. */
22119 closing_brace = cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
22120 /* Look for trailing attributes to apply to this class. */
22121 if (cp_parser_allow_gnu_extensions_p (parser))
22122 attributes = cp_parser_gnu_attributes_opt (parser);
22123 if (type != error_mark_node)
22124 type = finish_struct (type, attributes);
22125 if (nested_name_specifier_p)
22126 pop_inner_scope (old_scope, scope);
22128 /* We've finished a type definition. Check for the common syntax
22129 error of forgetting a semicolon after the definition. We need to
22130 be careful, as we can't just check for not-a-semicolon and be done
22131 with it; the user might have typed:
22133 class X { } c = ...;
22134 class X { } *p = ...;
22136 and so forth. Instead, enumerate all the possible tokens that
22137 might follow this production; if we don't see one of them, then
22138 complain and silently insert the semicolon. */
22140 cp_token *token = cp_lexer_peek_token (parser->lexer);
22141 bool want_semicolon = true;
22143 if (cp_next_tokens_can_be_std_attribute_p (parser))
22144 /* Don't try to parse c++11 attributes here. As per the
22145 grammar, that should be a task for
22146 cp_parser_decl_specifier_seq. */
22147 want_semicolon = false;
22149 switch (token->type)
22151 case CPP_NAME:
22152 case CPP_SEMICOLON:
22153 case CPP_MULT:
22154 case CPP_AND:
22155 case CPP_OPEN_PAREN:
22156 case CPP_CLOSE_PAREN:
22157 case CPP_COMMA:
22158 want_semicolon = false;
22159 break;
22161 /* While it's legal for type qualifiers and storage class
22162 specifiers to follow type definitions in the grammar, only
22163 compiler testsuites contain code like that. Assume that if
22164 we see such code, then what we're really seeing is a case
22165 like:
22167 class X { }
22168 const <type> var = ...;
22172 class Y { }
22173 static <type> func (...) ...
22175 i.e. the qualifier or specifier applies to the next
22176 declaration. To do so, however, we need to look ahead one
22177 more token to see if *that* token is a type specifier.
22179 This code could be improved to handle:
22181 class Z { }
22182 static const <type> var = ...; */
22183 case CPP_KEYWORD:
22184 if (keyword_is_decl_specifier (token->keyword))
22186 cp_token *lookahead = cp_lexer_peek_nth_token (parser->lexer, 2);
22188 /* Handling user-defined types here would be nice, but very
22189 tricky. */
22190 want_semicolon
22191 = (lookahead->type == CPP_KEYWORD
22192 && keyword_begins_type_specifier (lookahead->keyword));
22194 break;
22195 default:
22196 break;
22199 /* If we don't have a type, then something is very wrong and we
22200 shouldn't try to do anything clever. Likewise for not seeing the
22201 closing brace. */
22202 if (closing_brace && TYPE_P (type) && want_semicolon)
22204 /* Locate the closing brace. */
22205 cp_token_position prev
22206 = cp_lexer_previous_token_position (parser->lexer);
22207 cp_token *prev_token = cp_lexer_token_at (parser->lexer, prev);
22208 location_t loc = prev_token->location;
22210 /* We want to suggest insertion of a ';' immediately *after* the
22211 closing brace, so, if we can, offset the location by 1 column. */
22212 location_t next_loc = loc;
22213 if (!linemap_location_from_macro_expansion_p (line_table, loc))
22214 next_loc = linemap_position_for_loc_and_offset (line_table, loc, 1);
22216 rich_location richloc (line_table, next_loc);
22218 /* If we successfully offset the location, suggest the fix-it. */
22219 if (next_loc != loc)
22220 richloc.add_fixit_insert_before (next_loc, ";");
22222 if (CLASSTYPE_DECLARED_CLASS (type))
22223 error_at_rich_loc (&richloc,
22224 "expected %<;%> after class definition");
22225 else if (TREE_CODE (type) == RECORD_TYPE)
22226 error_at_rich_loc (&richloc,
22227 "expected %<;%> after struct definition");
22228 else if (TREE_CODE (type) == UNION_TYPE)
22229 error_at_rich_loc (&richloc,
22230 "expected %<;%> after union definition");
22231 else
22232 gcc_unreachable ();
22234 /* Unget one token and smash it to look as though we encountered
22235 a semicolon in the input stream. */
22236 cp_lexer_set_token_position (parser->lexer, prev);
22237 token = cp_lexer_peek_token (parser->lexer);
22238 token->type = CPP_SEMICOLON;
22239 token->keyword = RID_MAX;
22243 /* If this class is not itself within the scope of another class,
22244 then we need to parse the bodies of all of the queued function
22245 definitions. Note that the queued functions defined in a class
22246 are not always processed immediately following the
22247 class-specifier for that class. Consider:
22249 struct A {
22250 struct B { void f() { sizeof (A); } };
22253 If `f' were processed before the processing of `A' were
22254 completed, there would be no way to compute the size of `A'.
22255 Note that the nesting we are interested in here is lexical --
22256 not the semantic nesting given by TYPE_CONTEXT. In particular,
22257 for:
22259 struct A { struct B; };
22260 struct A::B { void f() { } };
22262 there is no need to delay the parsing of `A::B::f'. */
22263 if (--parser->num_classes_being_defined == 0)
22265 tree decl;
22266 tree class_type = NULL_TREE;
22267 tree pushed_scope = NULL_TREE;
22268 unsigned ix;
22269 cp_default_arg_entry *e;
22270 tree save_ccp, save_ccr;
22272 /* In a first pass, parse default arguments to the functions.
22273 Then, in a second pass, parse the bodies of the functions.
22274 This two-phased approach handles cases like:
22276 struct S {
22277 void f() { g(); }
22278 void g(int i = 3);
22282 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_default_args, ix, e)
22284 decl = e->decl;
22285 /* If there are default arguments that have not yet been processed,
22286 take care of them now. */
22287 if (class_type != e->class_type)
22289 if (pushed_scope)
22290 pop_scope (pushed_scope);
22291 class_type = e->class_type;
22292 pushed_scope = push_scope (class_type);
22294 /* Make sure that any template parameters are in scope. */
22295 maybe_begin_member_template_processing (decl);
22296 /* Parse the default argument expressions. */
22297 cp_parser_late_parsing_default_args (parser, decl);
22298 /* Remove any template parameters from the symbol table. */
22299 maybe_end_member_template_processing ();
22301 vec_safe_truncate (unparsed_funs_with_default_args, 0);
22302 /* Now parse any NSDMIs. */
22303 save_ccp = current_class_ptr;
22304 save_ccr = current_class_ref;
22305 FOR_EACH_VEC_SAFE_ELT (unparsed_nsdmis, ix, decl)
22307 if (class_type != DECL_CONTEXT (decl))
22309 if (pushed_scope)
22310 pop_scope (pushed_scope);
22311 class_type = DECL_CONTEXT (decl);
22312 pushed_scope = push_scope (class_type);
22314 inject_this_parameter (class_type, TYPE_UNQUALIFIED);
22315 cp_parser_late_parsing_nsdmi (parser, decl);
22317 vec_safe_truncate (unparsed_nsdmis, 0);
22318 current_class_ptr = save_ccp;
22319 current_class_ref = save_ccr;
22320 if (pushed_scope)
22321 pop_scope (pushed_scope);
22323 /* Now do some post-NSDMI bookkeeping. */
22324 FOR_EACH_VEC_SAFE_ELT (unparsed_classes, ix, class_type)
22325 after_nsdmi_defaulted_late_checks (class_type);
22326 vec_safe_truncate (unparsed_classes, 0);
22327 after_nsdmi_defaulted_late_checks (type);
22329 /* Now parse the body of the functions. */
22330 if (flag_openmp)
22332 /* OpenMP UDRs need to be parsed before all other functions. */
22333 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
22334 if (DECL_OMP_DECLARE_REDUCTION_P (decl))
22335 cp_parser_late_parsing_for_member (parser, decl);
22336 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
22337 if (!DECL_OMP_DECLARE_REDUCTION_P (decl))
22338 cp_parser_late_parsing_for_member (parser, decl);
22340 else
22341 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
22342 cp_parser_late_parsing_for_member (parser, decl);
22343 vec_safe_truncate (unparsed_funs_with_definitions, 0);
22345 else
22346 vec_safe_push (unparsed_classes, type);
22348 /* Put back any saved access checks. */
22349 pop_deferring_access_checks ();
22351 /* Restore saved state. */
22352 parser->in_switch_statement_p = in_switch_statement_p;
22353 parser->in_statement = in_statement;
22354 parser->in_function_body = saved_in_function_body;
22355 parser->num_template_parameter_lists
22356 = saved_num_template_parameter_lists;
22357 parser->in_unbraced_linkage_specification_p
22358 = saved_in_unbraced_linkage_specification_p;
22360 return type;
22363 static tree
22364 cp_parser_class_specifier (cp_parser* parser)
22366 tree ret;
22367 timevar_push (TV_PARSE_STRUCT);
22368 ret = cp_parser_class_specifier_1 (parser);
22369 timevar_pop (TV_PARSE_STRUCT);
22370 return ret;
22373 /* Parse a class-head.
22375 class-head:
22376 class-key identifier [opt] base-clause [opt]
22377 class-key nested-name-specifier identifier class-virt-specifier [opt] base-clause [opt]
22378 class-key nested-name-specifier [opt] template-id
22379 base-clause [opt]
22381 class-virt-specifier:
22382 final
22384 GNU Extensions:
22385 class-key attributes identifier [opt] base-clause [opt]
22386 class-key attributes nested-name-specifier identifier base-clause [opt]
22387 class-key attributes nested-name-specifier [opt] template-id
22388 base-clause [opt]
22390 Upon return BASES is initialized to the list of base classes (or
22391 NULL, if there are none) in the same form returned by
22392 cp_parser_base_clause.
22394 Returns the TYPE of the indicated class. Sets
22395 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
22396 involving a nested-name-specifier was used, and FALSE otherwise.
22398 Returns error_mark_node if this is not a class-head.
22400 Returns NULL_TREE if the class-head is syntactically valid, but
22401 semantically invalid in a way that means we should skip the entire
22402 body of the class. */
22404 static tree
22405 cp_parser_class_head (cp_parser* parser,
22406 bool* nested_name_specifier_p)
22408 tree nested_name_specifier;
22409 enum tag_types class_key;
22410 tree id = NULL_TREE;
22411 tree type = NULL_TREE;
22412 tree attributes;
22413 tree bases;
22414 cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
22415 bool template_id_p = false;
22416 bool qualified_p = false;
22417 bool invalid_nested_name_p = false;
22418 bool invalid_explicit_specialization_p = false;
22419 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
22420 tree pushed_scope = NULL_TREE;
22421 unsigned num_templates;
22422 cp_token *type_start_token = NULL, *nested_name_specifier_token_start = NULL;
22423 /* Assume no nested-name-specifier will be present. */
22424 *nested_name_specifier_p = false;
22425 /* Assume no template parameter lists will be used in defining the
22426 type. */
22427 num_templates = 0;
22428 parser->colon_corrects_to_scope_p = false;
22430 /* Look for the class-key. */
22431 class_key = cp_parser_class_key (parser);
22432 if (class_key == none_type)
22433 return error_mark_node;
22435 location_t class_head_start_location = input_location;
22437 /* Parse the attributes. */
22438 attributes = cp_parser_attributes_opt (parser);
22440 /* If the next token is `::', that is invalid -- but sometimes
22441 people do try to write:
22443 struct ::S {};
22445 Handle this gracefully by accepting the extra qualifier, and then
22446 issuing an error about it later if this really is a
22447 class-head. If it turns out just to be an elaborated type
22448 specifier, remain silent. */
22449 if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
22450 qualified_p = true;
22452 push_deferring_access_checks (dk_no_check);
22454 /* Determine the name of the class. Begin by looking for an
22455 optional nested-name-specifier. */
22456 nested_name_specifier_token_start = cp_lexer_peek_token (parser->lexer);
22457 nested_name_specifier
22458 = cp_parser_nested_name_specifier_opt (parser,
22459 /*typename_keyword_p=*/false,
22460 /*check_dependency_p=*/false,
22461 /*type_p=*/true,
22462 /*is_declaration=*/false);
22463 /* If there was a nested-name-specifier, then there *must* be an
22464 identifier. */
22465 if (nested_name_specifier)
22467 type_start_token = cp_lexer_peek_token (parser->lexer);
22468 /* Although the grammar says `identifier', it really means
22469 `class-name' or `template-name'. You are only allowed to
22470 define a class that has already been declared with this
22471 syntax.
22473 The proposed resolution for Core Issue 180 says that wherever
22474 you see `class T::X' you should treat `X' as a type-name.
22476 It is OK to define an inaccessible class; for example:
22478 class A { class B; };
22479 class A::B {};
22481 We do not know if we will see a class-name, or a
22482 template-name. We look for a class-name first, in case the
22483 class-name is a template-id; if we looked for the
22484 template-name first we would stop after the template-name. */
22485 cp_parser_parse_tentatively (parser);
22486 type = cp_parser_class_name (parser,
22487 /*typename_keyword_p=*/false,
22488 /*template_keyword_p=*/false,
22489 class_type,
22490 /*check_dependency_p=*/false,
22491 /*class_head_p=*/true,
22492 /*is_declaration=*/false);
22493 /* If that didn't work, ignore the nested-name-specifier. */
22494 if (!cp_parser_parse_definitely (parser))
22496 invalid_nested_name_p = true;
22497 type_start_token = cp_lexer_peek_token (parser->lexer);
22498 id = cp_parser_identifier (parser);
22499 if (id == error_mark_node)
22500 id = NULL_TREE;
22502 /* If we could not find a corresponding TYPE, treat this
22503 declaration like an unqualified declaration. */
22504 if (type == error_mark_node)
22505 nested_name_specifier = NULL_TREE;
22506 /* Otherwise, count the number of templates used in TYPE and its
22507 containing scopes. */
22508 else
22510 tree scope;
22512 for (scope = TREE_TYPE (type);
22513 scope && TREE_CODE (scope) != NAMESPACE_DECL;
22514 scope = get_containing_scope (scope))
22515 if (TYPE_P (scope)
22516 && CLASS_TYPE_P (scope)
22517 && CLASSTYPE_TEMPLATE_INFO (scope)
22518 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
22519 && (!CLASSTYPE_TEMPLATE_SPECIALIZATION (scope)
22520 || uses_template_parms (CLASSTYPE_TI_ARGS (scope))))
22521 ++num_templates;
22524 /* Otherwise, the identifier is optional. */
22525 else
22527 /* We don't know whether what comes next is a template-id,
22528 an identifier, or nothing at all. */
22529 cp_parser_parse_tentatively (parser);
22530 /* Check for a template-id. */
22531 type_start_token = cp_lexer_peek_token (parser->lexer);
22532 id = cp_parser_template_id (parser,
22533 /*template_keyword_p=*/false,
22534 /*check_dependency_p=*/true,
22535 class_key,
22536 /*is_declaration=*/true);
22537 /* If that didn't work, it could still be an identifier. */
22538 if (!cp_parser_parse_definitely (parser))
22540 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
22542 type_start_token = cp_lexer_peek_token (parser->lexer);
22543 id = cp_parser_identifier (parser);
22545 else
22546 id = NULL_TREE;
22548 else
22550 template_id_p = true;
22551 ++num_templates;
22555 pop_deferring_access_checks ();
22557 if (id)
22559 cp_parser_check_for_invalid_template_id (parser, id,
22560 class_key,
22561 type_start_token->location);
22563 virt_specifiers = cp_parser_virt_specifier_seq_opt (parser);
22565 /* If it's not a `:' or a `{' then we can't really be looking at a
22566 class-head, since a class-head only appears as part of a
22567 class-specifier. We have to detect this situation before calling
22568 xref_tag, since that has irreversible side-effects. */
22569 if (!cp_parser_next_token_starts_class_definition_p (parser))
22571 cp_parser_error (parser, "expected %<{%> or %<:%>");
22572 type = error_mark_node;
22573 goto out;
22576 /* At this point, we're going ahead with the class-specifier, even
22577 if some other problem occurs. */
22578 cp_parser_commit_to_tentative_parse (parser);
22579 if (virt_specifiers & VIRT_SPEC_OVERRIDE)
22581 cp_parser_error (parser,
22582 "cannot specify %<override%> for a class");
22583 type = error_mark_node;
22584 goto out;
22586 /* Issue the error about the overly-qualified name now. */
22587 if (qualified_p)
22589 cp_parser_error (parser,
22590 "global qualification of class name is invalid");
22591 type = error_mark_node;
22592 goto out;
22594 else if (invalid_nested_name_p)
22596 cp_parser_error (parser,
22597 "qualified name does not name a class");
22598 type = error_mark_node;
22599 goto out;
22601 else if (nested_name_specifier)
22603 tree scope;
22605 /* Reject typedef-names in class heads. */
22606 if (!DECL_IMPLICIT_TYPEDEF_P (type))
22608 error_at (type_start_token->location,
22609 "invalid class name in declaration of %qD",
22610 type);
22611 type = NULL_TREE;
22612 goto done;
22615 /* Figure out in what scope the declaration is being placed. */
22616 scope = current_scope ();
22617 /* If that scope does not contain the scope in which the
22618 class was originally declared, the program is invalid. */
22619 if (scope && !is_ancestor (scope, nested_name_specifier))
22621 if (at_namespace_scope_p ())
22622 error_at (type_start_token->location,
22623 "declaration of %qD in namespace %qD which does not "
22624 "enclose %qD",
22625 type, scope, nested_name_specifier);
22626 else
22627 error_at (type_start_token->location,
22628 "declaration of %qD in %qD which does not enclose %qD",
22629 type, scope, nested_name_specifier);
22630 type = NULL_TREE;
22631 goto done;
22633 /* [dcl.meaning]
22635 A declarator-id shall not be qualified except for the
22636 definition of a ... nested class outside of its class
22637 ... [or] the definition or explicit instantiation of a
22638 class member of a namespace outside of its namespace. */
22639 if (scope == nested_name_specifier)
22641 permerror (nested_name_specifier_token_start->location,
22642 "extra qualification not allowed");
22643 nested_name_specifier = NULL_TREE;
22644 num_templates = 0;
22647 /* An explicit-specialization must be preceded by "template <>". If
22648 it is not, try to recover gracefully. */
22649 if (at_namespace_scope_p ()
22650 && parser->num_template_parameter_lists == 0
22651 && !processing_template_parmlist
22652 && template_id_p)
22654 /* Build a location of this form:
22655 struct typename <ARGS>
22656 ^~~~~~~~~~~~~~~~~~~~~~
22657 with caret==start at the start token, and
22658 finishing at the end of the type. */
22659 location_t reported_loc
22660 = make_location (class_head_start_location,
22661 class_head_start_location,
22662 get_finish (type_start_token->location));
22663 rich_location richloc (line_table, reported_loc);
22664 richloc.add_fixit_insert_before (class_head_start_location,
22665 "template <> ");
22666 error_at_rich_loc
22667 (&richloc,
22668 "an explicit specialization must be preceded by %<template <>%>");
22669 invalid_explicit_specialization_p = true;
22670 /* Take the same action that would have been taken by
22671 cp_parser_explicit_specialization. */
22672 ++parser->num_template_parameter_lists;
22673 begin_specialization ();
22675 /* There must be no "return" statements between this point and the
22676 end of this function; set "type "to the correct return value and
22677 use "goto done;" to return. */
22678 /* Make sure that the right number of template parameters were
22679 present. */
22680 if (!cp_parser_check_template_parameters (parser, num_templates,
22681 type_start_token->location,
22682 /*declarator=*/NULL))
22684 /* If something went wrong, there is no point in even trying to
22685 process the class-definition. */
22686 type = NULL_TREE;
22687 goto done;
22690 /* Look up the type. */
22691 if (template_id_p)
22693 if (TREE_CODE (id) == TEMPLATE_ID_EXPR
22694 && (DECL_FUNCTION_TEMPLATE_P (TREE_OPERAND (id, 0))
22695 || TREE_CODE (TREE_OPERAND (id, 0)) == OVERLOAD))
22697 error_at (type_start_token->location,
22698 "function template %qD redeclared as a class template", id);
22699 type = error_mark_node;
22701 else
22703 type = TREE_TYPE (id);
22704 type = maybe_process_partial_specialization (type);
22706 /* Check the scope while we still know whether or not we had a
22707 nested-name-specifier. */
22708 if (type != error_mark_node)
22709 check_unqualified_spec_or_inst (type, type_start_token->location);
22711 if (nested_name_specifier)
22712 pushed_scope = push_scope (nested_name_specifier);
22714 else if (nested_name_specifier)
22716 tree class_type;
22718 /* Given:
22720 template <typename T> struct S { struct T };
22721 template <typename T> struct S<T>::T { };
22723 we will get a TYPENAME_TYPE when processing the definition of
22724 `S::T'. We need to resolve it to the actual type before we
22725 try to define it. */
22726 if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
22728 class_type = resolve_typename_type (TREE_TYPE (type),
22729 /*only_current_p=*/false);
22730 if (TREE_CODE (class_type) != TYPENAME_TYPE)
22731 type = TYPE_NAME (class_type);
22732 else
22734 cp_parser_error (parser, "could not resolve typename type");
22735 type = error_mark_node;
22739 if (maybe_process_partial_specialization (TREE_TYPE (type))
22740 == error_mark_node)
22742 type = NULL_TREE;
22743 goto done;
22746 class_type = current_class_type;
22747 /* Enter the scope indicated by the nested-name-specifier. */
22748 pushed_scope = push_scope (nested_name_specifier);
22749 /* Get the canonical version of this type. */
22750 type = TYPE_MAIN_DECL (TREE_TYPE (type));
22751 /* Call push_template_decl if it seems like we should be defining a
22752 template either from the template headers or the type we're
22753 defining, so that we diagnose both extra and missing headers. */
22754 if ((PROCESSING_REAL_TEMPLATE_DECL_P ()
22755 || CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (type)))
22756 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
22758 type = push_template_decl (type);
22759 if (type == error_mark_node)
22761 type = NULL_TREE;
22762 goto done;
22766 type = TREE_TYPE (type);
22767 *nested_name_specifier_p = true;
22769 else /* The name is not a nested name. */
22771 /* If the class was unnamed, create a dummy name. */
22772 if (!id)
22773 id = make_anon_name ();
22774 type = xref_tag (class_key, id, /*tag_scope=*/ts_current,
22775 parser->num_template_parameter_lists);
22778 /* Indicate whether this class was declared as a `class' or as a
22779 `struct'. */
22780 if (TREE_CODE (type) == RECORD_TYPE)
22781 CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
22782 cp_parser_check_class_key (class_key, type);
22784 /* If this type was already complete, and we see another definition,
22785 that's an error. */
22786 if (type != error_mark_node && COMPLETE_TYPE_P (type))
22788 error_at (type_start_token->location, "redefinition of %q#T",
22789 type);
22790 inform (location_of (type), "previous definition of %q#T",
22791 type);
22792 type = NULL_TREE;
22793 goto done;
22795 else if (type == error_mark_node)
22796 type = NULL_TREE;
22798 if (type)
22800 /* Apply attributes now, before any use of the class as a template
22801 argument in its base list. */
22802 cplus_decl_attributes (&type, attributes, (int)ATTR_FLAG_TYPE_IN_PLACE);
22803 fixup_attribute_variants (type);
22806 /* We will have entered the scope containing the class; the names of
22807 base classes should be looked up in that context. For example:
22809 struct A { struct B {}; struct C; };
22810 struct A::C : B {};
22812 is valid. */
22814 /* Get the list of base-classes, if there is one. */
22815 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
22817 /* PR59482: enter the class scope so that base-specifiers are looked
22818 up correctly. */
22819 if (type)
22820 pushclass (type);
22821 bases = cp_parser_base_clause (parser);
22822 /* PR59482: get out of the previously pushed class scope so that the
22823 subsequent pops pop the right thing. */
22824 if (type)
22825 popclass ();
22827 else
22828 bases = NULL_TREE;
22830 /* If we're really defining a class, process the base classes.
22831 If they're invalid, fail. */
22832 if (type && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
22833 xref_basetypes (type, bases);
22835 done:
22836 /* Leave the scope given by the nested-name-specifier. We will
22837 enter the class scope itself while processing the members. */
22838 if (pushed_scope)
22839 pop_scope (pushed_scope);
22841 if (invalid_explicit_specialization_p)
22843 end_specialization ();
22844 --parser->num_template_parameter_lists;
22847 if (type)
22848 DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
22849 if (type && (virt_specifiers & VIRT_SPEC_FINAL))
22850 CLASSTYPE_FINAL (type) = 1;
22851 out:
22852 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
22853 return type;
22856 /* Parse a class-key.
22858 class-key:
22859 class
22860 struct
22861 union
22863 Returns the kind of class-key specified, or none_type to indicate
22864 error. */
22866 static enum tag_types
22867 cp_parser_class_key (cp_parser* parser)
22869 cp_token *token;
22870 enum tag_types tag_type;
22872 /* Look for the class-key. */
22873 token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_KEY);
22874 if (!token)
22875 return none_type;
22877 /* Check to see if the TOKEN is a class-key. */
22878 tag_type = cp_parser_token_is_class_key (token);
22879 if (!tag_type)
22880 cp_parser_error (parser, "expected class-key");
22881 return tag_type;
22884 /* Parse a type-parameter-key.
22886 type-parameter-key:
22887 class
22888 typename
22891 static void
22892 cp_parser_type_parameter_key (cp_parser* parser)
22894 /* Look for the type-parameter-key. */
22895 enum tag_types tag_type = none_type;
22896 cp_token *token = cp_lexer_peek_token (parser->lexer);
22897 if ((tag_type = cp_parser_token_is_type_parameter_key (token)) != none_type)
22899 cp_lexer_consume_token (parser->lexer);
22900 if (pedantic && tag_type == typename_type && cxx_dialect < cxx1z)
22901 /* typename is not allowed in a template template parameter
22902 by the standard until C++1Z. */
22903 pedwarn (token->location, OPT_Wpedantic,
22904 "ISO C++ forbids typename key in template template parameter;"
22905 " use -std=c++1z or -std=gnu++1z");
22907 else
22908 cp_parser_error (parser, "expected %<class%> or %<typename%>");
22910 return;
22913 /* Parse an (optional) member-specification.
22915 member-specification:
22916 member-declaration member-specification [opt]
22917 access-specifier : member-specification [opt] */
22919 static void
22920 cp_parser_member_specification_opt (cp_parser* parser)
22922 while (true)
22924 cp_token *token;
22925 enum rid keyword;
22927 /* Peek at the next token. */
22928 token = cp_lexer_peek_token (parser->lexer);
22929 /* If it's a `}', or EOF then we've seen all the members. */
22930 if (token->type == CPP_CLOSE_BRACE
22931 || token->type == CPP_EOF
22932 || token->type == CPP_PRAGMA_EOL)
22933 break;
22935 /* See if this token is a keyword. */
22936 keyword = token->keyword;
22937 switch (keyword)
22939 case RID_PUBLIC:
22940 case RID_PROTECTED:
22941 case RID_PRIVATE:
22942 /* Consume the access-specifier. */
22943 cp_lexer_consume_token (parser->lexer);
22944 /* Remember which access-specifier is active. */
22945 current_access_specifier = token->u.value;
22946 /* Look for the `:'. */
22947 cp_parser_require (parser, CPP_COLON, RT_COLON);
22948 break;
22950 default:
22951 /* Accept #pragmas at class scope. */
22952 if (token->type == CPP_PRAGMA)
22954 cp_parser_pragma (parser, pragma_member, NULL);
22955 break;
22958 /* Otherwise, the next construction must be a
22959 member-declaration. */
22960 cp_parser_member_declaration (parser);
22965 /* Parse a member-declaration.
22967 member-declaration:
22968 decl-specifier-seq [opt] member-declarator-list [opt] ;
22969 function-definition ; [opt]
22970 :: [opt] nested-name-specifier template [opt] unqualified-id ;
22971 using-declaration
22972 template-declaration
22973 alias-declaration
22975 member-declarator-list:
22976 member-declarator
22977 member-declarator-list , member-declarator
22979 member-declarator:
22980 declarator pure-specifier [opt]
22981 declarator constant-initializer [opt]
22982 identifier [opt] : constant-expression
22984 GNU Extensions:
22986 member-declaration:
22987 __extension__ member-declaration
22989 member-declarator:
22990 declarator attributes [opt] pure-specifier [opt]
22991 declarator attributes [opt] constant-initializer [opt]
22992 identifier [opt] attributes [opt] : constant-expression
22994 C++0x Extensions:
22996 member-declaration:
22997 static_assert-declaration */
22999 static void
23000 cp_parser_member_declaration (cp_parser* parser)
23002 cp_decl_specifier_seq decl_specifiers;
23003 tree prefix_attributes;
23004 tree decl;
23005 int declares_class_or_enum;
23006 bool friend_p;
23007 cp_token *token = NULL;
23008 cp_token *decl_spec_token_start = NULL;
23009 cp_token *initializer_token_start = NULL;
23010 int saved_pedantic;
23011 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
23013 /* Check for the `__extension__' keyword. */
23014 if (cp_parser_extension_opt (parser, &saved_pedantic))
23016 /* Recurse. */
23017 cp_parser_member_declaration (parser);
23018 /* Restore the old value of the PEDANTIC flag. */
23019 pedantic = saved_pedantic;
23021 return;
23024 /* Check for a template-declaration. */
23025 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
23027 /* An explicit specialization here is an error condition, and we
23028 expect the specialization handler to detect and report this. */
23029 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
23030 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
23031 cp_parser_explicit_specialization (parser);
23032 else
23033 cp_parser_template_declaration (parser, /*member_p=*/true);
23035 return;
23037 /* Check for a template introduction. */
23038 else if (cp_parser_template_declaration_after_export (parser, true))
23039 return;
23041 /* Check for a using-declaration. */
23042 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
23044 if (cxx_dialect < cxx11)
23046 /* Parse the using-declaration. */
23047 cp_parser_using_declaration (parser,
23048 /*access_declaration_p=*/false);
23049 return;
23051 else
23053 tree decl;
23054 bool alias_decl_expected;
23055 cp_parser_parse_tentatively (parser);
23056 decl = cp_parser_alias_declaration (parser);
23057 /* Note that if we actually see the '=' token after the
23058 identifier, cp_parser_alias_declaration commits the
23059 tentative parse. In that case, we really expect an
23060 alias-declaration. Otherwise, we expect a using
23061 declaration. */
23062 alias_decl_expected =
23063 !cp_parser_uncommitted_to_tentative_parse_p (parser);
23064 cp_parser_parse_definitely (parser);
23066 if (alias_decl_expected)
23067 finish_member_declaration (decl);
23068 else
23069 cp_parser_using_declaration (parser,
23070 /*access_declaration_p=*/false);
23071 return;
23075 /* Check for @defs. */
23076 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_DEFS))
23078 tree ivar, member;
23079 tree ivar_chains = cp_parser_objc_defs_expression (parser);
23080 ivar = ivar_chains;
23081 while (ivar)
23083 member = ivar;
23084 ivar = TREE_CHAIN (member);
23085 TREE_CHAIN (member) = NULL_TREE;
23086 finish_member_declaration (member);
23088 return;
23091 /* If the next token is `static_assert' we have a static assertion. */
23092 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC_ASSERT))
23094 cp_parser_static_assert (parser, /*member_p=*/true);
23095 return;
23098 parser->colon_corrects_to_scope_p = false;
23100 if (cp_parser_using_declaration (parser, /*access_declaration=*/true))
23101 goto out;
23103 /* Parse the decl-specifier-seq. */
23104 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
23105 cp_parser_decl_specifier_seq (parser,
23106 CP_PARSER_FLAGS_OPTIONAL,
23107 &decl_specifiers,
23108 &declares_class_or_enum);
23109 /* Check for an invalid type-name. */
23110 if (!decl_specifiers.any_type_specifiers_p
23111 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
23112 goto out;
23113 /* If there is no declarator, then the decl-specifier-seq should
23114 specify a type. */
23115 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
23117 /* If there was no decl-specifier-seq, and the next token is a
23118 `;', then we have something like:
23120 struct S { ; };
23122 [class.mem]
23124 Each member-declaration shall declare at least one member
23125 name of the class. */
23126 if (!decl_specifiers.any_specifiers_p)
23128 cp_token *token = cp_lexer_peek_token (parser->lexer);
23129 if (!in_system_header_at (token->location))
23130 pedwarn (token->location, OPT_Wpedantic, "extra %<;%>");
23132 else
23134 tree type;
23136 /* See if this declaration is a friend. */
23137 friend_p = cp_parser_friend_p (&decl_specifiers);
23138 /* If there were decl-specifiers, check to see if there was
23139 a class-declaration. */
23140 type = check_tag_decl (&decl_specifiers,
23141 /*explicit_type_instantiation_p=*/false);
23142 /* Nested classes have already been added to the class, but
23143 a `friend' needs to be explicitly registered. */
23144 if (friend_p)
23146 /* If the `friend' keyword was present, the friend must
23147 be introduced with a class-key. */
23148 if (!declares_class_or_enum && cxx_dialect < cxx11)
23149 pedwarn (decl_spec_token_start->location, OPT_Wpedantic,
23150 "in C++03 a class-key must be used "
23151 "when declaring a friend");
23152 /* In this case:
23154 template <typename T> struct A {
23155 friend struct A<T>::B;
23158 A<T>::B will be represented by a TYPENAME_TYPE, and
23159 therefore not recognized by check_tag_decl. */
23160 if (!type)
23162 type = decl_specifiers.type;
23163 if (type && TREE_CODE (type) == TYPE_DECL)
23164 type = TREE_TYPE (type);
23166 if (!type || !TYPE_P (type))
23167 error_at (decl_spec_token_start->location,
23168 "friend declaration does not name a class or "
23169 "function");
23170 else
23171 make_friend_class (current_class_type, type,
23172 /*complain=*/true);
23174 /* If there is no TYPE, an error message will already have
23175 been issued. */
23176 else if (!type || type == error_mark_node)
23178 /* An anonymous aggregate has to be handled specially; such
23179 a declaration really declares a data member (with a
23180 particular type), as opposed to a nested class. */
23181 else if (ANON_AGGR_TYPE_P (type))
23183 /* C++11 9.5/6. */
23184 if (decl_specifiers.storage_class != sc_none)
23185 error_at (decl_spec_token_start->location,
23186 "a storage class on an anonymous aggregate "
23187 "in class scope is not allowed");
23189 /* Remove constructors and such from TYPE, now that we
23190 know it is an anonymous aggregate. */
23191 fixup_anonymous_aggr (type);
23192 /* And make the corresponding data member. */
23193 decl = build_decl (decl_spec_token_start->location,
23194 FIELD_DECL, NULL_TREE, type);
23195 /* Add it to the class. */
23196 finish_member_declaration (decl);
23198 else
23199 cp_parser_check_access_in_redeclaration
23200 (TYPE_NAME (type),
23201 decl_spec_token_start->location);
23204 else
23206 bool assume_semicolon = false;
23208 /* Clear attributes from the decl_specifiers but keep them
23209 around as prefix attributes that apply them to the entity
23210 being declared. */
23211 prefix_attributes = decl_specifiers.attributes;
23212 decl_specifiers.attributes = NULL_TREE;
23214 /* See if these declarations will be friends. */
23215 friend_p = cp_parser_friend_p (&decl_specifiers);
23217 /* Keep going until we hit the `;' at the end of the
23218 declaration. */
23219 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
23221 tree attributes = NULL_TREE;
23222 tree first_attribute;
23224 /* Peek at the next token. */
23225 token = cp_lexer_peek_token (parser->lexer);
23227 /* Check for a bitfield declaration. */
23228 if (token->type == CPP_COLON
23229 || (token->type == CPP_NAME
23230 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
23231 == CPP_COLON))
23233 tree identifier;
23234 tree width;
23236 /* Get the name of the bitfield. Note that we cannot just
23237 check TOKEN here because it may have been invalidated by
23238 the call to cp_lexer_peek_nth_token above. */
23239 if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
23240 identifier = cp_parser_identifier (parser);
23241 else
23242 identifier = NULL_TREE;
23244 /* Consume the `:' token. */
23245 cp_lexer_consume_token (parser->lexer);
23246 /* Get the width of the bitfield. */
23247 width
23248 = cp_parser_constant_expression (parser);
23250 /* Look for attributes that apply to the bitfield. */
23251 attributes = cp_parser_attributes_opt (parser);
23252 /* Remember which attributes are prefix attributes and
23253 which are not. */
23254 first_attribute = attributes;
23255 /* Combine the attributes. */
23256 attributes = chainon (prefix_attributes, attributes);
23258 /* Create the bitfield declaration. */
23259 decl = grokbitfield (identifier
23260 ? make_id_declarator (NULL_TREE,
23261 identifier,
23262 sfk_none)
23263 : NULL,
23264 &decl_specifiers,
23265 width,
23266 attributes);
23268 else
23270 cp_declarator *declarator;
23271 tree initializer;
23272 tree asm_specification;
23273 int ctor_dtor_or_conv_p;
23275 /* Parse the declarator. */
23276 declarator
23277 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
23278 &ctor_dtor_or_conv_p,
23279 /*parenthesized_p=*/NULL,
23280 /*member_p=*/true,
23281 friend_p);
23283 /* If something went wrong parsing the declarator, make sure
23284 that we at least consume some tokens. */
23285 if (declarator == cp_error_declarator)
23287 /* Skip to the end of the statement. */
23288 cp_parser_skip_to_end_of_statement (parser);
23289 /* If the next token is not a semicolon, that is
23290 probably because we just skipped over the body of
23291 a function. So, we consume a semicolon if
23292 present, but do not issue an error message if it
23293 is not present. */
23294 if (cp_lexer_next_token_is (parser->lexer,
23295 CPP_SEMICOLON))
23296 cp_lexer_consume_token (parser->lexer);
23297 goto out;
23300 if (declares_class_or_enum & 2)
23301 cp_parser_check_for_definition_in_return_type
23302 (declarator, decl_specifiers.type,
23303 decl_specifiers.locations[ds_type_spec]);
23305 /* Look for an asm-specification. */
23306 asm_specification = cp_parser_asm_specification_opt (parser);
23307 /* Look for attributes that apply to the declaration. */
23308 attributes = cp_parser_attributes_opt (parser);
23309 /* Remember which attributes are prefix attributes and
23310 which are not. */
23311 first_attribute = attributes;
23312 /* Combine the attributes. */
23313 attributes = chainon (prefix_attributes, attributes);
23315 /* If it's an `=', then we have a constant-initializer or a
23316 pure-specifier. It is not correct to parse the
23317 initializer before registering the member declaration
23318 since the member declaration should be in scope while
23319 its initializer is processed. However, the rest of the
23320 front end does not yet provide an interface that allows
23321 us to handle this correctly. */
23322 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
23324 /* In [class.mem]:
23326 A pure-specifier shall be used only in the declaration of
23327 a virtual function.
23329 A member-declarator can contain a constant-initializer
23330 only if it declares a static member of integral or
23331 enumeration type.
23333 Therefore, if the DECLARATOR is for a function, we look
23334 for a pure-specifier; otherwise, we look for a
23335 constant-initializer. When we call `grokfield', it will
23336 perform more stringent semantics checks. */
23337 initializer_token_start = cp_lexer_peek_token (parser->lexer);
23338 if (function_declarator_p (declarator)
23339 || (decl_specifiers.type
23340 && TREE_CODE (decl_specifiers.type) == TYPE_DECL
23341 && declarator->kind == cdk_id
23342 && (TREE_CODE (TREE_TYPE (decl_specifiers.type))
23343 == FUNCTION_TYPE)))
23344 initializer = cp_parser_pure_specifier (parser);
23345 else if (decl_specifiers.storage_class != sc_static)
23346 initializer = cp_parser_save_nsdmi (parser);
23347 else if (cxx_dialect >= cxx11)
23349 bool nonconst;
23350 /* Don't require a constant rvalue in C++11, since we
23351 might want a reference constant. We'll enforce
23352 constancy later. */
23353 cp_lexer_consume_token (parser->lexer);
23354 /* Parse the initializer. */
23355 initializer = cp_parser_initializer_clause (parser,
23356 &nonconst);
23358 else
23359 /* Parse the initializer. */
23360 initializer = cp_parser_constant_initializer (parser);
23362 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
23363 && !function_declarator_p (declarator))
23365 bool x;
23366 if (decl_specifiers.storage_class != sc_static)
23367 initializer = cp_parser_save_nsdmi (parser);
23368 else
23369 initializer = cp_parser_initializer (parser, &x, &x);
23371 /* Otherwise, there is no initializer. */
23372 else
23373 initializer = NULL_TREE;
23375 /* See if we are probably looking at a function
23376 definition. We are certainly not looking at a
23377 member-declarator. Calling `grokfield' has
23378 side-effects, so we must not do it unless we are sure
23379 that we are looking at a member-declarator. */
23380 if (cp_parser_token_starts_function_definition_p
23381 (cp_lexer_peek_token (parser->lexer)))
23383 /* The grammar does not allow a pure-specifier to be
23384 used when a member function is defined. (It is
23385 possible that this fact is an oversight in the
23386 standard, since a pure function may be defined
23387 outside of the class-specifier. */
23388 if (initializer && initializer_token_start)
23389 error_at (initializer_token_start->location,
23390 "pure-specifier on function-definition");
23391 decl = cp_parser_save_member_function_body (parser,
23392 &decl_specifiers,
23393 declarator,
23394 attributes);
23395 if (parser->fully_implicit_function_template_p)
23396 decl = finish_fully_implicit_template (parser, decl);
23397 /* If the member was not a friend, declare it here. */
23398 if (!friend_p)
23399 finish_member_declaration (decl);
23400 /* Peek at the next token. */
23401 token = cp_lexer_peek_token (parser->lexer);
23402 /* If the next token is a semicolon, consume it. */
23403 if (token->type == CPP_SEMICOLON)
23404 cp_lexer_consume_token (parser->lexer);
23405 goto out;
23407 else
23408 if (declarator->kind == cdk_function)
23409 declarator->id_loc = token->location;
23410 /* Create the declaration. */
23411 decl = grokfield (declarator, &decl_specifiers,
23412 initializer, /*init_const_expr_p=*/true,
23413 asm_specification, attributes);
23414 if (parser->fully_implicit_function_template_p)
23416 if (friend_p)
23417 finish_fully_implicit_template (parser, 0);
23418 else
23419 decl = finish_fully_implicit_template (parser, decl);
23423 cp_finalize_omp_declare_simd (parser, decl);
23424 cp_finalize_oacc_routine (parser, decl, false);
23426 /* Reset PREFIX_ATTRIBUTES. */
23427 while (attributes && TREE_CHAIN (attributes) != first_attribute)
23428 attributes = TREE_CHAIN (attributes);
23429 if (attributes)
23430 TREE_CHAIN (attributes) = NULL_TREE;
23432 /* If there is any qualification still in effect, clear it
23433 now; we will be starting fresh with the next declarator. */
23434 parser->scope = NULL_TREE;
23435 parser->qualifying_scope = NULL_TREE;
23436 parser->object_scope = NULL_TREE;
23437 /* If it's a `,', then there are more declarators. */
23438 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
23440 cp_lexer_consume_token (parser->lexer);
23441 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
23443 cp_token *token = cp_lexer_previous_token (parser->lexer);
23444 error_at (token->location,
23445 "stray %<,%> at end of member declaration");
23448 /* If the next token isn't a `;', then we have a parse error. */
23449 else if (cp_lexer_next_token_is_not (parser->lexer,
23450 CPP_SEMICOLON))
23452 /* The next token might be a ways away from where the
23453 actual semicolon is missing. Find the previous token
23454 and use that for our error position. */
23455 cp_token *token = cp_lexer_previous_token (parser->lexer);
23456 error_at (token->location,
23457 "expected %<;%> at end of member declaration");
23459 /* Assume that the user meant to provide a semicolon. If
23460 we were to cp_parser_skip_to_end_of_statement, we might
23461 skip to a semicolon inside a member function definition
23462 and issue nonsensical error messages. */
23463 assume_semicolon = true;
23466 if (decl)
23468 /* Add DECL to the list of members. */
23469 if (!friend_p
23470 /* Explicitly include, eg, NSDMIs, for better error
23471 recovery (c++/58650). */
23472 || !DECL_DECLARES_FUNCTION_P (decl))
23473 finish_member_declaration (decl);
23475 if (TREE_CODE (decl) == FUNCTION_DECL)
23476 cp_parser_save_default_args (parser, decl);
23477 else if (TREE_CODE (decl) == FIELD_DECL
23478 && !DECL_C_BIT_FIELD (decl)
23479 && DECL_INITIAL (decl))
23480 /* Add DECL to the queue of NSDMI to be parsed later. */
23481 vec_safe_push (unparsed_nsdmis, decl);
23484 if (assume_semicolon)
23485 goto out;
23489 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
23490 out:
23491 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
23494 /* Parse a pure-specifier.
23496 pure-specifier:
23499 Returns INTEGER_ZERO_NODE if a pure specifier is found.
23500 Otherwise, ERROR_MARK_NODE is returned. */
23502 static tree
23503 cp_parser_pure_specifier (cp_parser* parser)
23505 cp_token *token;
23507 /* Look for the `=' token. */
23508 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
23509 return error_mark_node;
23510 /* Look for the `0' token. */
23511 token = cp_lexer_peek_token (parser->lexer);
23513 if (token->type == CPP_EOF
23514 || token->type == CPP_PRAGMA_EOL)
23515 return error_mark_node;
23517 cp_lexer_consume_token (parser->lexer);
23519 /* Accept = default or = delete in c++0x mode. */
23520 if (token->keyword == RID_DEFAULT
23521 || token->keyword == RID_DELETE)
23523 maybe_warn_cpp0x (CPP0X_DEFAULTED_DELETED);
23524 return token->u.value;
23527 /* c_lex_with_flags marks a single digit '0' with PURE_ZERO. */
23528 if (token->type != CPP_NUMBER || !(token->flags & PURE_ZERO))
23530 cp_parser_error (parser,
23531 "invalid pure specifier (only %<= 0%> is allowed)");
23532 cp_parser_skip_to_end_of_statement (parser);
23533 return error_mark_node;
23535 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
23537 error_at (token->location, "templates may not be %<virtual%>");
23538 return error_mark_node;
23541 return integer_zero_node;
23544 /* Parse a constant-initializer.
23546 constant-initializer:
23547 = constant-expression
23549 Returns a representation of the constant-expression. */
23551 static tree
23552 cp_parser_constant_initializer (cp_parser* parser)
23554 /* Look for the `=' token. */
23555 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
23556 return error_mark_node;
23558 /* It is invalid to write:
23560 struct S { static const int i = { 7 }; };
23563 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
23565 cp_parser_error (parser,
23566 "a brace-enclosed initializer is not allowed here");
23567 /* Consume the opening brace. */
23568 cp_lexer_consume_token (parser->lexer);
23569 /* Skip the initializer. */
23570 cp_parser_skip_to_closing_brace (parser);
23571 /* Look for the trailing `}'. */
23572 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
23574 return error_mark_node;
23577 return cp_parser_constant_expression (parser);
23580 /* Derived classes [gram.class.derived] */
23582 /* Parse a base-clause.
23584 base-clause:
23585 : base-specifier-list
23587 base-specifier-list:
23588 base-specifier ... [opt]
23589 base-specifier-list , base-specifier ... [opt]
23591 Returns a TREE_LIST representing the base-classes, in the order in
23592 which they were declared. The representation of each node is as
23593 described by cp_parser_base_specifier.
23595 In the case that no bases are specified, this function will return
23596 NULL_TREE, not ERROR_MARK_NODE. */
23598 static tree
23599 cp_parser_base_clause (cp_parser* parser)
23601 tree bases = NULL_TREE;
23603 /* Look for the `:' that begins the list. */
23604 cp_parser_require (parser, CPP_COLON, RT_COLON);
23606 /* Scan the base-specifier-list. */
23607 while (true)
23609 cp_token *token;
23610 tree base;
23611 bool pack_expansion_p = false;
23613 /* Look for the base-specifier. */
23614 base = cp_parser_base_specifier (parser);
23615 /* Look for the (optional) ellipsis. */
23616 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
23618 /* Consume the `...'. */
23619 cp_lexer_consume_token (parser->lexer);
23621 pack_expansion_p = true;
23624 /* Add BASE to the front of the list. */
23625 if (base && base != error_mark_node)
23627 if (pack_expansion_p)
23628 /* Make this a pack expansion type. */
23629 TREE_VALUE (base) = make_pack_expansion (TREE_VALUE (base));
23631 if (!check_for_bare_parameter_packs (TREE_VALUE (base)))
23633 TREE_CHAIN (base) = bases;
23634 bases = base;
23637 /* Peek at the next token. */
23638 token = cp_lexer_peek_token (parser->lexer);
23639 /* If it's not a comma, then the list is complete. */
23640 if (token->type != CPP_COMMA)
23641 break;
23642 /* Consume the `,'. */
23643 cp_lexer_consume_token (parser->lexer);
23646 /* PARSER->SCOPE may still be non-NULL at this point, if the last
23647 base class had a qualified name. However, the next name that
23648 appears is certainly not qualified. */
23649 parser->scope = NULL_TREE;
23650 parser->qualifying_scope = NULL_TREE;
23651 parser->object_scope = NULL_TREE;
23653 return nreverse (bases);
23656 /* Parse a base-specifier.
23658 base-specifier:
23659 :: [opt] nested-name-specifier [opt] class-name
23660 virtual access-specifier [opt] :: [opt] nested-name-specifier
23661 [opt] class-name
23662 access-specifier virtual [opt] :: [opt] nested-name-specifier
23663 [opt] class-name
23665 Returns a TREE_LIST. The TREE_PURPOSE will be one of
23666 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
23667 indicate the specifiers provided. The TREE_VALUE will be a TYPE
23668 (or the ERROR_MARK_NODE) indicating the type that was specified. */
23670 static tree
23671 cp_parser_base_specifier (cp_parser* parser)
23673 cp_token *token;
23674 bool done = false;
23675 bool virtual_p = false;
23676 bool duplicate_virtual_error_issued_p = false;
23677 bool duplicate_access_error_issued_p = false;
23678 bool class_scope_p, template_p;
23679 tree access = access_default_node;
23680 tree type;
23682 /* Process the optional `virtual' and `access-specifier'. */
23683 while (!done)
23685 /* Peek at the next token. */
23686 token = cp_lexer_peek_token (parser->lexer);
23687 /* Process `virtual'. */
23688 switch (token->keyword)
23690 case RID_VIRTUAL:
23691 /* If `virtual' appears more than once, issue an error. */
23692 if (virtual_p && !duplicate_virtual_error_issued_p)
23694 cp_parser_error (parser,
23695 "%<virtual%> specified more than once in base-specified");
23696 duplicate_virtual_error_issued_p = true;
23699 virtual_p = true;
23701 /* Consume the `virtual' token. */
23702 cp_lexer_consume_token (parser->lexer);
23704 break;
23706 case RID_PUBLIC:
23707 case RID_PROTECTED:
23708 case RID_PRIVATE:
23709 /* If more than one access specifier appears, issue an
23710 error. */
23711 if (access != access_default_node
23712 && !duplicate_access_error_issued_p)
23714 cp_parser_error (parser,
23715 "more than one access specifier in base-specified");
23716 duplicate_access_error_issued_p = true;
23719 access = ridpointers[(int) token->keyword];
23721 /* Consume the access-specifier. */
23722 cp_lexer_consume_token (parser->lexer);
23724 break;
23726 default:
23727 done = true;
23728 break;
23731 /* It is not uncommon to see programs mechanically, erroneously, use
23732 the 'typename' keyword to denote (dependent) qualified types
23733 as base classes. */
23734 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
23736 token = cp_lexer_peek_token (parser->lexer);
23737 if (!processing_template_decl)
23738 error_at (token->location,
23739 "keyword %<typename%> not allowed outside of templates");
23740 else
23741 error_at (token->location,
23742 "keyword %<typename%> not allowed in this context "
23743 "(the base class is implicitly a type)");
23744 cp_lexer_consume_token (parser->lexer);
23747 /* Look for the optional `::' operator. */
23748 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
23749 /* Look for the nested-name-specifier. The simplest way to
23750 implement:
23752 [temp.res]
23754 The keyword `typename' is not permitted in a base-specifier or
23755 mem-initializer; in these contexts a qualified name that
23756 depends on a template-parameter is implicitly assumed to be a
23757 type name.
23759 is to pretend that we have seen the `typename' keyword at this
23760 point. */
23761 cp_parser_nested_name_specifier_opt (parser,
23762 /*typename_keyword_p=*/true,
23763 /*check_dependency_p=*/true,
23764 /*type_p=*/true,
23765 /*is_declaration=*/true);
23766 /* If the base class is given by a qualified name, assume that names
23767 we see are type names or templates, as appropriate. */
23768 class_scope_p = (parser->scope && TYPE_P (parser->scope));
23769 template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
23771 if (!parser->scope
23772 && cp_lexer_next_token_is_decltype (parser->lexer))
23773 /* DR 950 allows decltype as a base-specifier. */
23774 type = cp_parser_decltype (parser);
23775 else
23777 /* Otherwise, look for the class-name. */
23778 type = cp_parser_class_name (parser,
23779 class_scope_p,
23780 template_p,
23781 typename_type,
23782 /*check_dependency_p=*/true,
23783 /*class_head_p=*/false,
23784 /*is_declaration=*/true);
23785 type = TREE_TYPE (type);
23788 if (type == error_mark_node)
23789 return error_mark_node;
23791 return finish_base_specifier (type, access, virtual_p);
23794 /* Exception handling [gram.exception] */
23796 /* Parse an (optional) noexcept-specification.
23798 noexcept-specification:
23799 noexcept ( constant-expression ) [opt]
23801 If no noexcept-specification is present, returns NULL_TREE.
23802 Otherwise, if REQUIRE_CONSTEXPR is false, then either parse and return any
23803 expression if parentheses follow noexcept, or return BOOLEAN_TRUE_NODE if
23804 there are no parentheses. CONSUMED_EXPR will be set accordingly.
23805 Otherwise, returns a noexcept specification unless RETURN_COND is true,
23806 in which case a boolean condition is returned instead. */
23808 static tree
23809 cp_parser_noexcept_specification_opt (cp_parser* parser,
23810 bool require_constexpr,
23811 bool* consumed_expr,
23812 bool return_cond)
23814 cp_token *token;
23815 const char *saved_message;
23817 /* Peek at the next token. */
23818 token = cp_lexer_peek_token (parser->lexer);
23820 /* Is it a noexcept-specification? */
23821 if (cp_parser_is_keyword (token, RID_NOEXCEPT))
23823 tree expr;
23824 cp_lexer_consume_token (parser->lexer);
23826 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
23828 cp_lexer_consume_token (parser->lexer);
23830 if (require_constexpr)
23832 /* Types may not be defined in an exception-specification. */
23833 saved_message = parser->type_definition_forbidden_message;
23834 parser->type_definition_forbidden_message
23835 = G_("types may not be defined in an exception-specification");
23837 expr = cp_parser_constant_expression (parser);
23839 /* Restore the saved message. */
23840 parser->type_definition_forbidden_message = saved_message;
23842 else
23844 expr = cp_parser_expression (parser);
23845 *consumed_expr = true;
23848 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
23850 else
23852 expr = boolean_true_node;
23853 if (!require_constexpr)
23854 *consumed_expr = false;
23857 /* We cannot build a noexcept-spec right away because this will check
23858 that expr is a constexpr. */
23859 if (!return_cond)
23860 return build_noexcept_spec (expr, tf_warning_or_error);
23861 else
23862 return expr;
23864 else
23865 return NULL_TREE;
23868 /* Parse an (optional) exception-specification.
23870 exception-specification:
23871 throw ( type-id-list [opt] )
23873 Returns a TREE_LIST representing the exception-specification. The
23874 TREE_VALUE of each node is a type. */
23876 static tree
23877 cp_parser_exception_specification_opt (cp_parser* parser)
23879 cp_token *token;
23880 tree type_id_list;
23881 const char *saved_message;
23883 /* Peek at the next token. */
23884 token = cp_lexer_peek_token (parser->lexer);
23886 /* Is it a noexcept-specification? */
23887 type_id_list = cp_parser_noexcept_specification_opt (parser, true, NULL,
23888 false);
23889 if (type_id_list != NULL_TREE)
23890 return type_id_list;
23892 /* If it's not `throw', then there's no exception-specification. */
23893 if (!cp_parser_is_keyword (token, RID_THROW))
23894 return NULL_TREE;
23896 location_t loc = token->location;
23898 /* Consume the `throw'. */
23899 cp_lexer_consume_token (parser->lexer);
23901 /* Look for the `('. */
23902 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
23904 /* Peek at the next token. */
23905 token = cp_lexer_peek_token (parser->lexer);
23906 /* If it's not a `)', then there is a type-id-list. */
23907 if (token->type != CPP_CLOSE_PAREN)
23909 /* Types may not be defined in an exception-specification. */
23910 saved_message = parser->type_definition_forbidden_message;
23911 parser->type_definition_forbidden_message
23912 = G_("types may not be defined in an exception-specification");
23913 /* Parse the type-id-list. */
23914 type_id_list = cp_parser_type_id_list (parser);
23915 /* Restore the saved message. */
23916 parser->type_definition_forbidden_message = saved_message;
23918 if (cxx_dialect >= cxx1z)
23920 error_at (loc, "ISO C++1z does not allow dynamic exception "
23921 "specifications");
23922 type_id_list = NULL_TREE;
23924 else if (cxx_dialect >= cxx11 && !in_system_header_at (loc))
23925 warning_at (loc, OPT_Wdeprecated,
23926 "dynamic exception specifications are deprecated in C++11;"
23927 " use %<noexcept%> instead");
23929 /* In C++17, throw() is equivalent to noexcept (true). throw()
23930 is deprecated in C++11 and above as well, but is still widely used,
23931 so don't warn about it yet. */
23932 else if (cxx_dialect >= cxx1z)
23933 type_id_list = noexcept_true_spec;
23934 else
23935 type_id_list = empty_except_spec;
23937 /* Look for the `)'. */
23938 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
23940 return type_id_list;
23943 /* Parse an (optional) type-id-list.
23945 type-id-list:
23946 type-id ... [opt]
23947 type-id-list , type-id ... [opt]
23949 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
23950 in the order that the types were presented. */
23952 static tree
23953 cp_parser_type_id_list (cp_parser* parser)
23955 tree types = NULL_TREE;
23957 while (true)
23959 cp_token *token;
23960 tree type;
23962 token = cp_lexer_peek_token (parser->lexer);
23964 /* Get the next type-id. */
23965 type = cp_parser_type_id (parser);
23966 /* Check for invalid 'auto'. */
23967 if (flag_concepts && type_uses_auto (type))
23969 error_at (token->location,
23970 "invalid use of %<auto%> in exception-specification");
23971 type = error_mark_node;
23973 /* Parse the optional ellipsis. */
23974 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
23976 /* Consume the `...'. */
23977 cp_lexer_consume_token (parser->lexer);
23979 /* Turn the type into a pack expansion expression. */
23980 type = make_pack_expansion (type);
23982 /* Add it to the list. */
23983 types = add_exception_specifier (types, type, /*complain=*/1);
23984 /* Peek at the next token. */
23985 token = cp_lexer_peek_token (parser->lexer);
23986 /* If it is not a `,', we are done. */
23987 if (token->type != CPP_COMMA)
23988 break;
23989 /* Consume the `,'. */
23990 cp_lexer_consume_token (parser->lexer);
23993 return nreverse (types);
23996 /* Parse a try-block.
23998 try-block:
23999 try compound-statement handler-seq */
24001 static tree
24002 cp_parser_try_block (cp_parser* parser)
24004 tree try_block;
24006 cp_parser_require_keyword (parser, RID_TRY, RT_TRY);
24007 if (parser->in_function_body
24008 && DECL_DECLARED_CONSTEXPR_P (current_function_decl))
24009 error ("%<try%> in %<constexpr%> function");
24011 try_block = begin_try_block ();
24012 cp_parser_compound_statement (parser, NULL, BCS_TRY_BLOCK, false);
24013 finish_try_block (try_block);
24014 cp_parser_handler_seq (parser);
24015 finish_handler_sequence (try_block);
24017 return try_block;
24020 /* Parse a function-try-block.
24022 function-try-block:
24023 try ctor-initializer [opt] function-body handler-seq */
24025 static bool
24026 cp_parser_function_try_block (cp_parser* parser)
24028 tree compound_stmt;
24029 tree try_block;
24030 bool ctor_initializer_p;
24032 /* Look for the `try' keyword. */
24033 if (!cp_parser_require_keyword (parser, RID_TRY, RT_TRY))
24034 return false;
24035 /* Let the rest of the front end know where we are. */
24036 try_block = begin_function_try_block (&compound_stmt);
24037 /* Parse the function-body. */
24038 ctor_initializer_p = cp_parser_ctor_initializer_opt_and_function_body
24039 (parser, /*in_function_try_block=*/true);
24040 /* We're done with the `try' part. */
24041 finish_function_try_block (try_block);
24042 /* Parse the handlers. */
24043 cp_parser_handler_seq (parser);
24044 /* We're done with the handlers. */
24045 finish_function_handler_sequence (try_block, compound_stmt);
24047 return ctor_initializer_p;
24050 /* Parse a handler-seq.
24052 handler-seq:
24053 handler handler-seq [opt] */
24055 static void
24056 cp_parser_handler_seq (cp_parser* parser)
24058 while (true)
24060 cp_token *token;
24062 /* Parse the handler. */
24063 cp_parser_handler (parser);
24064 /* Peek at the next token. */
24065 token = cp_lexer_peek_token (parser->lexer);
24066 /* If it's not `catch' then there are no more handlers. */
24067 if (!cp_parser_is_keyword (token, RID_CATCH))
24068 break;
24072 /* Parse a handler.
24074 handler:
24075 catch ( exception-declaration ) compound-statement */
24077 static void
24078 cp_parser_handler (cp_parser* parser)
24080 tree handler;
24081 tree declaration;
24083 cp_parser_require_keyword (parser, RID_CATCH, RT_CATCH);
24084 handler = begin_handler ();
24085 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
24086 declaration = cp_parser_exception_declaration (parser);
24087 finish_handler_parms (declaration, handler);
24088 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
24089 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
24090 finish_handler (handler);
24093 /* Parse an exception-declaration.
24095 exception-declaration:
24096 type-specifier-seq declarator
24097 type-specifier-seq abstract-declarator
24098 type-specifier-seq
24101 Returns a VAR_DECL for the declaration, or NULL_TREE if the
24102 ellipsis variant is used. */
24104 static tree
24105 cp_parser_exception_declaration (cp_parser* parser)
24107 cp_decl_specifier_seq type_specifiers;
24108 cp_declarator *declarator;
24109 const char *saved_message;
24111 /* If it's an ellipsis, it's easy to handle. */
24112 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
24114 /* Consume the `...' token. */
24115 cp_lexer_consume_token (parser->lexer);
24116 return NULL_TREE;
24119 /* Types may not be defined in exception-declarations. */
24120 saved_message = parser->type_definition_forbidden_message;
24121 parser->type_definition_forbidden_message
24122 = G_("types may not be defined in exception-declarations");
24124 /* Parse the type-specifier-seq. */
24125 cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
24126 /*is_trailing_return=*/false,
24127 &type_specifiers);
24128 /* If it's a `)', then there is no declarator. */
24129 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
24130 declarator = NULL;
24131 else
24132 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
24133 /*ctor_dtor_or_conv_p=*/NULL,
24134 /*parenthesized_p=*/NULL,
24135 /*member_p=*/false,
24136 /*friend_p=*/false);
24138 /* Restore the saved message. */
24139 parser->type_definition_forbidden_message = saved_message;
24141 if (!type_specifiers.any_specifiers_p)
24142 return error_mark_node;
24144 return grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
24147 /* Parse a throw-expression.
24149 throw-expression:
24150 throw assignment-expression [opt]
24152 Returns a THROW_EXPR representing the throw-expression. */
24154 static tree
24155 cp_parser_throw_expression (cp_parser* parser)
24157 tree expression;
24158 cp_token* token;
24160 cp_parser_require_keyword (parser, RID_THROW, RT_THROW);
24161 token = cp_lexer_peek_token (parser->lexer);
24162 /* Figure out whether or not there is an assignment-expression
24163 following the "throw" keyword. */
24164 if (token->type == CPP_COMMA
24165 || token->type == CPP_SEMICOLON
24166 || token->type == CPP_CLOSE_PAREN
24167 || token->type == CPP_CLOSE_SQUARE
24168 || token->type == CPP_CLOSE_BRACE
24169 || token->type == CPP_COLON)
24170 expression = NULL_TREE;
24171 else
24172 expression = cp_parser_assignment_expression (parser);
24174 return build_throw (expression);
24177 /* GNU Extensions */
24179 /* Parse an (optional) asm-specification.
24181 asm-specification:
24182 asm ( string-literal )
24184 If the asm-specification is present, returns a STRING_CST
24185 corresponding to the string-literal. Otherwise, returns
24186 NULL_TREE. */
24188 static tree
24189 cp_parser_asm_specification_opt (cp_parser* parser)
24191 cp_token *token;
24192 tree asm_specification;
24194 /* Peek at the next token. */
24195 token = cp_lexer_peek_token (parser->lexer);
24196 /* If the next token isn't the `asm' keyword, then there's no
24197 asm-specification. */
24198 if (!cp_parser_is_keyword (token, RID_ASM))
24199 return NULL_TREE;
24201 /* Consume the `asm' token. */
24202 cp_lexer_consume_token (parser->lexer);
24203 /* Look for the `('. */
24204 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
24206 /* Look for the string-literal. */
24207 asm_specification = cp_parser_string_literal (parser, false, false);
24209 /* Look for the `)'. */
24210 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
24212 return asm_specification;
24215 /* Parse an asm-operand-list.
24217 asm-operand-list:
24218 asm-operand
24219 asm-operand-list , asm-operand
24221 asm-operand:
24222 string-literal ( expression )
24223 [ string-literal ] string-literal ( expression )
24225 Returns a TREE_LIST representing the operands. The TREE_VALUE of
24226 each node is the expression. The TREE_PURPOSE is itself a
24227 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
24228 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
24229 is a STRING_CST for the string literal before the parenthesis. Returns
24230 ERROR_MARK_NODE if any of the operands are invalid. */
24232 static tree
24233 cp_parser_asm_operand_list (cp_parser* parser)
24235 tree asm_operands = NULL_TREE;
24236 bool invalid_operands = false;
24238 while (true)
24240 tree string_literal;
24241 tree expression;
24242 tree name;
24244 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
24246 /* Consume the `[' token. */
24247 cp_lexer_consume_token (parser->lexer);
24248 /* Read the operand name. */
24249 name = cp_parser_identifier (parser);
24250 if (name != error_mark_node)
24251 name = build_string (IDENTIFIER_LENGTH (name),
24252 IDENTIFIER_POINTER (name));
24253 /* Look for the closing `]'. */
24254 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
24256 else
24257 name = NULL_TREE;
24258 /* Look for the string-literal. */
24259 string_literal = cp_parser_string_literal (parser, false, false);
24261 /* Look for the `('. */
24262 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
24263 /* Parse the expression. */
24264 expression = cp_parser_expression (parser);
24265 /* Look for the `)'. */
24266 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
24268 if (name == error_mark_node
24269 || string_literal == error_mark_node
24270 || expression == error_mark_node)
24271 invalid_operands = true;
24273 /* Add this operand to the list. */
24274 asm_operands = tree_cons (build_tree_list (name, string_literal),
24275 expression,
24276 asm_operands);
24277 /* If the next token is not a `,', there are no more
24278 operands. */
24279 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
24280 break;
24281 /* Consume the `,'. */
24282 cp_lexer_consume_token (parser->lexer);
24285 return invalid_operands ? error_mark_node : nreverse (asm_operands);
24288 /* Parse an asm-clobber-list.
24290 asm-clobber-list:
24291 string-literal
24292 asm-clobber-list , string-literal
24294 Returns a TREE_LIST, indicating the clobbers in the order that they
24295 appeared. The TREE_VALUE of each node is a STRING_CST. */
24297 static tree
24298 cp_parser_asm_clobber_list (cp_parser* parser)
24300 tree clobbers = NULL_TREE;
24302 while (true)
24304 tree string_literal;
24306 /* Look for the string literal. */
24307 string_literal = cp_parser_string_literal (parser, false, false);
24308 /* Add it to the list. */
24309 clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
24310 /* If the next token is not a `,', then the list is
24311 complete. */
24312 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
24313 break;
24314 /* Consume the `,' token. */
24315 cp_lexer_consume_token (parser->lexer);
24318 return clobbers;
24321 /* Parse an asm-label-list.
24323 asm-label-list:
24324 identifier
24325 asm-label-list , identifier
24327 Returns a TREE_LIST, indicating the labels in the order that they
24328 appeared. The TREE_VALUE of each node is a label. */
24330 static tree
24331 cp_parser_asm_label_list (cp_parser* parser)
24333 tree labels = NULL_TREE;
24335 while (true)
24337 tree identifier, label, name;
24339 /* Look for the identifier. */
24340 identifier = cp_parser_identifier (parser);
24341 if (!error_operand_p (identifier))
24343 label = lookup_label (identifier);
24344 if (TREE_CODE (label) == LABEL_DECL)
24346 TREE_USED (label) = 1;
24347 check_goto (label);
24348 name = build_string (IDENTIFIER_LENGTH (identifier),
24349 IDENTIFIER_POINTER (identifier));
24350 labels = tree_cons (name, label, labels);
24353 /* If the next token is not a `,', then the list is
24354 complete. */
24355 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
24356 break;
24357 /* Consume the `,' token. */
24358 cp_lexer_consume_token (parser->lexer);
24361 return nreverse (labels);
24364 /* Return TRUE iff the next tokens in the stream are possibly the
24365 beginning of a GNU extension attribute. */
24367 static bool
24368 cp_next_tokens_can_be_gnu_attribute_p (cp_parser *parser)
24370 return cp_nth_tokens_can_be_gnu_attribute_p (parser, 1);
24373 /* Return TRUE iff the next tokens in the stream are possibly the
24374 beginning of a standard C++-11 attribute specifier. */
24376 static bool
24377 cp_next_tokens_can_be_std_attribute_p (cp_parser *parser)
24379 return cp_nth_tokens_can_be_std_attribute_p (parser, 1);
24382 /* Return TRUE iff the next Nth tokens in the stream are possibly the
24383 beginning of a standard C++-11 attribute specifier. */
24385 static bool
24386 cp_nth_tokens_can_be_std_attribute_p (cp_parser *parser, size_t n)
24388 cp_token *token = cp_lexer_peek_nth_token (parser->lexer, n);
24390 return (cxx_dialect >= cxx11
24391 && ((token->type == CPP_KEYWORD && token->keyword == RID_ALIGNAS)
24392 || (token->type == CPP_OPEN_SQUARE
24393 && (token = cp_lexer_peek_nth_token (parser->lexer, n + 1))
24394 && token->type == CPP_OPEN_SQUARE)));
24397 /* Return TRUE iff the next Nth tokens in the stream are possibly the
24398 beginning of a GNU extension attribute. */
24400 static bool
24401 cp_nth_tokens_can_be_gnu_attribute_p (cp_parser *parser, size_t n)
24403 cp_token *token = cp_lexer_peek_nth_token (parser->lexer, n);
24405 return token->type == CPP_KEYWORD && token->keyword == RID_ATTRIBUTE;
24408 /* Return true iff the next tokens can be the beginning of either a
24409 GNU attribute list, or a standard C++11 attribute sequence. */
24411 static bool
24412 cp_next_tokens_can_be_attribute_p (cp_parser *parser)
24414 return (cp_next_tokens_can_be_gnu_attribute_p (parser)
24415 || cp_next_tokens_can_be_std_attribute_p (parser));
24418 /* Return true iff the next Nth tokens can be the beginning of either
24419 a GNU attribute list, or a standard C++11 attribute sequence. */
24421 static bool
24422 cp_nth_tokens_can_be_attribute_p (cp_parser *parser, size_t n)
24424 return (cp_nth_tokens_can_be_gnu_attribute_p (parser, n)
24425 || cp_nth_tokens_can_be_std_attribute_p (parser, n));
24428 /* Parse either a standard C++-11 attribute-specifier-seq, or a series
24429 of GNU attributes, or return NULL. */
24431 static tree
24432 cp_parser_attributes_opt (cp_parser *parser)
24434 if (cp_next_tokens_can_be_gnu_attribute_p (parser))
24435 return cp_parser_gnu_attributes_opt (parser);
24436 return cp_parser_std_attribute_spec_seq (parser);
24439 #define CILK_SIMD_FN_CLAUSE_MASK \
24440 ((OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_VECTORLENGTH) \
24441 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_LINEAR) \
24442 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_UNIFORM) \
24443 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_MASK) \
24444 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_NOMASK))
24446 /* Parses the Cilk Plus SIMD-enabled function's attribute. Syntax:
24447 vector [(<clauses>)] */
24449 static void
24450 cp_parser_cilk_simd_fn_vector_attrs (cp_parser *parser, cp_token *v_token)
24452 bool first_p = parser->cilk_simd_fn_info == NULL;
24453 cp_token *token = v_token;
24454 if (first_p)
24456 parser->cilk_simd_fn_info = XNEW (cp_omp_declare_simd_data);
24457 parser->cilk_simd_fn_info->error_seen = false;
24458 parser->cilk_simd_fn_info->fndecl_seen = false;
24459 parser->cilk_simd_fn_info->tokens = vNULL;
24460 parser->cilk_simd_fn_info->clauses = NULL_TREE;
24462 int paren_scope = 0;
24463 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
24465 cp_lexer_consume_token (parser->lexer);
24466 v_token = cp_lexer_peek_token (parser->lexer);
24467 paren_scope++;
24469 while (paren_scope > 0)
24471 token = cp_lexer_peek_token (parser->lexer);
24472 if (token->type == CPP_OPEN_PAREN)
24473 paren_scope++;
24474 else if (token->type == CPP_CLOSE_PAREN)
24475 paren_scope--;
24476 /* Do not push the last ')' */
24477 if (!(token->type == CPP_CLOSE_PAREN && paren_scope == 0))
24478 cp_lexer_consume_token (parser->lexer);
24481 token->type = CPP_PRAGMA_EOL;
24482 parser->lexer->next_token = token;
24483 cp_lexer_consume_token (parser->lexer);
24485 struct cp_token_cache *cp
24486 = cp_token_cache_new (v_token, cp_lexer_peek_token (parser->lexer));
24487 parser->cilk_simd_fn_info->tokens.safe_push (cp);
24490 /* Parse an (optional) series of attributes.
24492 attributes:
24493 attributes attribute
24495 attribute:
24496 __attribute__ (( attribute-list [opt] ))
24498 The return value is as for cp_parser_gnu_attribute_list. */
24500 static tree
24501 cp_parser_gnu_attributes_opt (cp_parser* parser)
24503 tree attributes = NULL_TREE;
24505 while (true)
24507 cp_token *token;
24508 tree attribute_list;
24509 bool ok = true;
24511 /* Peek at the next token. */
24512 token = cp_lexer_peek_token (parser->lexer);
24513 /* If it's not `__attribute__', then we're done. */
24514 if (token->keyword != RID_ATTRIBUTE)
24515 break;
24517 /* Consume the `__attribute__' keyword. */
24518 cp_lexer_consume_token (parser->lexer);
24519 /* Look for the two `(' tokens. */
24520 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
24521 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
24523 /* Peek at the next token. */
24524 token = cp_lexer_peek_token (parser->lexer);
24525 if (token->type != CPP_CLOSE_PAREN)
24526 /* Parse the attribute-list. */
24527 attribute_list = cp_parser_gnu_attribute_list (parser);
24528 else
24529 /* If the next token is a `)', then there is no attribute
24530 list. */
24531 attribute_list = NULL;
24533 /* Look for the two `)' tokens. */
24534 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
24535 ok = false;
24536 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
24537 ok = false;
24538 if (!ok)
24539 cp_parser_skip_to_end_of_statement (parser);
24541 /* Add these new attributes to the list. */
24542 attributes = chainon (attributes, attribute_list);
24545 return attributes;
24548 /* Parse a GNU attribute-list.
24550 attribute-list:
24551 attribute
24552 attribute-list , attribute
24554 attribute:
24555 identifier
24556 identifier ( identifier )
24557 identifier ( identifier , expression-list )
24558 identifier ( expression-list )
24560 Returns a TREE_LIST, or NULL_TREE on error. Each node corresponds
24561 to an attribute. The TREE_PURPOSE of each node is the identifier
24562 indicating which attribute is in use. The TREE_VALUE represents
24563 the arguments, if any. */
24565 static tree
24566 cp_parser_gnu_attribute_list (cp_parser* parser)
24568 tree attribute_list = NULL_TREE;
24569 bool save_translate_strings_p = parser->translate_strings_p;
24571 parser->translate_strings_p = false;
24572 while (true)
24574 cp_token *token;
24575 tree identifier;
24576 tree attribute;
24578 /* Look for the identifier. We also allow keywords here; for
24579 example `__attribute__ ((const))' is legal. */
24580 token = cp_lexer_peek_token (parser->lexer);
24581 if (token->type == CPP_NAME
24582 || token->type == CPP_KEYWORD)
24584 tree arguments = NULL_TREE;
24586 /* Consume the token, but save it since we need it for the
24587 SIMD enabled function parsing. */
24588 cp_token *id_token = cp_lexer_consume_token (parser->lexer);
24590 /* Save away the identifier that indicates which attribute
24591 this is. */
24592 identifier = (token->type == CPP_KEYWORD)
24593 /* For keywords, use the canonical spelling, not the
24594 parsed identifier. */
24595 ? ridpointers[(int) token->keyword]
24596 : id_token->u.value;
24598 attribute = build_tree_list (identifier, NULL_TREE);
24600 /* Peek at the next token. */
24601 token = cp_lexer_peek_token (parser->lexer);
24602 /* If it's an `(', then parse the attribute arguments. */
24603 if (token->type == CPP_OPEN_PAREN)
24605 vec<tree, va_gc> *vec;
24606 int attr_flag = (attribute_takes_identifier_p (identifier)
24607 ? id_attr : normal_attr);
24608 if (is_cilkplus_vector_p (identifier))
24610 cp_parser_cilk_simd_fn_vector_attrs (parser, id_token);
24611 continue;
24613 else
24614 vec = cp_parser_parenthesized_expression_list
24615 (parser, attr_flag, /*cast_p=*/false,
24616 /*allow_expansion_p=*/false,
24617 /*non_constant_p=*/NULL);
24618 if (vec == NULL)
24619 arguments = error_mark_node;
24620 else
24622 arguments = build_tree_list_vec (vec);
24623 release_tree_vector (vec);
24625 /* Save the arguments away. */
24626 TREE_VALUE (attribute) = arguments;
24628 else if (is_cilkplus_vector_p (identifier))
24630 cp_parser_cilk_simd_fn_vector_attrs (parser, id_token);
24631 continue;
24634 if (arguments != error_mark_node)
24636 /* Add this attribute to the list. */
24637 TREE_CHAIN (attribute) = attribute_list;
24638 attribute_list = attribute;
24641 token = cp_lexer_peek_token (parser->lexer);
24643 /* Now, look for more attributes. If the next token isn't a
24644 `,', we're done. */
24645 if (token->type != CPP_COMMA)
24646 break;
24648 /* Consume the comma and keep going. */
24649 cp_lexer_consume_token (parser->lexer);
24651 parser->translate_strings_p = save_translate_strings_p;
24653 /* We built up the list in reverse order. */
24654 return nreverse (attribute_list);
24657 /* Parse a standard C++11 attribute.
24659 The returned representation is a TREE_LIST which TREE_PURPOSE is
24660 the scoped name of the attribute, and the TREE_VALUE is its
24661 arguments list.
24663 Note that the scoped name of the attribute is itself a TREE_LIST
24664 which TREE_PURPOSE is the namespace of the attribute, and
24665 TREE_VALUE its name. This is unlike a GNU attribute -- as parsed
24666 by cp_parser_gnu_attribute_list -- that doesn't have any namespace
24667 and which TREE_PURPOSE is directly the attribute name.
24669 Clients of the attribute code should use get_attribute_namespace
24670 and get_attribute_name to get the actual namespace and name of
24671 attributes, regardless of their being GNU or C++11 attributes.
24673 attribute:
24674 attribute-token attribute-argument-clause [opt]
24676 attribute-token:
24677 identifier
24678 attribute-scoped-token
24680 attribute-scoped-token:
24681 attribute-namespace :: identifier
24683 attribute-namespace:
24684 identifier
24686 attribute-argument-clause:
24687 ( balanced-token-seq )
24689 balanced-token-seq:
24690 balanced-token [opt]
24691 balanced-token-seq balanced-token
24693 balanced-token:
24694 ( balanced-token-seq )
24695 [ balanced-token-seq ]
24696 { balanced-token-seq }. */
24698 static tree
24699 cp_parser_std_attribute (cp_parser *parser, tree attr_ns)
24701 tree attribute, attr_id = NULL_TREE, arguments;
24702 cp_token *token;
24704 /* First, parse name of the attribute, a.k.a attribute-token. */
24706 token = cp_lexer_peek_token (parser->lexer);
24707 if (token->type == CPP_NAME)
24708 attr_id = token->u.value;
24709 else if (token->type == CPP_KEYWORD)
24710 attr_id = ridpointers[(int) token->keyword];
24711 else if (token->flags & NAMED_OP)
24712 attr_id = get_identifier (cpp_type2name (token->type, token->flags));
24714 if (attr_id == NULL_TREE)
24715 return NULL_TREE;
24717 cp_lexer_consume_token (parser->lexer);
24719 token = cp_lexer_peek_token (parser->lexer);
24720 if (token->type == CPP_SCOPE)
24722 /* We are seeing a scoped attribute token. */
24724 cp_lexer_consume_token (parser->lexer);
24725 if (attr_ns)
24726 error_at (token->location, "attribute using prefix used together "
24727 "with scoped attribute token");
24728 attr_ns = attr_id;
24730 token = cp_lexer_consume_token (parser->lexer);
24731 if (token->type == CPP_NAME)
24732 attr_id = token->u.value;
24733 else if (token->type == CPP_KEYWORD)
24734 attr_id = ridpointers[(int) token->keyword];
24735 else if (token->flags & NAMED_OP)
24736 attr_id = get_identifier (cpp_type2name (token->type, token->flags));
24737 else
24739 error_at (token->location,
24740 "expected an identifier for the attribute name");
24741 return error_mark_node;
24743 attribute = build_tree_list (build_tree_list (attr_ns, attr_id),
24744 NULL_TREE);
24745 token = cp_lexer_peek_token (parser->lexer);
24747 else if (attr_ns)
24748 attribute = build_tree_list (build_tree_list (attr_ns, attr_id),
24749 NULL_TREE);
24750 else
24752 attribute = build_tree_list (build_tree_list (NULL_TREE, attr_id),
24753 NULL_TREE);
24754 /* C++11 noreturn attribute is equivalent to GNU's. */
24755 if (is_attribute_p ("noreturn", attr_id))
24756 TREE_PURPOSE (TREE_PURPOSE (attribute)) = get_identifier ("gnu");
24757 /* C++14 deprecated attribute is equivalent to GNU's. */
24758 else if (is_attribute_p ("deprecated", attr_id))
24759 TREE_PURPOSE (TREE_PURPOSE (attribute)) = get_identifier ("gnu");
24760 /* C++17 fallthrough attribute is equivalent to GNU's. */
24761 else if (is_attribute_p ("fallthrough", attr_id))
24762 TREE_PURPOSE (TREE_PURPOSE (attribute)) = get_identifier ("gnu");
24763 /* Transactional Memory TS optimize_for_synchronized attribute is
24764 equivalent to GNU transaction_callable. */
24765 else if (is_attribute_p ("optimize_for_synchronized", attr_id))
24766 TREE_PURPOSE (attribute)
24767 = get_identifier ("transaction_callable");
24768 /* Transactional Memory attributes are GNU attributes. */
24769 else if (tm_attr_to_mask (attr_id))
24770 TREE_PURPOSE (attribute) = attr_id;
24773 /* Now parse the optional argument clause of the attribute. */
24775 if (token->type != CPP_OPEN_PAREN)
24776 return attribute;
24779 vec<tree, va_gc> *vec;
24780 int attr_flag = normal_attr;
24782 if (attr_ns == get_identifier ("gnu")
24783 && attribute_takes_identifier_p (attr_id))
24784 /* A GNU attribute that takes an identifier in parameter. */
24785 attr_flag = id_attr;
24787 vec = cp_parser_parenthesized_expression_list
24788 (parser, attr_flag, /*cast_p=*/false,
24789 /*allow_expansion_p=*/true,
24790 /*non_constant_p=*/NULL);
24791 if (vec == NULL)
24792 arguments = error_mark_node;
24793 else
24795 arguments = build_tree_list_vec (vec);
24796 release_tree_vector (vec);
24799 if (arguments == error_mark_node)
24800 attribute = error_mark_node;
24801 else
24802 TREE_VALUE (attribute) = arguments;
24805 return attribute;
24808 /* Check that the attribute ATTRIBUTE appears at most once in the
24809 attribute-list ATTRIBUTES. This is enforced for noreturn (7.6.3)
24810 and deprecated (7.6.5). Note that carries_dependency (7.6.4)
24811 isn't implemented yet in GCC. */
24813 static void
24814 cp_parser_check_std_attribute (tree attributes, tree attribute)
24816 if (attributes)
24818 tree name = get_attribute_name (attribute);
24819 if (is_attribute_p ("noreturn", name)
24820 && lookup_attribute ("noreturn", attributes))
24821 error ("attribute %<noreturn%> can appear at most once "
24822 "in an attribute-list");
24823 else if (is_attribute_p ("deprecated", name)
24824 && lookup_attribute ("deprecated", attributes))
24825 error ("attribute %<deprecated%> can appear at most once "
24826 "in an attribute-list");
24830 /* Parse a list of standard C++-11 attributes.
24832 attribute-list:
24833 attribute [opt]
24834 attribute-list , attribute[opt]
24835 attribute ...
24836 attribute-list , attribute ...
24839 static tree
24840 cp_parser_std_attribute_list (cp_parser *parser, tree attr_ns)
24842 tree attributes = NULL_TREE, attribute = NULL_TREE;
24843 cp_token *token = NULL;
24845 while (true)
24847 attribute = cp_parser_std_attribute (parser, attr_ns);
24848 if (attribute == error_mark_node)
24849 break;
24850 if (attribute != NULL_TREE)
24852 cp_parser_check_std_attribute (attributes, attribute);
24853 TREE_CHAIN (attribute) = attributes;
24854 attributes = attribute;
24856 token = cp_lexer_peek_token (parser->lexer);
24857 if (token->type == CPP_ELLIPSIS)
24859 cp_lexer_consume_token (parser->lexer);
24860 if (attribute == NULL_TREE)
24861 error_at (token->location,
24862 "expected attribute before %<...%>");
24863 else
24864 TREE_VALUE (attribute)
24865 = make_pack_expansion (TREE_VALUE (attribute));
24866 token = cp_lexer_peek_token (parser->lexer);
24868 if (token->type != CPP_COMMA)
24869 break;
24870 cp_lexer_consume_token (parser->lexer);
24872 attributes = nreverse (attributes);
24873 return attributes;
24876 /* Parse a standard C++-11 attribute specifier.
24878 attribute-specifier:
24879 [ [ attribute-using-prefix [opt] attribute-list ] ]
24880 alignment-specifier
24882 attribute-using-prefix:
24883 using attribute-namespace :
24885 alignment-specifier:
24886 alignas ( type-id ... [opt] )
24887 alignas ( alignment-expression ... [opt] ). */
24889 static tree
24890 cp_parser_std_attribute_spec (cp_parser *parser)
24892 tree attributes = NULL_TREE;
24893 cp_token *token = cp_lexer_peek_token (parser->lexer);
24895 if (token->type == CPP_OPEN_SQUARE
24896 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_OPEN_SQUARE)
24898 tree attr_ns = NULL_TREE;
24900 cp_lexer_consume_token (parser->lexer);
24901 cp_lexer_consume_token (parser->lexer);
24903 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
24905 token = cp_lexer_peek_nth_token (parser->lexer, 2);
24906 if (token->type == CPP_NAME)
24907 attr_ns = token->u.value;
24908 else if (token->type == CPP_KEYWORD)
24909 attr_ns = ridpointers[(int) token->keyword];
24910 else if (token->flags & NAMED_OP)
24911 attr_ns = get_identifier (cpp_type2name (token->type,
24912 token->flags));
24913 if (attr_ns
24914 && cp_lexer_nth_token_is (parser->lexer, 3, CPP_COLON))
24916 if (cxx_dialect < cxx1z
24917 && !in_system_header_at (input_location))
24918 pedwarn (input_location, 0,
24919 "attribute using prefix only available "
24920 "with -std=c++1z or -std=gnu++1z");
24922 cp_lexer_consume_token (parser->lexer);
24923 cp_lexer_consume_token (parser->lexer);
24924 cp_lexer_consume_token (parser->lexer);
24926 else
24927 attr_ns = NULL_TREE;
24930 attributes = cp_parser_std_attribute_list (parser, attr_ns);
24932 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE)
24933 || !cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
24934 cp_parser_skip_to_end_of_statement (parser);
24935 else
24936 /* Warn about parsing c++11 attribute in non-c++1 mode, only
24937 when we are sure that we have actually parsed them. */
24938 maybe_warn_cpp0x (CPP0X_ATTRIBUTES);
24940 else
24942 tree alignas_expr;
24944 /* Look for an alignment-specifier. */
24946 token = cp_lexer_peek_token (parser->lexer);
24948 if (token->type != CPP_KEYWORD
24949 || token->keyword != RID_ALIGNAS)
24950 return NULL_TREE;
24952 cp_lexer_consume_token (parser->lexer);
24953 maybe_warn_cpp0x (CPP0X_ATTRIBUTES);
24955 if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN) == NULL)
24957 cp_parser_error (parser, "expected %<(%>");
24958 return error_mark_node;
24961 cp_parser_parse_tentatively (parser);
24962 alignas_expr = cp_parser_type_id (parser);
24964 if (!cp_parser_parse_definitely (parser))
24966 alignas_expr = cp_parser_assignment_expression (parser);
24967 if (alignas_expr == error_mark_node)
24968 cp_parser_skip_to_end_of_statement (parser);
24969 if (alignas_expr == NULL_TREE
24970 || alignas_expr == error_mark_node)
24971 return alignas_expr;
24974 alignas_expr = cxx_alignas_expr (alignas_expr);
24975 alignas_expr = build_tree_list (NULL_TREE, alignas_expr);
24977 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
24979 cp_lexer_consume_token (parser->lexer);
24980 alignas_expr = make_pack_expansion (alignas_expr);
24983 if (cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN) == NULL)
24985 cp_parser_error (parser, "expected %<)%>");
24986 return error_mark_node;
24989 /* Build the C++-11 representation of an 'aligned'
24990 attribute. */
24991 attributes =
24992 build_tree_list (build_tree_list (get_identifier ("gnu"),
24993 get_identifier ("aligned")),
24994 alignas_expr);
24997 return attributes;
25000 /* Parse a standard C++-11 attribute-specifier-seq.
25002 attribute-specifier-seq:
25003 attribute-specifier-seq [opt] attribute-specifier
25006 static tree
25007 cp_parser_std_attribute_spec_seq (cp_parser *parser)
25009 tree attr_specs = NULL_TREE;
25010 tree attr_last = NULL_TREE;
25012 while (true)
25014 tree attr_spec = cp_parser_std_attribute_spec (parser);
25015 if (attr_spec == NULL_TREE)
25016 break;
25017 if (attr_spec == error_mark_node)
25018 return error_mark_node;
25020 if (attr_last)
25021 TREE_CHAIN (attr_last) = attr_spec;
25022 else
25023 attr_specs = attr_last = attr_spec;
25024 attr_last = tree_last (attr_last);
25027 return attr_specs;
25030 /* Parse an optional `__extension__' keyword. Returns TRUE if it is
25031 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
25032 current value of the PEDANTIC flag, regardless of whether or not
25033 the `__extension__' keyword is present. The caller is responsible
25034 for restoring the value of the PEDANTIC flag. */
25036 static bool
25037 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
25039 /* Save the old value of the PEDANTIC flag. */
25040 *saved_pedantic = pedantic;
25042 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
25044 /* Consume the `__extension__' token. */
25045 cp_lexer_consume_token (parser->lexer);
25046 /* We're not being pedantic while the `__extension__' keyword is
25047 in effect. */
25048 pedantic = 0;
25050 return true;
25053 return false;
25056 /* Parse a label declaration.
25058 label-declaration:
25059 __label__ label-declarator-seq ;
25061 label-declarator-seq:
25062 identifier , label-declarator-seq
25063 identifier */
25065 static void
25066 cp_parser_label_declaration (cp_parser* parser)
25068 /* Look for the `__label__' keyword. */
25069 cp_parser_require_keyword (parser, RID_LABEL, RT_LABEL);
25071 while (true)
25073 tree identifier;
25075 /* Look for an identifier. */
25076 identifier = cp_parser_identifier (parser);
25077 /* If we failed, stop. */
25078 if (identifier == error_mark_node)
25079 break;
25080 /* Declare it as a label. */
25081 finish_label_decl (identifier);
25082 /* If the next token is a `;', stop. */
25083 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
25084 break;
25085 /* Look for the `,' separating the label declarations. */
25086 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
25089 /* Look for the final `;'. */
25090 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
25093 // -------------------------------------------------------------------------- //
25094 // Requires Clause
25096 // Parse a requires clause.
25098 // requires-clause:
25099 // 'requires' logical-or-expression
25101 // The required logical-or-expression must be a constant expression. Note
25102 // that we don't check that the expression is constepxr here. We defer until
25103 // we analyze constraints and then, we only check atomic constraints.
25104 static tree
25105 cp_parser_requires_clause (cp_parser *parser)
25107 // Parse the requires clause so that it is not automatically folded.
25108 ++processing_template_decl;
25109 tree expr = cp_parser_binary_expression (parser, false, false,
25110 PREC_NOT_OPERATOR, NULL);
25111 if (check_for_bare_parameter_packs (expr))
25112 expr = error_mark_node;
25113 --processing_template_decl;
25114 return expr;
25117 // Optionally parse a requires clause:
25118 static tree
25119 cp_parser_requires_clause_opt (cp_parser *parser)
25121 cp_token *tok = cp_lexer_peek_token (parser->lexer);
25122 if (tok->keyword != RID_REQUIRES)
25124 if (!flag_concepts && tok->type == CPP_NAME
25125 && tok->u.value == ridpointers[RID_REQUIRES])
25127 error_at (cp_lexer_peek_token (parser->lexer)->location,
25128 "%<requires%> only available with -fconcepts");
25129 /* Parse and discard the requires-clause. */
25130 cp_lexer_consume_token (parser->lexer);
25131 cp_parser_requires_clause (parser);
25133 return NULL_TREE;
25135 cp_lexer_consume_token (parser->lexer);
25136 return cp_parser_requires_clause (parser);
25140 /*---------------------------------------------------------------------------
25141 Requires expressions
25142 ---------------------------------------------------------------------------*/
25144 /* Parse a requires expression
25146 requirement-expression:
25147 'requires' requirement-parameter-list [opt] requirement-body */
25148 static tree
25149 cp_parser_requires_expression (cp_parser *parser)
25151 gcc_assert (cp_lexer_next_token_is_keyword (parser->lexer, RID_REQUIRES));
25152 location_t loc = cp_lexer_consume_token (parser->lexer)->location;
25154 /* A requires-expression shall appear only within a concept
25155 definition or a requires-clause.
25157 TODO: Implement this diagnostic correctly. */
25158 if (!processing_template_decl)
25160 error_at (loc, "a requires expression cannot appear outside a template");
25161 cp_parser_skip_to_end_of_statement (parser);
25162 return error_mark_node;
25165 tree parms, reqs;
25167 /* Local parameters are delared as variables within the scope
25168 of the expression. They are not visible past the end of
25169 the expression. Expressions within the requires-expression
25170 are unevaluated. */
25171 struct scope_sentinel
25173 scope_sentinel ()
25175 ++cp_unevaluated_operand;
25176 begin_scope (sk_block, NULL_TREE);
25179 ~scope_sentinel ()
25181 pop_bindings_and_leave_scope ();
25182 --cp_unevaluated_operand;
25184 } s;
25186 /* Parse the optional parameter list. */
25187 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
25189 parms = cp_parser_requirement_parameter_list (parser);
25190 if (parms == error_mark_node)
25191 return error_mark_node;
25193 else
25194 parms = NULL_TREE;
25196 /* Parse the requirement body. */
25197 reqs = cp_parser_requirement_body (parser);
25198 if (reqs == error_mark_node)
25199 return error_mark_node;
25202 /* This needs to happen after pop_bindings_and_leave_scope, as it reverses
25203 the parm chain. */
25204 grokparms (parms, &parms);
25205 return finish_requires_expr (parms, reqs);
25208 /* Parse a parameterized requirement.
25210 requirement-parameter-list:
25211 '(' parameter-declaration-clause ')' */
25212 static tree
25213 cp_parser_requirement_parameter_list (cp_parser *parser)
25215 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
25216 return error_mark_node;
25218 tree parms = cp_parser_parameter_declaration_clause (parser);
25220 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
25221 return error_mark_node;
25223 return parms;
25226 /* Parse the body of a requirement.
25228 requirement-body:
25229 '{' requirement-list '}' */
25230 static tree
25231 cp_parser_requirement_body (cp_parser *parser)
25233 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
25234 return error_mark_node;
25236 tree reqs = cp_parser_requirement_list (parser);
25238 if (!cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE))
25239 return error_mark_node;
25241 return reqs;
25244 /* Parse a list of requirements.
25246 requirement-list:
25247 requirement
25248 requirement-list ';' requirement[opt] */
25249 static tree
25250 cp_parser_requirement_list (cp_parser *parser)
25252 tree result = NULL_TREE;
25253 while (true)
25255 tree req = cp_parser_requirement (parser);
25256 if (req == error_mark_node)
25257 return error_mark_node;
25259 result = tree_cons (NULL_TREE, req, result);
25261 /* If we see a semi-colon, consume it. */
25262 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
25263 cp_lexer_consume_token (parser->lexer);
25265 /* Stop processing at the end of the list. */
25266 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
25267 break;
25270 /* Reverse the order of requirements so they are analyzed in
25271 declaration order. */
25272 return nreverse (result);
25275 /* Parse a syntactic requirement or type requirement.
25277 requirement:
25278 simple-requirement
25279 compound-requirement
25280 type-requirement
25281 nested-requirement */
25282 static tree
25283 cp_parser_requirement (cp_parser *parser)
25285 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
25286 return cp_parser_compound_requirement (parser);
25287 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
25288 return cp_parser_type_requirement (parser);
25289 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_REQUIRES))
25290 return cp_parser_nested_requirement (parser);
25291 else
25292 return cp_parser_simple_requirement (parser);
25295 /* Parse a simple requirement.
25297 simple-requirement:
25298 expression ';' */
25299 static tree
25300 cp_parser_simple_requirement (cp_parser *parser)
25302 tree expr = cp_parser_expression (parser, NULL, false, false);
25303 if (!expr || expr == error_mark_node)
25304 return error_mark_node;
25306 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
25307 return error_mark_node;
25309 return finish_simple_requirement (expr);
25312 /* Parse a type requirement
25314 type-requirement
25315 nested-name-specifier [opt] required-type-name ';'
25317 required-type-name:
25318 type-name
25319 'template' [opt] simple-template-id */
25320 static tree
25321 cp_parser_type_requirement (cp_parser *parser)
25323 cp_lexer_consume_token (parser->lexer);
25325 // Save the scope before parsing name specifiers.
25326 tree saved_scope = parser->scope;
25327 tree saved_object_scope = parser->object_scope;
25328 tree saved_qualifying_scope = parser->qualifying_scope;
25329 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
25330 cp_parser_nested_name_specifier_opt (parser,
25331 /*typename_keyword_p=*/true,
25332 /*check_dependency_p=*/false,
25333 /*type_p=*/true,
25334 /*is_declaration=*/false);
25336 tree type;
25337 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
25339 cp_lexer_consume_token (parser->lexer);
25340 type = cp_parser_template_id (parser,
25341 /*template_keyword_p=*/true,
25342 /*check_dependency=*/false,
25343 /*tag_type=*/none_type,
25344 /*is_declaration=*/false);
25345 type = make_typename_type (parser->scope, type, typename_type,
25346 /*complain=*/tf_error);
25348 else
25349 type = cp_parser_type_name (parser, /*typename_keyword_p=*/true);
25351 if (TREE_CODE (type) == TYPE_DECL)
25352 type = TREE_TYPE (type);
25354 parser->scope = saved_scope;
25355 parser->object_scope = saved_object_scope;
25356 parser->qualifying_scope = saved_qualifying_scope;
25358 if (type == error_mark_node)
25359 cp_parser_skip_to_end_of_statement (parser);
25361 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
25362 return error_mark_node;
25363 if (type == error_mark_node)
25364 return error_mark_node;
25366 return finish_type_requirement (type);
25369 /* Parse a compound requirement
25371 compound-requirement:
25372 '{' expression '}' 'noexcept' [opt] trailing-return-type [opt] ';' */
25373 static tree
25374 cp_parser_compound_requirement (cp_parser *parser)
25376 /* Parse an expression enclosed in '{ }'s. */
25377 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
25378 return error_mark_node;
25380 tree expr = cp_parser_expression (parser, NULL, false, false);
25381 if (!expr || expr == error_mark_node)
25382 return error_mark_node;
25384 if (!cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE))
25385 return error_mark_node;
25387 /* Parse the optional noexcept. */
25388 bool noexcept_p = false;
25389 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_NOEXCEPT))
25391 cp_lexer_consume_token (parser->lexer);
25392 noexcept_p = true;
25395 /* Parse the optional trailing return type. */
25396 tree type = NULL_TREE;
25397 if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
25399 cp_lexer_consume_token (parser->lexer);
25400 bool saved_result_type_constraint_p = parser->in_result_type_constraint_p;
25401 parser->in_result_type_constraint_p = true;
25402 type = cp_parser_trailing_type_id (parser);
25403 parser->in_result_type_constraint_p = saved_result_type_constraint_p;
25404 if (type == error_mark_node)
25405 return error_mark_node;
25408 return finish_compound_requirement (expr, type, noexcept_p);
25411 /* Parse a nested requirement. This is the same as a requires clause.
25413 nested-requirement:
25414 requires-clause */
25415 static tree
25416 cp_parser_nested_requirement (cp_parser *parser)
25418 cp_lexer_consume_token (parser->lexer);
25419 tree req = cp_parser_requires_clause (parser);
25420 if (req == error_mark_node)
25421 return error_mark_node;
25422 return finish_nested_requirement (req);
25425 /* Support Functions */
25427 /* Return the appropriate prefer_type argument for lookup_name_real based on
25428 tag_type and template_mem_access. */
25430 static inline int
25431 prefer_type_arg (tag_types tag_type, bool template_mem_access = false)
25433 /* DR 141: When looking in the current enclosing context for a template-name
25434 after -> or ., only consider class templates. */
25435 if (template_mem_access)
25436 return 2;
25437 switch (tag_type)
25439 case none_type: return 0; // No preference.
25440 case scope_type: return 1; // Type or namespace.
25441 default: return 2; // Type only.
25445 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
25446 NAME should have one of the representations used for an
25447 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
25448 is returned. If PARSER->SCOPE is a dependent type, then a
25449 SCOPE_REF is returned.
25451 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
25452 returned; the name was already resolved when the TEMPLATE_ID_EXPR
25453 was formed. Abstractly, such entities should not be passed to this
25454 function, because they do not need to be looked up, but it is
25455 simpler to check for this special case here, rather than at the
25456 call-sites.
25458 In cases not explicitly covered above, this function returns a
25459 DECL, OVERLOAD, or baselink representing the result of the lookup.
25460 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
25461 is returned.
25463 If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
25464 (e.g., "struct") that was used. In that case bindings that do not
25465 refer to types are ignored.
25467 If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
25468 ignored.
25470 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
25471 are ignored.
25473 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
25474 types.
25476 If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
25477 TREE_LIST of candidates if name-lookup results in an ambiguity, and
25478 NULL_TREE otherwise. */
25480 static cp_expr
25481 cp_parser_lookup_name (cp_parser *parser, tree name,
25482 enum tag_types tag_type,
25483 bool is_template,
25484 bool is_namespace,
25485 bool check_dependency,
25486 tree *ambiguous_decls,
25487 location_t name_location)
25489 tree decl;
25490 tree object_type = parser->context->object_type;
25492 /* Assume that the lookup will be unambiguous. */
25493 if (ambiguous_decls)
25494 *ambiguous_decls = NULL_TREE;
25496 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
25497 no longer valid. Note that if we are parsing tentatively, and
25498 the parse fails, OBJECT_TYPE will be automatically restored. */
25499 parser->context->object_type = NULL_TREE;
25501 if (name == error_mark_node)
25502 return error_mark_node;
25504 /* A template-id has already been resolved; there is no lookup to
25505 do. */
25506 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
25507 return name;
25508 if (BASELINK_P (name))
25510 gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
25511 == TEMPLATE_ID_EXPR);
25512 return name;
25515 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
25516 it should already have been checked to make sure that the name
25517 used matches the type being destroyed. */
25518 if (TREE_CODE (name) == BIT_NOT_EXPR)
25520 tree type;
25522 /* Figure out to which type this destructor applies. */
25523 if (parser->scope)
25524 type = parser->scope;
25525 else if (object_type)
25526 type = object_type;
25527 else
25528 type = current_class_type;
25529 /* If that's not a class type, there is no destructor. */
25530 if (!type || !CLASS_TYPE_P (type))
25531 return error_mark_node;
25532 if (CLASSTYPE_LAZY_DESTRUCTOR (type))
25533 lazily_declare_fn (sfk_destructor, type);
25534 if (!CLASSTYPE_DESTRUCTORS (type))
25535 return error_mark_node;
25536 /* If it was a class type, return the destructor. */
25537 return CLASSTYPE_DESTRUCTORS (type);
25540 /* By this point, the NAME should be an ordinary identifier. If
25541 the id-expression was a qualified name, the qualifying scope is
25542 stored in PARSER->SCOPE at this point. */
25543 gcc_assert (identifier_p (name));
25545 /* Perform the lookup. */
25546 if (parser->scope)
25548 bool dependent_p;
25550 if (parser->scope == error_mark_node)
25551 return error_mark_node;
25553 /* If the SCOPE is dependent, the lookup must be deferred until
25554 the template is instantiated -- unless we are explicitly
25555 looking up names in uninstantiated templates. Even then, we
25556 cannot look up the name if the scope is not a class type; it
25557 might, for example, be a template type parameter. */
25558 dependent_p = (TYPE_P (parser->scope)
25559 && dependent_scope_p (parser->scope));
25560 if ((check_dependency || !CLASS_TYPE_P (parser->scope))
25561 && dependent_p)
25562 /* Defer lookup. */
25563 decl = error_mark_node;
25564 else
25566 tree pushed_scope = NULL_TREE;
25568 /* If PARSER->SCOPE is a dependent type, then it must be a
25569 class type, and we must not be checking dependencies;
25570 otherwise, we would have processed this lookup above. So
25571 that PARSER->SCOPE is not considered a dependent base by
25572 lookup_member, we must enter the scope here. */
25573 if (dependent_p)
25574 pushed_scope = push_scope (parser->scope);
25576 /* If the PARSER->SCOPE is a template specialization, it
25577 may be instantiated during name lookup. In that case,
25578 errors may be issued. Even if we rollback the current
25579 tentative parse, those errors are valid. */
25580 decl = lookup_qualified_name (parser->scope, name,
25581 prefer_type_arg (tag_type),
25582 /*complain=*/true);
25584 /* 3.4.3.1: In a lookup in which the constructor is an acceptable
25585 lookup result and the nested-name-specifier nominates a class C:
25586 * if the name specified after the nested-name-specifier, when
25587 looked up in C, is the injected-class-name of C (Clause 9), or
25588 * if the name specified after the nested-name-specifier is the
25589 same as the identifier or the simple-template-id's template-
25590 name in the last component of the nested-name-specifier,
25591 the name is instead considered to name the constructor of
25592 class C. [ Note: for example, the constructor is not an
25593 acceptable lookup result in an elaborated-type-specifier so
25594 the constructor would not be used in place of the
25595 injected-class-name. --end note ] Such a constructor name
25596 shall be used only in the declarator-id of a declaration that
25597 names a constructor or in a using-declaration. */
25598 if (tag_type == none_type
25599 && DECL_SELF_REFERENCE_P (decl)
25600 && same_type_p (DECL_CONTEXT (decl), parser->scope))
25601 decl = lookup_qualified_name (parser->scope, ctor_identifier,
25602 prefer_type_arg (tag_type),
25603 /*complain=*/true);
25605 /* If we have a single function from a using decl, pull it out. */
25606 if (TREE_CODE (decl) == OVERLOAD
25607 && !really_overloaded_fn (decl))
25608 decl = OVL_FUNCTION (decl);
25610 if (pushed_scope)
25611 pop_scope (pushed_scope);
25614 /* If the scope is a dependent type and either we deferred lookup or
25615 we did lookup but didn't find the name, rememeber the name. */
25616 if (decl == error_mark_node && TYPE_P (parser->scope)
25617 && dependent_type_p (parser->scope))
25619 if (tag_type)
25621 tree type;
25623 /* The resolution to Core Issue 180 says that `struct
25624 A::B' should be considered a type-name, even if `A'
25625 is dependent. */
25626 type = make_typename_type (parser->scope, name, tag_type,
25627 /*complain=*/tf_error);
25628 if (type != error_mark_node)
25629 decl = TYPE_NAME (type);
25631 else if (is_template
25632 && (cp_parser_next_token_ends_template_argument_p (parser)
25633 || cp_lexer_next_token_is (parser->lexer,
25634 CPP_CLOSE_PAREN)))
25635 decl = make_unbound_class_template (parser->scope,
25636 name, NULL_TREE,
25637 /*complain=*/tf_error);
25638 else
25639 decl = build_qualified_name (/*type=*/NULL_TREE,
25640 parser->scope, name,
25641 is_template);
25643 parser->qualifying_scope = parser->scope;
25644 parser->object_scope = NULL_TREE;
25646 else if (object_type)
25648 /* Look up the name in the scope of the OBJECT_TYPE, unless the
25649 OBJECT_TYPE is not a class. */
25650 if (CLASS_TYPE_P (object_type))
25651 /* If the OBJECT_TYPE is a template specialization, it may
25652 be instantiated during name lookup. In that case, errors
25653 may be issued. Even if we rollback the current tentative
25654 parse, those errors are valid. */
25655 decl = lookup_member (object_type,
25656 name,
25657 /*protect=*/0,
25658 prefer_type_arg (tag_type),
25659 tf_warning_or_error);
25660 else
25661 decl = NULL_TREE;
25663 if (!decl)
25664 /* Look it up in the enclosing context. DR 141: When looking for a
25665 template-name after -> or ., only consider class templates. */
25666 decl = lookup_name_real (name, prefer_type_arg (tag_type, is_template),
25667 /*nonclass=*/0,
25668 /*block_p=*/true, is_namespace, 0);
25669 if (object_type == unknown_type_node)
25670 /* The object is type-dependent, so we can't look anything up; we used
25671 this to get the DR 141 behavior. */
25672 object_type = NULL_TREE;
25673 parser->object_scope = object_type;
25674 parser->qualifying_scope = NULL_TREE;
25676 else
25678 decl = lookup_name_real (name, prefer_type_arg (tag_type),
25679 /*nonclass=*/0,
25680 /*block_p=*/true, is_namespace, 0);
25681 parser->qualifying_scope = NULL_TREE;
25682 parser->object_scope = NULL_TREE;
25685 /* If the lookup failed, let our caller know. */
25686 if (!decl || decl == error_mark_node)
25687 return error_mark_node;
25689 /* Pull out the template from an injected-class-name (or multiple). */
25690 if (is_template)
25691 decl = maybe_get_template_decl_from_type_decl (decl);
25693 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
25694 if (TREE_CODE (decl) == TREE_LIST)
25696 if (ambiguous_decls)
25697 *ambiguous_decls = decl;
25698 /* The error message we have to print is too complicated for
25699 cp_parser_error, so we incorporate its actions directly. */
25700 if (!cp_parser_simulate_error (parser))
25702 error_at (name_location, "reference to %qD is ambiguous",
25703 name);
25704 print_candidates (decl);
25706 return error_mark_node;
25709 gcc_assert (DECL_P (decl)
25710 || TREE_CODE (decl) == OVERLOAD
25711 || TREE_CODE (decl) == SCOPE_REF
25712 || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
25713 || BASELINK_P (decl));
25715 /* If we have resolved the name of a member declaration, check to
25716 see if the declaration is accessible. When the name resolves to
25717 set of overloaded functions, accessibility is checked when
25718 overload resolution is done.
25720 During an explicit instantiation, access is not checked at all,
25721 as per [temp.explicit]. */
25722 if (DECL_P (decl))
25723 check_accessibility_of_qualified_id (decl, object_type, parser->scope);
25725 maybe_record_typedef_use (decl);
25727 return cp_expr (decl, name_location);
25730 /* Like cp_parser_lookup_name, but for use in the typical case where
25731 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
25732 IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE. */
25734 static tree
25735 cp_parser_lookup_name_simple (cp_parser* parser, tree name, location_t location)
25737 return cp_parser_lookup_name (parser, name,
25738 none_type,
25739 /*is_template=*/false,
25740 /*is_namespace=*/false,
25741 /*check_dependency=*/true,
25742 /*ambiguous_decls=*/NULL,
25743 location);
25746 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
25747 the current context, return the TYPE_DECL. If TAG_NAME_P is
25748 true, the DECL indicates the class being defined in a class-head,
25749 or declared in an elaborated-type-specifier.
25751 Otherwise, return DECL. */
25753 static tree
25754 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
25756 /* If the TEMPLATE_DECL is being declared as part of a class-head,
25757 the translation from TEMPLATE_DECL to TYPE_DECL occurs:
25759 struct A {
25760 template <typename T> struct B;
25763 template <typename T> struct A::B {};
25765 Similarly, in an elaborated-type-specifier:
25767 namespace N { struct X{}; }
25769 struct A {
25770 template <typename T> friend struct N::X;
25773 However, if the DECL refers to a class type, and we are in
25774 the scope of the class, then the name lookup automatically
25775 finds the TYPE_DECL created by build_self_reference rather
25776 than a TEMPLATE_DECL. For example, in:
25778 template <class T> struct S {
25779 S s;
25782 there is no need to handle such case. */
25784 if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
25785 return DECL_TEMPLATE_RESULT (decl);
25787 return decl;
25790 /* If too many, or too few, template-parameter lists apply to the
25791 declarator, issue an error message. Returns TRUE if all went well,
25792 and FALSE otherwise. */
25794 static bool
25795 cp_parser_check_declarator_template_parameters (cp_parser* parser,
25796 cp_declarator *declarator,
25797 location_t declarator_location)
25799 switch (declarator->kind)
25801 case cdk_id:
25803 unsigned num_templates = 0;
25804 tree scope = declarator->u.id.qualifying_scope;
25806 if (scope)
25807 num_templates = num_template_headers_for_class (scope);
25808 else if (TREE_CODE (declarator->u.id.unqualified_name)
25809 == TEMPLATE_ID_EXPR)
25810 /* If the DECLARATOR has the form `X<y>' then it uses one
25811 additional level of template parameters. */
25812 ++num_templates;
25814 return cp_parser_check_template_parameters
25815 (parser, num_templates, declarator_location, declarator);
25818 case cdk_function:
25819 case cdk_array:
25820 case cdk_pointer:
25821 case cdk_reference:
25822 case cdk_ptrmem:
25823 return (cp_parser_check_declarator_template_parameters
25824 (parser, declarator->declarator, declarator_location));
25826 case cdk_decomp:
25827 case cdk_error:
25828 return true;
25830 default:
25831 gcc_unreachable ();
25833 return false;
25836 /* NUM_TEMPLATES were used in the current declaration. If that is
25837 invalid, return FALSE and issue an error messages. Otherwise,
25838 return TRUE. If DECLARATOR is non-NULL, then we are checking a
25839 declarator and we can print more accurate diagnostics. */
25841 static bool
25842 cp_parser_check_template_parameters (cp_parser* parser,
25843 unsigned num_templates,
25844 location_t location,
25845 cp_declarator *declarator)
25847 /* If there are the same number of template classes and parameter
25848 lists, that's OK. */
25849 if (parser->num_template_parameter_lists == num_templates)
25850 return true;
25851 /* If there are more, but only one more, then we are referring to a
25852 member template. That's OK too. */
25853 if (parser->num_template_parameter_lists == num_templates + 1)
25854 return true;
25855 /* If there are more template classes than parameter lists, we have
25856 something like:
25858 template <class T> void S<T>::R<T>::f (); */
25859 if (parser->num_template_parameter_lists < num_templates)
25861 if (declarator && !current_function_decl)
25862 error_at (location, "specializing member %<%T::%E%> "
25863 "requires %<template<>%> syntax",
25864 declarator->u.id.qualifying_scope,
25865 declarator->u.id.unqualified_name);
25866 else if (declarator)
25867 error_at (location, "invalid declaration of %<%T::%E%>",
25868 declarator->u.id.qualifying_scope,
25869 declarator->u.id.unqualified_name);
25870 else
25871 error_at (location, "too few template-parameter-lists");
25872 return false;
25874 /* Otherwise, there are too many template parameter lists. We have
25875 something like:
25877 template <class T> template <class U> void S::f(); */
25878 error_at (location, "too many template-parameter-lists");
25879 return false;
25882 /* Parse an optional `::' token indicating that the following name is
25883 from the global namespace. If so, PARSER->SCOPE is set to the
25884 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
25885 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
25886 Returns the new value of PARSER->SCOPE, if the `::' token is
25887 present, and NULL_TREE otherwise. */
25889 static tree
25890 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
25892 cp_token *token;
25894 /* Peek at the next token. */
25895 token = cp_lexer_peek_token (parser->lexer);
25896 /* If we're looking at a `::' token then we're starting from the
25897 global namespace, not our current location. */
25898 if (token->type == CPP_SCOPE)
25900 /* Consume the `::' token. */
25901 cp_lexer_consume_token (parser->lexer);
25902 /* Set the SCOPE so that we know where to start the lookup. */
25903 parser->scope = global_namespace;
25904 parser->qualifying_scope = global_namespace;
25905 parser->object_scope = NULL_TREE;
25907 return parser->scope;
25909 else if (!current_scope_valid_p)
25911 parser->scope = NULL_TREE;
25912 parser->qualifying_scope = NULL_TREE;
25913 parser->object_scope = NULL_TREE;
25916 return NULL_TREE;
25919 /* Returns TRUE if the upcoming token sequence is the start of a
25920 constructor declarator or C++17 deduction guide. If FRIEND_P is true, the
25921 declarator is preceded by the `friend' specifier. */
25923 static bool
25924 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
25926 bool constructor_p;
25927 bool outside_class_specifier_p;
25928 tree nested_name_specifier;
25929 cp_token *next_token;
25931 /* The common case is that this is not a constructor declarator, so
25932 try to avoid doing lots of work if at all possible. It's not
25933 valid declare a constructor at function scope. */
25934 if (parser->in_function_body)
25935 return false;
25936 /* And only certain tokens can begin a constructor declarator. */
25937 next_token = cp_lexer_peek_token (parser->lexer);
25938 if (next_token->type != CPP_NAME
25939 && next_token->type != CPP_SCOPE
25940 && next_token->type != CPP_NESTED_NAME_SPECIFIER
25941 && next_token->type != CPP_TEMPLATE_ID)
25942 return false;
25944 /* Parse tentatively; we are going to roll back all of the tokens
25945 consumed here. */
25946 cp_parser_parse_tentatively (parser);
25947 /* Assume that we are looking at a constructor declarator. */
25948 constructor_p = true;
25950 /* Look for the optional `::' operator. */
25951 cp_parser_global_scope_opt (parser,
25952 /*current_scope_valid_p=*/false);
25953 /* Look for the nested-name-specifier. */
25954 nested_name_specifier
25955 = (cp_parser_nested_name_specifier_opt (parser,
25956 /*typename_keyword_p=*/false,
25957 /*check_dependency_p=*/false,
25958 /*type_p=*/false,
25959 /*is_declaration=*/false));
25961 outside_class_specifier_p = (!at_class_scope_p ()
25962 || !TYPE_BEING_DEFINED (current_class_type)
25963 || friend_p);
25965 /* Outside of a class-specifier, there must be a
25966 nested-name-specifier. Except in C++17 mode, where we
25967 might be declaring a guiding declaration. */
25968 if (!nested_name_specifier && outside_class_specifier_p
25969 && cxx_dialect < cxx1z)
25970 constructor_p = false;
25971 else if (nested_name_specifier == error_mark_node)
25972 constructor_p = false;
25974 /* If we have a class scope, this is easy; DR 147 says that S::S always
25975 names the constructor, and no other qualified name could. */
25976 if (constructor_p && nested_name_specifier
25977 && CLASS_TYPE_P (nested_name_specifier))
25979 tree id = cp_parser_unqualified_id (parser,
25980 /*template_keyword_p=*/false,
25981 /*check_dependency_p=*/false,
25982 /*declarator_p=*/true,
25983 /*optional_p=*/false);
25984 if (is_overloaded_fn (id))
25985 id = DECL_NAME (get_first_fn (id));
25986 if (!constructor_name_p (id, nested_name_specifier))
25987 constructor_p = false;
25989 /* If we still think that this might be a constructor-declarator,
25990 look for a class-name. */
25991 else if (constructor_p)
25993 /* If we have:
25995 template <typename T> struct S {
25996 S();
25999 we must recognize that the nested `S' names a class. */
26000 if (cxx_dialect >= cxx1z)
26001 cp_parser_parse_tentatively (parser);
26003 tree type_decl;
26004 type_decl = cp_parser_class_name (parser,
26005 /*typename_keyword_p=*/false,
26006 /*template_keyword_p=*/false,
26007 none_type,
26008 /*check_dependency_p=*/false,
26009 /*class_head_p=*/false,
26010 /*is_declaration=*/false);
26012 if (cxx_dialect >= cxx1z
26013 && !cp_parser_parse_definitely (parser))
26015 type_decl = NULL_TREE;
26016 tree tmpl = cp_parser_template_name (parser,
26017 /*template_keyword*/false,
26018 /*check_dependency_p*/false,
26019 /*is_declaration*/false,
26020 none_type,
26021 /*is_identifier*/NULL);
26022 if (DECL_CLASS_TEMPLATE_P (tmpl)
26023 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl))
26024 /* It's a deduction guide, return true. */;
26025 else
26026 cp_parser_simulate_error (parser);
26029 /* If there was no class-name, then this is not a constructor.
26030 Otherwise, if we are in a class-specifier and we aren't
26031 handling a friend declaration, check that its type matches
26032 current_class_type (c++/38313). Note: error_mark_node
26033 is left alone for error recovery purposes. */
26034 constructor_p = (!cp_parser_error_occurred (parser)
26035 && (outside_class_specifier_p
26036 || type_decl == NULL_TREE
26037 || type_decl == error_mark_node
26038 || same_type_p (current_class_type,
26039 TREE_TYPE (type_decl))));
26041 /* If we're still considering a constructor, we have to see a `(',
26042 to begin the parameter-declaration-clause, followed by either a
26043 `)', an `...', or a decl-specifier. We need to check for a
26044 type-specifier to avoid being fooled into thinking that:
26046 S (f) (int);
26048 is a constructor. (It is actually a function named `f' that
26049 takes one parameter (of type `int') and returns a value of type
26050 `S'. */
26051 if (constructor_p
26052 && !cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
26053 constructor_p = false;
26055 if (constructor_p
26056 && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
26057 && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
26058 /* A parameter declaration begins with a decl-specifier,
26059 which is either the "attribute" keyword, a storage class
26060 specifier, or (usually) a type-specifier. */
26061 && !cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer))
26063 tree type;
26064 tree pushed_scope = NULL_TREE;
26065 unsigned saved_num_template_parameter_lists;
26067 /* Names appearing in the type-specifier should be looked up
26068 in the scope of the class. */
26069 if (current_class_type)
26070 type = NULL_TREE;
26071 else if (type_decl)
26073 type = TREE_TYPE (type_decl);
26074 if (TREE_CODE (type) == TYPENAME_TYPE)
26076 type = resolve_typename_type (type,
26077 /*only_current_p=*/false);
26078 if (TREE_CODE (type) == TYPENAME_TYPE)
26080 cp_parser_abort_tentative_parse (parser);
26081 return false;
26084 pushed_scope = push_scope (type);
26087 /* Inside the constructor parameter list, surrounding
26088 template-parameter-lists do not apply. */
26089 saved_num_template_parameter_lists
26090 = parser->num_template_parameter_lists;
26091 parser->num_template_parameter_lists = 0;
26093 /* Look for the type-specifier. */
26094 cp_parser_type_specifier (parser,
26095 CP_PARSER_FLAGS_NONE,
26096 /*decl_specs=*/NULL,
26097 /*is_declarator=*/true,
26098 /*declares_class_or_enum=*/NULL,
26099 /*is_cv_qualifier=*/NULL);
26101 parser->num_template_parameter_lists
26102 = saved_num_template_parameter_lists;
26104 /* Leave the scope of the class. */
26105 if (pushed_scope)
26106 pop_scope (pushed_scope);
26108 constructor_p = !cp_parser_error_occurred (parser);
26112 /* We did not really want to consume any tokens. */
26113 cp_parser_abort_tentative_parse (parser);
26115 return constructor_p;
26118 /* Parse the definition of the function given by the DECL_SPECIFIERS,
26119 ATTRIBUTES, and DECLARATOR. The access checks have been deferred;
26120 they must be performed once we are in the scope of the function.
26122 Returns the function defined. */
26124 static tree
26125 cp_parser_function_definition_from_specifiers_and_declarator
26126 (cp_parser* parser,
26127 cp_decl_specifier_seq *decl_specifiers,
26128 tree attributes,
26129 const cp_declarator *declarator)
26131 tree fn;
26132 bool success_p;
26134 /* Begin the function-definition. */
26135 success_p = start_function (decl_specifiers, declarator, attributes);
26137 /* The things we're about to see are not directly qualified by any
26138 template headers we've seen thus far. */
26139 reset_specialization ();
26141 /* If there were names looked up in the decl-specifier-seq that we
26142 did not check, check them now. We must wait until we are in the
26143 scope of the function to perform the checks, since the function
26144 might be a friend. */
26145 perform_deferred_access_checks (tf_warning_or_error);
26147 if (success_p)
26149 cp_finalize_omp_declare_simd (parser, current_function_decl);
26150 parser->omp_declare_simd = NULL;
26151 cp_finalize_oacc_routine (parser, current_function_decl, true);
26152 parser->oacc_routine = NULL;
26155 if (!success_p)
26157 /* Skip the entire function. */
26158 cp_parser_skip_to_end_of_block_or_statement (parser);
26159 fn = error_mark_node;
26161 else if (DECL_INITIAL (current_function_decl) != error_mark_node)
26163 /* Seen already, skip it. An error message has already been output. */
26164 cp_parser_skip_to_end_of_block_or_statement (parser);
26165 fn = current_function_decl;
26166 current_function_decl = NULL_TREE;
26167 /* If this is a function from a class, pop the nested class. */
26168 if (current_class_name)
26169 pop_nested_class ();
26171 else
26173 timevar_id_t tv;
26174 if (DECL_DECLARED_INLINE_P (current_function_decl))
26175 tv = TV_PARSE_INLINE;
26176 else
26177 tv = TV_PARSE_FUNC;
26178 timevar_push (tv);
26179 fn = cp_parser_function_definition_after_declarator (parser,
26180 /*inline_p=*/false);
26181 timevar_pop (tv);
26184 return fn;
26187 /* Parse the part of a function-definition that follows the
26188 declarator. INLINE_P is TRUE iff this function is an inline
26189 function defined within a class-specifier.
26191 Returns the function defined. */
26193 static tree
26194 cp_parser_function_definition_after_declarator (cp_parser* parser,
26195 bool inline_p)
26197 tree fn;
26198 bool ctor_initializer_p = false;
26199 bool saved_in_unbraced_linkage_specification_p;
26200 bool saved_in_function_body;
26201 unsigned saved_num_template_parameter_lists;
26202 cp_token *token;
26203 bool fully_implicit_function_template_p
26204 = parser->fully_implicit_function_template_p;
26205 parser->fully_implicit_function_template_p = false;
26206 tree implicit_template_parms
26207 = parser->implicit_template_parms;
26208 parser->implicit_template_parms = 0;
26209 cp_binding_level* implicit_template_scope
26210 = parser->implicit_template_scope;
26211 parser->implicit_template_scope = 0;
26213 saved_in_function_body = parser->in_function_body;
26214 parser->in_function_body = true;
26215 /* If the next token is `return', then the code may be trying to
26216 make use of the "named return value" extension that G++ used to
26217 support. */
26218 token = cp_lexer_peek_token (parser->lexer);
26219 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
26221 /* Consume the `return' keyword. */
26222 cp_lexer_consume_token (parser->lexer);
26223 /* Look for the identifier that indicates what value is to be
26224 returned. */
26225 cp_parser_identifier (parser);
26226 /* Issue an error message. */
26227 error_at (token->location,
26228 "named return values are no longer supported");
26229 /* Skip tokens until we reach the start of the function body. */
26230 while (true)
26232 cp_token *token = cp_lexer_peek_token (parser->lexer);
26233 if (token->type == CPP_OPEN_BRACE
26234 || token->type == CPP_EOF
26235 || token->type == CPP_PRAGMA_EOL)
26236 break;
26237 cp_lexer_consume_token (parser->lexer);
26240 /* The `extern' in `extern "C" void f () { ... }' does not apply to
26241 anything declared inside `f'. */
26242 saved_in_unbraced_linkage_specification_p
26243 = parser->in_unbraced_linkage_specification_p;
26244 parser->in_unbraced_linkage_specification_p = false;
26245 /* Inside the function, surrounding template-parameter-lists do not
26246 apply. */
26247 saved_num_template_parameter_lists
26248 = parser->num_template_parameter_lists;
26249 parser->num_template_parameter_lists = 0;
26251 start_lambda_scope (current_function_decl);
26253 /* If the next token is `try', `__transaction_atomic', or
26254 `__transaction_relaxed`, then we are looking at either function-try-block
26255 or function-transaction-block. Note that all of these include the
26256 function-body. */
26257 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRANSACTION_ATOMIC))
26258 ctor_initializer_p = cp_parser_function_transaction (parser,
26259 RID_TRANSACTION_ATOMIC);
26260 else if (cp_lexer_next_token_is_keyword (parser->lexer,
26261 RID_TRANSACTION_RELAXED))
26262 ctor_initializer_p = cp_parser_function_transaction (parser,
26263 RID_TRANSACTION_RELAXED);
26264 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
26265 ctor_initializer_p = cp_parser_function_try_block (parser);
26266 else
26267 ctor_initializer_p = cp_parser_ctor_initializer_opt_and_function_body
26268 (parser, /*in_function_try_block=*/false);
26270 finish_lambda_scope ();
26272 /* Finish the function. */
26273 fn = finish_function ((ctor_initializer_p ? 1 : 0) |
26274 (inline_p ? 2 : 0));
26275 /* Generate code for it, if necessary. */
26276 expand_or_defer_fn (fn);
26277 /* Restore the saved values. */
26278 parser->in_unbraced_linkage_specification_p
26279 = saved_in_unbraced_linkage_specification_p;
26280 parser->num_template_parameter_lists
26281 = saved_num_template_parameter_lists;
26282 parser->in_function_body = saved_in_function_body;
26284 parser->fully_implicit_function_template_p
26285 = fully_implicit_function_template_p;
26286 parser->implicit_template_parms
26287 = implicit_template_parms;
26288 parser->implicit_template_scope
26289 = implicit_template_scope;
26291 if (parser->fully_implicit_function_template_p)
26292 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
26294 return fn;
26297 /* Parse a template-declaration body (following argument list). */
26299 static void
26300 cp_parser_template_declaration_after_parameters (cp_parser* parser,
26301 tree parameter_list,
26302 bool member_p)
26304 tree decl = NULL_TREE;
26305 bool friend_p = false;
26307 /* We just processed one more parameter list. */
26308 ++parser->num_template_parameter_lists;
26310 /* Get the deferred access checks from the parameter list. These
26311 will be checked once we know what is being declared, as for a
26312 member template the checks must be performed in the scope of the
26313 class containing the member. */
26314 vec<deferred_access_check, va_gc> *checks = get_deferred_access_checks ();
26316 /* Tentatively parse for a new template parameter list, which can either be
26317 the template keyword or a template introduction. */
26318 if (cp_parser_template_declaration_after_export (parser, member_p))
26319 /* OK */;
26320 else if (cxx_dialect >= cxx11
26321 && cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
26322 decl = cp_parser_alias_declaration (parser);
26323 else
26325 /* There are no access checks when parsing a template, as we do not
26326 know if a specialization will be a friend. */
26327 push_deferring_access_checks (dk_no_check);
26328 cp_token *token = cp_lexer_peek_token (parser->lexer);
26329 decl = cp_parser_single_declaration (parser,
26330 checks,
26331 member_p,
26332 /*explicit_specialization_p=*/false,
26333 &friend_p);
26334 pop_deferring_access_checks ();
26336 /* If this is a member template declaration, let the front
26337 end know. */
26338 if (member_p && !friend_p && decl)
26340 if (TREE_CODE (decl) == TYPE_DECL)
26341 cp_parser_check_access_in_redeclaration (decl, token->location);
26343 decl = finish_member_template_decl (decl);
26345 else if (friend_p && decl
26346 && DECL_DECLARES_TYPE_P (decl))
26347 make_friend_class (current_class_type, TREE_TYPE (decl),
26348 /*complain=*/true);
26350 /* We are done with the current parameter list. */
26351 --parser->num_template_parameter_lists;
26353 pop_deferring_access_checks ();
26355 /* Finish up. */
26356 finish_template_decl (parameter_list);
26358 /* Check the template arguments for a literal operator template. */
26359 if (decl
26360 && DECL_DECLARES_FUNCTION_P (decl)
26361 && UDLIT_OPER_P (DECL_NAME (decl)))
26363 bool ok = true;
26364 if (parameter_list == NULL_TREE)
26365 ok = false;
26366 else
26368 int num_parms = TREE_VEC_LENGTH (parameter_list);
26369 if (num_parms == 1)
26371 tree parm_list = TREE_VEC_ELT (parameter_list, 0);
26372 tree parm = INNERMOST_TEMPLATE_PARMS (parm_list);
26373 if (TREE_TYPE (parm) != char_type_node
26374 || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
26375 ok = false;
26377 else if (num_parms == 2 && cxx_dialect >= cxx14)
26379 tree parm_type = TREE_VEC_ELT (parameter_list, 0);
26380 tree type = INNERMOST_TEMPLATE_PARMS (parm_type);
26381 tree parm_list = TREE_VEC_ELT (parameter_list, 1);
26382 tree parm = INNERMOST_TEMPLATE_PARMS (parm_list);
26383 if (parm == error_mark_node
26384 || TREE_TYPE (parm) != TREE_TYPE (type)
26385 || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
26386 ok = false;
26388 else
26389 ok = false;
26391 if (!ok)
26393 if (cxx_dialect >= cxx14)
26394 error ("literal operator template %qD has invalid parameter list."
26395 " Expected non-type template argument pack <char...>"
26396 " or <typename CharT, CharT...>",
26397 decl);
26398 else
26399 error ("literal operator template %qD has invalid parameter list."
26400 " Expected non-type template argument pack <char...>",
26401 decl);
26405 /* Register member declarations. */
26406 if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
26407 finish_member_declaration (decl);
26408 /* If DECL is a function template, we must return to parse it later.
26409 (Even though there is no definition, there might be default
26410 arguments that need handling.) */
26411 if (member_p && decl
26412 && DECL_DECLARES_FUNCTION_P (decl))
26413 vec_safe_push (unparsed_funs_with_definitions, decl);
26416 /* Parse a template introduction header for a template-declaration. Returns
26417 false if tentative parse fails. */
26419 static bool
26420 cp_parser_template_introduction (cp_parser* parser, bool member_p)
26422 cp_parser_parse_tentatively (parser);
26424 tree saved_scope = parser->scope;
26425 tree saved_object_scope = parser->object_scope;
26426 tree saved_qualifying_scope = parser->qualifying_scope;
26428 /* Look for the optional `::' operator. */
26429 cp_parser_global_scope_opt (parser,
26430 /*current_scope_valid_p=*/false);
26431 /* Look for the nested-name-specifier. */
26432 cp_parser_nested_name_specifier_opt (parser,
26433 /*typename_keyword_p=*/false,
26434 /*check_dependency_p=*/true,
26435 /*type_p=*/false,
26436 /*is_declaration=*/false);
26438 cp_token *token = cp_lexer_peek_token (parser->lexer);
26439 tree concept_name = cp_parser_identifier (parser);
26441 /* Look up the concept for which we will be matching
26442 template parameters. */
26443 tree tmpl_decl = cp_parser_lookup_name_simple (parser, concept_name,
26444 token->location);
26445 parser->scope = saved_scope;
26446 parser->object_scope = saved_object_scope;
26447 parser->qualifying_scope = saved_qualifying_scope;
26449 if (concept_name == error_mark_node)
26450 cp_parser_simulate_error (parser);
26452 /* Look for opening brace for introduction. */
26453 cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
26455 if (!cp_parser_parse_definitely (parser))
26456 return false;
26458 push_deferring_access_checks (dk_deferred);
26460 /* Build vector of placeholder parameters and grab
26461 matching identifiers. */
26462 tree introduction_list = cp_parser_introduction_list (parser);
26464 /* The introduction-list shall not be empty. */
26465 int nargs = TREE_VEC_LENGTH (introduction_list);
26466 if (nargs == 0)
26468 error ("empty introduction-list");
26469 return true;
26472 /* Look for closing brace for introduction. */
26473 if (!cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE))
26474 return true;
26476 if (tmpl_decl == error_mark_node)
26478 cp_parser_name_lookup_error (parser, concept_name, tmpl_decl, NLE_NULL,
26479 token->location);
26480 return true;
26483 /* Build and associate the constraint. */
26484 tree parms = finish_template_introduction (tmpl_decl, introduction_list);
26485 if (parms && parms != error_mark_node)
26487 cp_parser_template_declaration_after_parameters (parser, parms,
26488 member_p);
26489 return true;
26492 error_at (token->location, "no matching concept for template-introduction");
26493 return true;
26496 /* Parse a normal template-declaration following the template keyword. */
26498 static void
26499 cp_parser_explicit_template_declaration (cp_parser* parser, bool member_p)
26501 tree parameter_list;
26502 bool need_lang_pop;
26503 location_t location = input_location;
26505 /* Look for the `<' token. */
26506 if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
26507 return;
26508 if (at_class_scope_p () && current_function_decl)
26510 /* 14.5.2.2 [temp.mem]
26512 A local class shall not have member templates. */
26513 error_at (location,
26514 "invalid declaration of member template in local class");
26515 cp_parser_skip_to_end_of_block_or_statement (parser);
26516 return;
26518 /* [temp]
26520 A template ... shall not have C linkage. */
26521 if (current_lang_name == lang_name_c)
26523 error_at (location, "template with C linkage");
26524 /* Give it C++ linkage to avoid confusing other parts of the
26525 front end. */
26526 push_lang_context (lang_name_cplusplus);
26527 need_lang_pop = true;
26529 else
26530 need_lang_pop = false;
26532 /* We cannot perform access checks on the template parameter
26533 declarations until we know what is being declared, just as we
26534 cannot check the decl-specifier list. */
26535 push_deferring_access_checks (dk_deferred);
26537 /* If the next token is `>', then we have an invalid
26538 specialization. Rather than complain about an invalid template
26539 parameter, issue an error message here. */
26540 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
26542 cp_parser_error (parser, "invalid explicit specialization");
26543 begin_specialization ();
26544 parameter_list = NULL_TREE;
26546 else
26548 /* Parse the template parameters. */
26549 parameter_list = cp_parser_template_parameter_list (parser);
26552 /* Look for the `>'. */
26553 cp_parser_skip_to_end_of_template_parameter_list (parser);
26555 /* Manage template requirements */
26556 if (flag_concepts)
26558 tree reqs = get_shorthand_constraints (current_template_parms);
26559 if (tree r = cp_parser_requires_clause_opt (parser))
26560 reqs = conjoin_constraints (reqs, normalize_expression (r));
26561 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) = reqs;
26564 cp_parser_template_declaration_after_parameters (parser, parameter_list,
26565 member_p);
26567 /* For the erroneous case of a template with C linkage, we pushed an
26568 implicit C++ linkage scope; exit that scope now. */
26569 if (need_lang_pop)
26570 pop_lang_context ();
26573 /* Parse a template-declaration, assuming that the `export' (and
26574 `extern') keywords, if present, has already been scanned. MEMBER_P
26575 is as for cp_parser_template_declaration. */
26577 static bool
26578 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
26580 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
26582 cp_lexer_consume_token (parser->lexer);
26583 cp_parser_explicit_template_declaration (parser, member_p);
26584 return true;
26586 else if (flag_concepts)
26587 return cp_parser_template_introduction (parser, member_p);
26589 return false;
26592 /* Perform the deferred access checks from a template-parameter-list.
26593 CHECKS is a TREE_LIST of access checks, as returned by
26594 get_deferred_access_checks. */
26596 static void
26597 cp_parser_perform_template_parameter_access_checks (vec<deferred_access_check, va_gc> *checks)
26599 ++processing_template_parmlist;
26600 perform_access_checks (checks, tf_warning_or_error);
26601 --processing_template_parmlist;
26604 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
26605 `function-definition' sequence that follows a template header.
26606 If MEMBER_P is true, this declaration appears in a class scope.
26608 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
26609 *FRIEND_P is set to TRUE iff the declaration is a friend. */
26611 static tree
26612 cp_parser_single_declaration (cp_parser* parser,
26613 vec<deferred_access_check, va_gc> *checks,
26614 bool member_p,
26615 bool explicit_specialization_p,
26616 bool* friend_p)
26618 int declares_class_or_enum;
26619 tree decl = NULL_TREE;
26620 cp_decl_specifier_seq decl_specifiers;
26621 bool function_definition_p = false;
26622 cp_token *decl_spec_token_start;
26624 /* This function is only used when processing a template
26625 declaration. */
26626 gcc_assert (innermost_scope_kind () == sk_template_parms
26627 || innermost_scope_kind () == sk_template_spec);
26629 /* Defer access checks until we know what is being declared. */
26630 push_deferring_access_checks (dk_deferred);
26632 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
26633 alternative. */
26634 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
26635 cp_parser_decl_specifier_seq (parser,
26636 CP_PARSER_FLAGS_OPTIONAL,
26637 &decl_specifiers,
26638 &declares_class_or_enum);
26639 if (friend_p)
26640 *friend_p = cp_parser_friend_p (&decl_specifiers);
26642 /* There are no template typedefs. */
26643 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_typedef))
26645 error_at (decl_spec_token_start->location,
26646 "template declaration of %<typedef%>");
26647 decl = error_mark_node;
26650 /* Gather up the access checks that occurred the
26651 decl-specifier-seq. */
26652 stop_deferring_access_checks ();
26654 /* Check for the declaration of a template class. */
26655 if (declares_class_or_enum)
26657 if (cp_parser_declares_only_class_p (parser)
26658 || (declares_class_or_enum & 2))
26660 // If this is a declaration, but not a definition, associate
26661 // any constraints with the type declaration. Constraints
26662 // are associated with definitions in cp_parser_class_specifier.
26663 if (declares_class_or_enum == 1)
26664 associate_classtype_constraints (decl_specifiers.type);
26666 decl = shadow_tag (&decl_specifiers);
26668 /* In this case:
26670 struct C {
26671 friend template <typename T> struct A<T>::B;
26674 A<T>::B will be represented by a TYPENAME_TYPE, and
26675 therefore not recognized by shadow_tag. */
26676 if (friend_p && *friend_p
26677 && !decl
26678 && decl_specifiers.type
26679 && TYPE_P (decl_specifiers.type))
26680 decl = decl_specifiers.type;
26682 if (decl && decl != error_mark_node)
26683 decl = TYPE_NAME (decl);
26684 else
26685 decl = error_mark_node;
26687 /* Perform access checks for template parameters. */
26688 cp_parser_perform_template_parameter_access_checks (checks);
26690 /* Give a helpful diagnostic for
26691 template <class T> struct A { } a;
26692 if we aren't already recovering from an error. */
26693 if (!cp_parser_declares_only_class_p (parser)
26694 && !seen_error ())
26696 error_at (cp_lexer_peek_token (parser->lexer)->location,
26697 "a class template declaration must not declare "
26698 "anything else");
26699 cp_parser_skip_to_end_of_block_or_statement (parser);
26700 goto out;
26705 /* Complain about missing 'typename' or other invalid type names. */
26706 if (!decl_specifiers.any_type_specifiers_p
26707 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
26709 /* cp_parser_parse_and_diagnose_invalid_type_name calls
26710 cp_parser_skip_to_end_of_block_or_statement, so don't try to parse
26711 the rest of this declaration. */
26712 decl = error_mark_node;
26713 goto out;
26716 /* If it's not a template class, try for a template function. If
26717 the next token is a `;', then this declaration does not declare
26718 anything. But, if there were errors in the decl-specifiers, then
26719 the error might well have come from an attempted class-specifier.
26720 In that case, there's no need to warn about a missing declarator. */
26721 if (!decl
26722 && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
26723 || decl_specifiers.type != error_mark_node))
26725 decl = cp_parser_init_declarator (parser,
26726 &decl_specifiers,
26727 checks,
26728 /*function_definition_allowed_p=*/true,
26729 member_p,
26730 declares_class_or_enum,
26731 &function_definition_p,
26732 NULL, NULL, NULL);
26734 /* 7.1.1-1 [dcl.stc]
26736 A storage-class-specifier shall not be specified in an explicit
26737 specialization... */
26738 if (decl
26739 && explicit_specialization_p
26740 && decl_specifiers.storage_class != sc_none)
26742 error_at (decl_spec_token_start->location,
26743 "explicit template specialization cannot have a storage class");
26744 decl = error_mark_node;
26747 if (decl && VAR_P (decl))
26748 check_template_variable (decl);
26751 /* Look for a trailing `;' after the declaration. */
26752 if (!function_definition_p
26753 && (decl == error_mark_node
26754 || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON)))
26755 cp_parser_skip_to_end_of_block_or_statement (parser);
26757 out:
26758 pop_deferring_access_checks ();
26760 /* Clear any current qualification; whatever comes next is the start
26761 of something new. */
26762 parser->scope = NULL_TREE;
26763 parser->qualifying_scope = NULL_TREE;
26764 parser->object_scope = NULL_TREE;
26766 return decl;
26769 /* Parse a cast-expression that is not the operand of a unary "&". */
26771 static cp_expr
26772 cp_parser_simple_cast_expression (cp_parser *parser)
26774 return cp_parser_cast_expression (parser, /*address_p=*/false,
26775 /*cast_p=*/false, /*decltype*/false, NULL);
26778 /* Parse a functional cast to TYPE. Returns an expression
26779 representing the cast. */
26781 static cp_expr
26782 cp_parser_functional_cast (cp_parser* parser, tree type)
26784 vec<tree, va_gc> *vec;
26785 tree expression_list;
26786 cp_expr cast;
26787 bool nonconst_p;
26789 location_t start_loc = input_location;
26791 if (!type)
26792 type = error_mark_node;
26794 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
26796 cp_lexer_set_source_position (parser->lexer);
26797 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
26798 expression_list = cp_parser_braced_list (parser, &nonconst_p);
26799 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
26800 if (TREE_CODE (type) == TYPE_DECL)
26801 type = TREE_TYPE (type);
26803 cast = finish_compound_literal (type, expression_list,
26804 tf_warning_or_error);
26805 /* Create a location of the form:
26806 type_name{i, f}
26807 ^~~~~~~~~~~~~~~
26808 with caret == start at the start of the type name,
26809 finishing at the closing brace. */
26810 location_t finish_loc
26811 = get_finish (cp_lexer_previous_token (parser->lexer)->location);
26812 location_t combined_loc = make_location (start_loc, start_loc,
26813 finish_loc);
26814 cast.set_location (combined_loc);
26815 return cast;
26819 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
26820 /*cast_p=*/true,
26821 /*allow_expansion_p=*/true,
26822 /*non_constant_p=*/NULL);
26823 if (vec == NULL)
26824 expression_list = error_mark_node;
26825 else
26827 expression_list = build_tree_list_vec (vec);
26828 release_tree_vector (vec);
26831 cast = build_functional_cast (type, expression_list,
26832 tf_warning_or_error);
26833 /* [expr.const]/1: In an integral constant expression "only type
26834 conversions to integral or enumeration type can be used". */
26835 if (TREE_CODE (type) == TYPE_DECL)
26836 type = TREE_TYPE (type);
26837 if (cast != error_mark_node
26838 && !cast_valid_in_integral_constant_expression_p (type)
26839 && cp_parser_non_integral_constant_expression (parser,
26840 NIC_CONSTRUCTOR))
26841 return error_mark_node;
26843 /* Create a location of the form:
26844 float(i)
26845 ^~~~~~~~
26846 with caret == start at the start of the type name,
26847 finishing at the closing paren. */
26848 location_t finish_loc
26849 = get_finish (cp_lexer_previous_token (parser->lexer)->location);
26850 location_t combined_loc = make_location (start_loc, start_loc, finish_loc);
26851 cast.set_location (combined_loc);
26852 return cast;
26855 /* Save the tokens that make up the body of a member function defined
26856 in a class-specifier. The DECL_SPECIFIERS and DECLARATOR have
26857 already been parsed. The ATTRIBUTES are any GNU "__attribute__"
26858 specifiers applied to the declaration. Returns the FUNCTION_DECL
26859 for the member function. */
26861 static tree
26862 cp_parser_save_member_function_body (cp_parser* parser,
26863 cp_decl_specifier_seq *decl_specifiers,
26864 cp_declarator *declarator,
26865 tree attributes)
26867 cp_token *first;
26868 cp_token *last;
26869 tree fn;
26870 bool function_try_block = false;
26872 /* Create the FUNCTION_DECL. */
26873 fn = grokmethod (decl_specifiers, declarator, attributes);
26874 cp_finalize_omp_declare_simd (parser, fn);
26875 cp_finalize_oacc_routine (parser, fn, true);
26876 /* If something went badly wrong, bail out now. */
26877 if (fn == error_mark_node)
26879 /* If there's a function-body, skip it. */
26880 if (cp_parser_token_starts_function_definition_p
26881 (cp_lexer_peek_token (parser->lexer)))
26882 cp_parser_skip_to_end_of_block_or_statement (parser);
26883 return error_mark_node;
26886 /* Remember it, if there default args to post process. */
26887 cp_parser_save_default_args (parser, fn);
26889 /* Save away the tokens that make up the body of the
26890 function. */
26891 first = parser->lexer->next_token;
26893 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRANSACTION_RELAXED))
26894 cp_lexer_consume_token (parser->lexer);
26895 else if (cp_lexer_next_token_is_keyword (parser->lexer,
26896 RID_TRANSACTION_ATOMIC))
26898 cp_lexer_consume_token (parser->lexer);
26899 /* Match cp_parser_txn_attribute_opt [[ identifier ]]. */
26900 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE)
26901 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_SQUARE)
26902 && (cp_lexer_nth_token_is (parser->lexer, 3, CPP_NAME)
26903 || cp_lexer_nth_token_is (parser->lexer, 3, CPP_KEYWORD))
26904 && cp_lexer_nth_token_is (parser->lexer, 4, CPP_CLOSE_SQUARE)
26905 && cp_lexer_nth_token_is (parser->lexer, 5, CPP_CLOSE_SQUARE))
26907 cp_lexer_consume_token (parser->lexer);
26908 cp_lexer_consume_token (parser->lexer);
26909 cp_lexer_consume_token (parser->lexer);
26910 cp_lexer_consume_token (parser->lexer);
26911 cp_lexer_consume_token (parser->lexer);
26913 else
26914 while (cp_next_tokens_can_be_gnu_attribute_p (parser)
26915 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_PAREN))
26917 cp_lexer_consume_token (parser->lexer);
26918 if (cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0))
26919 break;
26923 /* Handle function try blocks. */
26924 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
26926 cp_lexer_consume_token (parser->lexer);
26927 function_try_block = true;
26929 /* We can have braced-init-list mem-initializers before the fn body. */
26930 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
26932 cp_lexer_consume_token (parser->lexer);
26933 while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
26935 /* cache_group will stop after an un-nested { } pair, too. */
26936 if (cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0))
26937 break;
26939 /* variadic mem-inits have ... after the ')'. */
26940 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
26941 cp_lexer_consume_token (parser->lexer);
26944 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
26945 /* Handle function try blocks. */
26946 if (function_try_block)
26947 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
26948 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
26949 last = parser->lexer->next_token;
26951 /* Save away the inline definition; we will process it when the
26952 class is complete. */
26953 DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
26954 DECL_PENDING_INLINE_P (fn) = 1;
26956 /* We need to know that this was defined in the class, so that
26957 friend templates are handled correctly. */
26958 DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
26960 /* Add FN to the queue of functions to be parsed later. */
26961 vec_safe_push (unparsed_funs_with_definitions, fn);
26963 return fn;
26966 /* Save the tokens that make up the in-class initializer for a non-static
26967 data member. Returns a DEFAULT_ARG. */
26969 static tree
26970 cp_parser_save_nsdmi (cp_parser* parser)
26972 return cp_parser_cache_defarg (parser, /*nsdmi=*/true);
26975 /* Parse a template-argument-list, as well as the trailing ">" (but
26976 not the opening "<"). See cp_parser_template_argument_list for the
26977 return value. */
26979 static tree
26980 cp_parser_enclosed_template_argument_list (cp_parser* parser)
26982 tree arguments;
26983 tree saved_scope;
26984 tree saved_qualifying_scope;
26985 tree saved_object_scope;
26986 bool saved_greater_than_is_operator_p;
26987 int saved_unevaluated_operand;
26988 int saved_inhibit_evaluation_warnings;
26990 /* [temp.names]
26992 When parsing a template-id, the first non-nested `>' is taken as
26993 the end of the template-argument-list rather than a greater-than
26994 operator. */
26995 saved_greater_than_is_operator_p
26996 = parser->greater_than_is_operator_p;
26997 parser->greater_than_is_operator_p = false;
26998 /* Parsing the argument list may modify SCOPE, so we save it
26999 here. */
27000 saved_scope = parser->scope;
27001 saved_qualifying_scope = parser->qualifying_scope;
27002 saved_object_scope = parser->object_scope;
27003 /* We need to evaluate the template arguments, even though this
27004 template-id may be nested within a "sizeof". */
27005 saved_unevaluated_operand = cp_unevaluated_operand;
27006 cp_unevaluated_operand = 0;
27007 saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
27008 c_inhibit_evaluation_warnings = 0;
27009 /* Parse the template-argument-list itself. */
27010 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER)
27011 || cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
27012 arguments = NULL_TREE;
27013 else
27014 arguments = cp_parser_template_argument_list (parser);
27015 /* Look for the `>' that ends the template-argument-list. If we find
27016 a '>>' instead, it's probably just a typo. */
27017 if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
27019 if (cxx_dialect != cxx98)
27021 /* In C++0x, a `>>' in a template argument list or cast
27022 expression is considered to be two separate `>'
27023 tokens. So, change the current token to a `>', but don't
27024 consume it: it will be consumed later when the outer
27025 template argument list (or cast expression) is parsed.
27026 Note that this replacement of `>' for `>>' is necessary
27027 even if we are parsing tentatively: in the tentative
27028 case, after calling
27029 cp_parser_enclosed_template_argument_list we will always
27030 throw away all of the template arguments and the first
27031 closing `>', either because the template argument list
27032 was erroneous or because we are replacing those tokens
27033 with a CPP_TEMPLATE_ID token. The second `>' (which will
27034 not have been thrown away) is needed either to close an
27035 outer template argument list or to complete a new-style
27036 cast. */
27037 cp_token *token = cp_lexer_peek_token (parser->lexer);
27038 token->type = CPP_GREATER;
27040 else if (!saved_greater_than_is_operator_p)
27042 /* If we're in a nested template argument list, the '>>' has
27043 to be a typo for '> >'. We emit the error message, but we
27044 continue parsing and we push a '>' as next token, so that
27045 the argument list will be parsed correctly. Note that the
27046 global source location is still on the token before the
27047 '>>', so we need to say explicitly where we want it. */
27048 cp_token *token = cp_lexer_peek_token (parser->lexer);
27049 gcc_rich_location richloc (token->location);
27050 richloc.add_fixit_replace ("> >");
27051 error_at_rich_loc (&richloc, "%<>>%> should be %<> >%> "
27052 "within a nested template argument list");
27054 token->type = CPP_GREATER;
27056 else
27058 /* If this is not a nested template argument list, the '>>'
27059 is a typo for '>'. Emit an error message and continue.
27060 Same deal about the token location, but here we can get it
27061 right by consuming the '>>' before issuing the diagnostic. */
27062 cp_token *token = cp_lexer_consume_token (parser->lexer);
27063 error_at (token->location,
27064 "spurious %<>>%>, use %<>%> to terminate "
27065 "a template argument list");
27068 else
27069 cp_parser_skip_to_end_of_template_parameter_list (parser);
27070 /* The `>' token might be a greater-than operator again now. */
27071 parser->greater_than_is_operator_p
27072 = saved_greater_than_is_operator_p;
27073 /* Restore the SAVED_SCOPE. */
27074 parser->scope = saved_scope;
27075 parser->qualifying_scope = saved_qualifying_scope;
27076 parser->object_scope = saved_object_scope;
27077 cp_unevaluated_operand = saved_unevaluated_operand;
27078 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
27080 return arguments;
27083 /* MEMBER_FUNCTION is a member function, or a friend. If default
27084 arguments, or the body of the function have not yet been parsed,
27085 parse them now. */
27087 static void
27088 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
27090 timevar_push (TV_PARSE_INMETH);
27091 /* If this member is a template, get the underlying
27092 FUNCTION_DECL. */
27093 if (DECL_FUNCTION_TEMPLATE_P (member_function))
27094 member_function = DECL_TEMPLATE_RESULT (member_function);
27096 /* There should not be any class definitions in progress at this
27097 point; the bodies of members are only parsed outside of all class
27098 definitions. */
27099 gcc_assert (parser->num_classes_being_defined == 0);
27100 /* While we're parsing the member functions we might encounter more
27101 classes. We want to handle them right away, but we don't want
27102 them getting mixed up with functions that are currently in the
27103 queue. */
27104 push_unparsed_function_queues (parser);
27106 /* Make sure that any template parameters are in scope. */
27107 maybe_begin_member_template_processing (member_function);
27109 /* If the body of the function has not yet been parsed, parse it
27110 now. */
27111 if (DECL_PENDING_INLINE_P (member_function))
27113 tree function_scope;
27114 cp_token_cache *tokens;
27116 /* The function is no longer pending; we are processing it. */
27117 tokens = DECL_PENDING_INLINE_INFO (member_function);
27118 DECL_PENDING_INLINE_INFO (member_function) = NULL;
27119 DECL_PENDING_INLINE_P (member_function) = 0;
27121 /* If this is a local class, enter the scope of the containing
27122 function. */
27123 function_scope = current_function_decl;
27124 if (function_scope)
27125 push_function_context ();
27127 /* Push the body of the function onto the lexer stack. */
27128 cp_parser_push_lexer_for_tokens (parser, tokens);
27130 /* Let the front end know that we going to be defining this
27131 function. */
27132 start_preparsed_function (member_function, NULL_TREE,
27133 SF_PRE_PARSED | SF_INCLASS_INLINE);
27135 /* Don't do access checking if it is a templated function. */
27136 if (processing_template_decl)
27137 push_deferring_access_checks (dk_no_check);
27139 /* #pragma omp declare reduction needs special parsing. */
27140 if (DECL_OMP_DECLARE_REDUCTION_P (member_function))
27142 parser->lexer->in_pragma = true;
27143 cp_parser_omp_declare_reduction_exprs (member_function, parser);
27144 finish_function (/*inline*/2);
27145 cp_check_omp_declare_reduction (member_function);
27147 else
27148 /* Now, parse the body of the function. */
27149 cp_parser_function_definition_after_declarator (parser,
27150 /*inline_p=*/true);
27152 if (processing_template_decl)
27153 pop_deferring_access_checks ();
27155 /* Leave the scope of the containing function. */
27156 if (function_scope)
27157 pop_function_context ();
27158 cp_parser_pop_lexer (parser);
27161 /* Remove any template parameters from the symbol table. */
27162 maybe_end_member_template_processing ();
27164 /* Restore the queue. */
27165 pop_unparsed_function_queues (parser);
27166 timevar_pop (TV_PARSE_INMETH);
27169 /* If DECL contains any default args, remember it on the unparsed
27170 functions queue. */
27172 static void
27173 cp_parser_save_default_args (cp_parser* parser, tree decl)
27175 tree probe;
27177 for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
27178 probe;
27179 probe = TREE_CHAIN (probe))
27180 if (TREE_PURPOSE (probe))
27182 cp_default_arg_entry entry = {current_class_type, decl};
27183 vec_safe_push (unparsed_funs_with_default_args, entry);
27184 break;
27188 /* DEFAULT_ARG contains the saved tokens for the initializer of DECL,
27189 which is either a FIELD_DECL or PARM_DECL. Parse it and return
27190 the result. For a PARM_DECL, PARMTYPE is the corresponding type
27191 from the parameter-type-list. */
27193 static tree
27194 cp_parser_late_parse_one_default_arg (cp_parser *parser, tree decl,
27195 tree default_arg, tree parmtype)
27197 cp_token_cache *tokens;
27198 tree parsed_arg;
27199 bool dummy;
27201 if (default_arg == error_mark_node)
27202 return error_mark_node;
27204 /* Push the saved tokens for the default argument onto the parser's
27205 lexer stack. */
27206 tokens = DEFARG_TOKENS (default_arg);
27207 cp_parser_push_lexer_for_tokens (parser, tokens);
27209 start_lambda_scope (decl);
27211 /* Parse the default argument. */
27212 parsed_arg = cp_parser_initializer (parser, &dummy, &dummy);
27213 if (BRACE_ENCLOSED_INITIALIZER_P (parsed_arg))
27214 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
27216 finish_lambda_scope ();
27218 if (parsed_arg == error_mark_node)
27219 cp_parser_skip_to_end_of_statement (parser);
27221 if (!processing_template_decl)
27223 /* In a non-template class, check conversions now. In a template,
27224 we'll wait and instantiate these as needed. */
27225 if (TREE_CODE (decl) == PARM_DECL)
27226 parsed_arg = check_default_argument (parmtype, parsed_arg,
27227 tf_warning_or_error);
27228 else
27229 parsed_arg = digest_nsdmi_init (decl, parsed_arg);
27232 /* If the token stream has not been completely used up, then
27233 there was extra junk after the end of the default
27234 argument. */
27235 if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
27237 if (TREE_CODE (decl) == PARM_DECL)
27238 cp_parser_error (parser, "expected %<,%>");
27239 else
27240 cp_parser_error (parser, "expected %<;%>");
27243 /* Revert to the main lexer. */
27244 cp_parser_pop_lexer (parser);
27246 return parsed_arg;
27249 /* FIELD is a non-static data member with an initializer which we saved for
27250 later; parse it now. */
27252 static void
27253 cp_parser_late_parsing_nsdmi (cp_parser *parser, tree field)
27255 tree def;
27257 maybe_begin_member_template_processing (field);
27259 push_unparsed_function_queues (parser);
27260 def = cp_parser_late_parse_one_default_arg (parser, field,
27261 DECL_INITIAL (field),
27262 NULL_TREE);
27263 pop_unparsed_function_queues (parser);
27265 maybe_end_member_template_processing ();
27267 DECL_INITIAL (field) = def;
27270 /* FN is a FUNCTION_DECL which may contains a parameter with an
27271 unparsed DEFAULT_ARG. Parse the default args now. This function
27272 assumes that the current scope is the scope in which the default
27273 argument should be processed. */
27275 static void
27276 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
27278 bool saved_local_variables_forbidden_p;
27279 tree parm, parmdecl;
27281 /* While we're parsing the default args, we might (due to the
27282 statement expression extension) encounter more classes. We want
27283 to handle them right away, but we don't want them getting mixed
27284 up with default args that are currently in the queue. */
27285 push_unparsed_function_queues (parser);
27287 /* Local variable names (and the `this' keyword) may not appear
27288 in a default argument. */
27289 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
27290 parser->local_variables_forbidden_p = true;
27292 push_defarg_context (fn);
27294 for (parm = TYPE_ARG_TYPES (TREE_TYPE (fn)),
27295 parmdecl = DECL_ARGUMENTS (fn);
27296 parm && parm != void_list_node;
27297 parm = TREE_CHAIN (parm),
27298 parmdecl = DECL_CHAIN (parmdecl))
27300 tree default_arg = TREE_PURPOSE (parm);
27301 tree parsed_arg;
27302 vec<tree, va_gc> *insts;
27303 tree copy;
27304 unsigned ix;
27306 if (!default_arg)
27307 continue;
27309 if (TREE_CODE (default_arg) != DEFAULT_ARG)
27310 /* This can happen for a friend declaration for a function
27311 already declared with default arguments. */
27312 continue;
27314 parsed_arg
27315 = cp_parser_late_parse_one_default_arg (parser, parmdecl,
27316 default_arg,
27317 TREE_VALUE (parm));
27318 if (parsed_arg == error_mark_node)
27320 continue;
27323 TREE_PURPOSE (parm) = parsed_arg;
27325 /* Update any instantiations we've already created. */
27326 for (insts = DEFARG_INSTANTIATIONS (default_arg), ix = 0;
27327 vec_safe_iterate (insts, ix, &copy); ix++)
27328 TREE_PURPOSE (copy) = parsed_arg;
27331 pop_defarg_context ();
27333 /* Make sure no default arg is missing. */
27334 check_default_args (fn);
27336 /* Restore the state of local_variables_forbidden_p. */
27337 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
27339 /* Restore the queue. */
27340 pop_unparsed_function_queues (parser);
27343 /* Subroutine of cp_parser_sizeof_operand, for handling C++11
27345 sizeof ... ( identifier )
27347 where the 'sizeof' token has already been consumed. */
27349 static tree
27350 cp_parser_sizeof_pack (cp_parser *parser)
27352 /* Consume the `...'. */
27353 cp_lexer_consume_token (parser->lexer);
27354 maybe_warn_variadic_templates ();
27356 bool paren = cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN);
27357 if (paren)
27358 cp_lexer_consume_token (parser->lexer);
27359 else
27360 permerror (cp_lexer_peek_token (parser->lexer)->location,
27361 "%<sizeof...%> argument must be surrounded by parentheses");
27363 cp_token *token = cp_lexer_peek_token (parser->lexer);
27364 tree name = cp_parser_identifier (parser);
27365 if (name == error_mark_node)
27366 return error_mark_node;
27367 /* The name is not qualified. */
27368 parser->scope = NULL_TREE;
27369 parser->qualifying_scope = NULL_TREE;
27370 parser->object_scope = NULL_TREE;
27371 tree expr = cp_parser_lookup_name_simple (parser, name, token->location);
27372 if (expr == error_mark_node)
27373 cp_parser_name_lookup_error (parser, name, expr, NLE_NULL,
27374 token->location);
27375 if (TREE_CODE (expr) == TYPE_DECL || TREE_CODE (expr) == TEMPLATE_DECL)
27376 expr = TREE_TYPE (expr);
27377 else if (TREE_CODE (expr) == CONST_DECL)
27378 expr = DECL_INITIAL (expr);
27379 expr = make_pack_expansion (expr);
27380 PACK_EXPANSION_SIZEOF_P (expr) = true;
27382 if (paren)
27383 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
27385 return expr;
27388 /* Parse the operand of `sizeof' (or a similar operator). Returns
27389 either a TYPE or an expression, depending on the form of the
27390 input. The KEYWORD indicates which kind of expression we have
27391 encountered. */
27393 static tree
27394 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
27396 tree expr = NULL_TREE;
27397 const char *saved_message;
27398 char *tmp;
27399 bool saved_integral_constant_expression_p;
27400 bool saved_non_integral_constant_expression_p;
27402 /* If it's a `...', then we are computing the length of a parameter
27403 pack. */
27404 if (keyword == RID_SIZEOF
27405 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
27406 return cp_parser_sizeof_pack (parser);
27408 /* Types cannot be defined in a `sizeof' expression. Save away the
27409 old message. */
27410 saved_message = parser->type_definition_forbidden_message;
27411 /* And create the new one. */
27412 tmp = concat ("types may not be defined in %<",
27413 IDENTIFIER_POINTER (ridpointers[keyword]),
27414 "%> expressions", NULL);
27415 parser->type_definition_forbidden_message = tmp;
27417 /* The restrictions on constant-expressions do not apply inside
27418 sizeof expressions. */
27419 saved_integral_constant_expression_p
27420 = parser->integral_constant_expression_p;
27421 saved_non_integral_constant_expression_p
27422 = parser->non_integral_constant_expression_p;
27423 parser->integral_constant_expression_p = false;
27425 /* Do not actually evaluate the expression. */
27426 ++cp_unevaluated_operand;
27427 ++c_inhibit_evaluation_warnings;
27428 /* If it's a `(', then we might be looking at the type-id
27429 construction. */
27430 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
27432 tree type = NULL_TREE;
27434 /* We can't be sure yet whether we're looking at a type-id or an
27435 expression. */
27436 cp_parser_parse_tentatively (parser);
27437 /* Note: as a GNU Extension, compound literals are considered
27438 postfix-expressions as they are in C99, so they are valid
27439 arguments to sizeof. See comment in cp_parser_cast_expression
27440 for details. */
27441 if (cp_parser_compound_literal_p (parser))
27442 cp_parser_simulate_error (parser);
27443 else
27445 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
27446 parser->in_type_id_in_expr_p = true;
27447 /* Look for the type-id. */
27448 type = cp_parser_type_id (parser);
27449 /* Look for the closing `)'. */
27450 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
27451 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
27454 /* If all went well, then we're done. */
27455 if (cp_parser_parse_definitely (parser))
27457 cp_decl_specifier_seq decl_specs;
27459 /* Build a trivial decl-specifier-seq. */
27460 clear_decl_specs (&decl_specs);
27461 decl_specs.type = type;
27463 /* Call grokdeclarator to figure out what type this is. */
27464 expr = grokdeclarator (NULL,
27465 &decl_specs,
27466 TYPENAME,
27467 /*initialized=*/0,
27468 /*attrlist=*/NULL);
27472 /* If the type-id production did not work out, then we must be
27473 looking at the unary-expression production. */
27474 if (!expr)
27475 expr = cp_parser_unary_expression (parser);
27477 /* Go back to evaluating expressions. */
27478 --cp_unevaluated_operand;
27479 --c_inhibit_evaluation_warnings;
27481 /* Free the message we created. */
27482 free (tmp);
27483 /* And restore the old one. */
27484 parser->type_definition_forbidden_message = saved_message;
27485 parser->integral_constant_expression_p
27486 = saved_integral_constant_expression_p;
27487 parser->non_integral_constant_expression_p
27488 = saved_non_integral_constant_expression_p;
27490 return expr;
27493 /* If the current declaration has no declarator, return true. */
27495 static bool
27496 cp_parser_declares_only_class_p (cp_parser *parser)
27498 /* If the next token is a `;' or a `,' then there is no
27499 declarator. */
27500 return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
27501 || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
27504 /* Update the DECL_SPECS to reflect the storage class indicated by
27505 KEYWORD. */
27507 static void
27508 cp_parser_set_storage_class (cp_parser *parser,
27509 cp_decl_specifier_seq *decl_specs,
27510 enum rid keyword,
27511 cp_token *token)
27513 cp_storage_class storage_class;
27515 if (parser->in_unbraced_linkage_specification_p)
27517 error_at (token->location, "invalid use of %qD in linkage specification",
27518 ridpointers[keyword]);
27519 return;
27521 else if (decl_specs->storage_class != sc_none)
27523 decl_specs->conflicting_specifiers_p = true;
27524 return;
27527 if ((keyword == RID_EXTERN || keyword == RID_STATIC)
27528 && decl_spec_seq_has_spec_p (decl_specs, ds_thread)
27529 && decl_specs->gnu_thread_keyword_p)
27531 pedwarn (decl_specs->locations[ds_thread], 0,
27532 "%<__thread%> before %qD", ridpointers[keyword]);
27535 switch (keyword)
27537 case RID_AUTO:
27538 storage_class = sc_auto;
27539 break;
27540 case RID_REGISTER:
27541 storage_class = sc_register;
27542 break;
27543 case RID_STATIC:
27544 storage_class = sc_static;
27545 break;
27546 case RID_EXTERN:
27547 storage_class = sc_extern;
27548 break;
27549 case RID_MUTABLE:
27550 storage_class = sc_mutable;
27551 break;
27552 default:
27553 gcc_unreachable ();
27555 decl_specs->storage_class = storage_class;
27556 set_and_check_decl_spec_loc (decl_specs, ds_storage_class, token);
27558 /* A storage class specifier cannot be applied alongside a typedef
27559 specifier. If there is a typedef specifier present then set
27560 conflicting_specifiers_p which will trigger an error later
27561 on in grokdeclarator. */
27562 if (decl_spec_seq_has_spec_p (decl_specs, ds_typedef))
27563 decl_specs->conflicting_specifiers_p = true;
27566 /* Update the DECL_SPECS to reflect the TYPE_SPEC. If TYPE_DEFINITION_P
27567 is true, the type is a class or enum definition. */
27569 static void
27570 cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
27571 tree type_spec,
27572 cp_token *token,
27573 bool type_definition_p)
27575 decl_specs->any_specifiers_p = true;
27577 /* If the user tries to redeclare bool, char16_t, char32_t, or wchar_t
27578 (with, for example, in "typedef int wchar_t;") we remember that
27579 this is what happened. In system headers, we ignore these
27580 declarations so that G++ can work with system headers that are not
27581 C++-safe. */
27582 if (decl_spec_seq_has_spec_p (decl_specs, ds_typedef)
27583 && !type_definition_p
27584 && (type_spec == boolean_type_node
27585 || type_spec == char16_type_node
27586 || type_spec == char32_type_node
27587 || type_spec == wchar_type_node)
27588 && (decl_specs->type
27589 || decl_spec_seq_has_spec_p (decl_specs, ds_long)
27590 || decl_spec_seq_has_spec_p (decl_specs, ds_short)
27591 || decl_spec_seq_has_spec_p (decl_specs, ds_unsigned)
27592 || decl_spec_seq_has_spec_p (decl_specs, ds_signed)))
27594 decl_specs->redefined_builtin_type = type_spec;
27595 set_and_check_decl_spec_loc (decl_specs,
27596 ds_redefined_builtin_type_spec,
27597 token);
27598 if (!decl_specs->type)
27600 decl_specs->type = type_spec;
27601 decl_specs->type_definition_p = false;
27602 set_and_check_decl_spec_loc (decl_specs,ds_type_spec, token);
27605 else if (decl_specs->type)
27606 decl_specs->multiple_types_p = true;
27607 else
27609 decl_specs->type = type_spec;
27610 decl_specs->type_definition_p = type_definition_p;
27611 decl_specs->redefined_builtin_type = NULL_TREE;
27612 set_and_check_decl_spec_loc (decl_specs, ds_type_spec, token);
27616 /* True iff TOKEN is the GNU keyword __thread. */
27618 static bool
27619 token_is__thread (cp_token *token)
27621 gcc_assert (token->keyword == RID_THREAD);
27622 return !strcmp (IDENTIFIER_POINTER (token->u.value), "__thread");
27625 /* Set the location for a declarator specifier and check if it is
27626 duplicated.
27628 DECL_SPECS is the sequence of declarator specifiers onto which to
27629 set the location.
27631 DS is the single declarator specifier to set which location is to
27632 be set onto the existing sequence of declarators.
27634 LOCATION is the location for the declarator specifier to
27635 consider. */
27637 static void
27638 set_and_check_decl_spec_loc (cp_decl_specifier_seq *decl_specs,
27639 cp_decl_spec ds, cp_token *token)
27641 gcc_assert (ds < ds_last);
27643 if (decl_specs == NULL)
27644 return;
27646 source_location location = token->location;
27648 if (decl_specs->locations[ds] == 0)
27650 decl_specs->locations[ds] = location;
27651 if (ds == ds_thread)
27652 decl_specs->gnu_thread_keyword_p = token_is__thread (token);
27654 else
27656 if (ds == ds_long)
27658 if (decl_specs->locations[ds_long_long] != 0)
27659 error_at (location,
27660 "%<long long long%> is too long for GCC");
27661 else
27663 decl_specs->locations[ds_long_long] = location;
27664 pedwarn_cxx98 (location,
27665 OPT_Wlong_long,
27666 "ISO C++ 1998 does not support %<long long%>");
27669 else if (ds == ds_thread)
27671 bool gnu = token_is__thread (token);
27672 if (gnu != decl_specs->gnu_thread_keyword_p)
27673 error_at (location,
27674 "both %<__thread%> and %<thread_local%> specified");
27675 else
27676 error_at (location, "duplicate %qD", token->u.value);
27678 else
27680 static const char *const decl_spec_names[] = {
27681 "signed",
27682 "unsigned",
27683 "short",
27684 "long",
27685 "const",
27686 "volatile",
27687 "restrict",
27688 "inline",
27689 "virtual",
27690 "explicit",
27691 "friend",
27692 "typedef",
27693 "using",
27694 "constexpr",
27695 "__complex"
27697 error_at (location,
27698 "duplicate %qs", decl_spec_names[ds]);
27703 /* Return true iff the declarator specifier DS is present in the
27704 sequence of declarator specifiers DECL_SPECS. */
27706 bool
27707 decl_spec_seq_has_spec_p (const cp_decl_specifier_seq * decl_specs,
27708 cp_decl_spec ds)
27710 gcc_assert (ds < ds_last);
27712 if (decl_specs == NULL)
27713 return false;
27715 return decl_specs->locations[ds] != 0;
27718 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
27719 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
27721 static bool
27722 cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
27724 return decl_spec_seq_has_spec_p (decl_specifiers, ds_friend);
27727 /* Issue an error message indicating that TOKEN_DESC was expected.
27728 If KEYWORD is true, it indicated this function is called by
27729 cp_parser_require_keword and the required token can only be
27730 a indicated keyword. */
27732 static void
27733 cp_parser_required_error (cp_parser *parser,
27734 required_token token_desc,
27735 bool keyword)
27737 switch (token_desc)
27739 case RT_NEW:
27740 cp_parser_error (parser, "expected %<new%>");
27741 return;
27742 case RT_DELETE:
27743 cp_parser_error (parser, "expected %<delete%>");
27744 return;
27745 case RT_RETURN:
27746 cp_parser_error (parser, "expected %<return%>");
27747 return;
27748 case RT_WHILE:
27749 cp_parser_error (parser, "expected %<while%>");
27750 return;
27751 case RT_EXTERN:
27752 cp_parser_error (parser, "expected %<extern%>");
27753 return;
27754 case RT_STATIC_ASSERT:
27755 cp_parser_error (parser, "expected %<static_assert%>");
27756 return;
27757 case RT_DECLTYPE:
27758 cp_parser_error (parser, "expected %<decltype%>");
27759 return;
27760 case RT_OPERATOR:
27761 cp_parser_error (parser, "expected %<operator%>");
27762 return;
27763 case RT_CLASS:
27764 cp_parser_error (parser, "expected %<class%>");
27765 return;
27766 case RT_TEMPLATE:
27767 cp_parser_error (parser, "expected %<template%>");
27768 return;
27769 case RT_NAMESPACE:
27770 cp_parser_error (parser, "expected %<namespace%>");
27771 return;
27772 case RT_USING:
27773 cp_parser_error (parser, "expected %<using%>");
27774 return;
27775 case RT_ASM:
27776 cp_parser_error (parser, "expected %<asm%>");
27777 return;
27778 case RT_TRY:
27779 cp_parser_error (parser, "expected %<try%>");
27780 return;
27781 case RT_CATCH:
27782 cp_parser_error (parser, "expected %<catch%>");
27783 return;
27784 case RT_THROW:
27785 cp_parser_error (parser, "expected %<throw%>");
27786 return;
27787 case RT_LABEL:
27788 cp_parser_error (parser, "expected %<__label__%>");
27789 return;
27790 case RT_AT_TRY:
27791 cp_parser_error (parser, "expected %<@try%>");
27792 return;
27793 case RT_AT_SYNCHRONIZED:
27794 cp_parser_error (parser, "expected %<@synchronized%>");
27795 return;
27796 case RT_AT_THROW:
27797 cp_parser_error (parser, "expected %<@throw%>");
27798 return;
27799 case RT_TRANSACTION_ATOMIC:
27800 cp_parser_error (parser, "expected %<__transaction_atomic%>");
27801 return;
27802 case RT_TRANSACTION_RELAXED:
27803 cp_parser_error (parser, "expected %<__transaction_relaxed%>");
27804 return;
27805 default:
27806 break;
27808 if (!keyword)
27810 switch (token_desc)
27812 case RT_SEMICOLON:
27813 cp_parser_error (parser, "expected %<;%>");
27814 return;
27815 case RT_OPEN_PAREN:
27816 cp_parser_error (parser, "expected %<(%>");
27817 return;
27818 case RT_CLOSE_BRACE:
27819 cp_parser_error (parser, "expected %<}%>");
27820 return;
27821 case RT_OPEN_BRACE:
27822 cp_parser_error (parser, "expected %<{%>");
27823 return;
27824 case RT_CLOSE_SQUARE:
27825 cp_parser_error (parser, "expected %<]%>");
27826 return;
27827 case RT_OPEN_SQUARE:
27828 cp_parser_error (parser, "expected %<[%>");
27829 return;
27830 case RT_COMMA:
27831 cp_parser_error (parser, "expected %<,%>");
27832 return;
27833 case RT_SCOPE:
27834 cp_parser_error (parser, "expected %<::%>");
27835 return;
27836 case RT_LESS:
27837 cp_parser_error (parser, "expected %<<%>");
27838 return;
27839 case RT_GREATER:
27840 cp_parser_error (parser, "expected %<>%>");
27841 return;
27842 case RT_EQ:
27843 cp_parser_error (parser, "expected %<=%>");
27844 return;
27845 case RT_ELLIPSIS:
27846 cp_parser_error (parser, "expected %<...%>");
27847 return;
27848 case RT_MULT:
27849 cp_parser_error (parser, "expected %<*%>");
27850 return;
27851 case RT_COMPL:
27852 cp_parser_error (parser, "expected %<~%>");
27853 return;
27854 case RT_COLON:
27855 cp_parser_error (parser, "expected %<:%>");
27856 return;
27857 case RT_COLON_SCOPE:
27858 cp_parser_error (parser, "expected %<:%> or %<::%>");
27859 return;
27860 case RT_CLOSE_PAREN:
27861 cp_parser_error (parser, "expected %<)%>");
27862 return;
27863 case RT_COMMA_CLOSE_PAREN:
27864 cp_parser_error (parser, "expected %<,%> or %<)%>");
27865 return;
27866 case RT_PRAGMA_EOL:
27867 cp_parser_error (parser, "expected end of line");
27868 return;
27869 case RT_NAME:
27870 cp_parser_error (parser, "expected identifier");
27871 return;
27872 case RT_SELECT:
27873 cp_parser_error (parser, "expected selection-statement");
27874 return;
27875 case RT_INTERATION:
27876 cp_parser_error (parser, "expected iteration-statement");
27877 return;
27878 case RT_JUMP:
27879 cp_parser_error (parser, "expected jump-statement");
27880 return;
27881 case RT_CLASS_KEY:
27882 cp_parser_error (parser, "expected class-key");
27883 return;
27884 case RT_CLASS_TYPENAME_TEMPLATE:
27885 cp_parser_error (parser,
27886 "expected %<class%>, %<typename%>, or %<template%>");
27887 return;
27888 default:
27889 gcc_unreachable ();
27892 else
27893 gcc_unreachable ();
27898 /* If the next token is of the indicated TYPE, consume it. Otherwise,
27899 issue an error message indicating that TOKEN_DESC was expected.
27901 Returns the token consumed, if the token had the appropriate type.
27902 Otherwise, returns NULL. */
27904 static cp_token *
27905 cp_parser_require (cp_parser* parser,
27906 enum cpp_ttype type,
27907 required_token token_desc)
27909 if (cp_lexer_next_token_is (parser->lexer, type))
27910 return cp_lexer_consume_token (parser->lexer);
27911 else
27913 /* Output the MESSAGE -- unless we're parsing tentatively. */
27914 if (!cp_parser_simulate_error (parser))
27915 cp_parser_required_error (parser, token_desc, /*keyword=*/false);
27916 return NULL;
27920 /* An error message is produced if the next token is not '>'.
27921 All further tokens are skipped until the desired token is
27922 found or '{', '}', ';' or an unbalanced ')' or ']'. */
27924 static void
27925 cp_parser_skip_to_end_of_template_parameter_list (cp_parser* parser)
27927 /* Current level of '< ... >'. */
27928 unsigned level = 0;
27929 /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'. */
27930 unsigned nesting_depth = 0;
27932 /* Are we ready, yet? If not, issue error message. */
27933 if (cp_parser_require (parser, CPP_GREATER, RT_GREATER))
27934 return;
27936 /* Skip tokens until the desired token is found. */
27937 while (true)
27939 /* Peek at the next token. */
27940 switch (cp_lexer_peek_token (parser->lexer)->type)
27942 case CPP_LESS:
27943 if (!nesting_depth)
27944 ++level;
27945 break;
27947 case CPP_RSHIFT:
27948 if (cxx_dialect == cxx98)
27949 /* C++0x views the `>>' operator as two `>' tokens, but
27950 C++98 does not. */
27951 break;
27952 else if (!nesting_depth && level-- == 0)
27954 /* We've hit a `>>' where the first `>' closes the
27955 template argument list, and the second `>' is
27956 spurious. Just consume the `>>' and stop; we've
27957 already produced at least one error. */
27958 cp_lexer_consume_token (parser->lexer);
27959 return;
27961 /* Fall through for C++0x, so we handle the second `>' in
27962 the `>>'. */
27963 gcc_fallthrough ();
27965 case CPP_GREATER:
27966 if (!nesting_depth && level-- == 0)
27968 /* We've reached the token we want, consume it and stop. */
27969 cp_lexer_consume_token (parser->lexer);
27970 return;
27972 break;
27974 case CPP_OPEN_PAREN:
27975 case CPP_OPEN_SQUARE:
27976 ++nesting_depth;
27977 break;
27979 case CPP_CLOSE_PAREN:
27980 case CPP_CLOSE_SQUARE:
27981 if (nesting_depth-- == 0)
27982 return;
27983 break;
27985 case CPP_EOF:
27986 case CPP_PRAGMA_EOL:
27987 case CPP_SEMICOLON:
27988 case CPP_OPEN_BRACE:
27989 case CPP_CLOSE_BRACE:
27990 /* The '>' was probably forgotten, don't look further. */
27991 return;
27993 default:
27994 break;
27997 /* Consume this token. */
27998 cp_lexer_consume_token (parser->lexer);
28002 /* If the next token is the indicated keyword, consume it. Otherwise,
28003 issue an error message indicating that TOKEN_DESC was expected.
28005 Returns the token consumed, if the token had the appropriate type.
28006 Otherwise, returns NULL. */
28008 static cp_token *
28009 cp_parser_require_keyword (cp_parser* parser,
28010 enum rid keyword,
28011 required_token token_desc)
28013 cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
28015 if (token && token->keyword != keyword)
28017 cp_parser_required_error (parser, token_desc, /*keyword=*/true);
28018 return NULL;
28021 return token;
28024 /* Returns TRUE iff TOKEN is a token that can begin the body of a
28025 function-definition. */
28027 static bool
28028 cp_parser_token_starts_function_definition_p (cp_token* token)
28030 return (/* An ordinary function-body begins with an `{'. */
28031 token->type == CPP_OPEN_BRACE
28032 /* A ctor-initializer begins with a `:'. */
28033 || token->type == CPP_COLON
28034 /* A function-try-block begins with `try'. */
28035 || token->keyword == RID_TRY
28036 /* A function-transaction-block begins with `__transaction_atomic'
28037 or `__transaction_relaxed'. */
28038 || token->keyword == RID_TRANSACTION_ATOMIC
28039 || token->keyword == RID_TRANSACTION_RELAXED
28040 /* The named return value extension begins with `return'. */
28041 || token->keyword == RID_RETURN);
28044 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
28045 definition. */
28047 static bool
28048 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
28050 cp_token *token;
28052 token = cp_lexer_peek_token (parser->lexer);
28053 return (token->type == CPP_OPEN_BRACE
28054 || (token->type == CPP_COLON
28055 && !parser->colon_doesnt_start_class_def_p));
28058 /* Returns TRUE iff the next token is the "," or ">" (or `>>', in
28059 C++0x) ending a template-argument. */
28061 static bool
28062 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
28064 cp_token *token;
28066 token = cp_lexer_peek_token (parser->lexer);
28067 return (token->type == CPP_COMMA
28068 || token->type == CPP_GREATER
28069 || token->type == CPP_ELLIPSIS
28070 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT));
28073 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
28074 (n+1)-th is a ":" (which is a possible digraph typo for "< ::"). */
28076 static bool
28077 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
28078 size_t n)
28080 cp_token *token;
28082 token = cp_lexer_peek_nth_token (parser->lexer, n);
28083 if (token->type == CPP_LESS)
28084 return true;
28085 /* Check for the sequence `<::' in the original code. It would be lexed as
28086 `[:', where `[' is a digraph, and there is no whitespace before
28087 `:'. */
28088 if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
28090 cp_token *token2;
28091 token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
28092 if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
28093 return true;
28095 return false;
28098 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
28099 or none_type otherwise. */
28101 static enum tag_types
28102 cp_parser_token_is_class_key (cp_token* token)
28104 switch (token->keyword)
28106 case RID_CLASS:
28107 return class_type;
28108 case RID_STRUCT:
28109 return record_type;
28110 case RID_UNION:
28111 return union_type;
28113 default:
28114 return none_type;
28118 /* Returns the kind of tag indicated by TOKEN, if it is a type-parameter-key,
28119 or none_type otherwise or if the token is null. */
28121 static enum tag_types
28122 cp_parser_token_is_type_parameter_key (cp_token* token)
28124 if (!token)
28125 return none_type;
28127 switch (token->keyword)
28129 case RID_CLASS:
28130 return class_type;
28131 case RID_TYPENAME:
28132 return typename_type;
28134 default:
28135 return none_type;
28139 /* Issue an error message if the CLASS_KEY does not match the TYPE. */
28141 static void
28142 cp_parser_check_class_key (enum tag_types class_key, tree type)
28144 if (type == error_mark_node)
28145 return;
28146 if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
28148 if (permerror (input_location, "%qs tag used in naming %q#T",
28149 class_key == union_type ? "union"
28150 : class_key == record_type ? "struct" : "class",
28151 type))
28152 inform (DECL_SOURCE_LOCATION (TYPE_NAME (type)),
28153 "%q#T was previously declared here", type);
28157 /* Issue an error message if DECL is redeclared with different
28158 access than its original declaration [class.access.spec/3].
28159 This applies to nested classes, nested class templates and
28160 enumerations [class.mem/1]. */
28162 static void
28163 cp_parser_check_access_in_redeclaration (tree decl, location_t location)
28165 if (!decl
28166 || (!CLASS_TYPE_P (TREE_TYPE (decl))
28167 && TREE_CODE (TREE_TYPE (decl)) != ENUMERAL_TYPE))
28168 return;
28170 if ((TREE_PRIVATE (decl)
28171 != (current_access_specifier == access_private_node))
28172 || (TREE_PROTECTED (decl)
28173 != (current_access_specifier == access_protected_node)))
28174 error_at (location, "%qD redeclared with different access", decl);
28177 /* Look for the `template' keyword, as a syntactic disambiguator.
28178 Return TRUE iff it is present, in which case it will be
28179 consumed. */
28181 static bool
28182 cp_parser_optional_template_keyword (cp_parser *parser)
28184 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
28186 /* In C++98 the `template' keyword can only be used within templates;
28187 outside templates the parser can always figure out what is a
28188 template and what is not. In C++11, per the resolution of DR 468,
28189 `template' is allowed in cases where it is not strictly necessary. */
28190 if (!processing_template_decl
28191 && pedantic && cxx_dialect == cxx98)
28193 cp_token *token = cp_lexer_peek_token (parser->lexer);
28194 pedwarn (token->location, OPT_Wpedantic,
28195 "in C++98 %<template%> (as a disambiguator) is only "
28196 "allowed within templates");
28197 /* If this part of the token stream is rescanned, the same
28198 error message would be generated. So, we purge the token
28199 from the stream. */
28200 cp_lexer_purge_token (parser->lexer);
28201 return false;
28203 else
28205 /* Consume the `template' keyword. */
28206 cp_lexer_consume_token (parser->lexer);
28207 return true;
28210 return false;
28213 /* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
28214 set PARSER->SCOPE, and perform other related actions. */
28216 static void
28217 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
28219 struct tree_check *check_value;
28221 /* Get the stored value. */
28222 check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
28223 /* Set the scope from the stored value. */
28224 parser->scope = saved_checks_value (check_value);
28225 parser->qualifying_scope = check_value->qualifying_scope;
28226 parser->object_scope = NULL_TREE;
28229 /* Consume tokens up through a non-nested END token. Returns TRUE if we
28230 encounter the end of a block before what we were looking for. */
28232 static bool
28233 cp_parser_cache_group (cp_parser *parser,
28234 enum cpp_ttype end,
28235 unsigned depth)
28237 while (true)
28239 cp_token *token = cp_lexer_peek_token (parser->lexer);
28241 /* Abort a parenthesized expression if we encounter a semicolon. */
28242 if ((end == CPP_CLOSE_PAREN || depth == 0)
28243 && token->type == CPP_SEMICOLON)
28244 return true;
28245 /* If we've reached the end of the file, stop. */
28246 if (token->type == CPP_EOF
28247 || (end != CPP_PRAGMA_EOL
28248 && token->type == CPP_PRAGMA_EOL))
28249 return true;
28250 if (token->type == CPP_CLOSE_BRACE && depth == 0)
28251 /* We've hit the end of an enclosing block, so there's been some
28252 kind of syntax error. */
28253 return true;
28255 /* Consume the token. */
28256 cp_lexer_consume_token (parser->lexer);
28257 /* See if it starts a new group. */
28258 if (token->type == CPP_OPEN_BRACE)
28260 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
28261 /* In theory this should probably check end == '}', but
28262 cp_parser_save_member_function_body needs it to exit
28263 after either '}' or ')' when called with ')'. */
28264 if (depth == 0)
28265 return false;
28267 else if (token->type == CPP_OPEN_PAREN)
28269 cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
28270 if (depth == 0 && end == CPP_CLOSE_PAREN)
28271 return false;
28273 else if (token->type == CPP_PRAGMA)
28274 cp_parser_cache_group (parser, CPP_PRAGMA_EOL, depth + 1);
28275 else if (token->type == end)
28276 return false;
28280 /* Like above, for caching a default argument or NSDMI. Both of these are
28281 terminated by a non-nested comma, but it can be unclear whether or not a
28282 comma is nested in a template argument list unless we do more parsing.
28283 In order to handle this ambiguity, when we encounter a ',' after a '<'
28284 we try to parse what follows as a parameter-declaration-list (in the
28285 case of a default argument) or a member-declarator (in the case of an
28286 NSDMI). If that succeeds, then we stop caching. */
28288 static tree
28289 cp_parser_cache_defarg (cp_parser *parser, bool nsdmi)
28291 unsigned depth = 0;
28292 int maybe_template_id = 0;
28293 cp_token *first_token;
28294 cp_token *token;
28295 tree default_argument;
28297 /* Add tokens until we have processed the entire default
28298 argument. We add the range [first_token, token). */
28299 first_token = cp_lexer_peek_token (parser->lexer);
28300 if (first_token->type == CPP_OPEN_BRACE)
28302 /* For list-initialization, this is straightforward. */
28303 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
28304 token = cp_lexer_peek_token (parser->lexer);
28306 else while (true)
28308 bool done = false;
28310 /* Peek at the next token. */
28311 token = cp_lexer_peek_token (parser->lexer);
28312 /* What we do depends on what token we have. */
28313 switch (token->type)
28315 /* In valid code, a default argument must be
28316 immediately followed by a `,' `)', or `...'. */
28317 case CPP_COMMA:
28318 if (depth == 0 && maybe_template_id)
28320 /* If we've seen a '<', we might be in a
28321 template-argument-list. Until Core issue 325 is
28322 resolved, we don't know how this situation ought
28323 to be handled, so try to DTRT. We check whether
28324 what comes after the comma is a valid parameter
28325 declaration list. If it is, then the comma ends
28326 the default argument; otherwise the default
28327 argument continues. */
28328 bool error = false;
28329 cp_token *peek;
28331 /* Set ITALP so cp_parser_parameter_declaration_list
28332 doesn't decide to commit to this parse. */
28333 bool saved_italp = parser->in_template_argument_list_p;
28334 parser->in_template_argument_list_p = true;
28336 cp_parser_parse_tentatively (parser);
28338 if (nsdmi)
28340 /* Parse declarators until we reach a non-comma or
28341 somthing that cannot be an initializer.
28342 Just checking whether we're looking at a single
28343 declarator is insufficient. Consider:
28344 int var = tuple<T,U>::x;
28345 The template parameter 'U' looks exactly like a
28346 declarator. */
28349 int ctor_dtor_or_conv_p;
28350 cp_lexer_consume_token (parser->lexer);
28351 cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
28352 &ctor_dtor_or_conv_p,
28353 /*parenthesized_p=*/NULL,
28354 /*member_p=*/true,
28355 /*friend_p=*/false);
28356 peek = cp_lexer_peek_token (parser->lexer);
28357 if (cp_parser_error_occurred (parser))
28358 break;
28360 while (peek->type == CPP_COMMA);
28361 /* If we met an '=' or ';' then the original comma
28362 was the end of the NSDMI. Otherwise assume
28363 we're still in the NSDMI. */
28364 error = (peek->type != CPP_EQ
28365 && peek->type != CPP_SEMICOLON);
28367 else
28369 cp_lexer_consume_token (parser->lexer);
28370 begin_scope (sk_function_parms, NULL_TREE);
28371 cp_parser_parameter_declaration_list (parser, &error);
28372 pop_bindings_and_leave_scope ();
28374 if (!cp_parser_error_occurred (parser) && !error)
28375 done = true;
28376 cp_parser_abort_tentative_parse (parser);
28378 parser->in_template_argument_list_p = saved_italp;
28379 break;
28381 /* FALLTHRU */
28382 case CPP_CLOSE_PAREN:
28383 case CPP_ELLIPSIS:
28384 /* If we run into a non-nested `;', `}', or `]',
28385 then the code is invalid -- but the default
28386 argument is certainly over. */
28387 case CPP_SEMICOLON:
28388 case CPP_CLOSE_BRACE:
28389 case CPP_CLOSE_SQUARE:
28390 if (depth == 0
28391 /* Handle correctly int n = sizeof ... ( p ); */
28392 && token->type != CPP_ELLIPSIS)
28393 done = true;
28394 /* Update DEPTH, if necessary. */
28395 else if (token->type == CPP_CLOSE_PAREN
28396 || token->type == CPP_CLOSE_BRACE
28397 || token->type == CPP_CLOSE_SQUARE)
28398 --depth;
28399 break;
28401 case CPP_OPEN_PAREN:
28402 case CPP_OPEN_SQUARE:
28403 case CPP_OPEN_BRACE:
28404 ++depth;
28405 break;
28407 case CPP_LESS:
28408 if (depth == 0)
28409 /* This might be the comparison operator, or it might
28410 start a template argument list. */
28411 ++maybe_template_id;
28412 break;
28414 case CPP_RSHIFT:
28415 if (cxx_dialect == cxx98)
28416 break;
28417 /* Fall through for C++0x, which treats the `>>'
28418 operator like two `>' tokens in certain
28419 cases. */
28420 gcc_fallthrough ();
28422 case CPP_GREATER:
28423 if (depth == 0)
28425 /* This might be an operator, or it might close a
28426 template argument list. But if a previous '<'
28427 started a template argument list, this will have
28428 closed it, so we can't be in one anymore. */
28429 maybe_template_id -= 1 + (token->type == CPP_RSHIFT);
28430 if (maybe_template_id < 0)
28431 maybe_template_id = 0;
28433 break;
28435 /* If we run out of tokens, issue an error message. */
28436 case CPP_EOF:
28437 case CPP_PRAGMA_EOL:
28438 error_at (token->location, "file ends in default argument");
28439 return error_mark_node;
28441 case CPP_NAME:
28442 case CPP_SCOPE:
28443 /* In these cases, we should look for template-ids.
28444 For example, if the default argument is
28445 `X<int, double>()', we need to do name lookup to
28446 figure out whether or not `X' is a template; if
28447 so, the `,' does not end the default argument.
28449 That is not yet done. */
28450 break;
28452 default:
28453 break;
28456 /* If we've reached the end, stop. */
28457 if (done)
28458 break;
28460 /* Add the token to the token block. */
28461 token = cp_lexer_consume_token (parser->lexer);
28464 /* Create a DEFAULT_ARG to represent the unparsed default
28465 argument. */
28466 default_argument = make_node (DEFAULT_ARG);
28467 DEFARG_TOKENS (default_argument)
28468 = cp_token_cache_new (first_token, token);
28469 DEFARG_INSTANTIATIONS (default_argument) = NULL;
28471 return default_argument;
28474 /* Begin parsing tentatively. We always save tokens while parsing
28475 tentatively so that if the tentative parsing fails we can restore the
28476 tokens. */
28478 static void
28479 cp_parser_parse_tentatively (cp_parser* parser)
28481 /* Enter a new parsing context. */
28482 parser->context = cp_parser_context_new (parser->context);
28483 /* Begin saving tokens. */
28484 cp_lexer_save_tokens (parser->lexer);
28485 /* In order to avoid repetitive access control error messages,
28486 access checks are queued up until we are no longer parsing
28487 tentatively. */
28488 push_deferring_access_checks (dk_deferred);
28491 /* Commit to the currently active tentative parse. */
28493 static void
28494 cp_parser_commit_to_tentative_parse (cp_parser* parser)
28496 cp_parser_context *context;
28497 cp_lexer *lexer;
28499 /* Mark all of the levels as committed. */
28500 lexer = parser->lexer;
28501 for (context = parser->context; context->next; context = context->next)
28503 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
28504 break;
28505 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
28506 while (!cp_lexer_saving_tokens (lexer))
28507 lexer = lexer->next;
28508 cp_lexer_commit_tokens (lexer);
28512 /* Commit to the topmost currently active tentative parse.
28514 Note that this function shouldn't be called when there are
28515 irreversible side-effects while in a tentative state. For
28516 example, we shouldn't create a permanent entry in the symbol
28517 table, or issue an error message that might not apply if the
28518 tentative parse is aborted. */
28520 static void
28521 cp_parser_commit_to_topmost_tentative_parse (cp_parser* parser)
28523 cp_parser_context *context = parser->context;
28524 cp_lexer *lexer = parser->lexer;
28526 if (context)
28528 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
28529 return;
28530 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
28532 while (!cp_lexer_saving_tokens (lexer))
28533 lexer = lexer->next;
28534 cp_lexer_commit_tokens (lexer);
28538 /* Abort the currently active tentative parse. All consumed tokens
28539 will be rolled back, and no diagnostics will be issued. */
28541 static void
28542 cp_parser_abort_tentative_parse (cp_parser* parser)
28544 gcc_assert (parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED
28545 || errorcount > 0);
28546 cp_parser_simulate_error (parser);
28547 /* Now, pretend that we want to see if the construct was
28548 successfully parsed. */
28549 cp_parser_parse_definitely (parser);
28552 /* Stop parsing tentatively. If a parse error has occurred, restore the
28553 token stream. Otherwise, commit to the tokens we have consumed.
28554 Returns true if no error occurred; false otherwise. */
28556 static bool
28557 cp_parser_parse_definitely (cp_parser* parser)
28559 bool error_occurred;
28560 cp_parser_context *context;
28562 /* Remember whether or not an error occurred, since we are about to
28563 destroy that information. */
28564 error_occurred = cp_parser_error_occurred (parser);
28565 /* Remove the topmost context from the stack. */
28566 context = parser->context;
28567 parser->context = context->next;
28568 /* If no parse errors occurred, commit to the tentative parse. */
28569 if (!error_occurred)
28571 /* Commit to the tokens read tentatively, unless that was
28572 already done. */
28573 if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
28574 cp_lexer_commit_tokens (parser->lexer);
28576 pop_to_parent_deferring_access_checks ();
28578 /* Otherwise, if errors occurred, roll back our state so that things
28579 are just as they were before we began the tentative parse. */
28580 else
28582 cp_lexer_rollback_tokens (parser->lexer);
28583 pop_deferring_access_checks ();
28585 /* Add the context to the front of the free list. */
28586 context->next = cp_parser_context_free_list;
28587 cp_parser_context_free_list = context;
28589 return !error_occurred;
28592 /* Returns true if we are parsing tentatively and are not committed to
28593 this tentative parse. */
28595 static bool
28596 cp_parser_uncommitted_to_tentative_parse_p (cp_parser* parser)
28598 return (cp_parser_parsing_tentatively (parser)
28599 && parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED);
28602 /* Returns nonzero iff an error has occurred during the most recent
28603 tentative parse. */
28605 static bool
28606 cp_parser_error_occurred (cp_parser* parser)
28608 return (cp_parser_parsing_tentatively (parser)
28609 && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
28612 /* Returns nonzero if GNU extensions are allowed. */
28614 static bool
28615 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
28617 return parser->allow_gnu_extensions_p;
28620 /* Objective-C++ Productions */
28623 /* Parse an Objective-C expression, which feeds into a primary-expression
28624 above.
28626 objc-expression:
28627 objc-message-expression
28628 objc-string-literal
28629 objc-encode-expression
28630 objc-protocol-expression
28631 objc-selector-expression
28633 Returns a tree representation of the expression. */
28635 static cp_expr
28636 cp_parser_objc_expression (cp_parser* parser)
28638 /* Try to figure out what kind of declaration is present. */
28639 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
28641 switch (kwd->type)
28643 case CPP_OPEN_SQUARE:
28644 return cp_parser_objc_message_expression (parser);
28646 case CPP_OBJC_STRING:
28647 kwd = cp_lexer_consume_token (parser->lexer);
28648 return objc_build_string_object (kwd->u.value);
28650 case CPP_KEYWORD:
28651 switch (kwd->keyword)
28653 case RID_AT_ENCODE:
28654 return cp_parser_objc_encode_expression (parser);
28656 case RID_AT_PROTOCOL:
28657 return cp_parser_objc_protocol_expression (parser);
28659 case RID_AT_SELECTOR:
28660 return cp_parser_objc_selector_expression (parser);
28662 default:
28663 break;
28665 default:
28666 error_at (kwd->location,
28667 "misplaced %<@%D%> Objective-C++ construct",
28668 kwd->u.value);
28669 cp_parser_skip_to_end_of_block_or_statement (parser);
28672 return error_mark_node;
28675 /* Parse an Objective-C message expression.
28677 objc-message-expression:
28678 [ objc-message-receiver objc-message-args ]
28680 Returns a representation of an Objective-C message. */
28682 static tree
28683 cp_parser_objc_message_expression (cp_parser* parser)
28685 tree receiver, messageargs;
28687 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
28688 cp_lexer_consume_token (parser->lexer); /* Eat '['. */
28689 receiver = cp_parser_objc_message_receiver (parser);
28690 messageargs = cp_parser_objc_message_args (parser);
28691 location_t end_loc = cp_lexer_peek_token (parser->lexer)->location;
28692 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
28694 tree result = objc_build_message_expr (receiver, messageargs);
28696 /* Construct a location e.g.
28697 [self func1:5]
28698 ^~~~~~~~~~~~~~
28699 ranging from the '[' to the ']', with the caret at the start. */
28700 location_t combined_loc = make_location (start_loc, start_loc, end_loc);
28701 protected_set_expr_location (result, combined_loc);
28703 return result;
28706 /* Parse an objc-message-receiver.
28708 objc-message-receiver:
28709 expression
28710 simple-type-specifier
28712 Returns a representation of the type or expression. */
28714 static tree
28715 cp_parser_objc_message_receiver (cp_parser* parser)
28717 tree rcv;
28719 /* An Objective-C message receiver may be either (1) a type
28720 or (2) an expression. */
28721 cp_parser_parse_tentatively (parser);
28722 rcv = cp_parser_expression (parser);
28724 /* If that worked out, fine. */
28725 if (cp_parser_parse_definitely (parser))
28726 return rcv;
28728 cp_parser_parse_tentatively (parser);
28729 rcv = cp_parser_simple_type_specifier (parser,
28730 /*decl_specs=*/NULL,
28731 CP_PARSER_FLAGS_NONE);
28733 if (cp_parser_parse_definitely (parser))
28734 return objc_get_class_reference (rcv);
28736 cp_parser_error (parser, "objective-c++ message receiver expected");
28737 return error_mark_node;
28740 /* Parse the arguments and selectors comprising an Objective-C message.
28742 objc-message-args:
28743 objc-selector
28744 objc-selector-args
28745 objc-selector-args , objc-comma-args
28747 objc-selector-args:
28748 objc-selector [opt] : assignment-expression
28749 objc-selector-args objc-selector [opt] : assignment-expression
28751 objc-comma-args:
28752 assignment-expression
28753 objc-comma-args , assignment-expression
28755 Returns a TREE_LIST, with TREE_PURPOSE containing a list of
28756 selector arguments and TREE_VALUE containing a list of comma
28757 arguments. */
28759 static tree
28760 cp_parser_objc_message_args (cp_parser* parser)
28762 tree sel_args = NULL_TREE, addl_args = NULL_TREE;
28763 bool maybe_unary_selector_p = true;
28764 cp_token *token = cp_lexer_peek_token (parser->lexer);
28766 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
28768 tree selector = NULL_TREE, arg;
28770 if (token->type != CPP_COLON)
28771 selector = cp_parser_objc_selector (parser);
28773 /* Detect if we have a unary selector. */
28774 if (maybe_unary_selector_p
28775 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
28776 return build_tree_list (selector, NULL_TREE);
28778 maybe_unary_selector_p = false;
28779 cp_parser_require (parser, CPP_COLON, RT_COLON);
28780 arg = cp_parser_assignment_expression (parser);
28782 sel_args
28783 = chainon (sel_args,
28784 build_tree_list (selector, arg));
28786 token = cp_lexer_peek_token (parser->lexer);
28789 /* Handle non-selector arguments, if any. */
28790 while (token->type == CPP_COMMA)
28792 tree arg;
28794 cp_lexer_consume_token (parser->lexer);
28795 arg = cp_parser_assignment_expression (parser);
28797 addl_args
28798 = chainon (addl_args,
28799 build_tree_list (NULL_TREE, arg));
28801 token = cp_lexer_peek_token (parser->lexer);
28804 if (sel_args == NULL_TREE && addl_args == NULL_TREE)
28806 cp_parser_error (parser, "objective-c++ message argument(s) are expected");
28807 return build_tree_list (error_mark_node, error_mark_node);
28810 return build_tree_list (sel_args, addl_args);
28813 /* Parse an Objective-C encode expression.
28815 objc-encode-expression:
28816 @encode objc-typename
28818 Returns an encoded representation of the type argument. */
28820 static cp_expr
28821 cp_parser_objc_encode_expression (cp_parser* parser)
28823 tree type;
28824 cp_token *token;
28825 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
28827 cp_lexer_consume_token (parser->lexer); /* Eat '@encode'. */
28828 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
28829 token = cp_lexer_peek_token (parser->lexer);
28830 type = complete_type (cp_parser_type_id (parser));
28831 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
28833 if (!type)
28835 error_at (token->location,
28836 "%<@encode%> must specify a type as an argument");
28837 return error_mark_node;
28840 /* This happens if we find @encode(T) (where T is a template
28841 typename or something dependent on a template typename) when
28842 parsing a template. In that case, we can't compile it
28843 immediately, but we rather create an AT_ENCODE_EXPR which will
28844 need to be instantiated when the template is used.
28846 if (dependent_type_p (type))
28848 tree value = build_min (AT_ENCODE_EXPR, size_type_node, type);
28849 TREE_READONLY (value) = 1;
28850 return value;
28854 /* Build a location of the form:
28855 @encode(int)
28856 ^~~~~~~~~~~~
28857 with caret==start at the @ token, finishing at the close paren. */
28858 location_t combined_loc
28859 = make_location (start_loc, start_loc,
28860 cp_lexer_previous_token (parser->lexer)->location);
28862 return cp_expr (objc_build_encode_expr (type), combined_loc);
28865 /* Parse an Objective-C @defs expression. */
28867 static tree
28868 cp_parser_objc_defs_expression (cp_parser *parser)
28870 tree name;
28872 cp_lexer_consume_token (parser->lexer); /* Eat '@defs'. */
28873 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
28874 name = cp_parser_identifier (parser);
28875 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
28877 return objc_get_class_ivars (name);
28880 /* Parse an Objective-C protocol expression.
28882 objc-protocol-expression:
28883 @protocol ( identifier )
28885 Returns a representation of the protocol expression. */
28887 static tree
28888 cp_parser_objc_protocol_expression (cp_parser* parser)
28890 tree proto;
28891 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
28893 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
28894 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
28895 proto = cp_parser_identifier (parser);
28896 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
28898 /* Build a location of the form:
28899 @protocol(prot)
28900 ^~~~~~~~~~~~~~~
28901 with caret==start at the @ token, finishing at the close paren. */
28902 location_t combined_loc
28903 = make_location (start_loc, start_loc,
28904 cp_lexer_previous_token (parser->lexer)->location);
28905 tree result = objc_build_protocol_expr (proto);
28906 protected_set_expr_location (result, combined_loc);
28907 return result;
28910 /* Parse an Objective-C selector expression.
28912 objc-selector-expression:
28913 @selector ( objc-method-signature )
28915 objc-method-signature:
28916 objc-selector
28917 objc-selector-seq
28919 objc-selector-seq:
28920 objc-selector :
28921 objc-selector-seq objc-selector :
28923 Returns a representation of the method selector. */
28925 static tree
28926 cp_parser_objc_selector_expression (cp_parser* parser)
28928 tree sel_seq = NULL_TREE;
28929 bool maybe_unary_selector_p = true;
28930 cp_token *token;
28931 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
28933 cp_lexer_consume_token (parser->lexer); /* Eat '@selector'. */
28934 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
28935 token = cp_lexer_peek_token (parser->lexer);
28937 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON
28938 || token->type == CPP_SCOPE)
28940 tree selector = NULL_TREE;
28942 if (token->type != CPP_COLON
28943 || token->type == CPP_SCOPE)
28944 selector = cp_parser_objc_selector (parser);
28946 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON)
28947 && cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
28949 /* Detect if we have a unary selector. */
28950 if (maybe_unary_selector_p)
28952 sel_seq = selector;
28953 goto finish_selector;
28955 else
28957 cp_parser_error (parser, "expected %<:%>");
28960 maybe_unary_selector_p = false;
28961 token = cp_lexer_consume_token (parser->lexer);
28963 if (token->type == CPP_SCOPE)
28965 sel_seq
28966 = chainon (sel_seq,
28967 build_tree_list (selector, NULL_TREE));
28968 sel_seq
28969 = chainon (sel_seq,
28970 build_tree_list (NULL_TREE, NULL_TREE));
28972 else
28973 sel_seq
28974 = chainon (sel_seq,
28975 build_tree_list (selector, NULL_TREE));
28977 token = cp_lexer_peek_token (parser->lexer);
28980 finish_selector:
28981 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
28984 /* Build a location of the form:
28985 @selector(func)
28986 ^~~~~~~~~~~~~~~
28987 with caret==start at the @ token, finishing at the close paren. */
28988 location_t combined_loc
28989 = make_location (loc, loc,
28990 cp_lexer_previous_token (parser->lexer)->location);
28991 tree result = objc_build_selector_expr (combined_loc, sel_seq);
28992 /* TODO: objc_build_selector_expr doesn't always honor the location. */
28993 protected_set_expr_location (result, combined_loc);
28994 return result;
28997 /* Parse a list of identifiers.
28999 objc-identifier-list:
29000 identifier
29001 objc-identifier-list , identifier
29003 Returns a TREE_LIST of identifier nodes. */
29005 static tree
29006 cp_parser_objc_identifier_list (cp_parser* parser)
29008 tree identifier;
29009 tree list;
29010 cp_token *sep;
29012 identifier = cp_parser_identifier (parser);
29013 if (identifier == error_mark_node)
29014 return error_mark_node;
29016 list = build_tree_list (NULL_TREE, identifier);
29017 sep = cp_lexer_peek_token (parser->lexer);
29019 while (sep->type == CPP_COMMA)
29021 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
29022 identifier = cp_parser_identifier (parser);
29023 if (identifier == error_mark_node)
29024 return list;
29026 list = chainon (list, build_tree_list (NULL_TREE,
29027 identifier));
29028 sep = cp_lexer_peek_token (parser->lexer);
29031 return list;
29034 /* Parse an Objective-C alias declaration.
29036 objc-alias-declaration:
29037 @compatibility_alias identifier identifier ;
29039 This function registers the alias mapping with the Objective-C front end.
29040 It returns nothing. */
29042 static void
29043 cp_parser_objc_alias_declaration (cp_parser* parser)
29045 tree alias, orig;
29047 cp_lexer_consume_token (parser->lexer); /* Eat '@compatibility_alias'. */
29048 alias = cp_parser_identifier (parser);
29049 orig = cp_parser_identifier (parser);
29050 objc_declare_alias (alias, orig);
29051 cp_parser_consume_semicolon_at_end_of_statement (parser);
29054 /* Parse an Objective-C class forward-declaration.
29056 objc-class-declaration:
29057 @class objc-identifier-list ;
29059 The function registers the forward declarations with the Objective-C
29060 front end. It returns nothing. */
29062 static void
29063 cp_parser_objc_class_declaration (cp_parser* parser)
29065 cp_lexer_consume_token (parser->lexer); /* Eat '@class'. */
29066 while (true)
29068 tree id;
29070 id = cp_parser_identifier (parser);
29071 if (id == error_mark_node)
29072 break;
29074 objc_declare_class (id);
29076 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
29077 cp_lexer_consume_token (parser->lexer);
29078 else
29079 break;
29081 cp_parser_consume_semicolon_at_end_of_statement (parser);
29084 /* Parse a list of Objective-C protocol references.
29086 objc-protocol-refs-opt:
29087 objc-protocol-refs [opt]
29089 objc-protocol-refs:
29090 < objc-identifier-list >
29092 Returns a TREE_LIST of identifiers, if any. */
29094 static tree
29095 cp_parser_objc_protocol_refs_opt (cp_parser* parser)
29097 tree protorefs = NULL_TREE;
29099 if(cp_lexer_next_token_is (parser->lexer, CPP_LESS))
29101 cp_lexer_consume_token (parser->lexer); /* Eat '<'. */
29102 protorefs = cp_parser_objc_identifier_list (parser);
29103 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
29106 return protorefs;
29109 /* Parse a Objective-C visibility specification. */
29111 static void
29112 cp_parser_objc_visibility_spec (cp_parser* parser)
29114 cp_token *vis = cp_lexer_peek_token (parser->lexer);
29116 switch (vis->keyword)
29118 case RID_AT_PRIVATE:
29119 objc_set_visibility (OBJC_IVAR_VIS_PRIVATE);
29120 break;
29121 case RID_AT_PROTECTED:
29122 objc_set_visibility (OBJC_IVAR_VIS_PROTECTED);
29123 break;
29124 case RID_AT_PUBLIC:
29125 objc_set_visibility (OBJC_IVAR_VIS_PUBLIC);
29126 break;
29127 case RID_AT_PACKAGE:
29128 objc_set_visibility (OBJC_IVAR_VIS_PACKAGE);
29129 break;
29130 default:
29131 return;
29134 /* Eat '@private'/'@protected'/'@public'. */
29135 cp_lexer_consume_token (parser->lexer);
29138 /* Parse an Objective-C method type. Return 'true' if it is a class
29139 (+) method, and 'false' if it is an instance (-) method. */
29141 static inline bool
29142 cp_parser_objc_method_type (cp_parser* parser)
29144 if (cp_lexer_consume_token (parser->lexer)->type == CPP_PLUS)
29145 return true;
29146 else
29147 return false;
29150 /* Parse an Objective-C protocol qualifier. */
29152 static tree
29153 cp_parser_objc_protocol_qualifiers (cp_parser* parser)
29155 tree quals = NULL_TREE, node;
29156 cp_token *token = cp_lexer_peek_token (parser->lexer);
29158 node = token->u.value;
29160 while (node && identifier_p (node)
29161 && (node == ridpointers [(int) RID_IN]
29162 || node == ridpointers [(int) RID_OUT]
29163 || node == ridpointers [(int) RID_INOUT]
29164 || node == ridpointers [(int) RID_BYCOPY]
29165 || node == ridpointers [(int) RID_BYREF]
29166 || node == ridpointers [(int) RID_ONEWAY]))
29168 quals = tree_cons (NULL_TREE, node, quals);
29169 cp_lexer_consume_token (parser->lexer);
29170 token = cp_lexer_peek_token (parser->lexer);
29171 node = token->u.value;
29174 return quals;
29177 /* Parse an Objective-C typename. */
29179 static tree
29180 cp_parser_objc_typename (cp_parser* parser)
29182 tree type_name = NULL_TREE;
29184 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
29186 tree proto_quals, cp_type = NULL_TREE;
29188 cp_lexer_consume_token (parser->lexer); /* Eat '('. */
29189 proto_quals = cp_parser_objc_protocol_qualifiers (parser);
29191 /* An ObjC type name may consist of just protocol qualifiers, in which
29192 case the type shall default to 'id'. */
29193 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
29195 cp_type = cp_parser_type_id (parser);
29197 /* If the type could not be parsed, an error has already
29198 been produced. For error recovery, behave as if it had
29199 not been specified, which will use the default type
29200 'id'. */
29201 if (cp_type == error_mark_node)
29203 cp_type = NULL_TREE;
29204 /* We need to skip to the closing parenthesis as
29205 cp_parser_type_id() does not seem to do it for
29206 us. */
29207 cp_parser_skip_to_closing_parenthesis (parser,
29208 /*recovering=*/true,
29209 /*or_comma=*/false,
29210 /*consume_paren=*/false);
29214 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
29215 type_name = build_tree_list (proto_quals, cp_type);
29218 return type_name;
29221 /* Check to see if TYPE refers to an Objective-C selector name. */
29223 static bool
29224 cp_parser_objc_selector_p (enum cpp_ttype type)
29226 return (type == CPP_NAME || type == CPP_KEYWORD
29227 || type == CPP_AND_AND || type == CPP_AND_EQ || type == CPP_AND
29228 || type == CPP_OR || type == CPP_COMPL || type == CPP_NOT
29229 || type == CPP_NOT_EQ || type == CPP_OR_OR || type == CPP_OR_EQ
29230 || type == CPP_XOR || type == CPP_XOR_EQ);
29233 /* Parse an Objective-C selector. */
29235 static tree
29236 cp_parser_objc_selector (cp_parser* parser)
29238 cp_token *token = cp_lexer_consume_token (parser->lexer);
29240 if (!cp_parser_objc_selector_p (token->type))
29242 error_at (token->location, "invalid Objective-C++ selector name");
29243 return error_mark_node;
29246 /* C++ operator names are allowed to appear in ObjC selectors. */
29247 switch (token->type)
29249 case CPP_AND_AND: return get_identifier ("and");
29250 case CPP_AND_EQ: return get_identifier ("and_eq");
29251 case CPP_AND: return get_identifier ("bitand");
29252 case CPP_OR: return get_identifier ("bitor");
29253 case CPP_COMPL: return get_identifier ("compl");
29254 case CPP_NOT: return get_identifier ("not");
29255 case CPP_NOT_EQ: return get_identifier ("not_eq");
29256 case CPP_OR_OR: return get_identifier ("or");
29257 case CPP_OR_EQ: return get_identifier ("or_eq");
29258 case CPP_XOR: return get_identifier ("xor");
29259 case CPP_XOR_EQ: return get_identifier ("xor_eq");
29260 default: return token->u.value;
29264 /* Parse an Objective-C params list. */
29266 static tree
29267 cp_parser_objc_method_keyword_params (cp_parser* parser, tree* attributes)
29269 tree params = NULL_TREE;
29270 bool maybe_unary_selector_p = true;
29271 cp_token *token = cp_lexer_peek_token (parser->lexer);
29273 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
29275 tree selector = NULL_TREE, type_name, identifier;
29276 tree parm_attr = NULL_TREE;
29278 if (token->keyword == RID_ATTRIBUTE)
29279 break;
29281 if (token->type != CPP_COLON)
29282 selector = cp_parser_objc_selector (parser);
29284 /* Detect if we have a unary selector. */
29285 if (maybe_unary_selector_p
29286 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
29288 params = selector; /* Might be followed by attributes. */
29289 break;
29292 maybe_unary_selector_p = false;
29293 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
29295 /* Something went quite wrong. There should be a colon
29296 here, but there is not. Stop parsing parameters. */
29297 break;
29299 type_name = cp_parser_objc_typename (parser);
29300 /* New ObjC allows attributes on parameters too. */
29301 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
29302 parm_attr = cp_parser_attributes_opt (parser);
29303 identifier = cp_parser_identifier (parser);
29305 params
29306 = chainon (params,
29307 objc_build_keyword_decl (selector,
29308 type_name,
29309 identifier,
29310 parm_attr));
29312 token = cp_lexer_peek_token (parser->lexer);
29315 if (params == NULL_TREE)
29317 cp_parser_error (parser, "objective-c++ method declaration is expected");
29318 return error_mark_node;
29321 /* We allow tail attributes for the method. */
29322 if (token->keyword == RID_ATTRIBUTE)
29324 *attributes = cp_parser_attributes_opt (parser);
29325 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
29326 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
29327 return params;
29328 cp_parser_error (parser,
29329 "method attributes must be specified at the end");
29330 return error_mark_node;
29333 if (params == NULL_TREE)
29335 cp_parser_error (parser, "objective-c++ method declaration is expected");
29336 return error_mark_node;
29338 return params;
29341 /* Parse the non-keyword Objective-C params. */
29343 static tree
29344 cp_parser_objc_method_tail_params_opt (cp_parser* parser, bool *ellipsisp,
29345 tree* attributes)
29347 tree params = make_node (TREE_LIST);
29348 cp_token *token = cp_lexer_peek_token (parser->lexer);
29349 *ellipsisp = false; /* Initially, assume no ellipsis. */
29351 while (token->type == CPP_COMMA)
29353 cp_parameter_declarator *parmdecl;
29354 tree parm;
29356 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
29357 token = cp_lexer_peek_token (parser->lexer);
29359 if (token->type == CPP_ELLIPSIS)
29361 cp_lexer_consume_token (parser->lexer); /* Eat '...'. */
29362 *ellipsisp = true;
29363 token = cp_lexer_peek_token (parser->lexer);
29364 break;
29367 /* TODO: parse attributes for tail parameters. */
29368 parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
29369 parm = grokdeclarator (parmdecl->declarator,
29370 &parmdecl->decl_specifiers,
29371 PARM, /*initialized=*/0,
29372 /*attrlist=*/NULL);
29374 chainon (params, build_tree_list (NULL_TREE, parm));
29375 token = cp_lexer_peek_token (parser->lexer);
29378 /* We allow tail attributes for the method. */
29379 if (token->keyword == RID_ATTRIBUTE)
29381 if (*attributes == NULL_TREE)
29383 *attributes = cp_parser_attributes_opt (parser);
29384 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
29385 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
29386 return params;
29388 else
29389 /* We have an error, but parse the attributes, so that we can
29390 carry on. */
29391 *attributes = cp_parser_attributes_opt (parser);
29393 cp_parser_error (parser,
29394 "method attributes must be specified at the end");
29395 return error_mark_node;
29398 return params;
29401 /* Parse a linkage specification, a pragma, an extra semicolon or a block. */
29403 static void
29404 cp_parser_objc_interstitial_code (cp_parser* parser)
29406 cp_token *token = cp_lexer_peek_token (parser->lexer);
29408 /* If the next token is `extern' and the following token is a string
29409 literal, then we have a linkage specification. */
29410 if (token->keyword == RID_EXTERN
29411 && cp_parser_is_pure_string_literal
29412 (cp_lexer_peek_nth_token (parser->lexer, 2)))
29413 cp_parser_linkage_specification (parser);
29414 /* Handle #pragma, if any. */
29415 else if (token->type == CPP_PRAGMA)
29416 cp_parser_pragma (parser, pragma_objc_icode, NULL);
29417 /* Allow stray semicolons. */
29418 else if (token->type == CPP_SEMICOLON)
29419 cp_lexer_consume_token (parser->lexer);
29420 /* Mark methods as optional or required, when building protocols. */
29421 else if (token->keyword == RID_AT_OPTIONAL)
29423 cp_lexer_consume_token (parser->lexer);
29424 objc_set_method_opt (true);
29426 else if (token->keyword == RID_AT_REQUIRED)
29428 cp_lexer_consume_token (parser->lexer);
29429 objc_set_method_opt (false);
29431 else if (token->keyword == RID_NAMESPACE)
29432 cp_parser_namespace_definition (parser);
29433 /* Other stray characters must generate errors. */
29434 else if (token->type == CPP_OPEN_BRACE || token->type == CPP_CLOSE_BRACE)
29436 cp_lexer_consume_token (parser->lexer);
29437 error ("stray %qs between Objective-C++ methods",
29438 token->type == CPP_OPEN_BRACE ? "{" : "}");
29440 /* Finally, try to parse a block-declaration, or a function-definition. */
29441 else
29442 cp_parser_block_declaration (parser, /*statement_p=*/false);
29445 /* Parse a method signature. */
29447 static tree
29448 cp_parser_objc_method_signature (cp_parser* parser, tree* attributes)
29450 tree rettype, kwdparms, optparms;
29451 bool ellipsis = false;
29452 bool is_class_method;
29454 is_class_method = cp_parser_objc_method_type (parser);
29455 rettype = cp_parser_objc_typename (parser);
29456 *attributes = NULL_TREE;
29457 kwdparms = cp_parser_objc_method_keyword_params (parser, attributes);
29458 if (kwdparms == error_mark_node)
29459 return error_mark_node;
29460 optparms = cp_parser_objc_method_tail_params_opt (parser, &ellipsis, attributes);
29461 if (optparms == error_mark_node)
29462 return error_mark_node;
29464 return objc_build_method_signature (is_class_method, rettype, kwdparms, optparms, ellipsis);
29467 static bool
29468 cp_parser_objc_method_maybe_bad_prefix_attributes (cp_parser* parser)
29470 tree tattr;
29471 cp_lexer_save_tokens (parser->lexer);
29472 tattr = cp_parser_attributes_opt (parser);
29473 gcc_assert (tattr) ;
29475 /* If the attributes are followed by a method introducer, this is not allowed.
29476 Dump the attributes and flag the situation. */
29477 if (cp_lexer_next_token_is (parser->lexer, CPP_PLUS)
29478 || cp_lexer_next_token_is (parser->lexer, CPP_MINUS))
29479 return true;
29481 /* Otherwise, the attributes introduce some interstitial code, possibly so
29482 rewind to allow that check. */
29483 cp_lexer_rollback_tokens (parser->lexer);
29484 return false;
29487 /* Parse an Objective-C method prototype list. */
29489 static void
29490 cp_parser_objc_method_prototype_list (cp_parser* parser)
29492 cp_token *token = cp_lexer_peek_token (parser->lexer);
29494 while (token->keyword != RID_AT_END && token->type != CPP_EOF)
29496 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
29498 tree attributes, sig;
29499 bool is_class_method;
29500 if (token->type == CPP_PLUS)
29501 is_class_method = true;
29502 else
29503 is_class_method = false;
29504 sig = cp_parser_objc_method_signature (parser, &attributes);
29505 if (sig == error_mark_node)
29507 cp_parser_skip_to_end_of_block_or_statement (parser);
29508 token = cp_lexer_peek_token (parser->lexer);
29509 continue;
29511 objc_add_method_declaration (is_class_method, sig, attributes);
29512 cp_parser_consume_semicolon_at_end_of_statement (parser);
29514 else if (token->keyword == RID_AT_PROPERTY)
29515 cp_parser_objc_at_property_declaration (parser);
29516 else if (token->keyword == RID_ATTRIBUTE
29517 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
29518 warning_at (cp_lexer_peek_token (parser->lexer)->location,
29519 OPT_Wattributes,
29520 "prefix attributes are ignored for methods");
29521 else
29522 /* Allow for interspersed non-ObjC++ code. */
29523 cp_parser_objc_interstitial_code (parser);
29525 token = cp_lexer_peek_token (parser->lexer);
29528 if (token->type != CPP_EOF)
29529 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
29530 else
29531 cp_parser_error (parser, "expected %<@end%>");
29533 objc_finish_interface ();
29536 /* Parse an Objective-C method definition list. */
29538 static void
29539 cp_parser_objc_method_definition_list (cp_parser* parser)
29541 cp_token *token = cp_lexer_peek_token (parser->lexer);
29543 while (token->keyword != RID_AT_END && token->type != CPP_EOF)
29545 tree meth;
29547 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
29549 cp_token *ptk;
29550 tree sig, attribute;
29551 bool is_class_method;
29552 if (token->type == CPP_PLUS)
29553 is_class_method = true;
29554 else
29555 is_class_method = false;
29556 push_deferring_access_checks (dk_deferred);
29557 sig = cp_parser_objc_method_signature (parser, &attribute);
29558 if (sig == error_mark_node)
29560 cp_parser_skip_to_end_of_block_or_statement (parser);
29561 token = cp_lexer_peek_token (parser->lexer);
29562 continue;
29564 objc_start_method_definition (is_class_method, sig, attribute,
29565 NULL_TREE);
29567 /* For historical reasons, we accept an optional semicolon. */
29568 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
29569 cp_lexer_consume_token (parser->lexer);
29571 ptk = cp_lexer_peek_token (parser->lexer);
29572 if (!(ptk->type == CPP_PLUS || ptk->type == CPP_MINUS
29573 || ptk->type == CPP_EOF || ptk->keyword == RID_AT_END))
29575 perform_deferred_access_checks (tf_warning_or_error);
29576 stop_deferring_access_checks ();
29577 meth = cp_parser_function_definition_after_declarator (parser,
29578 false);
29579 pop_deferring_access_checks ();
29580 objc_finish_method_definition (meth);
29583 /* The following case will be removed once @synthesize is
29584 completely implemented. */
29585 else if (token->keyword == RID_AT_PROPERTY)
29586 cp_parser_objc_at_property_declaration (parser);
29587 else if (token->keyword == RID_AT_SYNTHESIZE)
29588 cp_parser_objc_at_synthesize_declaration (parser);
29589 else if (token->keyword == RID_AT_DYNAMIC)
29590 cp_parser_objc_at_dynamic_declaration (parser);
29591 else if (token->keyword == RID_ATTRIBUTE
29592 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
29593 warning_at (token->location, OPT_Wattributes,
29594 "prefix attributes are ignored for methods");
29595 else
29596 /* Allow for interspersed non-ObjC++ code. */
29597 cp_parser_objc_interstitial_code (parser);
29599 token = cp_lexer_peek_token (parser->lexer);
29602 if (token->type != CPP_EOF)
29603 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
29604 else
29605 cp_parser_error (parser, "expected %<@end%>");
29607 objc_finish_implementation ();
29610 /* Parse Objective-C ivars. */
29612 static void
29613 cp_parser_objc_class_ivars (cp_parser* parser)
29615 cp_token *token = cp_lexer_peek_token (parser->lexer);
29617 if (token->type != CPP_OPEN_BRACE)
29618 return; /* No ivars specified. */
29620 cp_lexer_consume_token (parser->lexer); /* Eat '{'. */
29621 token = cp_lexer_peek_token (parser->lexer);
29623 while (token->type != CPP_CLOSE_BRACE
29624 && token->keyword != RID_AT_END && token->type != CPP_EOF)
29626 cp_decl_specifier_seq declspecs;
29627 int decl_class_or_enum_p;
29628 tree prefix_attributes;
29630 cp_parser_objc_visibility_spec (parser);
29632 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
29633 break;
29635 cp_parser_decl_specifier_seq (parser,
29636 CP_PARSER_FLAGS_OPTIONAL,
29637 &declspecs,
29638 &decl_class_or_enum_p);
29640 /* auto, register, static, extern, mutable. */
29641 if (declspecs.storage_class != sc_none)
29643 cp_parser_error (parser, "invalid type for instance variable");
29644 declspecs.storage_class = sc_none;
29647 /* thread_local. */
29648 if (decl_spec_seq_has_spec_p (&declspecs, ds_thread))
29650 cp_parser_error (parser, "invalid type for instance variable");
29651 declspecs.locations[ds_thread] = 0;
29654 /* typedef. */
29655 if (decl_spec_seq_has_spec_p (&declspecs, ds_typedef))
29657 cp_parser_error (parser, "invalid type for instance variable");
29658 declspecs.locations[ds_typedef] = 0;
29661 prefix_attributes = declspecs.attributes;
29662 declspecs.attributes = NULL_TREE;
29664 /* Keep going until we hit the `;' at the end of the
29665 declaration. */
29666 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
29668 tree width = NULL_TREE, attributes, first_attribute, decl;
29669 cp_declarator *declarator = NULL;
29670 int ctor_dtor_or_conv_p;
29672 /* Check for a (possibly unnamed) bitfield declaration. */
29673 token = cp_lexer_peek_token (parser->lexer);
29674 if (token->type == CPP_COLON)
29675 goto eat_colon;
29677 if (token->type == CPP_NAME
29678 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
29679 == CPP_COLON))
29681 /* Get the name of the bitfield. */
29682 declarator = make_id_declarator (NULL_TREE,
29683 cp_parser_identifier (parser),
29684 sfk_none);
29686 eat_colon:
29687 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
29688 /* Get the width of the bitfield. */
29689 width
29690 = cp_parser_constant_expression (parser);
29692 else
29694 /* Parse the declarator. */
29695 declarator
29696 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
29697 &ctor_dtor_or_conv_p,
29698 /*parenthesized_p=*/NULL,
29699 /*member_p=*/false,
29700 /*friend_p=*/false);
29703 /* Look for attributes that apply to the ivar. */
29704 attributes = cp_parser_attributes_opt (parser);
29705 /* Remember which attributes are prefix attributes and
29706 which are not. */
29707 first_attribute = attributes;
29708 /* Combine the attributes. */
29709 attributes = chainon (prefix_attributes, attributes);
29711 if (width)
29712 /* Create the bitfield declaration. */
29713 decl = grokbitfield (declarator, &declspecs,
29714 width,
29715 attributes);
29716 else
29717 decl = grokfield (declarator, &declspecs,
29718 NULL_TREE, /*init_const_expr_p=*/false,
29719 NULL_TREE, attributes);
29721 /* Add the instance variable. */
29722 if (decl != error_mark_node && decl != NULL_TREE)
29723 objc_add_instance_variable (decl);
29725 /* Reset PREFIX_ATTRIBUTES. */
29726 while (attributes && TREE_CHAIN (attributes) != first_attribute)
29727 attributes = TREE_CHAIN (attributes);
29728 if (attributes)
29729 TREE_CHAIN (attributes) = NULL_TREE;
29731 token = cp_lexer_peek_token (parser->lexer);
29733 if (token->type == CPP_COMMA)
29735 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
29736 continue;
29738 break;
29741 cp_parser_consume_semicolon_at_end_of_statement (parser);
29742 token = cp_lexer_peek_token (parser->lexer);
29745 if (token->keyword == RID_AT_END)
29746 cp_parser_error (parser, "expected %<}%>");
29748 /* Do not consume the RID_AT_END, so it will be read again as terminating
29749 the @interface of @implementation. */
29750 if (token->keyword != RID_AT_END && token->type != CPP_EOF)
29751 cp_lexer_consume_token (parser->lexer); /* Eat '}'. */
29753 /* For historical reasons, we accept an optional semicolon. */
29754 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
29755 cp_lexer_consume_token (parser->lexer);
29758 /* Parse an Objective-C protocol declaration. */
29760 static void
29761 cp_parser_objc_protocol_declaration (cp_parser* parser, tree attributes)
29763 tree proto, protorefs;
29764 cp_token *tok;
29766 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
29767 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
29769 tok = cp_lexer_peek_token (parser->lexer);
29770 error_at (tok->location, "identifier expected after %<@protocol%>");
29771 cp_parser_consume_semicolon_at_end_of_statement (parser);
29772 return;
29775 /* See if we have a forward declaration or a definition. */
29776 tok = cp_lexer_peek_nth_token (parser->lexer, 2);
29778 /* Try a forward declaration first. */
29779 if (tok->type == CPP_COMMA || tok->type == CPP_SEMICOLON)
29781 while (true)
29783 tree id;
29785 id = cp_parser_identifier (parser);
29786 if (id == error_mark_node)
29787 break;
29789 objc_declare_protocol (id, attributes);
29791 if(cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
29792 cp_lexer_consume_token (parser->lexer);
29793 else
29794 break;
29796 cp_parser_consume_semicolon_at_end_of_statement (parser);
29799 /* Ok, we got a full-fledged definition (or at least should). */
29800 else
29802 proto = cp_parser_identifier (parser);
29803 protorefs = cp_parser_objc_protocol_refs_opt (parser);
29804 objc_start_protocol (proto, protorefs, attributes);
29805 cp_parser_objc_method_prototype_list (parser);
29809 /* Parse an Objective-C superclass or category. */
29811 static void
29812 cp_parser_objc_superclass_or_category (cp_parser *parser,
29813 bool iface_p,
29814 tree *super,
29815 tree *categ, bool *is_class_extension)
29817 cp_token *next = cp_lexer_peek_token (parser->lexer);
29819 *super = *categ = NULL_TREE;
29820 *is_class_extension = false;
29821 if (next->type == CPP_COLON)
29823 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
29824 *super = cp_parser_identifier (parser);
29826 else if (next->type == CPP_OPEN_PAREN)
29828 cp_lexer_consume_token (parser->lexer); /* Eat '('. */
29830 /* If there is no category name, and this is an @interface, we
29831 have a class extension. */
29832 if (iface_p && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
29834 *categ = NULL_TREE;
29835 *is_class_extension = true;
29837 else
29838 *categ = cp_parser_identifier (parser);
29840 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
29844 /* Parse an Objective-C class interface. */
29846 static void
29847 cp_parser_objc_class_interface (cp_parser* parser, tree attributes)
29849 tree name, super, categ, protos;
29850 bool is_class_extension;
29852 cp_lexer_consume_token (parser->lexer); /* Eat '@interface'. */
29853 name = cp_parser_identifier (parser);
29854 if (name == error_mark_node)
29856 /* It's hard to recover because even if valid @interface stuff
29857 is to follow, we can't compile it (or validate it) if we
29858 don't even know which class it refers to. Let's assume this
29859 was a stray '@interface' token in the stream and skip it.
29861 return;
29863 cp_parser_objc_superclass_or_category (parser, true, &super, &categ,
29864 &is_class_extension);
29865 protos = cp_parser_objc_protocol_refs_opt (parser);
29867 /* We have either a class or a category on our hands. */
29868 if (categ || is_class_extension)
29869 objc_start_category_interface (name, categ, protos, attributes);
29870 else
29872 objc_start_class_interface (name, super, protos, attributes);
29873 /* Handle instance variable declarations, if any. */
29874 cp_parser_objc_class_ivars (parser);
29875 objc_continue_interface ();
29878 cp_parser_objc_method_prototype_list (parser);
29881 /* Parse an Objective-C class implementation. */
29883 static void
29884 cp_parser_objc_class_implementation (cp_parser* parser)
29886 tree name, super, categ;
29887 bool is_class_extension;
29889 cp_lexer_consume_token (parser->lexer); /* Eat '@implementation'. */
29890 name = cp_parser_identifier (parser);
29891 if (name == error_mark_node)
29893 /* It's hard to recover because even if valid @implementation
29894 stuff is to follow, we can't compile it (or validate it) if
29895 we don't even know which class it refers to. Let's assume
29896 this was a stray '@implementation' token in the stream and
29897 skip it.
29899 return;
29901 cp_parser_objc_superclass_or_category (parser, false, &super, &categ,
29902 &is_class_extension);
29904 /* We have either a class or a category on our hands. */
29905 if (categ)
29906 objc_start_category_implementation (name, categ);
29907 else
29909 objc_start_class_implementation (name, super);
29910 /* Handle instance variable declarations, if any. */
29911 cp_parser_objc_class_ivars (parser);
29912 objc_continue_implementation ();
29915 cp_parser_objc_method_definition_list (parser);
29918 /* Consume the @end token and finish off the implementation. */
29920 static void
29921 cp_parser_objc_end_implementation (cp_parser* parser)
29923 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
29924 objc_finish_implementation ();
29927 /* Parse an Objective-C declaration. */
29929 static void
29930 cp_parser_objc_declaration (cp_parser* parser, tree attributes)
29932 /* Try to figure out what kind of declaration is present. */
29933 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
29935 if (attributes)
29936 switch (kwd->keyword)
29938 case RID_AT_ALIAS:
29939 case RID_AT_CLASS:
29940 case RID_AT_END:
29941 error_at (kwd->location, "attributes may not be specified before"
29942 " the %<@%D%> Objective-C++ keyword",
29943 kwd->u.value);
29944 attributes = NULL;
29945 break;
29946 case RID_AT_IMPLEMENTATION:
29947 warning_at (kwd->location, OPT_Wattributes,
29948 "prefix attributes are ignored before %<@%D%>",
29949 kwd->u.value);
29950 attributes = NULL;
29951 default:
29952 break;
29955 switch (kwd->keyword)
29957 case RID_AT_ALIAS:
29958 cp_parser_objc_alias_declaration (parser);
29959 break;
29960 case RID_AT_CLASS:
29961 cp_parser_objc_class_declaration (parser);
29962 break;
29963 case RID_AT_PROTOCOL:
29964 cp_parser_objc_protocol_declaration (parser, attributes);
29965 break;
29966 case RID_AT_INTERFACE:
29967 cp_parser_objc_class_interface (parser, attributes);
29968 break;
29969 case RID_AT_IMPLEMENTATION:
29970 cp_parser_objc_class_implementation (parser);
29971 break;
29972 case RID_AT_END:
29973 cp_parser_objc_end_implementation (parser);
29974 break;
29975 default:
29976 error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
29977 kwd->u.value);
29978 cp_parser_skip_to_end_of_block_or_statement (parser);
29982 /* Parse an Objective-C try-catch-finally statement.
29984 objc-try-catch-finally-stmt:
29985 @try compound-statement objc-catch-clause-seq [opt]
29986 objc-finally-clause [opt]
29988 objc-catch-clause-seq:
29989 objc-catch-clause objc-catch-clause-seq [opt]
29991 objc-catch-clause:
29992 @catch ( objc-exception-declaration ) compound-statement
29994 objc-finally-clause:
29995 @finally compound-statement
29997 objc-exception-declaration:
29998 parameter-declaration
29999 '...'
30001 where '...' is to be interpreted literally, that is, it means CPP_ELLIPSIS.
30003 Returns NULL_TREE.
30005 PS: This function is identical to c_parser_objc_try_catch_finally_statement
30006 for C. Keep them in sync. */
30008 static tree
30009 cp_parser_objc_try_catch_finally_statement (cp_parser *parser)
30011 location_t location;
30012 tree stmt;
30014 cp_parser_require_keyword (parser, RID_AT_TRY, RT_AT_TRY);
30015 location = cp_lexer_peek_token (parser->lexer)->location;
30016 objc_maybe_warn_exceptions (location);
30017 /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
30018 node, lest it get absorbed into the surrounding block. */
30019 stmt = push_stmt_list ();
30020 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
30021 objc_begin_try_stmt (location, pop_stmt_list (stmt));
30023 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_CATCH))
30025 cp_parameter_declarator *parm;
30026 tree parameter_declaration = error_mark_node;
30027 bool seen_open_paren = false;
30029 cp_lexer_consume_token (parser->lexer);
30030 if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
30031 seen_open_paren = true;
30032 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
30034 /* We have "@catch (...)" (where the '...' are literally
30035 what is in the code). Skip the '...'.
30036 parameter_declaration is set to NULL_TREE, and
30037 objc_being_catch_clauses() knows that that means
30038 '...'. */
30039 cp_lexer_consume_token (parser->lexer);
30040 parameter_declaration = NULL_TREE;
30042 else
30044 /* We have "@catch (NSException *exception)" or something
30045 like that. Parse the parameter declaration. */
30046 parm = cp_parser_parameter_declaration (parser, false, NULL);
30047 if (parm == NULL)
30048 parameter_declaration = error_mark_node;
30049 else
30050 parameter_declaration = grokdeclarator (parm->declarator,
30051 &parm->decl_specifiers,
30052 PARM, /*initialized=*/0,
30053 /*attrlist=*/NULL);
30055 if (seen_open_paren)
30056 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
30057 else
30059 /* If there was no open parenthesis, we are recovering from
30060 an error, and we are trying to figure out what mistake
30061 the user has made. */
30063 /* If there is an immediate closing parenthesis, the user
30064 probably forgot the opening one (ie, they typed "@catch
30065 NSException *e)". Parse the closing parenthesis and keep
30066 going. */
30067 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
30068 cp_lexer_consume_token (parser->lexer);
30070 /* If these is no immediate closing parenthesis, the user
30071 probably doesn't know that parenthesis are required at
30072 all (ie, they typed "@catch NSException *e"). So, just
30073 forget about the closing parenthesis and keep going. */
30075 objc_begin_catch_clause (parameter_declaration);
30076 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
30077 objc_finish_catch_clause ();
30079 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_FINALLY))
30081 cp_lexer_consume_token (parser->lexer);
30082 location = cp_lexer_peek_token (parser->lexer)->location;
30083 /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
30084 node, lest it get absorbed into the surrounding block. */
30085 stmt = push_stmt_list ();
30086 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
30087 objc_build_finally_clause (location, pop_stmt_list (stmt));
30090 return objc_finish_try_stmt ();
30093 /* Parse an Objective-C synchronized statement.
30095 objc-synchronized-stmt:
30096 @synchronized ( expression ) compound-statement
30098 Returns NULL_TREE. */
30100 static tree
30101 cp_parser_objc_synchronized_statement (cp_parser *parser)
30103 location_t location;
30104 tree lock, stmt;
30106 cp_parser_require_keyword (parser, RID_AT_SYNCHRONIZED, RT_AT_SYNCHRONIZED);
30108 location = cp_lexer_peek_token (parser->lexer)->location;
30109 objc_maybe_warn_exceptions (location);
30110 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
30111 lock = cp_parser_expression (parser);
30112 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
30114 /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
30115 node, lest it get absorbed into the surrounding block. */
30116 stmt = push_stmt_list ();
30117 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
30119 return objc_build_synchronized (location, lock, pop_stmt_list (stmt));
30122 /* Parse an Objective-C throw statement.
30124 objc-throw-stmt:
30125 @throw assignment-expression [opt] ;
30127 Returns a constructed '@throw' statement. */
30129 static tree
30130 cp_parser_objc_throw_statement (cp_parser *parser)
30132 tree expr = NULL_TREE;
30133 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
30135 cp_parser_require_keyword (parser, RID_AT_THROW, RT_AT_THROW);
30137 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
30138 expr = cp_parser_expression (parser);
30140 cp_parser_consume_semicolon_at_end_of_statement (parser);
30142 return objc_build_throw_stmt (loc, expr);
30145 /* Parse an Objective-C statement. */
30147 static tree
30148 cp_parser_objc_statement (cp_parser * parser)
30150 /* Try to figure out what kind of declaration is present. */
30151 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
30153 switch (kwd->keyword)
30155 case RID_AT_TRY:
30156 return cp_parser_objc_try_catch_finally_statement (parser);
30157 case RID_AT_SYNCHRONIZED:
30158 return cp_parser_objc_synchronized_statement (parser);
30159 case RID_AT_THROW:
30160 return cp_parser_objc_throw_statement (parser);
30161 default:
30162 error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
30163 kwd->u.value);
30164 cp_parser_skip_to_end_of_block_or_statement (parser);
30167 return error_mark_node;
30170 /* If we are compiling ObjC++ and we see an __attribute__ we neeed to
30171 look ahead to see if an objc keyword follows the attributes. This
30172 is to detect the use of prefix attributes on ObjC @interface and
30173 @protocol. */
30175 static bool
30176 cp_parser_objc_valid_prefix_attributes (cp_parser* parser, tree *attrib)
30178 cp_lexer_save_tokens (parser->lexer);
30179 *attrib = cp_parser_attributes_opt (parser);
30180 gcc_assert (*attrib);
30181 if (OBJC_IS_AT_KEYWORD (cp_lexer_peek_token (parser->lexer)->keyword))
30183 cp_lexer_commit_tokens (parser->lexer);
30184 return true;
30186 cp_lexer_rollback_tokens (parser->lexer);
30187 return false;
30190 /* This routine is a minimal replacement for
30191 c_parser_struct_declaration () used when parsing the list of
30192 types/names or ObjC++ properties. For example, when parsing the
30193 code
30195 @property (readonly) int a, b, c;
30197 this function is responsible for parsing "int a, int b, int c" and
30198 returning the declarations as CHAIN of DECLs.
30200 TODO: Share this code with cp_parser_objc_class_ivars. It's very
30201 similar parsing. */
30202 static tree
30203 cp_parser_objc_struct_declaration (cp_parser *parser)
30205 tree decls = NULL_TREE;
30206 cp_decl_specifier_seq declspecs;
30207 int decl_class_or_enum_p;
30208 tree prefix_attributes;
30210 cp_parser_decl_specifier_seq (parser,
30211 CP_PARSER_FLAGS_NONE,
30212 &declspecs,
30213 &decl_class_or_enum_p);
30215 if (declspecs.type == error_mark_node)
30216 return error_mark_node;
30218 /* auto, register, static, extern, mutable. */
30219 if (declspecs.storage_class != sc_none)
30221 cp_parser_error (parser, "invalid type for property");
30222 declspecs.storage_class = sc_none;
30225 /* thread_local. */
30226 if (decl_spec_seq_has_spec_p (&declspecs, ds_thread))
30228 cp_parser_error (parser, "invalid type for property");
30229 declspecs.locations[ds_thread] = 0;
30232 /* typedef. */
30233 if (decl_spec_seq_has_spec_p (&declspecs, ds_typedef))
30235 cp_parser_error (parser, "invalid type for property");
30236 declspecs.locations[ds_typedef] = 0;
30239 prefix_attributes = declspecs.attributes;
30240 declspecs.attributes = NULL_TREE;
30242 /* Keep going until we hit the `;' at the end of the declaration. */
30243 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
30245 tree attributes, first_attribute, decl;
30246 cp_declarator *declarator;
30247 cp_token *token;
30249 /* Parse the declarator. */
30250 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
30251 NULL, NULL, false, false);
30253 /* Look for attributes that apply to the ivar. */
30254 attributes = cp_parser_attributes_opt (parser);
30255 /* Remember which attributes are prefix attributes and
30256 which are not. */
30257 first_attribute = attributes;
30258 /* Combine the attributes. */
30259 attributes = chainon (prefix_attributes, attributes);
30261 decl = grokfield (declarator, &declspecs,
30262 NULL_TREE, /*init_const_expr_p=*/false,
30263 NULL_TREE, attributes);
30265 if (decl == error_mark_node || decl == NULL_TREE)
30266 return error_mark_node;
30268 /* Reset PREFIX_ATTRIBUTES. */
30269 while (attributes && TREE_CHAIN (attributes) != first_attribute)
30270 attributes = TREE_CHAIN (attributes);
30271 if (attributes)
30272 TREE_CHAIN (attributes) = NULL_TREE;
30274 DECL_CHAIN (decl) = decls;
30275 decls = decl;
30277 token = cp_lexer_peek_token (parser->lexer);
30278 if (token->type == CPP_COMMA)
30280 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
30281 continue;
30283 else
30284 break;
30286 return decls;
30289 /* Parse an Objective-C @property declaration. The syntax is:
30291 objc-property-declaration:
30292 '@property' objc-property-attributes[opt] struct-declaration ;
30294 objc-property-attributes:
30295 '(' objc-property-attribute-list ')'
30297 objc-property-attribute-list:
30298 objc-property-attribute
30299 objc-property-attribute-list, objc-property-attribute
30301 objc-property-attribute
30302 'getter' = identifier
30303 'setter' = identifier
30304 'readonly'
30305 'readwrite'
30306 'assign'
30307 'retain'
30308 'copy'
30309 'nonatomic'
30311 For example:
30312 @property NSString *name;
30313 @property (readonly) id object;
30314 @property (retain, nonatomic, getter=getTheName) id name;
30315 @property int a, b, c;
30317 PS: This function is identical to
30318 c_parser_objc_at_property_declaration for C. Keep them in sync. */
30319 static void
30320 cp_parser_objc_at_property_declaration (cp_parser *parser)
30322 /* The following variables hold the attributes of the properties as
30323 parsed. They are 'false' or 'NULL_TREE' if the attribute was not
30324 seen. When we see an attribute, we set them to 'true' (if they
30325 are boolean properties) or to the identifier (if they have an
30326 argument, ie, for getter and setter). Note that here we only
30327 parse the list of attributes, check the syntax and accumulate the
30328 attributes that we find. objc_add_property_declaration() will
30329 then process the information. */
30330 bool property_assign = false;
30331 bool property_copy = false;
30332 tree property_getter_ident = NULL_TREE;
30333 bool property_nonatomic = false;
30334 bool property_readonly = false;
30335 bool property_readwrite = false;
30336 bool property_retain = false;
30337 tree property_setter_ident = NULL_TREE;
30339 /* 'properties' is the list of properties that we read. Usually a
30340 single one, but maybe more (eg, in "@property int a, b, c;" there
30341 are three). */
30342 tree properties;
30343 location_t loc;
30345 loc = cp_lexer_peek_token (parser->lexer)->location;
30347 cp_lexer_consume_token (parser->lexer); /* Eat '@property'. */
30349 /* Parse the optional attribute list... */
30350 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
30352 /* Eat the '('. */
30353 cp_lexer_consume_token (parser->lexer);
30355 while (true)
30357 bool syntax_error = false;
30358 cp_token *token = cp_lexer_peek_token (parser->lexer);
30359 enum rid keyword;
30361 if (token->type != CPP_NAME)
30363 cp_parser_error (parser, "expected identifier");
30364 break;
30366 keyword = C_RID_CODE (token->u.value);
30367 cp_lexer_consume_token (parser->lexer);
30368 switch (keyword)
30370 case RID_ASSIGN: property_assign = true; break;
30371 case RID_COPY: property_copy = true; break;
30372 case RID_NONATOMIC: property_nonatomic = true; break;
30373 case RID_READONLY: property_readonly = true; break;
30374 case RID_READWRITE: property_readwrite = true; break;
30375 case RID_RETAIN: property_retain = true; break;
30377 case RID_GETTER:
30378 case RID_SETTER:
30379 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
30381 if (keyword == RID_GETTER)
30382 cp_parser_error (parser,
30383 "missing %<=%> (after %<getter%> attribute)");
30384 else
30385 cp_parser_error (parser,
30386 "missing %<=%> (after %<setter%> attribute)");
30387 syntax_error = true;
30388 break;
30390 cp_lexer_consume_token (parser->lexer); /* eat the = */
30391 if (!cp_parser_objc_selector_p (cp_lexer_peek_token (parser->lexer)->type))
30393 cp_parser_error (parser, "expected identifier");
30394 syntax_error = true;
30395 break;
30397 if (keyword == RID_SETTER)
30399 if (property_setter_ident != NULL_TREE)
30401 cp_parser_error (parser, "the %<setter%> attribute may only be specified once");
30402 cp_lexer_consume_token (parser->lexer);
30404 else
30405 property_setter_ident = cp_parser_objc_selector (parser);
30406 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
30407 cp_parser_error (parser, "setter name must terminate with %<:%>");
30408 else
30409 cp_lexer_consume_token (parser->lexer);
30411 else
30413 if (property_getter_ident != NULL_TREE)
30415 cp_parser_error (parser, "the %<getter%> attribute may only be specified once");
30416 cp_lexer_consume_token (parser->lexer);
30418 else
30419 property_getter_ident = cp_parser_objc_selector (parser);
30421 break;
30422 default:
30423 cp_parser_error (parser, "unknown property attribute");
30424 syntax_error = true;
30425 break;
30428 if (syntax_error)
30429 break;
30431 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
30432 cp_lexer_consume_token (parser->lexer);
30433 else
30434 break;
30437 /* FIXME: "@property (setter, assign);" will generate a spurious
30438 "error: expected ‘)’ before ‘,’ token". This is because
30439 cp_parser_require, unlike the C counterpart, will produce an
30440 error even if we are in error recovery. */
30441 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
30443 cp_parser_skip_to_closing_parenthesis (parser,
30444 /*recovering=*/true,
30445 /*or_comma=*/false,
30446 /*consume_paren=*/true);
30450 /* ... and the property declaration(s). */
30451 properties = cp_parser_objc_struct_declaration (parser);
30453 if (properties == error_mark_node)
30455 cp_parser_skip_to_end_of_statement (parser);
30456 /* If the next token is now a `;', consume it. */
30457 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
30458 cp_lexer_consume_token (parser->lexer);
30459 return;
30462 if (properties == NULL_TREE)
30463 cp_parser_error (parser, "expected identifier");
30464 else
30466 /* Comma-separated properties are chained together in
30467 reverse order; add them one by one. */
30468 properties = nreverse (properties);
30470 for (; properties; properties = TREE_CHAIN (properties))
30471 objc_add_property_declaration (loc, copy_node (properties),
30472 property_readonly, property_readwrite,
30473 property_assign, property_retain,
30474 property_copy, property_nonatomic,
30475 property_getter_ident, property_setter_ident);
30478 cp_parser_consume_semicolon_at_end_of_statement (parser);
30481 /* Parse an Objective-C++ @synthesize declaration. The syntax is:
30483 objc-synthesize-declaration:
30484 @synthesize objc-synthesize-identifier-list ;
30486 objc-synthesize-identifier-list:
30487 objc-synthesize-identifier
30488 objc-synthesize-identifier-list, objc-synthesize-identifier
30490 objc-synthesize-identifier
30491 identifier
30492 identifier = identifier
30494 For example:
30495 @synthesize MyProperty;
30496 @synthesize OneProperty, AnotherProperty=MyIvar, YetAnotherProperty;
30498 PS: This function is identical to c_parser_objc_at_synthesize_declaration
30499 for C. Keep them in sync.
30501 static void
30502 cp_parser_objc_at_synthesize_declaration (cp_parser *parser)
30504 tree list = NULL_TREE;
30505 location_t loc;
30506 loc = cp_lexer_peek_token (parser->lexer)->location;
30508 cp_lexer_consume_token (parser->lexer); /* Eat '@synthesize'. */
30509 while (true)
30511 tree property, ivar;
30512 property = cp_parser_identifier (parser);
30513 if (property == error_mark_node)
30515 cp_parser_consume_semicolon_at_end_of_statement (parser);
30516 return;
30518 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
30520 cp_lexer_consume_token (parser->lexer);
30521 ivar = cp_parser_identifier (parser);
30522 if (ivar == error_mark_node)
30524 cp_parser_consume_semicolon_at_end_of_statement (parser);
30525 return;
30528 else
30529 ivar = NULL_TREE;
30530 list = chainon (list, build_tree_list (ivar, property));
30531 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
30532 cp_lexer_consume_token (parser->lexer);
30533 else
30534 break;
30536 cp_parser_consume_semicolon_at_end_of_statement (parser);
30537 objc_add_synthesize_declaration (loc, list);
30540 /* Parse an Objective-C++ @dynamic declaration. The syntax is:
30542 objc-dynamic-declaration:
30543 @dynamic identifier-list ;
30545 For example:
30546 @dynamic MyProperty;
30547 @dynamic MyProperty, AnotherProperty;
30549 PS: This function is identical to c_parser_objc_at_dynamic_declaration
30550 for C. Keep them in sync.
30552 static void
30553 cp_parser_objc_at_dynamic_declaration (cp_parser *parser)
30555 tree list = NULL_TREE;
30556 location_t loc;
30557 loc = cp_lexer_peek_token (parser->lexer)->location;
30559 cp_lexer_consume_token (parser->lexer); /* Eat '@dynamic'. */
30560 while (true)
30562 tree property;
30563 property = cp_parser_identifier (parser);
30564 if (property == error_mark_node)
30566 cp_parser_consume_semicolon_at_end_of_statement (parser);
30567 return;
30569 list = chainon (list, build_tree_list (NULL, property));
30570 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
30571 cp_lexer_consume_token (parser->lexer);
30572 else
30573 break;
30575 cp_parser_consume_semicolon_at_end_of_statement (parser);
30576 objc_add_dynamic_declaration (loc, list);
30580 /* OpenMP 2.5 / 3.0 / 3.1 / 4.0 parsing routines. */
30582 /* Returns name of the next clause.
30583 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
30584 the token is not consumed. Otherwise appropriate pragma_omp_clause is
30585 returned and the token is consumed. */
30587 static pragma_omp_clause
30588 cp_parser_omp_clause_name (cp_parser *parser)
30590 pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
30592 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
30593 result = PRAGMA_OACC_CLAUSE_AUTO;
30594 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
30595 result = PRAGMA_OMP_CLAUSE_IF;
30596 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT))
30597 result = PRAGMA_OMP_CLAUSE_DEFAULT;
30598 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DELETE))
30599 result = PRAGMA_OACC_CLAUSE_DELETE;
30600 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_PRIVATE))
30601 result = PRAGMA_OMP_CLAUSE_PRIVATE;
30602 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
30603 result = PRAGMA_OMP_CLAUSE_FOR;
30604 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
30606 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
30607 const char *p = IDENTIFIER_POINTER (id);
30609 switch (p[0])
30611 case 'a':
30612 if (!strcmp ("aligned", p))
30613 result = PRAGMA_OMP_CLAUSE_ALIGNED;
30614 else if (!strcmp ("async", p))
30615 result = PRAGMA_OACC_CLAUSE_ASYNC;
30616 break;
30617 case 'c':
30618 if (!strcmp ("collapse", p))
30619 result = PRAGMA_OMP_CLAUSE_COLLAPSE;
30620 else if (!strcmp ("copy", p))
30621 result = PRAGMA_OACC_CLAUSE_COPY;
30622 else if (!strcmp ("copyin", p))
30623 result = PRAGMA_OMP_CLAUSE_COPYIN;
30624 else if (!strcmp ("copyout", p))
30625 result = PRAGMA_OACC_CLAUSE_COPYOUT;
30626 else if (!strcmp ("copyprivate", p))
30627 result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
30628 else if (!strcmp ("create", p))
30629 result = PRAGMA_OACC_CLAUSE_CREATE;
30630 break;
30631 case 'd':
30632 if (!strcmp ("defaultmap", p))
30633 result = PRAGMA_OMP_CLAUSE_DEFAULTMAP;
30634 else if (!strcmp ("depend", p))
30635 result = PRAGMA_OMP_CLAUSE_DEPEND;
30636 else if (!strcmp ("device", p))
30637 result = PRAGMA_OMP_CLAUSE_DEVICE;
30638 else if (!strcmp ("deviceptr", p))
30639 result = PRAGMA_OACC_CLAUSE_DEVICEPTR;
30640 else if (!strcmp ("device_resident", p))
30641 result = PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT;
30642 else if (!strcmp ("dist_schedule", p))
30643 result = PRAGMA_OMP_CLAUSE_DIST_SCHEDULE;
30644 break;
30645 case 'f':
30646 if (!strcmp ("final", p))
30647 result = PRAGMA_OMP_CLAUSE_FINAL;
30648 else if (!strcmp ("firstprivate", p))
30649 result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
30650 else if (!strcmp ("from", p))
30651 result = PRAGMA_OMP_CLAUSE_FROM;
30652 break;
30653 case 'g':
30654 if (!strcmp ("gang", p))
30655 result = PRAGMA_OACC_CLAUSE_GANG;
30656 else if (!strcmp ("grainsize", p))
30657 result = PRAGMA_OMP_CLAUSE_GRAINSIZE;
30658 break;
30659 case 'h':
30660 if (!strcmp ("hint", p))
30661 result = PRAGMA_OMP_CLAUSE_HINT;
30662 else if (!strcmp ("host", p))
30663 result = PRAGMA_OACC_CLAUSE_HOST;
30664 break;
30665 case 'i':
30666 if (!strcmp ("inbranch", p))
30667 result = PRAGMA_OMP_CLAUSE_INBRANCH;
30668 else if (!strcmp ("independent", p))
30669 result = PRAGMA_OACC_CLAUSE_INDEPENDENT;
30670 else if (!strcmp ("is_device_ptr", p))
30671 result = PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR;
30672 break;
30673 case 'l':
30674 if (!strcmp ("lastprivate", p))
30675 result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
30676 else if (!strcmp ("linear", p))
30677 result = PRAGMA_OMP_CLAUSE_LINEAR;
30678 else if (!strcmp ("link", p))
30679 result = PRAGMA_OMP_CLAUSE_LINK;
30680 break;
30681 case 'm':
30682 if (!strcmp ("map", p))
30683 result = PRAGMA_OMP_CLAUSE_MAP;
30684 else if (!strcmp ("mergeable", p))
30685 result = PRAGMA_OMP_CLAUSE_MERGEABLE;
30686 else if (flag_cilkplus && !strcmp ("mask", p))
30687 result = PRAGMA_CILK_CLAUSE_MASK;
30688 break;
30689 case 'n':
30690 if (!strcmp ("nogroup", p))
30691 result = PRAGMA_OMP_CLAUSE_NOGROUP;
30692 else if (!strcmp ("notinbranch", p))
30693 result = PRAGMA_OMP_CLAUSE_NOTINBRANCH;
30694 else if (!strcmp ("nowait", p))
30695 result = PRAGMA_OMP_CLAUSE_NOWAIT;
30696 else if (flag_cilkplus && !strcmp ("nomask", p))
30697 result = PRAGMA_CILK_CLAUSE_NOMASK;
30698 else if (!strcmp ("num_gangs", p))
30699 result = PRAGMA_OACC_CLAUSE_NUM_GANGS;
30700 else if (!strcmp ("num_tasks", p))
30701 result = PRAGMA_OMP_CLAUSE_NUM_TASKS;
30702 else if (!strcmp ("num_teams", p))
30703 result = PRAGMA_OMP_CLAUSE_NUM_TEAMS;
30704 else if (!strcmp ("num_threads", p))
30705 result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
30706 else if (!strcmp ("num_workers", p))
30707 result = PRAGMA_OACC_CLAUSE_NUM_WORKERS;
30708 break;
30709 case 'o':
30710 if (!strcmp ("ordered", p))
30711 result = PRAGMA_OMP_CLAUSE_ORDERED;
30712 break;
30713 case 'p':
30714 if (!strcmp ("parallel", p))
30715 result = PRAGMA_OMP_CLAUSE_PARALLEL;
30716 else if (!strcmp ("present", p))
30717 result = PRAGMA_OACC_CLAUSE_PRESENT;
30718 else if (!strcmp ("present_or_copy", p)
30719 || !strcmp ("pcopy", p))
30720 result = PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY;
30721 else if (!strcmp ("present_or_copyin", p)
30722 || !strcmp ("pcopyin", p))
30723 result = PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN;
30724 else if (!strcmp ("present_or_copyout", p)
30725 || !strcmp ("pcopyout", p))
30726 result = PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT;
30727 else if (!strcmp ("present_or_create", p)
30728 || !strcmp ("pcreate", p))
30729 result = PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE;
30730 else if (!strcmp ("priority", p))
30731 result = PRAGMA_OMP_CLAUSE_PRIORITY;
30732 else if (!strcmp ("proc_bind", p))
30733 result = PRAGMA_OMP_CLAUSE_PROC_BIND;
30734 break;
30735 case 'r':
30736 if (!strcmp ("reduction", p))
30737 result = PRAGMA_OMP_CLAUSE_REDUCTION;
30738 break;
30739 case 's':
30740 if (!strcmp ("safelen", p))
30741 result = PRAGMA_OMP_CLAUSE_SAFELEN;
30742 else if (!strcmp ("schedule", p))
30743 result = PRAGMA_OMP_CLAUSE_SCHEDULE;
30744 else if (!strcmp ("sections", p))
30745 result = PRAGMA_OMP_CLAUSE_SECTIONS;
30746 else if (!strcmp ("self", p))
30747 result = PRAGMA_OACC_CLAUSE_SELF;
30748 else if (!strcmp ("seq", p))
30749 result = PRAGMA_OACC_CLAUSE_SEQ;
30750 else if (!strcmp ("shared", p))
30751 result = PRAGMA_OMP_CLAUSE_SHARED;
30752 else if (!strcmp ("simd", p))
30753 result = PRAGMA_OMP_CLAUSE_SIMD;
30754 else if (!strcmp ("simdlen", p))
30755 result = PRAGMA_OMP_CLAUSE_SIMDLEN;
30756 break;
30757 case 't':
30758 if (!strcmp ("taskgroup", p))
30759 result = PRAGMA_OMP_CLAUSE_TASKGROUP;
30760 else if (!strcmp ("thread_limit", p))
30761 result = PRAGMA_OMP_CLAUSE_THREAD_LIMIT;
30762 else if (!strcmp ("threads", p))
30763 result = PRAGMA_OMP_CLAUSE_THREADS;
30764 else if (!strcmp ("tile", p))
30765 result = PRAGMA_OACC_CLAUSE_TILE;
30766 else if (!strcmp ("to", p))
30767 result = PRAGMA_OMP_CLAUSE_TO;
30768 break;
30769 case 'u':
30770 if (!strcmp ("uniform", p))
30771 result = PRAGMA_OMP_CLAUSE_UNIFORM;
30772 else if (!strcmp ("untied", p))
30773 result = PRAGMA_OMP_CLAUSE_UNTIED;
30774 else if (!strcmp ("use_device", p))
30775 result = PRAGMA_OACC_CLAUSE_USE_DEVICE;
30776 else if (!strcmp ("use_device_ptr", p))
30777 result = PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR;
30778 break;
30779 case 'v':
30780 if (!strcmp ("vector", p))
30781 result = PRAGMA_OACC_CLAUSE_VECTOR;
30782 else if (!strcmp ("vector_length", p))
30783 result = PRAGMA_OACC_CLAUSE_VECTOR_LENGTH;
30784 else if (flag_cilkplus && !strcmp ("vectorlength", p))
30785 result = PRAGMA_CILK_CLAUSE_VECTORLENGTH;
30786 break;
30787 case 'w':
30788 if (!strcmp ("wait", p))
30789 result = PRAGMA_OACC_CLAUSE_WAIT;
30790 else if (!strcmp ("worker", p))
30791 result = PRAGMA_OACC_CLAUSE_WORKER;
30792 break;
30796 if (result != PRAGMA_OMP_CLAUSE_NONE)
30797 cp_lexer_consume_token (parser->lexer);
30799 return result;
30802 /* Validate that a clause of the given type does not already exist. */
30804 static void
30805 check_no_duplicate_clause (tree clauses, enum omp_clause_code code,
30806 const char *name, location_t location)
30808 tree c;
30810 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
30811 if (OMP_CLAUSE_CODE (c) == code)
30813 error_at (location, "too many %qs clauses", name);
30814 break;
30818 /* OpenMP 2.5:
30819 variable-list:
30820 identifier
30821 variable-list , identifier
30823 In addition, we match a closing parenthesis (or, if COLON is non-NULL,
30824 colon). An opening parenthesis will have been consumed by the caller.
30826 If KIND is nonzero, create the appropriate node and install the decl
30827 in OMP_CLAUSE_DECL and add the node to the head of the list.
30829 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
30830 return the list created.
30832 COLON can be NULL if only closing parenthesis should end the list,
30833 or pointer to bool which will receive false if the list is terminated
30834 by closing parenthesis or true if the list is terminated by colon. */
30836 static tree
30837 cp_parser_omp_var_list_no_open (cp_parser *parser, enum omp_clause_code kind,
30838 tree list, bool *colon)
30840 cp_token *token;
30841 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
30842 if (colon)
30844 parser->colon_corrects_to_scope_p = false;
30845 *colon = false;
30847 while (1)
30849 tree name, decl;
30851 token = cp_lexer_peek_token (parser->lexer);
30852 if (kind != 0
30853 && current_class_ptr
30854 && cp_parser_is_keyword (token, RID_THIS))
30856 decl = finish_this_expr ();
30857 if (TREE_CODE (decl) == NON_LVALUE_EXPR
30858 || CONVERT_EXPR_P (decl))
30859 decl = TREE_OPERAND (decl, 0);
30860 cp_lexer_consume_token (parser->lexer);
30862 else
30864 name = cp_parser_id_expression (parser, /*template_p=*/false,
30865 /*check_dependency_p=*/true,
30866 /*template_p=*/NULL,
30867 /*declarator_p=*/false,
30868 /*optional_p=*/false);
30869 if (name == error_mark_node)
30870 goto skip_comma;
30872 decl = cp_parser_lookup_name_simple (parser, name, token->location);
30873 if (decl == error_mark_node)
30874 cp_parser_name_lookup_error (parser, name, decl, NLE_NULL,
30875 token->location);
30877 if (decl == error_mark_node)
30879 else if (kind != 0)
30881 switch (kind)
30883 case OMP_CLAUSE__CACHE_:
30884 /* The OpenACC cache directive explicitly only allows "array
30885 elements or subarrays". */
30886 if (cp_lexer_peek_token (parser->lexer)->type != CPP_OPEN_SQUARE)
30888 error_at (token->location, "expected %<[%>");
30889 decl = error_mark_node;
30890 break;
30892 /* FALLTHROUGH. */
30893 case OMP_CLAUSE_MAP:
30894 case OMP_CLAUSE_FROM:
30895 case OMP_CLAUSE_TO:
30896 while (cp_lexer_next_token_is (parser->lexer, CPP_DOT))
30898 location_t loc
30899 = cp_lexer_peek_token (parser->lexer)->location;
30900 cp_id_kind idk = CP_ID_KIND_NONE;
30901 cp_lexer_consume_token (parser->lexer);
30902 decl = convert_from_reference (decl);
30903 decl
30904 = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT,
30905 decl, false,
30906 &idk, loc);
30908 /* FALLTHROUGH. */
30909 case OMP_CLAUSE_DEPEND:
30910 case OMP_CLAUSE_REDUCTION:
30911 while (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
30913 tree low_bound = NULL_TREE, length = NULL_TREE;
30915 parser->colon_corrects_to_scope_p = false;
30916 cp_lexer_consume_token (parser->lexer);
30917 if (!cp_lexer_next_token_is (parser->lexer, CPP_COLON))
30918 low_bound = cp_parser_expression (parser);
30919 if (!colon)
30920 parser->colon_corrects_to_scope_p
30921 = saved_colon_corrects_to_scope_p;
30922 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_SQUARE))
30923 length = integer_one_node;
30924 else
30926 /* Look for `:'. */
30927 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
30928 goto skip_comma;
30929 if (!cp_lexer_next_token_is (parser->lexer,
30930 CPP_CLOSE_SQUARE))
30931 length = cp_parser_expression (parser);
30933 /* Look for the closing `]'. */
30934 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE,
30935 RT_CLOSE_SQUARE))
30936 goto skip_comma;
30938 decl = tree_cons (low_bound, length, decl);
30940 break;
30941 default:
30942 break;
30945 tree u = build_omp_clause (token->location, kind);
30946 OMP_CLAUSE_DECL (u) = decl;
30947 OMP_CLAUSE_CHAIN (u) = list;
30948 list = u;
30950 else
30951 list = tree_cons (decl, NULL_TREE, list);
30953 get_comma:
30954 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
30955 break;
30956 cp_lexer_consume_token (parser->lexer);
30959 if (colon)
30960 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
30962 if (colon != NULL && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
30964 *colon = true;
30965 cp_parser_require (parser, CPP_COLON, RT_COLON);
30966 return list;
30969 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
30971 int ending;
30973 /* Try to resync to an unnested comma. Copied from
30974 cp_parser_parenthesized_expression_list. */
30975 skip_comma:
30976 if (colon)
30977 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
30978 ending = cp_parser_skip_to_closing_parenthesis (parser,
30979 /*recovering=*/true,
30980 /*or_comma=*/true,
30981 /*consume_paren=*/true);
30982 if (ending < 0)
30983 goto get_comma;
30986 return list;
30989 /* Similarly, but expect leading and trailing parenthesis. This is a very
30990 common case for omp clauses. */
30992 static tree
30993 cp_parser_omp_var_list (cp_parser *parser, enum omp_clause_code kind, tree list)
30995 if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
30996 return cp_parser_omp_var_list_no_open (parser, kind, list, NULL);
30997 return list;
31000 /* OpenACC 2.0:
31001 copy ( variable-list )
31002 copyin ( variable-list )
31003 copyout ( variable-list )
31004 create ( variable-list )
31005 delete ( variable-list )
31006 present ( variable-list )
31007 present_or_copy ( variable-list )
31008 pcopy ( variable-list )
31009 present_or_copyin ( variable-list )
31010 pcopyin ( variable-list )
31011 present_or_copyout ( variable-list )
31012 pcopyout ( variable-list )
31013 present_or_create ( variable-list )
31014 pcreate ( variable-list ) */
31016 static tree
31017 cp_parser_oacc_data_clause (cp_parser *parser, pragma_omp_clause c_kind,
31018 tree list)
31020 enum gomp_map_kind kind;
31021 switch (c_kind)
31023 case PRAGMA_OACC_CLAUSE_COPY:
31024 kind = GOMP_MAP_FORCE_TOFROM;
31025 break;
31026 case PRAGMA_OACC_CLAUSE_COPYIN:
31027 kind = GOMP_MAP_FORCE_TO;
31028 break;
31029 case PRAGMA_OACC_CLAUSE_COPYOUT:
31030 kind = GOMP_MAP_FORCE_FROM;
31031 break;
31032 case PRAGMA_OACC_CLAUSE_CREATE:
31033 kind = GOMP_MAP_FORCE_ALLOC;
31034 break;
31035 case PRAGMA_OACC_CLAUSE_DELETE:
31036 kind = GOMP_MAP_DELETE;
31037 break;
31038 case PRAGMA_OACC_CLAUSE_DEVICE:
31039 kind = GOMP_MAP_FORCE_TO;
31040 break;
31041 case PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT:
31042 kind = GOMP_MAP_DEVICE_RESIDENT;
31043 break;
31044 case PRAGMA_OACC_CLAUSE_HOST:
31045 case PRAGMA_OACC_CLAUSE_SELF:
31046 kind = GOMP_MAP_FORCE_FROM;
31047 break;
31048 case PRAGMA_OACC_CLAUSE_LINK:
31049 kind = GOMP_MAP_LINK;
31050 break;
31051 case PRAGMA_OACC_CLAUSE_PRESENT:
31052 kind = GOMP_MAP_FORCE_PRESENT;
31053 break;
31054 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY:
31055 kind = GOMP_MAP_TOFROM;
31056 break;
31057 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN:
31058 kind = GOMP_MAP_TO;
31059 break;
31060 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT:
31061 kind = GOMP_MAP_FROM;
31062 break;
31063 case PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE:
31064 kind = GOMP_MAP_ALLOC;
31065 break;
31066 default:
31067 gcc_unreachable ();
31069 tree nl, c;
31070 nl = cp_parser_omp_var_list (parser, OMP_CLAUSE_MAP, list);
31072 for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
31073 OMP_CLAUSE_SET_MAP_KIND (c, kind);
31075 return nl;
31078 /* OpenACC 2.0:
31079 deviceptr ( variable-list ) */
31081 static tree
31082 cp_parser_oacc_data_clause_deviceptr (cp_parser *parser, tree list)
31084 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
31085 tree vars, t;
31087 /* Can't use OMP_CLAUSE_MAP here (that is, can't use the generic
31088 cp_parser_oacc_data_clause), as for PRAGMA_OACC_CLAUSE_DEVICEPTR,
31089 variable-list must only allow for pointer variables. */
31090 vars = cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
31091 for (t = vars; t; t = TREE_CHAIN (t))
31093 tree v = TREE_PURPOSE (t);
31094 tree u = build_omp_clause (loc, OMP_CLAUSE_MAP);
31095 OMP_CLAUSE_SET_MAP_KIND (u, GOMP_MAP_FORCE_DEVICEPTR);
31096 OMP_CLAUSE_DECL (u) = v;
31097 OMP_CLAUSE_CHAIN (u) = list;
31098 list = u;
31101 return list;
31104 /* OpenACC 2.0:
31105 auto
31106 independent
31107 nohost
31108 seq */
31110 static tree
31111 cp_parser_oacc_simple_clause (cp_parser * /* parser */,
31112 enum omp_clause_code code,
31113 tree list, location_t location)
31115 check_no_duplicate_clause (list, code, omp_clause_code_name[code], location);
31116 tree c = build_omp_clause (location, code);
31117 OMP_CLAUSE_CHAIN (c) = list;
31118 return c;
31121 /* OpenACC:
31122 num_gangs ( expression )
31123 num_workers ( expression )
31124 vector_length ( expression ) */
31126 static tree
31127 cp_parser_oacc_single_int_clause (cp_parser *parser, omp_clause_code code,
31128 const char *str, tree list)
31130 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
31132 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31133 return list;
31135 tree t = cp_parser_assignment_expression (parser, NULL, false, false);
31137 if (t == error_mark_node
31138 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31140 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31141 /*or_comma=*/false,
31142 /*consume_paren=*/true);
31143 return list;
31146 check_no_duplicate_clause (list, code, str, loc);
31148 tree c = build_omp_clause (loc, code);
31149 OMP_CLAUSE_OPERAND (c, 0) = t;
31150 OMP_CLAUSE_CHAIN (c) = list;
31151 return c;
31154 /* OpenACC:
31156 gang [( gang-arg-list )]
31157 worker [( [num:] int-expr )]
31158 vector [( [length:] int-expr )]
31160 where gang-arg is one of:
31162 [num:] int-expr
31163 static: size-expr
31165 and size-expr may be:
31168 int-expr
31171 static tree
31172 cp_parser_oacc_shape_clause (cp_parser *parser, omp_clause_code kind,
31173 const char *str, tree list)
31175 const char *id = "num";
31176 cp_lexer *lexer = parser->lexer;
31177 tree ops[2] = { NULL_TREE, NULL_TREE }, c;
31178 location_t loc = cp_lexer_peek_token (lexer)->location;
31180 if (kind == OMP_CLAUSE_VECTOR)
31181 id = "length";
31183 if (cp_lexer_next_token_is (lexer, CPP_OPEN_PAREN))
31185 cp_lexer_consume_token (lexer);
31189 cp_token *next = cp_lexer_peek_token (lexer);
31190 int idx = 0;
31192 /* Gang static argument. */
31193 if (kind == OMP_CLAUSE_GANG
31194 && cp_lexer_next_token_is_keyword (lexer, RID_STATIC))
31196 cp_lexer_consume_token (lexer);
31198 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
31199 goto cleanup_error;
31201 idx = 1;
31202 if (ops[idx] != NULL)
31204 cp_parser_error (parser, "too many %<static%> arguments");
31205 goto cleanup_error;
31208 /* Check for the '*' argument. */
31209 if (cp_lexer_next_token_is (lexer, CPP_MULT)
31210 && (cp_lexer_nth_token_is (parser->lexer, 2, CPP_COMMA)
31211 || cp_lexer_nth_token_is (parser->lexer, 2,
31212 CPP_CLOSE_PAREN)))
31214 cp_lexer_consume_token (lexer);
31215 ops[idx] = integer_minus_one_node;
31217 if (cp_lexer_next_token_is (lexer, CPP_COMMA))
31219 cp_lexer_consume_token (lexer);
31220 continue;
31222 else break;
31225 /* Worker num: argument and vector length: arguments. */
31226 else if (cp_lexer_next_token_is (lexer, CPP_NAME)
31227 && strcmp (id, IDENTIFIER_POINTER (next->u.value)) == 0
31228 && cp_lexer_nth_token_is (lexer, 2, CPP_COLON))
31230 cp_lexer_consume_token (lexer); /* id */
31231 cp_lexer_consume_token (lexer); /* ':' */
31234 /* Now collect the actual argument. */
31235 if (ops[idx] != NULL_TREE)
31237 cp_parser_error (parser, "unexpected argument");
31238 goto cleanup_error;
31241 tree expr = cp_parser_assignment_expression (parser, NULL, false,
31242 false);
31243 if (expr == error_mark_node)
31244 goto cleanup_error;
31246 mark_exp_read (expr);
31247 ops[idx] = expr;
31249 if (kind == OMP_CLAUSE_GANG
31250 && cp_lexer_next_token_is (lexer, CPP_COMMA))
31252 cp_lexer_consume_token (lexer);
31253 continue;
31255 break;
31257 while (1);
31259 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31260 goto cleanup_error;
31263 check_no_duplicate_clause (list, kind, str, loc);
31265 c = build_omp_clause (loc, kind);
31267 if (ops[1])
31268 OMP_CLAUSE_OPERAND (c, 1) = ops[1];
31270 OMP_CLAUSE_OPERAND (c, 0) = ops[0];
31271 OMP_CLAUSE_CHAIN (c) = list;
31273 return c;
31275 cleanup_error:
31276 cp_parser_skip_to_closing_parenthesis (parser, false, false, true);
31277 return list;
31280 /* OpenACC 2.0:
31281 tile ( size-expr-list ) */
31283 static tree
31284 cp_parser_oacc_clause_tile (cp_parser *parser, location_t clause_loc, tree list)
31286 tree c, expr = error_mark_node;
31287 tree tile = NULL_TREE;
31289 /* Collapse and tile are mutually exclusive. (The spec doesn't say
31290 so, but the spec authors never considered such a case and have
31291 differing opinions on what it might mean, including 'not
31292 allowed'.) */
31293 check_no_duplicate_clause (list, OMP_CLAUSE_TILE, "tile", clause_loc);
31294 check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse",
31295 clause_loc);
31297 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31298 return list;
31302 if (tile && !cp_parser_require (parser, CPP_COMMA, RT_COMMA))
31303 return list;
31305 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT)
31306 && (cp_lexer_nth_token_is (parser->lexer, 2, CPP_COMMA)
31307 || cp_lexer_nth_token_is (parser->lexer, 2, CPP_CLOSE_PAREN)))
31309 cp_lexer_consume_token (parser->lexer);
31310 expr = integer_zero_node;
31312 else
31313 expr = cp_parser_constant_expression (parser);
31315 tile = tree_cons (NULL_TREE, expr, tile);
31317 while (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN));
31319 /* Consume the trailing ')'. */
31320 cp_lexer_consume_token (parser->lexer);
31322 c = build_omp_clause (clause_loc, OMP_CLAUSE_TILE);
31323 tile = nreverse (tile);
31324 OMP_CLAUSE_TILE_LIST (c) = tile;
31325 OMP_CLAUSE_CHAIN (c) = list;
31326 return c;
31329 /* OpenACC 2.0
31330 Parse wait clause or directive parameters. */
31332 static tree
31333 cp_parser_oacc_wait_list (cp_parser *parser, location_t clause_loc, tree list)
31335 vec<tree, va_gc> *args;
31336 tree t, args_tree;
31338 args = cp_parser_parenthesized_expression_list (parser, non_attr,
31339 /*cast_p=*/false,
31340 /*allow_expansion_p=*/true,
31341 /*non_constant_p=*/NULL);
31343 if (args == NULL || args->length () == 0)
31345 cp_parser_error (parser, "expected integer expression before ')'");
31346 if (args != NULL)
31347 release_tree_vector (args);
31348 return list;
31351 args_tree = build_tree_list_vec (args);
31353 release_tree_vector (args);
31355 for (t = args_tree; t; t = TREE_CHAIN (t))
31357 tree targ = TREE_VALUE (t);
31359 if (targ != error_mark_node)
31361 if (!INTEGRAL_TYPE_P (TREE_TYPE (targ)))
31362 error ("%<wait%> expression must be integral");
31363 else
31365 tree c = build_omp_clause (clause_loc, OMP_CLAUSE_WAIT);
31367 mark_rvalue_use (targ);
31368 OMP_CLAUSE_DECL (c) = targ;
31369 OMP_CLAUSE_CHAIN (c) = list;
31370 list = c;
31375 return list;
31378 /* OpenACC:
31379 wait ( int-expr-list ) */
31381 static tree
31382 cp_parser_oacc_clause_wait (cp_parser *parser, tree list)
31384 location_t location = cp_lexer_peek_token (parser->lexer)->location;
31386 if (cp_lexer_peek_token (parser->lexer)->type != CPP_OPEN_PAREN)
31387 return list;
31389 list = cp_parser_oacc_wait_list (parser, location, list);
31391 return list;
31394 /* OpenMP 3.0:
31395 collapse ( constant-expression ) */
31397 static tree
31398 cp_parser_omp_clause_collapse (cp_parser *parser, tree list, location_t location)
31400 tree c, num;
31401 location_t loc;
31402 HOST_WIDE_INT n;
31404 loc = cp_lexer_peek_token (parser->lexer)->location;
31405 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31406 return list;
31408 num = cp_parser_constant_expression (parser);
31410 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31411 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31412 /*or_comma=*/false,
31413 /*consume_paren=*/true);
31415 if (num == error_mark_node)
31416 return list;
31417 num = fold_non_dependent_expr (num);
31418 if (!tree_fits_shwi_p (num)
31419 || !INTEGRAL_TYPE_P (TREE_TYPE (num))
31420 || (n = tree_to_shwi (num)) <= 0
31421 || (int) n != n)
31423 error_at (loc, "collapse argument needs positive constant integer expression");
31424 return list;
31427 check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse", location);
31428 check_no_duplicate_clause (list, OMP_CLAUSE_TILE, "tile", location);
31429 c = build_omp_clause (loc, OMP_CLAUSE_COLLAPSE);
31430 OMP_CLAUSE_CHAIN (c) = list;
31431 OMP_CLAUSE_COLLAPSE_EXPR (c) = num;
31433 return c;
31436 /* OpenMP 2.5:
31437 default ( shared | none )
31439 OpenACC 2.0
31440 default (none) */
31442 static tree
31443 cp_parser_omp_clause_default (cp_parser *parser, tree list,
31444 location_t location, bool is_oacc)
31446 enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
31447 tree c;
31449 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31450 return list;
31451 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
31453 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
31454 const char *p = IDENTIFIER_POINTER (id);
31456 switch (p[0])
31458 case 'n':
31459 if (strcmp ("none", p) != 0)
31460 goto invalid_kind;
31461 kind = OMP_CLAUSE_DEFAULT_NONE;
31462 break;
31464 case 's':
31465 if (strcmp ("shared", p) != 0 || is_oacc)
31466 goto invalid_kind;
31467 kind = OMP_CLAUSE_DEFAULT_SHARED;
31468 break;
31470 default:
31471 goto invalid_kind;
31474 cp_lexer_consume_token (parser->lexer);
31476 else
31478 invalid_kind:
31479 if (is_oacc)
31480 cp_parser_error (parser, "expected %<none%>");
31481 else
31482 cp_parser_error (parser, "expected %<none%> or %<shared%>");
31485 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31486 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31487 /*or_comma=*/false,
31488 /*consume_paren=*/true);
31490 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
31491 return list;
31493 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default", location);
31494 c = build_omp_clause (location, OMP_CLAUSE_DEFAULT);
31495 OMP_CLAUSE_CHAIN (c) = list;
31496 OMP_CLAUSE_DEFAULT_KIND (c) = kind;
31498 return c;
31501 /* OpenMP 3.1:
31502 final ( expression ) */
31504 static tree
31505 cp_parser_omp_clause_final (cp_parser *parser, tree list, location_t location)
31507 tree t, c;
31509 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31510 return list;
31512 t = cp_parser_condition (parser);
31514 if (t == error_mark_node
31515 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31516 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31517 /*or_comma=*/false,
31518 /*consume_paren=*/true);
31520 check_no_duplicate_clause (list, OMP_CLAUSE_FINAL, "final", location);
31522 c = build_omp_clause (location, OMP_CLAUSE_FINAL);
31523 OMP_CLAUSE_FINAL_EXPR (c) = t;
31524 OMP_CLAUSE_CHAIN (c) = list;
31526 return c;
31529 /* OpenMP 2.5:
31530 if ( expression )
31532 OpenMP 4.5:
31533 if ( directive-name-modifier : expression )
31535 directive-name-modifier:
31536 parallel | task | taskloop | target data | target | target update
31537 | target enter data | target exit data */
31539 static tree
31540 cp_parser_omp_clause_if (cp_parser *parser, tree list, location_t location,
31541 bool is_omp)
31543 tree t, c;
31544 enum tree_code if_modifier = ERROR_MARK;
31546 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31547 return list;
31549 if (is_omp && cp_lexer_next_token_is (parser->lexer, CPP_NAME))
31551 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
31552 const char *p = IDENTIFIER_POINTER (id);
31553 int n = 2;
31555 if (strcmp ("parallel", p) == 0)
31556 if_modifier = OMP_PARALLEL;
31557 else if (strcmp ("task", p) == 0)
31558 if_modifier = OMP_TASK;
31559 else if (strcmp ("taskloop", p) == 0)
31560 if_modifier = OMP_TASKLOOP;
31561 else if (strcmp ("target", p) == 0)
31563 if_modifier = OMP_TARGET;
31564 if (cp_lexer_nth_token_is (parser->lexer, 2, CPP_NAME))
31566 id = cp_lexer_peek_nth_token (parser->lexer, 2)->u.value;
31567 p = IDENTIFIER_POINTER (id);
31568 if (strcmp ("data", p) == 0)
31569 if_modifier = OMP_TARGET_DATA;
31570 else if (strcmp ("update", p) == 0)
31571 if_modifier = OMP_TARGET_UPDATE;
31572 else if (strcmp ("enter", p) == 0)
31573 if_modifier = OMP_TARGET_ENTER_DATA;
31574 else if (strcmp ("exit", p) == 0)
31575 if_modifier = OMP_TARGET_EXIT_DATA;
31576 if (if_modifier != OMP_TARGET)
31577 n = 3;
31578 else
31580 location_t loc
31581 = cp_lexer_peek_nth_token (parser->lexer, 2)->location;
31582 error_at (loc, "expected %<data%>, %<update%>, %<enter%> "
31583 "or %<exit%>");
31584 if_modifier = ERROR_MARK;
31586 if (if_modifier == OMP_TARGET_ENTER_DATA
31587 || if_modifier == OMP_TARGET_EXIT_DATA)
31589 if (cp_lexer_nth_token_is (parser->lexer, 3, CPP_NAME))
31591 id = cp_lexer_peek_nth_token (parser->lexer, 3)->u.value;
31592 p = IDENTIFIER_POINTER (id);
31593 if (strcmp ("data", p) == 0)
31594 n = 4;
31596 if (n != 4)
31598 location_t loc
31599 = cp_lexer_peek_nth_token (parser->lexer, 3)->location;
31600 error_at (loc, "expected %<data%>");
31601 if_modifier = ERROR_MARK;
31606 if (if_modifier != ERROR_MARK)
31608 if (cp_lexer_nth_token_is (parser->lexer, n, CPP_COLON))
31610 while (n-- > 0)
31611 cp_lexer_consume_token (parser->lexer);
31613 else
31615 if (n > 2)
31617 location_t loc
31618 = cp_lexer_peek_nth_token (parser->lexer, n)->location;
31619 error_at (loc, "expected %<:%>");
31621 if_modifier = ERROR_MARK;
31626 t = cp_parser_condition (parser);
31628 if (t == error_mark_node
31629 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31630 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31631 /*or_comma=*/false,
31632 /*consume_paren=*/true);
31634 for (c = list; c ; c = OMP_CLAUSE_CHAIN (c))
31635 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IF)
31637 if (if_modifier != ERROR_MARK
31638 && OMP_CLAUSE_IF_MODIFIER (c) == if_modifier)
31640 const char *p = NULL;
31641 switch (if_modifier)
31643 case OMP_PARALLEL: p = "parallel"; break;
31644 case OMP_TASK: p = "task"; break;
31645 case OMP_TASKLOOP: p = "taskloop"; break;
31646 case OMP_TARGET_DATA: p = "target data"; break;
31647 case OMP_TARGET: p = "target"; break;
31648 case OMP_TARGET_UPDATE: p = "target update"; break;
31649 case OMP_TARGET_ENTER_DATA: p = "enter data"; break;
31650 case OMP_TARGET_EXIT_DATA: p = "exit data"; break;
31651 default: gcc_unreachable ();
31653 error_at (location, "too many %<if%> clauses with %qs modifier",
31655 return list;
31657 else if (OMP_CLAUSE_IF_MODIFIER (c) == if_modifier)
31659 if (!is_omp)
31660 error_at (location, "too many %<if%> clauses");
31661 else
31662 error_at (location, "too many %<if%> clauses without modifier");
31663 return list;
31665 else if (if_modifier == ERROR_MARK
31666 || OMP_CLAUSE_IF_MODIFIER (c) == ERROR_MARK)
31668 error_at (location, "if any %<if%> clause has modifier, then all "
31669 "%<if%> clauses have to use modifier");
31670 return list;
31674 c = build_omp_clause (location, OMP_CLAUSE_IF);
31675 OMP_CLAUSE_IF_MODIFIER (c) = if_modifier;
31676 OMP_CLAUSE_IF_EXPR (c) = t;
31677 OMP_CLAUSE_CHAIN (c) = list;
31679 return c;
31682 /* OpenMP 3.1:
31683 mergeable */
31685 static tree
31686 cp_parser_omp_clause_mergeable (cp_parser * /*parser*/,
31687 tree list, location_t location)
31689 tree c;
31691 check_no_duplicate_clause (list, OMP_CLAUSE_MERGEABLE, "mergeable",
31692 location);
31694 c = build_omp_clause (location, OMP_CLAUSE_MERGEABLE);
31695 OMP_CLAUSE_CHAIN (c) = list;
31696 return c;
31699 /* OpenMP 2.5:
31700 nowait */
31702 static tree
31703 cp_parser_omp_clause_nowait (cp_parser * /*parser*/,
31704 tree list, location_t location)
31706 tree c;
31708 check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait", location);
31710 c = build_omp_clause (location, OMP_CLAUSE_NOWAIT);
31711 OMP_CLAUSE_CHAIN (c) = list;
31712 return c;
31715 /* OpenMP 2.5:
31716 num_threads ( expression ) */
31718 static tree
31719 cp_parser_omp_clause_num_threads (cp_parser *parser, tree list,
31720 location_t location)
31722 tree t, c;
31724 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31725 return list;
31727 t = cp_parser_expression (parser);
31729 if (t == error_mark_node
31730 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31731 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31732 /*or_comma=*/false,
31733 /*consume_paren=*/true);
31735 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS,
31736 "num_threads", location);
31738 c = build_omp_clause (location, OMP_CLAUSE_NUM_THREADS);
31739 OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
31740 OMP_CLAUSE_CHAIN (c) = list;
31742 return c;
31745 /* OpenMP 4.5:
31746 num_tasks ( expression ) */
31748 static tree
31749 cp_parser_omp_clause_num_tasks (cp_parser *parser, tree list,
31750 location_t location)
31752 tree t, c;
31754 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31755 return list;
31757 t = cp_parser_expression (parser);
31759 if (t == error_mark_node
31760 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31761 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31762 /*or_comma=*/false,
31763 /*consume_paren=*/true);
31765 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_TASKS,
31766 "num_tasks", location);
31768 c = build_omp_clause (location, OMP_CLAUSE_NUM_TASKS);
31769 OMP_CLAUSE_NUM_TASKS_EXPR (c) = t;
31770 OMP_CLAUSE_CHAIN (c) = list;
31772 return c;
31775 /* OpenMP 4.5:
31776 grainsize ( expression ) */
31778 static tree
31779 cp_parser_omp_clause_grainsize (cp_parser *parser, tree list,
31780 location_t location)
31782 tree t, c;
31784 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31785 return list;
31787 t = cp_parser_expression (parser);
31789 if (t == error_mark_node
31790 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31791 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31792 /*or_comma=*/false,
31793 /*consume_paren=*/true);
31795 check_no_duplicate_clause (list, OMP_CLAUSE_GRAINSIZE,
31796 "grainsize", location);
31798 c = build_omp_clause (location, OMP_CLAUSE_GRAINSIZE);
31799 OMP_CLAUSE_GRAINSIZE_EXPR (c) = t;
31800 OMP_CLAUSE_CHAIN (c) = list;
31802 return c;
31805 /* OpenMP 4.5:
31806 priority ( expression ) */
31808 static tree
31809 cp_parser_omp_clause_priority (cp_parser *parser, tree list,
31810 location_t location)
31812 tree t, c;
31814 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31815 return list;
31817 t = cp_parser_expression (parser);
31819 if (t == error_mark_node
31820 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31821 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31822 /*or_comma=*/false,
31823 /*consume_paren=*/true);
31825 check_no_duplicate_clause (list, OMP_CLAUSE_PRIORITY,
31826 "priority", location);
31828 c = build_omp_clause (location, OMP_CLAUSE_PRIORITY);
31829 OMP_CLAUSE_PRIORITY_EXPR (c) = t;
31830 OMP_CLAUSE_CHAIN (c) = list;
31832 return c;
31835 /* OpenMP 4.5:
31836 hint ( expression ) */
31838 static tree
31839 cp_parser_omp_clause_hint (cp_parser *parser, tree list,
31840 location_t location)
31842 tree t, c;
31844 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31845 return list;
31847 t = cp_parser_expression (parser);
31849 if (t == error_mark_node
31850 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31851 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31852 /*or_comma=*/false,
31853 /*consume_paren=*/true);
31855 check_no_duplicate_clause (list, OMP_CLAUSE_HINT, "hint", location);
31857 c = build_omp_clause (location, OMP_CLAUSE_HINT);
31858 OMP_CLAUSE_HINT_EXPR (c) = t;
31859 OMP_CLAUSE_CHAIN (c) = list;
31861 return c;
31864 /* OpenMP 4.5:
31865 defaultmap ( tofrom : scalar ) */
31867 static tree
31868 cp_parser_omp_clause_defaultmap (cp_parser *parser, tree list,
31869 location_t location)
31871 tree c, id;
31872 const char *p;
31874 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31875 return list;
31877 if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME))
31879 cp_parser_error (parser, "expected %<tofrom%>");
31880 goto out_err;
31882 id = cp_lexer_peek_token (parser->lexer)->u.value;
31883 p = IDENTIFIER_POINTER (id);
31884 if (strcmp (p, "tofrom") != 0)
31886 cp_parser_error (parser, "expected %<tofrom%>");
31887 goto out_err;
31889 cp_lexer_consume_token (parser->lexer);
31890 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
31891 goto out_err;
31893 if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME))
31895 cp_parser_error (parser, "expected %<scalar%>");
31896 goto out_err;
31898 id = cp_lexer_peek_token (parser->lexer)->u.value;
31899 p = IDENTIFIER_POINTER (id);
31900 if (strcmp (p, "scalar") != 0)
31902 cp_parser_error (parser, "expected %<scalar%>");
31903 goto out_err;
31905 cp_lexer_consume_token (parser->lexer);
31906 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31907 goto out_err;
31909 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULTMAP, "defaultmap",
31910 location);
31912 c = build_omp_clause (location, OMP_CLAUSE_DEFAULTMAP);
31913 OMP_CLAUSE_CHAIN (c) = list;
31914 return c;
31916 out_err:
31917 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31918 /*or_comma=*/false,
31919 /*consume_paren=*/true);
31920 return list;
31923 /* OpenMP 2.5:
31924 ordered
31926 OpenMP 4.5:
31927 ordered ( constant-expression ) */
31929 static tree
31930 cp_parser_omp_clause_ordered (cp_parser *parser,
31931 tree list, location_t location)
31933 tree c, num = NULL_TREE;
31934 HOST_WIDE_INT n;
31936 check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED,
31937 "ordered", location);
31939 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
31941 cp_lexer_consume_token (parser->lexer);
31943 num = cp_parser_constant_expression (parser);
31945 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31946 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31947 /*or_comma=*/false,
31948 /*consume_paren=*/true);
31950 if (num == error_mark_node)
31951 return list;
31952 num = fold_non_dependent_expr (num);
31953 if (!tree_fits_shwi_p (num)
31954 || !INTEGRAL_TYPE_P (TREE_TYPE (num))
31955 || (n = tree_to_shwi (num)) <= 0
31956 || (int) n != n)
31958 error_at (location,
31959 "ordered argument needs positive constant integer "
31960 "expression");
31961 return list;
31965 c = build_omp_clause (location, OMP_CLAUSE_ORDERED);
31966 OMP_CLAUSE_ORDERED_EXPR (c) = num;
31967 OMP_CLAUSE_CHAIN (c) = list;
31968 return c;
31971 /* OpenMP 2.5:
31972 reduction ( reduction-operator : variable-list )
31974 reduction-operator:
31975 One of: + * - & ^ | && ||
31977 OpenMP 3.1:
31979 reduction-operator:
31980 One of: + * - & ^ | && || min max
31982 OpenMP 4.0:
31984 reduction-operator:
31985 One of: + * - & ^ | && ||
31986 id-expression */
31988 static tree
31989 cp_parser_omp_clause_reduction (cp_parser *parser, tree list)
31991 enum tree_code code = ERROR_MARK;
31992 tree nlist, c, id = NULL_TREE;
31994 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31995 return list;
31997 switch (cp_lexer_peek_token (parser->lexer)->type)
31999 case CPP_PLUS: code = PLUS_EXPR; break;
32000 case CPP_MULT: code = MULT_EXPR; break;
32001 case CPP_MINUS: code = MINUS_EXPR; break;
32002 case CPP_AND: code = BIT_AND_EXPR; break;
32003 case CPP_XOR: code = BIT_XOR_EXPR; break;
32004 case CPP_OR: code = BIT_IOR_EXPR; break;
32005 case CPP_AND_AND: code = TRUTH_ANDIF_EXPR; break;
32006 case CPP_OR_OR: code = TRUTH_ORIF_EXPR; break;
32007 default: break;
32010 if (code != ERROR_MARK)
32011 cp_lexer_consume_token (parser->lexer);
32012 else
32014 bool saved_colon_corrects_to_scope_p;
32015 saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
32016 parser->colon_corrects_to_scope_p = false;
32017 id = cp_parser_id_expression (parser, /*template_p=*/false,
32018 /*check_dependency_p=*/true,
32019 /*template_p=*/NULL,
32020 /*declarator_p=*/false,
32021 /*optional_p=*/false);
32022 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
32023 if (identifier_p (id))
32025 const char *p = IDENTIFIER_POINTER (id);
32027 if (strcmp (p, "min") == 0)
32028 code = MIN_EXPR;
32029 else if (strcmp (p, "max") == 0)
32030 code = MAX_EXPR;
32031 else if (id == cp_operator_id (PLUS_EXPR))
32032 code = PLUS_EXPR;
32033 else if (id == cp_operator_id (MULT_EXPR))
32034 code = MULT_EXPR;
32035 else if (id == cp_operator_id (MINUS_EXPR))
32036 code = MINUS_EXPR;
32037 else if (id == cp_operator_id (BIT_AND_EXPR))
32038 code = BIT_AND_EXPR;
32039 else if (id == cp_operator_id (BIT_IOR_EXPR))
32040 code = BIT_IOR_EXPR;
32041 else if (id == cp_operator_id (BIT_XOR_EXPR))
32042 code = BIT_XOR_EXPR;
32043 else if (id == cp_operator_id (TRUTH_ANDIF_EXPR))
32044 code = TRUTH_ANDIF_EXPR;
32045 else if (id == cp_operator_id (TRUTH_ORIF_EXPR))
32046 code = TRUTH_ORIF_EXPR;
32047 id = omp_reduction_id (code, id, NULL_TREE);
32048 tree scope = parser->scope;
32049 if (scope)
32050 id = build_qualified_name (NULL_TREE, scope, id, false);
32051 parser->scope = NULL_TREE;
32052 parser->qualifying_scope = NULL_TREE;
32053 parser->object_scope = NULL_TREE;
32055 else
32057 error ("invalid reduction-identifier");
32058 resync_fail:
32059 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32060 /*or_comma=*/false,
32061 /*consume_paren=*/true);
32062 return list;
32066 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
32067 goto resync_fail;
32069 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_REDUCTION, list,
32070 NULL);
32071 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
32073 OMP_CLAUSE_REDUCTION_CODE (c) = code;
32074 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = id;
32077 return nlist;
32080 /* OpenMP 2.5:
32081 schedule ( schedule-kind )
32082 schedule ( schedule-kind , expression )
32084 schedule-kind:
32085 static | dynamic | guided | runtime | auto
32087 OpenMP 4.5:
32088 schedule ( schedule-modifier : schedule-kind )
32089 schedule ( schedule-modifier [ , schedule-modifier ] : schedule-kind , expression )
32091 schedule-modifier:
32092 simd
32093 monotonic
32094 nonmonotonic */
32096 static tree
32097 cp_parser_omp_clause_schedule (cp_parser *parser, tree list, location_t location)
32099 tree c, t;
32100 int modifiers = 0, nmodifiers = 0;
32102 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
32103 return list;
32105 c = build_omp_clause (location, OMP_CLAUSE_SCHEDULE);
32107 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32109 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
32110 const char *p = IDENTIFIER_POINTER (id);
32111 if (strcmp ("simd", p) == 0)
32112 OMP_CLAUSE_SCHEDULE_SIMD (c) = 1;
32113 else if (strcmp ("monotonic", p) == 0)
32114 modifiers |= OMP_CLAUSE_SCHEDULE_MONOTONIC;
32115 else if (strcmp ("nonmonotonic", p) == 0)
32116 modifiers |= OMP_CLAUSE_SCHEDULE_NONMONOTONIC;
32117 else
32118 break;
32119 cp_lexer_consume_token (parser->lexer);
32120 if (nmodifiers++ == 0
32121 && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
32122 cp_lexer_consume_token (parser->lexer);
32123 else
32125 cp_parser_require (parser, CPP_COLON, RT_COLON);
32126 break;
32130 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32132 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
32133 const char *p = IDENTIFIER_POINTER (id);
32135 switch (p[0])
32137 case 'd':
32138 if (strcmp ("dynamic", p) != 0)
32139 goto invalid_kind;
32140 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
32141 break;
32143 case 'g':
32144 if (strcmp ("guided", p) != 0)
32145 goto invalid_kind;
32146 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
32147 break;
32149 case 'r':
32150 if (strcmp ("runtime", p) != 0)
32151 goto invalid_kind;
32152 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
32153 break;
32155 default:
32156 goto invalid_kind;
32159 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
32160 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
32161 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
32162 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_AUTO;
32163 else
32164 goto invalid_kind;
32165 cp_lexer_consume_token (parser->lexer);
32167 if ((modifiers & (OMP_CLAUSE_SCHEDULE_MONOTONIC
32168 | OMP_CLAUSE_SCHEDULE_NONMONOTONIC))
32169 == (OMP_CLAUSE_SCHEDULE_MONOTONIC
32170 | OMP_CLAUSE_SCHEDULE_NONMONOTONIC))
32172 error_at (location, "both %<monotonic%> and %<nonmonotonic%> modifiers "
32173 "specified");
32174 modifiers = 0;
32177 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
32179 cp_token *token;
32180 cp_lexer_consume_token (parser->lexer);
32182 token = cp_lexer_peek_token (parser->lexer);
32183 t = cp_parser_assignment_expression (parser);
32185 if (t == error_mark_node)
32186 goto resync_fail;
32187 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
32188 error_at (token->location, "schedule %<runtime%> does not take "
32189 "a %<chunk_size%> parameter");
32190 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_AUTO)
32191 error_at (token->location, "schedule %<auto%> does not take "
32192 "a %<chunk_size%> parameter");
32193 else
32194 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
32196 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
32197 goto resync_fail;
32199 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
32200 goto resync_fail;
32202 OMP_CLAUSE_SCHEDULE_KIND (c)
32203 = (enum omp_clause_schedule_kind)
32204 (OMP_CLAUSE_SCHEDULE_KIND (c) | modifiers);
32206 check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule", location);
32207 OMP_CLAUSE_CHAIN (c) = list;
32208 return c;
32210 invalid_kind:
32211 cp_parser_error (parser, "invalid schedule kind");
32212 resync_fail:
32213 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32214 /*or_comma=*/false,
32215 /*consume_paren=*/true);
32216 return list;
32219 /* OpenMP 3.0:
32220 untied */
32222 static tree
32223 cp_parser_omp_clause_untied (cp_parser * /*parser*/,
32224 tree list, location_t location)
32226 tree c;
32228 check_no_duplicate_clause (list, OMP_CLAUSE_UNTIED, "untied", location);
32230 c = build_omp_clause (location, OMP_CLAUSE_UNTIED);
32231 OMP_CLAUSE_CHAIN (c) = list;
32232 return c;
32235 /* OpenMP 4.0:
32236 inbranch
32237 notinbranch */
32239 static tree
32240 cp_parser_omp_clause_branch (cp_parser * /*parser*/, enum omp_clause_code code,
32241 tree list, location_t location)
32243 check_no_duplicate_clause (list, code, omp_clause_code_name[code], location);
32244 tree c = build_omp_clause (location, code);
32245 OMP_CLAUSE_CHAIN (c) = list;
32246 return c;
32249 /* OpenMP 4.0:
32250 parallel
32252 sections
32253 taskgroup */
32255 static tree
32256 cp_parser_omp_clause_cancelkind (cp_parser * /*parser*/,
32257 enum omp_clause_code code,
32258 tree list, location_t location)
32260 tree c = build_omp_clause (location, code);
32261 OMP_CLAUSE_CHAIN (c) = list;
32262 return c;
32265 /* OpenMP 4.5:
32266 nogroup */
32268 static tree
32269 cp_parser_omp_clause_nogroup (cp_parser * /*parser*/,
32270 tree list, location_t location)
32272 check_no_duplicate_clause (list, OMP_CLAUSE_NOGROUP, "nogroup", location);
32273 tree c = build_omp_clause (location, OMP_CLAUSE_NOGROUP);
32274 OMP_CLAUSE_CHAIN (c) = list;
32275 return c;
32278 /* OpenMP 4.5:
32279 simd
32280 threads */
32282 static tree
32283 cp_parser_omp_clause_orderedkind (cp_parser * /*parser*/,
32284 enum omp_clause_code code,
32285 tree list, location_t location)
32287 check_no_duplicate_clause (list, code, omp_clause_code_name[code], location);
32288 tree c = build_omp_clause (location, code);
32289 OMP_CLAUSE_CHAIN (c) = list;
32290 return c;
32293 /* OpenMP 4.0:
32294 num_teams ( expression ) */
32296 static tree
32297 cp_parser_omp_clause_num_teams (cp_parser *parser, tree list,
32298 location_t location)
32300 tree t, c;
32302 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
32303 return list;
32305 t = cp_parser_expression (parser);
32307 if (t == error_mark_node
32308 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
32309 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32310 /*or_comma=*/false,
32311 /*consume_paren=*/true);
32313 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_TEAMS,
32314 "num_teams", location);
32316 c = build_omp_clause (location, OMP_CLAUSE_NUM_TEAMS);
32317 OMP_CLAUSE_NUM_TEAMS_EXPR (c) = t;
32318 OMP_CLAUSE_CHAIN (c) = list;
32320 return c;
32323 /* OpenMP 4.0:
32324 thread_limit ( expression ) */
32326 static tree
32327 cp_parser_omp_clause_thread_limit (cp_parser *parser, tree list,
32328 location_t location)
32330 tree t, c;
32332 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
32333 return list;
32335 t = cp_parser_expression (parser);
32337 if (t == error_mark_node
32338 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
32339 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32340 /*or_comma=*/false,
32341 /*consume_paren=*/true);
32343 check_no_duplicate_clause (list, OMP_CLAUSE_THREAD_LIMIT,
32344 "thread_limit", location);
32346 c = build_omp_clause (location, OMP_CLAUSE_THREAD_LIMIT);
32347 OMP_CLAUSE_THREAD_LIMIT_EXPR (c) = t;
32348 OMP_CLAUSE_CHAIN (c) = list;
32350 return c;
32353 /* OpenMP 4.0:
32354 aligned ( variable-list )
32355 aligned ( variable-list : constant-expression ) */
32357 static tree
32358 cp_parser_omp_clause_aligned (cp_parser *parser, tree list)
32360 tree nlist, c, alignment = NULL_TREE;
32361 bool colon;
32363 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
32364 return list;
32366 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_ALIGNED, list,
32367 &colon);
32369 if (colon)
32371 alignment = cp_parser_constant_expression (parser);
32373 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
32374 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32375 /*or_comma=*/false,
32376 /*consume_paren=*/true);
32378 if (alignment == error_mark_node)
32379 alignment = NULL_TREE;
32382 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
32383 OMP_CLAUSE_ALIGNED_ALIGNMENT (c) = alignment;
32385 return nlist;
32388 /* OpenMP 4.0:
32389 linear ( variable-list )
32390 linear ( variable-list : expression )
32392 OpenMP 4.5:
32393 linear ( modifier ( variable-list ) )
32394 linear ( modifier ( variable-list ) : expression ) */
32396 static tree
32397 cp_parser_omp_clause_linear (cp_parser *parser, tree list,
32398 bool is_cilk_simd_fn, bool declare_simd)
32400 tree nlist, c, step = integer_one_node;
32401 bool colon;
32402 enum omp_clause_linear_kind kind = OMP_CLAUSE_LINEAR_DEFAULT;
32404 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
32405 return list;
32407 if (!is_cilk_simd_fn
32408 && cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32410 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
32411 const char *p = IDENTIFIER_POINTER (id);
32413 if (strcmp ("ref", p) == 0)
32414 kind = OMP_CLAUSE_LINEAR_REF;
32415 else if (strcmp ("val", p) == 0)
32416 kind = OMP_CLAUSE_LINEAR_VAL;
32417 else if (strcmp ("uval", p) == 0)
32418 kind = OMP_CLAUSE_LINEAR_UVAL;
32419 if (cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_PAREN))
32420 cp_lexer_consume_token (parser->lexer);
32421 else
32422 kind = OMP_CLAUSE_LINEAR_DEFAULT;
32425 if (kind == OMP_CLAUSE_LINEAR_DEFAULT)
32426 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_LINEAR, list,
32427 &colon);
32428 else
32430 nlist = cp_parser_omp_var_list (parser, OMP_CLAUSE_LINEAR, list);
32431 colon = cp_lexer_next_token_is (parser->lexer, CPP_COLON);
32432 if (colon)
32433 cp_parser_require (parser, CPP_COLON, RT_COLON);
32434 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
32435 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32436 /*or_comma=*/false,
32437 /*consume_paren=*/true);
32440 if (colon)
32442 step = NULL_TREE;
32443 if (declare_simd
32444 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
32445 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_CLOSE_PAREN))
32447 cp_token *token = cp_lexer_peek_token (parser->lexer);
32448 cp_parser_parse_tentatively (parser);
32449 step = cp_parser_id_expression (parser, /*template_p=*/false,
32450 /*check_dependency_p=*/true,
32451 /*template_p=*/NULL,
32452 /*declarator_p=*/false,
32453 /*optional_p=*/false);
32454 if (step != error_mark_node)
32455 step = cp_parser_lookup_name_simple (parser, step, token->location);
32456 if (step == error_mark_node)
32458 step = NULL_TREE;
32459 cp_parser_abort_tentative_parse (parser);
32461 else if (!cp_parser_parse_definitely (parser))
32462 step = NULL_TREE;
32464 if (!step)
32465 step = cp_parser_expression (parser);
32467 if (is_cilk_simd_fn && TREE_CODE (step) == PARM_DECL)
32469 sorry ("using parameters for %<linear%> step is not supported yet");
32470 step = integer_one_node;
32472 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
32473 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32474 /*or_comma=*/false,
32475 /*consume_paren=*/true);
32477 if (step == error_mark_node)
32478 return list;
32481 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
32483 OMP_CLAUSE_LINEAR_STEP (c) = step;
32484 OMP_CLAUSE_LINEAR_KIND (c) = kind;
32487 return nlist;
32490 /* OpenMP 4.0:
32491 safelen ( constant-expression ) */
32493 static tree
32494 cp_parser_omp_clause_safelen (cp_parser *parser, tree list,
32495 location_t location)
32497 tree t, c;
32499 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
32500 return list;
32502 t = cp_parser_constant_expression (parser);
32504 if (t == error_mark_node
32505 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
32506 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32507 /*or_comma=*/false,
32508 /*consume_paren=*/true);
32510 check_no_duplicate_clause (list, OMP_CLAUSE_SAFELEN, "safelen", location);
32512 c = build_omp_clause (location, OMP_CLAUSE_SAFELEN);
32513 OMP_CLAUSE_SAFELEN_EXPR (c) = t;
32514 OMP_CLAUSE_CHAIN (c) = list;
32516 return c;
32519 /* OpenMP 4.0:
32520 simdlen ( constant-expression ) */
32522 static tree
32523 cp_parser_omp_clause_simdlen (cp_parser *parser, tree list,
32524 location_t location)
32526 tree t, c;
32528 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
32529 return list;
32531 t = cp_parser_constant_expression (parser);
32533 if (t == error_mark_node
32534 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
32535 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32536 /*or_comma=*/false,
32537 /*consume_paren=*/true);
32539 check_no_duplicate_clause (list, OMP_CLAUSE_SIMDLEN, "simdlen", location);
32541 c = build_omp_clause (location, OMP_CLAUSE_SIMDLEN);
32542 OMP_CLAUSE_SIMDLEN_EXPR (c) = t;
32543 OMP_CLAUSE_CHAIN (c) = list;
32545 return c;
32548 /* OpenMP 4.5:
32549 vec:
32550 identifier [+/- integer]
32551 vec , identifier [+/- integer]
32554 static tree
32555 cp_parser_omp_clause_depend_sink (cp_parser *parser, location_t clause_loc,
32556 tree list)
32558 tree vec = NULL;
32560 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
32562 cp_parser_error (parser, "expected identifier");
32563 return list;
32566 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32568 location_t id_loc = cp_lexer_peek_token (parser->lexer)->location;
32569 tree t, identifier = cp_parser_identifier (parser);
32570 tree addend = NULL;
32572 if (identifier == error_mark_node)
32573 t = error_mark_node;
32574 else
32576 t = cp_parser_lookup_name_simple
32577 (parser, identifier,
32578 cp_lexer_peek_token (parser->lexer)->location);
32579 if (t == error_mark_node)
32580 cp_parser_name_lookup_error (parser, identifier, t, NLE_NULL,
32581 id_loc);
32584 bool neg = false;
32585 if (cp_lexer_next_token_is (parser->lexer, CPP_MINUS))
32586 neg = true;
32587 else if (!cp_lexer_next_token_is (parser->lexer, CPP_PLUS))
32589 addend = integer_zero_node;
32590 goto add_to_vector;
32592 cp_lexer_consume_token (parser->lexer);
32594 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NUMBER))
32596 cp_parser_error (parser, "expected integer");
32597 return list;
32600 addend = cp_lexer_peek_token (parser->lexer)->u.value;
32601 if (TREE_CODE (addend) != INTEGER_CST)
32603 cp_parser_error (parser, "expected integer");
32604 return list;
32606 cp_lexer_consume_token (parser->lexer);
32608 add_to_vector:
32609 if (t != error_mark_node)
32611 vec = tree_cons (addend, t, vec);
32612 if (neg)
32613 OMP_CLAUSE_DEPEND_SINK_NEGATIVE (vec) = 1;
32616 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
32617 break;
32619 cp_lexer_consume_token (parser->lexer);
32622 if (cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN) && vec)
32624 tree u = build_omp_clause (clause_loc, OMP_CLAUSE_DEPEND);
32625 OMP_CLAUSE_DEPEND_KIND (u) = OMP_CLAUSE_DEPEND_SINK;
32626 OMP_CLAUSE_DECL (u) = nreverse (vec);
32627 OMP_CLAUSE_CHAIN (u) = list;
32628 return u;
32630 return list;
32633 /* OpenMP 4.0:
32634 depend ( depend-kind : variable-list )
32636 depend-kind:
32637 in | out | inout
32639 OpenMP 4.5:
32640 depend ( source )
32642 depend ( sink : vec ) */
32644 static tree
32645 cp_parser_omp_clause_depend (cp_parser *parser, tree list, location_t loc)
32647 tree nlist, c;
32648 enum omp_clause_depend_kind kind = OMP_CLAUSE_DEPEND_INOUT;
32650 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
32651 return list;
32653 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32655 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
32656 const char *p = IDENTIFIER_POINTER (id);
32658 if (strcmp ("in", p) == 0)
32659 kind = OMP_CLAUSE_DEPEND_IN;
32660 else if (strcmp ("inout", p) == 0)
32661 kind = OMP_CLAUSE_DEPEND_INOUT;
32662 else if (strcmp ("out", p) == 0)
32663 kind = OMP_CLAUSE_DEPEND_OUT;
32664 else if (strcmp ("source", p) == 0)
32665 kind = OMP_CLAUSE_DEPEND_SOURCE;
32666 else if (strcmp ("sink", p) == 0)
32667 kind = OMP_CLAUSE_DEPEND_SINK;
32668 else
32669 goto invalid_kind;
32671 else
32672 goto invalid_kind;
32674 cp_lexer_consume_token (parser->lexer);
32676 if (kind == OMP_CLAUSE_DEPEND_SOURCE)
32678 c = build_omp_clause (loc, OMP_CLAUSE_DEPEND);
32679 OMP_CLAUSE_DEPEND_KIND (c) = kind;
32680 OMP_CLAUSE_DECL (c) = NULL_TREE;
32681 OMP_CLAUSE_CHAIN (c) = list;
32682 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
32683 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32684 /*or_comma=*/false,
32685 /*consume_paren=*/true);
32686 return c;
32689 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
32690 goto resync_fail;
32692 if (kind == OMP_CLAUSE_DEPEND_SINK)
32693 nlist = cp_parser_omp_clause_depend_sink (parser, loc, list);
32694 else
32696 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_DEPEND,
32697 list, NULL);
32699 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
32700 OMP_CLAUSE_DEPEND_KIND (c) = kind;
32702 return nlist;
32704 invalid_kind:
32705 cp_parser_error (parser, "invalid depend kind");
32706 resync_fail:
32707 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32708 /*or_comma=*/false,
32709 /*consume_paren=*/true);
32710 return list;
32713 /* OpenMP 4.0:
32714 map ( map-kind : variable-list )
32715 map ( variable-list )
32717 map-kind:
32718 alloc | to | from | tofrom
32720 OpenMP 4.5:
32721 map-kind:
32722 alloc | to | from | tofrom | release | delete
32724 map ( always [,] map-kind: variable-list ) */
32726 static tree
32727 cp_parser_omp_clause_map (cp_parser *parser, tree list)
32729 tree nlist, c;
32730 enum gomp_map_kind kind = GOMP_MAP_TOFROM;
32731 bool always = false;
32733 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
32734 return list;
32736 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32738 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
32739 const char *p = IDENTIFIER_POINTER (id);
32741 if (strcmp ("always", p) == 0)
32743 int nth = 2;
32744 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COMMA)
32745 nth++;
32746 if ((cp_lexer_peek_nth_token (parser->lexer, nth)->type == CPP_NAME
32747 || (cp_lexer_peek_nth_token (parser->lexer, nth)->keyword
32748 == RID_DELETE))
32749 && (cp_lexer_peek_nth_token (parser->lexer, nth + 1)->type
32750 == CPP_COLON))
32752 always = true;
32753 cp_lexer_consume_token (parser->lexer);
32754 if (nth == 3)
32755 cp_lexer_consume_token (parser->lexer);
32760 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
32761 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
32763 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
32764 const char *p = IDENTIFIER_POINTER (id);
32766 if (strcmp ("alloc", p) == 0)
32767 kind = GOMP_MAP_ALLOC;
32768 else if (strcmp ("to", p) == 0)
32769 kind = always ? GOMP_MAP_ALWAYS_TO : GOMP_MAP_TO;
32770 else if (strcmp ("from", p) == 0)
32771 kind = always ? GOMP_MAP_ALWAYS_FROM : GOMP_MAP_FROM;
32772 else if (strcmp ("tofrom", p) == 0)
32773 kind = always ? GOMP_MAP_ALWAYS_TOFROM : GOMP_MAP_TOFROM;
32774 else if (strcmp ("release", p) == 0)
32775 kind = GOMP_MAP_RELEASE;
32776 else
32778 cp_parser_error (parser, "invalid map kind");
32779 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32780 /*or_comma=*/false,
32781 /*consume_paren=*/true);
32782 return list;
32784 cp_lexer_consume_token (parser->lexer);
32785 cp_lexer_consume_token (parser->lexer);
32787 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DELETE)
32788 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
32790 kind = GOMP_MAP_DELETE;
32791 cp_lexer_consume_token (parser->lexer);
32792 cp_lexer_consume_token (parser->lexer);
32795 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_MAP, list,
32796 NULL);
32798 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
32799 OMP_CLAUSE_SET_MAP_KIND (c, kind);
32801 return nlist;
32804 /* OpenMP 4.0:
32805 device ( expression ) */
32807 static tree
32808 cp_parser_omp_clause_device (cp_parser *parser, tree list,
32809 location_t location)
32811 tree t, c;
32813 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
32814 return list;
32816 t = cp_parser_expression (parser);
32818 if (t == error_mark_node
32819 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
32820 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32821 /*or_comma=*/false,
32822 /*consume_paren=*/true);
32824 check_no_duplicate_clause (list, OMP_CLAUSE_DEVICE,
32825 "device", location);
32827 c = build_omp_clause (location, OMP_CLAUSE_DEVICE);
32828 OMP_CLAUSE_DEVICE_ID (c) = t;
32829 OMP_CLAUSE_CHAIN (c) = list;
32831 return c;
32834 /* OpenMP 4.0:
32835 dist_schedule ( static )
32836 dist_schedule ( static , expression ) */
32838 static tree
32839 cp_parser_omp_clause_dist_schedule (cp_parser *parser, tree list,
32840 location_t location)
32842 tree c, t;
32844 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
32845 return list;
32847 c = build_omp_clause (location, OMP_CLAUSE_DIST_SCHEDULE);
32849 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
32850 goto invalid_kind;
32851 cp_lexer_consume_token (parser->lexer);
32853 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
32855 cp_lexer_consume_token (parser->lexer);
32857 t = cp_parser_assignment_expression (parser);
32859 if (t == error_mark_node)
32860 goto resync_fail;
32861 OMP_CLAUSE_DIST_SCHEDULE_CHUNK_EXPR (c) = t;
32863 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
32864 goto resync_fail;
32866 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
32867 goto resync_fail;
32869 check_no_duplicate_clause (list, OMP_CLAUSE_DIST_SCHEDULE, "dist_schedule",
32870 location);
32871 OMP_CLAUSE_CHAIN (c) = list;
32872 return c;
32874 invalid_kind:
32875 cp_parser_error (parser, "invalid dist_schedule kind");
32876 resync_fail:
32877 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32878 /*or_comma=*/false,
32879 /*consume_paren=*/true);
32880 return list;
32883 /* OpenMP 4.0:
32884 proc_bind ( proc-bind-kind )
32886 proc-bind-kind:
32887 master | close | spread */
32889 static tree
32890 cp_parser_omp_clause_proc_bind (cp_parser *parser, tree list,
32891 location_t location)
32893 tree c;
32894 enum omp_clause_proc_bind_kind kind;
32896 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
32897 return list;
32899 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32901 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
32902 const char *p = IDENTIFIER_POINTER (id);
32904 if (strcmp ("master", p) == 0)
32905 kind = OMP_CLAUSE_PROC_BIND_MASTER;
32906 else if (strcmp ("close", p) == 0)
32907 kind = OMP_CLAUSE_PROC_BIND_CLOSE;
32908 else if (strcmp ("spread", p) == 0)
32909 kind = OMP_CLAUSE_PROC_BIND_SPREAD;
32910 else
32911 goto invalid_kind;
32913 else
32914 goto invalid_kind;
32916 cp_lexer_consume_token (parser->lexer);
32917 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
32918 goto resync_fail;
32920 c = build_omp_clause (location, OMP_CLAUSE_PROC_BIND);
32921 check_no_duplicate_clause (list, OMP_CLAUSE_PROC_BIND, "proc_bind",
32922 location);
32923 OMP_CLAUSE_PROC_BIND_KIND (c) = kind;
32924 OMP_CLAUSE_CHAIN (c) = list;
32925 return c;
32927 invalid_kind:
32928 cp_parser_error (parser, "invalid depend kind");
32929 resync_fail:
32930 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32931 /*or_comma=*/false,
32932 /*consume_paren=*/true);
32933 return list;
32936 /* OpenACC:
32937 async [( int-expr )] */
32939 static tree
32940 cp_parser_oacc_clause_async (cp_parser *parser, tree list)
32942 tree c, t;
32943 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
32945 t = build_int_cst (integer_type_node, GOMP_ASYNC_NOVAL);
32947 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
32949 cp_lexer_consume_token (parser->lexer);
32951 t = cp_parser_expression (parser);
32952 if (t == error_mark_node
32953 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
32954 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32955 /*or_comma=*/false,
32956 /*consume_paren=*/true);
32959 check_no_duplicate_clause (list, OMP_CLAUSE_ASYNC, "async", loc);
32961 c = build_omp_clause (loc, OMP_CLAUSE_ASYNC);
32962 OMP_CLAUSE_ASYNC_EXPR (c) = t;
32963 OMP_CLAUSE_CHAIN (c) = list;
32964 list = c;
32966 return list;
32969 /* Parse all OpenACC clauses. The set clauses allowed by the directive
32970 is a bitmask in MASK. Return the list of clauses found. */
32972 static tree
32973 cp_parser_oacc_all_clauses (cp_parser *parser, omp_clause_mask mask,
32974 const char *where, cp_token *pragma_tok,
32975 bool finish_p = true)
32977 tree clauses = NULL;
32978 bool first = true;
32980 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
32982 location_t here;
32983 pragma_omp_clause c_kind;
32984 omp_clause_code code;
32985 const char *c_name;
32986 tree prev = clauses;
32988 if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
32989 cp_lexer_consume_token (parser->lexer);
32991 here = cp_lexer_peek_token (parser->lexer)->location;
32992 c_kind = cp_parser_omp_clause_name (parser);
32994 switch (c_kind)
32996 case PRAGMA_OACC_CLAUSE_ASYNC:
32997 clauses = cp_parser_oacc_clause_async (parser, clauses);
32998 c_name = "async";
32999 break;
33000 case PRAGMA_OACC_CLAUSE_AUTO:
33001 clauses = cp_parser_oacc_simple_clause (parser, OMP_CLAUSE_AUTO,
33002 clauses, here);
33003 c_name = "auto";
33004 break;
33005 case PRAGMA_OACC_CLAUSE_COLLAPSE:
33006 clauses = cp_parser_omp_clause_collapse (parser, clauses, here);
33007 c_name = "collapse";
33008 break;
33009 case PRAGMA_OACC_CLAUSE_COPY:
33010 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33011 c_name = "copy";
33012 break;
33013 case PRAGMA_OACC_CLAUSE_COPYIN:
33014 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33015 c_name = "copyin";
33016 break;
33017 case PRAGMA_OACC_CLAUSE_COPYOUT:
33018 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33019 c_name = "copyout";
33020 break;
33021 case PRAGMA_OACC_CLAUSE_CREATE:
33022 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33023 c_name = "create";
33024 break;
33025 case PRAGMA_OACC_CLAUSE_DELETE:
33026 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33027 c_name = "delete";
33028 break;
33029 case PRAGMA_OMP_CLAUSE_DEFAULT:
33030 clauses = cp_parser_omp_clause_default (parser, clauses, here, true);
33031 c_name = "default";
33032 break;
33033 case PRAGMA_OACC_CLAUSE_DEVICE:
33034 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33035 c_name = "device";
33036 break;
33037 case PRAGMA_OACC_CLAUSE_DEVICEPTR:
33038 clauses = cp_parser_oacc_data_clause_deviceptr (parser, clauses);
33039 c_name = "deviceptr";
33040 break;
33041 case PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT:
33042 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33043 c_name = "device_resident";
33044 break;
33045 case PRAGMA_OACC_CLAUSE_FIRSTPRIVATE:
33046 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
33047 clauses);
33048 c_name = "firstprivate";
33049 break;
33050 case PRAGMA_OACC_CLAUSE_GANG:
33051 c_name = "gang";
33052 clauses = cp_parser_oacc_shape_clause (parser, OMP_CLAUSE_GANG,
33053 c_name, clauses);
33054 break;
33055 case PRAGMA_OACC_CLAUSE_HOST:
33056 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33057 c_name = "host";
33058 break;
33059 case PRAGMA_OACC_CLAUSE_IF:
33060 clauses = cp_parser_omp_clause_if (parser, clauses, here, false);
33061 c_name = "if";
33062 break;
33063 case PRAGMA_OACC_CLAUSE_INDEPENDENT:
33064 clauses = cp_parser_oacc_simple_clause (parser,
33065 OMP_CLAUSE_INDEPENDENT,
33066 clauses, here);
33067 c_name = "independent";
33068 break;
33069 case PRAGMA_OACC_CLAUSE_LINK:
33070 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33071 c_name = "link";
33072 break;
33073 case PRAGMA_OACC_CLAUSE_NUM_GANGS:
33074 code = OMP_CLAUSE_NUM_GANGS;
33075 c_name = "num_gangs";
33076 clauses = cp_parser_oacc_single_int_clause (parser, code, c_name,
33077 clauses);
33078 break;
33079 case PRAGMA_OACC_CLAUSE_NUM_WORKERS:
33080 c_name = "num_workers";
33081 code = OMP_CLAUSE_NUM_WORKERS;
33082 clauses = cp_parser_oacc_single_int_clause (parser, code, c_name,
33083 clauses);
33084 break;
33085 case PRAGMA_OACC_CLAUSE_PRESENT:
33086 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33087 c_name = "present";
33088 break;
33089 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY:
33090 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33091 c_name = "present_or_copy";
33092 break;
33093 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN:
33094 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33095 c_name = "present_or_copyin";
33096 break;
33097 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT:
33098 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33099 c_name = "present_or_copyout";
33100 break;
33101 case PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE:
33102 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33103 c_name = "present_or_create";
33104 break;
33105 case PRAGMA_OACC_CLAUSE_PRIVATE:
33106 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
33107 clauses);
33108 c_name = "private";
33109 break;
33110 case PRAGMA_OACC_CLAUSE_REDUCTION:
33111 clauses = cp_parser_omp_clause_reduction (parser, clauses);
33112 c_name = "reduction";
33113 break;
33114 case PRAGMA_OACC_CLAUSE_SELF:
33115 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33116 c_name = "self";
33117 break;
33118 case PRAGMA_OACC_CLAUSE_SEQ:
33119 clauses = cp_parser_oacc_simple_clause (parser, OMP_CLAUSE_SEQ,
33120 clauses, here);
33121 c_name = "seq";
33122 break;
33123 case PRAGMA_OACC_CLAUSE_TILE:
33124 clauses = cp_parser_oacc_clause_tile (parser, here, clauses);
33125 c_name = "tile";
33126 break;
33127 case PRAGMA_OACC_CLAUSE_USE_DEVICE:
33128 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_USE_DEVICE_PTR,
33129 clauses);
33130 c_name = "use_device";
33131 break;
33132 case PRAGMA_OACC_CLAUSE_VECTOR:
33133 c_name = "vector";
33134 clauses = cp_parser_oacc_shape_clause (parser, OMP_CLAUSE_VECTOR,
33135 c_name, clauses);
33136 break;
33137 case PRAGMA_OACC_CLAUSE_VECTOR_LENGTH:
33138 c_name = "vector_length";
33139 code = OMP_CLAUSE_VECTOR_LENGTH;
33140 clauses = cp_parser_oacc_single_int_clause (parser, code, c_name,
33141 clauses);
33142 break;
33143 case PRAGMA_OACC_CLAUSE_WAIT:
33144 clauses = cp_parser_oacc_clause_wait (parser, clauses);
33145 c_name = "wait";
33146 break;
33147 case PRAGMA_OACC_CLAUSE_WORKER:
33148 c_name = "worker";
33149 clauses = cp_parser_oacc_shape_clause (parser, OMP_CLAUSE_WORKER,
33150 c_name, clauses);
33151 break;
33152 default:
33153 cp_parser_error (parser, "expected %<#pragma acc%> clause");
33154 goto saw_error;
33157 first = false;
33159 if (((mask >> c_kind) & 1) == 0)
33161 /* Remove the invalid clause(s) from the list to avoid
33162 confusing the rest of the compiler. */
33163 clauses = prev;
33164 error_at (here, "%qs is not valid for %qs", c_name, where);
33168 saw_error:
33169 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
33171 if (finish_p)
33172 return finish_omp_clauses (clauses, C_ORT_ACC);
33174 return clauses;
33177 /* Parse all OpenMP clauses. The set clauses allowed by the directive
33178 is a bitmask in MASK. Return the list of clauses found; the result
33179 of clause default goes in *pdefault. */
33181 static tree
33182 cp_parser_omp_all_clauses (cp_parser *parser, omp_clause_mask mask,
33183 const char *where, cp_token *pragma_tok,
33184 bool finish_p = true)
33186 tree clauses = NULL;
33187 bool first = true;
33188 cp_token *token = NULL;
33190 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
33192 pragma_omp_clause c_kind;
33193 const char *c_name;
33194 tree prev = clauses;
33196 if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
33197 cp_lexer_consume_token (parser->lexer);
33199 token = cp_lexer_peek_token (parser->lexer);
33200 c_kind = cp_parser_omp_clause_name (parser);
33202 switch (c_kind)
33204 case PRAGMA_OMP_CLAUSE_COLLAPSE:
33205 clauses = cp_parser_omp_clause_collapse (parser, clauses,
33206 token->location);
33207 c_name = "collapse";
33208 break;
33209 case PRAGMA_OMP_CLAUSE_COPYIN:
33210 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYIN, clauses);
33211 c_name = "copyin";
33212 break;
33213 case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
33214 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYPRIVATE,
33215 clauses);
33216 c_name = "copyprivate";
33217 break;
33218 case PRAGMA_OMP_CLAUSE_DEFAULT:
33219 clauses = cp_parser_omp_clause_default (parser, clauses,
33220 token->location, false);
33221 c_name = "default";
33222 break;
33223 case PRAGMA_OMP_CLAUSE_FINAL:
33224 clauses = cp_parser_omp_clause_final (parser, clauses, token->location);
33225 c_name = "final";
33226 break;
33227 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
33228 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
33229 clauses);
33230 c_name = "firstprivate";
33231 break;
33232 case PRAGMA_OMP_CLAUSE_GRAINSIZE:
33233 clauses = cp_parser_omp_clause_grainsize (parser, clauses,
33234 token->location);
33235 c_name = "grainsize";
33236 break;
33237 case PRAGMA_OMP_CLAUSE_HINT:
33238 clauses = cp_parser_omp_clause_hint (parser, clauses,
33239 token->location);
33240 c_name = "hint";
33241 break;
33242 case PRAGMA_OMP_CLAUSE_DEFAULTMAP:
33243 clauses = cp_parser_omp_clause_defaultmap (parser, clauses,
33244 token->location);
33245 c_name = "defaultmap";
33246 break;
33247 case PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR:
33248 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_USE_DEVICE_PTR,
33249 clauses);
33250 c_name = "use_device_ptr";
33251 break;
33252 case PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR:
33253 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_IS_DEVICE_PTR,
33254 clauses);
33255 c_name = "is_device_ptr";
33256 break;
33257 case PRAGMA_OMP_CLAUSE_IF:
33258 clauses = cp_parser_omp_clause_if (parser, clauses, token->location,
33259 true);
33260 c_name = "if";
33261 break;
33262 case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
33263 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LASTPRIVATE,
33264 clauses);
33265 c_name = "lastprivate";
33266 break;
33267 case PRAGMA_OMP_CLAUSE_MERGEABLE:
33268 clauses = cp_parser_omp_clause_mergeable (parser, clauses,
33269 token->location);
33270 c_name = "mergeable";
33271 break;
33272 case PRAGMA_OMP_CLAUSE_NOWAIT:
33273 clauses = cp_parser_omp_clause_nowait (parser, clauses, token->location);
33274 c_name = "nowait";
33275 break;
33276 case PRAGMA_OMP_CLAUSE_NUM_TASKS:
33277 clauses = cp_parser_omp_clause_num_tasks (parser, clauses,
33278 token->location);
33279 c_name = "num_tasks";
33280 break;
33281 case PRAGMA_OMP_CLAUSE_NUM_THREADS:
33282 clauses = cp_parser_omp_clause_num_threads (parser, clauses,
33283 token->location);
33284 c_name = "num_threads";
33285 break;
33286 case PRAGMA_OMP_CLAUSE_ORDERED:
33287 clauses = cp_parser_omp_clause_ordered (parser, clauses,
33288 token->location);
33289 c_name = "ordered";
33290 break;
33291 case PRAGMA_OMP_CLAUSE_PRIORITY:
33292 clauses = cp_parser_omp_clause_priority (parser, clauses,
33293 token->location);
33294 c_name = "priority";
33295 break;
33296 case PRAGMA_OMP_CLAUSE_PRIVATE:
33297 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
33298 clauses);
33299 c_name = "private";
33300 break;
33301 case PRAGMA_OMP_CLAUSE_REDUCTION:
33302 clauses = cp_parser_omp_clause_reduction (parser, clauses);
33303 c_name = "reduction";
33304 break;
33305 case PRAGMA_OMP_CLAUSE_SCHEDULE:
33306 clauses = cp_parser_omp_clause_schedule (parser, clauses,
33307 token->location);
33308 c_name = "schedule";
33309 break;
33310 case PRAGMA_OMP_CLAUSE_SHARED:
33311 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_SHARED,
33312 clauses);
33313 c_name = "shared";
33314 break;
33315 case PRAGMA_OMP_CLAUSE_UNTIED:
33316 clauses = cp_parser_omp_clause_untied (parser, clauses,
33317 token->location);
33318 c_name = "untied";
33319 break;
33320 case PRAGMA_OMP_CLAUSE_INBRANCH:
33321 case PRAGMA_CILK_CLAUSE_MASK:
33322 clauses = cp_parser_omp_clause_branch (parser, OMP_CLAUSE_INBRANCH,
33323 clauses, token->location);
33324 c_name = "inbranch";
33325 break;
33326 case PRAGMA_OMP_CLAUSE_NOTINBRANCH:
33327 case PRAGMA_CILK_CLAUSE_NOMASK:
33328 clauses = cp_parser_omp_clause_branch (parser,
33329 OMP_CLAUSE_NOTINBRANCH,
33330 clauses, token->location);
33331 c_name = "notinbranch";
33332 break;
33333 case PRAGMA_OMP_CLAUSE_PARALLEL:
33334 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_PARALLEL,
33335 clauses, token->location);
33336 c_name = "parallel";
33337 if (!first)
33339 clause_not_first:
33340 error_at (token->location, "%qs must be the first clause of %qs",
33341 c_name, where);
33342 clauses = prev;
33344 break;
33345 case PRAGMA_OMP_CLAUSE_FOR:
33346 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_FOR,
33347 clauses, token->location);
33348 c_name = "for";
33349 if (!first)
33350 goto clause_not_first;
33351 break;
33352 case PRAGMA_OMP_CLAUSE_SECTIONS:
33353 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_SECTIONS,
33354 clauses, token->location);
33355 c_name = "sections";
33356 if (!first)
33357 goto clause_not_first;
33358 break;
33359 case PRAGMA_OMP_CLAUSE_TASKGROUP:
33360 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_TASKGROUP,
33361 clauses, token->location);
33362 c_name = "taskgroup";
33363 if (!first)
33364 goto clause_not_first;
33365 break;
33366 case PRAGMA_OMP_CLAUSE_LINK:
33367 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LINK, clauses);
33368 c_name = "to";
33369 break;
33370 case PRAGMA_OMP_CLAUSE_TO:
33371 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINK)) != 0)
33372 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_TO_DECLARE,
33373 clauses);
33374 else
33375 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_TO, clauses);
33376 c_name = "to";
33377 break;
33378 case PRAGMA_OMP_CLAUSE_FROM:
33379 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FROM, clauses);
33380 c_name = "from";
33381 break;
33382 case PRAGMA_OMP_CLAUSE_UNIFORM:
33383 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_UNIFORM,
33384 clauses);
33385 c_name = "uniform";
33386 break;
33387 case PRAGMA_OMP_CLAUSE_NUM_TEAMS:
33388 clauses = cp_parser_omp_clause_num_teams (parser, clauses,
33389 token->location);
33390 c_name = "num_teams";
33391 break;
33392 case PRAGMA_OMP_CLAUSE_THREAD_LIMIT:
33393 clauses = cp_parser_omp_clause_thread_limit (parser, clauses,
33394 token->location);
33395 c_name = "thread_limit";
33396 break;
33397 case PRAGMA_OMP_CLAUSE_ALIGNED:
33398 clauses = cp_parser_omp_clause_aligned (parser, clauses);
33399 c_name = "aligned";
33400 break;
33401 case PRAGMA_OMP_CLAUSE_LINEAR:
33403 bool cilk_simd_fn = false, declare_simd = false;
33404 if (((mask >> PRAGMA_CILK_CLAUSE_VECTORLENGTH) & 1) != 0)
33405 cilk_simd_fn = true;
33406 else if (((mask >> PRAGMA_OMP_CLAUSE_UNIFORM) & 1) != 0)
33407 declare_simd = true;
33408 clauses = cp_parser_omp_clause_linear (parser, clauses,
33409 cilk_simd_fn, declare_simd);
33411 c_name = "linear";
33412 break;
33413 case PRAGMA_OMP_CLAUSE_DEPEND:
33414 clauses = cp_parser_omp_clause_depend (parser, clauses,
33415 token->location);
33416 c_name = "depend";
33417 break;
33418 case PRAGMA_OMP_CLAUSE_MAP:
33419 clauses = cp_parser_omp_clause_map (parser, clauses);
33420 c_name = "map";
33421 break;
33422 case PRAGMA_OMP_CLAUSE_DEVICE:
33423 clauses = cp_parser_omp_clause_device (parser, clauses,
33424 token->location);
33425 c_name = "device";
33426 break;
33427 case PRAGMA_OMP_CLAUSE_DIST_SCHEDULE:
33428 clauses = cp_parser_omp_clause_dist_schedule (parser, clauses,
33429 token->location);
33430 c_name = "dist_schedule";
33431 break;
33432 case PRAGMA_OMP_CLAUSE_PROC_BIND:
33433 clauses = cp_parser_omp_clause_proc_bind (parser, clauses,
33434 token->location);
33435 c_name = "proc_bind";
33436 break;
33437 case PRAGMA_OMP_CLAUSE_SAFELEN:
33438 clauses = cp_parser_omp_clause_safelen (parser, clauses,
33439 token->location);
33440 c_name = "safelen";
33441 break;
33442 case PRAGMA_OMP_CLAUSE_SIMDLEN:
33443 clauses = cp_parser_omp_clause_simdlen (parser, clauses,
33444 token->location);
33445 c_name = "simdlen";
33446 break;
33447 case PRAGMA_OMP_CLAUSE_NOGROUP:
33448 clauses = cp_parser_omp_clause_nogroup (parser, clauses,
33449 token->location);
33450 c_name = "nogroup";
33451 break;
33452 case PRAGMA_OMP_CLAUSE_THREADS:
33453 clauses
33454 = cp_parser_omp_clause_orderedkind (parser, OMP_CLAUSE_THREADS,
33455 clauses, token->location);
33456 c_name = "threads";
33457 break;
33458 case PRAGMA_OMP_CLAUSE_SIMD:
33459 clauses
33460 = cp_parser_omp_clause_orderedkind (parser, OMP_CLAUSE_SIMD,
33461 clauses, token->location);
33462 c_name = "simd";
33463 break;
33464 case PRAGMA_CILK_CLAUSE_VECTORLENGTH:
33465 clauses = cp_parser_cilk_simd_vectorlength (parser, clauses, true);
33466 c_name = "simdlen";
33467 break;
33468 default:
33469 cp_parser_error (parser, "expected %<#pragma omp%> clause");
33470 goto saw_error;
33473 first = false;
33475 if (((mask >> c_kind) & 1) == 0)
33477 /* Remove the invalid clause(s) from the list to avoid
33478 confusing the rest of the compiler. */
33479 clauses = prev;
33480 error_at (token->location, "%qs is not valid for %qs", c_name, where);
33483 saw_error:
33484 /* In Cilk Plus SIMD enabled functions there is no pragma_token, so
33485 no reason to skip to the end. */
33486 if (!(flag_cilkplus && pragma_tok == NULL))
33487 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
33488 if (finish_p)
33490 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNIFORM)) != 0)
33491 return finish_omp_clauses (clauses, C_ORT_OMP_DECLARE_SIMD);
33492 else
33493 return finish_omp_clauses (clauses, C_ORT_OMP);
33495 return clauses;
33498 /* OpenMP 2.5:
33499 structured-block:
33500 statement
33502 In practice, we're also interested in adding the statement to an
33503 outer node. So it is convenient if we work around the fact that
33504 cp_parser_statement calls add_stmt. */
33506 static unsigned
33507 cp_parser_begin_omp_structured_block (cp_parser *parser)
33509 unsigned save = parser->in_statement;
33511 /* Only move the values to IN_OMP_BLOCK if they weren't false.
33512 This preserves the "not within loop or switch" style error messages
33513 for nonsense cases like
33514 void foo() {
33515 #pragma omp single
33516 break;
33519 if (parser->in_statement)
33520 parser->in_statement = IN_OMP_BLOCK;
33522 return save;
33525 static void
33526 cp_parser_end_omp_structured_block (cp_parser *parser, unsigned save)
33528 parser->in_statement = save;
33531 static tree
33532 cp_parser_omp_structured_block (cp_parser *parser, bool *if_p)
33534 tree stmt = begin_omp_structured_block ();
33535 unsigned int save = cp_parser_begin_omp_structured_block (parser);
33537 cp_parser_statement (parser, NULL_TREE, false, if_p);
33539 cp_parser_end_omp_structured_block (parser, save);
33540 return finish_omp_structured_block (stmt);
33543 /* OpenMP 2.5:
33544 # pragma omp atomic new-line
33545 expression-stmt
33547 expression-stmt:
33548 x binop= expr | x++ | ++x | x-- | --x
33549 binop:
33550 +, *, -, /, &, ^, |, <<, >>
33552 where x is an lvalue expression with scalar type.
33554 OpenMP 3.1:
33555 # pragma omp atomic new-line
33556 update-stmt
33558 # pragma omp atomic read new-line
33559 read-stmt
33561 # pragma omp atomic write new-line
33562 write-stmt
33564 # pragma omp atomic update new-line
33565 update-stmt
33567 # pragma omp atomic capture new-line
33568 capture-stmt
33570 # pragma omp atomic capture new-line
33571 capture-block
33573 read-stmt:
33574 v = x
33575 write-stmt:
33576 x = expr
33577 update-stmt:
33578 expression-stmt | x = x binop expr
33579 capture-stmt:
33580 v = expression-stmt
33581 capture-block:
33582 { v = x; update-stmt; } | { update-stmt; v = x; }
33584 OpenMP 4.0:
33585 update-stmt:
33586 expression-stmt | x = x binop expr | x = expr binop x
33587 capture-stmt:
33588 v = update-stmt
33589 capture-block:
33590 { v = x; update-stmt; } | { update-stmt; v = x; } | { v = x; x = expr; }
33592 where x and v are lvalue expressions with scalar type. */
33594 static void
33595 cp_parser_omp_atomic (cp_parser *parser, cp_token *pragma_tok)
33597 tree lhs = NULL_TREE, rhs = NULL_TREE, v = NULL_TREE, lhs1 = NULL_TREE;
33598 tree rhs1 = NULL_TREE, orig_lhs;
33599 enum tree_code code = OMP_ATOMIC, opcode = NOP_EXPR;
33600 bool structured_block = false;
33601 bool seq_cst = false;
33603 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
33605 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
33606 const char *p = IDENTIFIER_POINTER (id);
33608 if (!strcmp (p, "seq_cst"))
33610 seq_cst = true;
33611 cp_lexer_consume_token (parser->lexer);
33612 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)
33613 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME)
33614 cp_lexer_consume_token (parser->lexer);
33617 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
33619 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
33620 const char *p = IDENTIFIER_POINTER (id);
33622 if (!strcmp (p, "read"))
33623 code = OMP_ATOMIC_READ;
33624 else if (!strcmp (p, "write"))
33625 code = NOP_EXPR;
33626 else if (!strcmp (p, "update"))
33627 code = OMP_ATOMIC;
33628 else if (!strcmp (p, "capture"))
33629 code = OMP_ATOMIC_CAPTURE_NEW;
33630 else
33631 p = NULL;
33632 if (p)
33633 cp_lexer_consume_token (parser->lexer);
33635 if (!seq_cst)
33637 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)
33638 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME)
33639 cp_lexer_consume_token (parser->lexer);
33641 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
33643 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
33644 const char *p = IDENTIFIER_POINTER (id);
33646 if (!strcmp (p, "seq_cst"))
33648 seq_cst = true;
33649 cp_lexer_consume_token (parser->lexer);
33653 cp_parser_require_pragma_eol (parser, pragma_tok);
33655 switch (code)
33657 case OMP_ATOMIC_READ:
33658 case NOP_EXPR: /* atomic write */
33659 v = cp_parser_unary_expression (parser);
33660 if (v == error_mark_node)
33661 goto saw_error;
33662 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
33663 goto saw_error;
33664 if (code == NOP_EXPR)
33665 lhs = cp_parser_expression (parser);
33666 else
33667 lhs = cp_parser_unary_expression (parser);
33668 if (lhs == error_mark_node)
33669 goto saw_error;
33670 if (code == NOP_EXPR)
33672 /* atomic write is represented by OMP_ATOMIC with NOP_EXPR
33673 opcode. */
33674 code = OMP_ATOMIC;
33675 rhs = lhs;
33676 lhs = v;
33677 v = NULL_TREE;
33679 goto done;
33680 case OMP_ATOMIC_CAPTURE_NEW:
33681 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
33683 cp_lexer_consume_token (parser->lexer);
33684 structured_block = true;
33686 else
33688 v = cp_parser_unary_expression (parser);
33689 if (v == error_mark_node)
33690 goto saw_error;
33691 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
33692 goto saw_error;
33694 default:
33695 break;
33698 restart:
33699 lhs = cp_parser_unary_expression (parser);
33700 orig_lhs = lhs;
33701 switch (TREE_CODE (lhs))
33703 case ERROR_MARK:
33704 goto saw_error;
33706 case POSTINCREMENT_EXPR:
33707 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
33708 code = OMP_ATOMIC_CAPTURE_OLD;
33709 /* FALLTHROUGH */
33710 case PREINCREMENT_EXPR:
33711 lhs = TREE_OPERAND (lhs, 0);
33712 opcode = PLUS_EXPR;
33713 rhs = integer_one_node;
33714 break;
33716 case POSTDECREMENT_EXPR:
33717 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
33718 code = OMP_ATOMIC_CAPTURE_OLD;
33719 /* FALLTHROUGH */
33720 case PREDECREMENT_EXPR:
33721 lhs = TREE_OPERAND (lhs, 0);
33722 opcode = MINUS_EXPR;
33723 rhs = integer_one_node;
33724 break;
33726 case COMPOUND_EXPR:
33727 if (TREE_CODE (TREE_OPERAND (lhs, 0)) == SAVE_EXPR
33728 && TREE_CODE (TREE_OPERAND (lhs, 1)) == COMPOUND_EXPR
33729 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (lhs, 1), 0)) == MODIFY_EXPR
33730 && TREE_OPERAND (TREE_OPERAND (lhs, 1), 1) == TREE_OPERAND (lhs, 0)
33731 && TREE_CODE (TREE_TYPE (TREE_OPERAND (TREE_OPERAND
33732 (TREE_OPERAND (lhs, 1), 0), 0)))
33733 == BOOLEAN_TYPE)
33734 /* Undo effects of boolean_increment for post {in,de}crement. */
33735 lhs = TREE_OPERAND (TREE_OPERAND (lhs, 1), 0);
33736 /* FALLTHRU */
33737 case MODIFY_EXPR:
33738 if (TREE_CODE (lhs) == MODIFY_EXPR
33739 && TREE_CODE (TREE_TYPE (TREE_OPERAND (lhs, 0))) == BOOLEAN_TYPE)
33741 /* Undo effects of boolean_increment. */
33742 if (integer_onep (TREE_OPERAND (lhs, 1)))
33744 /* This is pre or post increment. */
33745 rhs = TREE_OPERAND (lhs, 1);
33746 lhs = TREE_OPERAND (lhs, 0);
33747 opcode = NOP_EXPR;
33748 if (code == OMP_ATOMIC_CAPTURE_NEW
33749 && !structured_block
33750 && TREE_CODE (orig_lhs) == COMPOUND_EXPR)
33751 code = OMP_ATOMIC_CAPTURE_OLD;
33752 break;
33755 /* FALLTHRU */
33756 default:
33757 switch (cp_lexer_peek_token (parser->lexer)->type)
33759 case CPP_MULT_EQ:
33760 opcode = MULT_EXPR;
33761 break;
33762 case CPP_DIV_EQ:
33763 opcode = TRUNC_DIV_EXPR;
33764 break;
33765 case CPP_PLUS_EQ:
33766 opcode = PLUS_EXPR;
33767 break;
33768 case CPP_MINUS_EQ:
33769 opcode = MINUS_EXPR;
33770 break;
33771 case CPP_LSHIFT_EQ:
33772 opcode = LSHIFT_EXPR;
33773 break;
33774 case CPP_RSHIFT_EQ:
33775 opcode = RSHIFT_EXPR;
33776 break;
33777 case CPP_AND_EQ:
33778 opcode = BIT_AND_EXPR;
33779 break;
33780 case CPP_OR_EQ:
33781 opcode = BIT_IOR_EXPR;
33782 break;
33783 case CPP_XOR_EQ:
33784 opcode = BIT_XOR_EXPR;
33785 break;
33786 case CPP_EQ:
33787 enum cp_parser_prec oprec;
33788 cp_token *token;
33789 cp_lexer_consume_token (parser->lexer);
33790 cp_parser_parse_tentatively (parser);
33791 rhs1 = cp_parser_simple_cast_expression (parser);
33792 if (rhs1 == error_mark_node)
33794 cp_parser_abort_tentative_parse (parser);
33795 cp_parser_simple_cast_expression (parser);
33796 goto saw_error;
33798 token = cp_lexer_peek_token (parser->lexer);
33799 if (token->type != CPP_SEMICOLON && !cp_tree_equal (lhs, rhs1))
33801 cp_parser_abort_tentative_parse (parser);
33802 cp_parser_parse_tentatively (parser);
33803 rhs = cp_parser_binary_expression (parser, false, true,
33804 PREC_NOT_OPERATOR, NULL);
33805 if (rhs == error_mark_node)
33807 cp_parser_abort_tentative_parse (parser);
33808 cp_parser_binary_expression (parser, false, true,
33809 PREC_NOT_OPERATOR, NULL);
33810 goto saw_error;
33812 switch (TREE_CODE (rhs))
33814 case MULT_EXPR:
33815 case TRUNC_DIV_EXPR:
33816 case RDIV_EXPR:
33817 case PLUS_EXPR:
33818 case MINUS_EXPR:
33819 case LSHIFT_EXPR:
33820 case RSHIFT_EXPR:
33821 case BIT_AND_EXPR:
33822 case BIT_IOR_EXPR:
33823 case BIT_XOR_EXPR:
33824 if (cp_tree_equal (lhs, TREE_OPERAND (rhs, 1)))
33826 if (cp_parser_parse_definitely (parser))
33828 opcode = TREE_CODE (rhs);
33829 rhs1 = TREE_OPERAND (rhs, 0);
33830 rhs = TREE_OPERAND (rhs, 1);
33831 goto stmt_done;
33833 else
33834 goto saw_error;
33836 break;
33837 default:
33838 break;
33840 cp_parser_abort_tentative_parse (parser);
33841 if (structured_block && code == OMP_ATOMIC_CAPTURE_OLD)
33843 rhs = cp_parser_expression (parser);
33844 if (rhs == error_mark_node)
33845 goto saw_error;
33846 opcode = NOP_EXPR;
33847 rhs1 = NULL_TREE;
33848 goto stmt_done;
33850 cp_parser_error (parser,
33851 "invalid form of %<#pragma omp atomic%>");
33852 goto saw_error;
33854 if (!cp_parser_parse_definitely (parser))
33855 goto saw_error;
33856 switch (token->type)
33858 case CPP_SEMICOLON:
33859 if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
33861 code = OMP_ATOMIC_CAPTURE_OLD;
33862 v = lhs;
33863 lhs = NULL_TREE;
33864 lhs1 = rhs1;
33865 rhs1 = NULL_TREE;
33866 cp_lexer_consume_token (parser->lexer);
33867 goto restart;
33869 else if (structured_block)
33871 opcode = NOP_EXPR;
33872 rhs = rhs1;
33873 rhs1 = NULL_TREE;
33874 goto stmt_done;
33876 cp_parser_error (parser,
33877 "invalid form of %<#pragma omp atomic%>");
33878 goto saw_error;
33879 case CPP_MULT:
33880 opcode = MULT_EXPR;
33881 break;
33882 case CPP_DIV:
33883 opcode = TRUNC_DIV_EXPR;
33884 break;
33885 case CPP_PLUS:
33886 opcode = PLUS_EXPR;
33887 break;
33888 case CPP_MINUS:
33889 opcode = MINUS_EXPR;
33890 break;
33891 case CPP_LSHIFT:
33892 opcode = LSHIFT_EXPR;
33893 break;
33894 case CPP_RSHIFT:
33895 opcode = RSHIFT_EXPR;
33896 break;
33897 case CPP_AND:
33898 opcode = BIT_AND_EXPR;
33899 break;
33900 case CPP_OR:
33901 opcode = BIT_IOR_EXPR;
33902 break;
33903 case CPP_XOR:
33904 opcode = BIT_XOR_EXPR;
33905 break;
33906 default:
33907 cp_parser_error (parser,
33908 "invalid operator for %<#pragma omp atomic%>");
33909 goto saw_error;
33911 oprec = TOKEN_PRECEDENCE (token);
33912 gcc_assert (oprec != PREC_NOT_OPERATOR);
33913 if (commutative_tree_code (opcode))
33914 oprec = (enum cp_parser_prec) (oprec - 1);
33915 cp_lexer_consume_token (parser->lexer);
33916 rhs = cp_parser_binary_expression (parser, false, false,
33917 oprec, NULL);
33918 if (rhs == error_mark_node)
33919 goto saw_error;
33920 goto stmt_done;
33921 /* FALLTHROUGH */
33922 default:
33923 cp_parser_error (parser,
33924 "invalid operator for %<#pragma omp atomic%>");
33925 goto saw_error;
33927 cp_lexer_consume_token (parser->lexer);
33929 rhs = cp_parser_expression (parser);
33930 if (rhs == error_mark_node)
33931 goto saw_error;
33932 break;
33934 stmt_done:
33935 if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
33937 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
33938 goto saw_error;
33939 v = cp_parser_unary_expression (parser);
33940 if (v == error_mark_node)
33941 goto saw_error;
33942 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
33943 goto saw_error;
33944 lhs1 = cp_parser_unary_expression (parser);
33945 if (lhs1 == error_mark_node)
33946 goto saw_error;
33948 if (structured_block)
33950 cp_parser_consume_semicolon_at_end_of_statement (parser);
33951 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
33953 done:
33954 finish_omp_atomic (code, opcode, lhs, rhs, v, lhs1, rhs1, seq_cst);
33955 if (!structured_block)
33956 cp_parser_consume_semicolon_at_end_of_statement (parser);
33957 return;
33959 saw_error:
33960 cp_parser_skip_to_end_of_block_or_statement (parser);
33961 if (structured_block)
33963 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
33964 cp_lexer_consume_token (parser->lexer);
33965 else if (code == OMP_ATOMIC_CAPTURE_NEW)
33967 cp_parser_skip_to_end_of_block_or_statement (parser);
33968 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
33969 cp_lexer_consume_token (parser->lexer);
33975 /* OpenMP 2.5:
33976 # pragma omp barrier new-line */
33978 static void
33979 cp_parser_omp_barrier (cp_parser *parser, cp_token *pragma_tok)
33981 cp_parser_require_pragma_eol (parser, pragma_tok);
33982 finish_omp_barrier ();
33985 /* OpenMP 2.5:
33986 # pragma omp critical [(name)] new-line
33987 structured-block
33989 OpenMP 4.5:
33990 # pragma omp critical [(name) [hint(expression)]] new-line
33991 structured-block */
33993 #define OMP_CRITICAL_CLAUSE_MASK \
33994 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_HINT) )
33996 static tree
33997 cp_parser_omp_critical (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
33999 tree stmt, name = NULL_TREE, clauses = NULL_TREE;
34001 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
34003 cp_lexer_consume_token (parser->lexer);
34005 name = cp_parser_identifier (parser);
34007 if (name == error_mark_node
34008 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
34009 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
34010 /*or_comma=*/false,
34011 /*consume_paren=*/true);
34012 if (name == error_mark_node)
34013 name = NULL;
34015 clauses = cp_parser_omp_all_clauses (parser,
34016 OMP_CRITICAL_CLAUSE_MASK,
34017 "#pragma omp critical", pragma_tok);
34019 else
34020 cp_parser_require_pragma_eol (parser, pragma_tok);
34022 stmt = cp_parser_omp_structured_block (parser, if_p);
34023 return c_finish_omp_critical (input_location, stmt, name, clauses);
34026 /* OpenMP 2.5:
34027 # pragma omp flush flush-vars[opt] new-line
34029 flush-vars:
34030 ( variable-list ) */
34032 static void
34033 cp_parser_omp_flush (cp_parser *parser, cp_token *pragma_tok)
34035 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
34036 (void) cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
34037 cp_parser_require_pragma_eol (parser, pragma_tok);
34039 finish_omp_flush ();
34042 /* Helper function, to parse omp for increment expression. */
34044 static tree
34045 cp_parser_omp_for_cond (cp_parser *parser, tree decl, enum tree_code code)
34047 tree cond = cp_parser_binary_expression (parser, false, true,
34048 PREC_NOT_OPERATOR, NULL);
34049 if (cond == error_mark_node
34050 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
34052 cp_parser_skip_to_end_of_statement (parser);
34053 return error_mark_node;
34056 switch (TREE_CODE (cond))
34058 case GT_EXPR:
34059 case GE_EXPR:
34060 case LT_EXPR:
34061 case LE_EXPR:
34062 break;
34063 case NE_EXPR:
34064 if (code == CILK_SIMD || code == CILK_FOR)
34065 break;
34066 /* Fall through: OpenMP disallows NE_EXPR. */
34067 gcc_fallthrough ();
34068 default:
34069 return error_mark_node;
34072 /* If decl is an iterator, preserve LHS and RHS of the relational
34073 expr until finish_omp_for. */
34074 if (decl
34075 && (type_dependent_expression_p (decl)
34076 || CLASS_TYPE_P (TREE_TYPE (decl))))
34077 return cond;
34079 return build_x_binary_op (EXPR_LOC_OR_LOC (cond, input_location),
34080 TREE_CODE (cond),
34081 TREE_OPERAND (cond, 0), ERROR_MARK,
34082 TREE_OPERAND (cond, 1), ERROR_MARK,
34083 /*overload=*/NULL, tf_warning_or_error);
34086 /* Helper function, to parse omp for increment expression. */
34088 static tree
34089 cp_parser_omp_for_incr (cp_parser *parser, tree decl)
34091 cp_token *token = cp_lexer_peek_token (parser->lexer);
34092 enum tree_code op;
34093 tree lhs, rhs;
34094 cp_id_kind idk;
34095 bool decl_first;
34097 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
34099 op = (token->type == CPP_PLUS_PLUS
34100 ? PREINCREMENT_EXPR : PREDECREMENT_EXPR);
34101 cp_lexer_consume_token (parser->lexer);
34102 lhs = cp_parser_simple_cast_expression (parser);
34103 if (lhs != decl
34104 && (!processing_template_decl || !cp_tree_equal (lhs, decl)))
34105 return error_mark_node;
34106 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
34109 lhs = cp_parser_primary_expression (parser, false, false, false, &idk);
34110 if (lhs != decl
34111 && (!processing_template_decl || !cp_tree_equal (lhs, decl)))
34112 return error_mark_node;
34114 token = cp_lexer_peek_token (parser->lexer);
34115 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
34117 op = (token->type == CPP_PLUS_PLUS
34118 ? POSTINCREMENT_EXPR : POSTDECREMENT_EXPR);
34119 cp_lexer_consume_token (parser->lexer);
34120 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
34123 op = cp_parser_assignment_operator_opt (parser);
34124 if (op == ERROR_MARK)
34125 return error_mark_node;
34127 if (op != NOP_EXPR)
34129 rhs = cp_parser_assignment_expression (parser);
34130 rhs = build2 (op, TREE_TYPE (decl), decl, rhs);
34131 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
34134 lhs = cp_parser_binary_expression (parser, false, false,
34135 PREC_ADDITIVE_EXPRESSION, NULL);
34136 token = cp_lexer_peek_token (parser->lexer);
34137 decl_first = (lhs == decl
34138 || (processing_template_decl && cp_tree_equal (lhs, decl)));
34139 if (decl_first)
34140 lhs = NULL_TREE;
34141 if (token->type != CPP_PLUS
34142 && token->type != CPP_MINUS)
34143 return error_mark_node;
34147 op = token->type == CPP_PLUS ? PLUS_EXPR : MINUS_EXPR;
34148 cp_lexer_consume_token (parser->lexer);
34149 rhs = cp_parser_binary_expression (parser, false, false,
34150 PREC_ADDITIVE_EXPRESSION, NULL);
34151 token = cp_lexer_peek_token (parser->lexer);
34152 if (token->type == CPP_PLUS || token->type == CPP_MINUS || decl_first)
34154 if (lhs == NULL_TREE)
34156 if (op == PLUS_EXPR)
34157 lhs = rhs;
34158 else
34159 lhs = build_x_unary_op (input_location, NEGATE_EXPR, rhs,
34160 tf_warning_or_error);
34162 else
34163 lhs = build_x_binary_op (input_location, op, lhs, ERROR_MARK, rhs,
34164 ERROR_MARK, NULL, tf_warning_or_error);
34167 while (token->type == CPP_PLUS || token->type == CPP_MINUS);
34169 if (!decl_first)
34171 if ((rhs != decl
34172 && (!processing_template_decl || !cp_tree_equal (rhs, decl)))
34173 || op == MINUS_EXPR)
34174 return error_mark_node;
34175 rhs = build2 (op, TREE_TYPE (decl), lhs, decl);
34177 else
34178 rhs = build2 (PLUS_EXPR, TREE_TYPE (decl), decl, lhs);
34180 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
34183 /* Parse the initialization statement of either an OpenMP for loop or
34184 a Cilk Plus for loop.
34186 Return true if the resulting construct should have an
34187 OMP_CLAUSE_PRIVATE added to it. */
34189 static tree
34190 cp_parser_omp_for_loop_init (cp_parser *parser,
34191 enum tree_code code,
34192 tree &this_pre_body,
34193 vec<tree, va_gc> *for_block,
34194 tree &init,
34195 tree &orig_init,
34196 tree &decl,
34197 tree &real_decl)
34199 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
34200 return NULL_TREE;
34202 tree add_private_clause = NULL_TREE;
34204 /* See 2.5.1 (in OpenMP 3.0, similar wording is in 2.5 standard too):
34206 init-expr:
34207 var = lb
34208 integer-type var = lb
34209 random-access-iterator-type var = lb
34210 pointer-type var = lb
34212 cp_decl_specifier_seq type_specifiers;
34214 /* First, try to parse as an initialized declaration. See
34215 cp_parser_condition, from whence the bulk of this is copied. */
34217 cp_parser_parse_tentatively (parser);
34218 cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
34219 /*is_trailing_return=*/false,
34220 &type_specifiers);
34221 if (cp_parser_parse_definitely (parser))
34223 /* If parsing a type specifier seq succeeded, then this
34224 MUST be a initialized declaration. */
34225 tree asm_specification, attributes;
34226 cp_declarator *declarator;
34228 declarator = cp_parser_declarator (parser,
34229 CP_PARSER_DECLARATOR_NAMED,
34230 /*ctor_dtor_or_conv_p=*/NULL,
34231 /*parenthesized_p=*/NULL,
34232 /*member_p=*/false,
34233 /*friend_p=*/false);
34234 attributes = cp_parser_attributes_opt (parser);
34235 asm_specification = cp_parser_asm_specification_opt (parser);
34237 if (declarator == cp_error_declarator)
34238 cp_parser_skip_to_end_of_statement (parser);
34240 else
34242 tree pushed_scope, auto_node;
34244 decl = start_decl (declarator, &type_specifiers,
34245 SD_INITIALIZED, attributes,
34246 /*prefix_attributes=*/NULL_TREE,
34247 &pushed_scope);
34249 auto_node = type_uses_auto (TREE_TYPE (decl));
34250 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
34252 if (cp_lexer_next_token_is (parser->lexer,
34253 CPP_OPEN_PAREN))
34255 if (code != CILK_SIMD && code != CILK_FOR)
34256 error ("parenthesized initialization is not allowed in "
34257 "OpenMP %<for%> loop");
34258 else
34259 error ("parenthesized initialization is "
34260 "not allowed in for-loop");
34262 else
34263 /* Trigger an error. */
34264 cp_parser_require (parser, CPP_EQ, RT_EQ);
34266 init = error_mark_node;
34267 cp_parser_skip_to_end_of_statement (parser);
34269 else if (CLASS_TYPE_P (TREE_TYPE (decl))
34270 || type_dependent_expression_p (decl)
34271 || auto_node)
34273 bool is_direct_init, is_non_constant_init;
34275 init = cp_parser_initializer (parser,
34276 &is_direct_init,
34277 &is_non_constant_init);
34279 if (auto_node)
34281 TREE_TYPE (decl)
34282 = do_auto_deduction (TREE_TYPE (decl), init,
34283 auto_node);
34285 if (!CLASS_TYPE_P (TREE_TYPE (decl))
34286 && !type_dependent_expression_p (decl))
34287 goto non_class;
34290 cp_finish_decl (decl, init, !is_non_constant_init,
34291 asm_specification,
34292 LOOKUP_ONLYCONVERTING);
34293 orig_init = init;
34294 if (CLASS_TYPE_P (TREE_TYPE (decl)))
34296 vec_safe_push (for_block, this_pre_body);
34297 init = NULL_TREE;
34299 else
34301 init = pop_stmt_list (this_pre_body);
34302 if (init && TREE_CODE (init) == STATEMENT_LIST)
34304 tree_stmt_iterator i = tsi_start (init);
34305 /* Move lambda DECL_EXPRs to FOR_BLOCK. */
34306 while (!tsi_end_p (i))
34308 tree t = tsi_stmt (i);
34309 if (TREE_CODE (t) == DECL_EXPR
34310 && TREE_CODE (DECL_EXPR_DECL (t)) == TYPE_DECL)
34312 tsi_delink (&i);
34313 vec_safe_push (for_block, t);
34314 continue;
34316 break;
34318 if (tsi_one_before_end_p (i))
34320 tree t = tsi_stmt (i);
34321 tsi_delink (&i);
34322 free_stmt_list (init);
34323 init = t;
34327 this_pre_body = NULL_TREE;
34329 else
34331 /* Consume '='. */
34332 cp_lexer_consume_token (parser->lexer);
34333 init = cp_parser_assignment_expression (parser);
34335 non_class:
34336 if (TREE_CODE (TREE_TYPE (decl)) == REFERENCE_TYPE)
34337 init = error_mark_node;
34338 else
34339 cp_finish_decl (decl, NULL_TREE,
34340 /*init_const_expr_p=*/false,
34341 asm_specification,
34342 LOOKUP_ONLYCONVERTING);
34345 if (pushed_scope)
34346 pop_scope (pushed_scope);
34349 else
34351 cp_id_kind idk;
34352 /* If parsing a type specifier sequence failed, then
34353 this MUST be a simple expression. */
34354 if (code == CILK_FOR)
34355 error ("%<_Cilk_for%> allows expression instead of declaration only "
34356 "in C, not in C++");
34357 cp_parser_parse_tentatively (parser);
34358 decl = cp_parser_primary_expression (parser, false, false,
34359 false, &idk);
34360 cp_token *last_tok = cp_lexer_peek_token (parser->lexer);
34361 if (!cp_parser_error_occurred (parser)
34362 && decl
34363 && (TREE_CODE (decl) == COMPONENT_REF
34364 || (TREE_CODE (decl) == SCOPE_REF && TREE_TYPE (decl))))
34366 cp_parser_abort_tentative_parse (parser);
34367 cp_parser_parse_tentatively (parser);
34368 cp_token *token = cp_lexer_peek_token (parser->lexer);
34369 tree name = cp_parser_id_expression (parser, /*template_p=*/false,
34370 /*check_dependency_p=*/true,
34371 /*template_p=*/NULL,
34372 /*declarator_p=*/false,
34373 /*optional_p=*/false);
34374 if (name != error_mark_node
34375 && last_tok == cp_lexer_peek_token (parser->lexer))
34377 decl = cp_parser_lookup_name_simple (parser, name,
34378 token->location);
34379 if (TREE_CODE (decl) == FIELD_DECL)
34380 add_private_clause = omp_privatize_field (decl, false);
34382 cp_parser_abort_tentative_parse (parser);
34383 cp_parser_parse_tentatively (parser);
34384 decl = cp_parser_primary_expression (parser, false, false,
34385 false, &idk);
34387 if (!cp_parser_error_occurred (parser)
34388 && decl
34389 && DECL_P (decl)
34390 && CLASS_TYPE_P (TREE_TYPE (decl)))
34392 tree rhs;
34394 cp_parser_parse_definitely (parser);
34395 cp_parser_require (parser, CPP_EQ, RT_EQ);
34396 rhs = cp_parser_assignment_expression (parser);
34397 orig_init = rhs;
34398 finish_expr_stmt (build_x_modify_expr (EXPR_LOCATION (rhs),
34399 decl, NOP_EXPR,
34400 rhs,
34401 tf_warning_or_error));
34402 if (!add_private_clause)
34403 add_private_clause = decl;
34405 else
34407 decl = NULL;
34408 cp_parser_abort_tentative_parse (parser);
34409 init = cp_parser_expression (parser);
34410 if (init)
34412 if (TREE_CODE (init) == MODIFY_EXPR
34413 || TREE_CODE (init) == MODOP_EXPR)
34414 real_decl = TREE_OPERAND (init, 0);
34418 return add_private_clause;
34421 /* Parse the restricted form of the for statement allowed by OpenMP. */
34423 static tree
34424 cp_parser_omp_for_loop (cp_parser *parser, enum tree_code code, tree clauses,
34425 tree *cclauses, bool *if_p)
34427 tree init, orig_init, cond, incr, body, decl, pre_body = NULL_TREE, ret;
34428 tree real_decl, initv, condv, incrv, declv;
34429 tree this_pre_body, cl, ordered_cl = NULL_TREE;
34430 location_t loc_first;
34431 bool collapse_err = false;
34432 int i, collapse = 1, ordered = 0, count, nbraces = 0;
34433 vec<tree, va_gc> *for_block = make_tree_vector ();
34434 auto_vec<tree, 4> orig_inits;
34435 bool tiling = false;
34437 for (cl = clauses; cl; cl = OMP_CLAUSE_CHAIN (cl))
34438 if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_COLLAPSE)
34439 collapse = tree_to_shwi (OMP_CLAUSE_COLLAPSE_EXPR (cl));
34440 else if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_TILE)
34442 tiling = true;
34443 collapse = list_length (OMP_CLAUSE_TILE_LIST (cl));
34445 else if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_ORDERED
34446 && OMP_CLAUSE_ORDERED_EXPR (cl))
34448 ordered_cl = cl;
34449 ordered = tree_to_shwi (OMP_CLAUSE_ORDERED_EXPR (cl));
34452 if (ordered && ordered < collapse)
34454 error_at (OMP_CLAUSE_LOCATION (ordered_cl),
34455 "%<ordered%> clause parameter is less than %<collapse%>");
34456 OMP_CLAUSE_ORDERED_EXPR (ordered_cl)
34457 = build_int_cst (NULL_TREE, collapse);
34458 ordered = collapse;
34460 if (ordered)
34462 for (tree *pc = &clauses; *pc; )
34463 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_LINEAR)
34465 error_at (OMP_CLAUSE_LOCATION (*pc),
34466 "%<linear%> clause may not be specified together "
34467 "with %<ordered%> clause with a parameter");
34468 *pc = OMP_CLAUSE_CHAIN (*pc);
34470 else
34471 pc = &OMP_CLAUSE_CHAIN (*pc);
34474 gcc_assert (tiling || (collapse >= 1 && ordered >= 0));
34475 count = ordered ? ordered : collapse;
34477 declv = make_tree_vec (count);
34478 initv = make_tree_vec (count);
34479 condv = make_tree_vec (count);
34480 incrv = make_tree_vec (count);
34482 loc_first = cp_lexer_peek_token (parser->lexer)->location;
34484 for (i = 0; i < count; i++)
34486 int bracecount = 0;
34487 tree add_private_clause = NULL_TREE;
34488 location_t loc;
34490 if (code != CILK_FOR
34491 && !cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
34493 if (!collapse_err)
34494 cp_parser_error (parser, "for statement expected");
34495 return NULL;
34497 if (code == CILK_FOR
34498 && !cp_lexer_next_token_is_keyword (parser->lexer, RID_CILK_FOR))
34500 if (!collapse_err)
34501 cp_parser_error (parser, "_Cilk_for statement expected");
34502 return NULL;
34504 loc = cp_lexer_consume_token (parser->lexer)->location;
34506 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
34507 return NULL;
34509 init = orig_init = decl = real_decl = NULL;
34510 this_pre_body = push_stmt_list ();
34512 add_private_clause
34513 = cp_parser_omp_for_loop_init (parser, code,
34514 this_pre_body, for_block,
34515 init, orig_init, decl, real_decl);
34517 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
34518 if (this_pre_body)
34520 this_pre_body = pop_stmt_list (this_pre_body);
34521 if (pre_body)
34523 tree t = pre_body;
34524 pre_body = push_stmt_list ();
34525 add_stmt (t);
34526 add_stmt (this_pre_body);
34527 pre_body = pop_stmt_list (pre_body);
34529 else
34530 pre_body = this_pre_body;
34533 if (decl)
34534 real_decl = decl;
34535 if (cclauses != NULL
34536 && cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL] != NULL
34537 && real_decl != NULL_TREE)
34539 tree *c;
34540 for (c = &cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL]; *c ; )
34541 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_FIRSTPRIVATE
34542 && OMP_CLAUSE_DECL (*c) == real_decl)
34544 error_at (loc, "iteration variable %qD"
34545 " should not be firstprivate", real_decl);
34546 *c = OMP_CLAUSE_CHAIN (*c);
34548 else if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_LASTPRIVATE
34549 && OMP_CLAUSE_DECL (*c) == real_decl)
34551 /* Move lastprivate (decl) clause to OMP_FOR_CLAUSES. */
34552 tree l = *c;
34553 *c = OMP_CLAUSE_CHAIN (*c);
34554 if (code == OMP_SIMD)
34556 OMP_CLAUSE_CHAIN (l) = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
34557 cclauses[C_OMP_CLAUSE_SPLIT_FOR] = l;
34559 else
34561 OMP_CLAUSE_CHAIN (l) = clauses;
34562 clauses = l;
34564 add_private_clause = NULL_TREE;
34566 else
34568 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_PRIVATE
34569 && OMP_CLAUSE_DECL (*c) == real_decl)
34570 add_private_clause = NULL_TREE;
34571 c = &OMP_CLAUSE_CHAIN (*c);
34575 if (add_private_clause)
34577 tree c;
34578 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
34580 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
34581 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
34582 && OMP_CLAUSE_DECL (c) == decl)
34583 break;
34584 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
34585 && OMP_CLAUSE_DECL (c) == decl)
34586 error_at (loc, "iteration variable %qD "
34587 "should not be firstprivate",
34588 decl);
34589 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
34590 && OMP_CLAUSE_DECL (c) == decl)
34591 error_at (loc, "iteration variable %qD should not be reduction",
34592 decl);
34594 if (c == NULL)
34596 if (code != OMP_SIMD)
34597 c = build_omp_clause (loc, OMP_CLAUSE_PRIVATE);
34598 else if (collapse == 1)
34599 c = build_omp_clause (loc, OMP_CLAUSE_LINEAR);
34600 else
34601 c = build_omp_clause (loc, OMP_CLAUSE_LASTPRIVATE);
34602 OMP_CLAUSE_DECL (c) = add_private_clause;
34603 c = finish_omp_clauses (c, C_ORT_OMP);
34604 if (c)
34606 OMP_CLAUSE_CHAIN (c) = clauses;
34607 clauses = c;
34608 /* For linear, signal that we need to fill up
34609 the so far unknown linear step. */
34610 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINEAR)
34611 OMP_CLAUSE_LINEAR_STEP (c) = NULL_TREE;
34616 cond = NULL;
34617 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
34618 cond = cp_parser_omp_for_cond (parser, decl, code);
34619 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
34621 incr = NULL;
34622 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
34624 /* If decl is an iterator, preserve the operator on decl
34625 until finish_omp_for. */
34626 if (real_decl
34627 && ((processing_template_decl
34628 && (TREE_TYPE (real_decl) == NULL_TREE
34629 || !POINTER_TYPE_P (TREE_TYPE (real_decl))))
34630 || CLASS_TYPE_P (TREE_TYPE (real_decl))))
34631 incr = cp_parser_omp_for_incr (parser, real_decl);
34632 else
34633 incr = cp_parser_expression (parser);
34634 if (!EXPR_HAS_LOCATION (incr))
34635 protected_set_expr_location (incr, input_location);
34638 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
34639 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
34640 /*or_comma=*/false,
34641 /*consume_paren=*/true);
34643 TREE_VEC_ELT (declv, i) = decl;
34644 TREE_VEC_ELT (initv, i) = init;
34645 TREE_VEC_ELT (condv, i) = cond;
34646 TREE_VEC_ELT (incrv, i) = incr;
34647 if (orig_init)
34649 orig_inits.safe_grow_cleared (i + 1);
34650 orig_inits[i] = orig_init;
34653 if (i == count - 1)
34654 break;
34656 /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
34657 in between the collapsed for loops to be still considered perfectly
34658 nested. Hopefully the final version clarifies this.
34659 For now handle (multiple) {'s and empty statements. */
34660 cp_parser_parse_tentatively (parser);
34661 for (;;)
34663 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
34664 break;
34665 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
34667 cp_lexer_consume_token (parser->lexer);
34668 bracecount++;
34670 else if (bracecount
34671 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
34672 cp_lexer_consume_token (parser->lexer);
34673 else
34675 loc = cp_lexer_peek_token (parser->lexer)->location;
34676 error_at (loc, "not enough for loops to collapse");
34677 collapse_err = true;
34678 cp_parser_abort_tentative_parse (parser);
34679 declv = NULL_TREE;
34680 break;
34684 if (declv)
34686 cp_parser_parse_definitely (parser);
34687 nbraces += bracecount;
34691 if (nbraces)
34692 if_p = NULL;
34694 /* Note that we saved the original contents of this flag when we entered
34695 the structured block, and so we don't need to re-save it here. */
34696 if (code == CILK_SIMD || code == CILK_FOR)
34697 parser->in_statement = IN_CILK_SIMD_FOR;
34698 else
34699 parser->in_statement = IN_OMP_FOR;
34701 /* Note that the grammar doesn't call for a structured block here,
34702 though the loop as a whole is a structured block. */
34703 body = push_stmt_list ();
34704 cp_parser_statement (parser, NULL_TREE, false, if_p);
34705 body = pop_stmt_list (body);
34707 if (declv == NULL_TREE)
34708 ret = NULL_TREE;
34709 else
34710 ret = finish_omp_for (loc_first, code, declv, NULL, initv, condv, incrv,
34711 body, pre_body, &orig_inits, clauses);
34713 while (nbraces)
34715 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
34717 cp_lexer_consume_token (parser->lexer);
34718 nbraces--;
34720 else if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
34721 cp_lexer_consume_token (parser->lexer);
34722 else
34724 if (!collapse_err)
34726 error_at (cp_lexer_peek_token (parser->lexer)->location,
34727 "collapsed loops not perfectly nested");
34729 collapse_err = true;
34730 cp_parser_statement_seq_opt (parser, NULL);
34731 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
34732 break;
34736 while (!for_block->is_empty ())
34738 tree t = for_block->pop ();
34739 if (TREE_CODE (t) == STATEMENT_LIST)
34740 add_stmt (pop_stmt_list (t));
34741 else
34742 add_stmt (t);
34744 release_tree_vector (for_block);
34746 return ret;
34749 /* Helper function for OpenMP parsing, split clauses and call
34750 finish_omp_clauses on each of the set of clauses afterwards. */
34752 static void
34753 cp_omp_split_clauses (location_t loc, enum tree_code code,
34754 omp_clause_mask mask, tree clauses, tree *cclauses)
34756 int i;
34757 c_omp_split_clauses (loc, code, mask, clauses, cclauses);
34758 for (i = 0; i < C_OMP_CLAUSE_SPLIT_COUNT; i++)
34759 if (cclauses[i])
34760 cclauses[i] = finish_omp_clauses (cclauses[i], C_ORT_OMP);
34763 /* OpenMP 4.0:
34764 #pragma omp simd simd-clause[optseq] new-line
34765 for-loop */
34767 #define OMP_SIMD_CLAUSE_MASK \
34768 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SAFELEN) \
34769 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN) \
34770 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
34771 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
34772 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
34773 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
34774 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
34775 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
34777 static tree
34778 cp_parser_omp_simd (cp_parser *parser, cp_token *pragma_tok,
34779 char *p_name, omp_clause_mask mask, tree *cclauses,
34780 bool *if_p)
34782 tree clauses, sb, ret;
34783 unsigned int save;
34784 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
34786 strcat (p_name, " simd");
34787 mask |= OMP_SIMD_CLAUSE_MASK;
34789 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
34790 cclauses == NULL);
34791 if (cclauses)
34793 cp_omp_split_clauses (loc, OMP_SIMD, mask, clauses, cclauses);
34794 clauses = cclauses[C_OMP_CLAUSE_SPLIT_SIMD];
34795 tree c = omp_find_clause (cclauses[C_OMP_CLAUSE_SPLIT_FOR],
34796 OMP_CLAUSE_ORDERED);
34797 if (c && OMP_CLAUSE_ORDERED_EXPR (c))
34799 error_at (OMP_CLAUSE_LOCATION (c),
34800 "%<ordered%> clause with parameter may not be specified "
34801 "on %qs construct", p_name);
34802 OMP_CLAUSE_ORDERED_EXPR (c) = NULL_TREE;
34806 sb = begin_omp_structured_block ();
34807 save = cp_parser_begin_omp_structured_block (parser);
34809 ret = cp_parser_omp_for_loop (parser, OMP_SIMD, clauses, cclauses, if_p);
34811 cp_parser_end_omp_structured_block (parser, save);
34812 add_stmt (finish_omp_structured_block (sb));
34814 return ret;
34817 /* OpenMP 2.5:
34818 #pragma omp for for-clause[optseq] new-line
34819 for-loop
34821 OpenMP 4.0:
34822 #pragma omp for simd for-simd-clause[optseq] new-line
34823 for-loop */
34825 #define OMP_FOR_CLAUSE_MASK \
34826 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
34827 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
34828 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
34829 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
34830 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
34831 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED) \
34832 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SCHEDULE) \
34833 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT) \
34834 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
34836 static tree
34837 cp_parser_omp_for (cp_parser *parser, cp_token *pragma_tok,
34838 char *p_name, omp_clause_mask mask, tree *cclauses,
34839 bool *if_p)
34841 tree clauses, sb, ret;
34842 unsigned int save;
34843 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
34845 strcat (p_name, " for");
34846 mask |= OMP_FOR_CLAUSE_MASK;
34847 /* parallel for{, simd} disallows nowait clause, but for
34848 target {teams distribute ,}parallel for{, simd} it should be accepted. */
34849 if (cclauses && (mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP)) == 0)
34850 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT);
34851 /* Composite distribute parallel for{, simd} disallows ordered clause. */
34852 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
34853 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED);
34855 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
34857 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
34858 const char *p = IDENTIFIER_POINTER (id);
34860 if (strcmp (p, "simd") == 0)
34862 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
34863 if (cclauses == NULL)
34864 cclauses = cclauses_buf;
34866 cp_lexer_consume_token (parser->lexer);
34867 if (!flag_openmp) /* flag_openmp_simd */
34868 return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
34869 cclauses, if_p);
34870 sb = begin_omp_structured_block ();
34871 save = cp_parser_begin_omp_structured_block (parser);
34872 ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
34873 cclauses, if_p);
34874 cp_parser_end_omp_structured_block (parser, save);
34875 tree body = finish_omp_structured_block (sb);
34876 if (ret == NULL)
34877 return ret;
34878 ret = make_node (OMP_FOR);
34879 TREE_TYPE (ret) = void_type_node;
34880 OMP_FOR_BODY (ret) = body;
34881 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
34882 SET_EXPR_LOCATION (ret, loc);
34883 add_stmt (ret);
34884 return ret;
34887 if (!flag_openmp) /* flag_openmp_simd */
34889 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
34890 return NULL_TREE;
34893 /* Composite distribute parallel for disallows linear clause. */
34894 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
34895 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR);
34897 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
34898 cclauses == NULL);
34899 if (cclauses)
34901 cp_omp_split_clauses (loc, OMP_FOR, mask, clauses, cclauses);
34902 clauses = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
34905 sb = begin_omp_structured_block ();
34906 save = cp_parser_begin_omp_structured_block (parser);
34908 ret = cp_parser_omp_for_loop (parser, OMP_FOR, clauses, cclauses, if_p);
34910 cp_parser_end_omp_structured_block (parser, save);
34911 add_stmt (finish_omp_structured_block (sb));
34913 return ret;
34916 /* OpenMP 2.5:
34917 # pragma omp master new-line
34918 structured-block */
34920 static tree
34921 cp_parser_omp_master (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
34923 cp_parser_require_pragma_eol (parser, pragma_tok);
34924 return c_finish_omp_master (input_location,
34925 cp_parser_omp_structured_block (parser, if_p));
34928 /* OpenMP 2.5:
34929 # pragma omp ordered new-line
34930 structured-block
34932 OpenMP 4.5:
34933 # pragma omp ordered ordered-clauses new-line
34934 structured-block */
34936 #define OMP_ORDERED_CLAUSE_MASK \
34937 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREADS) \
34938 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMD))
34940 #define OMP_ORDERED_DEPEND_CLAUSE_MASK \
34941 (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND)
34943 static bool
34944 cp_parser_omp_ordered (cp_parser *parser, cp_token *pragma_tok,
34945 enum pragma_context context, bool *if_p)
34947 location_t loc = pragma_tok->location;
34949 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
34951 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
34952 const char *p = IDENTIFIER_POINTER (id);
34954 if (strcmp (p, "depend") == 0)
34956 if (context == pragma_stmt)
34958 error_at (pragma_tok->location, "%<#pragma omp ordered%> with "
34959 "%<depend%> clause may only be used in compound "
34960 "statements");
34961 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
34962 return false;
34964 tree clauses
34965 = cp_parser_omp_all_clauses (parser,
34966 OMP_ORDERED_DEPEND_CLAUSE_MASK,
34967 "#pragma omp ordered", pragma_tok);
34968 c_finish_omp_ordered (loc, clauses, NULL_TREE);
34969 return false;
34973 tree clauses
34974 = cp_parser_omp_all_clauses (parser, OMP_ORDERED_CLAUSE_MASK,
34975 "#pragma omp ordered", pragma_tok);
34976 c_finish_omp_ordered (loc, clauses,
34977 cp_parser_omp_structured_block (parser, if_p));
34978 return true;
34981 /* OpenMP 2.5:
34983 section-scope:
34984 { section-sequence }
34986 section-sequence:
34987 section-directive[opt] structured-block
34988 section-sequence section-directive structured-block */
34990 static tree
34991 cp_parser_omp_sections_scope (cp_parser *parser)
34993 tree stmt, substmt;
34994 bool error_suppress = false;
34995 cp_token *tok;
34997 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
34998 return NULL_TREE;
35000 stmt = push_stmt_list ();
35002 if (cp_parser_pragma_kind (cp_lexer_peek_token (parser->lexer))
35003 != PRAGMA_OMP_SECTION)
35005 substmt = cp_parser_omp_structured_block (parser, NULL);
35006 substmt = build1 (OMP_SECTION, void_type_node, substmt);
35007 add_stmt (substmt);
35010 while (1)
35012 tok = cp_lexer_peek_token (parser->lexer);
35013 if (tok->type == CPP_CLOSE_BRACE)
35014 break;
35015 if (tok->type == CPP_EOF)
35016 break;
35018 if (cp_parser_pragma_kind (tok) == PRAGMA_OMP_SECTION)
35020 cp_lexer_consume_token (parser->lexer);
35021 cp_parser_require_pragma_eol (parser, tok);
35022 error_suppress = false;
35024 else if (!error_suppress)
35026 cp_parser_error (parser, "expected %<#pragma omp section%> or %<}%>");
35027 error_suppress = true;
35030 substmt = cp_parser_omp_structured_block (parser, NULL);
35031 substmt = build1 (OMP_SECTION, void_type_node, substmt);
35032 add_stmt (substmt);
35034 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
35036 substmt = pop_stmt_list (stmt);
35038 stmt = make_node (OMP_SECTIONS);
35039 TREE_TYPE (stmt) = void_type_node;
35040 OMP_SECTIONS_BODY (stmt) = substmt;
35042 add_stmt (stmt);
35043 return stmt;
35046 /* OpenMP 2.5:
35047 # pragma omp sections sections-clause[optseq] newline
35048 sections-scope */
35050 #define OMP_SECTIONS_CLAUSE_MASK \
35051 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35052 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35053 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
35054 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
35055 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
35057 static tree
35058 cp_parser_omp_sections (cp_parser *parser, cp_token *pragma_tok,
35059 char *p_name, omp_clause_mask mask, tree *cclauses)
35061 tree clauses, ret;
35062 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
35064 strcat (p_name, " sections");
35065 mask |= OMP_SECTIONS_CLAUSE_MASK;
35066 if (cclauses)
35067 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT);
35069 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
35070 cclauses == NULL);
35071 if (cclauses)
35073 cp_omp_split_clauses (loc, OMP_SECTIONS, mask, clauses, cclauses);
35074 clauses = cclauses[C_OMP_CLAUSE_SPLIT_SECTIONS];
35077 ret = cp_parser_omp_sections_scope (parser);
35078 if (ret)
35079 OMP_SECTIONS_CLAUSES (ret) = clauses;
35081 return ret;
35084 /* OpenMP 2.5:
35085 # pragma omp parallel parallel-clause[optseq] new-line
35086 structured-block
35087 # pragma omp parallel for parallel-for-clause[optseq] new-line
35088 structured-block
35089 # pragma omp parallel sections parallel-sections-clause[optseq] new-line
35090 structured-block
35092 OpenMP 4.0:
35093 # pragma omp parallel for simd parallel-for-simd-clause[optseq] new-line
35094 structured-block */
35096 #define OMP_PARALLEL_CLAUSE_MASK \
35097 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
35098 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35099 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35100 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
35101 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
35102 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYIN) \
35103 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
35104 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_THREADS) \
35105 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PROC_BIND))
35107 static tree
35108 cp_parser_omp_parallel (cp_parser *parser, cp_token *pragma_tok,
35109 char *p_name, omp_clause_mask mask, tree *cclauses,
35110 bool *if_p)
35112 tree stmt, clauses, block;
35113 unsigned int save;
35114 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
35116 strcat (p_name, " parallel");
35117 mask |= OMP_PARALLEL_CLAUSE_MASK;
35118 /* #pragma omp target parallel{, for, for simd} disallow copyin clause. */
35119 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP)) != 0
35120 && (mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) == 0)
35121 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYIN);
35123 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
35125 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
35126 if (cclauses == NULL)
35127 cclauses = cclauses_buf;
35129 cp_lexer_consume_token (parser->lexer);
35130 if (!flag_openmp) /* flag_openmp_simd */
35131 return cp_parser_omp_for (parser, pragma_tok, p_name, mask, cclauses,
35132 if_p);
35133 block = begin_omp_parallel ();
35134 save = cp_parser_begin_omp_structured_block (parser);
35135 tree ret = cp_parser_omp_for (parser, pragma_tok, p_name, mask, cclauses,
35136 if_p);
35137 cp_parser_end_omp_structured_block (parser, save);
35138 stmt = finish_omp_parallel (cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
35139 block);
35140 if (ret == NULL_TREE)
35141 return ret;
35142 OMP_PARALLEL_COMBINED (stmt) = 1;
35143 return stmt;
35145 /* When combined with distribute, parallel has to be followed by for.
35146 #pragma omp target parallel is allowed though. */
35147 else if (cclauses
35148 && (mask & (OMP_CLAUSE_MASK_1
35149 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
35151 error_at (loc, "expected %<for%> after %qs", p_name);
35152 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35153 return NULL_TREE;
35155 else if (!flag_openmp) /* flag_openmp_simd */
35157 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35158 return NULL_TREE;
35160 else if (cclauses == NULL && cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35162 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
35163 const char *p = IDENTIFIER_POINTER (id);
35164 if (strcmp (p, "sections") == 0)
35166 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
35167 cclauses = cclauses_buf;
35169 cp_lexer_consume_token (parser->lexer);
35170 block = begin_omp_parallel ();
35171 save = cp_parser_begin_omp_structured_block (parser);
35172 cp_parser_omp_sections (parser, pragma_tok, p_name, mask, cclauses);
35173 cp_parser_end_omp_structured_block (parser, save);
35174 stmt = finish_omp_parallel (cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
35175 block);
35176 OMP_PARALLEL_COMBINED (stmt) = 1;
35177 return stmt;
35181 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
35182 cclauses == NULL);
35183 if (cclauses)
35185 cp_omp_split_clauses (loc, OMP_PARALLEL, mask, clauses, cclauses);
35186 clauses = cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL];
35189 block = begin_omp_parallel ();
35190 save = cp_parser_begin_omp_structured_block (parser);
35191 cp_parser_statement (parser, NULL_TREE, false, if_p);
35192 cp_parser_end_omp_structured_block (parser, save);
35193 stmt = finish_omp_parallel (clauses, block);
35194 return stmt;
35197 /* OpenMP 2.5:
35198 # pragma omp single single-clause[optseq] new-line
35199 structured-block */
35201 #define OMP_SINGLE_CLAUSE_MASK \
35202 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35203 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35204 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \
35205 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
35207 static tree
35208 cp_parser_omp_single (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
35210 tree stmt = make_node (OMP_SINGLE);
35211 TREE_TYPE (stmt) = void_type_node;
35213 OMP_SINGLE_CLAUSES (stmt)
35214 = cp_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
35215 "#pragma omp single", pragma_tok);
35216 OMP_SINGLE_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
35218 return add_stmt (stmt);
35221 /* OpenMP 3.0:
35222 # pragma omp task task-clause[optseq] new-line
35223 structured-block */
35225 #define OMP_TASK_CLAUSE_MASK \
35226 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
35227 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED) \
35228 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
35229 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35230 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35231 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
35232 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL) \
35233 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE) \
35234 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
35235 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIORITY))
35237 static tree
35238 cp_parser_omp_task (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
35240 tree clauses, block;
35241 unsigned int save;
35243 clauses = cp_parser_omp_all_clauses (parser, OMP_TASK_CLAUSE_MASK,
35244 "#pragma omp task", pragma_tok);
35245 block = begin_omp_task ();
35246 save = cp_parser_begin_omp_structured_block (parser);
35247 cp_parser_statement (parser, NULL_TREE, false, if_p);
35248 cp_parser_end_omp_structured_block (parser, save);
35249 return finish_omp_task (clauses, block);
35252 /* OpenMP 3.0:
35253 # pragma omp taskwait new-line */
35255 static void
35256 cp_parser_omp_taskwait (cp_parser *parser, cp_token *pragma_tok)
35258 cp_parser_require_pragma_eol (parser, pragma_tok);
35259 finish_omp_taskwait ();
35262 /* OpenMP 3.1:
35263 # pragma omp taskyield new-line */
35265 static void
35266 cp_parser_omp_taskyield (cp_parser *parser, cp_token *pragma_tok)
35268 cp_parser_require_pragma_eol (parser, pragma_tok);
35269 finish_omp_taskyield ();
35272 /* OpenMP 4.0:
35273 # pragma omp taskgroup new-line
35274 structured-block */
35276 static tree
35277 cp_parser_omp_taskgroup (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
35279 cp_parser_require_pragma_eol (parser, pragma_tok);
35280 return c_finish_omp_taskgroup (input_location,
35281 cp_parser_omp_structured_block (parser,
35282 if_p));
35286 /* OpenMP 2.5:
35287 # pragma omp threadprivate (variable-list) */
35289 static void
35290 cp_parser_omp_threadprivate (cp_parser *parser, cp_token *pragma_tok)
35292 tree vars;
35294 vars = cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
35295 cp_parser_require_pragma_eol (parser, pragma_tok);
35297 finish_omp_threadprivate (vars);
35300 /* OpenMP 4.0:
35301 # pragma omp cancel cancel-clause[optseq] new-line */
35303 #define OMP_CANCEL_CLAUSE_MASK \
35304 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
35305 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
35306 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
35307 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP) \
35308 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
35310 static void
35311 cp_parser_omp_cancel (cp_parser *parser, cp_token *pragma_tok)
35313 tree clauses = cp_parser_omp_all_clauses (parser, OMP_CANCEL_CLAUSE_MASK,
35314 "#pragma omp cancel", pragma_tok);
35315 finish_omp_cancel (clauses);
35318 /* OpenMP 4.0:
35319 # pragma omp cancellation point cancelpt-clause[optseq] new-line */
35321 #define OMP_CANCELLATION_POINT_CLAUSE_MASK \
35322 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
35323 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
35324 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
35325 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP))
35327 static void
35328 cp_parser_omp_cancellation_point (cp_parser *parser, cp_token *pragma_tok,
35329 enum pragma_context context)
35331 tree clauses;
35332 bool point_seen = false;
35334 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35336 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
35337 const char *p = IDENTIFIER_POINTER (id);
35339 if (strcmp (p, "point") == 0)
35341 cp_lexer_consume_token (parser->lexer);
35342 point_seen = true;
35345 if (!point_seen)
35347 cp_parser_error (parser, "expected %<point%>");
35348 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35349 return;
35352 if (context != pragma_compound)
35354 if (context == pragma_stmt)
35355 error_at (pragma_tok->location,
35356 "%<#pragma omp cancellation point%> may only be used in"
35357 " compound statements");
35358 else
35359 cp_parser_error (parser, "expected declaration specifiers");
35360 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35361 return;
35364 clauses = cp_parser_omp_all_clauses (parser,
35365 OMP_CANCELLATION_POINT_CLAUSE_MASK,
35366 "#pragma omp cancellation point",
35367 pragma_tok);
35368 finish_omp_cancellation_point (clauses);
35371 /* OpenMP 4.0:
35372 #pragma omp distribute distribute-clause[optseq] new-line
35373 for-loop */
35375 #define OMP_DISTRIBUTE_CLAUSE_MASK \
35376 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35377 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35378 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
35379 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)\
35380 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
35382 static tree
35383 cp_parser_omp_distribute (cp_parser *parser, cp_token *pragma_tok,
35384 char *p_name, omp_clause_mask mask, tree *cclauses,
35385 bool *if_p)
35387 tree clauses, sb, ret;
35388 unsigned int save;
35389 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
35391 strcat (p_name, " distribute");
35392 mask |= OMP_DISTRIBUTE_CLAUSE_MASK;
35394 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35396 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
35397 const char *p = IDENTIFIER_POINTER (id);
35398 bool simd = false;
35399 bool parallel = false;
35401 if (strcmp (p, "simd") == 0)
35402 simd = true;
35403 else
35404 parallel = strcmp (p, "parallel") == 0;
35405 if (parallel || simd)
35407 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
35408 if (cclauses == NULL)
35409 cclauses = cclauses_buf;
35410 cp_lexer_consume_token (parser->lexer);
35411 if (!flag_openmp) /* flag_openmp_simd */
35413 if (simd)
35414 return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
35415 cclauses, if_p);
35416 else
35417 return cp_parser_omp_parallel (parser, pragma_tok, p_name, mask,
35418 cclauses, if_p);
35420 sb = begin_omp_structured_block ();
35421 save = cp_parser_begin_omp_structured_block (parser);
35422 if (simd)
35423 ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
35424 cclauses, if_p);
35425 else
35426 ret = cp_parser_omp_parallel (parser, pragma_tok, p_name, mask,
35427 cclauses, if_p);
35428 cp_parser_end_omp_structured_block (parser, save);
35429 tree body = finish_omp_structured_block (sb);
35430 if (ret == NULL)
35431 return ret;
35432 ret = make_node (OMP_DISTRIBUTE);
35433 TREE_TYPE (ret) = void_type_node;
35434 OMP_FOR_BODY (ret) = body;
35435 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_DISTRIBUTE];
35436 SET_EXPR_LOCATION (ret, loc);
35437 add_stmt (ret);
35438 return ret;
35441 if (!flag_openmp) /* flag_openmp_simd */
35443 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35444 return NULL_TREE;
35447 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
35448 cclauses == NULL);
35449 if (cclauses)
35451 cp_omp_split_clauses (loc, OMP_DISTRIBUTE, mask, clauses, cclauses);
35452 clauses = cclauses[C_OMP_CLAUSE_SPLIT_DISTRIBUTE];
35455 sb = begin_omp_structured_block ();
35456 save = cp_parser_begin_omp_structured_block (parser);
35458 ret = cp_parser_omp_for_loop (parser, OMP_DISTRIBUTE, clauses, NULL, if_p);
35460 cp_parser_end_omp_structured_block (parser, save);
35461 add_stmt (finish_omp_structured_block (sb));
35463 return ret;
35466 /* OpenMP 4.0:
35467 # pragma omp teams teams-clause[optseq] new-line
35468 structured-block */
35470 #define OMP_TEAMS_CLAUSE_MASK \
35471 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35472 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35473 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
35474 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
35475 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TEAMS) \
35476 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREAD_LIMIT) \
35477 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT))
35479 static tree
35480 cp_parser_omp_teams (cp_parser *parser, cp_token *pragma_tok,
35481 char *p_name, omp_clause_mask mask, tree *cclauses,
35482 bool *if_p)
35484 tree clauses, sb, ret;
35485 unsigned int save;
35486 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
35488 strcat (p_name, " teams");
35489 mask |= OMP_TEAMS_CLAUSE_MASK;
35491 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35493 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
35494 const char *p = IDENTIFIER_POINTER (id);
35495 if (strcmp (p, "distribute") == 0)
35497 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
35498 if (cclauses == NULL)
35499 cclauses = cclauses_buf;
35501 cp_lexer_consume_token (parser->lexer);
35502 if (!flag_openmp) /* flag_openmp_simd */
35503 return cp_parser_omp_distribute (parser, pragma_tok, p_name, mask,
35504 cclauses, if_p);
35505 sb = begin_omp_structured_block ();
35506 save = cp_parser_begin_omp_structured_block (parser);
35507 ret = cp_parser_omp_distribute (parser, pragma_tok, p_name, mask,
35508 cclauses, if_p);
35509 cp_parser_end_omp_structured_block (parser, save);
35510 tree body = finish_omp_structured_block (sb);
35511 if (ret == NULL)
35512 return ret;
35513 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
35514 ret = make_node (OMP_TEAMS);
35515 TREE_TYPE (ret) = void_type_node;
35516 OMP_TEAMS_CLAUSES (ret) = clauses;
35517 OMP_TEAMS_BODY (ret) = body;
35518 OMP_TEAMS_COMBINED (ret) = 1;
35519 return add_stmt (ret);
35522 if (!flag_openmp) /* flag_openmp_simd */
35524 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35525 return NULL_TREE;
35528 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
35529 cclauses == NULL);
35530 if (cclauses)
35532 cp_omp_split_clauses (loc, OMP_TEAMS, mask, clauses, cclauses);
35533 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
35536 tree stmt = make_node (OMP_TEAMS);
35537 TREE_TYPE (stmt) = void_type_node;
35538 OMP_TEAMS_CLAUSES (stmt) = clauses;
35539 OMP_TEAMS_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
35541 return add_stmt (stmt);
35544 /* OpenMP 4.0:
35545 # pragma omp target data target-data-clause[optseq] new-line
35546 structured-block */
35548 #define OMP_TARGET_DATA_CLAUSE_MASK \
35549 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
35550 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
35551 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
35552 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR))
35554 static tree
35555 cp_parser_omp_target_data (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
35557 tree clauses
35558 = cp_parser_omp_all_clauses (parser, OMP_TARGET_DATA_CLAUSE_MASK,
35559 "#pragma omp target data", pragma_tok);
35560 int map_seen = 0;
35561 for (tree *pc = &clauses; *pc;)
35563 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
35564 switch (OMP_CLAUSE_MAP_KIND (*pc))
35566 case GOMP_MAP_TO:
35567 case GOMP_MAP_ALWAYS_TO:
35568 case GOMP_MAP_FROM:
35569 case GOMP_MAP_ALWAYS_FROM:
35570 case GOMP_MAP_TOFROM:
35571 case GOMP_MAP_ALWAYS_TOFROM:
35572 case GOMP_MAP_ALLOC:
35573 map_seen = 3;
35574 break;
35575 case GOMP_MAP_FIRSTPRIVATE_POINTER:
35576 case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
35577 case GOMP_MAP_ALWAYS_POINTER:
35578 break;
35579 default:
35580 map_seen |= 1;
35581 error_at (OMP_CLAUSE_LOCATION (*pc),
35582 "%<#pragma omp target data%> with map-type other "
35583 "than %<to%>, %<from%>, %<tofrom%> or %<alloc%> "
35584 "on %<map%> clause");
35585 *pc = OMP_CLAUSE_CHAIN (*pc);
35586 continue;
35588 pc = &OMP_CLAUSE_CHAIN (*pc);
35591 if (map_seen != 3)
35593 if (map_seen == 0)
35594 error_at (pragma_tok->location,
35595 "%<#pragma omp target data%> must contain at least "
35596 "one %<map%> clause");
35597 return NULL_TREE;
35600 tree stmt = make_node (OMP_TARGET_DATA);
35601 TREE_TYPE (stmt) = void_type_node;
35602 OMP_TARGET_DATA_CLAUSES (stmt) = clauses;
35604 keep_next_level (true);
35605 OMP_TARGET_DATA_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
35607 SET_EXPR_LOCATION (stmt, pragma_tok->location);
35608 return add_stmt (stmt);
35611 /* OpenMP 4.5:
35612 # pragma omp target enter data target-enter-data-clause[optseq] new-line
35613 structured-block */
35615 #define OMP_TARGET_ENTER_DATA_CLAUSE_MASK \
35616 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
35617 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
35618 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
35619 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
35620 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
35622 static tree
35623 cp_parser_omp_target_enter_data (cp_parser *parser, cp_token *pragma_tok,
35624 enum pragma_context context)
35626 bool data_seen = false;
35627 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35629 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
35630 const char *p = IDENTIFIER_POINTER (id);
35632 if (strcmp (p, "data") == 0)
35634 cp_lexer_consume_token (parser->lexer);
35635 data_seen = true;
35638 if (!data_seen)
35640 cp_parser_error (parser, "expected %<data%>");
35641 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35642 return NULL_TREE;
35645 if (context == pragma_stmt)
35647 error_at (pragma_tok->location,
35648 "%<#pragma omp target enter data%> may only be "
35649 "used in compound statements");
35650 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35651 return NULL_TREE;
35654 tree clauses
35655 = cp_parser_omp_all_clauses (parser, OMP_TARGET_ENTER_DATA_CLAUSE_MASK,
35656 "#pragma omp target enter data", pragma_tok);
35657 int map_seen = 0;
35658 for (tree *pc = &clauses; *pc;)
35660 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
35661 switch (OMP_CLAUSE_MAP_KIND (*pc))
35663 case GOMP_MAP_TO:
35664 case GOMP_MAP_ALWAYS_TO:
35665 case GOMP_MAP_ALLOC:
35666 map_seen = 3;
35667 break;
35668 case GOMP_MAP_FIRSTPRIVATE_POINTER:
35669 case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
35670 case GOMP_MAP_ALWAYS_POINTER:
35671 break;
35672 default:
35673 map_seen |= 1;
35674 error_at (OMP_CLAUSE_LOCATION (*pc),
35675 "%<#pragma omp target enter data%> with map-type other "
35676 "than %<to%> or %<alloc%> on %<map%> clause");
35677 *pc = OMP_CLAUSE_CHAIN (*pc);
35678 continue;
35680 pc = &OMP_CLAUSE_CHAIN (*pc);
35683 if (map_seen != 3)
35685 if (map_seen == 0)
35686 error_at (pragma_tok->location,
35687 "%<#pragma omp target enter data%> must contain at least "
35688 "one %<map%> clause");
35689 return NULL_TREE;
35692 tree stmt = make_node (OMP_TARGET_ENTER_DATA);
35693 TREE_TYPE (stmt) = void_type_node;
35694 OMP_TARGET_ENTER_DATA_CLAUSES (stmt) = clauses;
35695 SET_EXPR_LOCATION (stmt, pragma_tok->location);
35696 return add_stmt (stmt);
35699 /* OpenMP 4.5:
35700 # pragma omp target exit data target-enter-data-clause[optseq] new-line
35701 structured-block */
35703 #define OMP_TARGET_EXIT_DATA_CLAUSE_MASK \
35704 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
35705 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
35706 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
35707 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
35708 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
35710 static tree
35711 cp_parser_omp_target_exit_data (cp_parser *parser, cp_token *pragma_tok,
35712 enum pragma_context context)
35714 bool data_seen = false;
35715 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35717 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
35718 const char *p = IDENTIFIER_POINTER (id);
35720 if (strcmp (p, "data") == 0)
35722 cp_lexer_consume_token (parser->lexer);
35723 data_seen = true;
35726 if (!data_seen)
35728 cp_parser_error (parser, "expected %<data%>");
35729 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35730 return NULL_TREE;
35733 if (context == pragma_stmt)
35735 error_at (pragma_tok->location,
35736 "%<#pragma omp target exit data%> may only be "
35737 "used in compound statements");
35738 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35739 return NULL_TREE;
35742 tree clauses
35743 = cp_parser_omp_all_clauses (parser, OMP_TARGET_EXIT_DATA_CLAUSE_MASK,
35744 "#pragma omp target exit data", pragma_tok);
35745 int map_seen = 0;
35746 for (tree *pc = &clauses; *pc;)
35748 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
35749 switch (OMP_CLAUSE_MAP_KIND (*pc))
35751 case GOMP_MAP_FROM:
35752 case GOMP_MAP_ALWAYS_FROM:
35753 case GOMP_MAP_RELEASE:
35754 case GOMP_MAP_DELETE:
35755 map_seen = 3;
35756 break;
35757 case GOMP_MAP_FIRSTPRIVATE_POINTER:
35758 case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
35759 case GOMP_MAP_ALWAYS_POINTER:
35760 break;
35761 default:
35762 map_seen |= 1;
35763 error_at (OMP_CLAUSE_LOCATION (*pc),
35764 "%<#pragma omp target exit data%> with map-type other "
35765 "than %<from%>, %<release%> or %<delete%> on %<map%>"
35766 " clause");
35767 *pc = OMP_CLAUSE_CHAIN (*pc);
35768 continue;
35770 pc = &OMP_CLAUSE_CHAIN (*pc);
35773 if (map_seen != 3)
35775 if (map_seen == 0)
35776 error_at (pragma_tok->location,
35777 "%<#pragma omp target exit data%> must contain at least "
35778 "one %<map%> clause");
35779 return NULL_TREE;
35782 tree stmt = make_node (OMP_TARGET_EXIT_DATA);
35783 TREE_TYPE (stmt) = void_type_node;
35784 OMP_TARGET_EXIT_DATA_CLAUSES (stmt) = clauses;
35785 SET_EXPR_LOCATION (stmt, pragma_tok->location);
35786 return add_stmt (stmt);
35789 /* OpenMP 4.0:
35790 # pragma omp target update target-update-clause[optseq] new-line */
35792 #define OMP_TARGET_UPDATE_CLAUSE_MASK \
35793 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FROM) \
35794 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO) \
35795 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
35796 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
35797 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
35798 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
35800 static bool
35801 cp_parser_omp_target_update (cp_parser *parser, cp_token *pragma_tok,
35802 enum pragma_context context)
35804 if (context == pragma_stmt)
35806 error_at (pragma_tok->location,
35807 "%<#pragma omp target update%> may only be "
35808 "used in compound statements");
35809 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35810 return false;
35813 tree clauses
35814 = cp_parser_omp_all_clauses (parser, OMP_TARGET_UPDATE_CLAUSE_MASK,
35815 "#pragma omp target update", pragma_tok);
35816 if (omp_find_clause (clauses, OMP_CLAUSE_TO) == NULL_TREE
35817 && omp_find_clause (clauses, OMP_CLAUSE_FROM) == NULL_TREE)
35819 error_at (pragma_tok->location,
35820 "%<#pragma omp target update%> must contain at least one "
35821 "%<from%> or %<to%> clauses");
35822 return false;
35825 tree stmt = make_node (OMP_TARGET_UPDATE);
35826 TREE_TYPE (stmt) = void_type_node;
35827 OMP_TARGET_UPDATE_CLAUSES (stmt) = clauses;
35828 SET_EXPR_LOCATION (stmt, pragma_tok->location);
35829 add_stmt (stmt);
35830 return false;
35833 /* OpenMP 4.0:
35834 # pragma omp target target-clause[optseq] new-line
35835 structured-block */
35837 #define OMP_TARGET_CLAUSE_MASK \
35838 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
35839 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
35840 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
35841 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
35842 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT) \
35843 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35844 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35845 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULTMAP) \
35846 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR))
35848 static bool
35849 cp_parser_omp_target (cp_parser *parser, cp_token *pragma_tok,
35850 enum pragma_context context, bool *if_p)
35852 tree *pc = NULL, stmt;
35854 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35856 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
35857 const char *p = IDENTIFIER_POINTER (id);
35858 enum tree_code ccode = ERROR_MARK;
35860 if (strcmp (p, "teams") == 0)
35861 ccode = OMP_TEAMS;
35862 else if (strcmp (p, "parallel") == 0)
35863 ccode = OMP_PARALLEL;
35864 else if (strcmp (p, "simd") == 0)
35865 ccode = OMP_SIMD;
35866 if (ccode != ERROR_MARK)
35868 tree cclauses[C_OMP_CLAUSE_SPLIT_COUNT];
35869 char p_name[sizeof ("#pragma omp target teams distribute "
35870 "parallel for simd")];
35872 cp_lexer_consume_token (parser->lexer);
35873 strcpy (p_name, "#pragma omp target");
35874 if (!flag_openmp) /* flag_openmp_simd */
35876 tree stmt;
35877 switch (ccode)
35879 case OMP_TEAMS:
35880 stmt = cp_parser_omp_teams (parser, pragma_tok, p_name,
35881 OMP_TARGET_CLAUSE_MASK,
35882 cclauses, if_p);
35883 break;
35884 case OMP_PARALLEL:
35885 stmt = cp_parser_omp_parallel (parser, pragma_tok, p_name,
35886 OMP_TARGET_CLAUSE_MASK,
35887 cclauses, if_p);
35888 break;
35889 case OMP_SIMD:
35890 stmt = cp_parser_omp_simd (parser, pragma_tok, p_name,
35891 OMP_TARGET_CLAUSE_MASK,
35892 cclauses, if_p);
35893 break;
35894 default:
35895 gcc_unreachable ();
35897 return stmt != NULL_TREE;
35899 keep_next_level (true);
35900 tree sb = begin_omp_structured_block (), ret;
35901 unsigned save = cp_parser_begin_omp_structured_block (parser);
35902 switch (ccode)
35904 case OMP_TEAMS:
35905 ret = cp_parser_omp_teams (parser, pragma_tok, p_name,
35906 OMP_TARGET_CLAUSE_MASK, cclauses,
35907 if_p);
35908 break;
35909 case OMP_PARALLEL:
35910 ret = cp_parser_omp_parallel (parser, pragma_tok, p_name,
35911 OMP_TARGET_CLAUSE_MASK, cclauses,
35912 if_p);
35913 break;
35914 case OMP_SIMD:
35915 ret = cp_parser_omp_simd (parser, pragma_tok, p_name,
35916 OMP_TARGET_CLAUSE_MASK, cclauses,
35917 if_p);
35918 break;
35919 default:
35920 gcc_unreachable ();
35922 cp_parser_end_omp_structured_block (parser, save);
35923 tree body = finish_omp_structured_block (sb);
35924 if (ret == NULL_TREE)
35925 return false;
35926 if (ccode == OMP_TEAMS && !processing_template_decl)
35928 /* For combined target teams, ensure the num_teams and
35929 thread_limit clause expressions are evaluated on the host,
35930 before entering the target construct. */
35931 tree c;
35932 for (c = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
35933 c; c = OMP_CLAUSE_CHAIN (c))
35934 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_NUM_TEAMS
35935 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_THREAD_LIMIT)
35936 && TREE_CODE (OMP_CLAUSE_OPERAND (c, 0)) != INTEGER_CST)
35938 tree expr = OMP_CLAUSE_OPERAND (c, 0);
35939 expr = force_target_expr (TREE_TYPE (expr), expr, tf_none);
35940 if (expr == error_mark_node)
35941 continue;
35942 tree tmp = TARGET_EXPR_SLOT (expr);
35943 add_stmt (expr);
35944 OMP_CLAUSE_OPERAND (c, 0) = expr;
35945 tree tc = build_omp_clause (OMP_CLAUSE_LOCATION (c),
35946 OMP_CLAUSE_FIRSTPRIVATE);
35947 OMP_CLAUSE_DECL (tc) = tmp;
35948 OMP_CLAUSE_CHAIN (tc)
35949 = cclauses[C_OMP_CLAUSE_SPLIT_TARGET];
35950 cclauses[C_OMP_CLAUSE_SPLIT_TARGET] = tc;
35953 tree stmt = make_node (OMP_TARGET);
35954 TREE_TYPE (stmt) = void_type_node;
35955 OMP_TARGET_CLAUSES (stmt) = cclauses[C_OMP_CLAUSE_SPLIT_TARGET];
35956 OMP_TARGET_BODY (stmt) = body;
35957 OMP_TARGET_COMBINED (stmt) = 1;
35958 add_stmt (stmt);
35959 pc = &OMP_TARGET_CLAUSES (stmt);
35960 goto check_clauses;
35962 else if (!flag_openmp) /* flag_openmp_simd */
35964 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35965 return false;
35967 else if (strcmp (p, "data") == 0)
35969 cp_lexer_consume_token (parser->lexer);
35970 cp_parser_omp_target_data (parser, pragma_tok, if_p);
35971 return true;
35973 else if (strcmp (p, "enter") == 0)
35975 cp_lexer_consume_token (parser->lexer);
35976 cp_parser_omp_target_enter_data (parser, pragma_tok, context);
35977 return false;
35979 else if (strcmp (p, "exit") == 0)
35981 cp_lexer_consume_token (parser->lexer);
35982 cp_parser_omp_target_exit_data (parser, pragma_tok, context);
35983 return false;
35985 else if (strcmp (p, "update") == 0)
35987 cp_lexer_consume_token (parser->lexer);
35988 return cp_parser_omp_target_update (parser, pragma_tok, context);
35991 if (!flag_openmp) /* flag_openmp_simd */
35993 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35994 return false;
35997 stmt = make_node (OMP_TARGET);
35998 TREE_TYPE (stmt) = void_type_node;
36000 OMP_TARGET_CLAUSES (stmt)
36001 = cp_parser_omp_all_clauses (parser, OMP_TARGET_CLAUSE_MASK,
36002 "#pragma omp target", pragma_tok);
36003 pc = &OMP_TARGET_CLAUSES (stmt);
36004 keep_next_level (true);
36005 OMP_TARGET_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
36007 SET_EXPR_LOCATION (stmt, pragma_tok->location);
36008 add_stmt (stmt);
36010 check_clauses:
36011 while (*pc)
36013 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
36014 switch (OMP_CLAUSE_MAP_KIND (*pc))
36016 case GOMP_MAP_TO:
36017 case GOMP_MAP_ALWAYS_TO:
36018 case GOMP_MAP_FROM:
36019 case GOMP_MAP_ALWAYS_FROM:
36020 case GOMP_MAP_TOFROM:
36021 case GOMP_MAP_ALWAYS_TOFROM:
36022 case GOMP_MAP_ALLOC:
36023 case GOMP_MAP_FIRSTPRIVATE_POINTER:
36024 case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
36025 case GOMP_MAP_ALWAYS_POINTER:
36026 break;
36027 default:
36028 error_at (OMP_CLAUSE_LOCATION (*pc),
36029 "%<#pragma omp target%> with map-type other "
36030 "than %<to%>, %<from%>, %<tofrom%> or %<alloc%> "
36031 "on %<map%> clause");
36032 *pc = OMP_CLAUSE_CHAIN (*pc);
36033 continue;
36035 pc = &OMP_CLAUSE_CHAIN (*pc);
36037 return true;
36040 /* OpenACC 2.0:
36041 # pragma acc cache (variable-list) new-line
36044 static tree
36045 cp_parser_oacc_cache (cp_parser *parser, cp_token *pragma_tok)
36047 tree stmt, clauses;
36049 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE__CACHE_, NULL_TREE);
36050 clauses = finish_omp_clauses (clauses, C_ORT_ACC);
36052 cp_parser_require_pragma_eol (parser, cp_lexer_peek_token (parser->lexer));
36054 stmt = make_node (OACC_CACHE);
36055 TREE_TYPE (stmt) = void_type_node;
36056 OACC_CACHE_CLAUSES (stmt) = clauses;
36057 SET_EXPR_LOCATION (stmt, pragma_tok->location);
36058 add_stmt (stmt);
36060 return stmt;
36063 /* OpenACC 2.0:
36064 # pragma acc data oacc-data-clause[optseq] new-line
36065 structured-block */
36067 #define OACC_DATA_CLAUSE_MASK \
36068 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
36069 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
36070 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
36071 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
36072 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
36073 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
36074 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
36075 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
36076 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
36077 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
36078 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE))
36080 static tree
36081 cp_parser_oacc_data (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
36083 tree stmt, clauses, block;
36084 unsigned int save;
36086 clauses = cp_parser_oacc_all_clauses (parser, OACC_DATA_CLAUSE_MASK,
36087 "#pragma acc data", pragma_tok);
36089 block = begin_omp_parallel ();
36090 save = cp_parser_begin_omp_structured_block (parser);
36091 cp_parser_statement (parser, NULL_TREE, false, if_p);
36092 cp_parser_end_omp_structured_block (parser, save);
36093 stmt = finish_oacc_data (clauses, block);
36094 return stmt;
36097 /* OpenACC 2.0:
36098 # pragma acc host_data <clauses> new-line
36099 structured-block */
36101 #define OACC_HOST_DATA_CLAUSE_MASK \
36102 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_USE_DEVICE) )
36104 static tree
36105 cp_parser_oacc_host_data (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
36107 tree stmt, clauses, block;
36108 unsigned int save;
36110 clauses = cp_parser_oacc_all_clauses (parser, OACC_HOST_DATA_CLAUSE_MASK,
36111 "#pragma acc host_data", pragma_tok);
36113 block = begin_omp_parallel ();
36114 save = cp_parser_begin_omp_structured_block (parser);
36115 cp_parser_statement (parser, NULL_TREE, false, if_p);
36116 cp_parser_end_omp_structured_block (parser, save);
36117 stmt = finish_oacc_host_data (clauses, block);
36118 return stmt;
36121 /* OpenACC 2.0:
36122 # pragma acc declare oacc-data-clause[optseq] new-line
36125 #define OACC_DECLARE_CLAUSE_MASK \
36126 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
36127 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
36128 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
36129 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
36130 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
36131 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT) \
36132 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_LINK) \
36133 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
36134 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
36135 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
36136 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
36137 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE))
36139 static tree
36140 cp_parser_oacc_declare (cp_parser *parser, cp_token *pragma_tok)
36142 tree clauses, stmt;
36143 bool error = false;
36145 clauses = cp_parser_oacc_all_clauses (parser, OACC_DECLARE_CLAUSE_MASK,
36146 "#pragma acc declare", pragma_tok, true);
36149 if (omp_find_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
36151 error_at (pragma_tok->location,
36152 "no valid clauses specified in %<#pragma acc declare%>");
36153 return NULL_TREE;
36156 for (tree t = clauses; t; t = OMP_CLAUSE_CHAIN (t))
36158 location_t loc = OMP_CLAUSE_LOCATION (t);
36159 tree decl = OMP_CLAUSE_DECL (t);
36160 if (!DECL_P (decl))
36162 error_at (loc, "array section in %<#pragma acc declare%>");
36163 error = true;
36164 continue;
36166 gcc_assert (OMP_CLAUSE_CODE (t) == OMP_CLAUSE_MAP);
36167 switch (OMP_CLAUSE_MAP_KIND (t))
36169 case GOMP_MAP_FIRSTPRIVATE_POINTER:
36170 case GOMP_MAP_FORCE_ALLOC:
36171 case GOMP_MAP_FORCE_TO:
36172 case GOMP_MAP_FORCE_DEVICEPTR:
36173 case GOMP_MAP_DEVICE_RESIDENT:
36174 break;
36176 case GOMP_MAP_LINK:
36177 if (!global_bindings_p ()
36178 && (TREE_STATIC (decl)
36179 || !DECL_EXTERNAL (decl)))
36181 error_at (loc,
36182 "%qD must be a global variable in "
36183 "%<#pragma acc declare link%>",
36184 decl);
36185 error = true;
36186 continue;
36188 break;
36190 default:
36191 if (global_bindings_p ())
36193 error_at (loc, "invalid OpenACC clause at file scope");
36194 error = true;
36195 continue;
36197 if (DECL_EXTERNAL (decl))
36199 error_at (loc,
36200 "invalid use of %<extern%> variable %qD "
36201 "in %<#pragma acc declare%>", decl);
36202 error = true;
36203 continue;
36205 else if (TREE_PUBLIC (decl))
36207 error_at (loc,
36208 "invalid use of %<global%> variable %qD "
36209 "in %<#pragma acc declare%>", decl);
36210 error = true;
36211 continue;
36213 break;
36216 if (lookup_attribute ("omp declare target", DECL_ATTRIBUTES (decl))
36217 || lookup_attribute ("omp declare target link",
36218 DECL_ATTRIBUTES (decl)))
36220 error_at (loc, "variable %qD used more than once with "
36221 "%<#pragma acc declare%>", decl);
36222 error = true;
36223 continue;
36226 if (!error)
36228 tree id;
36230 if (OMP_CLAUSE_MAP_KIND (t) == GOMP_MAP_LINK)
36231 id = get_identifier ("omp declare target link");
36232 else
36233 id = get_identifier ("omp declare target");
36235 DECL_ATTRIBUTES (decl)
36236 = tree_cons (id, NULL_TREE, DECL_ATTRIBUTES (decl));
36237 if (global_bindings_p ())
36239 symtab_node *node = symtab_node::get (decl);
36240 if (node != NULL)
36242 node->offloadable = 1;
36243 if (ENABLE_OFFLOADING)
36245 g->have_offload = true;
36246 if (is_a <varpool_node *> (node))
36247 vec_safe_push (offload_vars, decl);
36254 if (error || global_bindings_p ())
36255 return NULL_TREE;
36257 stmt = make_node (OACC_DECLARE);
36258 TREE_TYPE (stmt) = void_type_node;
36259 OACC_DECLARE_CLAUSES (stmt) = clauses;
36260 SET_EXPR_LOCATION (stmt, pragma_tok->location);
36262 add_stmt (stmt);
36264 return NULL_TREE;
36267 /* OpenACC 2.0:
36268 # pragma acc enter data oacc-enter-data-clause[optseq] new-line
36272 # pragma acc exit data oacc-exit-data-clause[optseq] new-line
36274 LOC is the location of the #pragma token.
36277 #define OACC_ENTER_DATA_CLAUSE_MASK \
36278 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
36279 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
36280 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
36281 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
36282 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
36283 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE) \
36284 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
36286 #define OACC_EXIT_DATA_CLAUSE_MASK \
36287 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
36288 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
36289 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
36290 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DELETE) \
36291 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
36293 static tree
36294 cp_parser_oacc_enter_exit_data (cp_parser *parser, cp_token *pragma_tok,
36295 bool enter)
36297 location_t loc = pragma_tok->location;
36298 tree stmt, clauses;
36299 const char *p = "";
36301 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36302 p = IDENTIFIER_POINTER (cp_lexer_peek_token (parser->lexer)->u.value);
36304 if (strcmp (p, "data") != 0)
36306 error_at (loc, enter
36307 ? "expected %<data%> after %<#pragma acc enter%>"
36308 : "expected %<data%> after %<#pragma acc exit%>");
36309 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36310 return NULL_TREE;
36313 cp_lexer_consume_token (parser->lexer);
36315 if (enter)
36316 clauses = cp_parser_oacc_all_clauses (parser, OACC_ENTER_DATA_CLAUSE_MASK,
36317 "#pragma acc enter data", pragma_tok);
36318 else
36319 clauses = cp_parser_oacc_all_clauses (parser, OACC_EXIT_DATA_CLAUSE_MASK,
36320 "#pragma acc exit data", pragma_tok);
36322 if (omp_find_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
36324 error_at (loc, "%<#pragma acc %s data%> has no data movement clause",
36325 enter ? "enter" : "exit");
36326 return NULL_TREE;
36329 stmt = enter ? make_node (OACC_ENTER_DATA) : make_node (OACC_EXIT_DATA);
36330 TREE_TYPE (stmt) = void_type_node;
36331 OMP_STANDALONE_CLAUSES (stmt) = clauses;
36332 SET_EXPR_LOCATION (stmt, pragma_tok->location);
36333 add_stmt (stmt);
36334 return stmt;
36337 /* OpenACC 2.0:
36338 # pragma acc loop oacc-loop-clause[optseq] new-line
36339 structured-block */
36341 #define OACC_LOOP_CLAUSE_MASK \
36342 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COLLAPSE) \
36343 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRIVATE) \
36344 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION) \
36345 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_GANG) \
36346 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR) \
36347 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WORKER) \
36348 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_AUTO) \
36349 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_INDEPENDENT) \
36350 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SEQ) \
36351 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_TILE))
36353 static tree
36354 cp_parser_oacc_loop (cp_parser *parser, cp_token *pragma_tok, char *p_name,
36355 omp_clause_mask mask, tree *cclauses, bool *if_p)
36357 bool is_parallel = ((mask >> PRAGMA_OACC_CLAUSE_REDUCTION) & 1) == 1;
36359 strcat (p_name, " loop");
36360 mask |= OACC_LOOP_CLAUSE_MASK;
36362 tree clauses = cp_parser_oacc_all_clauses (parser, mask, p_name, pragma_tok,
36363 cclauses == NULL);
36364 if (cclauses)
36366 clauses = c_oacc_split_loop_clauses (clauses, cclauses, is_parallel);
36367 if (*cclauses)
36368 *cclauses = finish_omp_clauses (*cclauses, C_ORT_ACC);
36369 if (clauses)
36370 clauses = finish_omp_clauses (clauses, C_ORT_ACC);
36373 tree block = begin_omp_structured_block ();
36374 int save = cp_parser_begin_omp_structured_block (parser);
36375 tree stmt = cp_parser_omp_for_loop (parser, OACC_LOOP, clauses, NULL, if_p);
36376 cp_parser_end_omp_structured_block (parser, save);
36377 add_stmt (finish_omp_structured_block (block));
36379 return stmt;
36382 /* OpenACC 2.0:
36383 # pragma acc kernels oacc-kernels-clause[optseq] new-line
36384 structured-block
36388 # pragma acc parallel oacc-parallel-clause[optseq] new-line
36389 structured-block
36392 #define OACC_KERNELS_CLAUSE_MASK \
36393 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
36394 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
36395 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
36396 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
36397 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
36398 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEFAULT) \
36399 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
36400 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
36401 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
36402 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
36403 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
36404 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
36405 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE) \
36406 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
36408 #define OACC_PARALLEL_CLAUSE_MASK \
36409 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
36410 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
36411 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
36412 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
36413 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
36414 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEFAULT) \
36415 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
36416 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_FIRSTPRIVATE) \
36417 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
36418 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_GANGS) \
36419 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_WORKERS) \
36420 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
36421 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
36422 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
36423 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
36424 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE) \
36425 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRIVATE) \
36426 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION) \
36427 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR_LENGTH) \
36428 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
36430 static tree
36431 cp_parser_oacc_kernels_parallel (cp_parser *parser, cp_token *pragma_tok,
36432 char *p_name, bool *if_p)
36434 omp_clause_mask mask;
36435 enum tree_code code;
36436 switch (cp_parser_pragma_kind (pragma_tok))
36438 case PRAGMA_OACC_KERNELS:
36439 strcat (p_name, " kernels");
36440 mask = OACC_KERNELS_CLAUSE_MASK;
36441 code = OACC_KERNELS;
36442 break;
36443 case PRAGMA_OACC_PARALLEL:
36444 strcat (p_name, " parallel");
36445 mask = OACC_PARALLEL_CLAUSE_MASK;
36446 code = OACC_PARALLEL;
36447 break;
36448 default:
36449 gcc_unreachable ();
36452 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36454 const char *p
36455 = IDENTIFIER_POINTER (cp_lexer_peek_token (parser->lexer)->u.value);
36456 if (strcmp (p, "loop") == 0)
36458 cp_lexer_consume_token (parser->lexer);
36459 tree block = begin_omp_parallel ();
36460 tree clauses;
36461 cp_parser_oacc_loop (parser, pragma_tok, p_name, mask, &clauses,
36462 if_p);
36463 return finish_omp_construct (code, block, clauses);
36467 tree clauses = cp_parser_oacc_all_clauses (parser, mask, p_name, pragma_tok);
36469 tree block = begin_omp_parallel ();
36470 unsigned int save = cp_parser_begin_omp_structured_block (parser);
36471 cp_parser_statement (parser, NULL_TREE, false, if_p);
36472 cp_parser_end_omp_structured_block (parser, save);
36473 return finish_omp_construct (code, block, clauses);
36476 /* OpenACC 2.0:
36477 # pragma acc update oacc-update-clause[optseq] new-line
36480 #define OACC_UPDATE_CLAUSE_MASK \
36481 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
36482 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICE) \
36483 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_HOST) \
36484 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
36485 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SELF) \
36486 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT))
36488 static tree
36489 cp_parser_oacc_update (cp_parser *parser, cp_token *pragma_tok)
36491 tree stmt, clauses;
36493 clauses = cp_parser_oacc_all_clauses (parser, OACC_UPDATE_CLAUSE_MASK,
36494 "#pragma acc update", pragma_tok);
36496 if (omp_find_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
36498 error_at (pragma_tok->location,
36499 "%<#pragma acc update%> must contain at least one "
36500 "%<device%> or %<host%> or %<self%> clause");
36501 return NULL_TREE;
36504 stmt = make_node (OACC_UPDATE);
36505 TREE_TYPE (stmt) = void_type_node;
36506 OACC_UPDATE_CLAUSES (stmt) = clauses;
36507 SET_EXPR_LOCATION (stmt, pragma_tok->location);
36508 add_stmt (stmt);
36509 return stmt;
36512 /* OpenACC 2.0:
36513 # pragma acc wait [(intseq)] oacc-wait-clause[optseq] new-line
36515 LOC is the location of the #pragma token.
36518 #define OACC_WAIT_CLAUSE_MASK \
36519 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC))
36521 static tree
36522 cp_parser_oacc_wait (cp_parser *parser, cp_token *pragma_tok)
36524 tree clauses, list = NULL_TREE, stmt = NULL_TREE;
36525 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
36527 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
36528 list = cp_parser_oacc_wait_list (parser, loc, list);
36530 clauses = cp_parser_oacc_all_clauses (parser, OACC_WAIT_CLAUSE_MASK,
36531 "#pragma acc wait", pragma_tok);
36533 stmt = c_finish_oacc_wait (loc, list, clauses);
36534 stmt = finish_expr_stmt (stmt);
36536 return stmt;
36539 /* OpenMP 4.0:
36540 # pragma omp declare simd declare-simd-clauses[optseq] new-line */
36542 #define OMP_DECLARE_SIMD_CLAUSE_MASK \
36543 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN) \
36544 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
36545 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
36546 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNIFORM) \
36547 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_INBRANCH) \
36548 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOTINBRANCH))
36550 static void
36551 cp_parser_omp_declare_simd (cp_parser *parser, cp_token *pragma_tok,
36552 enum pragma_context context)
36554 bool first_p = parser->omp_declare_simd == NULL;
36555 cp_omp_declare_simd_data data;
36556 if (first_p)
36558 data.error_seen = false;
36559 data.fndecl_seen = false;
36560 data.tokens = vNULL;
36561 data.clauses = NULL_TREE;
36562 /* It is safe to take the address of a local variable; it will only be
36563 used while this scope is live. */
36564 parser->omp_declare_simd = &data;
36567 /* Store away all pragma tokens. */
36568 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
36569 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
36570 cp_lexer_consume_token (parser->lexer);
36571 if (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
36572 parser->omp_declare_simd->error_seen = true;
36573 cp_parser_require_pragma_eol (parser, pragma_tok);
36574 struct cp_token_cache *cp
36575 = cp_token_cache_new (pragma_tok, cp_lexer_peek_token (parser->lexer));
36576 parser->omp_declare_simd->tokens.safe_push (cp);
36578 if (first_p)
36580 while (cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA))
36581 cp_parser_pragma (parser, context, NULL);
36582 switch (context)
36584 case pragma_external:
36585 cp_parser_declaration (parser);
36586 break;
36587 case pragma_member:
36588 cp_parser_member_declaration (parser);
36589 break;
36590 case pragma_objc_icode:
36591 cp_parser_block_declaration (parser, /*statement_p=*/false);
36592 break;
36593 default:
36594 cp_parser_declaration_statement (parser);
36595 break;
36597 if (parser->omp_declare_simd
36598 && !parser->omp_declare_simd->error_seen
36599 && !parser->omp_declare_simd->fndecl_seen)
36600 error_at (pragma_tok->location,
36601 "%<#pragma omp declare simd%> not immediately followed by "
36602 "function declaration or definition");
36603 data.tokens.release ();
36604 parser->omp_declare_simd = NULL;
36608 /* Handles the delayed parsing of the Cilk Plus SIMD-enabled function.
36609 This function is modelled similar to the late parsing of omp declare
36610 simd. */
36612 static tree
36613 cp_parser_late_parsing_cilk_simd_fn_info (cp_parser *parser, tree attrs)
36615 struct cp_token_cache *ce;
36616 cp_omp_declare_simd_data *info = parser->cilk_simd_fn_info;
36617 int ii = 0;
36619 if (parser->omp_declare_simd != NULL
36620 || lookup_attribute ("simd", attrs))
36622 error ("%<#pragma omp declare simd%> or %<simd%> attribute cannot be "
36623 "used in the same function marked as a Cilk Plus SIMD-enabled "
36624 "function");
36625 parser->cilk_simd_fn_info->tokens.release ();
36626 XDELETE (parser->cilk_simd_fn_info);
36627 parser->cilk_simd_fn_info = NULL;
36628 return attrs;
36630 if (!info->error_seen && info->fndecl_seen)
36632 error ("vector attribute not immediately followed by a single function"
36633 " declaration or definition");
36634 info->error_seen = true;
36636 if (info->error_seen)
36637 return attrs;
36639 FOR_EACH_VEC_ELT (info->tokens, ii, ce)
36641 tree c, cl;
36643 cp_parser_push_lexer_for_tokens (parser, ce);
36644 parser->lexer->in_pragma = true;
36645 cl = cp_parser_omp_all_clauses (parser, CILK_SIMD_FN_CLAUSE_MASK,
36646 "SIMD-enabled functions attribute",
36647 NULL);
36648 cp_parser_pop_lexer (parser);
36649 if (cl)
36650 cl = tree_cons (NULL_TREE, cl, NULL_TREE);
36652 c = build_tree_list (get_identifier ("cilk simd function"), NULL_TREE);
36653 TREE_CHAIN (c) = attrs;
36654 attrs = c;
36656 c = build_tree_list (get_identifier ("omp declare simd"), cl);
36657 TREE_CHAIN (c) = attrs;
36658 if (processing_template_decl)
36659 ATTR_IS_DEPENDENT (c) = 1;
36660 attrs = c;
36662 info->fndecl_seen = true;
36663 parser->cilk_simd_fn_info->tokens.release ();
36664 XDELETE (parser->cilk_simd_fn_info);
36665 parser->cilk_simd_fn_info = NULL;
36666 return attrs;
36669 /* Finalize #pragma omp declare simd clauses after direct declarator has
36670 been parsed, and put that into "omp declare simd" attribute. */
36672 static tree
36673 cp_parser_late_parsing_omp_declare_simd (cp_parser *parser, tree attrs)
36675 struct cp_token_cache *ce;
36676 cp_omp_declare_simd_data *data = parser->omp_declare_simd;
36677 int i;
36679 if (!data->error_seen && data->fndecl_seen)
36681 error ("%<#pragma omp declare simd%> not immediately followed by "
36682 "a single function declaration or definition");
36683 data->error_seen = true;
36685 if (data->error_seen)
36686 return attrs;
36688 FOR_EACH_VEC_ELT (data->tokens, i, ce)
36690 tree c, cl;
36692 cp_parser_push_lexer_for_tokens (parser, ce);
36693 parser->lexer->in_pragma = true;
36694 gcc_assert (cp_lexer_peek_token (parser->lexer)->type == CPP_PRAGMA);
36695 cp_token *pragma_tok = cp_lexer_consume_token (parser->lexer);
36696 cp_lexer_consume_token (parser->lexer);
36697 cl = cp_parser_omp_all_clauses (parser, OMP_DECLARE_SIMD_CLAUSE_MASK,
36698 "#pragma omp declare simd", pragma_tok);
36699 cp_parser_pop_lexer (parser);
36700 if (cl)
36701 cl = tree_cons (NULL_TREE, cl, NULL_TREE);
36702 c = build_tree_list (get_identifier ("omp declare simd"), cl);
36703 TREE_CHAIN (c) = attrs;
36704 if (processing_template_decl)
36705 ATTR_IS_DEPENDENT (c) = 1;
36706 attrs = c;
36709 data->fndecl_seen = true;
36710 return attrs;
36714 /* OpenMP 4.0:
36715 # pragma omp declare target new-line
36716 declarations and definitions
36717 # pragma omp end declare target new-line
36719 OpenMP 4.5:
36720 # pragma omp declare target ( extended-list ) new-line
36722 # pragma omp declare target declare-target-clauses[seq] new-line */
36724 #define OMP_DECLARE_TARGET_CLAUSE_MASK \
36725 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO) \
36726 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINK))
36728 static void
36729 cp_parser_omp_declare_target (cp_parser *parser, cp_token *pragma_tok)
36731 tree clauses = NULL_TREE;
36732 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36733 clauses
36734 = cp_parser_omp_all_clauses (parser, OMP_DECLARE_TARGET_CLAUSE_MASK,
36735 "#pragma omp declare target", pragma_tok);
36736 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
36738 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_TO_DECLARE,
36739 clauses);
36740 clauses = finish_omp_clauses (clauses, C_ORT_OMP);
36741 cp_parser_require_pragma_eol (parser, pragma_tok);
36743 else
36745 cp_parser_require_pragma_eol (parser, pragma_tok);
36746 scope_chain->omp_declare_target_attribute++;
36747 return;
36749 if (scope_chain->omp_declare_target_attribute)
36750 error_at (pragma_tok->location,
36751 "%<#pragma omp declare target%> with clauses in between "
36752 "%<#pragma omp declare target%> without clauses and "
36753 "%<#pragma omp end declare target%>");
36754 for (tree c = clauses; c; c = OMP_CLAUSE_CHAIN (c))
36756 tree t = OMP_CLAUSE_DECL (c), id;
36757 tree at1 = lookup_attribute ("omp declare target", DECL_ATTRIBUTES (t));
36758 tree at2 = lookup_attribute ("omp declare target link",
36759 DECL_ATTRIBUTES (t));
36760 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINK)
36762 id = get_identifier ("omp declare target link");
36763 std::swap (at1, at2);
36765 else
36766 id = get_identifier ("omp declare target");
36767 if (at2)
36769 error_at (OMP_CLAUSE_LOCATION (c),
36770 "%qD specified both in declare target %<link%> and %<to%>"
36771 " clauses", t);
36772 continue;
36774 if (!at1)
36776 DECL_ATTRIBUTES (t) = tree_cons (id, NULL_TREE, DECL_ATTRIBUTES (t));
36777 if (TREE_CODE (t) != FUNCTION_DECL && !is_global_var (t))
36778 continue;
36780 symtab_node *node = symtab_node::get (t);
36781 if (node != NULL)
36783 node->offloadable = 1;
36784 if (ENABLE_OFFLOADING)
36786 g->have_offload = true;
36787 if (is_a <varpool_node *> (node))
36788 vec_safe_push (offload_vars, t);
36795 static void
36796 cp_parser_omp_end_declare_target (cp_parser *parser, cp_token *pragma_tok)
36798 const char *p = "";
36799 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36801 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
36802 p = IDENTIFIER_POINTER (id);
36804 if (strcmp (p, "declare") == 0)
36806 cp_lexer_consume_token (parser->lexer);
36807 p = "";
36808 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36810 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
36811 p = IDENTIFIER_POINTER (id);
36813 if (strcmp (p, "target") == 0)
36814 cp_lexer_consume_token (parser->lexer);
36815 else
36817 cp_parser_error (parser, "expected %<target%>");
36818 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36819 return;
36822 else
36824 cp_parser_error (parser, "expected %<declare%>");
36825 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36826 return;
36828 cp_parser_require_pragma_eol (parser, pragma_tok);
36829 if (!scope_chain->omp_declare_target_attribute)
36830 error_at (pragma_tok->location,
36831 "%<#pragma omp end declare target%> without corresponding "
36832 "%<#pragma omp declare target%>");
36833 else
36834 scope_chain->omp_declare_target_attribute--;
36837 /* Helper function of cp_parser_omp_declare_reduction. Parse the combiner
36838 expression and optional initializer clause of
36839 #pragma omp declare reduction. We store the expression(s) as
36840 either 3, 6 or 7 special statements inside of the artificial function's
36841 body. The first two statements are DECL_EXPRs for the artificial
36842 OMP_OUT resp. OMP_IN variables, followed by a statement with the combiner
36843 expression that uses those variables.
36844 If there was any INITIALIZER clause, this is followed by further statements,
36845 the fourth and fifth statements are DECL_EXPRs for the artificial
36846 OMP_PRIV resp. OMP_ORIG variables. If the INITIALIZER clause wasn't the
36847 constructor variant (first token after open paren is not omp_priv),
36848 then the sixth statement is a statement with the function call expression
36849 that uses the OMP_PRIV and optionally OMP_ORIG variable.
36850 Otherwise, the sixth statement is whatever statement cp_finish_decl emits
36851 to initialize the OMP_PRIV artificial variable and there is seventh
36852 statement, a DECL_EXPR of the OMP_PRIV statement again. */
36854 static bool
36855 cp_parser_omp_declare_reduction_exprs (tree fndecl, cp_parser *parser)
36857 tree type = TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (fndecl)));
36858 gcc_assert (TREE_CODE (type) == REFERENCE_TYPE);
36859 type = TREE_TYPE (type);
36860 tree omp_out = build_lang_decl (VAR_DECL, get_identifier ("omp_out"), type);
36861 DECL_ARTIFICIAL (omp_out) = 1;
36862 pushdecl (omp_out);
36863 add_decl_expr (omp_out);
36864 tree omp_in = build_lang_decl (VAR_DECL, get_identifier ("omp_in"), type);
36865 DECL_ARTIFICIAL (omp_in) = 1;
36866 pushdecl (omp_in);
36867 add_decl_expr (omp_in);
36868 tree combiner;
36869 tree omp_priv = NULL_TREE, omp_orig = NULL_TREE, initializer = NULL_TREE;
36871 keep_next_level (true);
36872 tree block = begin_omp_structured_block ();
36873 combiner = cp_parser_expression (parser);
36874 finish_expr_stmt (combiner);
36875 block = finish_omp_structured_block (block);
36876 add_stmt (block);
36878 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
36879 return false;
36881 const char *p = "";
36882 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36884 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
36885 p = IDENTIFIER_POINTER (id);
36888 if (strcmp (p, "initializer") == 0)
36890 cp_lexer_consume_token (parser->lexer);
36891 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
36892 return false;
36894 p = "";
36895 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36897 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
36898 p = IDENTIFIER_POINTER (id);
36901 omp_priv = build_lang_decl (VAR_DECL, get_identifier ("omp_priv"), type);
36902 DECL_ARTIFICIAL (omp_priv) = 1;
36903 pushdecl (omp_priv);
36904 add_decl_expr (omp_priv);
36905 omp_orig = build_lang_decl (VAR_DECL, get_identifier ("omp_orig"), type);
36906 DECL_ARTIFICIAL (omp_orig) = 1;
36907 pushdecl (omp_orig);
36908 add_decl_expr (omp_orig);
36910 keep_next_level (true);
36911 block = begin_omp_structured_block ();
36913 bool ctor = false;
36914 if (strcmp (p, "omp_priv") == 0)
36916 bool is_direct_init, is_non_constant_init;
36917 ctor = true;
36918 cp_lexer_consume_token (parser->lexer);
36919 /* Reject initializer (omp_priv) and initializer (omp_priv ()). */
36920 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
36921 || (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
36922 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
36923 == CPP_CLOSE_PAREN
36924 && cp_lexer_peek_nth_token (parser->lexer, 3)->type
36925 == CPP_CLOSE_PAREN))
36927 finish_omp_structured_block (block);
36928 error ("invalid initializer clause");
36929 return false;
36931 initializer = cp_parser_initializer (parser, &is_direct_init,
36932 &is_non_constant_init);
36933 cp_finish_decl (omp_priv, initializer, !is_non_constant_init,
36934 NULL_TREE, LOOKUP_ONLYCONVERTING);
36936 else
36938 cp_parser_parse_tentatively (parser);
36939 tree fn_name = cp_parser_id_expression (parser, /*template_p=*/false,
36940 /*check_dependency_p=*/true,
36941 /*template_p=*/NULL,
36942 /*declarator_p=*/false,
36943 /*optional_p=*/false);
36944 vec<tree, va_gc> *args;
36945 if (fn_name == error_mark_node
36946 || cp_parser_error_occurred (parser)
36947 || !cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
36948 || ((args = cp_parser_parenthesized_expression_list
36949 (parser, non_attr, /*cast_p=*/false,
36950 /*allow_expansion_p=*/true,
36951 /*non_constant_p=*/NULL)),
36952 cp_parser_error_occurred (parser)))
36954 finish_omp_structured_block (block);
36955 cp_parser_abort_tentative_parse (parser);
36956 cp_parser_error (parser, "expected id-expression (arguments)");
36957 return false;
36959 unsigned int i;
36960 tree arg;
36961 FOR_EACH_VEC_SAFE_ELT (args, i, arg)
36962 if (arg == omp_priv
36963 || (TREE_CODE (arg) == ADDR_EXPR
36964 && TREE_OPERAND (arg, 0) == omp_priv))
36965 break;
36966 cp_parser_abort_tentative_parse (parser);
36967 if (arg == NULL_TREE)
36968 error ("one of the initializer call arguments should be %<omp_priv%>"
36969 " or %<&omp_priv%>");
36970 initializer = cp_parser_postfix_expression (parser, false, false, false,
36971 false, NULL);
36972 finish_expr_stmt (initializer);
36975 block = finish_omp_structured_block (block);
36976 cp_walk_tree (&block, cp_remove_omp_priv_cleanup_stmt, omp_priv, NULL);
36977 add_stmt (block);
36979 if (ctor)
36980 add_decl_expr (omp_orig);
36982 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
36983 return false;
36986 if (!cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA_EOL))
36987 cp_parser_required_error (parser, RT_PRAGMA_EOL, /*keyword=*/false);
36989 return true;
36992 /* OpenMP 4.0
36993 #pragma omp declare reduction (reduction-id : typename-list : expression) \
36994 initializer-clause[opt] new-line
36996 initializer-clause:
36997 initializer (omp_priv initializer)
36998 initializer (function-name (argument-list)) */
37000 static void
37001 cp_parser_omp_declare_reduction (cp_parser *parser, cp_token *pragma_tok,
37002 enum pragma_context)
37004 auto_vec<tree> types;
37005 enum tree_code reduc_code = ERROR_MARK;
37006 tree reduc_id = NULL_TREE, orig_reduc_id = NULL_TREE, type;
37007 unsigned int i;
37008 cp_token *first_token;
37009 cp_token_cache *cp;
37010 int errs;
37011 void *p;
37013 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
37014 p = obstack_alloc (&declarator_obstack, 0);
37016 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
37017 goto fail;
37019 switch (cp_lexer_peek_token (parser->lexer)->type)
37021 case CPP_PLUS:
37022 reduc_code = PLUS_EXPR;
37023 break;
37024 case CPP_MULT:
37025 reduc_code = MULT_EXPR;
37026 break;
37027 case CPP_MINUS:
37028 reduc_code = MINUS_EXPR;
37029 break;
37030 case CPP_AND:
37031 reduc_code = BIT_AND_EXPR;
37032 break;
37033 case CPP_XOR:
37034 reduc_code = BIT_XOR_EXPR;
37035 break;
37036 case CPP_OR:
37037 reduc_code = BIT_IOR_EXPR;
37038 break;
37039 case CPP_AND_AND:
37040 reduc_code = TRUTH_ANDIF_EXPR;
37041 break;
37042 case CPP_OR_OR:
37043 reduc_code = TRUTH_ORIF_EXPR;
37044 break;
37045 case CPP_NAME:
37046 reduc_id = orig_reduc_id = cp_parser_identifier (parser);
37047 break;
37048 default:
37049 cp_parser_error (parser, "expected %<+%>, %<*%>, %<-%>, %<&%>, %<^%>, "
37050 "%<|%>, %<&&%>, %<||%> or identifier");
37051 goto fail;
37054 if (reduc_code != ERROR_MARK)
37055 cp_lexer_consume_token (parser->lexer);
37057 reduc_id = omp_reduction_id (reduc_code, reduc_id, NULL_TREE);
37058 if (reduc_id == error_mark_node)
37059 goto fail;
37061 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
37062 goto fail;
37064 /* Types may not be defined in declare reduction type list. */
37065 const char *saved_message;
37066 saved_message = parser->type_definition_forbidden_message;
37067 parser->type_definition_forbidden_message
37068 = G_("types may not be defined in declare reduction type list");
37069 bool saved_colon_corrects_to_scope_p;
37070 saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
37071 parser->colon_corrects_to_scope_p = false;
37072 bool saved_colon_doesnt_start_class_def_p;
37073 saved_colon_doesnt_start_class_def_p
37074 = parser->colon_doesnt_start_class_def_p;
37075 parser->colon_doesnt_start_class_def_p = true;
37077 while (true)
37079 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
37080 type = cp_parser_type_id (parser);
37081 if (type == error_mark_node)
37083 else if (ARITHMETIC_TYPE_P (type)
37084 && (orig_reduc_id == NULL_TREE
37085 || (TREE_CODE (type) != COMPLEX_TYPE
37086 && (strcmp (IDENTIFIER_POINTER (orig_reduc_id),
37087 "min") == 0
37088 || strcmp (IDENTIFIER_POINTER (orig_reduc_id),
37089 "max") == 0))))
37090 error_at (loc, "predeclared arithmetic type %qT in "
37091 "%<#pragma omp declare reduction%>", type);
37092 else if (TREE_CODE (type) == FUNCTION_TYPE
37093 || TREE_CODE (type) == METHOD_TYPE
37094 || TREE_CODE (type) == ARRAY_TYPE)
37095 error_at (loc, "function or array type %qT in "
37096 "%<#pragma omp declare reduction%>", type);
37097 else if (TREE_CODE (type) == REFERENCE_TYPE)
37098 error_at (loc, "reference type %qT in "
37099 "%<#pragma omp declare reduction%>", type);
37100 else if (TYPE_QUALS_NO_ADDR_SPACE (type))
37101 error_at (loc, "const, volatile or __restrict qualified type %qT in "
37102 "%<#pragma omp declare reduction%>", type);
37103 else
37104 types.safe_push (type);
37106 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
37107 cp_lexer_consume_token (parser->lexer);
37108 else
37109 break;
37112 /* Restore the saved message. */
37113 parser->type_definition_forbidden_message = saved_message;
37114 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
37115 parser->colon_doesnt_start_class_def_p
37116 = saved_colon_doesnt_start_class_def_p;
37118 if (!cp_parser_require (parser, CPP_COLON, RT_COLON)
37119 || types.is_empty ())
37121 fail:
37122 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37123 goto done;
37126 first_token = cp_lexer_peek_token (parser->lexer);
37127 cp = NULL;
37128 errs = errorcount;
37129 FOR_EACH_VEC_ELT (types, i, type)
37131 tree fntype
37132 = build_function_type_list (void_type_node,
37133 cp_build_reference_type (type, false),
37134 NULL_TREE);
37135 tree this_reduc_id = reduc_id;
37136 if (!dependent_type_p (type))
37137 this_reduc_id = omp_reduction_id (ERROR_MARK, reduc_id, type);
37138 tree fndecl = build_lang_decl (FUNCTION_DECL, this_reduc_id, fntype);
37139 DECL_SOURCE_LOCATION (fndecl) = pragma_tok->location;
37140 DECL_ARTIFICIAL (fndecl) = 1;
37141 DECL_EXTERNAL (fndecl) = 1;
37142 DECL_DECLARED_INLINE_P (fndecl) = 1;
37143 DECL_IGNORED_P (fndecl) = 1;
37144 DECL_OMP_DECLARE_REDUCTION_P (fndecl) = 1;
37145 SET_DECL_ASSEMBLER_NAME (fndecl, get_identifier ("<udr>"));
37146 DECL_ATTRIBUTES (fndecl)
37147 = tree_cons (get_identifier ("gnu_inline"), NULL_TREE,
37148 DECL_ATTRIBUTES (fndecl));
37149 if (processing_template_decl)
37150 fndecl = push_template_decl (fndecl);
37151 bool block_scope = false;
37152 tree block = NULL_TREE;
37153 if (current_function_decl)
37155 block_scope = true;
37156 DECL_CONTEXT (fndecl) = global_namespace;
37157 if (!processing_template_decl)
37158 pushdecl (fndecl);
37160 else if (current_class_type)
37162 if (cp == NULL)
37164 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
37165 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
37166 cp_lexer_consume_token (parser->lexer);
37167 if (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
37168 goto fail;
37169 cp = cp_token_cache_new (first_token,
37170 cp_lexer_peek_nth_token (parser->lexer,
37171 2));
37173 DECL_STATIC_FUNCTION_P (fndecl) = 1;
37174 finish_member_declaration (fndecl);
37175 DECL_PENDING_INLINE_INFO (fndecl) = cp;
37176 DECL_PENDING_INLINE_P (fndecl) = 1;
37177 vec_safe_push (unparsed_funs_with_definitions, fndecl);
37178 continue;
37180 else
37182 DECL_CONTEXT (fndecl) = current_namespace;
37183 pushdecl (fndecl);
37185 if (!block_scope)
37186 start_preparsed_function (fndecl, NULL_TREE, SF_PRE_PARSED);
37187 else
37188 block = begin_omp_structured_block ();
37189 if (cp)
37191 cp_parser_push_lexer_for_tokens (parser, cp);
37192 parser->lexer->in_pragma = true;
37194 if (!cp_parser_omp_declare_reduction_exprs (fndecl, parser))
37196 if (!block_scope)
37197 finish_function (0);
37198 else
37199 DECL_CONTEXT (fndecl) = current_function_decl;
37200 if (cp)
37201 cp_parser_pop_lexer (parser);
37202 goto fail;
37204 if (cp)
37205 cp_parser_pop_lexer (parser);
37206 if (!block_scope)
37207 finish_function (0);
37208 else
37210 DECL_CONTEXT (fndecl) = current_function_decl;
37211 block = finish_omp_structured_block (block);
37212 if (TREE_CODE (block) == BIND_EXPR)
37213 DECL_SAVED_TREE (fndecl) = BIND_EXPR_BODY (block);
37214 else if (TREE_CODE (block) == STATEMENT_LIST)
37215 DECL_SAVED_TREE (fndecl) = block;
37216 if (processing_template_decl)
37217 add_decl_expr (fndecl);
37219 cp_check_omp_declare_reduction (fndecl);
37220 if (cp == NULL && types.length () > 1)
37221 cp = cp_token_cache_new (first_token,
37222 cp_lexer_peek_nth_token (parser->lexer, 2));
37223 if (errs != errorcount)
37224 break;
37227 cp_parser_require_pragma_eol (parser, pragma_tok);
37229 done:
37230 /* Free any declarators allocated. */
37231 obstack_free (&declarator_obstack, p);
37234 /* OpenMP 4.0
37235 #pragma omp declare simd declare-simd-clauses[optseq] new-line
37236 #pragma omp declare reduction (reduction-id : typename-list : expression) \
37237 initializer-clause[opt] new-line
37238 #pragma omp declare target new-line */
37240 static void
37241 cp_parser_omp_declare (cp_parser *parser, cp_token *pragma_tok,
37242 enum pragma_context context)
37244 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37246 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37247 const char *p = IDENTIFIER_POINTER (id);
37249 if (strcmp (p, "simd") == 0)
37251 cp_lexer_consume_token (parser->lexer);
37252 cp_parser_omp_declare_simd (parser, pragma_tok,
37253 context);
37254 return;
37256 cp_ensure_no_omp_declare_simd (parser);
37257 if (strcmp (p, "reduction") == 0)
37259 cp_lexer_consume_token (parser->lexer);
37260 cp_parser_omp_declare_reduction (parser, pragma_tok,
37261 context);
37262 return;
37264 if (!flag_openmp) /* flag_openmp_simd */
37266 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37267 return;
37269 if (strcmp (p, "target") == 0)
37271 cp_lexer_consume_token (parser->lexer);
37272 cp_parser_omp_declare_target (parser, pragma_tok);
37273 return;
37276 cp_parser_error (parser, "expected %<simd%> or %<reduction%> "
37277 "or %<target%>");
37278 cp_parser_require_pragma_eol (parser, pragma_tok);
37281 /* OpenMP 4.5:
37282 #pragma omp taskloop taskloop-clause[optseq] new-line
37283 for-loop
37285 #pragma omp taskloop simd taskloop-simd-clause[optseq] new-line
37286 for-loop */
37288 #define OMP_TASKLOOP_CLAUSE_MASK \
37289 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
37290 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
37291 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
37292 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
37293 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
37294 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_GRAINSIZE) \
37295 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TASKS) \
37296 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE) \
37297 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED) \
37298 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
37299 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL) \
37300 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE) \
37301 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOGROUP) \
37302 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIORITY))
37304 static tree
37305 cp_parser_omp_taskloop (cp_parser *parser, cp_token *pragma_tok,
37306 char *p_name, omp_clause_mask mask, tree *cclauses,
37307 bool *if_p)
37309 tree clauses, sb, ret;
37310 unsigned int save;
37311 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
37313 strcat (p_name, " taskloop");
37314 mask |= OMP_TASKLOOP_CLAUSE_MASK;
37316 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37318 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37319 const char *p = IDENTIFIER_POINTER (id);
37321 if (strcmp (p, "simd") == 0)
37323 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
37324 if (cclauses == NULL)
37325 cclauses = cclauses_buf;
37327 cp_lexer_consume_token (parser->lexer);
37328 if (!flag_openmp) /* flag_openmp_simd */
37329 return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
37330 cclauses, if_p);
37331 sb = begin_omp_structured_block ();
37332 save = cp_parser_begin_omp_structured_block (parser);
37333 ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
37334 cclauses, if_p);
37335 cp_parser_end_omp_structured_block (parser, save);
37336 tree body = finish_omp_structured_block (sb);
37337 if (ret == NULL)
37338 return ret;
37339 ret = make_node (OMP_TASKLOOP);
37340 TREE_TYPE (ret) = void_type_node;
37341 OMP_FOR_BODY (ret) = body;
37342 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_TASKLOOP];
37343 SET_EXPR_LOCATION (ret, loc);
37344 add_stmt (ret);
37345 return ret;
37348 if (!flag_openmp) /* flag_openmp_simd */
37350 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37351 return NULL_TREE;
37354 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
37355 cclauses == NULL);
37356 if (cclauses)
37358 cp_omp_split_clauses (loc, OMP_TASKLOOP, mask, clauses, cclauses);
37359 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TASKLOOP];
37362 sb = begin_omp_structured_block ();
37363 save = cp_parser_begin_omp_structured_block (parser);
37365 ret = cp_parser_omp_for_loop (parser, OMP_TASKLOOP, clauses, cclauses,
37366 if_p);
37368 cp_parser_end_omp_structured_block (parser, save);
37369 add_stmt (finish_omp_structured_block (sb));
37371 return ret;
37375 /* OpenACC 2.0:
37376 # pragma acc routine oacc-routine-clause[optseq] new-line
37377 function-definition
37379 # pragma acc routine ( name ) oacc-routine-clause[optseq] new-line
37382 #define OACC_ROUTINE_CLAUSE_MASK \
37383 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_GANG) \
37384 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WORKER) \
37385 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR) \
37386 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SEQ))
37389 /* Parse the OpenACC routine pragma. This has an optional '( name )'
37390 component, which must resolve to a declared namespace-scope
37391 function. The clauses are either processed directly (for a named
37392 function), or defered until the immediatley following declaration
37393 is parsed. */
37395 static void
37396 cp_parser_oacc_routine (cp_parser *parser, cp_token *pragma_tok,
37397 enum pragma_context context)
37399 gcc_checking_assert (context == pragma_external);
37400 /* The checking for "another pragma following this one" in the "no optional
37401 '( name )'" case makes sure that we dont re-enter. */
37402 gcc_checking_assert (parser->oacc_routine == NULL);
37404 cp_oacc_routine_data data;
37405 data.error_seen = false;
37406 data.fndecl_seen = false;
37407 data.tokens = vNULL;
37408 data.clauses = NULL_TREE;
37409 data.loc = pragma_tok->location;
37410 /* It is safe to take the address of a local variable; it will only be
37411 used while this scope is live. */
37412 parser->oacc_routine = &data;
37414 /* Look for optional '( name )'. */
37415 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
37417 cp_lexer_consume_token (parser->lexer); /* '(' */
37419 /* We parse the name as an id-expression. If it resolves to
37420 anything other than a non-overloaded function at namespace
37421 scope, it's an error. */
37422 location_t name_loc = cp_lexer_peek_token (parser->lexer)->location;
37423 tree name = cp_parser_id_expression (parser,
37424 /*template_keyword_p=*/false,
37425 /*check_dependency_p=*/false,
37426 /*template_p=*/NULL,
37427 /*declarator_p=*/false,
37428 /*optional_p=*/false);
37429 tree decl = cp_parser_lookup_name_simple (parser, name, name_loc);
37430 if (name != error_mark_node && decl == error_mark_node)
37431 cp_parser_name_lookup_error (parser, name, decl, NLE_NULL, name_loc);
37433 if (decl == error_mark_node
37434 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
37436 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37437 parser->oacc_routine = NULL;
37438 return;
37441 data.clauses
37442 = cp_parser_oacc_all_clauses (parser, OACC_ROUTINE_CLAUSE_MASK,
37443 "#pragma acc routine",
37444 cp_lexer_peek_token (parser->lexer));
37446 if (decl && is_overloaded_fn (decl)
37447 && (TREE_CODE (decl) != FUNCTION_DECL
37448 || DECL_FUNCTION_TEMPLATE_P (decl)))
37450 error_at (name_loc,
37451 "%<#pragma acc routine%> names a set of overloads");
37452 parser->oacc_routine = NULL;
37453 return;
37456 /* Perhaps we should use the same rule as declarations in different
37457 namespaces? */
37458 if (!DECL_NAMESPACE_SCOPE_P (decl))
37460 error_at (name_loc,
37461 "%qD does not refer to a namespace scope function", decl);
37462 parser->oacc_routine = NULL;
37463 return;
37466 if (TREE_CODE (decl) != FUNCTION_DECL)
37468 error_at (name_loc, "%qD does not refer to a function", decl);
37469 parser->oacc_routine = NULL;
37470 return;
37473 cp_finalize_oacc_routine (parser, decl, false);
37474 parser->oacc_routine = NULL;
37476 else /* No optional '( name )'. */
37478 /* Store away all pragma tokens. */
37479 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
37480 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
37481 cp_lexer_consume_token (parser->lexer);
37482 if (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
37483 parser->oacc_routine->error_seen = true;
37484 cp_parser_require_pragma_eol (parser, pragma_tok);
37485 struct cp_token_cache *cp
37486 = cp_token_cache_new (pragma_tok, cp_lexer_peek_token (parser->lexer));
37487 parser->oacc_routine->tokens.safe_push (cp);
37489 /* Emit a helpful diagnostic if there's another pragma following this
37490 one. */
37491 if (cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA))
37493 cp_ensure_no_oacc_routine (parser);
37494 data.tokens.release ();
37495 /* ..., and then just keep going. */
37496 return;
37499 /* We only have to consider the pragma_external case here. */
37500 cp_parser_declaration (parser);
37501 if (parser->oacc_routine
37502 && !parser->oacc_routine->fndecl_seen)
37503 cp_ensure_no_oacc_routine (parser);
37504 else
37505 parser->oacc_routine = NULL;
37506 data.tokens.release ();
37510 /* Finalize #pragma acc routine clauses after direct declarator has
37511 been parsed. */
37513 static tree
37514 cp_parser_late_parsing_oacc_routine (cp_parser *parser, tree attrs)
37516 struct cp_token_cache *ce;
37517 cp_oacc_routine_data *data = parser->oacc_routine;
37519 if (!data->error_seen && data->fndecl_seen)
37521 error_at (data->loc,
37522 "%<#pragma acc routine%> not immediately followed by "
37523 "a single function declaration or definition");
37524 data->error_seen = true;
37526 if (data->error_seen)
37527 return attrs;
37529 gcc_checking_assert (data->tokens.length () == 1);
37530 ce = data->tokens[0];
37532 cp_parser_push_lexer_for_tokens (parser, ce);
37533 parser->lexer->in_pragma = true;
37534 gcc_assert (cp_lexer_peek_token (parser->lexer)->type == CPP_PRAGMA);
37536 cp_token *pragma_tok = cp_lexer_consume_token (parser->lexer);
37537 gcc_checking_assert (parser->oacc_routine->clauses == NULL_TREE);
37538 parser->oacc_routine->clauses
37539 = cp_parser_oacc_all_clauses (parser, OACC_ROUTINE_CLAUSE_MASK,
37540 "#pragma acc routine", pragma_tok);
37541 cp_parser_pop_lexer (parser);
37542 /* Later, cp_finalize_oacc_routine will process the clauses, and then set
37543 fndecl_seen. */
37545 return attrs;
37548 /* Apply any saved OpenACC routine clauses to a just-parsed
37549 declaration. */
37551 static void
37552 cp_finalize_oacc_routine (cp_parser *parser, tree fndecl, bool is_defn)
37554 if (__builtin_expect (parser->oacc_routine != NULL, 0))
37556 /* Keep going if we're in error reporting mode. */
37557 if (parser->oacc_routine->error_seen
37558 || fndecl == error_mark_node)
37559 return;
37561 if (parser->oacc_routine->fndecl_seen)
37563 error_at (parser->oacc_routine->loc,
37564 "%<#pragma acc routine%> not immediately followed by"
37565 " a single function declaration or definition");
37566 parser->oacc_routine = NULL;
37567 return;
37569 if (TREE_CODE (fndecl) != FUNCTION_DECL)
37571 cp_ensure_no_oacc_routine (parser);
37572 return;
37575 if (oacc_get_fn_attrib (fndecl))
37577 error_at (parser->oacc_routine->loc,
37578 "%<#pragma acc routine%> already applied to %qD", fndecl);
37579 parser->oacc_routine = NULL;
37580 return;
37583 if (TREE_USED (fndecl) || (!is_defn && DECL_SAVED_TREE (fndecl)))
37585 error_at (parser->oacc_routine->loc,
37586 "%<#pragma acc routine%> must be applied before %s",
37587 TREE_USED (fndecl) ? "use" : "definition");
37588 parser->oacc_routine = NULL;
37589 return;
37592 /* Process the routine's dimension clauses. */
37593 tree dims = oacc_build_routine_dims (parser->oacc_routine->clauses);
37594 oacc_replace_fn_attrib (fndecl, dims);
37596 /* Add an "omp declare target" attribute. */
37597 DECL_ATTRIBUTES (fndecl)
37598 = tree_cons (get_identifier ("omp declare target"),
37599 NULL_TREE, DECL_ATTRIBUTES (fndecl));
37601 /* Don't unset parser->oacc_routine here: we may still need it to
37602 diagnose wrong usage. But, remember that we've used this "#pragma acc
37603 routine". */
37604 parser->oacc_routine->fndecl_seen = true;
37608 /* Main entry point to OpenMP statement pragmas. */
37610 static void
37611 cp_parser_omp_construct (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
37613 tree stmt;
37614 char p_name[sizeof "#pragma omp teams distribute parallel for simd"];
37615 omp_clause_mask mask (0);
37617 switch (cp_parser_pragma_kind (pragma_tok))
37619 case PRAGMA_OACC_ATOMIC:
37620 cp_parser_omp_atomic (parser, pragma_tok);
37621 return;
37622 case PRAGMA_OACC_CACHE:
37623 stmt = cp_parser_oacc_cache (parser, pragma_tok);
37624 break;
37625 case PRAGMA_OACC_DATA:
37626 stmt = cp_parser_oacc_data (parser, pragma_tok, if_p);
37627 break;
37628 case PRAGMA_OACC_ENTER_DATA:
37629 stmt = cp_parser_oacc_enter_exit_data (parser, pragma_tok, true);
37630 break;
37631 case PRAGMA_OACC_EXIT_DATA:
37632 stmt = cp_parser_oacc_enter_exit_data (parser, pragma_tok, false);
37633 break;
37634 case PRAGMA_OACC_HOST_DATA:
37635 stmt = cp_parser_oacc_host_data (parser, pragma_tok, if_p);
37636 break;
37637 case PRAGMA_OACC_KERNELS:
37638 case PRAGMA_OACC_PARALLEL:
37639 strcpy (p_name, "#pragma acc");
37640 stmt = cp_parser_oacc_kernels_parallel (parser, pragma_tok, p_name,
37641 if_p);
37642 break;
37643 case PRAGMA_OACC_LOOP:
37644 strcpy (p_name, "#pragma acc");
37645 stmt = cp_parser_oacc_loop (parser, pragma_tok, p_name, mask, NULL,
37646 if_p);
37647 break;
37648 case PRAGMA_OACC_UPDATE:
37649 stmt = cp_parser_oacc_update (parser, pragma_tok);
37650 break;
37651 case PRAGMA_OACC_WAIT:
37652 stmt = cp_parser_oacc_wait (parser, pragma_tok);
37653 break;
37654 case PRAGMA_OMP_ATOMIC:
37655 cp_parser_omp_atomic (parser, pragma_tok);
37656 return;
37657 case PRAGMA_OMP_CRITICAL:
37658 stmt = cp_parser_omp_critical (parser, pragma_tok, if_p);
37659 break;
37660 case PRAGMA_OMP_DISTRIBUTE:
37661 strcpy (p_name, "#pragma omp");
37662 stmt = cp_parser_omp_distribute (parser, pragma_tok, p_name, mask, NULL,
37663 if_p);
37664 break;
37665 case PRAGMA_OMP_FOR:
37666 strcpy (p_name, "#pragma omp");
37667 stmt = cp_parser_omp_for (parser, pragma_tok, p_name, mask, NULL,
37668 if_p);
37669 break;
37670 case PRAGMA_OMP_MASTER:
37671 stmt = cp_parser_omp_master (parser, pragma_tok, if_p);
37672 break;
37673 case PRAGMA_OMP_PARALLEL:
37674 strcpy (p_name, "#pragma omp");
37675 stmt = cp_parser_omp_parallel (parser, pragma_tok, p_name, mask, NULL,
37676 if_p);
37677 break;
37678 case PRAGMA_OMP_SECTIONS:
37679 strcpy (p_name, "#pragma omp");
37680 stmt = cp_parser_omp_sections (parser, pragma_tok, p_name, mask, NULL);
37681 break;
37682 case PRAGMA_OMP_SIMD:
37683 strcpy (p_name, "#pragma omp");
37684 stmt = cp_parser_omp_simd (parser, pragma_tok, p_name, mask, NULL,
37685 if_p);
37686 break;
37687 case PRAGMA_OMP_SINGLE:
37688 stmt = cp_parser_omp_single (parser, pragma_tok, if_p);
37689 break;
37690 case PRAGMA_OMP_TASK:
37691 stmt = cp_parser_omp_task (parser, pragma_tok, if_p);
37692 break;
37693 case PRAGMA_OMP_TASKGROUP:
37694 stmt = cp_parser_omp_taskgroup (parser, pragma_tok, if_p);
37695 break;
37696 case PRAGMA_OMP_TASKLOOP:
37697 strcpy (p_name, "#pragma omp");
37698 stmt = cp_parser_omp_taskloop (parser, pragma_tok, p_name, mask, NULL,
37699 if_p);
37700 break;
37701 case PRAGMA_OMP_TEAMS:
37702 strcpy (p_name, "#pragma omp");
37703 stmt = cp_parser_omp_teams (parser, pragma_tok, p_name, mask, NULL,
37704 if_p);
37705 break;
37706 default:
37707 gcc_unreachable ();
37710 protected_set_expr_location (stmt, pragma_tok->location);
37713 /* Transactional Memory parsing routines. */
37715 /* Parse a transaction attribute.
37717 txn-attribute:
37718 attribute
37719 [ [ identifier ] ]
37721 We use this instead of cp_parser_attributes_opt for transactions to avoid
37722 the pedwarn in C++98 mode. */
37724 static tree
37725 cp_parser_txn_attribute_opt (cp_parser *parser)
37727 cp_token *token;
37728 tree attr_name, attr = NULL;
37730 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
37731 return cp_parser_attributes_opt (parser);
37733 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
37734 return NULL_TREE;
37735 cp_lexer_consume_token (parser->lexer);
37736 if (!cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE))
37737 goto error1;
37739 token = cp_lexer_peek_token (parser->lexer);
37740 if (token->type == CPP_NAME || token->type == CPP_KEYWORD)
37742 token = cp_lexer_consume_token (parser->lexer);
37744 attr_name = (token->type == CPP_KEYWORD
37745 /* For keywords, use the canonical spelling,
37746 not the parsed identifier. */
37747 ? ridpointers[(int) token->keyword]
37748 : token->u.value);
37749 attr = build_tree_list (attr_name, NULL_TREE);
37751 else
37752 cp_parser_error (parser, "expected identifier");
37754 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
37755 error1:
37756 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
37757 return attr;
37760 /* Parse a __transaction_atomic or __transaction_relaxed statement.
37762 transaction-statement:
37763 __transaction_atomic txn-attribute[opt] txn-noexcept-spec[opt]
37764 compound-statement
37765 __transaction_relaxed txn-noexcept-spec[opt] compound-statement
37768 static tree
37769 cp_parser_transaction (cp_parser *parser, cp_token *token)
37771 unsigned char old_in = parser->in_transaction;
37772 unsigned char this_in = 1, new_in;
37773 enum rid keyword = token->keyword;
37774 tree stmt, attrs, noex;
37776 cp_lexer_consume_token (parser->lexer);
37778 if (keyword == RID_TRANSACTION_RELAXED
37779 || keyword == RID_SYNCHRONIZED)
37780 this_in |= TM_STMT_ATTR_RELAXED;
37781 else
37783 attrs = cp_parser_txn_attribute_opt (parser);
37784 if (attrs)
37785 this_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
37788 /* Parse a noexcept specification. */
37789 if (keyword == RID_ATOMIC_NOEXCEPT)
37790 noex = boolean_true_node;
37791 else if (keyword == RID_ATOMIC_CANCEL)
37793 /* cancel-and-throw is unimplemented. */
37794 sorry ("atomic_cancel");
37795 noex = NULL_TREE;
37797 else
37798 noex = cp_parser_noexcept_specification_opt (parser, true, NULL, true);
37800 /* Keep track if we're in the lexical scope of an outer transaction. */
37801 new_in = this_in | (old_in & TM_STMT_ATTR_OUTER);
37803 stmt = begin_transaction_stmt (token->location, NULL, this_in);
37805 parser->in_transaction = new_in;
37806 cp_parser_compound_statement (parser, NULL, BCS_TRANSACTION, false);
37807 parser->in_transaction = old_in;
37809 finish_transaction_stmt (stmt, NULL, this_in, noex);
37811 return stmt;
37814 /* Parse a __transaction_atomic or __transaction_relaxed expression.
37816 transaction-expression:
37817 __transaction_atomic txn-noexcept-spec[opt] ( expression )
37818 __transaction_relaxed txn-noexcept-spec[opt] ( expression )
37821 static tree
37822 cp_parser_transaction_expression (cp_parser *parser, enum rid keyword)
37824 unsigned char old_in = parser->in_transaction;
37825 unsigned char this_in = 1;
37826 cp_token *token;
37827 tree expr, noex;
37828 bool noex_expr;
37829 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
37831 gcc_assert (keyword == RID_TRANSACTION_ATOMIC
37832 || keyword == RID_TRANSACTION_RELAXED);
37834 if (!flag_tm)
37835 error_at (loc,
37836 keyword == RID_TRANSACTION_RELAXED
37837 ? G_("%<__transaction_relaxed%> without transactional memory "
37838 "support enabled")
37839 : G_("%<__transaction_atomic%> without transactional memory "
37840 "support enabled"));
37842 token = cp_parser_require_keyword (parser, keyword,
37843 (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
37844 : RT_TRANSACTION_RELAXED));
37845 gcc_assert (token != NULL);
37847 if (keyword == RID_TRANSACTION_RELAXED)
37848 this_in |= TM_STMT_ATTR_RELAXED;
37850 /* Set this early. This might mean that we allow transaction_cancel in
37851 an expression that we find out later actually has to be a constexpr.
37852 However, we expect that cxx_constant_value will be able to deal with
37853 this; also, if the noexcept has no constexpr, then what we parse next
37854 really is a transaction's body. */
37855 parser->in_transaction = this_in;
37857 /* Parse a noexcept specification. */
37858 noex = cp_parser_noexcept_specification_opt (parser, false, &noex_expr,
37859 true);
37861 if (!noex || !noex_expr
37862 || cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
37864 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
37866 expr = cp_parser_expression (parser);
37867 expr = finish_parenthesized_expr (expr);
37869 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
37871 else
37873 /* The only expression that is available got parsed for the noexcept
37874 already. noexcept is true then. */
37875 expr = noex;
37876 noex = boolean_true_node;
37879 expr = build_transaction_expr (token->location, expr, this_in, noex);
37880 parser->in_transaction = old_in;
37882 if (cp_parser_non_integral_constant_expression (parser, NIC_TRANSACTION))
37883 return error_mark_node;
37885 return (flag_tm ? expr : error_mark_node);
37888 /* Parse a function-transaction-block.
37890 function-transaction-block:
37891 __transaction_atomic txn-attribute[opt] ctor-initializer[opt]
37892 function-body
37893 __transaction_atomic txn-attribute[opt] function-try-block
37894 __transaction_relaxed ctor-initializer[opt] function-body
37895 __transaction_relaxed function-try-block
37898 static bool
37899 cp_parser_function_transaction (cp_parser *parser, enum rid keyword)
37901 unsigned char old_in = parser->in_transaction;
37902 unsigned char new_in = 1;
37903 tree compound_stmt, stmt, attrs;
37904 bool ctor_initializer_p;
37905 cp_token *token;
37907 gcc_assert (keyword == RID_TRANSACTION_ATOMIC
37908 || keyword == RID_TRANSACTION_RELAXED);
37909 token = cp_parser_require_keyword (parser, keyword,
37910 (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
37911 : RT_TRANSACTION_RELAXED));
37912 gcc_assert (token != NULL);
37914 if (keyword == RID_TRANSACTION_RELAXED)
37915 new_in |= TM_STMT_ATTR_RELAXED;
37916 else
37918 attrs = cp_parser_txn_attribute_opt (parser);
37919 if (attrs)
37920 new_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
37923 stmt = begin_transaction_stmt (token->location, &compound_stmt, new_in);
37925 parser->in_transaction = new_in;
37927 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
37928 ctor_initializer_p = cp_parser_function_try_block (parser);
37929 else
37930 ctor_initializer_p = cp_parser_ctor_initializer_opt_and_function_body
37931 (parser, /*in_function_try_block=*/false);
37933 parser->in_transaction = old_in;
37935 finish_transaction_stmt (stmt, compound_stmt, new_in, NULL_TREE);
37937 return ctor_initializer_p;
37940 /* Parse a __transaction_cancel statement.
37942 cancel-statement:
37943 __transaction_cancel txn-attribute[opt] ;
37944 __transaction_cancel txn-attribute[opt] throw-expression ;
37946 ??? Cancel and throw is not yet implemented. */
37948 static tree
37949 cp_parser_transaction_cancel (cp_parser *parser)
37951 cp_token *token;
37952 bool is_outer = false;
37953 tree stmt, attrs;
37955 token = cp_parser_require_keyword (parser, RID_TRANSACTION_CANCEL,
37956 RT_TRANSACTION_CANCEL);
37957 gcc_assert (token != NULL);
37959 attrs = cp_parser_txn_attribute_opt (parser);
37960 if (attrs)
37961 is_outer = (parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER) != 0);
37963 /* ??? Parse cancel-and-throw here. */
37965 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
37967 if (!flag_tm)
37969 error_at (token->location, "%<__transaction_cancel%> without "
37970 "transactional memory support enabled");
37971 return error_mark_node;
37973 else if (parser->in_transaction & TM_STMT_ATTR_RELAXED)
37975 error_at (token->location, "%<__transaction_cancel%> within a "
37976 "%<__transaction_relaxed%>");
37977 return error_mark_node;
37979 else if (is_outer)
37981 if ((parser->in_transaction & TM_STMT_ATTR_OUTER) == 0
37982 && !is_tm_may_cancel_outer (current_function_decl))
37984 error_at (token->location, "outer %<__transaction_cancel%> not "
37985 "within outer %<__transaction_atomic%>");
37986 error_at (token->location,
37987 " or a %<transaction_may_cancel_outer%> function");
37988 return error_mark_node;
37991 else if (parser->in_transaction == 0)
37993 error_at (token->location, "%<__transaction_cancel%> not within "
37994 "%<__transaction_atomic%>");
37995 return error_mark_node;
37998 stmt = build_tm_abort_call (token->location, is_outer);
37999 add_stmt (stmt);
38001 return stmt;
38004 /* The parser. */
38006 static GTY (()) cp_parser *the_parser;
38009 /* Special handling for the first token or line in the file. The first
38010 thing in the file might be #pragma GCC pch_preprocess, which loads a
38011 PCH file, which is a GC collection point. So we need to handle this
38012 first pragma without benefit of an existing lexer structure.
38014 Always returns one token to the caller in *FIRST_TOKEN. This is
38015 either the true first token of the file, or the first token after
38016 the initial pragma. */
38018 static void
38019 cp_parser_initial_pragma (cp_token *first_token)
38021 tree name = NULL;
38023 cp_lexer_get_preprocessor_token (NULL, first_token);
38024 if (cp_parser_pragma_kind (first_token) != PRAGMA_GCC_PCH_PREPROCESS)
38025 return;
38027 cp_lexer_get_preprocessor_token (NULL, first_token);
38028 if (first_token->type == CPP_STRING)
38030 name = first_token->u.value;
38032 cp_lexer_get_preprocessor_token (NULL, first_token);
38033 if (first_token->type != CPP_PRAGMA_EOL)
38034 error_at (first_token->location,
38035 "junk at end of %<#pragma GCC pch_preprocess%>");
38037 else
38038 error_at (first_token->location, "expected string literal");
38040 /* Skip to the end of the pragma. */
38041 while (first_token->type != CPP_PRAGMA_EOL && first_token->type != CPP_EOF)
38042 cp_lexer_get_preprocessor_token (NULL, first_token);
38044 /* Now actually load the PCH file. */
38045 if (name)
38046 c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
38048 /* Read one more token to return to our caller. We have to do this
38049 after reading the PCH file in, since its pointers have to be
38050 live. */
38051 cp_lexer_get_preprocessor_token (NULL, first_token);
38054 /* Parses the grainsize pragma for the _Cilk_for statement.
38055 Syntax:
38056 #pragma cilk grainsize = <VALUE>. */
38058 static void
38059 cp_parser_cilk_grainsize (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
38061 if (cp_parser_require (parser, CPP_EQ, RT_EQ))
38063 tree exp = cp_parser_binary_expression (parser, false, false,
38064 PREC_NOT_OPERATOR, NULL);
38065 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
38066 if (!exp || exp == error_mark_node)
38068 error_at (pragma_tok->location, "invalid grainsize for _Cilk_for");
38069 return;
38072 /* Make sure the next token is _Cilk_for, it is invalid otherwise. */
38073 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CILK_FOR))
38074 cp_parser_cilk_for (parser, exp, if_p);
38075 else
38076 warning_at (cp_lexer_peek_token (parser->lexer)->location, 0,
38077 "%<#pragma cilk grainsize%> is not followed by "
38078 "%<_Cilk_for%>");
38079 return;
38081 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
38084 /* Normal parsing of a pragma token. Here we can (and must) use the
38085 regular lexer. */
38087 static bool
38088 cp_parser_pragma (cp_parser *parser, enum pragma_context context, bool *if_p)
38090 cp_token *pragma_tok;
38091 unsigned int id;
38092 tree stmt;
38093 bool ret;
38095 pragma_tok = cp_lexer_consume_token (parser->lexer);
38096 gcc_assert (pragma_tok->type == CPP_PRAGMA);
38097 parser->lexer->in_pragma = true;
38099 id = cp_parser_pragma_kind (pragma_tok);
38100 if (id != PRAGMA_OMP_DECLARE && id != PRAGMA_OACC_ROUTINE)
38101 cp_ensure_no_omp_declare_simd (parser);
38102 switch (id)
38104 case PRAGMA_GCC_PCH_PREPROCESS:
38105 error_at (pragma_tok->location,
38106 "%<#pragma GCC pch_preprocess%> must be first");
38107 break;
38109 case PRAGMA_OMP_BARRIER:
38110 switch (context)
38112 case pragma_compound:
38113 cp_parser_omp_barrier (parser, pragma_tok);
38114 return false;
38115 case pragma_stmt:
38116 error_at (pragma_tok->location, "%<#pragma omp barrier%> may only be "
38117 "used in compound statements");
38118 break;
38119 default:
38120 goto bad_stmt;
38122 break;
38124 case PRAGMA_OMP_FLUSH:
38125 switch (context)
38127 case pragma_compound:
38128 cp_parser_omp_flush (parser, pragma_tok);
38129 return false;
38130 case pragma_stmt:
38131 error_at (pragma_tok->location, "%<#pragma omp flush%> may only be "
38132 "used in compound statements");
38133 break;
38134 default:
38135 goto bad_stmt;
38137 break;
38139 case PRAGMA_OMP_TASKWAIT:
38140 switch (context)
38142 case pragma_compound:
38143 cp_parser_omp_taskwait (parser, pragma_tok);
38144 return false;
38145 case pragma_stmt:
38146 error_at (pragma_tok->location,
38147 "%<#pragma omp taskwait%> may only be "
38148 "used in compound statements");
38149 break;
38150 default:
38151 goto bad_stmt;
38153 break;
38155 case PRAGMA_OMP_TASKYIELD:
38156 switch (context)
38158 case pragma_compound:
38159 cp_parser_omp_taskyield (parser, pragma_tok);
38160 return false;
38161 case pragma_stmt:
38162 error_at (pragma_tok->location,
38163 "%<#pragma omp taskyield%> may only be "
38164 "used in compound statements");
38165 break;
38166 default:
38167 goto bad_stmt;
38169 break;
38171 case PRAGMA_OMP_CANCEL:
38172 switch (context)
38174 case pragma_compound:
38175 cp_parser_omp_cancel (parser, pragma_tok);
38176 return false;
38177 case pragma_stmt:
38178 error_at (pragma_tok->location,
38179 "%<#pragma omp cancel%> may only be "
38180 "used in compound statements");
38181 break;
38182 default:
38183 goto bad_stmt;
38185 break;
38187 case PRAGMA_OMP_CANCELLATION_POINT:
38188 cp_parser_omp_cancellation_point (parser, pragma_tok, context);
38189 return false;
38191 case PRAGMA_OMP_THREADPRIVATE:
38192 cp_parser_omp_threadprivate (parser, pragma_tok);
38193 return false;
38195 case PRAGMA_OMP_DECLARE:
38196 cp_parser_omp_declare (parser, pragma_tok, context);
38197 return false;
38199 case PRAGMA_OACC_DECLARE:
38200 cp_parser_oacc_declare (parser, pragma_tok);
38201 return false;
38203 case PRAGMA_OACC_ENTER_DATA:
38204 if (context == pragma_stmt)
38206 cp_parser_error (parser, "%<#pragma acc enter data%> may only be "
38207 "used in compound statements");
38208 break;
38210 else if (context != pragma_compound)
38211 goto bad_stmt;
38212 cp_parser_omp_construct (parser, pragma_tok, if_p);
38213 return true;
38215 case PRAGMA_OACC_EXIT_DATA:
38216 if (context == pragma_stmt)
38218 cp_parser_error (parser, "%<#pragma acc exit data%> may only be "
38219 "used in compound statements");
38220 break;
38222 else if (context != pragma_compound)
38223 goto bad_stmt;
38224 cp_parser_omp_construct (parser, pragma_tok, if_p);
38225 return true;
38227 case PRAGMA_OACC_ROUTINE:
38228 if (context != pragma_external)
38230 error_at (pragma_tok->location,
38231 "%<#pragma acc routine%> must be at file scope");
38232 break;
38234 cp_parser_oacc_routine (parser, pragma_tok, context);
38235 return false;
38237 case PRAGMA_OACC_UPDATE:
38238 if (context == pragma_stmt)
38240 cp_parser_error (parser, "%<#pragma acc update%> may only be "
38241 "used in compound statements");
38242 break;
38244 else if (context != pragma_compound)
38245 goto bad_stmt;
38246 cp_parser_omp_construct (parser, pragma_tok, if_p);
38247 return true;
38249 case PRAGMA_OACC_WAIT:
38250 if (context == pragma_stmt)
38252 cp_parser_error (parser, "%<#pragma acc wait%> may only be "
38253 "used in compound statements");
38254 break;
38256 else if (context != pragma_compound)
38257 goto bad_stmt;
38258 cp_parser_omp_construct (parser, pragma_tok, if_p);
38259 return true;
38261 case PRAGMA_OACC_ATOMIC:
38262 case PRAGMA_OACC_CACHE:
38263 case PRAGMA_OACC_DATA:
38264 case PRAGMA_OACC_HOST_DATA:
38265 case PRAGMA_OACC_KERNELS:
38266 case PRAGMA_OACC_PARALLEL:
38267 case PRAGMA_OACC_LOOP:
38268 case PRAGMA_OMP_ATOMIC:
38269 case PRAGMA_OMP_CRITICAL:
38270 case PRAGMA_OMP_DISTRIBUTE:
38271 case PRAGMA_OMP_FOR:
38272 case PRAGMA_OMP_MASTER:
38273 case PRAGMA_OMP_PARALLEL:
38274 case PRAGMA_OMP_SECTIONS:
38275 case PRAGMA_OMP_SIMD:
38276 case PRAGMA_OMP_SINGLE:
38277 case PRAGMA_OMP_TASK:
38278 case PRAGMA_OMP_TASKGROUP:
38279 case PRAGMA_OMP_TASKLOOP:
38280 case PRAGMA_OMP_TEAMS:
38281 if (context != pragma_stmt && context != pragma_compound)
38282 goto bad_stmt;
38283 stmt = push_omp_privatization_clauses (false);
38284 cp_parser_omp_construct (parser, pragma_tok, if_p);
38285 pop_omp_privatization_clauses (stmt);
38286 return true;
38288 case PRAGMA_OMP_ORDERED:
38289 if (context != pragma_stmt && context != pragma_compound)
38290 goto bad_stmt;
38291 stmt = push_omp_privatization_clauses (false);
38292 ret = cp_parser_omp_ordered (parser, pragma_tok, context, if_p);
38293 pop_omp_privatization_clauses (stmt);
38294 return ret;
38296 case PRAGMA_OMP_TARGET:
38297 if (context != pragma_stmt && context != pragma_compound)
38298 goto bad_stmt;
38299 stmt = push_omp_privatization_clauses (false);
38300 ret = cp_parser_omp_target (parser, pragma_tok, context, if_p);
38301 pop_omp_privatization_clauses (stmt);
38302 return ret;
38304 case PRAGMA_OMP_END_DECLARE_TARGET:
38305 cp_parser_omp_end_declare_target (parser, pragma_tok);
38306 return false;
38308 case PRAGMA_OMP_SECTION:
38309 error_at (pragma_tok->location,
38310 "%<#pragma omp section%> may only be used in "
38311 "%<#pragma omp sections%> construct");
38312 break;
38314 case PRAGMA_IVDEP:
38316 if (context == pragma_external)
38318 error_at (pragma_tok->location,
38319 "%<#pragma GCC ivdep%> must be inside a function");
38320 break;
38322 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
38323 cp_token *tok;
38324 tok = cp_lexer_peek_token (the_parser->lexer);
38325 if (tok->type != CPP_KEYWORD
38326 || (tok->keyword != RID_FOR && tok->keyword != RID_WHILE
38327 && tok->keyword != RID_DO))
38329 cp_parser_error (parser, "for, while or do statement expected");
38330 return false;
38332 cp_parser_iteration_statement (parser, if_p, true);
38333 return true;
38336 case PRAGMA_CILK_SIMD:
38337 if (context == pragma_external)
38339 error_at (pragma_tok->location,
38340 "%<#pragma simd%> must be inside a function");
38341 break;
38343 stmt = push_omp_privatization_clauses (false);
38344 cp_parser_cilk_simd (parser, pragma_tok, if_p);
38345 pop_omp_privatization_clauses (stmt);
38346 return true;
38348 case PRAGMA_CILK_GRAINSIZE:
38349 if (context == pragma_external)
38351 error_at (pragma_tok->location,
38352 "%<#pragma cilk grainsize%> must be inside a function");
38353 break;
38356 /* Ignore the pragma if Cilk Plus is not enabled. */
38357 if (flag_cilkplus)
38359 cp_parser_cilk_grainsize (parser, pragma_tok, if_p);
38360 return true;
38362 else
38364 error_at (pragma_tok->location, "-fcilkplus must be enabled to use "
38365 "%<#pragma cilk grainsize%>");
38366 break;
38369 default:
38370 gcc_assert (id >= PRAGMA_FIRST_EXTERNAL);
38371 c_invoke_pragma_handler (id);
38372 break;
38374 bad_stmt:
38375 cp_parser_error (parser, "expected declaration specifiers");
38376 break;
38379 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
38380 return false;
38383 /* The interface the pragma parsers have to the lexer. */
38385 enum cpp_ttype
38386 pragma_lex (tree *value, location_t *loc)
38388 cp_token *tok = cp_lexer_peek_token (the_parser->lexer);
38389 enum cpp_ttype ret = tok->type;
38391 *value = tok->u.value;
38392 if (loc)
38393 *loc = tok->location;
38395 if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
38396 ret = CPP_EOF;
38397 else if (ret == CPP_STRING)
38398 *value = cp_parser_string_literal (the_parser, false, false);
38399 else
38401 if (ret == CPP_KEYWORD)
38402 ret = CPP_NAME;
38403 cp_lexer_consume_token (the_parser->lexer);
38406 return ret;
38410 /* External interface. */
38412 /* Parse one entire translation unit. */
38414 void
38415 c_parse_file (void)
38417 static bool already_called = false;
38419 if (already_called)
38420 fatal_error (input_location,
38421 "inter-module optimizations not implemented for C++");
38422 already_called = true;
38424 the_parser = cp_parser_new ();
38425 push_deferring_access_checks (flag_access_control
38426 ? dk_no_deferred : dk_no_check);
38427 cp_parser_translation_unit (the_parser);
38428 the_parser = NULL;
38431 /* Parses the Cilk Plus #pragma simd and SIMD-enabled function attribute's
38432 vectorlength clause:
38433 Syntax:
38434 vectorlength ( constant-expression ) */
38436 static tree
38437 cp_parser_cilk_simd_vectorlength (cp_parser *parser, tree clauses,
38438 bool is_simd_fn)
38440 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
38441 tree expr;
38442 /* The vectorlength clause in #pragma simd behaves exactly like OpenMP's
38443 safelen clause. Thus, vectorlength is represented as OMP 4.0
38444 safelen. For SIMD-enabled function it is represented by OMP 4.0
38445 simdlen. */
38446 if (!is_simd_fn)
38447 check_no_duplicate_clause (clauses, OMP_CLAUSE_SAFELEN, "vectorlength",
38448 loc);
38449 else
38450 check_no_duplicate_clause (clauses, OMP_CLAUSE_SIMDLEN, "vectorlength",
38451 loc);
38453 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
38454 return error_mark_node;
38456 expr = cp_parser_constant_expression (parser);
38457 expr = maybe_constant_value (expr);
38459 /* If expr == error_mark_node, then don't emit any errors nor
38460 create a clause. if any of the above functions returns
38461 error mark node then they would have emitted an error message. */
38462 if (expr == error_mark_node)
38464 else if (!TREE_TYPE (expr)
38465 || !TREE_CONSTANT (expr)
38466 || !INTEGRAL_TYPE_P (TREE_TYPE (expr)))
38467 error_at (loc, "vectorlength must be an integer constant");
38468 else if (TREE_CONSTANT (expr)
38469 && !pow2p_hwi (TREE_INT_CST_LOW (expr)))
38470 error_at (loc, "vectorlength must be a power of 2");
38471 else
38473 tree c;
38474 if (!is_simd_fn)
38476 c = build_omp_clause (loc, OMP_CLAUSE_SAFELEN);
38477 OMP_CLAUSE_SAFELEN_EXPR (c) = expr;
38478 OMP_CLAUSE_CHAIN (c) = clauses;
38479 clauses = c;
38481 else
38483 c = build_omp_clause (loc, OMP_CLAUSE_SIMDLEN);
38484 OMP_CLAUSE_SIMDLEN_EXPR (c) = expr;
38485 OMP_CLAUSE_CHAIN (c) = clauses;
38486 clauses = c;
38490 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
38491 return error_mark_node;
38492 return clauses;
38495 /* Handles the Cilk Plus #pragma simd linear clause.
38496 Syntax:
38497 linear ( simd-linear-variable-list )
38499 simd-linear-variable-list:
38500 simd-linear-variable
38501 simd-linear-variable-list , simd-linear-variable
38503 simd-linear-variable:
38504 id-expression
38505 id-expression : simd-linear-step
38507 simd-linear-step:
38508 conditional-expression */
38510 static tree
38511 cp_parser_cilk_simd_linear (cp_parser *parser, tree clauses)
38513 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
38515 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
38516 return clauses;
38517 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
38519 cp_parser_error (parser, "expected identifier");
38520 cp_parser_skip_to_closing_parenthesis (parser, false, false, true);
38521 return error_mark_node;
38524 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
38525 parser->colon_corrects_to_scope_p = false;
38526 while (1)
38528 cp_token *token = cp_lexer_peek_token (parser->lexer);
38529 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
38531 cp_parser_error (parser, "expected variable-name");
38532 clauses = error_mark_node;
38533 break;
38536 tree var_name = cp_parser_id_expression (parser, false, true, NULL,
38537 false, false);
38538 tree decl = cp_parser_lookup_name_simple (parser, var_name,
38539 token->location);
38540 if (decl == error_mark_node)
38542 cp_parser_name_lookup_error (parser, var_name, decl, NLE_NULL,
38543 token->location);
38544 clauses = error_mark_node;
38546 else
38548 tree e = NULL_TREE;
38549 tree step_size = integer_one_node;
38551 /* If present, parse the linear step. Otherwise, assume the default
38552 value of 1. */
38553 if (cp_lexer_peek_token (parser->lexer)->type == CPP_COLON)
38555 cp_lexer_consume_token (parser->lexer);
38557 e = cp_parser_assignment_expression (parser);
38558 e = maybe_constant_value (e);
38560 if (e == error_mark_node)
38562 /* If an error has occurred, then the whole pragma is
38563 considered ill-formed. Thus, no reason to keep
38564 parsing. */
38565 clauses = error_mark_node;
38566 break;
38568 else if (type_dependent_expression_p (e)
38569 || value_dependent_expression_p (e)
38570 || (TREE_TYPE (e)
38571 && INTEGRAL_TYPE_P (TREE_TYPE (e))
38572 && (TREE_CONSTANT (e)
38573 || DECL_P (e))))
38574 step_size = e;
38575 else
38576 cp_parser_error (parser,
38577 "step size must be an integer constant "
38578 "expression or an integer variable");
38581 /* Use the OMP_CLAUSE_LINEAR, which has the same semantics. */
38582 tree l = build_omp_clause (loc, OMP_CLAUSE_LINEAR);
38583 OMP_CLAUSE_DECL (l) = decl;
38584 OMP_CLAUSE_LINEAR_STEP (l) = step_size;
38585 OMP_CLAUSE_CHAIN (l) = clauses;
38586 clauses = l;
38588 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
38589 cp_lexer_consume_token (parser->lexer);
38590 else if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
38591 break;
38592 else
38594 error_at (cp_lexer_peek_token (parser->lexer)->location,
38595 "expected %<,%> or %<)%> after %qE", decl);
38596 clauses = error_mark_node;
38597 break;
38600 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
38601 cp_parser_skip_to_closing_parenthesis (parser, false, false, true);
38602 return clauses;
38605 /* Returns the name of the next clause. If the clause is not
38606 recognized, then PRAGMA_CILK_CLAUSE_NONE is returned and the next
38607 token is not consumed. Otherwise, the appropriate enum from the
38608 pragma_simd_clause is returned and the token is consumed. */
38610 static pragma_omp_clause
38611 cp_parser_cilk_simd_clause_name (cp_parser *parser)
38613 pragma_omp_clause clause_type;
38614 cp_token *token = cp_lexer_peek_token (parser->lexer);
38616 if (token->keyword == RID_PRIVATE)
38617 clause_type = PRAGMA_CILK_CLAUSE_PRIVATE;
38618 else if (!token->u.value || token->type != CPP_NAME)
38619 return PRAGMA_CILK_CLAUSE_NONE;
38620 else if (!strcmp (IDENTIFIER_POINTER (token->u.value), "vectorlength"))
38621 clause_type = PRAGMA_CILK_CLAUSE_VECTORLENGTH;
38622 else if (!strcmp (IDENTIFIER_POINTER (token->u.value), "linear"))
38623 clause_type = PRAGMA_CILK_CLAUSE_LINEAR;
38624 else if (!strcmp (IDENTIFIER_POINTER (token->u.value), "firstprivate"))
38625 clause_type = PRAGMA_CILK_CLAUSE_FIRSTPRIVATE;
38626 else if (!strcmp (IDENTIFIER_POINTER (token->u.value), "lastprivate"))
38627 clause_type = PRAGMA_CILK_CLAUSE_LASTPRIVATE;
38628 else if (!strcmp (IDENTIFIER_POINTER (token->u.value), "reduction"))
38629 clause_type = PRAGMA_CILK_CLAUSE_REDUCTION;
38630 else
38631 return PRAGMA_CILK_CLAUSE_NONE;
38633 cp_lexer_consume_token (parser->lexer);
38634 return clause_type;
38637 /* Parses all the #pragma simd clauses. Returns a list of clauses found. */
38639 static tree
38640 cp_parser_cilk_simd_all_clauses (cp_parser *parser, cp_token *pragma_token)
38642 tree clauses = NULL_TREE;
38644 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
38645 && clauses != error_mark_node)
38647 pragma_omp_clause c_kind;
38648 c_kind = cp_parser_cilk_simd_clause_name (parser);
38649 if (c_kind == PRAGMA_CILK_CLAUSE_VECTORLENGTH)
38650 clauses = cp_parser_cilk_simd_vectorlength (parser, clauses, false);
38651 else if (c_kind == PRAGMA_CILK_CLAUSE_LINEAR)
38652 clauses = cp_parser_cilk_simd_linear (parser, clauses);
38653 else if (c_kind == PRAGMA_CILK_CLAUSE_PRIVATE)
38654 /* Use the OpenMP 4.0 equivalent function. */
38655 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE, clauses);
38656 else if (c_kind == PRAGMA_CILK_CLAUSE_FIRSTPRIVATE)
38657 /* Use the OpenMP 4.0 equivalent function. */
38658 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
38659 clauses);
38660 else if (c_kind == PRAGMA_CILK_CLAUSE_LASTPRIVATE)
38661 /* Use the OMP 4.0 equivalent function. */
38662 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LASTPRIVATE,
38663 clauses);
38664 else if (c_kind == PRAGMA_CILK_CLAUSE_REDUCTION)
38665 /* Use the OMP 4.0 equivalent function. */
38666 clauses = cp_parser_omp_clause_reduction (parser, clauses);
38667 else
38669 clauses = error_mark_node;
38670 cp_parser_error (parser, "expected %<#pragma simd%> clause");
38671 break;
38675 cp_parser_skip_to_pragma_eol (parser, pragma_token);
38677 if (clauses == error_mark_node)
38678 return error_mark_node;
38679 else
38680 return finish_omp_clauses (clauses, C_ORT_CILK);
38683 /* Main entry-point for parsing Cilk Plus <#pragma simd> for loops. */
38685 static void
38686 cp_parser_cilk_simd (cp_parser *parser, cp_token *pragma_token, bool *if_p)
38688 tree clauses = cp_parser_cilk_simd_all_clauses (parser, pragma_token);
38690 if (clauses == error_mark_node)
38691 return;
38693 if (cp_lexer_next_token_is_not_keyword (parser->lexer, RID_FOR))
38695 error_at (cp_lexer_peek_token (parser->lexer)->location,
38696 "for statement expected");
38697 return;
38700 tree sb = begin_omp_structured_block ();
38701 int save = cp_parser_begin_omp_structured_block (parser);
38702 tree ret = cp_parser_omp_for_loop (parser, CILK_SIMD, clauses, NULL, if_p);
38703 if (ret)
38704 cpp_validate_cilk_plus_loop (OMP_FOR_BODY (ret));
38705 cp_parser_end_omp_structured_block (parser, save);
38706 add_stmt (finish_omp_structured_block (sb));
38709 /* Main entry-point for parsing Cilk Plus _Cilk_for
38710 loops. The return value is error_mark_node
38711 when errors happen and CILK_FOR tree on success. */
38713 static tree
38714 cp_parser_cilk_for (cp_parser *parser, tree grain, bool *if_p)
38716 if (cp_lexer_next_token_is_not_keyword (parser->lexer, RID_CILK_FOR))
38717 gcc_unreachable ();
38719 tree sb = begin_omp_structured_block ();
38720 int save = cp_parser_begin_omp_structured_block (parser);
38722 tree clauses = build_omp_clause (EXPR_LOCATION (grain), OMP_CLAUSE_SCHEDULE);
38723 OMP_CLAUSE_SCHEDULE_KIND (clauses) = OMP_CLAUSE_SCHEDULE_CILKFOR;
38724 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (clauses) = grain;
38725 clauses = finish_omp_clauses (clauses, C_ORT_CILK);
38727 tree ret = cp_parser_omp_for_loop (parser, CILK_FOR, clauses, NULL, if_p);
38728 if (ret)
38729 cpp_validate_cilk_plus_loop (ret);
38730 else
38731 ret = error_mark_node;
38733 cp_parser_end_omp_structured_block (parser, save);
38734 add_stmt (finish_omp_structured_block (sb));
38735 return ret;
38738 /* Create an identifier for a generic parameter type (a synthesized
38739 template parameter implied by `auto' or a concept identifier). */
38741 static GTY(()) int generic_parm_count;
38742 static tree
38743 make_generic_type_name ()
38745 char buf[32];
38746 sprintf (buf, "auto:%d", ++generic_parm_count);
38747 return get_identifier (buf);
38750 /* Predicate that behaves as is_auto_or_concept but matches the parent
38751 node of the generic type rather than the generic type itself. This
38752 allows for type transformation in add_implicit_template_parms. */
38754 static inline bool
38755 tree_type_is_auto_or_concept (const_tree t)
38757 return TREE_TYPE (t) && is_auto_or_concept (TREE_TYPE (t));
38760 /* Add an implicit template type parameter to the CURRENT_TEMPLATE_PARMS
38761 (creating a new template parameter list if necessary). Returns the newly
38762 created template type parm. */
38764 static tree
38765 synthesize_implicit_template_parm (cp_parser *parser, tree constr)
38767 gcc_assert (current_binding_level->kind == sk_function_parms);
38769 /* Before committing to modifying any scope, if we're in an
38770 implicit template scope, and we're trying to synthesize a
38771 constrained parameter, try to find a previous parameter with
38772 the same name. This is the same-type rule for abbreviated
38773 function templates.
38775 NOTE: We can generate implicit parameters when tentatively
38776 parsing a nested name specifier, only to reject that parse
38777 later. However, matching the same template-id as part of a
38778 direct-declarator should generate an identical template
38779 parameter, so this rule will merge them. */
38780 if (parser->implicit_template_scope && constr)
38782 tree t = parser->implicit_template_parms;
38783 while (t)
38785 if (equivalent_placeholder_constraints (TREE_TYPE (t), constr))
38787 tree d = TREE_VALUE (t);
38788 if (TREE_CODE (d) == PARM_DECL)
38789 /* Return the TEMPLATE_PARM_INDEX. */
38790 d = DECL_INITIAL (d);
38791 return d;
38793 t = TREE_CHAIN (t);
38797 /* We are either continuing a function template that already contains implicit
38798 template parameters, creating a new fully-implicit function template, or
38799 extending an existing explicit function template with implicit template
38800 parameters. */
38802 cp_binding_level *const entry_scope = current_binding_level;
38804 bool become_template = false;
38805 cp_binding_level *parent_scope = 0;
38807 if (parser->implicit_template_scope)
38809 gcc_assert (parser->implicit_template_parms);
38811 current_binding_level = parser->implicit_template_scope;
38813 else
38815 /* Roll back to the existing template parameter scope (in the case of
38816 extending an explicit function template) or introduce a new template
38817 parameter scope ahead of the function parameter scope (or class scope
38818 in the case of out-of-line member definitions). The function scope is
38819 added back after template parameter synthesis below. */
38821 cp_binding_level *scope = entry_scope;
38823 while (scope->kind == sk_function_parms)
38825 parent_scope = scope;
38826 scope = scope->level_chain;
38828 if (current_class_type && !LAMBDA_TYPE_P (current_class_type))
38830 /* If not defining a class, then any class scope is a scope level in
38831 an out-of-line member definition. In this case simply wind back
38832 beyond the first such scope to inject the template parameter list.
38833 Otherwise wind back to the class being defined. The latter can
38834 occur in class member friend declarations such as:
38836 class A {
38837 void foo (auto);
38839 class B {
38840 friend void A::foo (auto);
38843 The template parameter list synthesized for the friend declaration
38844 must be injected in the scope of 'B'. This can also occur in
38845 erroneous cases such as:
38847 struct A {
38848 struct B {
38849 void foo (auto);
38851 void B::foo (auto) {}
38854 Here the attempted definition of 'B::foo' within 'A' is ill-formed
38855 but, nevertheless, the template parameter list synthesized for the
38856 declarator should be injected into the scope of 'A' as if the
38857 ill-formed template was specified explicitly. */
38859 while (scope->kind == sk_class && !scope->defining_class_p)
38861 parent_scope = scope;
38862 scope = scope->level_chain;
38866 current_binding_level = scope;
38868 if (scope->kind != sk_template_parms
38869 || !function_being_declared_is_template_p (parser))
38871 /* Introduce a new template parameter list for implicit template
38872 parameters. */
38874 become_template = true;
38876 parser->implicit_template_scope
38877 = begin_scope (sk_template_parms, NULL);
38879 ++processing_template_decl;
38881 parser->fully_implicit_function_template_p = true;
38882 ++parser->num_template_parameter_lists;
38884 else
38886 /* Synthesize implicit template parameters at the end of the explicit
38887 template parameter list. */
38889 gcc_assert (current_template_parms);
38891 parser->implicit_template_scope = scope;
38893 tree v = INNERMOST_TEMPLATE_PARMS (current_template_parms);
38894 parser->implicit_template_parms
38895 = TREE_VEC_ELT (v, TREE_VEC_LENGTH (v) - 1);
38899 /* Synthesize a new template parameter and track the current template
38900 parameter chain with implicit_template_parms. */
38902 tree proto = constr ? DECL_INITIAL (constr) : NULL_TREE;
38903 tree synth_id = make_generic_type_name ();
38904 tree synth_tmpl_parm;
38905 bool non_type = false;
38907 if (proto == NULL_TREE || TREE_CODE (proto) == TYPE_DECL)
38908 synth_tmpl_parm
38909 = finish_template_type_parm (class_type_node, synth_id);
38910 else if (TREE_CODE (proto) == TEMPLATE_DECL)
38911 synth_tmpl_parm
38912 = finish_constrained_template_template_parm (proto, synth_id);
38913 else
38915 synth_tmpl_parm = copy_decl (proto);
38916 DECL_NAME (synth_tmpl_parm) = synth_id;
38917 non_type = true;
38920 // Attach the constraint to the parm before processing.
38921 tree node = build_tree_list (NULL_TREE, synth_tmpl_parm);
38922 TREE_TYPE (node) = constr;
38923 tree new_parm
38924 = process_template_parm (parser->implicit_template_parms,
38925 input_location,
38926 node,
38927 /*non_type=*/non_type,
38928 /*param_pack=*/false);
38930 // Chain the new parameter to the list of implicit parameters.
38931 if (parser->implicit_template_parms)
38932 parser->implicit_template_parms
38933 = TREE_CHAIN (parser->implicit_template_parms);
38934 else
38935 parser->implicit_template_parms = new_parm;
38937 tree new_decl = getdecls ();
38938 if (non_type)
38939 /* Return the TEMPLATE_PARM_INDEX, not the PARM_DECL. */
38940 new_decl = DECL_INITIAL (new_decl);
38942 /* If creating a fully implicit function template, start the new implicit
38943 template parameter list with this synthesized type, otherwise grow the
38944 current template parameter list. */
38946 if (become_template)
38948 parent_scope->level_chain = current_binding_level;
38950 tree new_parms = make_tree_vec (1);
38951 TREE_VEC_ELT (new_parms, 0) = parser->implicit_template_parms;
38952 current_template_parms = tree_cons (size_int (processing_template_decl),
38953 new_parms, current_template_parms);
38955 else
38957 tree& new_parms = INNERMOST_TEMPLATE_PARMS (current_template_parms);
38958 int new_parm_idx = TREE_VEC_LENGTH (new_parms);
38959 new_parms = grow_tree_vec (new_parms, new_parm_idx + 1);
38960 TREE_VEC_ELT (new_parms, new_parm_idx) = parser->implicit_template_parms;
38963 // If the new parameter was constrained, we need to add that to the
38964 // constraints in the template parameter list.
38965 if (tree req = TEMPLATE_PARM_CONSTRAINTS (tree_last (new_parm)))
38967 tree reqs = TEMPLATE_PARMS_CONSTRAINTS (current_template_parms);
38968 reqs = conjoin_constraints (reqs, req);
38969 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) = reqs;
38972 current_binding_level = entry_scope;
38974 return new_decl;
38977 /* Finish the declaration of a fully implicit function template. Such a
38978 template has no explicit template parameter list so has not been through the
38979 normal template head and tail processing. synthesize_implicit_template_parm
38980 tries to do the head; this tries to do the tail. MEMBER_DECL_OPT should be
38981 provided if the declaration is a class member such that its template
38982 declaration can be completed. If MEMBER_DECL_OPT is provided the finished
38983 form is returned. Otherwise NULL_TREE is returned. */
38985 static tree
38986 finish_fully_implicit_template (cp_parser *parser, tree member_decl_opt)
38988 gcc_assert (parser->fully_implicit_function_template_p);
38990 if (member_decl_opt && member_decl_opt != error_mark_node
38991 && DECL_VIRTUAL_P (member_decl_opt))
38993 error_at (DECL_SOURCE_LOCATION (member_decl_opt),
38994 "implicit templates may not be %<virtual%>");
38995 DECL_VIRTUAL_P (member_decl_opt) = false;
38998 if (member_decl_opt)
38999 member_decl_opt = finish_member_template_decl (member_decl_opt);
39000 end_template_decl ();
39002 parser->fully_implicit_function_template_p = false;
39003 --parser->num_template_parameter_lists;
39005 return member_decl_opt;
39008 #include "gt-cp-parser.h"